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
8 unresolved threads

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

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
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
  • 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 the Drupalsite 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?

    • Please register or sign in to reply
  • 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
  • 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!"
    • I think, "Please check the inputs" would be a better choice instead of "Please try again" since, the operator will always ensure the Deployment and Service exist as long as the Drupalsite is present.

      Same when for L86 and L93

    • Please register or sign in to reply
  • 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
  • 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
  • 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
  • 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?

    • Please register or sign in to reply
  • Overall besides my comments, I tested the script & works perfectly :ok_hand: Goodjob Subhashis!

  • Sadly abandoned, closing.

  • Please register or sign in to reply
    Loading