Skip to content

Commit

Permalink
Docker to Azure v1 DEVOPS-796 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcascone authored Jun 14, 2023
1 parent 7f6afe6 commit b686014
Show file tree
Hide file tree
Showing 38 changed files with 876 additions and 256 deletions.
13 changes: 9 additions & 4 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ inputs:

# For remote connectivity:
# TODO: Port prerequisite not met. Add an inbound network security group rule with destination port 22.
# TODO: IP address prerequisite not met. Add a public IP address to the VM's network interface.

# Terraform
tf_state_bucket:
Expand Down Expand Up @@ -119,14 +118,18 @@ inputs:
default: data
data_mount_target:
description: "Directory path within docker env to mount directory to, default is `/data`"
efs_mount_target:
description: "Directory path in efs to mount directory to, default is `/`"
# efs_mount_target:
# description: "Directory path in efs to mount directory to, default is `/`"

# BitOps
DEBUG_MODE:
description: 'Set to "true" to enable debug mode'
required: false
default: 'false'
BITOPS_IMAGE:
description: 'Set to override the BitOps image to use for the deployment. Defaults to `bitovi/bitops:latest`.'
required: false
default: 'bitovi/bitops:latest'

outputs:
vm_url:
Expand Down Expand Up @@ -175,6 +178,8 @@ runs:
DOCKER_FULL_CLEANUP: ${{ inputs.docker_full_cleanup }}
APP_DIRECTORY: ${{ inputs.app_directory }}
APP_DIRECTORY_CLEANUP: ${{ inputs.app_directory_cleanup }}
APPLICATION_MOUNT_TARGET: ${{ inputs.application_mount_target }}
DATA_MOUNT_TARGET: ${{ inputs.data_mount_target }}
ADDITIONAL_TAGS: ${{ inputs.additional_tags }}
DEBUG_MODE: ${{ inputs.DEBUG_MODE }}

Expand Down Expand Up @@ -216,4 +221,4 @@ runs:
run: |
echo "## Workflow failed to run :fire:" >> $GITHUB_STEP_SUMMARY
echo "Please check the logs for possible errors." >> $GITHUB_STEP_SUMMARY
echo "If you consider this is a bug in the Github Action, please submit an issue to our repo." >> $GITHUB_STEP_SUMMARY
echo "If you consider this is a bug in the Github Action, please submit an issue to our repo." >> $GITHUB_STEP_SUMMARY
14 changes: 8 additions & 6 deletions operations/_scripts/deploy/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export LB_LOGS_BUCKET

$SCRIPTS_PATH/deploy/check_bucket_name.sh $LB_LOGS_BUCKET

source "$SCRIPTS_PATH/generate/generate_helpers.sh"
# Generate subdomain
$SCRIPTS_PATH/generate/generate_subdomain.sh

Expand All @@ -34,16 +35,16 @@ $SCRIPTS_PATH/generate/generate_provider.sh
$SCRIPTS_PATH/generate/generate_tf_vars.sh

# Generate dot_env
# $SCRIPTS_PATH/generate/generate_dot_env.sh
$SCRIPTS_PATH/generate/generate_dot_env.sh

# Generate app repo
# $SCRIPTS_PATH/generate/generate_app_repo.sh
$SCRIPTS_PATH/generate/generate_app_repo.sh

# Generate bitops config
$SCRIPTS_PATH/generate/generate_bitops_config.sh

# Generate Ansible playbook
# $SCRIPTS_PATH/generate/generate_ansible_playbook.sh
$SCRIPTS_PATH/generate/generate_ansible_playbook.sh

# List terraform folder
# cmd="ls -al $OPS_ENV_PATH/terraform/"
Expand All @@ -56,8 +57,8 @@ echo $cmd && $cmd
cmd="cat $OPS_ENV_PATH/terraform/bitops.config.yaml"
echo $cmd && $cmd

# echo "ls OPS_ENV_PATH/ansible/app/${GITHUB_REPO_NAME}"
# ls "$OPS_ENV_PATH/ansible/app/${GITHUB_REPO_NAME}"
cmd="ls $OPS_ENV_PATH/ansible/app/${GITHUB_REPO_NAME}"
echo $cmd && $cmd

TERRAFORM_COMMAND=""
TERRAFORM_DESTROY=""
Expand Down Expand Up @@ -104,4 +105,5 @@ else
fi

