Release.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. namespace FD3;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Docker\Composer\FileFormat2;
  9. use Symfony\Component\Yaml\Yaml;
  10. class Release extends Command
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('make:install')
  16. ->setDescription('Create a new install.')
  17. ->setHelp('This command allows you to create a new installation...')
  18. ->addArgument('dir', InputArgument::REQUIRED, 'The directory where to create the installation.')
  19. ->addOption('base-repo',null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Base', "git@bitbucket.org:ikflowdat/base.git")
  20. ->addOption('base-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Base app', "master")
  21. ->addOption('ftth-repo',null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Base', "git@bitbucket.org:ikflowdat/ftth.git")
  22. ->addOption('ftth-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Ftth appFtth', "master")
  23. ->addOption('mapas-repo',null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Base', "git@bitbucket.org:ikflowdat/mapas.git")
  24. ->addOption('mapas-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Mapas app', "master")
  25. ->addOption('stats-repo',null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Base', "git@bitbucket.org:ikflowdat/stats.git")
  26. ->addOption('stats-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Stats app', "master")
  27. ->addOption('extra-repo',null, InputOption::VALUE_REQUIRED, 'Git clone Url fot the app Base', "git@bitbucket.org:ikflowdat/extra.git")
  28. ->addOption('extra-ref', null, InputOption::VALUE_REQUIRED, 'Git reference (branch or tag) to clone the Extra files and apps', "master")
  29. ->addOption('host-ip', null, InputOption::VALUE_REQUIRED, 'Ip of the runnning host to be added to the /etc/hosts file, eventually', "127.0.1.1")
  30. ->addOption('domain', null, InputOption::VALUE_REQUIRED, 'Domain where the flowdat will be installed', "fd3.flowdat.com")
  31. ->addOption('docker-tag', null, InputOption::VALUE_REQUIRED, 'Docker tag to be used', "latest")
  32. ;
  33. }
  34. protected function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. $dir = $input->getArgument('dir');
  37. $domain = $input->getOption("domain");
  38. $docker_tag = $input->getOption("docker-tag");
  39. $dObj = new DevOps\FileSystem($dir);
  40. $dObj->dirExists()->realpath();
  41. $install_config = array(
  42. "base" => array(
  43. 'url' => $input->getOption("base-repo"),
  44. 'branch' => $input->getOption("base-ref"),
  45. ),
  46. "ftth" => array(
  47. 'url' => $input->getOption("ftth-repo"),
  48. 'branch' => $input->getOption("ftth-ref"),
  49. ),
  50. "mapas" => array(
  51. 'url' => $input->getOption("mapas-repo"),
  52. 'branch' => $input->getOption("mapas-ref"),
  53. ),
  54. "stats" => array(
  55. 'url' => $input->getOption("stats-repo"),
  56. 'branch' => $input->getOption("stats-ref"),
  57. ),
  58. "extra" => array(
  59. 'url' => $input->getOption("extra-repo"),
  60. 'branch' => $input->getOption("extra-ref"),
  61. ),
  62. );
  63. $dObj->file("git.ini")->writeIniConfig($install_config);
  64. $dObj->file("docker-compose.yml")->content(
  65. $this->getDockerComposer( $docker_tag, "host.env", "docker.infra.flowdat.com/", $domain)
  66. );
  67. $config_ip = $input->getOption("host-ip");
  68. $hostConfig = $this->getHostConfig($config_ip, $domain);
  69. $hostfile_content = "";
  70. foreach($hostConfig as $host => $ip){
  71. $hostfile_content .= $ip . "\t". $host. "\n";
  72. }
  73. $dObj->file("hostsFile")->content($hostfile_content);
  74. $hostEnvConfig = $this->getHostEnv($domain);
  75. $env_content = "";
  76. foreach($hostEnvConfig as $var => $val){
  77. $env_content .= $var."=".$val."\n";
  78. }
  79. $dObj->file('host.env')->content($env_content);
  80. $dObj->file('install.yml')->content(
  81. yaml::dump(array(
  82. "install_dir" => realpath($dir),
  83. 'docker_apps' => array('base', 'stats', 'ftth', 'mapas'),
  84. 'domain' => $domain,
  85. )
  86. )
  87. );
  88. $dObj->file('ansible.cfg')->content(
  89. "[defaults]
  90. inventory=inventory.ini
  91. "
  92. );
  93. }
  94. function getHostEnv($fd_domain = "fd3.flowdat.com"){
  95. return array(
  96. "HOST_BASE" => "base.". $fd_domain,
  97. "HOST_FTTH" => "ftth.". $fd_domain,
  98. "HOST_MAPAS" => "mapas.".$fd_domain,
  99. "HOST_STATS" => "stats.".$fd_domain,
  100. "HOST_GRAFANA" => "grafana.".$fd_domain,
  101. "HOST_PMA" => "pma.".$fd_domain,
  102. );
  103. }
  104. function getHostConfig($config_ip, $fd_domain = "fd3.flowdat.com"){
  105. return array(
  106. "base.". $fd_domain => $config_ip,
  107. "ftth.". $fd_domain => $config_ip,
  108. "mapas.". $fd_domain => $config_ip,
  109. "stats.". $fd_domain => $config_ip,
  110. "grafana.". $fd_domain => $config_ip,
  111. "pma.". $fd_domain => $config_ip,
  112. );
  113. }
  114. function getDockerComposer( $v = "latest", $host_env_file = "host.env", $registry = "docker.infra.flowdat.com/", $fd_domain = "fd3.flowdat.com")
  115. {
  116. $mysql_root_pass="235r2342gtfsw";
  117. $mysql_user="iksop";
  118. $mysq_pass="235r2342gtfsw";
  119. $oauth_client = "1_3323sq6urn8kwccg8s4ok848ggwwgkw4c08wsc4cwkc08osocc";
  120. $oauth_client_secret = "5w7gx6ptdoo4g8cwwo88o8gowosgco84sso08ssow0osg88g8k";
  121. $composer = new FileFormat2("../");
  122. $composer->addService("base")
  123. ->image($registry."fd3/base:" . $v)
  124. ->build("./base/")
  125. ->restart("always")
  126. ->addLinks("mysql")
  127. ->addLinks("amqp")
  128. ->addEnviroment("VIRTUAL_HOST", "base.".$fd_domain)
  129. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  130. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  131. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  132. ->addEnviroment("HOST_BASE", "base." .$fd_domain)
  133. ->addVolumes("./base/", "/opt/base")
  134. ;
  135. $composer->addService("ftth")
  136. ->image($registry."fd3/ftth:" . $v)
  137. ->build("./ftth/")
  138. ->restart("always")
  139. ->addLinks("mysql")
  140. ->addLinks("base")
  141. ->addLinks("amqp")
  142. ->addLinks("base", "base.".$fd_domain)
  143. ->addEnv_file("ftth.oauth.env")
  144. ->addEnviroment("VIRTUAL_HOST", "ftth.".$fd_domain)
  145. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  146. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  147. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  148. ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  149. //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  150. //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  151. //->addEnviroment("HTTPS_METHOD", "nohttps")
  152. ->addVolumes("./ftth/", "/opt/ftth")
  153. ;
  154. $composer->addService("mapas")
  155. ->image($registry."fd3/mapas:" . $v)
  156. ->build("./mapas/")
  157. ->addLinks("mysql")
  158. ->addLinks("base")
  159. ->addLinks("amqp")
  160. ->addLinks("base", "base.".$fd_domain)
  161. ->addEnv_file("mapas.oauth.env")
  162. ->addEnviroment("VIRTUAL_HOST", "mapas.".$fd_domain)
  163. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  164. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  165. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  166. ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  167. //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  168. //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  169. //->addEnviroment("HTTPS_METHOD", "nohttps")
  170. ->addVolumes("./mapas/", "/opt/mapas")
  171. ->addVolumes("./mapas/web/uploads", "/opt/mapas/web/uploads")
  172. ;
  173. $composer->addService("stats")
  174. ->image($registry."fd3/stats:" . $v)
  175. ->build("./stats/")
  176. ->addLinks("mysql")
  177. ->addLinks("base")
  178. ->addLinks("amqp")
  179. ->addLinks("base", "base.".$fd_domain)
  180. ->addLinks("jsendpoint", "endpoint")
  181. ->addLinks("statsd", "statsd")
  182. ->addVolumes("./stats/", "/opt/stats")
  183. ->addEnv_file("stats.oauth.env")
  184. ->addEnviroment("VIRTUAL_HOST", "stats.".$fd_domain)
  185. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  186. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  187. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  188. ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  189. //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  190. //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  191. //->addEnviroment("HTTPS_METHOD", "nohttps")
  192. ;
  193. /**************************************************************************************/
  194. /* Servicios */
  195. /**************************************************************************************/
  196. $composer->addService("mysql")->image($registry."fd3/mysql:". $v)
  197. ->build("./extra/mysql")
  198. ->addVolumes("./mysql/", "/var/lib/mysql/")
  199. ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  200. ->addEnviroment("MYSQL_USER", $mysql_user)
  201. ->addEnviroment("MYSQL_PASSWORD", $mysq_pass)
  202. ;
  203. $composer->addService("amqp")->image("rabbitmq:3-management")
  204. ->restart("always")
  205. ;
  206. /**************************************************************************************/
  207. /* Workers */
  208. /**************************************************************************************/
  209. $composer->addService("ftth_worker")->image($registry."fd3/ftth:" . $v)
  210. ->build("./ftth/")
  211. ->restart("always")
  212. ->addLinks("mysql")
  213. ->addLinks("base")
  214. ->addLinks("amqp")
  215. ->addLinks("base", "base.".$fd_domain)
  216. ->addEnviroment("VIRTUAL_HOST", "ftth.".$fd_domain)
  217. ->addEnviroment("HTTPS_METHOD", "nohttps")
  218. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  219. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  220. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  221. ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  222. //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  223. //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  224. ->addVolumes("./ftth/", "/opt/ftth")
  225. ;
  226. $composer->addService("phpmyadmin")->image("phpmyadmin/phpmyadmin")
  227. ->restart("always")
  228. ->addPorts(8080, 80)
  229. ->addLinks("mysql", "db")
  230. ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  231. ->addEnviroment("VIRTUAL_HOST", "pma.".$fd_domain)
  232. ->addEnviroment("HTTPS_METHOD", "nohttps")
  233. ;
  234. $composer->addService("grafana")->image("grafana/grafana")
  235. ->addLinks("jsendpoint","endpoint")
  236. ->addLinks("mysql")
  237. ->restart("always")
  238. ->addEnviroment("VIRTUAL_HOST", "grafana.".$fd_domain)
  239. ->addEnviroment("HTTPS_METHOD", "nohttps")
  240. ->addEnviroment("./statsd/grafana/lib", "/var/lib/grafana")
  241. ->addEnviroment("GF_SECURITY_ADMIN_PASSWORD", "queRini6")
  242. ->addEnviroment("GF_INSTALL_PLUGINS", "grafana-simple-json-datasource")
  243. ->addEnviroment("GF_DEFAULT_THEME", "light")
  244. ->addEnviroment("GF_AUTH_ANONYMOUS_ORG_NAME", "Main Org.")
  245. ->addEnviroment("GF_AUTH_ANONYMOUS_ORG_ROLE", "Viewer")
  246. ->addEnviroment("GF_AUTH_ANONYMOUS_ENABLED", "true")
  247. ->addEnviroment("GF_DATABASE_URL", "mysql://root:".$mysql_root_pass."@mysql:3306/grafana")
  248. ->addEnviroment("GF_SERVER_ROOT_URL", "http://grafana.".$fd_domain."/")
  249. ;
  250. $composer->addService("jsendpoint")
  251. ->build("./extra/statsd/endpoint/json")
  252. ->image($registry."fd3/jsonep:$v")
  253. ->addVolumes("./extra/statsd/endpoint/json", "/opt/datasource")
  254. ->addLinks("jsonep_mysql")
  255. ->addLinks("jsonep_mongo")
  256. ->restart("always")
  257. ;
  258. $composer->addService("jsonep_mysql")
  259. ->build("./extra/statsd/endpoint/mysql")
  260. ->image($registry."fd3/jsonep_mysql:$v")
  261. ->addVolumes("./extra/statsd/endpoint/mysql", "/opt/datasource")
  262. ->addLinks("mysql")
  263. ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  264. ->restart("always")
  265. ;
  266. $composer->addService("jsonep_mongo")
  267. ->build("./extra/statsd/endpoint/mongodb")
  268. ->image($registry."fd3/jsonep_mongo:$v")
  269. ->addVolumes("./extra/statsd/endpoint/mongodb", "/opt/datasource")
  270. ->addLinks("mongodb")
  271. ->restart("always")
  272. ;
  273. $composer->addService("mongodb")
  274. ->image("mongo:3.4")
  275. ->addVolumes("./mongodb", "/data/db")
  276. ;
  277. $composer->addService("statsd")->build("./extra/statsd/statsd")
  278. ->image($registry."fd3/statsd:$v")
  279. ->addPorts("8125", "8125/udp")
  280. ->addLinks("mysql")
  281. ->addLinks("mongodb")
  282. ->restart("always")
  283. ->addVolumes("./extra/statsd/statsd/statsd.config.js", "/opt/config/statsd.config.js")
  284. ;
  285. $composer->addService("supervisord")->build("./extra/supervisord")
  286. ->image($registry."fd3/supervisord:$v")
  287. ->privileged(true)
  288. ->restart("always")
  289. ->addEnviroment("./extra/supervisord/", "/etc/supervisord/")
  290. ->addEnviroment("./extra/supervisord/var/", "/var/log/supervisor/")
  291. ->addEnviroment("./extra/supervisord/sshd_config", "/etc/ssh/sshd_config")
  292. ->addEnviroment("./extra/supervisord/bin/fiberhome", "/usr/bin/fiberhome")
  293. ->addEnviroment("./extra/supervisord/bin/fiberlink", "/usr/bin/fiberlink")
  294. ;
  295. $composer->addService("nginx")->build("extra/nginx/")
  296. ->image($registry."fd3/nginx:".$v)
  297. ->addEnv_file($host_env_file)
  298. ->restart("always")
  299. ->addPorts(80, 80)
  300. ->addPorts(443, 443)
  301. ->addVolumes("/var/run/docker.sock", "/tmp/docker.sock:ro")
  302. ->addVolumes("./extra/nginx/certs", "/etc/nginx/certs:ro")
  303. ->addVolumes("./extra/nginx/vhost.d", "/etc/nginx/vhost.d")
  304. ->addVolumes("./extra/nginx/share", "/usr/share/nginx/html")
  305. ;
  306. return $composer->render();
  307. }
  308. }