-
Notifications
You must be signed in to change notification settings - Fork 3
/
ok.sh
executable file
·107 lines (86 loc) · 2 KB
/
ok.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh
set -o errexit
set -o nounset
# Check if the script is running on a CI
CI_WORKSPACE=${CI_WORKSPACE:-}
LOCAL_PATH=${CI_WORKSPACE:-/tmp/ok}
PROFILE=${PROFILE:-generic}
REPOSITORY="https://github.com/davidmogar/ok.git"
TAGS=${TAGS:-all}
check_dependencies() {
info "Checking dependencies..."
missing_dependencies=false
command_exists ansible || {
missing_dependencies=true
error "ansible is not installed"
}
command_exists git || {
missing_dependencies=true
error "git is not installed"
}
command_exists sudo || {
missing_dependencies=true
error "sudo is not installed"
}
if [ "$missing_dependencies" = true ]; then
exit 1
fi
}
clone_repository() {
if [ -n "$CI_WORKSPACE" ]; then
warning "Running on a CI workflow. Skipping clonning..."
else
if [ -d ${LOCAL_PATH} ]; then
info "Old installation found. Deleting..."
rm -rf ${LOCAL_PATH}
fi
info "Clonning repository into '${LOCAL_PATH}'..."
mkdir -p $LOCAL_PATH
git clone $REPOSITORY $LOCAL_PATH
fi
}
command_exists() {
command -v "$@" >/dev/null 2>&1
}
error() {
echo ${RED}"Error: $@"${RESET} >&2;
}
info() {
echo ${GREEN}"$@"${RESET}
}
run_playbook() {
info "Running playbook..."
cd $LOCAL_PATH
ansible-playbook \
--ask-become-pass \
--diff \
-i inventory \
--limit $PROFILE \
--tags $TAGS \
playbook.yml
}
setup_colors() {
# Only use colors if connected to a terminal
if [ -t 1 ]; then
GREEN=$(printf '\033[32m')
RED=$(printf '\033[31m')
RESET=$(printf '\033[m')
YELLOW=$(printf '\033[33m')
else
GREEN=""; RED=""; RESET=""; YELLOW=""
fi
}
warning() {
info ${YELLOW}"Warning: $@"${RESET}
}
main() {
if ! [ $(id -u) = 0 ]; then
error "This boostrap script must be executed by root"
exit 1
fi
setup_colors
check_dependencies
clone_repository
run_playbook
}
main "$@"