src/Entity/Environment.php line 146

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\HttpClient\HttpClient;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. /**
  9.  * @ORM\Entity(repositoryClass="App\Repository\EnvironmentRepository")
  10.  */
  11. class Environment
  12. {
  13.     /**
  14.      * @ORM\Id()
  15.      * @ORM\GeneratedValue()
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\ManyToOne(targetEntity="App\Entity\Project", inversedBy="environment")
  21.      * @ORM\JoinColumn(nullable=false)
  22.      */
  23.     private $project;
  24.     /**
  25.      * @ORM\ManyToOne(targetEntity="App\Entity\EnvironmentType")
  26.      * @ORM\JoinColumn(nullable=false)
  27.      */
  28.     private $type;
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity="App\Entity\Config")
  31.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  32.      */
  33.     private $server;
  34.     /**
  35.      * @ORM\OneToMany(targetEntity="App\Entity\EnvironmentOption", mappedBy="environment", orphanRemoval=true, cascade={"persist", "remove"})
  36.      */
  37.     private $environmentOptions;
  38.     /**
  39.      * @ORM\OneToMany(targetEntity="App\Entity\Job", mappedBy="environment", orphanRemoval=true)
  40.      * @ORM\OrderBy({"id" = "DESC"})
  41.      */
  42.     private $jobs;
  43.     /**
  44.      * @ORM\Column(type="boolean")
  45.      */
  46.     private $autoStop;
  47.     /**
  48.      * @ORM\ManyToOne(targetEntity="App\Entity\Config")
  49.      * @ORM\JoinColumn(nullable=false)
  50.      */
  51.     private $monitoring;
  52.     /**
  53.      * @ORM\Column(type="string", length=255, nullable=true)
  54.      */
  55.     private $fqdn;
  56.     /**
  57.      * @ORM\Column(type="datetime", nullable=true)
  58.      */
  59.     private $lastAutoStopChange;
  60.     /**
  61.      * @ORM\OneToMany(targetEntity="App\Entity\DeployTag", mappedBy="environment")
  62.      */
  63.     private $deployTags;
  64.     /**
  65.      * @ORM\Column(type="boolean", options={"default":0})
  66.      */
  67.     private $autoRestart;
  68.     /**
  69.      * @ORM\ManyToMany(targetEntity=Config::class)
  70.      */
  71.     private $modules;
  72.     /**
  73.      * @ORM\OneToMany(targetEntity=EnvironmentMonitor::class, mappedBy="environment", orphanRemoval=true, cascade={"persist"})
  74.      */
  75.     private $environmentMonitors;
  76.     public function __construct()
  77.     {
  78.         $this->environmentOptions = new ArrayCollection();
  79.         $this->jobs = new ArrayCollection();
  80.         $this->deployTags = new ArrayCollection();
  81.         $this->modules = new ArrayCollection();
  82.         $this->environmentMonitors = new ArrayCollection();
  83.         $this->debugMode false;
  84.         $this->lastDebugModeChange = new \DateTime();
  85.     }
  86.     public function getId(): ?int
  87.     {
  88.         return $this->id;
  89.     }
  90.     public function getProject(): ?Project
  91.     {
  92.         return $this->project;
  93.     }
  94.     public function setProject(?Project $project): self
  95.     {
  96.         $this->project $project;
  97.         return $this;
  98.     }
  99.     public function getType(): ?EnvironmentType
  100.     {
  101.         return $this->type;
  102.     }
  103.     public function setType(?EnvironmentType $type): self
  104.     {
  105.         $this->type $type;
  106.         return $this;
  107.     }
  108.     public function getServer(): ?Config
  109.     {
  110.         return $this->server;
  111.     }
  112.     public function setServer(?Config $server): self
  113.     {
  114.         $this->server $server;
  115.         return $this;
  116.     }
  117.     
  118.     public function __toString() {
  119.         foreach($this->getModules() as $module) {
  120.             $modules[] = $module->getValue();
  121.         }
  122.         return $this->project->getName()." ".strtoupper($this->type->getName())." on ".$this->server->getValue()." (".implode($modules", ").")";
  123.     }
  124.     /**
  125.      * @var \DateTime $created
  126.      *
  127.      * @Gedmo\Timestampable(on="create")
  128.      * @ORM\Column(type="datetime")
  129.      */
  130.     private $created;
  131.     /**
  132.      * @var \DateTime $updated
  133.      *
  134.      * @Gedmo\Timestampable(on="update")
  135.      * @ORM\Column(type="datetime")
  136.      */
  137.     private $updated;
  138.     /**
  139.      * @ORM\Column(type="boolean")
  140.      */
  141.     private $debugMode;
  142.     /**
  143.      * @ORM\Column(type="datetime")
  144.      */
  145.     private $lastDebugModeChange;
  146.     /**
  147.      * @return Collection|EnvironmentOption[]
  148.      */
  149.     public function getEnvironmentOptions(): Collection
  150.     {
  151.         return $this->environmentOptions;
  152.     }
  153.     public function addEnvironmentOption(EnvironmentOption $environmentOption): self
  154.     {
  155.         if (!$this->environmentOptions->contains($environmentOption)) {
  156.             $this->environmentOptions[] = $environmentOption;
  157.             $environmentOption->setEnvironment($this);
  158.         }
  159.         return $this;
  160.     }
  161.     public function removeEnvironmentOption(EnvironmentOption $environmentOption): self
  162.     {
  163.         if ($this->environmentOptions->contains($environmentOption)) {
  164.             $this->environmentOptions->removeElement($environmentOption);
  165.             // set the owning side to null (unless already changed)
  166.             if ($environmentOption->getEnvironment() === $this) {
  167.                 $environmentOption->setEnvironment(null);
  168.             }
  169.         }
  170.         return $this;
  171.     }
  172.     
  173.     public function getEnvironmentOption(string $type): ?EnvironmentOption    {
  174.         foreach($this->environmentOptions as $environmentOption) {
  175.             if($environmentOption->getOptionType()->getName() == $type) {
  176.                 return $environmentOption;
  177.             }
  178.         }
  179.         return null;
  180.     }
  181.     public function memoryLimit(): ?EnvironmentOption {
  182.        return $this->getEnvironmentOption("java.memoryLimit");
  183.     }
  184.     public function cpuLimit(): ?EnvironmentOption {
  185.        return $this->getEnvironmentOption("java.cpuLimit");
  186.     }
  187.     /**
  188.      * @return Collection|Job[]
  189.      */
  190.     public function getJobs(): Collection
  191.     {
  192.         return $this->jobs;
  193.     }
  194.     public function isDbSet() {
  195.         $ret false;
  196.         foreach($this->getEnvironmentOptions() AS $envOption) {
  197.             if($envOption->getOptionType()->getName() == "jdbc.url" && $envOption->getValue() != "") {
  198.                 $ret true;
  199.                 break;
  200.             } elseif($envOption->getOptionType()->getName() == "jdbc.admin.url" && $envOption->getValue() != "") {
  201.                 $ret true;
  202.                 break;
  203.             }
  204.  
  205.         }
  206.         return $ret;
  207.     }
  208.     
  209.     public function getLinks() {
  210.         $links = array();
  211.         $isAdmin false;
  212.         $isAdmin2 false;
  213.         $isWidget false;
  214.         $isResource false;
  215.         $botEnvName $this->getProject()->getBotname();
  216.         if($this->getType()->getName() != "prod") {
  217.                 $botEnvName .= $this->getType()->getName();
  218.         }
  219.         $server $this->getServer()->getValue();
  220.         $prefix "";
  221.         foreach($this->getEnvironmentOptions() AS $envOption) {
  222.             if($envOption->getOptionType()->getName() == "bot.fqdn") {
  223.                 $server $envOption->getValue();
  224.             } elseif($envOption->getOptionType()->getName() == "bot.prefix") {
  225.                 $prefix $envOption->getValue();
  226.             }
  227.             if(strpos($envOption->getOptionType()->getName(), "links.") !== false) {
  228.                 $links[] = array("type" => $envOption->getOptionType()->getName(), "link" => $envOption->getValue());
  229.                 if($envOption->getOptionType()->getName() == "links.admin") {
  230.                     $isAdmin true;
  231.                 } elseif($envOption->getOptionType()->getName() == "links.admin2") {
  232.                     $isAdmin2 true;
  233.                 } elseif($envOption->getOptionType()->getName() == "links.widget") {
  234.                     $isWidget true;
  235.                 } elseif($envOption->getOptionType()->getName() == "links.resource") {
  236.                     $isResource true;
  237.                 }
  238.             }
  239.         }
  240.         $modules = [];
  241.         foreach($this->getModules() as $module) {
  242.             $modules[] = $module->getValue();
  243.         }
  244.         if(!$isAdmin) {
  245.             if($this->getProject()->getType()->getValue() != "phoenix") {
  246.                 $links[] = array("type" => "links.admin""link" => "https://".$server."/admin".$botEnvName);
  247.             } elseif (in_array("naomi"$modules)) {
  248.                 $links[] = array("type" => "links.admin""link" => "https://".$server."/admin/login");
  249.             }
  250.         }
  251.         if(!$isAdmin2 && ($this->getProject()->getType()->getValue() == "cheq" || $this->getProject()->getType()->getValue() == "cheqcrisis")) {
  252.             $links[] = array("type" => "links.admin2""link" => "https://".$server."/admin2".$botEnvName);
  253.         }
  254.         if(!$isWidget) {
  255.             if(in_array("phoenix"$modules) ||in_array("bot"$modules)) {
  256.                 $links[] = array("type" => "links.widget""link" => "https://resource01.botoffice.net/".$this->getProject()->getBotname()."/".$this->getType()->getName()."/chat/".$botEnvName.".html");
  257.             }
  258.         }
  259.         if(!$isResource) {
  260.             if(in_array("resourceproxy"$modules)) {
  261.                 $links[] = array("type" => "links.resoruce""link" => "https://".$server."/".($prefix != "" $prefix."/" "")."api/docs");
  262.             }
  263.         }
  264.         if(in_array("phoenix"$modules) ||in_array("bot"$modules)) {
  265.             $links[] = array("type" => "links.jmeter""link" => "https://jmeter.talkabot.net/".$this->getEnvironmentName()."/");
  266.         }
  267.         return $links;
  268.     }
  269.     
  270.     public function getLatestDeploy()
  271.     {
  272.         $ret = ["version" => ""];
  273.         $latestJobs $this->getJobs();
  274.         foreach($latestJobs as $job) {
  275.             $jobType $job->getType()->getId();
  276.             if($jobType == || $jobType == 28 || $jobType == 45 || $jobType == 74 || $jobType == 124 || $jobType == 138 || $jobType == 162 || $jobType == 181 || $jobType == 187 || $jobType == 224) {
  277.                 $params json_decode($job->getParams(), true);
  278.                 if(array_key_exists("version"$params)) {
  279.                     $ret["version"] = $params["version"];
  280.                 }
  281.                 break;
  282.             }
  283.         }
  284.         $server $this->getServer()->getValue();
  285.         foreach($this->getEnvironmentOptions() AS $envOption) {
  286.             if($envOption->getOptionType()->getName() == "bot.fqdn") {
  287.                 $server $envOption->getValue();
  288.                 break;
  289.             }
  290.         }
  291.         $botEnvName $this->getProject()->getBotname();
  292.         if($this->getType()->getName() != "prod") {
  293.                 $botEnvName .= $this->getType()->getName();
  294.         }
  295.         $ret["server"] = $server;
  296.         $ret["botEnvName"] = $botEnvName;
  297.         return $ret;
  298.         
  299.         
  300.             
  301.         // try {
  302.             // $httpClient = HttpClient::create(['headers' => [
  303.                 // 'User-Agent' => 'Talk-A-Bot Dashboard 2.0',
  304.                 // 'Accept-Encoding' => 'application/json',
  305.             // ]]);
  306.             // $botEnvName = $this->getProject()->getBotname();
  307.             // if($this->getType()->getName() != "prod") {
  308.                     // $botEnvName .= $this->getType()->getName();
  309.             // }
  310.             
  311.             // $response = $httpClient->request("POST", "https://".$server."/dockerapi", [
  312.                 // 'json' => [
  313.                     // "botEnvName" => $botEnvName,
  314.                     // "auth" => "49D0910F77ECEC742F886E21D6A6C9A2A7EFBBD80C62FBDF1D0224BFE12B0C8C1DD20C6E1FC69763943832F0E78378674B1354AC1410B85F53B107B420613309"
  315.                 // ],
  316.                 // 'timeout' => 1.0
  317.             // ]);
  318.             // $needGuess = false;
  319.             // $ret = array();
  320.             // if (200 !== $response->getStatusCode()) {
  321.                 // $ret["status"] = "N.A. - HTTP Error";
  322.                 // $ret["deploy"] = "";
  323.                 // $ret["bg"] = "bg-danger";
  324.                 // $needGuess = true;
  325.                 // $ret["version"] = "";
  326.                 // $ret["coreversion"] = "";
  327.                 // $ret["buildtime"] = "";
  328.             // } else {
  329.                 // $content = json_decode($response->getContent(false), true);
  330.                 // if(array_key_exists("Status", $content)) {
  331.                     // $ret["status"] = "It's ".$content["State"]." and is, ".$content["Status"];
  332.                     // $ret["deploy"] = "\nReal Latest Deploy: ".$content["Image"];
  333.                     // $ret["bg"] = "bg-success";
  334.                     // $ret["version"] = $content["Image"];
  335.                     // if(isset($content["coreVersion"])) {
  336.                         // $ret["coreversion"] = $content["coreVersion"];
  337.                         // $ret["buildtime"] = $content["buildTime"];
  338.                         // // $date = DateTime::createFromFormat('U', ($content["buildTime"]/1000), new DateTimeZone('Europe/Budapest'));
  339.                         // // $ret["buildtime"] = $date->format('Y-m-d H:i:sP');
  340.                     // } else {
  341.                         // $ret["coreversion"] = "";
  342.                         // $ret["buildtime"] = "";
  343.                     // }
  344.                 // } else {
  345.                     // $ret["status"] = $content[0];
  346.                     // $ret["deploy"] = "";
  347.                     // $needGuess = true;
  348.                     // $ret["bg"] = "bg-warning";
  349.                     // $ret["version"] = "";
  350.                     // $ret["coreversion"] = "";
  351.                     // $ret["buildtime"] = "";
  352.                 // }
  353.             // }
  354.             // if($needGuess) {
  355.                 // $latestJobs = $this->getJobs();
  356.                 // foreach($latestJobs as $job) {
  357.                     // if($job->getType()->getId() != 6) {
  358.                             // $ret["deploy"] = "Couldn't guess latest deploy";
  359.                             // $ret["version"] = "";
  360.                             // $ret["coreversion"] = "";
  361.                             // $ret["buildtime"] = "";
  362.                         // continue;
  363.                     // }
  364.                     // if($job) {
  365.                         // $params = json_decode($job->getParams(), true);
  366.                         // if(array_key_exists("version", $params)) {
  367.                             // $ret["deploy"] = "\nGuessed Latest Deploy: ".$params["version"];
  368.                             // $ret["version"] = $params["version"];
  369.                             // $ret["coreversion"] = "";
  370.                             // $ret["buildtime"] = "";
  371.                             // break;
  372.                         // }
  373.                     // }
  374.                     // // $ret["deploy"] = $job->getId();
  375.                 // }
  376.             // }
  377.         // } catch (\Exception $e) {
  378.             // $ret = array();
  379.             // $ret["status"] = "N.A. - HTTP Timeout";
  380.             // $ret["deploy"] = "";
  381.             // $ret["bg"] = "bg-danger";
  382.             // $ret["version"] = "";
  383.             // $ret["coreversion"] = "";
  384.             // $ret["buildtime"] = "";
  385.         // } catch (\Throwable $e) {
  386.             // $ret = array();
  387.             // $ret["status"] = "N.A. - HTTP Timeout";
  388.             // $ret["deploy"] = "";
  389.             // $ret["bg"] = "bg-danger";
  390.             // $ret["version"] = "";
  391.             // $ret["coreversion"] = "";
  392.             // $ret["buildtime"] = "";
  393.         // }
  394.         return $ret;
  395.     }
  396.     public function addJob(Job $job): self
  397.     {
  398.         if (!$this->jobs->contains($job)) {
  399.             $this->jobs[] = $job;
  400.             $job->setEnvironment($this);
  401.         }
  402.         return $this;
  403.     }
  404.     public function removeJob(Job $job): self
  405.     {
  406.         if ($this->jobs->contains($job)) {
  407.             $this->jobs->removeElement($job);
  408.             // set the owning side to null (unless already changed)
  409.             if ($job->getEnvironment() === $this) {
  410.                 $job->setEnvironment(null);
  411.             }
  412.         }
  413.         return $this;
  414.     }
  415.     public function getAutoStop(): ?bool
  416.     {
  417.         return $this->autoStop;
  418.     }
  419.     public function setAutoStop(bool $autoStop): self
  420.     {
  421.         $this->autoStop $autoStop;
  422.         return $this;
  423.     }
  424.     public function getMonitoring(): ?Config
  425.     {
  426.         return $this->monitoring;
  427.     }
  428.     public function setMonitoring(?Config $monitoring): self
  429.     {
  430.         $this->monitoring $monitoring;
  431.         return $this;
  432.     }
  433.     public function getFqdn(): ?string
  434.     {
  435.         return $this->fqdn;
  436.     }
  437.     public function setFqdn(?string $fqdn): self
  438.     {
  439.         $this->fqdn $fqdn;
  440.         return $this;
  441.     }
  442.     public function getLastAutoStopChange(): ?\DateTimeInterface
  443.     {
  444.         return $this->lastAutoStopChange;
  445.     }
  446.     public function setLastAutoStopChange(?\DateTimeInterface $lastAutoStopChange): self
  447.     {
  448.         $this->lastAutoStopChange $lastAutoStopChange;
  449.         return $this;
  450.     }
  451.     /**
  452.      * @return Collection|DeployTag[]
  453.      */
  454.     public function getDeployTags(): Collection
  455.     {
  456.         return $this->deployTags;
  457.     }
  458.     public function addDeployTag(DeployTag $deployTag): self
  459.     {
  460.         if (!$this->deployTags->contains($deployTag)) {
  461.             $this->deployTags[] = $deployTag;
  462.             $deployTag->setEnvironment($this);
  463.         }
  464.         return $this;
  465.     }
  466.     public function removeDeployTag(DeployTag $deployTag): self
  467.     {
  468.         if ($this->deployTags->contains($deployTag)) {
  469.             $this->deployTags->removeElement($deployTag);
  470.             // set the owning side to null (unless already changed)
  471.             if ($deployTag->getEnvironment() === $this) {
  472.                 $deployTag->setEnvironment(null);
  473.             }
  474.         }
  475.         return $this;
  476.     }
  477.     public function getAutoRestart(): ?bool
  478.     {
  479.         return $this->autoRestart;
  480.     }
  481.     public function setAutoRestart(bool $autoRestart): self
  482.     {
  483.         $this->autoRestart $autoRestart;
  484.         return $this;
  485.     }
  486.     /**
  487.      * @return Collection|Config[]
  488.      */
  489.     public function getModules(): Collection
  490.     {
  491.         return $this->modules;
  492.     }
  493.     public function addModule(Config $module): self
  494.     {
  495.         if (!$this->modules->contains($module)) {
  496.             $this->modules[] = $module;
  497.         }
  498.         return $this;
  499.     }
  500.     public function removeModule(Config $module): self
  501.     {
  502.         $this->modules->removeElement($module);
  503.         return $this;
  504.     }
  505.     /**
  506.      * @return Collection|EnvironmentMonitor[]
  507.      */
  508.     public function getEnvironmentMonitors(): Collection
  509.     {
  510.         return $this->environmentMonitors;
  511.     }
  512.     public function addEnvironmentMonitor(EnvironmentMonitor $environmentMonitor): self
  513.     {
  514.         if (!$this->environmentMonitors->contains($environmentMonitor)) {
  515.             $this->environmentMonitors[] = $environmentMonitor;
  516.             $environmentMonitor->setEnvironment($this);
  517.         }
  518.         return $this;
  519.     }
  520.     public function removeEnvironmentMonitor(EnvironmentMonitor $environmentMonitor): self
  521.     {
  522.         if ($this->environmentMonitors->removeElement($environmentMonitor)) {
  523.             // set the owning side to null (unless already changed)
  524.             if ($environmentMonitor->getEnvironment() === $this) {
  525.                 $environmentMonitor->setEnvironment(null);
  526.             }
  527.         }
  528.         return $this;
  529.     }
  530.     public function getEnvironmentName(): string
  531.     {
  532.         $botEnvName $this->getProject()->getBotname();
  533.         if($this->getType()->getName() != "prod") {
  534.             $botEnvName .= $this->getType()->getName();
  535.         }
  536.         return $botEnvName;
  537.     }
  538.     public function getCreated()
  539.     {
  540.         return $this->created;
  541.     }
  542.     public function getUpdated()
  543.     {
  544.         return $this->updated;
  545.     }
  546.     public function getDebugMode(): ?bool
  547.     {
  548.         if($this->debugMode == null) {
  549.             $this->debugMode false;
  550.         }
  551.         return $this->debugMode;
  552.     }
  553.     public function setDebugMode(?bool $debugMode): self
  554.     {
  555.         if($debugMode == null) {
  556.             $debugMode false;
  557.         }
  558.         $this->debugMode $debugMode;
  559.         return $this;
  560.     }
  561.     public function getLastDebugModeChange(): ?\DateTimeInterface
  562.     {
  563.         return $this->lastDebugModeChange;
  564.     }
  565.     public function setLastDebugModeChange(?\DateTimeInterface $lastDebugModeChange): self
  566.     {
  567.         if($lastDebugModeChange == null) {
  568.             $lastDebugModeChange = new \DateTime();
  569.         }
  570.         $this->lastDebugModeChange $lastDebugModeChange;
  571.         return $this;
  572.     }
  573. }