Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.

Commit 364eb0a

Browse files
Nishad BakshiNishad Bakshi
authored andcommitted
Updated to support local.ecosystem.json and to have a separate Install guide
1 parent 0487640 commit 364eb0a

File tree

5 files changed

+138
-102
lines changed

5 files changed

+138
-102
lines changed

app/templates/INSTALL.mdown

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#Deploying to a server
2+
3+
To deploy, make sure both your local machine and your remote server/deployment target have pm2 installed with:
4+
5+
`npm install pm2 -g`
6+
7+
Make sure you have the file `ecosystem.json` with any saved deployment targets inside, to create this file or add a new deployment target run:
8+
9+
`gulp add-deploy-target`
10+
11+
`ecosystem.json` will include any config as well as any deployment config along with your credentials. If you need to put in any credentials or sensitive information that can't be in the repository then answer the final question from `gulp add-deploy-target` with a yes and it will put the deployment config into `local.ecosystem.json`.
12+
13+
Once you've got your TARGETNAME (answer to the first question in `gulp add-deploy-target`) you can initialize the target host with:
14+
15+
`pm2 deploy TARGETNAME setup`
16+
17+
Then deploy your latest code from your git repository and run any post-deploy commands with:
18+
19+
`pm2 deploy TARGETNAME`
20+
21+
**Hint**, pm2 automatically looks for `ecosystem.json` in your current directory, but you can use your `local.ecosystem.json` configuration with `pm2 deploy local.ecosystem.json TARGETNAME`
22+
23+
If you have files you haven't commited, pm2 will ask you to commit them before deploying, to ignore this use `--force`:
24+
25+
`pm2 deploy TARGETNAME --force`
26+
27+
You will now need to ssh into the server, once you've ssh'd in it's very **Important** that when deploying to a secure server that you do all pm2 commands as the pm2 user by using:
28+
29+
`sudo su pm2`
30+
31+
You can then navigate to the app's folder that you chose during `gulp add-deploy-target`.
32+
33+
Once you're in the folder you should have 3 folders: 'current', 'shared', and 'source'; go into the current folder:
34+
35+
`cd current`
36+
37+
You can now launch the app using pm2 with:
38+
39+
`pm2 startOrRestart ecosystem.json`
40+
41+
You can also launch the app using different environment configurations in ecosystem.json (env name prefixed with "env_"), for example:
42+
43+
`pm2 startOrRestart ecosystem.json --env prod`
44+
45+
You should then save your current pm2 deployments with:
46+
47+
`pm2 save`
48+
49+
Running `pm2 save` whenever you deploy a new app is very important as in the event of a server restart the previously saved apps will be resurrected.
50+
51+
Now that the app is started you will not have to stop or restart it when making updates. pm2 will automatically restart the app if any of it's files are changed so you can do `pm2 deploy TARGETNAME --force` from your local machine and the pm2 will automatically restart the app with any changes!
52+
53+
#Administration
54+
55+
To view all running pm2 applications use:
56+
57+
`pm2 list`
58+
59+
To view any logs use:
60+
61+
`pm2 logs`
62+
63+
To monitor ram or cpu usage use:
64+
65+
`pm2 monit`
66+
67+
**If** you've deployed applications previously but don't see them when you run pm2 list then it might be because you're not logged in as the pm2 user or you deployed them under a different user.
68+
69+
If you accidently launch pm2 under your own account then you can kill the process with:
70+
71+
`pm2 kill`
72+
73+
And if you accidently run that command when logged in as the pm2 user then you can start pm2 and resurrect all processes from the last `pm2 save` with:
74+
75+
`pm2 resurrect`
76+
77+
For more information use the help commands or consult the documentation [here](http://pm2.keymetrics.io/docs/usage/quick-start/):
78+
79+
`pm2 help`
80+
81+
`pm2 deploy help`
82+

app/templates/README.mdown

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -69,85 +69,3 @@ Or on Windows:
6969
The application comes with a sample set of JSON data. The detail controller,
7070
the part that handles showing your data, can not only handle JSON, but also
7171
XML, Binary, and Text data out of the box.
72-
73-
## Running with PM2
74-
75-
To run you must first install pm2 if you do not have it with:
76-
77-
`npm install pm2 -g`
78-
79-
It's very **Important** that when deploying to a secure server that you do all pm2 commands as the pm2 user by using:
80-
81-
`sudo su pm2`
82-
83-
You can then launch the app using pm2 with:
84-
85-
`pm2 startOrRestart boot.json`
86-
87-
You can also launch the app using different environment configurations in boot.json (env name prefixed with "env_"), for example:
88-
89-
`pm2 startOrRestart boot.json --env prod`
90-
91-
You can also save your current pm2 deployments with:
92-
93-
`pm2 save`
94-
95-
You should run `pm2 save` whenever you deploy a new app.
96-
97-
To view all running pm2 applications use:
98-
99-
`pm2 list`
100-
101-
To view any logs use:
102-
103-
`pm2 logs`
104-
105-
To monitor ram or cpu usage use:
106-
107-
`pm2 monit`
108-
109-
**If** you've deployed applications previously but don't see them when you run pm2 list then it might be because you're not logged in as the pm2 user or you deployed them under a different user.
110-
111-
If you accidently launch pm2 under your own account then you can kill the process with:
112-
113-
`pm2 kill`
114-
115-
And if you accidently run that command when logged in as the pm2 user then you can start pm2 and resurrect all processes from the last `pm2 save` with:
116-
117-
`pm2 resurrect`
118-
119-
For more information use:
120-
121-
`pm2 help`
122-
123-
## Deploying with PM2
124-
125-
To deploy, make sure both your local machine and your remote server/deployment target have pm2 installed with:
126-
127-
`npm install pm2 -g`
128-
129-
Make sure you have the file `ecosystem.json` with any saved deployment targets inside, to create this file or add a new deployment target run:
130-
131-
`gulp add-deploy-target`
132-
133-
`ecosystem.json` will include any config as well as any deployment config along with your credentials. It is git ignored by default and we recommend you avoid commiting it and instead put any repository appropriate configuration in `boot.json`
134-
135-
First initialize the target host:
136-
137-
`pm2 deploy TARGETNAME setup`
138-
139-
Then deploy your latest code from your git repository and run any post-deploy commands with:
140-
141-
`pm2 deploy TARGETNAME`
142-
143-
**Hint**, pm2 automatically looks `ecosystem.json` in your current directory, but you can use other config files with `pm2 deploy otherEcosystemFile.json TARGETNAME`
144-
145-
If you have files you haven't commited, pm2 will ask you to commit them before deploying, to ignore this use `--force`:
146-
147-
`pm2 deploy TARGETNAME --force`
148-
149-
With your application deployed follow the above "Running with PM2" instructions to start the application, once you've started the app it will listen for changes and automatically restart when you make deploy changes with `pm2 deploy TARGETNAME`.
150-
151-
For more information use:
152-
153-
`pm2 deploy help`

app/templates/_gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ coverage
44
ui/fonts
55
deploy/local.properties
66
local.json
7-
ecosystem.json
7+
local.ecosystem.json
88
/dist
99
.DS_Store
1010
*.pid

app/templates/boot.json renamed to app/templates/ecosystem.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"name": "@sample-app-name",
44
"script": "./node-server/node-app.js",
55
"watch": true,
6+
"restart_delay": 4000,
67
"env": {
78
"NODE_ENV": "local"
89
},
@@ -15,5 +16,6 @@
1516
"env_prod": {
1617
"NODE_ENV": "prod"
1718
}
18-
}]
19+
}],
20+
"deploy": {}
1921
}

