Release.php 14 KB

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