Skip to content
Snippets Groups Projects
Commit 30f9b7db authored by Subhashis Suara's avatar Subhashis Suara
Browse files

Add remote-dev-env.sh

parent 01a846b4
No related branches found
No related tags found
No related merge requests found
Pipeline #4354120 passed
#!/bin/bash
# This is a helper script for creating or removing remote development environment.
# Before running this script, make sure that your drupalsite is ready and you have a remote-dev-env image ready.
check_script_requirements() {
echo $'Welcome to remote development environment helper script!\nBefore proceeding further, make sure that your drupalsite is ready and you have a remote-dev-env image ready.\nChecking requirements...\n'
# Check if oc cli is installed
if ! command -v oc &> /dev/null
then
echo "oc command not found! Please install oc cli: https://docs.okd.io/4.9/cli_reference/openshift_cli/getting-started-cli.html"
exit
fi
# Check if user is logged in to the openshift server
oc project &> /dev/null
if ! [ "$?" == "0" ]; then
echo "Please log in to the openshift server or add cluster configuration and try again!"
exit
fi
# Check if jq is installed
if ! command -v jq &> /dev/null
then
echo "jq command not found! Please install jq: https://stedolan.github.io/jq/download/"
exit
fi
}
take_user_input() {
# Ask user for oc drupalsite name, gitlab registy image and choice of action
read -p 'Enter drupalsite name: ' drupalsite
read -p 'Enter remote-dev-env gitlab registry image: ' remote_dev_env_img
read -p $'\nDo you want to:\n1. Create a remote development environment\n2. Remove an exisitng remote development environment\n\nEnter your choice (1 or 2): ' user_choice
# New line
echo " "
}
check_user_input() {
# Check if drupalsite input is empty
if [[ -z $drupalsite ]]; then
echo "drupalsite input is empty! Exiting..."
exit
fi
# Check if remote-dev-env gitlab registry image input is empty
if [[ -z $remote_dev_env_img ]]; then
echo "remote-dev-env gitlab registry image input is empty! Exiting..."
exit
fi
# Check if user_choice is empty or invalid
if [[ -z $user_choice ]] || ! [[ "$user_choice" =~ ^[1-2]+$ ]]; then
echo "Invalid choice selection! Exiting..."
exit
fi
# Get project name
project=$(oc project -q)
# Check if deployment exists
oc get "deployment/${drupalsite}" &> /dev/null
if ! [[ "$?" == "0" ]]; then
echo "\"${drupalsite}\" deployment does not exist in project \"${project}\"! Please try again!"
exit
fi
# Check if drupalsite exists
oc get "drupalsite/${drupalsite}" &> /dev/null
if ! [[ "$?" == "0" ]]; then
echo "\"${drupalsite}\" drupalsite does not exist in project \"${project}\"! Please try again!"
exit
fi
# Check if service exists
oc get "service/${drupalsite}" &> /dev/null
if ! [[ "$?" == "0" ]]; then
echo "\"${drupalsite}\" service does not exist in project \"${project}\"! Please try again!"
exit
fi
}
create_remote_dev_env() {
echo $'Creating remote development environment for "'"${drupalsite}"'" drupalsite...'$'\n'''
# Add admin-pause-reconcile: ' ' to annotations in drupalsite (To allow edits to deployment without the operator reverting it back)
oc get "drupalsite/${drupalsite}" -o json | jq '.metadata.annotations += {"admin-pause-reconcile": " "}' | cat > "${drupalsite}-drupalsite".json
# Apply the drupalsite changes
oc apply -f "${drupalsite}-drupalsite".json
oc get "deployment/${drupalsite}" -o json \
`# Inject remote-dev-env image into the deployment` \
| jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").image) |= "'"$remote_dev_env_img"'"' \
`# Change commands to ["/bin/bash", "-c"] in the deployment` \
| jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").command) = ["/bin/bash", "-c"]' \
`# Add args as ["/run-php-fpm.sh & code-server /app"] in the deployment` \
| jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").args) = ["/run-php-fpm.sh & code-server /app"]' \
`# Add port config for code-server in the deployment` \
| jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").ports) += [{"containerPort":8888,"name":"code-server","protocol":"TCP"}]' \
`# Remove startupProbe and livenessProbe in the deployment (Otherwise it keeps restarting the pod)` \
| jq 'del(.spec.template.spec.containers[] | select(.name == "php-fpm").startupProbe)' \
| jq 'del(.spec.template.spec.containers[] | select(.name == "php-fpm").livenessProbe)' \
`# Output the deployment JSON` \
| cat > "${drupalsite}-deployment".json
# Apply the deployment changes
oc apply -f "${drupalsite}-deployment".json
# Create remote dev env service
oc get "service/${drupalsite}" -o json \
`# Change the service name` \
| jq '.metadata.name = "dev-" + .metadata.name' \
`# Add port config for code-server in the service` \
| jq '.spec.ports = [{"name":"code-server","port":8888,"protocol":"TCP","targetPort":8888}]' \
`# Remove clusterIP and clusterIPs to avoid conflict` \
| jq 'del(.spec.clusterIP)' \
| jq 'del(.spec.clusterIPs)' \
`# Output the service JSON` \
| cat > "${drupalsite}-service".json
# Create new service for code-server
oc create -f "${drupalsite}-service".json
# Create remote dev env route
oc create route edge --service="dev-${drupalsite}" --hostname="dev-${drupalsite}.webtest.cern.ch"
echo $'\nWaiting for new pod(s) to run and DNS to update...'
# Wait till the link for remote development environment is accessible
while true; do
if (($(curl -o /dev/null -s -w "%{http_code}" https://dev-"$drupalsite".webtest.cern.ch) == "302" )); then
break;
fi
sleep 5
done
echo $'\nThe remote development environment for "'"${drupalsite}"'" drupalsite is ready! You can access it at https://dev-'"${drupalsite}"'.webtest.cern.ch'
# Cleanup files and exit
echo $'\nCleaning up...'
rm -f "${drupalsite}-drupalsite".json
rm -f "${drupalsite}-deployment".json
rm -f "${drupalsite}-service".json
echo "Exiting script... Goodbye! Have a great day!"
exit
}
remove_remote_dev_env() {
echo $'Removing remote development environment for "'"${drupalsite}"'" drupalsite...'$'\n'''
# Remove admin-pause-reconcile: ' ' from annotations in drupalsite
oc get "drupalsite/${drupalsite}" -o json | jq 'del(.metadata.annotations."admin-pause-reconcile")' | cat > "${drupalsite}-drupalsite".json
# Apply the drupalsite changes
oc apply -f "${drupalsite}-drupalsite".json
# Delete the deployment (Operator will redeploy the original version without code-server as the "admin-pause-reconcile" label has been removed)
oc delete deployment/"${drupalsite}"
# Remove remote dev env service
oc delete service/"dev-${drupalsite}"
# Remove remote dev env route
oc delete route/"dev-${drupalsite}"
echo $'\nThe remote development environment for "'"${drupalsite}"'" drupalsite has been removed!'
# Cleanup files and exit
echo $'\nCleaning up...'
rm -f "${drupalsite}-drupalsite".json
echo "Exiting script... Goodbye! Have a great day!"
exit
}
main_execution() {
# Check the script requirements before proceeding further
check_script_requirements
# Take user input for drupalsite name and gitlab registy image
take_user_input
# Check user input
check_user_input
# Create or remove remote development environment according to the user's choice
case $user_choice in
"1")
create_remote_dev_env
;;
"2")
remove_remote_dev_env
;;
*)
echo $'\nUnexpected choice selection! Exiting...'
exit
;;
esac
}
main_execution
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment