Skip to content

Commit

Permalink
Merge pull request #43 from hellofresh/feature/ami-compliant
Browse files Browse the repository at this point in the history
Add support for provisioning AMI images
  • Loading branch information
antonu17 authored Apr 19, 2019
2 parents e7b4061 + 79cbee2 commit 33d7a3a
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 149 deletions.
31 changes: 6 additions & 25 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,7 @@ provisioner:

platforms :

- name : "ubuntu1404-v2.0"
driver_config :
image : ubuntu:14.04
platform : ubuntu
provision_command:
- test -e /usr/bin/socat || (apt-get -y update && apt-get install -y iproute2 net-tools netcat socat)
provisioner :
ansible_playbook_bin : "$(avm path v2.0)ansible-playbook"

- name : "ubuntu1604-v2.0"
driver_config :
image : ubuntu:16.04
platform : ubuntu
provision_command:
- test -e /usr/bin/python || (apt-get -y update && apt-get install -y python-minimal)
- test -e /usr/bin/socat || (apt-get -y update && apt-get install -y iproute2 net-tools netcat socat)
provisioner :
ansible_playbook_bin : "$(avm path v2.0)ansible-playbook"

- name : "alpine3.4-v2.2"
- name : "alpine3.4-v2.3"
driver_config :
image : harningt/base-alpine-s6-overlay:3.4
platform : alpine
Expand All @@ -51,28 +32,28 @@ platforms :
- mkdir -p /opt # needs to be in ansible

provisioner :
ansible_playbook_bin : "$(avm path v2.2)ansible-playbook"
ansible_playbook_bin : "$(avm path v2.3)ansible-playbook"
extra_vars : {consul_service: s6, consul_s6_servicedir: /var/run/s6/services}

- name : "ubuntu1404-v2.2"
- name : "ubuntu1404-v2.3"
driver_config :
image : ubuntu:14.04
platform : ubuntu
provision_command:
- test -e /usr/bin/socat || (apt-get -y update && apt-get install -y iproute2 net-tools netcat socat)

provisioner :
ansible_playbook_bin : "$(avm path v2.2)ansible-playbook"
ansible_playbook_bin : "$(avm path v2.3)ansible-playbook"

- name : "ubuntu1604-v2.2"
- name : "ubuntu1604-v2.3"
driver_config :
image : ubuntu:16.04
platform : ubuntu
provision_command:
- test -e /usr/bin/python || (apt-get -y update && apt-get install -y python-minimal)
- test -e /usr/bin/socat || (apt-get -y update && apt-get install -y iproute2 net-tools netcat socat)
provisioner :
ansible_playbook_bin : "$(avm path v2.2)ansible-playbook"
ansible_playbook_bin : "$(avm path v2.3)ansible-playbook"

suites :
- name : basic-server
Expand Down
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
language: ruby
rvm:
- 2.2
- 2.5

sudo: required
services:
Expand All @@ -22,14 +22,12 @@ env:
before_install:
# Make sure everything's up to date.
- sudo apt-get update -qq
- sudo apt-get install -qq python-apt python-pycurl git python-pip ruby ruby-dev build-essential autoconf
- gem install bundler
- sudo apt-get install -qq python-apt python-pycurl git python-pip build-essential autoconf

install:
- bash test/ansible-setup.sh
- bundle install
- ~/.venv_ansible/v2.0/venv/bin/pip install netaddr
- ~/.venv_ansible/v2.2/venv/bin/pip install netaddr
- ~/.venv_ansible/v2.3/venv/bin/pip install netaddr

script:
- ansible --version
Expand Down
83 changes: 0 additions & 83 deletions Gemfile.lock

