Skip to content

Instantly share code, notes, and snippets.

@acidjazz
Last active December 12, 2019 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acidjazz/0776b87b3097af0d82bd8680f7f088d2 to your computer and use it in GitHub Desktop.
Save acidjazz/0776b87b3097af0d82bd8680f7f088d2 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# cloudinit-kong.sh
#
# cloudinit script to spin up and configure a kong instance
#
SERVICE=kong
ENV=production
REPO=github-kong:company/kong.git
BUCKET=company-$ENV-vault
HOSTNAME=$SERVICE-$ENV
hostname $HOSTNAME
# grab our keys
aws s3 cp s3://$BUCKET/keys/ /home/ec2-user/.ssh/ --recursive
chmod 0700 /home/ec2-user/.ssh/*
chown -R ec2-user:ec2-user /home/ec2-user/.ssh/
yum -y update
amazon-linux-extras install epel -y
yum -y install git
# grab and install kong v1.3.0
wget \
https://bintray.com/kong/kong-rpm/download_file?file_path=amazonlinux/amazonlinux/kong-1.3.0.aws.amd64.rpm \
-O kong.rpm
yum -y install kong.rpm --nogpgcheck
# grab our config
aws s3 cp s3://$BUCKET/envs/kong.conf kong.conf
su ec2-user -c "
cd ~/
ssh-keyscan github.com >> ~/.ssh/known_hosts
git clone $REPO
cd $SERVICE
git checkout $ENV
./deploy.py $ENV
sudo /usr/local/bin/kong start -c /home/ec2-user/kong/kong.conf
"
@acidjazz
Copy link
Author

acidjazz commented Dec 12, 2019

here is deploy.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Generate a configuration file for kong
#
# Distributed under terms of the MIT license.

import os
import sys

if len(sys.argv) != 2:
    print('please specify an environment')
    exit(0)

env = sys.argv[1]

if env != 'local':
    os.system('git pull')
    os.system('aws s3 cp s3://company-' + env + '-vault/envs/kong.env .env')

secret = open('.env', 'r').read().strip().split('=')[1]
config = open('kong-' + env + '.yml').read()
open('kong.yml', 'w').write(config.replace('{{ APP_KEY }}', secret))

if env != 'local':
    os.system('sudo /usr/local/bin/kong restart -c /home/ec2-user/kong/kong.conf')

@acidjazz
Copy link
Author

here is my circleci auto-deployment

version: 2
jobs:
  build:
    docker:
      - image: circleci/python:3.8.0b4-buster-browsers
    working_directory: ~/kong

    steps:

      - checkout

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "kong-local.yml" }}-{{ checksum "kong-staging.yml" }}-{{ checksum "kong-production.yml" }}
          - v1-dependencies-

      - run:
          name: Installing Kong
          command: |
            sudo apt-get update
            sudo apt-get install openssl libpcre3 procps perl
            curl -L "https://bintray.com/kong/kong-deb/download_file?file_path=kong-1.3.0.bionic.amd64.deb" -o kong.deb
            sudo dpkg -i kong.deb

      - run:
          name: Install pip and awscli
          command: |
            curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
            python get-pip.py --user
            ~/.local/bin/pip install awscli --upgrade --user

      - save_cache:
          key: v1-dependencies-{{ checksum "kong-local.yml" }}-{{ checksum "kong-staging.yml" }}-{{ checksum "kong-production.yml" }}
          paths:
            - ~/.local
      - run:
          name: Test our local config file
          command: kong config -c kong.conf parse kong-local.yml 
      - run:
          name: Test our staging config file
          command: kong config -c kong.conf parse kong-staging.yml 
      - run:
          name: Test our production config file
          command: kong config -c kong.conf parse kong-production.yml 

      - run:
          name: Populate our credentials file
          command: |
            mkdir ~/.aws 
            echo -e "[staging]\naws_access_key_id=$AWS_ACCESS_KEY_ID\naws_secret_access_key=$AWS_SECRET_ACCESS_KEY\n" > ~/.aws/credentials
            echo -e "[production]\naws_access_key_id=$AWS_PRODUCTION_ACCESS_KEY_ID\naws_secret_access_key=$AWS_PRODUCTION_SECRET_ACCESS_KEY\n" >> ~/.aws/credentials
      - deploy:
          name: Deploy code via SSM
          command: |
            if [ "${CIRCLE_BRANCH}" = 'production' ] ||  [ "${CIRCLE_BRANCH}" = 'staging' ]; then
              commandId=$(~/.local/bin/aws --profile=${CIRCLE_BRANCH} ssm send-command \
                --region us-east-2 \
                --targets "Key=tag:ssm,Values=kong-${CIRCLE_BRANCH}" \
                --document-name "AWS-RunShellScript" \
                --comment "Kong to ${CIRCLE_BRANCH}" \
                --parameters '{"commands": ["su - ec2-user -c \"cd ~/kong; ./deploy.py '"${CIRCLE_BRANCH}"' \""]}' \
                --output text \
                --query "Command.CommandId")
              status="InProgress"
              echo ["$commandId"] Status: "$status"
              while  [ "$status" = InProgress ]
              do
                status=$(~/.local/bin/aws --profile=${CIRCLE_BRANCH} ssm list-commands \
                  --region=us-east-2 \
                  --command-id "$commandId" \
                  --query "Commands[*].Status" \
                  | tr -cd '[:alpha:]')
                printf .
              done
              echo
              echo ["$commandId"] Status: "$status"
              ~/.local/bin/aws --profile=${CIRCLE_BRANCH} ssm list-command-invocations \
                --region=us-east-2 \
                --command-id "$commandId" \
                --query="CommandInvocations[*].CommandPlugins[*].Output" \
                --details \
                | sed 1,2d | sed "s/^[ \t]*\"//" | sed 's/\\n/\'$'\n/g' | head -n -3
            fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment