UNPKG

6.36 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.3.1
2
3/*
4CLI to automatically deploy stuff, kind of like heroku.
5Ubuntu only! (upstart)
6
7TODO remember last command again
8TODO multiple services
9TODO multiple cron
10*/
11
12
13(function() {
14 var COMMIT_HISTORY, CONFIG, LOGS_LINES, Layer, MainConfig, VERSION, exec, finish, fs, getConfigRepo, getLayer, init, list, mainConfigPath, path, program, reponame;
15
16 CONFIG = "ggg";
17
18 LOGS_LINES = 40;
19
20 COMMIT_HISTORY = 5;
21
22 VERSION = "0.4.4";
23
24 exec = require("child_process").exec;
25
26 fs = require('fs');
27
28 path = require('path');
29
30 program = require('commander');
31
32 MainConfig = require("./lib/MainConfig");
33
34 Layer = require("./lib/Layer");
35
36 program.version(VERSION).option("-l, --local", "deploy locally for bootstrapping");
37
38 program.command("init").description("creates a ggg.js config file for you").action(function() {
39 return init(finish);
40 });
41
42 program.command("deploy <name> [branch]").description("deploys a branch (defaults to origin/master) to named server").action(function(name, branch) {
43 return getLayer(name, function(err, layer) {
44 if (err != null) {
45 return finish(err);
46 }
47 branch = branch || "origin/master";
48 return layer.deploy(branch, finish);
49 });
50 });
51
52 program.command("restart <name>").description("restarts named server").action(function(name) {
53 return getLayer(name, function(err, layer) {
54 if (err != null) {
55 return finish(err);
56 }
57 return layer.restart(finish);
58 });
59 });
60
61 program.command("start <name>").description("starts named server").action(function(name) {
62 return getLayer(name, function(err, layer) {
63 if (err != null) {
64 return finish(err);
65 }
66 return layer.start(finish);
67 });
68 });
69
70 program.command("stop <name>").description("stops named server").action(function(name) {
71 return getLayer(name, function(err, layer) {
72 if (err != null) {
73 return finish(err);
74 }
75 return layer.stop(finish);
76 });
77 });
78
79 program.command("logs <name>").description("Logs " + LOGS_LINES + " lines of named servers log files").option("-l, --lines <num>", "the number of lines to log").action(function(name) {
80 return getLayer(name, function(err, layer) {
81 var lines;
82 if (err != null) {
83 return finish(err);
84 }
85 lines = program.lines || LOGS_LINES;
86 return layer.serverLogs(lines, finish);
87 });
88 });
89
90 program.command("history <name>").description("Shows a history of " + COMMIT_HISTORY + " last commits deployed").option("-r, --revisions <num>", "the number of commits to show").action(function(name) {
91 return getLayer(name, function(err, layer) {
92 var revisions;
93 if (err != null) {
94 return finish(err);
95 }
96 revisions = program.args.revisions || COMMIT_HISTORY;
97 return layer.commitHistory(revisions, finish);
98 });
99 });
100
101 program.command("list").description("lists all the servers").action(function() {
102 return getConfigRepo(function(err, repoName, mainConfig) {
103 if (err != null) {
104 return finish(err);
105 }
106 return list(mainConfig, finish);
107 });
108 });
109
110 program.command("help").description("display this help").action(function() {
111 console.log(program.helpInformation());
112 return finish();
113 });
114
115 program.command("*").action(function() {
116 return finish(new Error("bad command!"));
117 });
118
119 init = function(cb) {
120 var initConfigContent;
121 initConfigContent = "// example ggg.js. Delete what you don't need\nmodule.exports = {\n\n // services\n start: \"node app.js\",\n\n // install\n install: \"npm install\",\n\n // cron jobs (from your app folder)\n cron: {\n someTask: { time: \"0 3 * * *\", command: \"node sometask.js\"},\n }\n\n // servers to deploy to\n servers: {\n dev: \"deploy@dev.mycompany.com\",\n staging: [\"deploy@staging.mycompany.com\", \"deploy@staging2.mycompany.com\"]\n prod: {\n hosts: [\"deploy@mycompany.com\", \"deploy@backup.mycompany.com\"],\n cron: {\n someTask: {time: \"0 3 * * *\", command: \"node sometask.js\"},\n anotherTask: {time: \"0 3 * * *\", command: \"node secondTask.js\"}\n },\n start: \"prodstart app.js\"\n }\n }\n}";
122 console.log("GOGOGO INITIALIZING!");
123 console.log("*** Written to ggg.js ***");
124 console.log(initConfigContent);
125 return fs.writeFile(mainConfigPath() + ".js", initConfigContent, 0x1ed, cb);
126 };
127
128 list = function(mainConfig, cb) {
129 console.log("GOGOGO servers (see ggg.js)");
130 return console.log(" - " + mainConfig.getLayerNames().join("\n - "));
131 };
132
133 reponame = function(dir, cb) {
134 return exec("git config --get remote.origin.url", {
135 cwd: dir
136 }, function(err, stdout, stderr) {
137 var url;
138 if (err != null) {
139 return cb(null, path.basename(dir));
140 } else {
141 url = stdout.replace("\n", "");
142 return cb(null, path.basename(url).replace(".git", ""));
143 }
144 });
145 };
146
147 mainConfigPath = function() {
148 return path.join(process.cwd(), CONFIG);
149 };
150
151 getConfigRepo = function(cb) {
152 return reponame(process.cwd(), function(err, repoName) {
153 if (err != null) {
154 return cb(err);
155 }
156 return MainConfig.loadFromFile(mainConfigPath(), function(err, mainConfig) {
157 if (err) {
158 return cb(new Error("Bad gogogo config file, ggg.js. Run 'gogogo init' to create one. Err=" + err.message));
159 }
160 return cb(null, repoName, mainConfig);
161 });
162 });
163 };
164
165 getLayer = function(name, cb) {
166 return getConfigRepo(function(err, repoName, mainConfig) {
167 var layer, layerConfig;
168 if (err != null) {
169 return cb(err);
170 }
171 layerConfig = mainConfig.getLayerByName(name);
172 if (!layerConfig) {
173 return cb(new Error("Invalid Layer Name: " + name));
174 }
175 layer = new Layer(name, layerConfig, repoName, mainConfig, program.local);
176 layer.on("error", function(err) {
177 return cb(err);
178 });
179 return layer.on("ready", function() {
180 return cb(null, layer);
181 });
182 });
183 };
184
185 finish = function(err) {
186 if (err != null) {
187 console.log("!!! " + err.message);
188 console.log("stack follows:\n\n " + err.stack);
189 process.exit(1);
190 }
191 return console.log("OK");
192 };
193
194 module.exports = program;
195
196}).call(this);