Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre Launch #12

Merged
merged 49 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e8ef987
Add a VPC Cloudformation template
vladgh Jan 14, 2016
516be5c
Add security groups cloudformation template
vladgh Jan 14, 2016
c3f0bd1
Update README
vladgh Jan 15, 2016
148eced
Fix alignment
vladgh Jan 15, 2016
a009409
Update modules
vladgh Jan 15, 2016
a9b4f10
Improve bootstrap
vladgh Jan 16, 2016
105115c
Add puppet color support in the bootstrap script
vladgh Jan 17, 2016
afa071a
Add AWS IAM cloudformation template
vladgh Jan 17, 2016
c665520
Validate Cloudformation templates when committing
vladgh Jan 17, 2016
fcf83c9
Minor change
vladgh Jan 17, 2016
40169b3
Add common functions and variables
vladgh Jan 17, 2016
8aaf7f3
Add AWS functions
vladgh Jan 17, 2016
d0fbc30
Add a private .env file
vladgh Jan 17, 2016
5c03531
Improve shebang
vladgh Jan 17, 2016
5ea6fa7
Move functions to a separate include folder
vladgh Jan 17, 2016
6d6eee5
Update README
vladgh Jan 17, 2016
1561add
Add main CloudFormation template
vladgh Jan 18, 2016
ee7d59b
Deploy to S3 with Travis
vladgh Jan 18, 2016
9b0d943
Install awscli
vladgh Jan 18, 2016
950cfec
Add sudo
vladgh Jan 18, 2016
1110437
Fix sudo RTFM
vladgh Jan 18, 2016
b92e5df
Fix environment
vladgh Jan 18, 2016
cd6a200
Add encrypted environment
vladgh Jan 18, 2016
b0aac45
Use a custom script for building
vladgh Jan 18, 2016
3a04811
Add cfn_create_stack command
vladgh Jan 18, 2016
717d860
Do not fail if can not find ip
vladgh Jan 18, 2016
dc4d1dd
Use after_success instead of deploy
vladgh Jan 18, 2016
e045afc
Use after_script instead of after_success
vladgh Jan 18, 2016
7735554
Simplify sync command
vladgh Jan 18, 2016
63c8713
Debug build
vladgh Jan 18, 2016
2e02f67
Debug build
vladgh Jan 18, 2016
6996831
Debug build
vladgh Jan 18, 2016
e6671ba
Revert to TRAVIS_BRANCH to find the current branch
vladgh Jan 18, 2016
18572e9
Fix branch
vladgh Jan 18, 2016
d23694f
Cache pip dependencies
vladgh Jan 18, 2016
d64146c
Test cache
vladgh Jan 18, 2016
cbb175e
Improve aws scripts
vladgh Jan 18, 2016
4007e76
Improve AWS functions
vladgh Jan 18, 2016
626208d
Fix aws command
vladgh Jan 19, 2016
48cc36c
Improve wait function for CloudFormation
vladgh Jan 19, 2016
9ccf36e
Fix CloudFormation parameters and tags declaration
vladgh Jan 19, 2016
2f5ff7a
Fix create stack command
vladgh Jan 19, 2016
367d863
Fix Security Group CFN template
vladgh Jan 19, 2016
4eeb6c2
Remove CI user in the IAM CFN template
vladgh Jan 19, 2016
9e485ff
Add a delete stack command
vladgh Jan 19, 2016
93f2459
Remove stack notifications
vladgh Jan 20, 2016
a16d5e5
Upgrade aws cli if needed
vladgh Jan 20, 2016
5c33019
Add a load balancer
vladgh Jan 20, 2016
aeea06c
Ignore external ip error
vladgh Jan 20, 2016
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
Binary file added .env.enc
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Private
.env

