Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

A bigger refactor #5

Merged
merged 14 commits into from
This page is out of date. Refresh to see the latest.
View
16 README.md
@@ -34,8 +34,20 @@ santa config set provider:provider <pkgcloud-provider-name>
`santa` expects 3 files in your application:
* `package.json`
- * `scripts/install` - should install your application
- * `scripts/start` - should start your application
+ * `scripts/install`
+ * `scripts/configure`
+ * `scripts/start`
+
+#### Scripts
+
+##### `scripts/install`
+Install all your software.
+
+##### `scripts/configure`
+Configure all your software.
+
+##### `scripts/start`
+Start all your software.
Once you set up the structure, simply execute `santa deploy` in your application
directory.
View
38 lib/utils/deploy.js → lib/bootstrapper/bootstrap.js
@@ -1,12 +1,12 @@
var path = require('path');
-var animal = require('animal-id');
var pkgcloud = require('pkgcloud');
var Bootstrapper = require('pkgcloud-bootstrapper');
var santa = require('../../');
-var inspect = require('./inspect');
+var inspect = require('../utils/inspect');
+var image = require('./image.js');
-module.exports = function deploy(options, callback) {
- var client = pkgcloud.compute.createClient(options.compute);
+module.exports = function bootstrap(options, callback) {
+ var client = options.compute;
var bootstrapper = new Bootstrapper({
compute: client,
@@ -17,32 +17,31 @@ module.exports = function deploy(options, callback) {
});
var name = options.name;
- if (!name && options.app) {
- name = options.app.name + '-' + options.app.version + '-' + animal.getId();
- }
- else {
- name = animal.getUuid();
- }
client.listKeys(function (err, keys) {
if (err) {
return callback(err);
}
+ var commands = [
+ 'tar -zxvf package.tgz',
+ 'cd package'
+ ];
+
+ var scripts = options.scripts || ['install', 'configure', 'start'];
+ Array.prototype.push.apply(commands, scripts.map(function (script) {
+ return 'scripts/' + script;
+ }));
+
var createOptions = {
name: name,
files: [
{ source: options.package, target: 'package.tgz' }
],
- commands: [
- 'tar -zxvf package.tgz',
- 'cd package',
- 'scripts/install',
- 'scripts/start'
- ],
+ commands: commands,
keynames: keys.map(function (key) { return key.id }),
- image: '1505699', // TODO: unhardcode from Ubuntu 13.10 x64 on DO
- flavor: '63' // TODO: unhardcode from 1 GB on DO
+ image: options.image,
+ flavor: options.flavor
};
santa.inspect.putObject(createOptions);
@@ -74,8 +73,7 @@ module.exports = function deploy(options, callback) {
ee.on('complete', function (server) {
santa.log.info('Server creation complete');
inspect.server(server);
+ callback(null, server);
});
-
- callback(null, ee);
});
};
View
0  lib/utils/build.js → lib/bootstrapper/build.js
File renamed without changes
View
21 lib/bootstrapper/image.js
@@ -0,0 +1,21 @@
+var async = require('async');
+var uuid = require('node-uuid');
+
+module.exports = function image(options, callback) {
+ var client = options.compute;
+ var server = options.server;
+ var name = options.name;
+ var image;
+
+ santa.log.info('Imaging the server');
+ async.waterfall([
+ client.shutdownServer.bind(client, server),
+ function createImage(next) {
+ client.createImage({
+ server: server,
+ name: name,
+ }, next);
+ },
+ client.destroyServer.bind(client, server)
+ ], callback);
+};
View
32 lib/commands/deploy.js
@@ -1,11 +1,20 @@
var path = require('path');
var santa = require('../..');
var async = require('async');
+var animal = require('animal-id');
var readPackageJson = require('read-package-json');
-var build = require('../utils/build.js');
-var deploy = require('../utils/deploy.js');
+var build = require('../bootstrapper/build.js');
+var bootstrap = require('../bootstrapper/bootstrap.js');
module.exports = function (callback) {
+ var flavor = santa.config.get('flavor');
+ var image = santa.config.get('image');
+
+ if (!flavor || !image) {
+ santa.log.error('Flavor and image (`--flavor` and `--image`) are required');
+ return;
+ }
+
santa.log.info('Deploying application (hold on to your butts!)...');
santa.log.info('Building application...');
@@ -13,9 +22,9 @@ module.exports = function (callback) {
async.parallel({
build: async.apply(build, { path: cwd }),
- readPackage: async.apply(readPackageJson, path.join(cwd, 'package.json'))
+ package: async.apply(readPackageJson, path.join(cwd, 'package.json'))
}, function (err, results) {
- var output;
+ var output, options;
if (err) {
bender.log.error('Build failed');
@@ -24,11 +33,15 @@ module.exports = function (callback) {
santa.log.info('Application build to ' + results.build);
- deploy({
- compute: santa.config.get('provider'),
+ options = {
+ compute: santa.pkgcloud,
package: results.build,
- app: results.readPackage
- }, function (err, ee) {
+ name: [ results.package.name, results.package.version, animal.getId() ].join('-'),
+ flavor: flavor,
+ image: image
+ };
+
+ bootstrap(options, function (err, ee) {
if (err) {
santa.log.error('Error while preparing server creation');
return callback(err);
@@ -37,7 +50,4 @@ module.exports = function (callback) {
ee.on('error', callback);
});
});
-
- build({ path: process.cwd() }, function (err, output) {
- });
};
View
43 lib/commands/image.js
@@ -0,0 +1,43 @@
+var path = require('path');
+var santa = require('../..');
+var async = require('async');
+var readPackageJson = require('read-package-json');
+var build = require('../bootstrapper/build.js');
+var bootstrap = require('../bootstrapper/bootstrap.js');
+
+module.exports = function (callback) {
+ santa.log.info('Imaging application...');
+ santa.log.info('Building application...');
+
+ var cwd = process.cwd();
+
+ async.parallel({
+ build: async.apply(build, { path: cwd }),
+ readPackage: async.apply(readPackageJson, path.join(cwd, 'package.json'))
+ }, function (err, results) {
+ var output, options;
+
+ if (err) {
+ bender.log.error('Build failed');
+ return callback(err);
+ }
+
+ santa.log.info('Application build to ' + results.build);
+
+ options = {
+ compute: santa.pkgcloud,
+ package: results.build,
+ name: results.app
+ image: santa.argv.image
+ };
+
+ image(options, function (err, ee) {
+ if (err) {
+ santa.log.error('Error while preparing server creation');
+ return callback(err);
+ }
+
+ ee.on('error', callback);
+ });
+ });
+};
View
9 lib/commands/images.js
@@ -1,3 +1,4 @@
+var async = require('async');
var santa = require('../../');
var inspect = require('../utils/inspect');
@@ -11,3 +12,11 @@ exports.list = function (callback) {
callback();
});
};
+
+exports.destroy = function () {
+ var args = Array.apply(null, arguments);
+ var ids = args.slice(0, args.length - 1);
+ var callback = args[args.length - 1];
+
+ async.forEachLimit(ids, 4, santa.pkgcloud.destroyImage.bind(santa.pkgcloud), callback);
+};
View
7 lib/utils/inspect.js
@@ -23,6 +23,13 @@ exports.server = function (server) {
santa.inspect.putObject(obj);
};
+exports.image = function (image) {
+ santa.inspect.putObject({
+ id: image.id,
+ name: image.name
+ });
+};
+
exports.images = function (images) {
var rows = [['id', 'name']];
var colors = ['underline', 'underline'];
View
5 package.json
@@ -7,12 +7,13 @@
"flatiron": "~0.3.11",
"flatiron-cli-config": "~0.1.4",
"animal-id": "0.0.1",
- "pkgcloud-bootstrapper": "~0.1.0",
+ "pkgcloud-bootstrapper": "~0.2.0",
"async": "~0.2.9",
"tar": "~0.1.19",
"fstream": "~0.1.25",
"fstream-npm": "~0.1.6",
- "read-package-json": "~1.1.4"
+ "read-package-json": "~1.1.4",
+ "node-uuid": "~1.4.1"
},
"main": "./lib/santa.js",
"bin": {
View
1  scripts/configure
@@ -0,0 +1 @@
+#!/usr/bin/env bash
View
4 scripts/install
@@ -1,5 +1,3 @@
#!/usr/bin/env bash
-dir=$(pwd)
-cd /usr/local && curl http://nodejs.org/dist/v0.10.23/node-v0.10.23-linux-x64.tar.gz | tar -xzvf- --strip-components=1
-cd "$dir"
+curl http://nodejs.org/dist/v0.10.23/node-v0.10.23-linux-x64.tar.gz | tar -xzvf- -C/usr/local --strip-components=1
npm install --production
Something went wrong with that request. Please try again.