Skip to content

Commit

Permalink
Refines the shared environment template
Browse files Browse the repository at this point in the history
TL;DR
-----

Improves the shared environment template based on recent lab work

Details
-------

Updates the shared environment template based on recurring lifecycle
script needs.

* Assures all challenges have the minimal lifecycle scripts for each
  stage and host
* Uses `/usr/bin/env bash` as the shebang for all lifecycle scripts
* Sets `KUBECONFIG` or `HOME_DIR` variables  at the start of each
  lifecycle script
* Sets the most used agent variables in the track setup scripts
* Adds `get_app_slug` to the header and refines it to use the
  application name instead of assuming the first app is the right one
  • Loading branch information
crdant committed May 1, 2024
1 parent 80b8461 commit 77c1345
Show file tree
Hide file tree
Showing 27 changed files with 211 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

# save the entire session to check user inputs and outputs
tmux capture-pane -t shell -S -
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

# clear the tmux pane and scrollback to look like a fresh shell
tmux clear-history -t shell
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail

HOME_DIR=/home/replicant
source /etc/profile.d/header.sh

### Assure the tmux session exists
#
# In a test scenario Instuqt does not run the user shell for the
# challenge, which means the tmux session is never established. We
# need to session for the solve scripts for other challenges to
# succeed, so let's create it here.
#
if ! tmux has-session -t shell ; then
tmux new-session -d -s shell su - replicant
fi

# Wait for Instruqt bootstrap to be complete
while [ ! -f /opt/instruqt/bootstrap/host-bootstrap-completed ]
do
echo "Waiting for Instruqt to finish booting the VM"
sleep 1
done

# Display PARTICIPANT_ID info for Vendor Portal
cat<<SCRIPT >> ${HOME_DIR}/.bashrc
show_credentials () {
echo Credentials for https://vendor.replicated.com
echo Username: $INSTRUQT_PARTICIPANT_ID@replicated-labs.com
echo Password: $INSTRUQT_PARTICIPANT_ID
}
show_credentials
SCRIPT
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

### Assure the tmux session exists
#
# In a test scenario Instuqt does not run the user shell for the
# challenge, which means the tmux session is never established. We
# need to session for the solve scripts for other challenges to
# succeed, so let's create it here.
#

if ! tmux has-session -t shell ; then
tmux new-session -d -s shell su - replicant
fi

# set the variable into the shell
tmux send-keys -t shell export SPACE 'THIS="the way"' ENTER
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

# save the entire session to check user inputs and outputs
tmux capture-pane -t shell -S -
SESSION=$(tmux save-buffer -)

# check for disconnection
# check that the variable has been set
if ! grep -qE 'echo' <(echo ${SESSION}) ; then
fail-message 'Please make sure you can still access `$THIS`'
exit 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
16 changes: 8 additions & 8 deletions instruqt/shared-env-template/02-using-the-template/cleanup-shell
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/sh
#
# This script runs when the platform cleanup the challenge.
#
# The platform determines if the script was successful using the exit code of this
# script. If the exit code is not 0, the script fails.
#
#!/usr/bin/env bash

echo "This is the cleanup script"
# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
#
# clear the tmux pane and scrollback to look like a fresh shell
tmux clear-history -t shell
tmux send-keys -t shell clear ENTER

exit 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
19 changes: 19 additions & 0 deletions instruqt/shared-env-template/02-using-the-template/setup-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

### Assure the tmux session exists
#
# In a test scenario Instuqt does not run the user shell for the
# challenge, which means the tmux session is never established. We
# need to session for the solve scripts for other challenges to
# succeed, so let's create it here.
#
if ! tmux has-session -t shell ; then
tmux new-session -d -s shell su - replicant
fi

exit 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
15 changes: 3 additions & 12 deletions instruqt/shared-env-template/02-using-the-template/solve-shell
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