# Everything in data except global.yaml
/hieradata/*
!/hieradata/common.yaml
Expand Down
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
sudo: false
language: ruby
install: cd dist/profile && bundle install --without development system_tests --path vendor
before_install:
- openssl aes-256-cbc -K $encrypted_4543e8d896b8_key -iv $encrypted_4543e8d896b8_iv -in .env.enc -out .env -d || true
install: bin/ci.sh install
script: bin/ci.sh script
after_script: bin/ci.sh deploy
env: PUPPET_GEM_VERSION="~> 4.0" STRICT_VARIABLES=yes
script: bundle exec rake test
cache:
directories:
- dist/profile/vendor
- dist/profile/spec/fixtures/modules
- $HOME/.cache/pip
notifications:
slack:
secure: PO+9V7wfK1zJZTO3RgewarOo2Uk6bhPXTAUFwDhwEoBnbhOEIq1ItLkOZfXW4LgQ9WpK03x1y9LNA4xHK89Z3yxO/s+/Csf5YOcv9+CUuwQR4vjfiFmedB8DCObGKc5HdsuohJx6G/YyLag8gFTJMGIDyQRN4uodXjrK2NGuMG8QRYhq4qwYtmnw3Q4q3o9Nsso1486qTnbWuRtF78ofgVnBurNYefbOiycGRvX4on2WI8syGcBVLQWIO/9Lc3Nye3arKdvtrVKI15/QM5DpgyPCmSTeqeu122XgVP/vPqYtmBXttU3lTladAq3S5VpMo9DQlltgTLvAl4a8ma+mzqtux1K628tMsAcNLUpcsXYbbUTRQILCmIQlDomC8oQOtSWnYPCtMpTr/5pGElVlNoWUSahlZS/VV9m/1Om64lgFdxY+MI9sI8UlXi6Ny47JfpHp7xQJPe80sFVLtwtrs9ievcEyw1R+9rSL1ASs3JIevKu5q9wTFL8Dy8lzWHlwyth8kkSruzUc7b5wsLpOPhJoKXuEJjY1ac3abgBjuX06Vk0y0y/dJdPDKy3Uo+ajY4IdjoppuJPRklXv77/hU04W8XTlD5eBR+M4tWAsUZgU1GkgtoUV05GSfNwhRfpeqzqqqzVjeUDUmXvhUG1YmkNd3wdRaW6t57iCi7P6ikI=
21 changes: 4 additions & 17 deletions Puppetfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@ mod 'puppetlabs/mysql', '3.6.2'
mod 'puppetlabs/ntp', '4.1.2'
mod 'puppetlabs/stdlib', '4.10.0'
mod 'puppetlabs/vcsrepo', '1.3.2'
mod 'concat',
:git => 'https://github.com/puppetlabs/puppetlabs-concat.git',
:tag => '2.0.1'
mod 'puppetlabs/concat', '1.2.5'

# Puppet approved modules
mod 'garethr/docker', '5.0.0'
mod 'hunner/hiera', '1.4.1'
mod 'maestrodev/wget', '1.7.1'
# This can revert to forge syntax when v5.x is released
# Currently there is now support for the new docker repositories
mod 'docker',
:git => 'https://github.com/garethr/garethr-docker.git',
:ref => 'master'
# This can revert to forge syntax when v1.3.3 is released
# PR #86 is needed
mod 'hiera',
:git => 'https://github.com/hunner/puppet-hiera.git',
:ref => 'master'
# This can revert to forge syntax when version > 3.1.1 is released
mod 'r10k',
:git => 'https://github.com/acidprime/r10k.git',
:ref => 'master'
mod 'zack/r10k', '3.2.0'

# Others
mod 'saz/limits', '2.3.0'
Expand Down
42 changes: 26 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,45 @@ This project is still in a prototype development stage.
Vlad's Puppet Control Repo.

## Description
### Puppetfile
r10k needs this file to figure out what component modules you want from the
Forge. The result is a modules directory containing all the modules specified in
this file, for each environment/branch. The modules directory is listed in
environment.conf's modulepath.

### environment.conf
This file can override several settings whenever the Puppet master is serving
nodes assigned to that environment.
[Config Files: environment.conf](https://docs.puppetlabs.com/puppet/latest/reference/config_file_environment.html)

### bin/
Contains various executable scripts.

### data/
Contains the hiera data files. It's intended to serve as a base only, for
public data, and it should be overwritten or amended with data from private
sources.
### cfn/
Contains AWS CloudFormation templates.

### dist/
Contains organization-specific roles and profiles.
This directory is specified as a modulepath in environment.conf
[Designing Puppet – Roles and Profiles.](http://www.craigdunn.org/2012/05/239/)

### hieradata/
Contains the hiera data files. It's intended to serve as a base only, for
public data, with sane defaults. It should be overwritten or amended with data
from private sources.

### include/
Contains various functions that can be sourced in other scripts.

### manifests/
Contains Puppet's manifests:
- `bootstrap.pp`: the bootstrapping manifest
- `site.pp`: the main manifest

### Puppetfile
r10k needs this file to figure out what component modules you want from the
Forge. The result is a modules directory containing all the modules specified in
this file, for each environment/branch. The modules directory is listed in
environment.conf's modulepath.

### environment.conf
This file can override several settings whenever the Puppet master is serving
nodes assigned to that environment.
[Config Files: environment.conf](https://docs.puppetlabs.com/puppet/latest/reference/config_file_environment.html)

### environment.sh
This file contains global variables. It can be sourced by other scripts. **All
variables declared here are public**. Any sensitive information should be
placed in an `.env` file which will overwrite the information here.

## Testing
### Prerequisites

Expand Down
53 changes: 30 additions & 23 deletions bin/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
#
# * Trusted facts info: https://docs.puppetlabs.com/puppet/latest/reference/lang_facts_and_builtin_vars.html#trusted-facts

# Immediately exit on errors
set -euo pipefail

# DEFAULTS
PP_MASTER=${PP_MASTER:-puppet}
PP_ROLE=${PP_ROLE:-none}
PP_SECRET=${PP_SECRET:-none}
PP_COLLECTION=${PP_COLLECTION:-pc1}
PP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
PP_COLOR=${PP_COLOR:-true}
PATH="/opt/puppetlabs/bin:/opt/puppetlabs/puppet/bin:${PATH}"
CONFDIR="$(puppet master --configprint confdir)"

# Immediately exit on errors
set -euo pipefail

# Check if root
is_root(){
Expand Down Expand Up @@ -98,19 +98,23 @@ configure_puppet(){
echo 'Install/update puppet modules'
r10k puppetfile install \
--puppetfile "${PP_DIR}/Puppetfile" \
--moduledir "${PP_DIR}/modules" \
--moduledir '/tmp/modules' \
--verbose
elif [[ "$PP_MASTER" != 'puppet' ]]; then
echo "Set puppet master address - '$PP_MASTER'"
puppet config set \
server "$PP_MASTER" --section master
fi

# VARs
PP_CONFDIR="$(puppet master --configprint confdir)"
}

# Generate certificate request attributes file
generate_csr_attributes_file(){
[[ "$PP_MASTER" == 'none' ]] && return
echo 'Generating a CSR Attributes file'
local file="${CONFDIR}/csr_attributes.yaml"
local file="${PP_CONFDIR}/csr_attributes.yaml"
local file_path; file_path=$(dirname "$file")

# Ensure directory is present
Expand Down Expand Up @@ -150,24 +154,27 @@ EPP
}" > "$file"
}

# Apply puppet
apply_puppet(){
[[ "$PP_MASTER" == 'none' ]] || return 1
echo 'Applying puppet'
FACTER_ROLE="${PP_ROLE}" puppet apply \
--color="$PP_COLOR" \
--modulepath "${PP_DIR}/dist:/tmp/modules" \
"${PP_DIR}/manifests/site.pp"
}

# Run puppet
run_puppet(){
if [[ "$PP_MASTER" == 'none' ]]; then
echo 'Applying puppet'
FACTER_ROLE="${PP_ROLE}" puppet apply \
--modulepath "${PP_DIR}/dist:${PP_DIR}/modules" \
"${PP_DIR}/manifests/site.pp"
elif [[ "$PP_MASTER" != 'puppet' ]]; then
echo 'Running puppet'
puppet agent \
--server "$PP_MASTER" \
--waitforcert 5 \
--no-daemonize \
--onetime \
--verbose
else
echo 'WARNING: No puppet master specified'
fi
[[ -n "$PP_MASTER" ]] || return 1
echo 'Running puppet'
puppet agent \
--server "$PP_MASTER" \
--waitforcert 5 \
--no-daemonize \
--onetime \
--color="$PP_COLOR" \
--verbose
}

# Logic
Expand All @@ -176,7 +183,7 @@ main(){
install_release_pkg
configure_puppet
generate_csr_attributes_file
run_puppet
apply_puppet || run_puppet || echo 'WARNING: No puppet master specified'
}

# Run
Expand Down
19 changes: 19 additions & 0 deletions bin/cfn_create_stack
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Creates CloudFormation stack

# Load environment
# shellcheck disable=1090
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)/../include/aws.sh"

aws cloudformation create-stack \
--stack-name "${vgh_stack_name:?}" \
--template-body "${vgh_stack_file:?}" \
--parameters "${vgh_stack_parameters:?}" \
--capabilities "${vgh_stack_capabilities:?}" \
--tags "${vgh_stack_tags:?}" \
--on-failure 'DELETE'

if ! aws_cfn_wait_for_stack "${vgh_stack_name:?}"; then
echo "FATAL: The stack ${vgh_stack_name:?} failed to create properly" >&2
exit 1
fi
13 changes: 13 additions & 0 deletions bin/cfn_delete_stack
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# Creates CloudFormation stack

# Load environment
# shellcheck disable=1090
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)/../include/aws.sh"

aws cloudformation delete-stack --stack-name "${vgh_stack_name:?}"

if ! aws_cfn_wait_for_stack "${vgh_stack_name:?}"; then
echo "FATAL: The stack ${vgh_stack_name:?} failed to delete properly" >&2
exit 1
fi
24 changes: 24 additions & 0 deletions bin/ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# Creates CloudFormation stack

# Load environment
# shellcheck disable=1090
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)/../include/aws.sh"

case "$1" in
install)
pip install --user --upgrade awscli
cd dist/profile || exit
bundle install --without development system_tests --path vendor
;;
script)
cd dist/profile || exit
bundle exec rake test
;;
deploy)
aws s3 sync "${REPODIR}/cfn/" "${vgh_cfn_stack_s3:?}/" \
--delete --acl public-read \
--exclude "*" --include "*.json"
;;
esac

30 changes: 29 additions & 1 deletion bin/pre-commit-hook
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
# pre-commit git hook
#
# Prerequisites:
Expand All @@ -17,6 +17,10 @@
[ -s "${HOME}/.rvm/scripts/rvm" ] && source "${HOME}/.rvm/scripts/rvm"
[ -d "${HOME}/.rvm" ] && export PATH="$PATH:$HOME/.rvm/bin"

# Add /usr/local/bin to PATH
PATH=/usr/local/bin:${PATH}

# Check YAML
function checkyaml() {
ruby -e "require 'yaml'; YAML.load_file('$1')"
}
Expand Down Expand Up @@ -56,6 +60,14 @@ if ! [ -x "$path_to_ruby" ]; then
exit 1
fi

path_to_aws=$(command -v aws)
if ! [ -x "$path_to_aws" ]; then
echo "The AWS CLI binary wasn't found."
echo "Sorry, I won't allow you to commit without aws cli installed."
echo "Please install aws cli and try again."
exit 1
fi

echo "### Checking puppet syntax, for science! ###"
# for file in `git diff --name-only --cached | grep -E '\.(pp|erb)'`
for file in $(git diff --name-only --cached | grep -E '\.(pp)'); do
Expand Down Expand Up @@ -134,6 +146,22 @@ for file in $(git diff --name-only --cached | grep -E '\.(yaml)'); do
done
echo ""

echo "### Checking if CloudFormation syntax is valid ###"
for file in $(git diff --name-only --cached | grep -E 'cfn\/.*\.(json)'); do
if [[ -f $file ]]; then
aws --region 'us-west-2' --output text \
cloudformation validate-template \
--template-body "file://${file}"
if [[ $? -ne 0 ]]; then
echo "ERROR: Cloudformation syntax validation failed at: $file"
syntax_is_bad=1
else
echo "OK: $file looks valid"
fi
fi
done
echo ""

if [[ $syntax_is_bad -eq 1 ]]; then
echo
echo "################################################################"
Expand Down
Loading