From 6acbea615bf5b5ac54772acd6dd564d7f12aabd4 Mon Sep 17 00:00:00 2001 From: Lars Hahn Date: Mon, 22 Jul 2024 20:15:51 +0200 Subject: [PATCH] Initial commit --- LICENSE | 9 ++ README.md | 2 + defaults/main.yml | 8 ++ meta/main.yml | 16 +++ tasks/main.yml | 8 ++ templates/usr/local/bin/cloud-control.j2 | 155 +++++++++++++++++++++++ vars/mail.yml | 2 + 7 files changed, 200 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100755 tasks/main.yml create mode 100755 templates/usr/local/bin/cloud-control.j2 create mode 100644 vars/mail.yml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a6646bd --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 HuehnerCloud + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f675787 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# cloud-control + diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..75ccc42 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,8 @@ +--- +cloud_name: cloud +cloud_env: production +cloud_env_path: "{{ cloud_home }}/{{ cloud_env }}" +cloud_host_group: server +cloud_control_version: 1.0.0 +cloud_control_name: cloud-control +cloud_git_branch_main: main diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..a17deaf --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,16 @@ +--- +galaxy_info: + role_name: control + namespace: hahn-cloud + author: Lars Hahn + company: OpenDevChain + license: MIT + description: Basis role to setup a script handling ansible for cloud control + min_ansible_version: 2.7 + platforms: + - name: Debian + versions: + - 10 + galaxy_tags: + - control +dependencies: [] diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100755 index 0000000..8bd2795 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,8 @@ +--- +- name: setup Cloud Control script + template: + src: "templates{{ cloud_control_path }}/cloud-control.j2" + dest: "{{ cloud_control_path }}/{{ cloud_control_name }}" + owner: root + group: root + mode: 0754 diff --git a/templates/usr/local/bin/cloud-control.j2 b/templates/usr/local/bin/cloud-control.j2 new file mode 100755 index 0000000..6179181 --- /dev/null +++ b/templates/usr/local/bin/cloud-control.j2 @@ -0,0 +1,155 @@ +#!/bin/bash +set -e + + +### VARIABLE ################################################################### +environment_folder={{ cloud_env_path }} +environment="{{ cloud_env }}" +host_type="{{ cloud_host_group }}" +script_name=$(basename $0) +cloud_name="{{ cloud_name }}" +cloud_type="{{ cloud_type }}" +version="{{ cloud_control_version }}" +branch_main="{{ cloud_git_branch_main }}" +################################################################################ + + + +### FUNCTION ################################################################### +to_working_directory() { + if [ ! -d $environment_folder/.git ] || [ ! -d $environment_folder ]; then + echo "Environment '$environment' in '$environment_folder' not available or folder not a git repository! Abort." + exit 1 + fi + cd $environment_folder/$environment +} + +help() { + echo "$script_name, version $version by L.Hahn" + echo "" + echo " $cloud_name script for cloud control" + echo " You can checkout environment (branches), rollout configurations," + echo " run ansible and restore entire configurations." + echo "" + echo "Usage: $script_name [command] [options]" + echo "" + echo "commands:" + echo " - help print this help" + echo " - maintenance setup local server into maintenance mode; no automatic ansible call" + echo " - environment" + echo " download checkout from remote repository" + echo " update load latest remote changes for current branch" + echo " reset stash changes and reset current branch from remote repository with latest changes" + echo " restore checkout latest $branch_main branch from remote repository" + echo " - update get latest roles according to environment requirements.yml" + echo " - play play current loaded ansible playbooks" + echo " - reset perform 1. environment restore, 2. update, 3. execute" + echo "" + echo "" + echo "example:" + echo "~# $script_name environment update" + echo " this will download changes from the currently active remote branch" +} + +environment() { + to_working_directory + current_branch=$(git branch | grep "^\*" | cut -d " " -f 2) + current_upstream=$(git rev-parse $current_branch@{upstream}) + + env_option=$1 + case $env_option in + "update") + echo "### Updating branch '$current_branch' in $environment_folder ###" + git pull + ;; + "reset") + echo "### Resetting branch '$current_branch' in $environment_folder ###" + git reset --hard $current_branch + git pull + ;; + "restore") + echo "### Restoring branch '$branch_main' in $environment_folder ###" + git reset --hard HEAD + git clean -f + git checkout $branch_main + git pull + ;; + "download") + if [ $# -lt 2 ]; then + echo "Missing branch name for environment downloading" + exit 1 + fi + echo "### Stashing branch '$current_branch' & downloading branch '$2' in $environment_folder ###" + git stash + git checkout -b $2 origin/$2 + git pull + ;; + *) + echo "Unknown environments option '$env_option', abort!" + exit 1 + ;; + esac +} + +maintenance() { + to_working_directory + echo "maint" +} + +update() { + to_working_directory + ansible-galaxy install -f -p roles/ -r requirements.yml +} + +play() { + to_working_directory + ansible-playbook $cloud_type"-"$host_type".yml" +} +################################################################################ + + +### MAIN ####################################################################### +if [ $# -eq 0 ]; then + help + exit 1 +fi +script_command=$1 + +case $script_command in + "help") + help + ;; + "maintenance") + maintenance + ;; + "environment") + if [ $# -lt 2 ]; then + echo "ERROR! environment command needs options! None provided." + echo "Call '~# $script_name help' for more information." + exit 1 + fi + environment $2 $3 + ;; + "update") + update + ;; + "play") + play + ;; + "reset") + echo "#=== restore environment ===#" + environment restore + echo "" + echo "#=== update roles ===#" + update + echo "" + echo "#=== play playbook ===#" + play + echo "" + ;; + *) + echo "Unknown command '$script_command', abort!" + echo "Call '~# $script_name help' for more information." + ;; +esac +################################################################################ \ No newline at end of file diff --git a/vars/mail.yml b/vars/mail.yml new file mode 100644 index 0000000..b62dfe7 --- /dev/null +++ b/vars/mail.yml @@ -0,0 +1,2 @@ +--- +cloud_control_path: "/usr/local/bin" \ No newline at end of file