app/templates/gulpfile.js

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -244,23 +244,6 @@ gulp.task('init-prod', function(done) {
244244
gulp.task('add-deploy-target', function(done) {
245245
log('Update ecosystem.json targets or create new ones!');
246246

247-
var ecosystem = 'ecosystem.json';
248-
249-
if (!fs.existsSync(ecosystem)) {
250-
try {
251-
var configJSON = {};
252-
configJSON.deploy = {};
253-
254-
var configString = JSON.stringify(configJSON, null, 2) + '\n';
255-
256-
fs.writeFileSync('ecosystem.json', configString, {
257-
encoding: 'utf8'
258-
});
259-
} catch (e) {
260-
console.log('failed to write ecosystem.json: ' + e.message);
261-
}
262-
}
263-
264247
var properties = fs.readFileSync('deploy/build.properties', {
265248
encoding: 'utf8'
266249
});
@@ -269,6 +252,10 @@ gulp.task('add-deploy-target', function(done) {
269252
var gitUrl = 'https://github.com/';
270253
var folderPath = '/space/projects/' + name;
271254

255+
var ecosystem = 'ecosystem.json';
256+
257+
ecosystemMustExist(ecosystem, name);
258+
272259
$.git.exec({
273260
args: 'config --get remote.origin.url',
274261
quiet: true
@@ -312,11 +299,22 @@ gulp.task('add-deploy-target', function(done) {
312299
name: 'folder',
313300
message: 'Where do you want to store the project on your target server?',
314301
default: folderPath
302+
}, {
303+
type: 'list',
304+
name: 'local',
305+
message: 'Is there any sensitive information or credentials here that shouldn\'t go in source control?',
306+
choices: ['no', 'yes'],
307+
default: 0
315308
}];
316309

317310
gulp.src(ecosystem)
318311
.pipe($.prompt.prompt(questions, function(answers) {
319312

313+
if (answers.local === 'yes') {
314+
ecosystem = 'local.ecosystem.json';
315+
ecosystemMustExist(ecosystem, name);
316+
}
317+
320318
gulp.src(ecosystem)
321319
.pipe($.jsonEditor(function(json) {
322320
json.deploy[answers.targetName] = {
@@ -326,7 +324,7 @@ gulp.task('add-deploy-target', function(done) {
326324
'ref': 'origin/' + answers.branch,
327325
'repo': answers.gitUrl,
328326
'path': answers.folder,
329-
'post-deploy': 'npm install; bower install; gulp build'
327+
'post-deploy': 'npm install && bower install && gulp build'
330328
};
331329
return json; // must return JSON object.
332330
}))
@@ -335,6 +333,42 @@ gulp.task('add-deploy-target', function(done) {
335333
});
336334
});
337335

336+
function ecosystemMustExist(ecosystem, name) {
337+
if (!fs.existsSync(ecosystem)) {
338+
try {
339+
var configJSON = {
340+
'apps': [{
341+
'name': name,
342+
'script': './node-server/node-app.js',
343+
'watch': true,
344+
'restart_delay': 4000,
345+
'env': {
346+
'NODE_ENV': 'local'
347+
},
348+
'env_local': {
349+
'NODE_ENV': 'local'
350+
},
351+
'env_dev': {
352+
'NODE_ENV': 'dev'
353+
},
354+
'env_prod': {
355+
'NODE_ENV': 'prod'
356+
}
357+
}],
358+
'deploy': {}
359+
};
360+
361+
var configString = JSON.stringify(configJSON, null, 2) + '\n';
362+
363+
fs.writeFileSync(ecosystem, configString, {
364+
encoding: 'utf8'
365+
});
366+
} catch (e) {
367+
console.log('failed to write ecosystem.json: ' + e.message);
368+
}
369+
}
370+
}
371+
338372
/**
339373
* Run the spec runner
340374
* @param {Function} done - callback when complete

0 commit comments

Comments
 (0)