Skip to content

Commit

Permalink
Merge pull request #22 from uicpharm/jcurt/deploy-script
Browse files Browse the repository at this point in the history
Support for CI Deployments
  • Loading branch information
akamal4 authored Dec 10, 2024
2 parents 224975f + fac30c2 commit 56e02f1
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 32 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@ sudo ./init.sh
For more documentation about the stacks, please view the
[stacks readme](./stacks/README.md).

### Using the `deploy` script

For all environments, a `deploy` script is installed in the system that helps with
deploying applications that use the docker-host environment. They provide a
docker-compose stack and any additional required files (`.env` file, etc), and then
deploy with this script.

It takes these standard steps for deployment of UIC Pharmacy stacks:

- Login to your Docker repo
- Stop the application if it is currently running
- Create a pod *(if using podman)*
- Start up the stack using the stack path you provide
- Install the stack as a service *(if using podman)*

Example:

```bash
deploy production.yml
```

This will automatically deploy the application and install it as a service named
`prior-auth-drug-search-production`, if you are running a `podman` system.

Note if you are using an environment file named something other than `.env`, you must
pass it to the script as well:

```bash
deploy production.yml --env-file=production.env
```

The deploy script can handle upgrades as well. Just pass the `--upgrade` flag to ensure
it handles pulling fresh container images:

```bash
deploy production.yml --upgrade
```

### How do I remove the GitHub Runner Service?

If you've installed the GitHub Runner service and now you want to remove it, you can do so
Expand All @@ -43,14 +81,14 @@ by following these steps:
As `root`:

```sh
cd /home/github/actions-runner
cd /home/github/runner
./svc.sh stop
./svc.sh uninstall
```

Then, as the `github` user:

```sh
cd actions-runner
cd ~/runner
./config.sh remove --token your-token-supplied-by-github
```
12 changes: 12 additions & 0 deletions centos-7/docker.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

scr_dir=$(realpath "${0%/*}")

SCRIPT_TITLE="Install Docker"
if [[ " $* " == *" --title "* ]]; then echo "$SCRIPT_TITLE"; exit 0; fi
echo -e "$(tput bold)\n#\n# $SCRIPT_TITLE \n#\n$(tput sgr0)"
Expand All @@ -22,6 +24,16 @@ elif [ -n "$(command -v firewall-cmd)" ]; then
firewall-cmd --reload
fi

# Add scripts to /usr/bin so it will be in the path
if [ -d '/usr/bin' ]; then
bin_dir='/usr/bin'
elif [ -d '/usr/local/bin' ]; then
bin_dir='/usr/local/bin'
fi
if [ -n "$bin_dir" ]; then
ln -f -s "$(realpath "$scr_dir/../shared/bin/deploy.sh")" "$bin_dir/deploy"
fi

# Start/Enable Docker
systemctl enable --now docker

Expand Down
5 changes: 5 additions & 0 deletions custom-words.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
aarch
actionsdir
adduser
buildx
chcon
codeready
containerd
epel
Expand All @@ -13,9 +15,12 @@ jcurt
lolhens
makecache
nodocker
NOPASSWD
realpath
rmul
rpms
runnerdir
runsvc
runuser
scriptnum
setaf
Expand Down
11 changes: 11 additions & 0 deletions macos/docker.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

scr_dir=$(realpath "${0%/*}")

SCRIPT_TITLE="Install Docker Desktop"
if [[ " $* " == *" --title "* ]]; then echo "$SCRIPT_TITLE"; exit 0; fi
echo -e "$(tput bold)\n#\n# $SCRIPT_TITLE \n#\n$(tput sgr0)"
Expand All @@ -13,3 +15,12 @@ open -a Docker
echo -n 'Waiting for Docker Desktop to start up...'
sleep 20
until docker ps &> /dev/null; do echo -n '.'; sleep 5; done

# Add scripts to /usr/local/bin so it will be in the path
# (On macOS, we must use this one because /usr/bin is not permitted)
if [ -d '/usr/local/bin' ]; then
bin_dir='/usr/local/bin'
fi
if [ -n "$bin_dir" ]; then
ln -f -s "$(realpath "$scr_dir/../shared/bin/deploy.sh")" "$bin_dir/deploy"
fi
5 changes: 3 additions & 2 deletions rhel-9/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ elif [ -d '/usr/local/bin' ]; then
bin_dir='/usr/local/bin'
fi
if [ -n "$bin_dir" ]; then
ln -s "$scr_dir/bin/docker-compose.sh" "$bin_dir/docker-compose"
ln -s "$scr_dir/bin/podman-install-service.sh" "$bin_dir/podman-install-service"
ln -f -s "$(realpath "$scr_dir/../shared/bin/deploy.sh")" "$bin_dir/deploy"
ln -f -s "$scr_dir/bin/docker-compose.sh" "$bin_dir/docker-compose"
ln -f -s "$scr_dir/bin/podman-install-service.sh" "$bin_dir/podman-install-service"
fi