exit $BITOPS_RESULT
# echo result: $BITOPS_RESULT

# TODO: support incoming image tag from workflow
35 changes: 21 additions & 14 deletions operations/_scripts/generate/generate_ansible_playbook.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/bin/bash
# shellcheck disable=SC1091

set -e
echo "In $(basename $0)"
[[ -n $DEBUG_MODE && $DEBUG_MODE == 'true' ]] && set -x

echo "In generate_ansible_playbook.sh"
source "$GITHUB_ACTION_PATH/operations/_scripts/generate/generate_helpers.sh"

PLAYBOOK_PATH=$GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml

echo -en "- name: Ensure hosts is up and running
hosts: bitops_servers
Expand All @@ -16,22 +21,22 @@ echo -en "- name: Ensure hosts is up and running
hosts: bitops_servers
become: true
tasks:
" > $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml
" > $PLAYBOOK_PATH

# Adding docker cleanup task to playbook
if [[ $DOCKER_FULL_CLEANUP = true ]]; then
echo -en "
- name: Docker Cleanup
include_tasks: tasks/docker_cleanup.yml
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml
" >> $PLAYBOOK_PATH
fi

# Adding app_pore cleanup task to playbook
# Adding app_repo cleanup task to playbook
if [[ $APP_DIRECTORY_CLEANUP = true ]]; then
echo -en "
- name: EC2 Cleanup
include_tasks: tasks/ec2_cleanup.yml
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml
" >> $PLAYBOOK_PATH
fi

# Continue adding the defaults
Expand All @@ -43,15 +48,17 @@ echo -en "
# Notes on why unmounting is required can be found in umount.yaml
- name: Unmount efs
include_tasks: tasks/umount.yml
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml
if [[ $(alpha_only "$AWS_EFS_CREATE") == true ]] || [[ $(alpha_only "$AWS_EFS_CREATE_HA") == true ]] || [[ $AWS_EFS_MOUNT_ID != "" ]]; then
echo -en "
- name: Mount efs
include_tasks: tasks/mount.yml
when: mount_efs
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml
fi
" >> $PLAYBOOK_PATH

# if [[ $(alpha_only "$AWS_EFS_CREATE") == true ]] || [[ $(alpha_only "$AWS_EFS_CREATE_HA") == true ]] || [[ $AWS_EFS_MOUNT_ID != "" ]]; then
# echo -en "
# - name: Mount efs
# include_tasks: tasks/mount.yml
# when: mount_efs
# " >> $PLAYBOOK_PATH
# fi

echo -en "
- name: Include start
include_tasks: tasks/start.yml
" >> $GITHUB_ACTION_PATH/operations/deployment/ansible/playbook.yml
" >> $PLAYBOOK_PATH
30 changes: 17 additions & 13 deletions operations/_scripts/generate/generate_app_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@

set -e

echo "In $(basename $0)"
[[ -n $DEBUG_MODE && $DEBUG_MODE == 'true' ]] && set -x

echo "In generate_app_repo.sh"
GITHUB_REPO_NAME=$(echo $GITHUB_REPOSITORY | sed 's/^.*\///')
GITHUB_REPO_NAME=$(echo "$GITHUB_REPOSITORY" | sed 's/^.*\///')

echo "Copying files from GITHUB_WORKSPACE ($GITHUB_WORKSPACE) to ops repo's Ansible deployment (${GITHUB_ACTION_PATH}/operations/deployment/ansible/app/${GITHUB_REPO_NAME})"
mkdir -p "${GITHUB_ACTION_PATH}/operations/deployment/ansible/app/${GITHUB_REPO_NAME}"
ANSIBLE_DEPLOYMENT_PATH="$GITHUB_ACTION_PATH/operations/deployment/ansible/app/$GITHUB_REPO_NAME"

echo "Copying files from GITHUB_WORKSPACE '$GITHUB_WORKSPACE' to ops repo's Ansible deployment '$ANSIBLE_DEPLOYMENT_PATH'"
mkdir -p "$ANSIBLE_DEPLOYMENT_PATH"

