Add remote-dev-env.sh
This MR adds the remote development environment script as discussed in: https://gitlab.cern.ch/webservices/webframeworks-planning/-/issues/990#note_5791923
Merge request reports
Activity
mentioned in merge request cern-drupal-distribution!141 (closed)
- scripts/remote-dev-env.sh 0 → 100755
15 # Gitlab > Packages & Registries > Container Registry 16 # Open drupal/paas/cern-drupal-distribution/remote-dev-env 17 # Copy the relevant image link 18 19 check_script_requirements() { 20 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' 21 22 # Check if oc cli is installed 23 if ! command -v oc &> /dev/null 24 then 25 echo "oc command not found! Please install oc cli: https://docs.okd.io/4.9/cli_reference/openshift_cli/getting-started-cli.html" 26 exit 27 fi 28 29 # Check if user is logged in to the openshift server 30 oc project &> /dev/null This is good. But the recommended way is to do
oc whoami
- scripts/remote-dev-env.sh 0 → 100755
59 fi 60 61 # Check if remote-dev-env gitlab registry image input is empty 62 if [[ -z $remote_dev_env_img ]]; then 63 echo "remote-dev-env gitlab registry image input is empty! Exiting..." 64 exit 65 fi 66 67 # Check if user_choice is empty or invalid 68 if [[ -z $user_choice ]] || ! [[ "$user_choice" =~ ^[1-2]+$ ]]; then 69 echo "Invalid choice selection! Exiting..." 70 exit 71 fi 72 73 # Get project name 74 project=$(oc project -q) There's a minor catch here. Here, we can not assume, the user is in the 'right' project already. Say for example, when I use https://gitlab.cern.ch/paas-tools/oc-sso-login to login, the default project after I log in is
default
. I do understand, you are trying to check if theDrupalsite
itself in the given project in later steps. It should be easier if we inform the users early on in the script to make sure that 'they are in the desired project' or take the project name as an input as well. Wdyt?
- scripts/remote-dev-env.sh 0 → 100755
73 # Get project name 74 project=$(oc project -q) 75 76 # Check if deployment exists 77 oc get "deployment/${drupalsite}" &> /dev/null 78 if ! [[ "$?" == "0" ]]; then 79 echo "\"${drupalsite}\" deployment does not exist in project \"${project}\"! Please try again!" 80 exit 81 fi 82 83 # Check if drupalsite exists 84 oc get "drupalsite/${drupalsite}" &> /dev/null 85 if ! [[ "$?" == "0" ]]; then 86 echo "\"${drupalsite}\" drupalsite does not exist in project \"${project}\"! Please try again!" 87 exit 88 fi - Comment on lines +76 to +88
- scripts/remote-dev-env.sh 0 → 100755
64 exit 65 fi 66 67 # Check if user_choice is empty or invalid 68 if [[ -z $user_choice ]] || ! [[ "$user_choice" =~ ^[1-2]+$ ]]; then 69 echo "Invalid choice selection! Exiting..." 70 exit 71 fi 72 73 # Get project name 74 project=$(oc project -q) 75 76 # Check if deployment exists 77 oc get "deployment/${drupalsite}" &> /dev/null 78 if ! [[ "$?" == "0" ]]; then 79 echo "\"${drupalsite}\" deployment does not exist in project \"${project}\"! Please try again!" - scripts/remote-dev-env.sh 0 → 100755
201 # Create or remove remote development environment according to the user's choice 202 case $user_choice in 203 "1") 204 create_remote_dev_env 205 ;; 206 "2") 207 remove_remote_dev_env 208 ;; 209 *) 210 echo $'\nUnexpected choice selection! Exiting...' 211 exit 212 ;; 213 esac 214 } 215 216 main_execution FYI: It's a good practice to have a newline at the end of the file. You can configure your code editor to do this by default
- scripts/remote-dev-env.sh 0 → 100755
105 oc apply -f "${drupalsite}-drupalsite".json 106 107 oc get "deployment/${drupalsite}" -o json \ 108 `# Inject remote-dev-env image into the deployment` \ 109 | jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").image) |= "'"$remote_dev_env_img"'"' \ 110 `# Change commands to ["/bin/bash", "-c"] in the deployment` \ 111 | jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").command) = ["/bin/bash", "-c"]' \ 112 `# Add args as ["/run-php-fpm.sh & code-server /app"] in the deployment` \ 113 | jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").args) = ["/run-php-fpm.sh & code-server /app"]' \ 114 `# Add port config for code-server in the deployment` \ 115 | jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").ports) += [{"containerPort":8888,"name":"code-server","protocol":"TCP"}]' \ 116 `# Remove startupProbe and livenessProbe in the deployment (Otherwise it keeps restarting the pod)` \ 117 | jq 'del(.spec.template.spec.containers[] | select(.name == "php-fpm").startupProbe)' \ 118 | jq 'del(.spec.template.spec.containers[] | select(.name == "php-fpm").livenessProbe)' \ 119 `# Output the deployment JSON` \ 120 | cat > "${drupalsite}-deployment".json - Comment on lines +107 to +120
This is good. But if you prefer a simpler solution, I would recommend using
oc patch
For example:
oc patch --type=merge drupalsite/test -n test -p '{"spec":{"version":{"name": "v9.4-1"}}}'
This command lets you perform complex things as wellRef: https://kubernetes.io/docs/reference/kubectl/cheatsheet/#patching-resources
- scripts/remote-dev-env.sh 0 → 100755
106 107 oc get "deployment/${drupalsite}" -o json \ 108 `# Inject remote-dev-env image into the deployment` \ 109 | jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").image) |= "'"$remote_dev_env_img"'"' \ 110 `# Change commands to ["/bin/bash", "-c"] in the deployment` \ 111 | jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").command) = ["/bin/bash", "-c"]' \ 112 `# Add args as ["/run-php-fpm.sh & code-server /app"] in the deployment` \ 113 | jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").args) = ["/run-php-fpm.sh & code-server /app"]' \ 114 `# Add port config for code-server in the deployment` \ 115 | jq '(.spec.template.spec.containers[] | select(.name == "php-fpm").ports) += [{"containerPort":8888,"name":"code-server","protocol":"TCP"}]' \ 116 `# Remove startupProbe and livenessProbe in the deployment (Otherwise it keeps restarting the pod)` \ 117 | jq 'del(.spec.template.spec.containers[] | select(.name == "php-fpm").startupProbe)' \ 118 | jq 'del(.spec.template.spec.containers[] | select(.name == "php-fpm").livenessProbe)' \ 119 `# Output the deployment JSON` \ 120 | cat > "${drupalsite}-deployment".json 121 Do you also want to validate if the patch is successful? If so, you can check by fetching specific fields using
oc get
&jsonPath
orfieldSelector
# Get all running pods in the namespace kubectl get pods --field-selector=status.phase=Running # Get ExternalIPs of all nodes kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
Check https://kubernetes.io/docs/reference/kubectl/cheatsheet/#viewing-finding-resources
- scripts/remote-dev-env.sh 0 → 100755
32 echo "Please log in to the openshift server or add cluster configuration and try again!" 33 exit 34 fi 35 36 # Check if jq is installed 37 if ! command -v jq &> /dev/null 38 then 39 echo "jq command not found! Please install jq: https://stedolan.github.io/jq/download/" 40 exit 41 fi 42 } 43 44 take_user_input() { 45 # Ask user for oc drupalsite name, gitlab registy image and choice of action 46 read -p 'Enter drupalsite name: ' drupalsite 47 read -p 'Enter remote-dev-env gitlab registry image: ' remote_dev_env_img - Comment on lines +46 to +47
One other quick suggestion, can we pass this inputs as a flag? Like
./remote-dev-env.sh --site test --image gitlab-registry.cern.ch/drupal/paas/cern-drupal-distribution/remote-dev-env:remote-dev-env-589e2aa0
? I am asking this because, we need to give the image name to create & to delete the env. Sometimes we might not have the image name handy. If we make it a flag, we will have command history & also we can add a default value to the flag inside the script. Does it make sense?