start.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const path = require('path');
  2. const isRoot = require('is-root');
  3. const hostile = require('hostile');
  4. const colors = require('colors');
  5. const docker = require('docker-compose');
  6. const log = require('../inc/log');
  7. const shell = require('shelljs');
  8. const ProjectEnvironment = require('../classes/ProjectEnvironment');
  9. const installHosts = (hosts) => {
  10. if (!isRoot()) {
  11. log.warn('Unable to set hosts because you need root privileges');
  12. } else {
  13. for (var ip in hosts) {
  14. const hostsString = hosts[ip].join(' ');
  15. log.log('Installing hosts for %s: %s', ip, hostsString);
  16. hostile.set(ip, hostsString);
  17. }
  18. }
  19. };
  20. const runServiceHook = (hook, service, projectEnv) => {
  21. const env = projectEnv.getEnv();
  22. const scripts = [
  23. `${hook}.before`,
  24. `${hook}.before.${env}`,
  25. `${hook}`,
  26. `${hook}.${env}`,
  27. `${hook}.after`,
  28. `${hook}.after.${env}`,
  29. ];
  30. let scriptArgument = {
  31. service: service,
  32. log: (message) => {
  33. log.log('%s - %s', colors.bold(service), message);
  34. },
  35. env: projectEnv.getEnv(),
  36. shell: (cmd) => {
  37. shell.cd(path.join(projectEnv.getCwd(), service));
  38. const ret = shell.exec(cmd, { fatal: true });
  39. if (ret.code !== 0) {
  40. throw ret.code;
  41. }
  42. }
  43. };
  44. switch (hook) {
  45. case "start":
  46. scriptArgument.exec = (command, options) => {
  47. const opts = { ...projectEnv.getDockerOptions(), ...options };
  48. return docker.exec(service, command, opts);
  49. };
  50. break;
  51. default:
  52. break;
  53. }
  54. let chain = Promise.resolve();
  55. scripts.forEach(script => {
  56. chain = chain.then(() => {
  57. return new Promise((resolve, reject) => {
  58. try {
  59. const scriptFunction = require(path.join(projectEnv.getCwd(), service, script));
  60. log.log(`\nExecuting %s script for %s...`, colors.bold(script), colors.bold(service));
  61. try {
  62. Promise
  63. .all([scriptFunction({ ...scriptArgument })])
  64. .then(() => {
  65. resolve();
  66. })
  67. .catch((e) => {
  68. reject(e);
  69. });
  70. } catch (ex) {
  71. reject(ex);
  72. }
  73. } catch (ex) {
  74. resolve();
  75. }
  76. });
  77. });
  78. });
  79. return chain.then(() => { });
  80. };
  81. const runServices = async (projectEnv) => {
  82. const services = await projectEnv.getServices();
  83. let preparePromises = [];
  84. services.forEach(service => {
  85. preparePromises.push(runServiceHook("prepare", service, projectEnv));
  86. });
  87. try {
  88. await Promise.all(preparePromises);
  89. } catch (e) {
  90. log.error(e);
  91. return;
  92. }
  93. await docker.buildAll(projectEnv.getDockerOptions());
  94. await docker.upAll(projectEnv.getDockerOptions());
  95. let startPromises = [];
  96. services.forEach(service => {
  97. startPromises.push(runServiceHook("start", service, projectEnv));
  98. });
  99. await Promise.all(startPromises);
  100. };
  101. module.exports = (env) => {
  102. try {
  103. const projectEnv = new ProjectEnvironment(process.cwd(), env);
  104. log.log("Starting project (%s environment)...", colors.bold(env));
  105. // Install hosts
  106. installHosts(projectEnv.getHosts());
  107. // Docker up services
  108. runServices(projectEnv);
  109. } catch (ex) {
  110. log.error(...ex);
  111. }
  112. };