TARGET_PATH="$GITHUB_WORKSPACE"
if [ -n "$APP_DIRECTORY" ]; then
echo "APP_DIRECTORY: $APP_DIRECTORY"
TARGET_PATH="${TARGET_PATH}/${APP_DIRECTORY}"
TARGET_PATH="$TARGET_PATH/$APP_DIRECTORY"
fi

if [ -f "$TARGET_PATH/.gha-ignore" ]; then
rsync -a --exclude-from="$TARGET_PATH/.gha-gnore" "$TARGET_PATH"/ "${GITHUB_ACTION_PATH}/operations/deployment/ansible/app/${GITHUB_REPO_NAME}/"
rsync -a --exclude-from="$TARGET_PATH/.gha-gnore" "$TARGET_PATH/" "$ANSIBLE_DEPLOYMENT_PATH/"
else
rsync -a "$TARGET_PATH"/ "${GITHUB_ACTION_PATH}/operations/deployment/ansible/app/${GITHUB_REPO_NAME}/"
rsync -a "$TARGET_PATH/" "$ANSIBLE_DEPLOYMENT_PATH/"
fi

if [ -s "$TARGET_PATH/$REPO_ENV" ]; then
echo "Copying checked in env file from repo to Ansible deployment path"
cp "$TARGET_PATH/$REPO_ENV" "${GITHUB_ACTION_PATH}/operations/deployment/ansible/repo.env"
else
echo "Checked in env file from repo is empty or couldn't be found"
fi
# # check if the dir exists and has a size greater than zero.
# if [ -s "$TARGET_PATH/$REPO_ENV" ]; then
# echo "Copying checked in env file from repo to Ansible deployment path"
# cp "$TARGET_PATH/$REPO_ENV" "$GITHUB_ACTION_PATH/operations/deployment/ansible/repo.env"
# else
# echo "Checked in env file from repo is empty or couldn't be found"
# fi
3 changes: 2 additions & 1 deletion operations/_scripts/generate/generate_bitops_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

set -e

echo "In generate_bitops_config.sh"
echo "In $(basename $0)"
[[ -n $DEBUG_MODE && $DEBUG_MODE == 'true' ]] && set -x

CONFIG_STACK_ACTION="apply"
if [ "$STACK_DESTROY" == "true" ]; then
Expand Down
9 changes: 5 additions & 4 deletions operations/_scripts/generate/generate_dot_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ set -e

echo "In $(basename $0)"

ghv_path="${GITHUB_ACTION_PATH}/operations/deployment/ansible/ghv.env"
ghs_path="${GITHUB_ACTION_PATH}/operations/deployment/ansible/ghs.env"
[[ -n $DEBUG_MODE && $DEBUG_MODE == 'true' ]] && set -x

touch $ghv_path && cat "$GHV_ENV" > $ghv_path
touch $ghs_path && cat "$GHS_ENV" > $ghs_path
ENV_FILE_PATH="${GITHUB_ACTION_PATH}/operations/deployment/ansible"

echo "$GHV_ENV" > "$ENV_FILE_PATH/ghv.env"
echo "$GHS_ENV" > "$ENV_FILE_PATH/ghs.env"
42 changes: 42 additions & 0 deletions operations/_scripts/generate/generate_helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# TODO: wrap the generate scripts in functions in here

# # convert 'a,b,c'
# # to '["a","b","c"]'
# comma_str_to_tf_array () {
# local IFS=','
# local str=$1

# local out=""
# local first_item_flag="1"
# for item in $str; do
# if [ -z $first_item_flag ]; then
# out="${out},"
# fi
# first_item_flag=""

# item="$(echo $item | xargs)"
# out="${out}\"${item}\""
# done
# echo "[${out}]"
# }

# -------------------------------------------------- #
# Generator #
# Function to generate the variable content based on the fact that it could be empty.
# This way, we only pass terraform variables that are defined, hence not overwriting terraform defaults.

function alpha_only() {
echo "$1" | tr -cd '[:alpha:]' | tr '[:upper:]' '[:lower:]'
}