### Assure the tmux session exists
#
# In a test scenario Instuqt does not run the user shell for the
# challenge, which means the tmux session is never established. We
# need to session for the solve scripts for other challenges to
# succeed, so let's create it here.
#

if ! tmux has-session -t shell ; then
tmux new-session -d -s shell su - replicant
fi

# show the variable in the shell
tmux send-keys -t shell echo SPACE '$THIS' ENTER
8 changes: 8 additions & 0 deletions instruqt/shared-env-template/03-tips-and-tricks/check-cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
8 changes: 8 additions & 0 deletions instruqt/shared-env-template/03-tips-and-tricks/check-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

exit 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
9 changes: 9 additions & 0 deletions instruqt/shared-env-template/03-tips-and-tricks/cleanup-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

exit 0

8 changes: 8 additions & 0 deletions instruqt/shared-env-template/03-tips-and-tricks/setup-cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
19 changes: 19 additions & 0 deletions instruqt/shared-env-template/03-tips-and-tricks/setup-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

### Assure the tmux session exists
#
# In a test scenario Instuqt does not run the user shell for the
# challenge, which means the tmux session is never established. We
# need to session for the solve scripts for other challenges to
# succeed, so let's create it here.
#
if ! tmux has-session -t shell ; then
tmux new-session -d -s shell su - replicant
fi

exit 0
8 changes: 8 additions & 0 deletions instruqt/shared-env-template/03-tips-and-tricks/solve-cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
source /etc/profile/header.sh
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

exit 0
8 changes: 8 additions & 0 deletions instruqt/shared-env-template/03-tips-and-tricks/solve-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This set line ensures that all failures will cause the script to error and exit
set -euxo pipefail
HOME_DIR=/home/replicant
source /etc/profile/header.sh

exit 0
1 change: 1 addition & 0 deletions instruqt/shared-env-template/track_scripts/setup-cluster
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -euxo pipefail

# use our shared libary in setup scripts
curl -s -o /etc/profile.d/header.sh https://raw.githubusercontent.com/replicatedhq/kots-field-labs/main/libs/header.sh
source /etc/profile.d/header.sh

# simple SSH client setup so we can SSH to/from the shell
cat <<EOF >> "$HOME/.ssh/config"
Expand Down
12 changes: 12 additions & 0 deletions instruqt/shared-env-template/track_scripts/setup-shell
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ HOME_DIR=/home/replicant

# use our shared libary in setup scripts
curl -s -o /etc/profile.d/header.sh https://raw.githubusercontent.com/replicatedhq/kots-field-labs/main/libs/header.sh
source /etc/profile.d/header.sh

# simple SSH client setup so we can SSH to/from the shell
cat <<EOF >> "$HOME/.ssh/config"
Expand All @@ -20,3 +21,14 @@ ssh-keygen -t rsa -f /etc/dropbear/dropbear_rsa_host_key -N ''
# change the cluster URI
yq -i '.clusters[0].cluster.server = "https://cluster:6443"' ${HOME_DIR}/.kube/config
chown -R replicant ${HOME_DIR}/.kube

# set up some variables that are needed in most labs

# there's only one app created by the automation, so just grab the first in the list
access_token=$(get_api_token)
app_slug=$(get_app_slug)

agent variable set USERNAME $(get_username)
agent variable set PASSWORD $(get_password)
agent variable set REPLICATED_API_TOKEN ${access_token}
agent variable set REPLICATED_APP ${app_slug}
6 changes: 6 additions & 0 deletions libs/header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ get_api_token () {

echo ${access_token}
}

get_app_slug () {
application=${1:-"Slackernews"}
access_token=$(get_api_token)
app_slug=$(curl --header 'Accept: application/json' --header "Authorization: ${access_token}" https://api.replicated.com/vendor/v3/apps | jq -r '| jq -r --arg application ${application} '.apps[] | select( .name == $application ) | .slug')
}

0 comments on commit 77c1345

Please sign in to comment.