152 lines
3.3 KiB
Bash
152 lines
3.3 KiB
Bash
#!/bin/bash
|
|
|
|
ALLOWED_ACTIONS='help create start stop delete'
|
|
CONFIGURATION_FILE='./configuration/script.properties'
|
|
|
|
action=''
|
|
runner_name=''
|
|
|
|
gitea_instance_url=''
|
|
gitea_runner_registration_token=''
|
|
|
|
function error() {
|
|
local error_message=$1
|
|
local should_exit=$2
|
|
echo "$error_message" >&2
|
|
echo >&2
|
|
|
|
if [[ $should_exit -eq 1 ]]
|
|
then
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
function print_help() {
|
|
cat << EOF
|
|
Usage: bash $0 ACTION [PARAMETERS]...
|
|
Handle gitea actions runners.
|
|
|
|
This helper aims to make easier to create, start, stop and delete gitea actions runners which run in docker containers.
|
|
|
|
Here are the possible actions:
|
|
- create Creates a new runner.
|
|
- start Starts an existing runner.
|
|
- stop Stops an existing runner.
|
|
- delete Deletes an existing runner.
|
|
|
|
Some actions need parameters.
|
|
Parameters of action "create":
|
|
- \$1 The name of the runner to create.
|
|
|
|
Parameters of action "start":
|
|
- \$1 The name of the runner to start.
|
|
|
|
Parameters of action "stop":
|
|
- \$1 The name of the runner to stop.
|
|
|
|
Parameters of action "delete":
|
|
- \$1 The name of the runner to delete.
|
|
|
|
EOF
|
|
}
|
|
|
|
function is_action_allowed() {
|
|
local arg_action=$1
|
|
local action_is_allowed=1
|
|
|
|
echo "$ALLOWED_ACTIONS" | grep "$arg_action"
|
|
}
|
|
|
|
function handle_args() {
|
|
local arg_action=$1
|
|
local arg_parameter_1=$2
|
|
|
|
if [[ $# -ne 2 ]]
|
|
then
|
|
error 'Bad arguments...' 1
|
|
print_help
|
|
fi
|
|
|
|
local action_is_allowed=$(is_action_allowed "$arg_action")
|
|
echo "actions_is_allowed='$action_is_allowed'"
|
|
if [[ -z $action_is_allowed ]]
|
|
then
|
|
error "Unknown action '$arg_action'..."
|
|
print_help
|
|
|
|
exit 1
|
|
fi
|
|
|
|
action=$arg_action
|
|
runner_name=$arg_parameter_1
|
|
}
|
|
|
|
function get_property() {
|
|
local key=$1
|
|
local value
|
|
|
|
value=$(grep "^${key}=" "$CONFIGURATION_FILE" | cut -d '=' -f 2-)
|
|
|
|
echo $value
|
|
}
|
|
|
|
function check_configuration() {
|
|
if [[ ! -f $CONFIGURATION_FILE ]]; then
|
|
error 'Configuration file not found!' 1
|
|
fi
|
|
|
|
gitea_instance_url=$(get_property 'gitea_instance_url')
|
|
if [[ -z "$gitea_instance_url" ]]
|
|
then
|
|
error 'Gitea instance url should be set into properties file' 1
|
|
fi
|
|
|
|
gitea_runner_registration_token=$(get_property 'gitea_runner_registration_token')
|
|
if [[ -z "$gitea_runner_registration_token" ]]
|
|
then
|
|
error 'Gitea runner registration token should be set into properties file' 1
|
|
fi
|
|
}
|
|
|
|
function create_new_runner() {
|
|
echo "Creation of a new runner named '$runner_name'..."
|
|
|
|
docker run \
|
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
|
-e GITEA_INSTANCE_URL=$gitea_instance_url \
|
|
-e GITEA_RUNNER_REGISTRATION_TOKEN=$gitea_runner_registration_token \
|
|
-e GITEA_RUNNER_NAME=$runner_name \
|
|
--name $runner_name \
|
|
-d docker.io/gitea/act_runner:latest
|
|
}
|
|
|
|
function start_runner() {
|
|
echo "Start runner '$runner_name'..."
|
|
docker start $runner_name
|
|
}
|
|
|
|
function main() {
|
|
handle_args "$@"
|
|
|
|
check_configuration
|
|
|
|
case $action in
|
|
'create')
|
|
create_new_runner
|
|
;;
|
|
|
|
'start')
|
|
start_runner
|
|
;;
|
|
|
|
'stop')
|
|
stop_runner
|
|
;;
|
|
|
|
'delete')
|
|
delete_runner
|
|
;;
|
|
esac
|
|
}
|
|
|
|
main "$@" |