Skip to content
Merged
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
8 changes: 2 additions & 6 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ def wrap = { fn->
variable: 'GOOGLE_APPLICATION_CREDENTIALS_OVERRIDE')]) {
withCredentials([string(credentialsId: 'newrelic.license.key',
variable: 'NEWRELIC_LICENSE_KEY_OVERRIDE')]) {
sh ("""
cp env.sh.sample env.sh
rm -rf build
mkdir build
""")
sh ("bin/clean-workspace.sh")
fn()
}
}
Expand Down Expand Up @@ -182,7 +178,7 @@ stage('Build CodeDeploy Archive') {
node {
unstash 'src'
wrap.call({
sh ("./codedeploy/bin/build.sh")
sh ("./bin/build-codedeploy.sh")
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion ansible/bakery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
nrinfragent_os_version: 7
nrinfragent_config:
license_key: ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
log_file: /var/log/newrelic/nr-infra.log
log_file: /var/log/newrelic-infra/nr-infra.log
log_to_stdout: false

- name: Install Web Application
Expand Down
20 changes: 20 additions & 0 deletions ansible/newrelic-infrastructure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
# Use ansible to install the newrelic infra agent.

# Thanks https://www.tricksofthetrades.net/2017/10/02/ansible-local-playbooks/ for
# the trick on installing locally using "hosts: 127.0.0.1" and "connection:local"

- name: Set the newrelic license key
hosts: 127.0.0.1
connection: local
become: yes
tasks:
- command: bash /app/bin/set-newrelic-license-key.sh

- name: Restart newrelic
hosts: 127.0.0.1
connection: local
become: yes
tasks:
- command: service newrelic-infra restart

26 changes: 13 additions & 13 deletions codedeploy/bin/build.sh → bin/build-codedeploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASE_DIR="$DIR/.."
BUILD_DIR="$BASE_DIR/build"
ANSIBLE_DIR="$BASE_DIR/../ansible"
APPLICTION_DIR="$BASE_DIR/../application"
SRC_DIR="$BASE_DIR/../src"
VENV_DIR="$BASE_DIR/../venv"
DOCKER_DIR="$BASE_DIR/.."
ANSIBLE_DIR="$BASE_DIR/ansible"
APPLICTION_DIR="$BASE_DIR/application"
SRC_DIR="$BASE_DIR/src"

GIT_REV="$(git rev-parse --short HEAD)"
BUILD_NUMBER=${BUILD_NUMBER:-0}
Expand All @@ -39,20 +37,22 @@ fi
mkdir -p "$BUILD_DIR/socket"

echo Build docker container $CONTAINERNAME
docker build -f=Dockerfile -t "$CONTAINERNAME" "$DOCKER_DIR"
docker build -f=Dockerfile -t "$CONTAINERNAME" "$BASE_DIR"

echo Create python virtual environment
docker run --rm -v "$DOCKER_DIR:/src" "$CONTAINERNAME" /bin/bash -c \
"mkdir -p /src/venv ; \
cp -fa /app/venv/* /src/venv"
docker run \
--rm \
-v "$BASE_DIR:/src" \
"$CONTAINERNAME" \
/bin/bash -c \
"mkdir -p /src/build/venv ; \
cp -fa /app/venv/* /src/build/venv"

SOURCES="$BASE_DIR/bin
$ANSIBLE_DIR
$APPLICTION_DIR
$SRC_DIR
$BASE_DIR/appspec.yml
$BASE_DIR/bin
$VENV_DIR"
$BASE_DIR/codedeploy/appspec.yml"
for src in $SOURCES; do
cp -a "$src" "$BUILD_DIR"
done
Expand All @@ -70,7 +70,7 @@ done
)

echo Remove docker generated files
docker run --rm -v "$DOCKER_DIR:/src" "$CONTAINERNAME" /bin/bash -c \
docker run --rm -v "$BASE_DIR:/src" "$CONTAINERNAME" /bin/bash -c \
"rm -rf /src/venv"

cd "$BUILD_DIR"
Expand Down
25 changes: 25 additions & 0 deletions bin/clean-workspace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
# clean workspace
#
# Set bash unofficial strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail

# Set DEBUG to true for enhanced debugging: run prefixed with "DEBUG=true"
${DEBUG:-false} && set -vx
# Credit to https://stackoverflow.com/a/17805088
# and http://wiki.bash-hackers.org/scripting/debuggingtips
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

# Credit to http://stackoverflow.com/a/246128/424301
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASE_DIR="$DIR/.."
BUILD_DIR="$BASE_DIR/build"
export BASE_DIR

# shellcheck disable=SC1090
. "$DIR/common.sh"

cp "$BASE_DIR/env.sh.sample" "$BASE_DIR/env.sh"
clean_root_owned_docker_files
rm -rf "$BUILD_DIR"
mkdir "$BUILD_DIR"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

# Credit to http://stackoverflow.com/a/246128/424301
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASE_DIR="$DIR/.."
BASE_DIR="$DIR/../.."
ANSIBLE_DIR="$BASE_DIR/ansible"

# Invoke Ansible for final set up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

# Credit to http://stackoverflow.com/a/246128/424301
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASE_DIR="$DIR/.."
BASE_DIR="$DIR/../.."
ANSIBLE_DIR="$BASE_DIR/ansible"

# Invoke Ansible for final set up
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions bin/set-newrelic-license-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Set the newrelic license key fron AWS credentials
#
# Set bash unofficial strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail

# Set DEBUG to true for enhanced debugging: run prefixed with "DEBUG=true"
${DEBUG:-false} && set -vx
# Credit to https://stackoverflow.com/a/17805088
# and http://wiki.bash-hackers.org/scripting/debuggingtips
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

# Credit to http://stackoverflow.com/a/246128/424301
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASE_DIR="$DIR/.."
export BASE_DIR

NEWRELIC_CONFIG_FILE="/etc/newrelic-infra.yml"
# Thanks Stack Overflow https://stackoverflow.com/a/9735663/424301
EC2_AVAIL_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
EC2_REGION="$(sed 's/[a-z]$//' <<<"$EC2_AVAIL_ZONE")"

NEWRELIC_LICENSE_KEY=$(aws secretsmanager get-secret-value \
--region="$EC2_REGION" \
--secret-id newrelic_license \
--output text \
--query '[SecretString]')

cp -a "${NEWRELIC_CONFIG_FILE}" "${NEWRELIC_CONFIG_FILE}.orig"
sed "s/ZZZZ*ZZZZ/${NEWRELIC_LICENSE_KEY}/" "${NEWRELIC_CONFIG_FILE}.orig" > "${NEWRELIC_CONFIG_FILE}"
14 changes: 9 additions & 5 deletions codedeploy/appspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ files:
destination: /app/application
- source: src
destination: /app/src
- source: bin
destination: /app/bin
- source: ansible
destination: /app/ansible
hooks:
ApplicationStop:
- location: bin/ApplicationStop.sh
- location: bin/codedeploy/ApplicationStop.sh
timeout: 600
BeforeInstall:
- location: bin/BeforeInstall.sh
- location: bin/codedeploy/BeforeInstall.sh
timeout: 30
AfterInstall:
- location: bin/AfterInstall.sh
- location: bin/codedeploy/AfterInstall.sh
timeout: 300
ApplicationStart:
- location: bin/ApplicationStart.sh
- location: bin/codedeploy/ApplicationStart.sh
timeout: 120
ValidateService:
- location: bin/ValidateService.sh
- location: bin/codedeploy/ValidateService.sh
timeout: 60
2 changes: 1 addition & 1 deletion terraform/cloud-config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#cloud-config
runcmd:
- sudo -u centos ansible-playbook -l localhost /app/ansible/codedeploy.yml
- ansible-playbook -l localhost /app/ansible/codedeploy.yml /app/ansible/newrelic-infrastructure.yml