Skip to content

Commit

Permalink
wip: Enhance CLI functionalities #188
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Nouguier committed Nov 29, 2020
1 parent 4811b65 commit 6148da4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 53 deletions.
16 changes: 0 additions & 16 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ The service should be running before archiving.

Displays some information about the current workspace

### labels

`usage: kargo labels <add|remove>`

Add or remove the labels assigned to the nodes. The node list is read from the environment variable `NODES` and the labels are read for each node from the environment variable `<NODE_NAME>_LABLELS`.

### pull

`usage: kargo pull`
Expand All @@ -134,16 +128,6 @@ Re-deploys the specified stack. The stack must be defined in the `STACKS` variab
This is a convenient command that run the series of commands: `remove`, `configure` and `deploy`.
:::

### update

`usage: kargo update [--force|-f] <stack|all>`

Tells **Kargo** to update the services of the given stack. You can force the update with the option `--force`.

::: tip
The `--force` flag causes the service’s tasks to be shut down and replaced with new ones.
:::

### use

`usage: kargo use <workspace>`
Expand Down
63 changes: 26 additions & 37 deletions kargo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ kargo_usage()
printf " %-10s %-30s %s\n" "execute" "<script>" "exec the given script"
printf " %-10s %-30s %s\n" "info" "" "generate the current configuration"
printf " %-10s %-30s %s\n" "image" "<load|save> <service>" "load/save the given service from an archive/to an archive"
printf " %-10s %-30s %s\n" "labels" "<add|remove>" "add/remove the labels assigned to the nodes"
printf " %-10s %-30s %s\n" "pull" "" "pull the current workspace"
printf " %-10s %-30s %s\n" "redeploy" "<stack|all>" "re deploy the given stack"
printf " %-10s %-30s %s\n" "remove" "<stack|all>" "remove the given stack"
Expand Down Expand Up @@ -98,6 +97,31 @@ kargo_check_stack()
fi
}

kargo_update_labels()
{
if [ -n "$NODES" ]; then
for NODE in $NODES; do
local NODE_NAME=${NODE//-/_}
local NODE_LABELS_VAR=${NODE_NAME^^}_LABELS
local NODE_NEW_LABELS=${!NODE_LABELS_VAR}
local NODE_EXISTING_LABELS=`docker node inspect -f '{{ range $k, $v := .Spec.Labels }}{{ $k }}={{ $v }} {{end}}' $NODE`
# Keep the existing labels if needed
for NODE_EXISTING_LABEL in $NODE_EXISTING_LABELS; do
if [[ ! $NODE_NEW_LABELS =~ $NODE_EXISTING_LABEL ]]; then
local NODE_EXISTING_LABEL_KEY=${NODE_EXISTING_LABEL%=*}
docker node update --label-rm $NODE_EXISTING_LABEL_KEY $NODE > /dev/null
fi
done
# Add the label of needed
for NODE_NEW_LABEL in $NODE_NEW_LABELS; do
if [[ ! $NODE_EXISTING_LABELS =~ $NODE_NEW_LABEL ]]; then
docker node update --label-add $NODE_NEW_LABEL $NODE > /dev/null
fi
done
done
fi
}

kargo_configure()
{
kargo_read_workspace
Expand Down Expand Up @@ -145,6 +169,7 @@ kargo_configure()
rsync -au --delete .tmp/deploy .kargo/
rsync -au --delete .tmp/configs .kargo/
rm -fr .tmp
kargo_update_labels
echo workspace \"$WORKSPACE\" ready.
}

Expand Down Expand Up @@ -260,41 +285,6 @@ kargo_image()
esac
}

kargo_labels()
{
kargo_read_configuration
if [ -z $1 ]; then
echo "error: the command \"kargo labels\" requires one argument."
kargo_usage
exit 1
fi
if [ "$1" != "add" ] && [ "$1" != "remove" ]; then
echo "error: the argument of the command \"kargo labels\" should be either \"add\" or \"remove\"."
kargo_usage
exit 1
fi
for NODE in $NODES; do
local NODE_ID=`docker node ls | grep $NODE | awk '{print $1}'`
if [ -z $NODE_ID ]; then
echo "error: uable to find the node $NODE"
exit 1
fi
local NODE_NAME=${NODE//-/_}
local NODE_LABELS_VAR=${NODE_NAME^^}_LABELS
local NODE_LABELS=${!NODE_LABELS_VAR}
for LABEL in $NODE_LABELS; do
if [ "$1" == "add" ]; then
echo -e " adding $LABEL to $NODE"
docker node update --label-add $LABEL $NODE_ID > /dev/null
else
local LABEL_KEY=`echo $LABEL | cut -d'=' -f1`
echo -e " removing $LABEL_KEY from $NODE"
docker node update --label-rm $LABEL_KEY $NODE_ID > /dev/null
fi
done
done
}

kargo_build()
{
kargo_read_configuration
Expand Down Expand Up @@ -415,7 +405,6 @@ case $1 in
exec) kargo_exec "$2" "$3";;
info) kargo_info;;
image) kargo_image "$2" "$3";;
labels) kargo_labels "$2";;
pull) kargo_pull;;
redeploy) kargo_redeploy "$2";;
remove) kargo_remove "$2";;
Expand Down

0 comments on commit 6148da4

Please sign in to comment.