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
133 changes: 106 additions & 27 deletions lib/yeoman/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,27 @@ var WordpressGenerator = yeoman.generators.Base.extend({
},

promptForRoles: function() {
// populate choices based on static roles array
var choices = [];
WordpressGenerator.roles.forEach(function (role) {
choices.push({
name: WordpressGenerator.padRight(role.name, 14) + ' - ' + role.desc,
value: role.name,
checked: role.default,
});
});

try {
var provisionYml = this.readFileAsString(path.join(this.env.cwd, 'lib', 'ansible', 'provision.yml'));

WordpressGenerator.roles.forEach(function (role, index) {
var regex = new RegExp('^\\s*-\\s+' + role.value.split(' ')[0], 'm');
// detect roles in simple or expanded yaml form
// TODO: parse as yaml & evaluate data, instead of brittle string/regex matches
choices.forEach(function (role, index) {
var simple = new RegExp('^\\s*-\\s+' + role.value, 'm');
var expanded = new RegExp('^\\s*-\\s*[{][^}]*?role:\\s*' + role.value, 'm');

if (!regex.exec(provisionYml)) {
WordpressGenerator.roles[index].checked = false;
if (!simple.exec(provisionYml) && !expanded.exec(provisionYml)) {
choices[index].checked = false;
}
});
} catch(e) {};
Expand All @@ -328,7 +341,7 @@ var WordpressGenerator = yeoman.generators.Base.extend({
type: 'checkbox',
name: 'roles',
message: 'Optional features',
choices: WordpressGenerator.roles,
choices: choices,
});
},

Expand Down Expand Up @@ -484,6 +497,54 @@ var WordpressGenerator = yeoman.generators.Base.extend({
}.bind(this));
},

promptForLocalVarnish: function() {
// test whether varnish role is used in the first place
var doVarnish = this.props.roles.filter(function (value) {
return value == 'varnish';
});

// prompt for local varnish bypass, as necessary
if (doVarnish.length) {
var choices = [
{
name: 'Yes, enable varnish locally',
value: true,
},
{
name: 'No, just on staging and production',
value: false,
},
];

try {
var provisionYml = this.readFileAsString(path.join(this.env.cwd, 'lib', 'ansible', 'provision.yml'));

if (provisionYml.indexOf("{ role: varnish, when: stage != 'local' }") != -1) {
choices.reverse();
}
} catch(e) {};

var done = this.async();

this.prompt(
[
{
required: true,
type: 'list',
name: 'varnishLocal',
message: 'Enable varnish on your local stage?',
choices: choices,
}
], function(props) {
this.props.varnishLocal = props.varnishLocal;

done();
}.bind(this));
} else {
this.props.varnishLocal = true;
}
},

prepareReadme: function() {
try {
this.readmeFile = this.readFileAsString(path.join(this.env.cwd, 'README.md'));
Expand Down Expand Up @@ -519,7 +580,17 @@ var WordpressGenerator = yeoman.generators.Base.extend({

prepareRoles: function() {
this.props.roles = WordpressGenerator.roles.map(function(role) {
role.checked = !!~this.props.roles.indexOf(role.value);
var checked = !!~this.props.roles.indexOf(role.name);

role.value = (checked ? '-' : '#') + ' ';

if (role.name == 'varnish' && !this.props.varnishLocal) {
role.value += "{ role: varnish, when: stage != 'local' }";
} else {
role.value += WordpressGenerator.padRight(role.name, 17);
}

role.value += ' # (Optional) ' + role.desc;

return role;
}.bind(this));
Expand Down Expand Up @@ -727,42 +798,50 @@ var WordpressGenerator = yeoman.generators.Base.extend({

WordpressGenerator.roles = [
{
name: 'apache-prefork - Dynamic Apache performance tuning',
value: 'apache-prefork # (Optional) Dynamic Apache performance tuning',
checked: true,
name: 'apache-prefork',
desc: 'Dynamic Apache performance tuning',
default: true,
},
{
name: 'php-hardened - Security-minded restrictions on PHP userland',
value: 'php-hardened # (Optional) Security-minded restrictions on PHP userland',
checked: true,
name: 'php-hardened',
desc: 'Security-minded restrictions on PHP userland',
default: true,
},
{
name: 'varnish - High-performance reverse-proxy cache',
value: 'varnish # (Optional) High-performance reverse-proxy cache',
checked: true,
name: 'varnish',
desc: 'High-performance reverse-proxy cache',
default: true,
},
{
name: 'mail - Ability to send e-mails from the server (i.e. PHP)',
value: 'mail # (Optional) Ability to send e-mails from the server (i.e. PHP)',
checked: true,
name: 'mail',
desc: 'Ability to send e-mails from the server (i.e. PHP)',
default: true,
},
{
name: 'firewall - Simple intrusion protection via Fail2ban + iptables',
value: 'firewall # (Optional) Simple intrusion protection via Fail2ban + iptables',
checked: true,
name: 'firewall',
desc: 'Simple intrusion protection via Fail2ban + iptables',
default: true,
},
{
name: 'debug - Tools to monitor processes & debug when on the server',
value: 'debug # (Optional) Tools to monitor processes & debug when on the server',
checked: true,
name: 'debug',
desc: 'Tools to monitor processes & debug when on the server',
default: true,
},
{
name: 'awstats - HTTP log analyzer',
value: 'awstats # (Optional) HTTP log analyzer',
checked: true,
name: 'awstats',
desc: 'HTTP log analyzer',
default: true,
},
];

WordpressGenerator.padRight = function(str, len, char) {
if (str.length < len) {
return str + Array(len - str.length + 1).join(char||" ");
} else {
return str;
}
};

WordpressGenerator.latest = function(username, repo, fn) {
request({
url: 'https://api.github.com/repos/' + username + '/' + repo + '/git/refs/tags/',
Expand Down
4 changes: 2 additions & 2 deletions lib/yeoman/templates/lib/ansible/provision.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
- wp-cli # (Required) CLI for managing WordPress

# Optional Features
<% if (props.ssl) { %>- pound # (Optional) SSL support & decryption<% } %><% props.roles.map(function(role) { %>
<%= role.checked ? '-' : '#' %> <%= role.value %><% }) %>
<% if (props.ssl) { %>- pound # (Optional) SSL support & decryption<% } %><% props.roles.forEach(function(role) { %>
<%= role.value %><% }) %>
<% if (props.newrelic) { %> - { role: newrelic, sudo: yes, when: stage == 'production' } # (Optional) New Relic application/server monitoring
<% } %><% if (props.datadog) { %> - { role: Datadog.datadog, sudo: yes, when: stage == 'production' } # (Optional) Datadog monitoring support
<% } %> # /Optional Features
Expand Down