Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Warning

You have tried to access an archived page. Please go to the new https://root360.atlassian.net/wiki/spaces/KB to find more documents.

...

The Deploy system supports hooks that are started before or after deployment.
These hooks are executable files of any kind (e.g. shell scripts, Linux executable binaries).

Hooks are not executed when a deployment process is triggered by automatic processes such as scaling out an instance of a autoscaling group because of high load. They are only executed through calling r3 deploy.

Table of contents

Table of Contents
excludeTable of contents

General usage instructions

...

Step-by-step guide

Show help context

...

Show help example
linenumbers
Code Block
true
languagecollapsetexttrue
~$ r3 deploy -h
Deploy customer project. Needs to be run with sudo.

optional arguments:
  -h, --help            show this help message and exit
  -d                    run in verbose mode (use twice to be more verbose)
  -q                    run in quiet mode (only errors are shown)
  -p PROJECT, --project PROJECT
                        The necessary name of the project to work on
  -r ROLE, --role ROLE  Server role to work on
  --pre-hook [PREHOOKDIR]
                        run pre-deploy hook scripts in default path "pre-
                        hook.d/" or given directory (optional)
  --post-hook [POSTHOOKDIR]
                        run post-deploy hook scripts in default path "post-
                        hook.d/" or given directory (optional)
  -n                    skip update of source code (optional)
  -m                    skip distribution of source code (optional)
  --rev REV             specify tag/branch/revision to clone (git deployment
                        only!)
  --list-roles          list deployable roles

How to use hooks

...

...

Deployment pre-hook example command line
linenumbers
Code Block
true
languagecollapsetexttrue
r3 deploy --pre-hook [directory] 


The --pre-hook option enables the hooks to be started before deploying and sets the directory with pre-hooks. If  If no explicit path is specified, the path pre-hook.d is used in the project directory (e.g. git-root).

The --post-hook option enables the hooks to be started after deployment and sets the directory with post hooks. If  If no explicit path is specified, the path post-hook.d is used in the project directory (eg git-root).

Hooks are valid only under the following conditions:

  • The executable flag of the file of the hook is set. The flag must already be set in your deployment before r3 deploy copies it to us.

    • If you are using Windows and git, use "git add --chmod=+x 00_somehook.sh" to set the flag.

  • The filename starts with a two-digit number (00-99) followed by a _ (underscore), e.g. 00_firsthook, 10_otherhook.sh

The hooks will run in the context of the project user on the natgw/jump server/bastion host. You can use any command in the hook that you can also use manually.

The hooks are called with the following options:

  • -p project: project name

  • -r role: server group name (e.g. web)

  • -e environment: environment name (e.g. test)

Example Hook:

Code Block
languagebashlinenumberstrue
#!/bin/bash
 
source /usr/local/lib/helper.sh
 
function usage() {
  echo "${SCRIPT} [-r role] [-p project] [-e environment] [-q|-d] [-h]"
  echo "  -h                  : print this help"
  echo "  -r role             : server role"
  echo "  -p project          : project name"
  echo "  -e environment      : environment name"
  exit 1
}
 
# iterate options
while getopts ':hr:p:e:' opt; do
  case "${opt}" in
    "r")
      role="${OPTARG}"
      ;;
    "p")
      project="${OPTARG}"
      ;;
    "e")
      environment="${OPTARG}"
      ;;
    "h")
      usage
      ;;
    ":")
      log error "Missing argument for option ${OPTARG}"
      usage
      ;;
    *)
      log error "Unknown option ${opt}"
      usage
      ;;
  esac
done
 
# print given project, environment and role
echo "${project}-${environment}-${role}"
 
# iterate all servers with given role in current project and run a command
for target in $(get-instances-by-role "${role}" --output text | awk -F ' ' '{print$2}' | grep -v 'ip'); do
  # connect to server and run cache.php
  if ssh "${target}" php /var/www/cache.php; then
    # if command run successfully on one server skip all other
    break
  fi
done
 
# do some more stuff

...