function generate_var () {
if [[ -n "$2" ]];then
if [[ $(alpha_only "$2") == "true" ]] || [[ $(alpha_only "$2") == "false" ]]; then
echo "$1 = $(alpha_only $2)"
else
echo "$1 = \"$2\""
fi
fi
}
2 changes: 1 addition & 1 deletion operations/_scripts/generate/generate_provider.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ echo "In $(basename $0)"
cat << PROVIDER_HCL > "$GITHUB_ACTION_PATH/operations/deployment/terraform/provider.tf"
terraform {
required_version = ">=0.12"
required_version = ">=0.13"
required_providers {
azurerm = {
Expand Down
55 changes: 9 additions & 46 deletions operations/_scripts/generate/generate_tf_vars.sh
Original file line number Diff line number Diff line change
@@ -1,65 +1,29 @@
#!/bin/bash
# shellcheck disable=SC2086,SC1091

[[ -n $DEBUG_MODE && $DEBUG_MODE == 'true' ]] && set -x

set -e

echo "In $(basename $0)"
source "$GITHUB_ACTION_PATH/operations/_scripts/generate/generate_helpers.sh"

# convert 'a,b,c'
# to '["a","b","c"]'
comma_str_to_tf_array () {
local IFS=','
local str=$1

local out=""
local first_item_flag="1"
for item in $str; do
if [ -z $first_item_flag ]; then
out="${out},"
fi
first_item_flag=""

item="$(echo $item | xargs)"
out="${out}\"${item}\""
done
echo "[${out}]"
}
echo "In $(basename $0)"

GITHUB_ORG_NAME=$(echo $GITHUB_REPOSITORY | sed 's/\/.*//')
GITHUB_REPO_NAME=$(echo $GITHUB_REPOSITORY | sed 's/^.*\///')

if [ -n "$GITHUB_HEAD_REF" ]; then
GITHUB_BRANCH_NAME=${GITHUB_HEAD_REF}
GITHUB_BRANCH_NAME=$GITHUB_HEAD_REF
else
GITHUB_BRANCH_NAME=${GITHUB_REF_NAME}
GITHUB_BRANCH_NAME=$GITHUB_REF_NAME
fi


GITHUB_IDENTIFIER="$($GITHUB_ACTION_PATH/operations/_scripts/generate/generate_identifier.sh)"
echo "GITHUB_IDENTIFIER: [$GITHUB_IDENTIFIER]"

GITHUB_IDENTIFIER_SS="$($GITHUB_ACTION_PATH/operations/_scripts/generate/generate_identifier_supershort.sh)"
echo "GITHUB_IDENTIFIER SS: [$GITHUB_IDENTIFIER_SS]"


# -------------------------------------------------- #
# Generator #
# Function to generate the variable content based on the fact that it could be empty.
# This way, we only pass terraform variables that are defined, hence not overwriting terraform defaults.

function alpha_only() {
echo "$1" | tr -cd '[:alpha:]' | tr '[:upper:]' '[:lower:]'
}

function generate_var () {
if [[ -n "$2" ]];then
if [[ $(alpha_only "$2") == "true" ]] || [[ $(alpha_only "$2") == "false" ]]; then
echo "$1 = $(alpha_only $2)"
else
echo "$1 = \"$2\""
fi
fi
}

# Fixed values

ops_repo_environment="ops_repo_environment = \"deployment\""
Expand Down Expand Up @@ -114,9 +78,9 @@ root_domain=$(generate_var root_domain $ROOT_DOMAIN)
additional_tags=$(generate_var additional_tags $ADDITIONAL_TAGS)
#-- ANSIBLE --##
application_mount_target=$(generate_var application_mount_target $APPLICATION_MOUNT_TARGET)
efs_mount_target=$(generate_var efs_mount_target $EFS_MOUNT_TARGET)
# efs_mount_target=$(generate_var efs_mount_target $EFS_MOUNT_TARGET)
data_mount_target=$(generate_var data_mount_target $DATA_MOUNT_TARGET)
ec2_volume_size=$(generate_var ec2_volume_size $EC2_VOLUME_SIZE)
# ec2_volume_size=$(generate_var ec2_volume_size $EC2_VOLUME_SIZE)


# -------------------------------------------------- #
Expand Down Expand Up @@ -155,6 +119,5 @@ $additional_tags
##-- ANSIBLE --##
$application_mount_target
$efs_mount_target
$data_mount_target
TFVARS
12 changes: 12 additions & 0 deletions operations/deployment/ansible/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# KitchenCI
.kitchen/
.kitchen.local.yml

# Ansible Retry
*.retry

# Vagrant
.vagrant/

# Log files
*.log
Loading

0 comments on commit b686014

Please sign in to comment.