Skip to content
Snippets Groups Projects

Add remote-dev-env.sh

Closed Subhashis Suara requested to merge susuara/drupal-operations:remote-dev-env into master
1 file
+ 216
0
Compare changes
  • Side-by-side
  • Inline
+ 216
0
#!/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.
#
# For drupalsite name, enter the name that appears when you do `oc get drupalsite`
#
# For remote-dev-env image go to:
# Gitlab > CI/CD > Pipelines
# Open the required pipeline
# Run the remote-dev-env job under Develop stage
# After the remote-dev-env job has succeeded:
# You can open the job and copy the image link from there
# OR
# You can go to:
# Gitlab > Packages & Registries > Container Registry
# Open drupal/paas/cern-drupal-distribution/remote-dev-env
# Copy the relevant image link
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
Loading