This file was deleted.

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ This is an opinionated setup of consul. That manages services discovery and clie
(Consumer) (Producer)
```

Using with AMI
--------------

When the role is used to provision AMI image, ensure the following variables are set to these specific values

```yaml
consul_start_service: false # Required: Prevent consul from starting at provision time
consul_node_name: auto # Required: read node name from cloud meta-data
consul_network_bind: auto # Required: read private IP from cloud meta-data
consul_network_autobind: false # Required: disable provision time IP address discovery
consul_node_name_prefix: "service-" # Optional: node name prefix
```
Requirements
------------
Expand Down
3 changes: 3 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
consul_agent_version : "0.9.2"
consul_template_version : "0.19.0"

## Start service right away, during provision
consul_start_service: true

## Node name
consul_node_name : "{{ inventory_hostname }}"

Expand Down
12 changes: 9 additions & 3 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
msg="Notifier restart consul service"
changed_when: true
notify:
- enable consul service (service)
- restart consul service (service)
- Reload s6 service
- restart consul service (s6)

- name: restart consul service (service)
- name: enable consul service (service)
service:
name="consul"
state="restarted"
enabled="yes"
when: "consul_service == 'service'"

- name: restart consul service (service)
service:
name="consul"
state="restarted"
when: "consul_service == 'service' and consul_start_service"

- name: Reload s6 service
shell: s6-svscanctl -a {{ consul_s6_servicedir }}
when: "consul_service == 's6'"
Expand All @@ -43,7 +49,7 @@
service:
name="consul"
state="restarted"
when: "consul_service == 'service'"
when: "consul_service == 'service' and consul_start_service"

- name: restart consul-template service
debug:
Expand Down
7 changes: 5 additions & 2 deletions templates/consul-agent-run.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
#

NAME="consul"
LOCAL_IP="$(curl http://169.254.169.254/latest/meta-data/local-ipv4)"
LOCAL_HOSTNAME="$(curl -s http://169.254.169.254/latest/meta-data/local-hostname | cut -d "." -f 1)"
CONSUL_CONFIG_DIR="{{ consul_config_dir }}"
CONSUL_DATA_DIR="{{ consul_data_dir }}"
CONSUL_LOG_FILE="{{ consul_agent_log_file }}"
CONSUL_CONFIG_FILE="{{ consul_config_agent_file }}"
CONSUL_BIN="{{ consul_bin_dir }}"
CONSUL_BIND="{{ consul_network_bind }}"
CONSUL_BIND="{{ '${LOCAL_IP}' if consul_network_bind == 'auto' else consul_network_bind }}"
CONSUL_NODE="{{ (consul_node_name_prefix if consul_node_name_prefix is defined else '') + '${LOCAL_HOSTNAME}' if consul_node_name == 'auto' else consul_node_name }}"

# Make sure to use all available proc
# https://groups.google.com/forum/#!topic/consul-tool/qewFEqgAoF8
export GOMAXPROCS="$(grep -c ^processor /proc/cpuinfo)"

echo "$(date) **** Consul agent start ****" >> "${CONSUL_LOG_FILE}"
exec "${CONSUL_BIN}"/"${NAME}" agent -config-dir="${CONSUL_CONFIG_DIR}" -config-file="${CONSUL_CONFIG_FILE}" -data-dir="${CONSUL_DATA_DIR}" -bind="${CONSUL_BIND}" >> "${CONSUL_LOG_FILE}" 2>&1
exec "${CONSUL_BIN}"/"${NAME}" agent -config-dir="${CONSUL_CONFIG_DIR}" -config-file="${CONSUL_CONFIG_FILE}" -data-dir="${CONSUL_DATA_DIR}" -bind="${CONSUL_BIND}" -node="${CONSUL_NODE}" >> "${CONSUL_LOG_FILE}" 2>&1
RC="$?"
echo "$(date) **** Consul-agent ended with ${RC} ****" >> "${CONSUL_LOG_FILE}"
exit "${RC}"
2 changes: 0 additions & 2 deletions templates/consul-agent.json.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"node_name": "{{ consul_node_name }}",
"bind_addr": "{{ consul_network_bind }}",
"datacenter": "{{ consul_datacenter }}",
"client_addr": "{{ consul_client_addr }}",
"domain": "{{ consul_domain }}",
Expand Down
25 changes: 4 additions & 21 deletions templates/consul-init.d.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ BASE_DIR="{{ consul_home_dir }}"
GREP_NAME="consul agent"

RUN_CMD="{{ consul_bin_dir }}/consul_agent_run.sh"
#
STOPCMD="kill -9"
#How many sec to wait before checking if proc is up
SLEEP_FOR=1
STOP_TIMEOUT=10

IGNORE_PLAY_PID=1

Expand Down Expand Up @@ -64,26 +63,10 @@ stop() {
PID=$(PROG_PID)
if [ -n "$PID" ]; then
echo -n "Stoping $NAME "
kill -9 $PID
kill -INT $PID
timeout $STOP_TIMEOUT tail --pid=$PID -f /dev/null
[ "$?" -ne 0 ] && echo "[ FAILED ]" && exit 1

let kwait=$SLEEP_FOR
count=0;
until [ $count -gt $kwait ]
do
echo -n ".";
sleep 1
let count=$count+1;
done
PID=$(PROG_PID)
if [ -n "$PID" ]; then
echo "[ FAILED ]"
exit 1
else
echo "[ OK ]"
fi
else
echo "$NAME not running."
echo "[ OK ]"
fi
}

Expand Down
12 changes: 4 additions & 8 deletions test/ansible-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
SETUP_VERSION="v0.0.3"
#SETUP_VERBOSITY="vv"

## Install Ansible 2.0
ANSIBLE_VERSIONS[0]="2.0.2.0"
## Install Ansible 2.3
ANSIBLE_VERSIONS[0]="2.3.2.0"
INSTALL_TYPE[0]="pip"
ANSIBLE_LABEL[0]="v2.0"

ANSIBLE_VERSIONS[1]="2.2.0.0"
INSTALL_TYPE[1]="pip"
ANSIBLE_LABEL[1]="v2.2"
ANSIBLE_LABEL[0]="v2.3"

# Whats the default version
ANSIBLE_DEFAULT_VERSION="v2.0"
ANSIBLE_DEFAULT_VERSION="v2.3"

## Create a temp dir
filename=$( echo ${0} | sed 's|/||g' )
Expand Down

0 comments on commit 33d7a3a

Please sign in to comment.