From 4268b587fec3eb3ed06d9b87e3d332ee57138c35 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 31 Mar 2019 21:28:49 +0200 Subject: [PATCH] Can register runner several times --- actions.json | 49 +++++++++++++++++ manifest.json | 10 ++-- scripts/_common.sh | 80 +++++++++++++++++++++++++-- scripts/actions/register | 114 +++++++++++++++++++++++++++++++++++++++ scripts/install | 49 +++++++++++++---- scripts/restore | 34 ++++++++---- 6 files changed, 306 insertions(+), 30 deletions(-) create mode 100644 actions.json create mode 100644 scripts/actions/register diff --git a/actions.json b/actions.json new file mode 100644 index 0000000..6161b8b --- /dev/null +++ b/actions.json @@ -0,0 +1,49 @@ +[ + { + "id": "register", + "name": "Register", + "command": "/bin/bash scripts/actions/register", + "user": "root", + "accepted_return_codes": [ + 0 + ], + "description": { + "en": "Add this runner to a new repo/GitLab instance.", + "fr": "Ajoute ce runner à un nouveau·elle dépôt/instance GitLab." + }, + "arguments": [ + { + "name": "gitlab_url", + "type": "string", + "ask": { + "en": "Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com)", + "fr": "Veuillez entrer l'URL du coordinateur gitlab-ci (par ex. https://gitlab.com)" + }, + "example": "https://gitlab.com/" + }, + { + "name": "token", + "type": "password", + "ask": { + "en": "Please enter the gitlab-ci token for this runner", + "fr": "Veuillez entrer le jeton gitlab-ci pour ce runner" + }, + "help": { + "en": "This token can be retrived at this location: Gitlab Project->Settings->Runner", + "fr": "Ce token peut être récupéré à cet emplacement : Projet Gitlab->Settings->Runner" + }, + "example": "xxx" + }, + { + "name": "docker_image", + "type": "string", + "ask": { + "en": "Please enter the Docker image", + "fr": "Veuillez entrer l'image du Docker" + }, + "example": "ruby:2.1", + "default": "alpine:latest" + } + ] + } +] \ No newline at end of file diff --git a/manifest.json b/manifest.json index ae3d81a..72698eb 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "GitLab Runner is a continuous integration tool to use with a GitLab instance (YNH or not).", "fr": "GitLab Runner est un outil d'intégration continue à utiliser avec une instance GitLab (YNH ou non)." }, - "version": "11.9.0~ynh1", + "version": "11.9.0~ynh2", "url": "https://gitlab.com/gitlab-org/gitlab-runner", "license": "MIT", "maintainer": { @@ -25,7 +25,7 @@ "name": "gitlab_url", "type": "string", "ask": { - "en": "Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )", + "en": "Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com)", "fr": "Veuillez entrer l'URL du coordinateur gitlab-ci (par ex. https://gitlab.com)" }, "example": "https://gitlab.com/" @@ -47,10 +47,10 @@ "name": "docker_image", "type": "string", "ask": { - "en": "Please enter the Docker image (eg. ruby:2.1)", - "fr": "Veuillez entrer l'image du Docker (ex. ruby:2.1)" + "en": "Please enter the Docker image", + "fr": "Veuillez entrer l'image du Docker" }, - "example": "alpine:latest", + "example": "ruby:2.1", "default": "alpine:latest" } ] diff --git a/scripts/_common.sh b/scripts/_common.sh index c3e851a..61d9b23 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -11,13 +11,87 @@ pkg_dependencies="ca-certificates git curl tar docker-ce" # PERSONAL HELPERS #================================================= +# Restore a previous backup if the action process failed +# +# usage: +# ynh_backup_before_action +# ynh_clean_setup () { +# ynh_restore_actionbackup +# } +# ynh_abort_if_errors +ynh_restore_actionbackup () { + echo "Action failed." >&2 + local app_bck=${app//_/-} # Replace all '_' by '-' + + NO_BACKUP_ACTION=${NO_BACKUP_ACTION:-0} + + if [ $NO_BACKUP_ACTION -eq "0" ] + then + # Check if an existing backup can be found before removing and restoring the application. + if sudo yunohost backup list | grep -q $app_bck-pre-action$backup_number + then + # Remove the application then restore it + sudo yunohost app remove $app + # Restore the backup + sudo yunohost backup restore $app_bck-pre-action$backup_number --apps $app --force --debug + ynh_die --message="The app was restored to the way it was before the failed action." + fi + else + echo "\$NO_BACKUP_ACTION is set, that means there's no backup to restore. You have to fix this action by yourself !" >&2 + fi +} + +# Make a backup in case of failed action +# +# usage: +# ynh_backup_before_action +# ynh_clean_setup () { +# ynh_restore_actionbackup +# } +# ynh_abort_if_errors +ynh_backup_before_action () { + if [ ! -e "/etc/yunohost/apps/$app/scripts/backup" ] + then + echo "This app doesn't have any backup script." >&2 + return + fi + backup_number=1 + local old_backup_number=2 + local app_bck=${app//_/-} # Replace all '_' by '-' + NO_BACKUP_ACTION=${NO_BACKUP_ACTION:-0} + + if [ $NO_BACKUP_ACTION -eq "0" ] + then + # Check if a backup already exists with the prefix 1 + if sudo yunohost backup list | grep -q $app_bck-pre-action1 + then + # Prefix becomes 2 to preserve the previous backup + backup_number=2 + old_backup_number=1 + fi + + # Create backup + sudo BACKUP_CORE_ONLY=1 yunohost backup create --apps $app --name $app_bck-pre-action$backup_number --debug + if [ "$?" -eq 0 ] + then + # If the backup succeeded, remove the previous backup + if sudo yunohost backup list | grep -q $app_bck-pre-action$old_backup_number + then + # Remove the previous backup only if it exists + sudo yunohost backup delete $app_bck-pre-action$old_backup_number > /dev/null + fi + else + ynh_die --message="Backup failed, the action process was aborted." + fi + else + echo "\$NO_BACKUP_ACTION is set, backup will be avoided. Be careful, this action is going to be operated without a security backup" + fi +} + #================================================= # EXPERIMENTAL HELPERS #================================================= - -#!/bin/bash - # Pin a repository. # # usage: ynh_pin_repo --package=packages --pin=pin_filter --priority=priority_value [--name=name] [--append] diff --git a/scripts/actions/register b/scripts/actions/register new file mode 100644 index 0000000..d3b52fc --- /dev/null +++ b/scripts/actions/register @@ -0,0 +1,114 @@ +#!/bin/bash + +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source scripts/_common.sh +source /usr/share/yunohost/helpers + +#================================================= +# RETRIEVE ARGUMENTS +#================================================= + +new_gitlab_url=${YNH_ACTION_GITLAB_URL} +new_token=${YNH_ACTION_TOKEN} +new_docker_image=${YNH_ACTION_DOCKER_IMAGE} + +app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} + +gitlab_url=$(ynh_app_setting_get $app gitlab_url) +token=$(ynh_app_setting_get $app token) +docker_image=$(ynh_app_setting_get $app docker_image) +executor=$(ynh_app_setting_get $app executor) +architecture=$(ynh_app_setting_get $app architecture) + +#================================================= +# CHECK IF ARGUMENTS ARE CORRECT +#================================================= + +# Adding a comma at the end +if [ ${new_gitlab_url: -1} != "," ]; then + new_gitlab_url=${new_gitlab_url}, +fi + +# Adding a comma at the end +if [ ${new_token: -1} != "," ]; then + new_token=${new_token}, +fi + +# Adding a comma at the end +if [ ${new_docker_image: -1} != "," ]; then + new_docker_image=${new_docker_image}, +fi + +#================================================= +# BACKUP BEFORE ACTION THEN ACTIVE TRAP +#================================================= +ynh_print_info "Backing up the app before action (may take a while)..." + +# Backup the current version of the app +ynh_backup_before_action +ynh_clean_setup () { + # restore it if the action fails + ynh_restore_actionbackup +} +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# CHECK IF AN ACTION HAS TO BE DONE +#================================================= + + +#================================================= +# SPECIFIC ACTION +#================================================= +# REGISTER THE RUNNER +#================================================= +ynh_print_info "Register Gitlab Runner..." + +# Can be registered several time, to do this give a list of gitlab_url, token and docker_image separated by a comma. +split_char="," + +nb_to_register=$(echo "${new_gitlab_url}" | awk -F"${split_char}" '{print NF-1}') + +for i in $(seq $nb_to_register) +do + url=$(echo $new_gitlab_url | cut -d$split_char -f$i) + tok=$(echo $new_token | cut -d$split_char -f$i) + docker_img=$(echo $new_docker_image | cut -d$split_char -f$i) + + # Register the runner + ynh_exec_warn_less $app register \ + --non-interactive \ + --url "$url" \ + --registration-token "$tok" \ + --executor "$executor" \ + --docker-image "$docker_img" \ + --description "Yunohost runner" \ + --tag-list "$executor,$architecture" \ + --run-untagged \ + --locked="false" + + gitlab_url=${gitlab_url}${url}, + token=${token}${tok}, + docker_image=${docker_image}${docker_img}, +done + +#================================================= +# STORE SETTINGS +#================================================= +ynh_print_info "Storing settings..." + +ynh_app_setting_set $app gitlab_url $gitlab_url +ynh_app_setting_set $app token $token +ynh_app_setting_set $app docker_image $docker_image + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_print_info "Action of $app completed" diff --git a/scripts/install b/scripts/install index 457e69d..c039cdf 100755 --- a/scripts/install +++ b/scripts/install @@ -53,6 +53,21 @@ fi # By default, the runner will use docker to run your builds. PR are welcomes to implement more executors executor="docker" +# Adding a comma at the end +if [ ${gitlab_url: -1} != "," ]; then + gitlab_url=${gitlab_url}, +fi + +# Adding a comma at the end +if [ ${token: -1} != "," ]; then + token=${token}, +fi + +# Adding a comma at the end +if [ ${docker_image: -1} != "," ]; then + docker_image=${docker_image}, +fi + #================================================= # STORE SETTINGS FROM MANIFEST #================================================= @@ -116,17 +131,29 @@ dpkg -i $tempdir/$gitlab_runner_filename #================================================= ynh_print_info "Configuring Gitlab Runner..." -# Register the runner -ynh_exec_warn_less $app register \ - --non-interactive \ - --url "$gitlab_url" \ - --registration-token "$token" \ - --executor "$executor" \ - --docker-image "$docker_image" \ - --description "Yunohost runner" \ - --tag-list "$executor,$architecture" \ - --run-untagged \ - --locked="false" \ +# Can be registered several time, to do this give a list of gitlab_url, token and docker_image separated by a comma. +split_char="," + +nb_to_register=$(echo "${gitlab_url}" | awk -F"${split_char}" '{print NF-1}') + +for i in $(seq $nb_to_register) +do + url=$(echo $gitlab_url | cut -d$split_char -f$i) + tok=$(echo $token | cut -d$split_char -f$i) + docker_img=$(echo $docker_image | cut -d$split_char -f$i) + + # Register the runner + ynh_exec_warn_less $app register \ + --non-interactive \ + --url "$url" \ + --registration-token "$tok" \ + --executor "$executor" \ + --docker-image "$docker_img" \ + --description "Yunohost runner" \ + --tag-list "$executor,$architecture" \ + --run-untagged \ + --locked="false" +done #================================================= # STORE THE CONFIG FILE CHECKSUM diff --git a/scripts/restore b/scripts/restore index 4e03e15..b42b306 100755 --- a/scripts/restore +++ b/scripts/restore @@ -95,17 +95,29 @@ dpkg -i $tempdir/$gitlab_runner_filename #================================================= ynh_print_info "Configuring Gitlab Runner..." -# Register the runner -ynh_exec_warn_less $app register \ - --non-interactive \ - --url "$gitlab_url" \ - --registration-token "$token" \ - --executor "$executor" \ - --docker-image "$docker_image" \ - --description "Yunohost runner" \ - --tag-list "$executor,$architecture" \ - --run-untagged \ - --locked="false" \ +# Can be registered several time, to do this give a list of gitlab_url, token and docker_image separated by a comma. +split_char="," + +nb_to_register=$(echo "${gitlab_url}" | awk -F"${split_char}" '{print NF-1}') + +for i in $(seq $nb_to_register) +do + url=$(echo $gitlab_url | cut -d$split_char -f$i) + tok=$(echo $token | cut -d$split_char -f$i) + docker_img=$(echo $docker_image | cut -d$split_char -f$i) + + # Register the runner + ynh_exec_warn_less $app register \ + --non-interactive \ + --url "$url" \ + --registration-token "$tok" \ + --executor "$executor" \ + --docker-image "$docker_img" \ + --description "Yunohost runner" \ + --tag-list "$executor,$architecture" \ + --run-untagged \ + --locked="false" +done #================================================= # ADVERTISE SERVICE IN ADMIN PANEL