-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJakefile.js
167 lines (129 loc) · 4.22 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
desc('Install required npm modules');
task('install-npm-depends', [], function () {
// Get npm programatically and the fs module
var exec = require('child_process').exec;
exec("npm install", function (error, stdout, stderr) {
if (error !== null) {
console.log(error.message);
throw error;
} else {
console.log(stdout);
complete();
}
});
}, true);
// Initialise properties
var properties = null,
versionedPath = null,
gitPath = null,
livePath = null;
desc('Loads in properties file');
task('load-props', ["install-npm-depends"], function() {
console.log("Attempting to read in build properties");
var fs = require("fs");
// Read in and parse build properties
properties = JSON.parse(fs.readFileSync('config/props.json'));
// Print the properties to the console
console.log("Properties:");
for (var p in properties) {
if (properties.hasOwnProperty(p)) {
console.log(p + properties[p]);
}
}
// Build some paths from properties for use later on
versionedPath = properties.siteLocation + properties.state + "/.versions/" +
properties.siteName + "@" + properties.version;
livePath = properties.siteLocation + properties.state + "/" + properties.siteName;
gitPath = "~/" + properties.siteName + ".net.git";
console.log("Properties read successfully");
complete();
}, true);
desc('Create versioned site directory');
task('create-versioned-dir', ["load-props"], function() {
console.log("Attempting to create versioned directory");
var exec = require('child_process').exec,
mkdir;
// 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 - " + versionedPath);
complete();
}
});
}, true);
desc('Move files to desired location');
task('move-files', ["load-props", "create-versioned-dir"], function() {
console.log("Attempting to moved files into desired location");
var exec = require('child_process').exec,
rsync;
// Move files from temporary directory to versioned directory just created
rsync = exec("rsync -av . " + versionedPath, function (error, stdout, stderr) {
if (error !== null) {
console.log(error.message);
throw error;
} else {
console.log("Files moved successfully");
complete();
}
});
}, true);
desc('Symlink new version');
task('symlink-live', ["load-props", "create-versioned-dir", "move-files"], function() {
console.log("Attempting to make a symbolic link");
var exec = require('child_process').exec,
ln;
// Symlink to the versioned directory
var command = "rm " + livePath + " && ln -sv " + versionedPath + " " + livePath;
console.log ("running: "+command);
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(error + " "+ stdout+ " " + stderr);
console.log("Symlink created");
complete();
}
});
}, true);
desc('Puts the development site live');
task('default', ["load-props", "create-versioned-dir", "move-files", "symlink-live"], function() {
console.log("Attempting to kill old instance of site");
var exec = require('child_process').exec,
spawn = require('child_process').spawn,
upstart;
// Stop the old version of the app and start the new version with monit
var command = "sudo monit stop " + properties.siteName + "-" + properties.state;
console.log("About to run: "+command);
exec("whoami", function (error, stdout, stderr) {
if (error) {
throw error;
} else {
console.log(error, stdout, stderr);
}
});
exec("ln -sv "+gitPath+" " + livePath+"/.git", function (error, stdout, stderr) {
if (error) {
throw error;
} else {
console.log(error, stdout, stderr);
}
});
exec(command, function (error, stdout, stderr) {
if (error) {
throw error;
} else {
exec("sudo monit start " + properties.siteName + "-" + properties.state, function (error, stdout, stderr) {
console.log(error, stdout, stderr);
if (error) {
throw error;
} else {
complete();
}
});
}
});
});