Deploy.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Copyright 2013-2022 the PM2 project authors. All rights reserved.
  3. * Use of this source code is governed by a license that
  4. * can be found in the LICENSE file.
  5. */
  6. var fs = require('fs');
  7. var cst = require('../../constants.js');
  8. var Utility = require('../Utility.js');
  9. var Common = require('../Common.js');
  10. function deployHelper() {
  11. console.log('');
  12. console.log('-----> Helper: Deployment with PM2');
  13. console.log('');
  14. console.log(' Generate a sample ecosystem.config.js with the command');
  15. console.log(' $ pm2 ecosystem');
  16. console.log(' Then edit the file depending on your needs');
  17. console.log('');
  18. console.log(' Commands:');
  19. console.log(' setup run remote setup commands');
  20. console.log(' update update deploy to the latest release');
  21. console.log(' revert [n] revert to [n]th last deployment or 1');
  22. console.log(' curr[ent] output current release commit');
  23. console.log(' prev[ious] output previous release commit');
  24. console.log(' exec|run <cmd> execute the given <cmd>');
  25. console.log(' list list previous deploy commits');
  26. console.log(' [ref] deploy to [ref], the "ref" setting, or latest tag');
  27. console.log('');
  28. console.log('');
  29. console.log(' Basic Examples:');
  30. console.log('');
  31. console.log(' First initialize remote production host:');
  32. console.log(' $ pm2 deploy ecosystem.config.js production setup');
  33. console.log('');
  34. console.log(' Then deploy new code:');
  35. console.log(' $ pm2 deploy ecosystem.config.js production');
  36. console.log('');
  37. console.log(' If I want to revert to the previous commit:');
  38. console.log(' $ pm2 deploy ecosystem.config.js production revert 1');
  39. console.log('');
  40. console.log(' Execute a command on remote server:');
  41. console.log(' $ pm2 deploy ecosystem.config.js production exec "pm2 restart all"');
  42. console.log('');
  43. console.log(' PM2 will look by default to the ecosystem.config.js file so you dont need to give the file name:');
  44. console.log(' $ pm2 deploy production');
  45. console.log(' Else you have to tell PM2 the name of your ecosystem file');
  46. console.log('');
  47. console.log(' More examples in https://github.com/Unitech/pm2');
  48. console.log('');
  49. };
  50. module.exports = function(CLI) {
  51. CLI.prototype.deploy = function(file, commands, cb) {
  52. var that = this;
  53. if (file == 'help') {
  54. deployHelper();
  55. return cb ? cb() : that.exitCli(cst.SUCCESS_EXIT);
  56. }
  57. var args = commands.rawArgs;
  58. var env;
  59. args.splice(0, args.indexOf('deploy') + 1);
  60. // Find ecosystem file by default
  61. if (!Common.isConfigFile(file)) {
  62. env = args[0];
  63. var defaultConfigNames = ['ecosystem.config.js', 'ecosystem.json', 'ecosystem.json5', 'package.json'];
  64. file = Utility.whichFileExists(defaultConfigNames);
  65. if (!file) {
  66. Common.printError('Not any default deployment file exists.'+
  67. ' Allowed default config file names are: ' + defaultConfigNames.join(', '));
  68. return cb ? cb('Not any default ecosystem file present') : that.exitCli(cst.ERROR_EXIT);
  69. }
  70. }
  71. else
  72. env = args[1];
  73. var json_conf = null;
  74. try {
  75. json_conf = Common.parseConfig(fs.readFileSync(file), file);
  76. } catch (e) {
  77. Common.printError(e);
  78. return cb ? cb(e) : that.exitCli(cst.ERROR_EXIT);
  79. }
  80. if (!env) {
  81. deployHelper();
  82. return cb ? cb() : that.exitCli(cst.SUCCESS_EXIT);
  83. }
  84. if (!json_conf.deploy || !json_conf.deploy[env]) {
  85. Common.printError('%s environment is not defined in %s file', env, file);
  86. return cb ? cb('%s environment is not defined in %s file') : that.exitCli(cst.ERROR_EXIT);
  87. }
  88. if (!json_conf.deploy[env]['post-deploy']) {
  89. json_conf.deploy[env]['post-deploy'] = 'pm2 startOrRestart ' + file + ' --env ' + env;
  90. }
  91. require('pm2-deploy').deployForEnv(json_conf.deploy, env, args, function(err, data) {
  92. if (err) {
  93. Common.printError('Deploy failed');
  94. Common.printError(err.message || err);
  95. return cb ? cb(err) : that.exitCli(cst.ERROR_EXIT);
  96. }
  97. Common.printOut('--> Success');
  98. return cb ? cb(null, data) : that.exitCli(cst.SUCCESS_EXIT);
  99. });
  100. };
  101. };