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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ Session.vim
.terraform
terraform.tfstate*
build.log

**/*.tar.7z
infra/v?/terraform
**/.env_vars
**/osc_config.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ else
}
EOF
fi

38 changes: 38 additions & 0 deletions images/consul/v1/files/_scripts/format_EBS.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -e

DEVICE="/dev/xvdb"
logFile="/var/log/$(basename ${0}).log"

function error() {
DATE=`date '+%Y-%m-%d %H:%M:%S'`
if [[ -z "$logFile" ]]; then echo "$DATE -- $1" >> $logFile; fi
echo $1
exit 1
}

MY_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | sed 's/ "region" : "\(.*\)",\?/\1/')

# 60 seconds / 12 attempts
cnt=12
while [[ ! -b /dev/xvdb ]] && [[ "$cnt" -gt 0 ]] ; do
cnt=$(($cnt-1))
sleep 5
done

[[ -b "$DEVICE" ]] || error "$DEVICE not available"

# cnt=12
# while [[ -z "$STATUS" || "$STATUS" != "attached" ]] && [[ "$cnt" -gt 0 ]]; do
# STATUS=$(aws ec2 describe-instances --region ${REGION} --instance ${MY_INSTANCE_ID}|jq -r --arg DEVICE "$DEVICE" '.Reservations[].Instances[].BlockDeviceMappings[]|select(.DeviceName==$DEVICE)|.Ebs.Status')
# cnt=$(($cnt-1))
# sleep 5
# done

if blkid ${DEVICE}; then
echo "$DEVICE setup is fine"
else
wipefs -fa $DEVICE && mkfs.ext4 $DEVICE
# parted -s /dev/xvdb mklabel msdos && parted -s /dev/xvdb mkpart primary ext4 1MiB 100% && mkfs.ext4 /dev/xvdb1
fi
3 changes: 1 addition & 2 deletions images/consul/v1/files/systemd/consul.service
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ After=generate-consul-config.service
[Service]
Environment=GOMAXPROCS=2
Restart=on-failure
ExecStartPre=/usr/bin/mkdir -p /mnt/data/consul
ExecStartPre=if [ -d /mnt/data/consul ]; then /usr/bin/mkdir -p /mnt/data/consul; fi
ExecStart=/usr/local/bin/consul agent -data-dir=/mnt/data/consul -config-dir=/etc/consul.d -ui
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

7 changes: 3 additions & 4 deletions images/consul/v1/files/systemd/format-data-volume.service
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[Unit]
Description=Format EBS volume if needed
# ConditionFirstBoot=yes

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c \
'(/usr/sbin/blkid /dev/xvdb) || \
(/usr/sbin/wipefs -fa /dev/xvdb && /usr/sbin/mkfs.ext4 /dev/xvdb)'

ExecStart=/usr/local/bin/format_EBS.sh
TimeoutStartSec=60
2 changes: 1 addition & 1 deletion images/consul/v1/scripts/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -x
set -e

dnf install -y curl wget unzip dnsmasq jq bind-utils
apt-get install -y curl wget unzip dnsmasq jq bind9utils parted
2 changes: 1 addition & 1 deletion images/consul/v1/scripts/configure_network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -x
set -e

# Add PEERDNS=no to ifcfg-eth0 since we don't want the dhcp client to rewrite /etc/resolv.conf
echo "PEERDNS=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0
# echo "PEERDNS=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0

cat <<EOF > /etc/resolv.conf
search ec2.internal
Expand Down
8 changes: 4 additions & 4 deletions images/consul/v1/scripts/copy_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ set -e

STAGING_DIR="/tmp/packer/files/"
mkdir -p ${STAGING_DIR}
chown -R fedora:fedora ${STAGING_DIR}
chown -R admin:admin ${STAGING_DIR}

