forked from bengourley/Node-Deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJakefile.js
194 lines (146 loc) · 5.47 KB
/
Jakefile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
desc('Install required npm modules');
task('install-npm-depends', [], function () {
console.log("\n\n > Attempting to install dependencies via npm\n".blue);
// Get the fs module
var spawn = require('child_process').spawn;
console.log(" Executing command:\n $ npm install\n".grey);
npm = spawn("npm", ["install"]);
npm.stdout.on('data', function (data) {
process.stdout.write((" " + data).grey);
});
npm.stderr.on('data', function (data) {
process.stdout.write((" " + data).grey);
});
npm.on('exit', function (code) {
if (code === 0) {
console.log("\n + npm installed dependencies successfully".green);
complete();
} else {
throw new Error("npm exited with error code " + code);
}
});
}, true);
// Initialise properties
var properties = null,
versionedPath = null,
livePath = null;
desc('Loads in properties file');
task('load-props', ["install-npm-depends"], function() {
console.log("\n\n > Attempting to read in build properties\n".blue);
var fs = require("fs");
// Read in and parse build properties
properties = JSON.parse(fs.readFileSync('config/props.json'));
// Print the properties to the console
for (var p in properties) {
if (properties.hasOwnProperty(p)) {
console.log((" " + p + " : " + properties[p]).grey);
}
}
// Build some paths from properties for use later on
versionedPath = properties.siteLocation + properties.state + "/.versions/" +
properties.siteName + "@" + properties.version + "-" + new Date().getTime();
livePath = properties.siteLocation + properties.state + "/" + properties.siteName;
console.log("\n + Properties read successfully".green);
complete();
}, true);
desc('Create versioned site directory');
task('create-versioned-dir', ["load-props"], function() {
console.log("\n\n > Attempting to create versioned directory\n".blue);
var exec = require('child_process').exec,
mkdir;
console.log((" Executing command:\n $ mkdir " + versionedPath + "\n").grey);
// Create versioned directory
mkdir = exec("mkdir " + versionedPath, function (error, stdout, stderr) {
if (error !== null) {
console.log(error.message);
throw error;
} else {
console.log((" + Versioned directory created successfully").green);
complete();
}
});
}, true);
desc('Move files to desired location');
task('move-files', ["load-props", "create-versioned-dir"], function() {
console.log("\n\n > Attempting to moved files into desired location\n".blue);
var exec = require('child_process').exec,
rsync;
console.log((" Executing command:\n $ rsync -a . " + versionedPath).grey);
// Move files from temporary directory to versioned directory just created
rsync = exec("rsync -a . " + versionedPath, function (error, stdout, stderr) {
if (error !== null) {
console.log(error.message);
throw error;
} else {
console.log("\n + Files moved successfully".green);
complete();
}
});
}, true);
desc('Symlink new version');
task('symlink-live', ["load-props", "create-versioned-dir", "move-files"], function() {
console.log("\n\n > Attempting to make a symbolic link\n".blue);
var exec = require('child_process').exec,
ln;
console.log((" Executing command:\n $ rm " + livePath + " && ln -sv " + versionedPath + " " + livePath).grey);
// Symlink to the versioned directory
ln = exec("rm " + livePath + " && ln -sv " + versionedPath + " " + livePath, function (error, stdout, stderr) {
if (error !== null && error.message.indexOf("No such file or directory") === -1) {
console.log(error.message);
throw error;
} else {
console.log("\n + Symlink created".green);
complete();
}
});
}, true);
desc('Puts the site live');
task('default', ["load-props", "create-versioned-dir", "move-files", "symlink-live"], function() {
console.log("\n\n > Attempting to put the site live\n".blue);
var exec = require('child_process').exec,
spawn = require('child_process').spawn,
upstart;
console.log((" Executing command:\n $ sudo monit stop " + properties.siteName + "-" + properties.state).grey);
// Stop the old version of the app and start the new version with monit
exec("sudo monit stop " + properties.siteName + "-" + properties.state, function (error, stdout, stderr) {
if (error) {
throw error;
} else {
console.log(("\n Executing command:\n $sudo monit start " +
properties.siteName + "-" + properties.state).grey);
exec("sudo monit start " + properties.siteName + "-" + properties.state, function (error, stdout, stderr) {
console.log("\n + Old instance killed successfully\n".green);
if (error) {
console.log(" + New instance failed to be put live\n".red);
throw error;
} else {
console.log(" + New instance is put live\n".green);
complete();
}
});
}
});
});
function stylize(str, style) {
var styles = {
//styles
'bold' : [1, 22], 'italic' : [3, 23],
'underline' : [4, 24], 'inverse' : [7, 27],
//grayscale
'white' : [37, 39], 'grey' : [90, 39],
'black' : [90, 39],
//colors
'blue' : [34, 39], 'cyan' : [36, 39],
'green' : [32, 39], 'magenta' : [35, 39],
'red' : [31, 39],'yellow' : [33, 39]
};
return '\033[' + styles[style][0] + 'm' + str + '\033[' + styles[style][1] + 'm';
}
['bold', 'underline', 'italic',
'inverse', 'grey', 'yellow',
'red', 'green', 'blue',
'white', 'cyan', 'magenta'].forEach(function (style) {
String.prototype.__defineGetter__(style, function () {
return stylize(this, style);
});
});