UNPKG

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