chown root:root ${STAGING_DIR}/systemd/*
chown root:root ${STAGING_DIR}/bin/*
chmod +x ${STAGING_DIR}/bin/*
chown root:root ${STAGING_DIR}/_scripts/*
chmod +x ${STAGING_DIR}/_scripts/*

cp ${STAGING_DIR}/systemd/* /etc/systemd/system/
cp ${STAGING_DIR}/bin/* /usr/local/bin
cp ${STAGING_DIR}/_scripts/* /usr/local/bin

5 changes: 3 additions & 2 deletions images/consul/v1/scripts/install_awscli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -x
set -e

dnf install -y python-pip
apt-get install -y python
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip install awscli

2 changes: 1 addition & 1 deletion images/consul/v1/scripts/install_consul.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e

echo "Fetching Consul..."
cd /tmp
wget https://releases.hashicorp.com/consul/0.6.2/consul_0.6.2_linux_amd64.zip -O consul.zip
wget https://releases.hashicorp.com/consul/1.0.6/consul_1.0.6_linux_amd64.zip -O consul.zip

echo "Installing Consul..."
unzip consul.zip >/dev/null
Expand Down
2 changes: 1 addition & 1 deletion images/consul/v1/scripts/prepare_staging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -x
set -e

mkdir -p /tmp/packer/files
chown -R fedora:fedora /tmp/packer/
chown -R admin:admin /tmp/packer/
2 changes: 1 addition & 1 deletion images/consul/v1/scripts/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -x
set -e

dnf -y update
apt-get update
7 changes: 3 additions & 4 deletions images/consul/v1/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"access_key": "{{user `access_key`}}",
"secret_key": "{{user `secret_key`}}",
"region": "{{user `region`}}",
"source_ami": "ami-518bfb3b",
"source_ami": "ami-0dc82b70",
"instance_type": "t2.micro",
"ssh_username": "fedora",
"ssh_username": "admin",
"ami_name": "consul ami {{timestamp}}",
"tags": {
"test": ""
Expand All @@ -22,9 +22,8 @@
"type": "shell",
"execute_command": "{{ .Vars }} sudo -E sh '{{ .Path }}'",
"scripts": [
"./scripts/base.sh",
"./scripts/disable_selinux.sh",
"./scripts/update.sh",
"./scripts/base.sh",
"./scripts/install_awscli.sh",
"./scripts/install_consul.sh",
"./scripts/configure_network.sh",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ else
}
EOF
fi

38 changes: 38 additions & 0 deletions images/consul/v2/files/_scripts/format_EBS.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -e

DEVICE="/dev/xvdb"
logFile="/var/log/$(basename ${0}).log"

function error() {
DATE=`date '+%Y-%m-%d %H:%M:%S'`
if [[ -z "$logFile" ]]; then echo "$DATE -- $1" >> $logFile; fi
echo $1
exit 1
}

MY_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | sed 's/ "region" : "\(.*\)",\?/\1/')

# 60 seconds / 12 attempts
cnt=12
while [[ ! -b /dev/xvdb ]] && [[ "$cnt" -gt 0 ]] ; do
cnt=$(($cnt-1))
sleep 5
done

[[ -b "$DEVICE" ]] || error "$DEVICE not available"

# cnt=12
# while [[ -z "$STATUS" || "$STATUS" != "attached" ]] && [[ "$cnt" -gt 0 ]]; do
# STATUS=$(aws ec2 describe-instances --region ${REGION} --instance ${MY_INSTANCE_ID}|jq -r --arg DEVICE "$DEVICE" '.Reservations[].Instances[].BlockDeviceMappings[]|select(.DeviceName==$DEVICE)|.Ebs.Status')
# cnt=$(($cnt-1))
# sleep 5
# done

if blkid ${DEVICE}; then
echo "$DEVICE setup is fine"
else
wipefs -fa $DEVICE && mkfs.ext4 $DEVICE
# parted -s /dev/xvdb mklabel msdos && parted -s /dev/xvdb mkpart primary ext4 1MiB 100% && mkfs.ext4 /dev/xvdb1
fi
3 changes: 1 addition & 2 deletions images/consul/v2/files/systemd/consul.service
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ After=generate-consul-config.service
[Service]
Environment=GOMAXPROCS=2
Restart=on-failure
ExecStartPre=/usr/bin/mkdir -p /mnt/data/consul
ExecStartPre=if [ -d /mnt/data/consul ]; then /usr/bin/mkdir -p /mnt/data/consul; fi
ExecStart=/usr/local/bin/consul agent -data-dir=/mnt/data/consul -config-dir=/etc/consul.d -ui
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

7 changes: 3 additions & 4 deletions images/consul/v2/files/systemd/format-data-volume.service
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[Unit]
Description=Format EBS volume if needed
# ConditionFirstBoot=yes

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c \
'(/usr/sbin/blkid /dev/xvdb) || \
(/usr/sbin/wipefs -fa /dev/xvdb && /usr/sbin/mkfs.ext4 /dev/xvdb)'

ExecStart=/usr/local/bin/format_EBS.sh
TimeoutStartSec=60
2 changes: 1 addition & 1 deletion images/consul/v2/scripts/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -x
set -e

dnf install -y curl wget unzip dnsmasq jq bind-utils
apt-get install -y curl wget unzip dnsmasq jq bind9utils parted
2 changes: 1 addition & 1 deletion images/consul/v2/scripts/configure_network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -x
set -e

# Add PEERDNS=no to ifcfg-eth0 since we don't want the dhcp client to rewrite /etc/resolv.conf
echo "PEERDNS=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0
# echo "PEERDNS=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0

cat <<EOF > /etc/resolv.conf
search ec2.internal
Expand Down
8 changes: 4 additions & 4 deletions images/consul/v2/scripts/copy_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ set -e

STAGING_DIR="/tmp/packer/files/"
mkdir -p ${STAGING_DIR}
chown -R fedora:fedora ${STAGING_DIR}
chown -R admin:admin ${STAGING_DIR}

chown root:root ${STAGING_DIR}/systemd/*
chown root:root ${STAGING_DIR}/bin/*
chmod +x ${STAGING_DIR}/bin/*
chown root:root ${STAGING_DIR}/_scripts/*
chmod +x ${STAGING_DIR}/_scripts/*

cp ${STAGING_DIR}/systemd/* /etc/systemd/system/
cp ${STAGING_DIR}/bin/* /usr/local/bin
cp ${STAGING_DIR}/_scripts/* /usr/local/bin

5 changes: 3 additions & 2 deletions images/consul/v2/scripts/install_awscli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -x
set -e

dnf install -y python-pip
apt-get install -y python
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip install awscli

2 changes: 1 addition & 1 deletion images/consul/v2/scripts/install_consul.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e

echo "Fetching Consul..."
cd /tmp
wget https://releases.hashicorp.com/consul/0.6.3/consul_0.6.3_linux_amd64.zip -O consul.zip
wget https://releases.hashicorp.com/consul/1.0.5/consul_1.0.5_linux_amd64.zip -O consul.zip

echo "Installing Consul..."
unzip consul.zip >/dev/null
Expand Down
2 changes: 1 addition & 1 deletion images/consul/v2/scripts/prepare_staging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -x
set -e

mkdir -p /tmp/packer/files
chown -R fedora:fedora /tmp/packer/
chown -R admin:admin /tmp/packer/
2 changes: 1 addition & 1 deletion images/consul/v2/scripts/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -x
set -e

dnf -y update
apt-get update
7 changes: 3 additions & 4 deletions images/consul/v2/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"access_key": "{{user `access_key`}}",
"secret_key": "{{user `secret_key`}}",
"region": "{{user `region`}}",
"source_ami": "ami-518bfb3b",
"source_ami": "ami-0dc82b70",
"instance_type": "t2.micro",
"ssh_username": "fedora",
"ssh_username": "admin",
"ami_name": "consul ami {{timestamp}}",
"tags": {
"test": ""
Expand All @@ -22,9 +22,8 @@
"type": "shell",
"execute_command": "{{ .Vars }} sudo -E sh '{{ .Path }}'",
"scripts": [
"./scripts/base.sh",
"./scripts/disable_selinux.sh",
"./scripts/update.sh",
"./scripts/base.sh",
"./scripts/install_awscli.sh",
"./scripts/install_consul.sh",
"./scripts/configure_network.sh",
Expand Down
14 changes: 9 additions & 5 deletions infra/v1/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
FROM fedora:23
FROM debian:stretch

RUN dnf -y install wget curl git unzip jq python python-pip python-boto3 awscli ansible docker
RUN apt-get update && apt-get install -y wget curl git unzip jq python python-pip python-boto3 awscli ansible docker uuid-runtime \
vim iproute2 net-tools nano

RUN curl https://releases.hashicorp.com/terraform/0.6.12/terraform_0.6.12_linux_amd64.zip -o /tmp/terraform_0.6.12_linux_amd64.zip
RUN curl https://releases.hashicorp.com/terraform/0.11.3/terraform_0.11.3_linux_amd64.zip -o /tmp/terraform_0.11.3_linux_amd64.zip

WORKDIR /usr/local/bin/
RUN unzip /tmp/terraform_0.6.12_linux_amd64.zip
RUN unzip /tmp/terraform_0.11.3_linux_amd64.zip

RUN mkdir -p /root/.ssh
RUN chmod 700 /root/.ssh
RUN echo -e "Host *\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config
RUN /bin/echo -e "Host *\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config

RUN curl -fsSL get.docker.com -o get-docker.sh && sh get-docker.sh
# RUN usermod -aG docker admin

WORKDIR /deploy
ADD main.tf config ./
Expand Down
4 changes: 2 additions & 2 deletions infra/v1/ansible/test_consul_servers_active.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

---
- hosts: all
remote_user: fedora
remote_user: admin
become_user: root
gather_facts: false
tasks:
Expand Down
4 changes: 2 additions & 2 deletions infra/v1/ansible/wait_instance_stopped.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

---
- hosts: all
remote_user: fedora
remote_user: admin
become_user: root
gather_facts: false
tasks:
Expand Down
4 changes: 2 additions & 2 deletions infra/v1/ansible/wait_instance_up.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

---
- hosts: all
remote_user: fedora
remote_user: admin
become_user: root
gather_facts: false
tasks:
Expand Down
3 changes: 2 additions & 1 deletion infra/v1/config
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CONSUL_AMI_ID= # Put here the ami id reported at the end of the build of `images/consul/v1`
# Put here the ami id reported at the end of the build of `images/consul/v1`
CONSUL_AMI_ID=ami-6bf50016
Loading