Release.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 addBase($composer, $config = array(), $flavor = "dev"){
  118. $version = "latest";
  119. $domain = "fd3.flowdat.com";
  120. extract($config);
  121. $composer->addService("base")
  122. ->image($registry."fd3/base:" . $version)
  123. ->build("./base/")
  124. ->restart("always")
  125. ->addLinks("mysql")
  126. ->addLinks("amqp")
  127. ->addEnviroment("VIRTUAL_HOST", "base.".$domain)
  128. ->addEnviroment("HOST_FTTH", "ftth.".$domain)
  129. ->addEnviroment("HOST_STATS", "stats.".$domain)
  130. ->addEnviroment("HOST_MAPAS", "mapas.".$domain)
  131. ->addEnviroment("HOST_BASE", "base." .$domain)
  132. ->addVolumes("./base/", "/opt/base")
  133. ;
  134. }
  135. function addMySql($composer, $config, $flavor = "dev"){
  136. $version = "latest";
  137. $domain = "fd3.flowdat.com";
  138. extract($config);
  139. $composer->addService("mysql")->image($registry."fd3/mysql:". $version)
  140. ->build("./extra/mysql")
  141. ->addVolumes("./mysql/", "/var/lib/mysql/")
  142. ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  143. ->addEnviroment("MYSQL_USER", $mysql_user)
  144. ->addEnviroment("MYSQL_PASSWORD", $mysq_pass)
  145. ;
  146. }
  147. function getDockerComposer( $v = "latest", $host_env_file = "host.env", $registry = "docker.infra.flowdat.com/", $fd_domain = "fd3.flowdat.com")
  148. {
  149. $mysql_root_pass="235r2342gtfsw";
  150. $mysql_user="iksop";
  151. $mysq_pass="235r2342gtfsw";
  152. $oauth_client = "1_3323sq6urn8kwccg8s4ok848ggwwgkw4c08wsc4cwkc08osocc";
  153. $oauth_client_secret = "5w7gx6ptdoo4g8cwwo88o8gowosgco84sso08ssow0osg88g8k";
  154. $composer = new FileFormat2("../");
  155. $this->addBase($composer, array("version" => $v, "domain" => $fd_domain));
  156. //$composer->addService("ftth")
  157. // ->image($registry."fd3/ftth:" . $v)
  158. // ->build("./ftth/")
  159. // ->restart("always")
  160. // ->addLinks("mysql")
  161. // ->addLinks("base")
  162. // ->addLinks("amqp")
  163. // ->addLinks("base", "base.".$fd_domain)
  164. // ->addEnv_file("ftth.oauth.env")
  165. // ->addEnviroment("VIRTUAL_HOST", "ftth.".$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("./ftth/", "/opt/ftth")
  174. //;
  175. //$composer->addService("mapas")
  176. // ->image($registry."fd3/mapas:" . $v)
  177. // ->build("./mapas/")
  178. // ->addLinks("mysql")
  179. // ->addLinks("base")
  180. // ->addLinks("amqp")
  181. // ->addLinks("base", "base.".$fd_domain)
  182. // ->addEnv_file("mapas.oauth.env")
  183. // ->addEnviroment("VIRTUAL_HOST", "mapas.".$fd_domain)
  184. // ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  185. // ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  186. // ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  187. // ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  188. // //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  189. // //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  190. // //->addEnviroment("HTTPS_METHOD", "nohttps")
  191. // ->addVolumes("./mapas/", "/opt/mapas")
  192. // ->addVolumes("./mapas/web/uploads", "/opt/mapas/web/uploads")
  193. //;
  194. //$composer->addService("stats")
  195. // ->image($registry."fd3/stats:" . $v)
  196. // ->build("./stats/")
  197. // ->addLinks("mysql")
  198. // ->addLinks("base")
  199. // ->addLinks("amqp")
  200. // ->addLinks("base", "base.".$fd_domain)
  201. // ->addLinks("jsendpoint", "endpoint")
  202. // ->addLinks("statsd", "statsd")
  203. // ->addVolumes("./stats/", "/opt/stats")
  204. // ->addEnv_file("stats.oauth.env")
  205. // ->addEnviroment("VIRTUAL_HOST", "stats.".$fd_domain)
  206. // ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  207. // ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  208. // ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  209. // ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  210. // //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  211. // //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  212. // //->addEnviroment("HTTPS_METHOD", "nohttps")
  213. //;
  214. /**************************************************************************************/
  215. /* Servicios */
  216. /**************************************************************************************/
  217. $this->addMySql($composer, array(
  218. 'mysql_root_pass' => $mysql_root_pass,
  219. 'mysql_user' => $mysql_user,
  220. 'mysql_pass' => $mysql_pass,
  221. ));
  222. $composer->addService("amqp")->image("rabbitmq:3-management")
  223. ->restart("always")
  224. ;
  225. /**************************************************************************************/
  226. /* Workers */
  227. /**************************************************************************************/
  228. //$composer->addService("ftth_worker")->image($registry."fd3/ftth:" . $v)
  229. // ->build("./ftth/")
  230. // ->restart("always")
  231. // ->addLinks("mysql")
  232. // ->addLinks("base")
  233. // ->addLinks("amqp")
  234. // ->addLinks("base", "base.".$fd_domain)
  235. // ->addEnviroment("VIRTUAL_HOST", "ftth.".$fd_domain)
  236. // ->addEnviroment("HTTPS_METHOD", "nohttps")
  237. // ->addEnviroment("HOST_FTTH", "ftth.".$fd_domain)
  238. // ->addEnviroment("HOST_STATS", "stats.".$fd_domain)
  239. // ->addEnviroment("HOST_MAPAS", "mapas.".$fd_domain)
  240. // ->addEnviroment("HOST_BASE", "base.".$fd_domain)
  241. // //->addEnviroment("OAUTH_CLIENT_ID", $oauth_client)
  242. // //->addEnviroment("OAUTH_CLIENT_SECRET", $oauth_client_secret)
  243. // ->addVolumes("./ftth/", "/opt/ftth")
  244. //;
  245. //$composer->addService("phpmyadmin")->image("phpmyadmin/phpmyadmin")
  246. // ->restart("always")
  247. // ->addPorts(8080, 80)
  248. // ->addLinks("mysql", "db")
  249. // ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  250. // ->addEnviroment("VIRTUAL_HOST", "pma.".$fd_domain)
  251. // ->addEnviroment("HTTPS_METHOD", "nohttps")
  252. //;
  253. //$composer->addService("grafana")->image("grafana/grafana")
  254. // ->addLinks("jsendpoint","endpoint")
  255. // ->addLinks("mysql")
  256. // ->restart("always")
  257. // ->addEnviroment("VIRTUAL_HOST", "grafana.".$fd_domain)
  258. // ->addEnviroment("HTTPS_METHOD", "nohttps")
  259. // ->addEnviroment("./statsd/grafana/lib", "/var/lib/grafana")
  260. // ->addEnviroment("GF_SECURITY_ADMIN_PASSWORD", "queRini6")
  261. // ->addEnviroment("GF_INSTALL_PLUGINS", "grafana-simple-json-datasource")
  262. // ->addEnviroment("GF_DEFAULT_THEME", "light")
  263. // ->addEnviroment("GF_AUTH_ANONYMOUS_ORG_NAME", "Main Org.")
  264. // ->addEnviroment("GF_AUTH_ANONYMOUS_ORG_ROLE", "Viewer")
  265. // ->addEnviroment("GF_AUTH_ANONYMOUS_ENABLED", "true")
  266. // ->addEnviroment("GF_DATABASE_URL", "mysql://root:".$mysql_root_pass."@mysql:3306/grafana")
  267. // ->addEnviroment("GF_SERVER_ROOT_URL", "http://grafana.".$fd_domain."/")
  268. //;
  269. //$composer->addService("jsendpoint")
  270. // ->build("./extra/statsd/endpoint/json")
  271. // ->image($registry."fd3/jsonep:$v")
  272. // ->addVolumes("./extra/statsd/endpoint/json", "/opt/datasource")
  273. // ->addLinks("jsonep_mysql")
  274. // ->addLinks("jsonep_mongo")
  275. // ->restart("always")
  276. // ;
  277. //$composer->addService("jsonep_mysql")
  278. // ->build("./extra/statsd/endpoint/mysql")
  279. // ->image($registry."fd3/jsonep_mysql:$v")
  280. // ->addVolumes("./extra/statsd/endpoint/mysql", "/opt/datasource")
  281. // ->addLinks("mysql")
  282. // ->addEnviroment("MYSQL_ROOT_PASSWORD", $mysql_root_pass)
  283. // ->restart("always")
  284. // ;
  285. //$composer->addService("jsonep_mongo")
  286. // ->build("./extra/statsd/endpoint/mongodb")
  287. // ->image($registry."fd3/jsonep_mongo:$v")
  288. // ->addVolumes("./extra/statsd/endpoint/mongodb", "/opt/datasource")
  289. // ->addLinks("mongodb")
  290. // ->restart("always")
  291. // ;
  292. //$composer->addService("mongodb")
  293. // ->image("mongo:3.4")
  294. // ->addVolumes("./mongodb", "/data/db")
  295. //;
  296. //$composer->addService("statsd")->build("./extra/statsd/statsd")
  297. // ->image($registry."fd3/statsd:$v")
  298. // ->addPorts("8125", "8125/udp")
  299. // ->addLinks("mysql")
  300. // ->addLinks("mongodb")
  301. // ->restart("always")
  302. // ->addVolumes("./extra/statsd/statsd/statsd.config.js", "/opt/config/statsd.config.js")
  303. //;
  304. //$composer->addService("supervisord")->build("./extra/supervisord")
  305. // ->image($registry."fd3/supervisord:$v")
  306. // ->privileged(true)
  307. // ->restart("always")
  308. // ->addEnviroment("./extra/supervisord/", "/etc/supervisord/")
  309. // ->addEnviroment("./extra/supervisord/var/", "/var/log/supervisor/")
  310. // ->addEnviroment("./extra/supervisord/sshd_config", "/etc/ssh/sshd_config")
  311. // ->addEnviroment("./extra/supervisord/bin/fiberhome", "/usr/bin/fiberhome")
  312. // ->addEnviroment("./extra/supervisord/bin/fiberlink", "/usr/bin/fiberlink")
  313. //
  314. //;
  315. $composer->addService("nginx")->build("extra/nginx/")
  316. ->image($registry."fd3/nginx:".$v)
  317. ->addEnv_file($host_env_file)
  318. ->restart("always")
  319. ->addPorts(80, 80)
  320. ->addPorts(443, 443)
  321. ->addVolumes("/var/run/docker.sock", "/tmp/docker.sock:ro")
  322. ->addVolumes("./extra/nginx/certs", "/etc/nginx/certs:ro")
  323. ->addVolumes("./extra/nginx/vhost.d", "/etc/nginx/vhost.d")
  324. ->addVolumes("./extra/nginx/share", "/usr/share/nginx/html")
  325. ;
  326. return $composer->render();
  327. }
  328. }