Cloud Foundry / Pivotal Web Services by gregz67 · Pull Request #624 · angular-fullstack/generator-angular-fullstack · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gruntfile.js
4 changes: 3 additions & 1 deletion app/templates/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ module.exports = function (grunt) {
'<%%= yeoman.dist %>/*',
'!<%%= yeoman.dist %>/.git*',
'!<%%= yeoman.dist %>/.openshift',
'!<%%= yeoman.dist %>/Procfile'
'!<%%= yeoman.dist %>/.cfignore',
'!<%%= yeoman.dist %>/Procfile',
'!<%%= yeoman.dist %>/manifest.yml'
]
}]
},
Expand Down
2 changes: 1 addition & 1 deletion app/templates/server/config/environment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var all = {
root: path.normalize(__dirname + '/../../..'),

// Server port
port: process.env.PORT || 9000,
port: process.env.PORT || process.env.VCAP_APP_PORT || 9000,

// Should we populate the DB with sample data?
seedDB: false,
Expand Down
13 changes: 13 additions & 0 deletions app/templates/server/config/environment/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

// Production specific configuration
// =================================

// cloud foundry
var getCfMongo = function() {
var vcapServices = JSON.parse(process.env.vcapServices);
var mongoUri;
if (vcapServices.mongolab && vcapServices.mongolab.length > 0) {
mongoUri = vcapServices.mongolab[0].credentials.uri;
}
return mongoUri;
};

module.exports = {
// Server IP
ip: process.env.OPENSHIFT_NODEJS_IP ||
Expand All @@ -11,6 +22,7 @@ module.exports = {
// Server port
port: process.env.OPENSHIFT_NODEJS_PORT ||
process.env.PORT ||
process.env.VCAP_APP_PORT ||
8080,

// MongoDB connection options
Expand All @@ -19,6 +31,7 @@ module.exports = {
process.env.MONGOHQ_URL ||
process.env.OPENSHIFT_MONGODB_DB_URL +
process.env.OPENSHIFT_APP_NAME ||
getCfMongo() ||
'mongodb://localhost/<%= _.slugify(appname) %>'
}
};
8 changes: 8 additions & 0 deletions cloudfoundry/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Description:
Initalizes a cloud foundr app and generates a `dist` folder which is ready to push to cloud foundry.

Example:
yo angular-fullstack:cloudfoundry

This will create:
a dist folder and initialize a cloud foundry app
209 changes: 209 additions & 0 deletions cloudfoundry/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
'use strict';
var util = require('util');
var genUtils = require('../util.js');
var yeoman = require('yeoman-generator');
var exec = require('child_process').exec;
var chalk = require('chalk');
var path = require('path');

var Generator = module.exports = function Generator() {
yeoman.generators.Base.apply(this, arguments);
this.sourceRoot(path.join(__dirname, './templates'));

try {
this.appname = require(path.join(process.cwd(), 'bower.json')).name;
} catch (e) {
this.appname = path.basename(process.cwd());
}
this.appname = this._.slugify(this.appname);
this.filters = this.config.get('filters') || {};
};

util.inherits(Generator, yeoman.generators.NamedBase);

Generator.prototype.askForRoute = function askForRoute() {
var done = this.async();

var prompts = [
{
name: 'routeName',
message: 'Name of route to deploy (Leave blank for a random route name):'
}
];

this.prompt(prompts, function(props) {
this.routeName = this._.slugify(props.routeName);
done();
}.bind(this));
};

Generator.prototype.checkInstallation = function checkInstallation() {
if (this.abort) return;
var done = this.async();

exec('cf --version', function(err) {
if (err) {
this.log.error('You don\'t have the Cloud Foundry CLI installed. ' +
'Grab it from https://github.com/cloudfoundry/cli');
this.abort = true;
}
done();
}.bind(this));
};

Generator.prototype.askForApiEndpoint = function askForApiEndpoint() {
if (this.abort) return;
var done = this.async();

var prompts = [
{
name: 'apiEndpoint',
default: 'api.run.pivotal.io',
message: 'What api endpoint will you be using for Cloud Foundry?:'
}
];

this.prompt(prompts, function(props) {
this.apiEndpoint = props.apiEndpoint;
done();
}.bind(this));
};

Generator.prototype.cfInit = function cfInit() {
if (this.abort) return;
var done = this.async();

this.log(chalk.bold('Setting Cloud Foundry api endpoint'));
this.mkdir('dist');
var child = exec('cf api ' + this.apiEndpoint, { cwd: 'dist' }, function(err, stdout, stderr) {
if (err) {
this.abort = true;
this.log.error(err);
} else {
if (stdout.indexOf('Not logged in.') !== -1) {
this.log.error('You don\'t appear to be logged. Please login and try again.');
this.abort = true;
} else {
this.log('stdout: ' + stdout);
}

}
done();
}.bind(this));

child.stdout.on('data', function(data) {
this.log(this._.trim(data.toString(), "\n\r"));
}.bind(this));
}

Generator.prototype.copyProcfile = function copyProcfile() {
if (this.abort) return;
var done = this.async();
this.log(chalk.bold('Creating Procfile and manifest.yml'));
genUtils.processDirectory(this, '.', './dist');
this.conflicter.resolve(function(err) {
done();
});
};

Generator.prototype.gruntBuild = function gruntBuild() {
if (this.abort) return;
var done = this.async();

this.log(chalk.bold('\nBuilding dist folder, please wait...'));
var child = exec('grunt build', function(err, stdout) {
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};

Generator.prototype.cfPush = function cfPush() {
if (this.abort) return;
var done = this.async();

this.log(chalk.bold("\nUploading your initial application code.\n This may take " + chalk.cyan('several minutes') + " depending on your connection speed..."));

var randomRoute = this.routeName === '' ? '--random-route' : '';
var child = exec(['cf push', this.appname, randomRoute, ' --no-start'].join(' '), { cwd: 'dist' }, function(err, stdout, stderr) {
if (err) {
this.abort = true;
this.log.error(err);
} else {
this.log('stdout: ' + stdout);
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(this._.trim(data.toString(), "\n\r"));
}.bind(this));
};

Generator.prototype.cfSetEnvVars = function cfSetEnvVars() {
if (this.abort) return;
var done = this.async();

var child = exec('cf set-env ' + this.appname + ' NODE_ENV production', { cwd: 'dist' }, function(err, stdout, stderr) {
if (err) {
this.abort = true;
this.log.error(err);
}
done();

}.bind(this));
child.stdout.on('data', function(data) {
this.log(this._.trim(data.toString(), "\n\r"));
}.bind(this));
};

Generator.prototype.cfStart = function cfStart() {
if (this.abort) return;
var done = this.async();

var child = exec('cf start ' + this.appname, { cwd: 'dist' }, function(err, stdout, stderr) {
if (err) {
this.abort = true;
this.log.error(err);
} else {
var hasWarning = false;

if (this.filters.mongoose) {
this.log(chalk.yellow('\nBecause you\'re using mongoose, you must add mongoDB to your cloud foundry app.\n\t' + 'from `/dist`: ' + chalk.bold('cf create-service mongolab sandbox my-mongo') + '\n'));
hasWarning = true;
}

if (this.filters.facebookAuth) {
this.log(chalk.yellow('You will need to set environment variables for facebook auth. From `/dist`:\n\t' +
chalk.bold('cf set-env ' + this.appname + ' FACEBOOK_ID appId\n\t') +
chalk.bold('cf set-env ' + this.appname + ' FACEBOOK_SECRET secret\n')));
hasWarning = true;
}
if (this.filters.googleAuth) {
this.log(chalk.yellow('You will need to set environment variables for google auth. From `/dist`:\n\t' +
chalk.bold('cf set-env ' + this.appname + ' GOOGLE_ID appId\n\t') +
chalk.bold('cf set-env ' + this.appname + ' GOOGLE_SECRET secret\n')));
hasWarning = true;
}
if (this.filters.twitterAuth) {
this.log(chalk.yellow('You will need to set environment variables for twitter auth. From `/dist`:\n\t' +
chalk.bold('cf set-env ' + this.appname + ' TWITTER_ID appId\n\t') +
chalk.bold('cf set-env ' + this.appname + ' TWITTER_SECRET secret\n')));
hasWarning = true;
}

this.log(chalk.green('\nYour app should now be live.'));
if (hasWarning) {
this.log(chalk.green('\nYou may need to address the issues mentioned above and restart the server for the app to work correctly.'));
}
this.log(chalk.yellow('After app modification run\n\t' + chalk.bold('grunt build') +
'\nThen deploy (from dist directory ) with\n\t' + chalk.bold('cf push')));
}
done();

}.bind(this));
child.stdout.on('data', function(data) {
this.log(this._.trim(data.toString(), "\n\r"));
}.bind(this));

};
1 change: 1 addition & 0 deletions cloudfoundry/templates/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node server/app.js
5 changes: 5 additions & 0 deletions cloudfoundry/templates/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
applications:
- name: <%= appname %>
buildpack: https://github.com/cloudfoundry/heroku-buildpack-nodejs.git
command: node server/app.js
31 changes: 31 additions & 0 deletions readme.md
Loading