From d93ed417d9c3430e795df0c2c411da8eb5297fa3 Mon Sep 17 00:00:00 2001 From: Jason Stillerman Date: Mon, 25 Jul 2016 11:31:48 -0400 Subject: [PATCH] Added built-in roles Added a nginx role and now health checks pass out of box Added a file transfer role --- lib/Generator.js | 20 +++++++++++++- lib/questions.js | 12 +++++++++ lib/templates/HelloWorld.txt.mustache | 5 ++++ lib/templates/immutable.yaml.mustache | 6 +++++ lib/templates/roles/nginx/tasks/main.yaml | 10 +++++++ .../roles/nginx/templates/default.conf | 27 +++++++++++++++++++ lib/templates/roles/push-code/tasks/main.yaml | 7 +++++ package.json | 1 + spec/lib/questionsSpec.js | 4 +-- 9 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 lib/templates/HelloWorld.txt.mustache create mode 100644 lib/templates/roles/nginx/tasks/main.yaml create mode 100644 lib/templates/roles/nginx/templates/default.conf create mode 100644 lib/templates/roles/push-code/tasks/main.yaml diff --git a/lib/Generator.js b/lib/Generator.js index d442299..25baf40 100644 --- a/lib/Generator.js +++ b/lib/Generator.js @@ -5,6 +5,7 @@ var fs = require('fs'); var Q = require('q'); var mkdirp = require('mkdirp'); + var ncp = require('ncp').ncp; // create promise versions of read and write files var read = Q.denodeify(fs.readFile); var write = Q.denodeify(fs.writeFile); @@ -15,12 +16,23 @@ answers.tag_old_app = 'old' + answers.appName.charAt(0).toUpperCase() + answers.appName.slice(1); answers.converted_environment = answers.environment.replace(/-/g, "_"); + answers.roles = {}; + answers.builtin_roles.forEach(function(role){ + answers.roles[role] = true; + }); // create promise versions of chmod and mkdirp var chmod = Q.denodeify(fs.chmod); var mkdir = Q.denodeify(mkdirp); + + function cpdir(dir, dest) { + ncp(__dirname+'/templates/'+dir, dest, function(err){ + if(err) console.log(err); + return Promise.resolve(); + }); + } //function that handles .gitignore function gitignore(path) { return function() { @@ -33,6 +45,7 @@ // Kick off a promise chain mkdir('ansible/inventory', '0755') + //anible folder .then(process('ansible.cfg.mustache', 'ansible/ansible.cfg')) .then(process('aws_keys.mustache', 'ansible/inventory/aws_keys')) .then(gitignore('ansible/inventory/aws_keys')) @@ -43,13 +56,18 @@ .then(process('provision.sh.mustache', 'ansible/provision.sh')) .then(process('tag-old-nodes.yaml.mustache', 'ansible/tag-old-nodes.yaml')) .then(mkdir('ansible/roles/' + answers.appName + '/tasks')) + .then(function(){if(answers.roles.Nginx) cpdir('roles/nginx/', 'ansible/roles/nginx');}) + .then(function(){if(answers.roles["File Transfer"]) cpdir('roles/push-code/', 'ansible/roles/push-code');}) .then(process('roles/appName/tasks/main.yaml.mustache', 'ansible/roles/' + answers.appName + '/tasks/main.yaml')) .then(process('myApp.pem.mustache', 'ansible/' + answers.appPem)) .then(gitignore('ansible/' + answers.appPem)) - .finally(function() { + .then(function() { fs.chmod('ansible/provision.sh', '0744'); fs.chmod('ansible/inventory/ec2.py', '0755'); }) + .then(mkdir('HelloWorld/')) + .finally(process('HelloWorld.txt.mustache', "HelloWorld/HelloWorld.txt")) + .catch(handler); diff --git a/lib/questions.js b/lib/questions.js index 4e6c236..e72287e 100644 --- a/lib/questions.js +++ b/lib/questions.js @@ -361,6 +361,18 @@ 'base-64 string with nothing before or after'; return validateInput(value, regex, errorMessage); } + }, { + type: 'checkbox', + name: 'builtin_roles', + message: 'What roles would you like to include', + choices: [ + { + name: "File Transfer" + }, + { + name: "Nginx" + } + ] }]; module.exports = questions; diff --git a/lib/templates/HelloWorld.txt.mustache b/lib/templates/HelloWorld.txt.mustache new file mode 100644 index 0000000..5cd25a9 --- /dev/null +++ b/lib/templates/HelloWorld.txt.mustache @@ -0,0 +1,5 @@ +This file will be transfered over to all the new machines. +You can change the path of the transfered directory in ansible/roles/push-code/tasks/main.yaml + + +Hello world! diff --git a/lib/templates/immutable.yaml.mustache b/lib/templates/immutable.yaml.mustache index 5a5dc71..b0c5b5c 100644 --- a/lib/templates/immutable.yaml.mustache +++ b/lib/templates/immutable.yaml.mustache @@ -41,6 +41,12 @@ user: {{{ remote_user }}} sudo: yes roles: + {{#roles.Nginx}} + - nginx + {{/roles.Nginx}} + {{#roles['File Transfer']}} + - push-code + {{/roles['File Transfer']}} - {{{ appName }}} - hosts: ec2hosts diff --git a/lib/templates/roles/nginx/tasks/main.yaml b/lib/templates/roles/nginx/tasks/main.yaml new file mode 100644 index 0000000..996a8f0 --- /dev/null +++ b/lib/templates/roles/nginx/tasks/main.yaml @@ -0,0 +1,10 @@ +--- +- name: Ensure nginx stable PPA + apt_repository: repo='ppa:nginx/stable' update_cache="yes" + +- name: Install nginx full + apt: name=nginx-full state=installed + +- name: Update default site config + template: src=default.conf dest=/etc/nginx/sites-available/default + owner=root group=root mode=0440 diff --git a/lib/templates/roles/nginx/templates/default.conf b/lib/templates/roles/nginx/templates/default.conf new file mode 100644 index 0000000..a326205 --- /dev/null +++ b/lib/templates/roles/nginx/templates/default.conf @@ -0,0 +1,27 @@ +server { + listen 80 default_server; + listen [::]:80 default_server; + + root /opt/dashboard; + + # Add index.php to the list if you are using PHP + index index.html index.htm index.nginx-debian.html; + + server_name _; + + + # Reverse proxy to services + location / { + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_set_header X-NginX-Proxy true; + proxy_pass http://127.0.0.1:3000; + proxy_redirect off; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + + proxy_redirect off; + } +} diff --git a/lib/templates/roles/push-code/tasks/main.yaml b/lib/templates/roles/push-code/tasks/main.yaml new file mode 100644 index 0000000..a53726e --- /dev/null +++ b/lib/templates/roles/push-code/tasks/main.yaml @@ -0,0 +1,7 @@ +--- +- name: Push up code + synchronize: src=../../../../HelloWorld/ + dest=/opt/HelloWorld + recursive=yes + rsync_opts="--verbose, --exclude ansible --exclude docs --exclude .git --exclude .idea --exclude .DS_Store --exclude .awscredentials --exclude .gitignore --exclude tests" + archive=no diff --git a/package.json b/package.json index 84cd0d1..2618950 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "inquirer": "^0.11.0", "mkdirp": "^0.5.1", "mustache": "^2.2.1", + "ncp": "^2.0.0", "openurl": "^1.1.0", "q": "^1.4.1" }, diff --git a/spec/lib/questionsSpec.js b/spec/lib/questionsSpec.js index bdeb0f5..e279707 100644 --- a/spec/lib/questionsSpec.js +++ b/spec/lib/questionsSpec.js @@ -2,8 +2,8 @@ var questions = require('../../lib/questions.js'); describe('questions.js', function() { describe('format', function() { - it('should be an array of 25 questions', function() { - expect(questions.length).toEqual(25); + it('should be an array of 26 questions', function() { + expect(questions.length).toEqual(26); }); }); });