Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI fixes and visual improvements #69

Merged
merged 5 commits into from
May 16, 2024
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: 8 additions & 0 deletions test-package/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.9'

services:
test-package:
ports:
- target: 1
published: 2
mode: host
5 changes: 5 additions & 0 deletions test-package/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: '3.9'

services:
test-package:
image: test/image
11 changes: 11 additions & 0 deletions test-package/package-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "test-package-root",
"name": "Test Package",
"description": "A package for testing",
"type": "infrastructure",
"version": "0.0.1",
"dependencies": [],
"environmentVariables": {
"TEST_VAR": "test-value"
}
}
63 changes: 63 additions & 0 deletions test-package/swarm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

declare ACTION=""
declare MODE=""
declare COMPOSE_FILE_PATH=""
declare UTILS_PATH="/home/ryanc/git/instant-v2/utils"
declare SERVICE_NAMES=()

function init_vars() {
ACTION=$1
MODE=$2

COMPOSE_FILE_PATH=$(
cd "$(dirname "${BASH_SOURCE[0]}")" || exit
pwd -P
)

# UTILS_PATH="${COMPOSE_FILE_PATH}/../utils"

SERVICE_NAMES=(
"test-package"
)

readonly ACTION
readonly MODE
readonly COMPOSE_FILE_PATH
readonly UTILS_PATH
readonly SERVICE_NAMES
}

# shellcheck disable=SC1091
function import_sources() {
source "${UTILS_PATH}/docker-utils.sh"
source "${UTILS_PATH}/log.sh"
}

main() {
init_vars "$@"
import_sources

log debug "Debug message"
log info "Info message"
log warn "Warning message"
log error "Error message"

try 'false' 'catch' 'Failed to run false (expected)'

if [[ "${ACTION}" == "init" ]] || [[ "${ACTION}" == "up" ]]; then
log info "Running package in Single node mode"

elif [[ "${ACTION}" == "down" ]]; then
log info "Scaling down package"

docker::scale_services_down "${SERVICE_NAMES[@]}"
elif [[ "${ACTION}" == "destroy" ]]; then
log info "Destroying package"

else
log error "Valid options are: init, up, down, or destroy"
fi
}

main "$@"
2 changes: 1 addition & 1 deletion utils/config-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ config::await_service_running() {

docker service rm "$STACK_NAME"_await-helper &>/dev/null

try "docker stack deploy -c $AWAIT_HELPER_FILE_PATH $STACK_NAME" throw "Failed to deploy await helper"
try "docker stack deploy -d -c $AWAIT_HELPER_FILE_PATH $STACK_NAME" throw "Failed to deploy await helper"
until [[ $(docker service ls -f name="$STACK_NAME"_"$SERVICE_NAME" --format "{{.Replicas}}") == *"$SERVICE_INSTANCES/$SERVICE_INSTANCES"* ]]; do
config::timeout_check "$start_time" "$SERVICE_NAME to start" "$exit_time" "$warning_time"
sleep 1
Expand Down
6 changes: 3 additions & 3 deletions utils/docker-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ docker::deploy_service() {
docker::prepare_config_digests "$DOCKER_COMPOSE_PATH/$DOCKER_COMPOSE_FILE" ${docker_compose_param//-c /}
docker::ensure_external_networks_existence "$DOCKER_COMPOSE_PATH/$DOCKER_COMPOSE_FILE" ${docker_compose_param//-c /}

try "docker stack deploy \
try "docker stack deploy -d \
rcrichton marked this conversation as resolved.
Show resolved Hide resolved
-c ${DOCKER_COMPOSE_PATH}/$DOCKER_COMPOSE_FILE \
$docker_compose_param \
--with-registry-auth \
Expand Down Expand Up @@ -367,7 +367,7 @@ docker::deploy_config_importer() {
config::set_config_digests "$CONFIG_COMPOSE_PATH"

try \
"docker stack deploy -c ${CONFIG_COMPOSE_PATH} ${STACK_NAME}" \
"docker stack deploy -d -c ${CONFIG_COMPOSE_PATH} ${STACK_NAME}" \
throw \
"Wrong configuration in $CONFIG_COMPOSE_PATH"

Expand Down Expand Up @@ -519,7 +519,7 @@ docker::join_network() {
else
log info "Waiting to join $SERVICE_NAME to external network $NETWORK_NAME ..."
try \
"docker service update \
"docker service update -d \
rcrichton marked this conversation as resolved.
Show resolved Hide resolved
--network-add name=$NETWORK_NAME \
$SERVICE_NAME" \
throw \
Expand Down
15 changes: 9 additions & 6 deletions utils/log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ function log() {

local -A emoticons
emoticons['DEBUG']='🔷'
emoticons['INFO']=''
emoticons['INFO']='ℹ️ '
emoticons['NOTICE']='💡'
emoticons['WARN']='🔶'
emoticons['ERROR']='❌'
emoticons['CRIT']='⛔'
emoticons['ALERT']='❗'
emoticons['ALERT']='❗'
emoticons['EMERG']='🚨'
emoticons['DEFAULT']=''

Expand All @@ -140,15 +140,15 @@ function log() {
# Standard Output (Pretty)
case "${level}" in
'default' | 'info' | 'warn')
echo -e "${std_line}"
echo -en "${std_line}"
;;
'debug')
if [ "${debug_level}" -gt 0 ]; then
echo -e "${std_line}"
echo -en "${std_line}"
fi
;;
'error')
echo -e "${std_line}" >&2
echo -en "${std_line}" >&2
;;
*)
log 'error' "Undefined log level trying to log: ${@}"
Expand Down Expand Up @@ -186,7 +186,10 @@ overwrite() {
if [ "${DEBUG}" -eq 1 ]; then
log info "${MESSAGE}"
else
log info "${CLEAR_PREV_LINE}${MESSAGE}"
# We need to find a way to do this that works with concurrency
# It might not be possible, disable for now
# log info "${CLEAR_PREV_LINE}${MESSAGE}"
log info "${MESSAGE}"
rcrichton marked this conversation as resolved.
Show resolved Hide resolved
fi
}

Expand Down
Loading