Release.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. foreach(array("ftth", "mapas", "stats") as $app){
  81. $dObj->file($app.'.oauth.env')->content("");
  82. }
  83. $dObj->file('install.yml')->content(
  84. yaml::dump(array(
  85. "install_dir" => realpath($dir),
  86. 'docker_apps' => array('base', 'stats', 'ftth', 'mapas'),
  87. 'domain' => $domain,
  88. )
  89. )
  90. );
  91. $dObj->file('ansible.cfg')->content(
  92. "[defaults]
  93. inventory=inventory.ini
  94. "
  95. );
  96. }
  97. function getHostEnv($fd_domain = "fd3.flowdat.com"){
  98. return array(
  99. "HOST_BASE" => "base.". $fd_domain,
  100. "HOST_FTTH" => "ftth.". $fd_domain,
  101. "HOST_MAPAS" => "mapas.".$fd_domain,
  102. "HOST_STATS" => "stats.".$fd_domain,
  103. "HOST_GRAFANA" => "grafana.".$fd_domain,
  104. "HOST_PMA" => "pma.".$fd_domain,
  105. );
  106. }
  107. function getHostConfig($config_ip, $fd_domain = "fd3.flowdat.com"){
  108. return array(
  109. "base.". $fd_domain => $config_ip,
  110. "ftth.". $fd_domain => $config_ip,
  111. "mapas.". $fd_domain => $config_ip,
  112. "stats.". $fd_domain => $config_ip,
  113. "grafana.". $fd_domain => $config_ip,
  114. "pma.". $fd_domain => $config_ip,
  115. );
  116. }
  117. function getDockerComposer( $v = "latest", $host_env_file = "host.env", $registry = "docker.infra.flowdat.com/", $fd_domain = "fd3.flowdat.com")
  118. {
  119. $mysql_root_pass="235r2342gtfsw";
  120. $mysql_user="iksop";
  121. $mysq_pass="235r2342gtfsw";
  122. $oauth_client = "1_3323sq6urn8kwccg8s4ok848ggwwgkw4c08wsc4cwkc08osocc";
  123. $oauth_client_secret = "5w7gx6ptdoo4g8cwwo88o8gowosgco84sso08ssow0osg88g8k";
  124. $composer = new FileFormat2("../");
  125. $composer->addService("base")
  126. ->image($registry."fd3/base:" . $v)
  127. ->build("./base/")
  128. ->restart("always")
  129. ->addLinks("mysql")
  130. ->addLinks("amqp")
  131. ->addEnviroment("VIRTUAL_HOST", "base.".$fd_domain)
  132. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  133. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  134. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  135. ->addEnviroment("HOST_BASE", "base." .$fd_domain)
  136. ->addVolumes("./base/", "/opt/base")
  137. ;
  138. $composer->addService("ftth")
  139. ->image($registry."fd3/ftth:" . $v)
  140. ->build("./ftth/")
  141. ->restart("always")
  142. ->addLinks("mysql")
  143. ->addLinks("base")
  144. ->addLinks("amqp")
  145. ->addLinks("base", "base.".$fd_domain)
  146. ->addEnv_file("ftth.oauth.env")
  147. ->addEnviroment("VIRTUAL_HOST", "ftth.".$fd_domain)
  148. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  149. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  150. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  151. ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  152. //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  153. //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  154. //->addEnviroment("HTTPS_METHOD", "nohttps")
  155. ->addVolumes("./ftth/", "/opt/ftth")
  156. ;
  157. $composer->addService("mapas")
  158. ->image($registry."fd3/mapas:" . $v)
  159. ->build("./mapas/")
  160. ->addLinks("mysql")
  161. ->addLinks("base")
  162. ->addLinks("amqp")
  163. ->addLinks("base", "base.".$fd_domain)
  164. ->addEnv_file("mapas.oauth.env")
  165. ->addEnviroment("VIRTUAL_HOST", "mapas.".$fd_domain)
  166. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  167. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  168. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  169. ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  170. //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  171. //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  172. //->addEnviroment("HTTPS_METHOD", "nohttps")
  173. ->addVolumes("./mapas/", "/opt/mapas")
  174. ->addVolumes("./mapas/web/uploads", "/opt/mapas/web/uploads")
  175. ;
  176. $composer->addService("stats")
  177. ->image($registry."fd3/stats:" . $v)
  178. ->build("./stats/")
  179. ->addLinks("mysql")
  180. ->addLinks("base")
  181. ->addLinks("amqp")
  182. ->addLinks("base", "base.".$fd_domain)
  183. ->addLinks("jsendpoint", "endpoint")
  184. ->addLinks("statsd", "statsd")
  185. ->addVolumes("./stats/", "/opt/stats")
  186. ->addEnv_file("stats.oauth.env")
  187. ->addEnviroment("VIRTUAL_HOST", "stats.".$fd_domain)
  188. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  189. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  190. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  191. ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  192. //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  193. //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  194. //->addEnviroment("HTTPS_METHOD", "nohttps")
  195. ;
  196. /**************************************************************************************/
  197. /* Servicios */
  198. /**************************************************************************************/
  199. $composer->addService("mysql")->image($registry."fd3/mysql:". $v)
  200. ->build("./extra/mysql")
  201. ->addVolumes("./mysql/", "/var/lib/mysql/")
  202. ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  203. ->addEnviroment("MYSQL_USER", $mysql_user)
  204. ->addEnviroment("MYSQL_PASSWORD", $mysq_pass)
  205. ;
  206. $composer->addService("amqp")->image("rabbitmq:3-management")
  207. ->restart("always")
  208. ;
  209. /**************************************************************************************/
  210. /* Workers */
  211. /**************************************************************************************/
  212. $composer->addService("ftth_worker")->image($registry."fd3/ftth:" . $v)
  213. ->build("./ftth/")
  214. ->restart("always")
  215. ->addLinks("mysql")
  216. ->addLinks("base")
  217. ->addLinks("amqp")
  218. ->addLinks("base", "base.".$fd_domain)
  219. ->addEnviroment("VIRTUAL_HOST", "ftth.".$fd_domain)
  220. ->addEnviroment("HTTPS_METHOD", "nohttps")
  221. ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  222. ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  223. ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  224. ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  225. //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  226. //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  227. ->addVolumes("./ftth/", "/opt/ftth")
  228. ;
  229. $composer->addService("phpmyadmin")->image("phpmyadmin/phpmyadmin")
  230. ->restart("always")
  231. ->addPorts(8080, 80)
  232. ->addLinks("mysql", "db")
  233. ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  234. ->addEnviroment("VIRTUAL_HOST", "pma.".$fd_domain)
  235. ->addEnviroment("HTTPS_METHOD", "nohttps")
  236. ;
  237. $composer->addService("grafana")->image("grafana/grafana")
  238. ->addLinks("jsendpoint","endpoint")
  239. ->addLinks("mysql")
  240. ->restart("always")
  241. ->addEnviroment("VIRTUAL_HOST", "grafana.".$fd_domain)
  242. ->addEnviroment("HTTPS_METHOD", "nohttps")
  243. ->addEnviroment("./statsd/grafana/lib", "/var/lib/grafana")
  244. ->addEnviroment("GF_SECURITY_ADMIN_PASSWORD", "queRini6")
  245. ->addEnviroment("GF_INSTALL_PLUGINS", "grafana-simple-json-datasource")
  246. ->addEnviroment("GF_DEFAULT_THEME", "light")
  247. ->addEnviroment("GF_AUTH_ANONYMOUS_ORG_NAME", "Main Org.")
  248. ->addEnviroment("GF_AUTH_ANONYMOUS_ORG_ROLE", "Viewer")
  249. ->addEnviroment("GF_AUTH_ANONYMOUS_ENABLED", "true")
  250. ->addEnviroment("GF_DATABASE_URL", "mysql://root:".$mysql_root_pass."@mysql:3306/grafana")
  251. ->addEnviroment("GF_SERVER_ROOT_URL", "http://grafana.".$fd_domain."/")
  252. ;
  253. $composer->addService("jsendpoint")
  254. ->build("./extra/statsd/endpoint/json")
  255. ->image($registry."fd3/jsonep:$v")
  256. ->addVolumes("./extra/statsd/endpoint/json", "/opt/datasource")
  257. ->addLinks("jsonep_mysql")
  258. ->addLinks("jsonep_mongo")
  259. ->restart("always")
  260. ;
  261. $composer->addService("jsonep_mysql")
  262. ->build("./extra/statsd/endpoint/mysql")
  263. ->image($registry."fd3/jsonep_mysql:$v")
  264. ->addVolumes("./extra/statsd/endpoint/mysql", "/opt/datasource")
  265. ->addLinks("mysql")
  266. ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  267. ->restart("always")
  268. ;
  269. $composer->addService("jsonep_mongo")
  270. ->build("./extra/statsd/endpoint/mongodb")
  271. ->image($registry."fd3/jsonep_mongo:$v")
  272. ->addVolumes("./extra/statsd/endpoint/mongodb", "/opt/datasource")
  273. ->addLinks("mongodb")
  274. ->restart("always")
  275. ;
  276. $composer->addService("mongodb")
  277. ->image("mongo:3.4")
  278. ->addVolumes("./mongodb", "/data/db")
  279. ;
  280. $composer->addService("statsd")->build("./extra/statsd/statsd")
  281. ->image($registry."fd3/statsd:$v")
  282. ->addPorts("8125", "8125/udp")
  283. ->addLinks("mysql")
  284. ->addLinks("mongodb")
  285. ->restart("always")
  286. ->addVolumes("./extra/statsd/statsd/statsd.config.js", "/opt/config/statsd.config.js")
  287. ;
  288. $composer->addService("supervisord")->build("./extra/supervisord")
  289. ->image($registry."fd3/supervisord:$v")
  290. ->privileged(true)
  291. ->restart("always")
  292. ->addEnviroment("./extra/supervisord/", "/etc/supervisord/")
  293. ->addEnviroment("./extra/supervisord/var/", "/var/log/supervisor/")
  294. ->addEnviroment("./extra/supervisord/sshd_config", "/etc/ssh/sshd_config")
  295. ->addEnviroment("./extra/supervisord/bin/fiberhome", "/usr/bin/fiberhome")
  296. ->addEnviroment("./extra/supervisord/bin/fiberlink", "/usr/bin/fiberlink")
  297. ;
  298. $composer->addService("nginx")->build("extra/nginx/")
  299. ->image($registry."fd3/nginx:".$v)
  300. ->addEnv_file($host_env_file)
  301. ->restart("always")
  302. ->addPorts(80, 80)
  303. ->addPorts(443, 443)
  304. ->addVolumes("/var/run/docker.sock", "/tmp/docker.sock:ro")
  305. ->addVolumes("./extra/nginx/certs", "/etc/nginx/certs:ro")
  306. ->addVolumes("./extra/nginx/vhost.d", "/etc/nginx/vhost.d")
  307. ->addVolumes("./extra/nginx/share", "/usr/share/nginx/html")
  308. ;
  309. return $composer->render();
  310. }
  311. }