# Silence Docker emulation messages
Expand Down
131 changes: 131 additions & 0 deletions shared/bin/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/bin/bash

bold=$(tput bold)
dim=$(tput dim)
ul=$(tput smul)
rmul=$(tput rmul)
red=$(tput setaf 1)
norm=$(tput sgr0)

repo_default=ghcr.io/uicpharm
repo=$repo_default
upgrade=false
verbose=false
interactive=true
env_file=

display_help() {
cat <<EOF
Usage: $(basename "$0") <stack path> [OPTIONS]
Deploys an application stack with UIC Pharmacy standards:
- Login to your Docker repo
- Stop the application if it is currently running
- Create a pod $dim(if using podman)$norm
- Start up the stack using the stack path you point at
- Install the stack as a service $dim(if using podman)$norm
Options:
-h, --help Show this help message and exit.
-e, --env-file Specify an alternate environment file.
-n, --non-interactive Do not prompt, run in non-interactive mode.
-r, --repo Specify the repo to login to. (Default: $ul$repo_default$rmul)
-u, --upgrade Upgrade by pulling the latest image.
-v, --verbose Provide more verbose output.
EOF
}

# Positional parameter: Stack Path
if [[ $1 == -* || -z $1 ]]; then
[[ $1 == -h || $1 == --help ]] || echo "${red}You must provide a stack path.$norm" >&2
display_help; exit 1;
else
stack_path=$(realpath "$1")
shift
fi

# Collect optional arguments.
# spellchecker: disable-next-line
while getopts hnuve:r:-: OPT; do
# Ref: https://stackoverflow.com/a/28466267/519360
if [ "$OPT" = "-" ]; then
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi
case "$OPT" in
h | help) display_help; exit 0 ;;
r | repo) repo=$OPTARG ;;
e | env-file) env_file=$(realpath "$OPTARG") ;;
n | non-interactive) interactive=false ;;
u | upgrade) upgrade=true ;;
v | verbose) verbose=true ;;
\?) echo "${red}Invalid option: -$OPT$norm" >&2 ;;
*) echo "${red}Some of these options are invalid:$norm $*" >&2; exit 2 ;;
esac
done
shift $((OPTIND - 1))

# Validation
[[ ! -e $stack_path ]] && echo "${red}The stack $ul$stack_path$rmul doesn't exist.$norm" >&2 && exit 2

dir=${stack_path%/*}
env_file=${env_file:-$dir/.env} # If no env file provided, use standard `.env` in stack directory
pod_name=$(basename "$dir")-$(basename "$stack_path" .yml)

$verbose && (
echo "$bold${ul}Settings$norm"
echo
echo "${bold}Stack Path:$norm $stack_path"
echo "${bold}Env File:$norm $env_file"
echo "${bold}Pod Name:$norm $pod_name"
echo "${bold}Repo:$norm $repo"
echo "${bold}Upgrade:$norm $upgrade"
echo
)

# Check if logged into ghcr.io/uicpharm
$interactive && (
echo "Checking login to $repo..."
if ! docker login "$repo"; then
echo "Access to $repo is required. Aborting."
exit 1
fi
)

# Detect podman
[[ $(docker --version) == podman* ]] && IS_PODMAN=true || IS_PODMAN=false

# Detect podman-install-service
which podman-install-service &> /dev/null && HAS_SERVICE_INSTALLER=true || HAS_SERVICE_INSTALLER=false

# Stop the stack. Detect if its a service with systemctl or just a docker-compose stack.
# By querying `is-active`, and then checking `docker-compose ps`, it will even catch the
# scenario when the service is newly created but systemctl doesn't see it yet.
if which systemctl &> /dev/null && ! systemctl status "$pod_name" 2>&1 | grep -q 'could not be found' && systemctl -q is-active "$pod_name"; then
systemctl stop "$pod_name"
elif [[ $(docker-compose -f "$stack_path" --env-file "$env_file" ps -q 2>/dev/null | wc -w) -gt 0 ]]; then
docker-compose -f "$stack_path" down
fi

# Create pod
$IS_PODMAN && ! podman pod exists "$pod_name" && podman pod create --name "$pod_name"

# Prep docker-compose args
podman_args=()
docker_args=('-d')
$IS_PODMAN && podman_args+=(--pod "$pod_name")

# Upgrade stack if requested
if $upgrade; then
$IS_PODMAN && podman_args+=(--pull always) || docker_args+=(--pull always)
fi

# Finalize args, start stack and install service
[[ ${#podman_args[@]} -gt 0 ]] && podman_args=(--podman-run-args "${podman_args[*]}")
(
cd "$dir" && \
docker-compose "${podman_args[@]}" -f "$stack_path" --env-file "$env_file" up "${docker_args[@]}" && \
$IS_PODMAN && $HAS_SERVICE_INSTALLER && podman-install-service "$pod_name" -n && \
echo "Installed service $pod_name!"
)
Loading

0 comments on commit 56e02f1

Please sign in to comment.