Skip to content

Commit

Permalink
Update automation script for git-registration (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu committed Oct 30, 2023
1 parent ec66d0d commit 15799d9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
41 changes: 26 additions & 15 deletions bin/container
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ ROOT=$(dirname $0)/..

function usage {
echo Error: $*
echo Usage: $0 target { prep, build } [--no-check] [repo]
echo Project: $GCP_PROJECT
echo Try starting with: $0 build
echo Usage: $0 target { prep, build, push, apply } [--no-check] [repo]
exit 1
}

Expand All @@ -30,6 +28,7 @@ git_repo=${git_prefix#*.com:}

GCP_PROJECT=$(gcloud config get project)
REPOSITORY=ghcr.io/${git_repo}
TEMPLATES=$(cd etc; ls k8s_*.yaml)

if [[ $1 == "--no-check" ]]; then
NOCHECK=1
Expand Down Expand Up @@ -77,7 +76,21 @@ version=`git describe`

RUNARGS="--rm -ti -v $PWD/var:/udmi -v $HOME/.config:/root/.config --tmpfs /tmp"

if [[ $cmd == prep || $cmd == build ]]; then
prep=
build=
push=
apply=

[[ $cmd == apply ]] && apply=y
[[ $cmd == push || -n $apply ]] && push=y
[[ $cmd == build || -n $push ]] && build=y
[[ $cmd == prep || -n $build ]] && prep=y

if [[ -z $prep ]]; then
usage Unknown command $cmd
fi

if [[ -n $prep ]]; then
rm -rf var tmp && mkdir -p var tmp

cp -f etc/*.json var/ || true
Expand All @@ -97,14 +110,13 @@ EOF
more var/deployed_version.json | cat
fi

if [[ $cmd == build ]]; then
if [[ -n $build ]]; then
echo Building Dockerfile.$target
docker build -f Dockerfile.$target -t $target .
docker tag $target $udmi_ref
fi

TEMPLATES=$(cd etc; ls k8s_*.yaml)
if [[ $cmd == build && $REPOSITORY != local ]]; then
docker tag $target $udmi_ref
if [[ -n $push ]]; then
docker push $udmi_ref
hash=$(docker images --digests ${udmi_ref%:*} | fgrep $udmi_tag | awk '{print $3}')
ihash=$udmi_ref@$hash
Expand All @@ -120,15 +132,14 @@ if [[ $cmd == build && $REPOSITORY != local ]]; then
echo Customized output in tmp/$file
fi
done
echo Done with container build.
for file in tmp/k8s_*.yaml; do
echo " kubectl apply -f $target/$file"
done
fi

if [[ $cmd == prep ]]; then
echo Done with container prep.
elif [[ $cmd == build ]]; then
echo Done with container build.
if [[ -n $apply ]]; then
for file in tmp/k8s_*.yaml; do
echo kubectl apply -f $target/$file
kubectl apply -f ../$target/$file
done
else
fail Unknown container command $cmd
fi
3 changes: 3 additions & 0 deletions bin/set_project
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

if [[ $# != 1 ]]; then
echo Usage: $0 gcp_project[/udmi_namespace]
echo
echo Current context/project is $(kubectl config current-context)
echo
false
fi

Expand Down
59 changes: 43 additions & 16 deletions validator/bin/automate
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -e
#!/bin/bash -ex
#
# Automation of tools using PubSub triggers.
#
Expand All @@ -8,7 +8,8 @@ UDMI_PREFIX=${UDMI_NAMESPACE}~
PUBSUB_FILE=pubsub.json
MESSAGE_FILE=message.json
RETRY_DELAY_SEC=60
MODEL_DIR=source_model
CLONE_DIR=source_model/
MODEL_SUBDIR=udmi/
CONFIG_FILE=registrar_config.json
REGISTRAR_TRIGGER=run-registrar

Expand Down Expand Up @@ -60,29 +61,54 @@ while true; do
repo_url=$(jq -r .url $MESSAGE_FILE)
repo_name=${repo_url##*/}

echo Cloning repo $repo_name from $repo_url into $MODEL_DIR
echo Cloning repo $repo_name from $repo_url into $CLONE_DIR

rm -rf $MODEL_DIR
rm -rf $CLONE_DIR

echo gcloud source repos clone $repo_name $MODEL_DIR --project=$GCP_PROJECT
gcloud source repos clone $repo_name $MODEL_DIR --project=$GCP_PROJECT || true
echo gcloud source repos clone $repo_name $CLONE_DIR --project=$GCP_PROJECT
gcloud source repos clone $repo_name $CLONE_DIR --project=$GCP_PROJECT || true

if [[ ! -d $MODEL_DIR ]]; then
if [[ ! -d $CLONE_DIR ]]; then
echo
echo Error cloning repo, try again!
echo
continue
fi

(
cd $MODEL_DIR
cd $CLONE_DIR
git checkout main || true
pwd
ls -l
)

if [[ ! -f $MODEL_DIR/$REGISTRAR_TRIGGER ]]; then
echo Missing $MODEL_DIR/$REGISTRAR_TRIGGER trigger file, doing nothing!
if [[ ! -f $CLONE_DIR/$REGISTRAR_TRIGGER ]]; then
echo Missing $CLONE_DIR/$REGISTRAR_TRIGGER trigger file, doing nothing!
continue
fi

MODEL_DIR=$(realpath $CLONE_DIR/$MODEL_SUBDIR/)
HISTORY_DIR=$MODEL_DIR/history
SUMMARY_FILE=$MODEL_DIR/out/registration_summary.json

TIMESTAMP=$(date --utc +%y%m%d%H%M%S)
mkdir -p $HISTORY_DIR
HISTORY_FILE=$HISTORY_DIR/registration_summary_${TIMESTAMP}.json
touch $HISTORY_FILE
rm -f $CLONE_DIR/$REGISTRAR_TRIGGER $SUMMARY_FILE

echo
echo Updating site model with start-of-action:
(
cd $CLONE_DIR
git add .
git status
git commit -m "Starting registration run $HISTORY_FILE on $HOSTNAME"
git push || touch failed
)

if [[ -f $CLONE_DIR/failed ]]; then
echo Run start push failed, aborting.
continue
fi

Expand All @@ -92,7 +118,7 @@ while true; do
"project_id": "$GCP_PROJECT",
"udmi_namespace": "$UDMI_NAMESPACE",
"bridge_host": "${host_suffix}",
"site_model": "$MODEL_DIR/udmi"
"site_model": "$MODEL_DIR"
}
EOF
echo Registrar config $CONFIG_FILE:
Expand All @@ -102,14 +128,15 @@ EOF
echo validator/bin/registrar $CONFIG_FILE
validator/bin/registrar $CONFIG_FILE || true

cp $SUMMARY_FILE $HISTORY_FILE

echo
echo Site model status:
echo Updating site model with completion results:
(
cd $MODEL_DIR
rm -f $REGISTRAR_TRIGGER
git status
cd $CLONE_DIR
git add .
git commit -F udmi/out/registration_summary.json
git status
git commit -F $SUMMARY_FILE
git push || true
)
done

0 comments on commit 15799d9

Please sign in to comment.