<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpClient\HttpClient;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass="App\Repository\EnvironmentRepository")
*/
class Environment
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Project", inversedBy="environment")
* @ORM\JoinColumn(nullable=false)
*/
private $project;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\EnvironmentType")
* @ORM\JoinColumn(nullable=false)
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Config")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $server;
/**
* @ORM\OneToMany(targetEntity="App\Entity\EnvironmentOption", mappedBy="environment", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $environmentOptions;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Job", mappedBy="environment", orphanRemoval=true)
* @ORM\OrderBy({"id" = "DESC"})
*/
private $jobs;
/**
* @ORM\Column(type="boolean")
*/
private $autoStop;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Config")
* @ORM\JoinColumn(nullable=false)
*/
private $monitoring;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $fqdn;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastAutoStopChange;
/**
* @ORM\OneToMany(targetEntity="App\Entity\DeployTag", mappedBy="environment")
*/
private $deployTags;
/**
* @ORM\Column(type="boolean", options={"default":0})
*/
private $autoRestart;
/**
* @ORM\ManyToMany(targetEntity=Config::class)
*/
private $modules;
/**
* @ORM\OneToMany(targetEntity=EnvironmentMonitor::class, mappedBy="environment", orphanRemoval=true, cascade={"persist"})
*/
private $environmentMonitors;
public function __construct()
{
$this->environmentOptions = new ArrayCollection();
$this->jobs = new ArrayCollection();
$this->deployTags = new ArrayCollection();
$this->modules = new ArrayCollection();
$this->environmentMonitors = new ArrayCollection();
$this->debugMode = false;
$this->lastDebugModeChange = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getProject(): ?Project
{
return $this->project;
}
public function setProject(?Project $project): self
{
$this->project = $project;
return $this;
}
public function getType(): ?EnvironmentType
{
return $this->type;
}
public function setType(?EnvironmentType $type): self
{
$this->type = $type;
return $this;
}
public function getServer(): ?Config
{
return $this->server;
}
public function setServer(?Config $server): self
{
$this->server = $server;
return $this;
}
public function __toString() {
foreach($this->getModules() as $module) {
$modules[] = $module->getValue();
}
return $this->project->getName()." ".strtoupper($this->type->getName())." on ".$this->server->getValue()." (".implode($modules, ", ").")";
}
/**
* @var \DateTime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var \DateTime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated;
/**
* @ORM\Column(type="boolean")
*/
private $debugMode;
/**
* @ORM\Column(type="datetime")
*/
private $lastDebugModeChange;
/**
* @return Collection|EnvironmentOption[]
*/
public function getEnvironmentOptions(): Collection
{
return $this->environmentOptions;
}
public function addEnvironmentOption(EnvironmentOption $environmentOption): self
{
if (!$this->environmentOptions->contains($environmentOption)) {
$this->environmentOptions[] = $environmentOption;
$environmentOption->setEnvironment($this);
}
return $this;
}
public function removeEnvironmentOption(EnvironmentOption $environmentOption): self
{
if ($this->environmentOptions->contains($environmentOption)) {
$this->environmentOptions->removeElement($environmentOption);
// set the owning side to null (unless already changed)
if ($environmentOption->getEnvironment() === $this) {
$environmentOption->setEnvironment(null);
}
}
return $this;
}
public function getEnvironmentOption(string $type): ?EnvironmentOption {
foreach($this->environmentOptions as $environmentOption) {
if($environmentOption->getOptionType()->getName() == $type) {
return $environmentOption;
}
}
return null;
}
public function memoryLimit(): ?EnvironmentOption {
return $this->getEnvironmentOption("java.memoryLimit");
}
public function cpuLimit(): ?EnvironmentOption {
return $this->getEnvironmentOption("java.cpuLimit");
}
/**
* @return Collection|Job[]
*/
public function getJobs(): Collection
{
return $this->jobs;
}
public function isDbSet() {
$ret = false;
foreach($this->getEnvironmentOptions() AS $envOption) {
if($envOption->getOptionType()->getName() == "jdbc.url" && $envOption->getValue() != "") {
$ret = true;
break;
} elseif($envOption->getOptionType()->getName() == "jdbc.admin.url" && $envOption->getValue() != "") {
$ret = true;
break;
}
}
return $ret;
}
public function getLinks() {
$links = array();
$isAdmin = false;
$isAdmin2 = false;
$isWidget = false;
$isResource = false;
$botEnvName = $this->getProject()->getBotname();
if($this->getType()->getName() != "prod") {
$botEnvName .= $this->getType()->getName();
}
$server = $this->getServer()->getValue();
$prefix = "";
foreach($this->getEnvironmentOptions() AS $envOption) {
if($envOption->getOptionType()->getName() == "bot.fqdn") {
$server = $envOption->getValue();
} elseif($envOption->getOptionType()->getName() == "bot.prefix") {
$prefix = $envOption->getValue();
}
if(strpos($envOption->getOptionType()->getName(), "links.") !== false) {
$links[] = array("type" => $envOption->getOptionType()->getName(), "link" => $envOption->getValue());
if($envOption->getOptionType()->getName() == "links.admin") {
$isAdmin = true;
} elseif($envOption->getOptionType()->getName() == "links.admin2") {
$isAdmin2 = true;
} elseif($envOption->getOptionType()->getName() == "links.widget") {
$isWidget = true;
} elseif($envOption->getOptionType()->getName() == "links.resource") {
$isResource = true;
}
}
}
$modules = [];
foreach($this->getModules() as $module) {
$modules[] = $module->getValue();
}
if(!$isAdmin) {
if($this->getProject()->getType()->getValue() != "phoenix") {
$links[] = array("type" => "links.admin", "link" => "https://".$server."/admin".$botEnvName);
} elseif (in_array("naomi", $modules)) {
$links[] = array("type" => "links.admin", "link" => "https://".$server."/admin/login");
}
}
if(!$isAdmin2 && ($this->getProject()->getType()->getValue() == "cheq" || $this->getProject()->getType()->getValue() == "cheqcrisis")) {
$links[] = array("type" => "links.admin2", "link" => "https://".$server."/admin2".$botEnvName);
}
if(!$isWidget) {
if(in_array("phoenix", $modules) ||in_array("bot", $modules)) {
$links[] = array("type" => "links.widget", "link" => "https://resource01.botoffice.net/".$this->getProject()->getBotname()."/".$this->getType()->getName()."/chat/".$botEnvName.".html");
}
}
if(!$isResource) {
if(in_array("resourceproxy", $modules)) {
$links[] = array("type" => "links.resoruce", "link" => "https://".$server."/".($prefix != "" ? $prefix."/" : "")."api/docs");
}
}
if(in_array("phoenix", $modules) ||in_array("bot", $modules)) {
$links[] = array("type" => "links.jmeter", "link" => "https://jmeter.talkabot.net/".$this->getEnvironmentName()."/");
}
return $links;
}
public function getLatestDeploy()
{
$ret = ["version" => ""];
$latestJobs = $this->getJobs();
foreach($latestJobs as $job) {
$jobType = $job->getType()->getId();
if($jobType == 6 || $jobType == 28 || $jobType == 45 || $jobType == 74 || $jobType == 124 || $jobType == 138 || $jobType == 162 || $jobType == 181 || $jobType == 187 || $jobType == 224) {
$params = json_decode($job->getParams(), true);
if(array_key_exists("version", $params)) {
$ret["version"] = $params["version"];
}
break;
}
}
$server = $this->getServer()->getValue();
foreach($this->getEnvironmentOptions() AS $envOption) {
if($envOption->getOptionType()->getName() == "bot.fqdn") {
$server = $envOption->getValue();
break;
}
}
$botEnvName = $this->getProject()->getBotname();
if($this->getType()->getName() != "prod") {
$botEnvName .= $this->getType()->getName();
}
$ret["server"] = $server;
$ret["botEnvName"] = $botEnvName;
return $ret;
// try {
// $httpClient = HttpClient::create(['headers' => [
// 'User-Agent' => 'Talk-A-Bot Dashboard 2.0',
// 'Accept-Encoding' => 'application/json',
// ]]);
// $botEnvName = $this->getProject()->getBotname();
// if($this->getType()->getName() != "prod") {
// $botEnvName .= $this->getType()->getName();
// }
// $response = $httpClient->request("POST", "https://".$server."/dockerapi", [
// 'json' => [
// "botEnvName" => $botEnvName,
// "auth" => "49D0910F77ECEC742F886E21D6A6C9A2A7EFBBD80C62FBDF1D0224BFE12B0C8C1DD20C6E1FC69763943832F0E78378674B1354AC1410B85F53B107B420613309"
// ],
// 'timeout' => 1.0
// ]);
// $needGuess = false;
// $ret = array();
// if (200 !== $response->getStatusCode()) {
// $ret["status"] = "N.A. - HTTP Error";
// $ret["deploy"] = "";
// $ret["bg"] = "bg-danger";
// $needGuess = true;
// $ret["version"] = "";
// $ret["coreversion"] = "";
// $ret["buildtime"] = "";
// } else {
// $content = json_decode($response->getContent(false), true);
// if(array_key_exists("Status", $content)) {
// $ret["status"] = "It's ".$content["State"]." and is, ".$content["Status"];
// $ret["deploy"] = "\nReal Latest Deploy: ".$content["Image"];
// $ret["bg"] = "bg-success";
// $ret["version"] = $content["Image"];
// if(isset($content["coreVersion"])) {
// $ret["coreversion"] = $content["coreVersion"];
// $ret["buildtime"] = $content["buildTime"];
// // $date = DateTime::createFromFormat('U', ($content["buildTime"]/1000), new DateTimeZone('Europe/Budapest'));
// // $ret["buildtime"] = $date->format('Y-m-d H:i:sP');
// } else {
// $ret["coreversion"] = "";
// $ret["buildtime"] = "";
// }
// } else {
// $ret["status"] = $content[0];
// $ret["deploy"] = "";
// $needGuess = true;
// $ret["bg"] = "bg-warning";
// $ret["version"] = "";
// $ret["coreversion"] = "";
// $ret["buildtime"] = "";
// }
// }
// if($needGuess) {
// $latestJobs = $this->getJobs();
// foreach($latestJobs as $job) {
// if($job->getType()->getId() != 6) {
// $ret["deploy"] = "Couldn't guess latest deploy";
// $ret["version"] = "";
// $ret["coreversion"] = "";
// $ret["buildtime"] = "";
// continue;
// }
// if($job) {
// $params = json_decode($job->getParams(), true);
// if(array_key_exists("version", $params)) {
// $ret["deploy"] = "\nGuessed Latest Deploy: ".$params["version"];
// $ret["version"] = $params["version"];
// $ret["coreversion"] = "";
// $ret["buildtime"] = "";
// break;
// }
// }
// // $ret["deploy"] = $job->getId();
// }
// }
// } catch (\Exception $e) {
// $ret = array();
// $ret["status"] = "N.A. - HTTP Timeout";
// $ret["deploy"] = "";
// $ret["bg"] = "bg-danger";
// $ret["version"] = "";
// $ret["coreversion"] = "";
// $ret["buildtime"] = "";
// } catch (\Throwable $e) {
// $ret = array();
// $ret["status"] = "N.A. - HTTP Timeout";
// $ret["deploy"] = "";
// $ret["bg"] = "bg-danger";
// $ret["version"] = "";
// $ret["coreversion"] = "";
// $ret["buildtime"] = "";
// }
return $ret;
}
public function addJob(Job $job): self
{
if (!$this->jobs->contains($job)) {
$this->jobs[] = $job;
$job->setEnvironment($this);
}
return $this;
}
public function removeJob(Job $job): self
{
if ($this->jobs->contains($job)) {
$this->jobs->removeElement($job);
// set the owning side to null (unless already changed)
if ($job->getEnvironment() === $this) {
$job->setEnvironment(null);
}
}
return $this;
}
public function getAutoStop(): ?bool
{
return $this->autoStop;
}
public function setAutoStop(bool $autoStop): self
{
$this->autoStop = $autoStop;
return $this;
}
public function getMonitoring(): ?Config
{
return $this->monitoring;
}
public function setMonitoring(?Config $monitoring): self
{
$this->monitoring = $monitoring;
return $this;
}
public function getFqdn(): ?string
{
return $this->fqdn;
}
public function setFqdn(?string $fqdn): self
{
$this->fqdn = $fqdn;
return $this;
}
public function getLastAutoStopChange(): ?\DateTimeInterface
{
return $this->lastAutoStopChange;
}
public function setLastAutoStopChange(?\DateTimeInterface $lastAutoStopChange): self
{
$this->lastAutoStopChange = $lastAutoStopChange;
return $this;
}
/**
* @return Collection|DeployTag[]
*/
public function getDeployTags(): Collection
{
return $this->deployTags;
}
public function addDeployTag(DeployTag $deployTag): self
{
if (!$this->deployTags->contains($deployTag)) {
$this->deployTags[] = $deployTag;
$deployTag->setEnvironment($this);
}
return $this;
}
public function removeDeployTag(DeployTag $deployTag): self
{
if ($this->deployTags->contains($deployTag)) {
$this->deployTags->removeElement($deployTag);
// set the owning side to null (unless already changed)
if ($deployTag->getEnvironment() === $this) {
$deployTag->setEnvironment(null);
}
}
return $this;
}
public function getAutoRestart(): ?bool
{
return $this->autoRestart;
}
public function setAutoRestart(bool $autoRestart): self
{
$this->autoRestart = $autoRestart;
return $this;
}
/**
* @return Collection|Config[]
*/
public function getModules(): Collection
{
return $this->modules;
}
public function addModule(Config $module): self
{
if (!$this->modules->contains($module)) {
$this->modules[] = $module;
}
return $this;
}
public function removeModule(Config $module): self
{
$this->modules->removeElement($module);
return $this;
}
/**
* @return Collection|EnvironmentMonitor[]
*/
public function getEnvironmentMonitors(): Collection
{
return $this->environmentMonitors;
}
public function addEnvironmentMonitor(EnvironmentMonitor $environmentMonitor): self
{
if (!$this->environmentMonitors->contains($environmentMonitor)) {
$this->environmentMonitors[] = $environmentMonitor;
$environmentMonitor->setEnvironment($this);
}
return $this;
}
public function removeEnvironmentMonitor(EnvironmentMonitor $environmentMonitor): self
{
if ($this->environmentMonitors->removeElement($environmentMonitor)) {
// set the owning side to null (unless already changed)
if ($environmentMonitor->getEnvironment() === $this) {
$environmentMonitor->setEnvironment(null);
}
}
return $this;
}
public function getEnvironmentName(): string
{
$botEnvName = $this->getProject()->getBotname();
if($this->getType()->getName() != "prod") {
$botEnvName .= $this->getType()->getName();
}
return $botEnvName;
}
public function getCreated()
{
return $this->created;
}
public function getUpdated()
{
return $this->updated;
}
public function getDebugMode(): ?bool
{
if($this->debugMode == null) {
$this->debugMode = false;
}
return $this->debugMode;
}
public function setDebugMode(?bool $debugMode): self
{
if($debugMode == null) {
$debugMode = false;
}
$this->debugMode = $debugMode;
return $this;
}
public function getLastDebugModeChange(): ?\DateTimeInterface
{
return $this->lastDebugModeChange;
}
public function setLastDebugModeChange(?\DateTimeInterface $lastDebugModeChange): self
{
if($lastDebugModeChange == null) {
$lastDebugModeChange = new \DateTime();
}
$this->lastDebugModeChange = $lastDebugModeChange;
return $this;
}
}