-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlmm.sh
executable file
·212 lines (164 loc) · 4.45 KB
/
lmm.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/bash
set -e
#
# Global section
#
MAJOR_VERSION=0
MINOR_VERSION=1
CACHE_DIR=".lmm-cache"
LOG_FILE="${CACHE_DIR}/log.txt"
INSTALL_ROLE_FILE=".install.yml"
APP_PATH="$(dirname "$(readlink -f "$0")")"
cd "${APP_PATH}"
mkdir -p "${CACHE_DIR}"
mkdir -p ~/.local/share/lmm
#
# Logs
#
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
NO_COLOR='\033[0m'
log_error() {
echo -e "${RED}$*${NO_COLOR}" | tee -a "${LOG_FILE}"
}
log_warn() {
echo -e "${YELLOW}$*${NO_COLOR}" | tee -a "${LOG_FILE}"
}
log_info() {
echo -e "${GREEN}$*${NO_COLOR}" | tee -a "${LOG_FILE}"
}
case_error() {
echo "$(log_error "Error:") Unsupported param '$(log_warn "$1")' in args line: $2" >&2
exit 1
}
#
# Functions
#
init_ansible() {
if [ -f "${CACHE_DIR}/${MAJOR_VERSION}" ]; then
return
fi
rm -rf "${CACHE_DIR:?}"/* "collections"
#https://docs.ansible.com/ansible/latest/galaxy/user_guide.html#install-multiple-collections-with-a-requirements-file
#current roles don't require extra roles or collections
cat >/tmp/ansible_requirements.yml <<EOF
---
EOF
# shellcheck disable=SC2094
ansible-galaxy role install -fr /tmp/ansible_requirements.yml \
-p ./roles 2>>"${LOG_FILE}" | tee -a "${LOG_FILE}"
# shellcheck disable=SC2094
ansible-galaxy collection install -fr /tmp/ansible_requirements.yml \
-p ./collections/ansible_collections 2>>"${LOG_FILE}" | tee -a "${LOG_FILE}"
date >"${CACHE_DIR}/${MAJOR_VERSION}"
}
check_sudo_session() {
if [ "$EUID" = 0 ]; then
log_error "Don't run lmm as superuser. Please use sudo session. You can start it with 'sudo true' command"
exit 1
fi
if sudo -n true 2>/dev/null; then
log_info "Sudo session is ACTIVE"
else
log_info "Sudo session is disabled"
fi
}
install() {
role=${1:?}
# shellcheck disable=SC2207
shell_vars=($( (cd roles/"${role}"/vars/ && ls *.sh) 2>/dev/null || true))
vars_for_yml=''
for sv in "${shell_vars[@]}"; do
vars_for_yml="${vars_for_yml} ${sv/.sh/}: \"{{ $(cd roles/"${role}"/vars/ && sh "$sv") }}\"\n"
done
prepare_for_sudo=''
if [[ "${role}" == "sudo."* ]]; then
prepare_for_sudo=$(
cat <<EOF
tasks:
- name: Ensure /etc/apt/keyrings exists
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
EOF
)
fi
cat >${INSTALL_ROLE_FILE} <<EOF
---
- name: Install ${role}
hosts: localhost
connection: local
vars:
_user: "{{ ansible_env.USER }}"
_install_dir: "/home/{{ ansible_env.USER }}/.local/share/lmm"
_local_bin: "/home/{{ ansible_env.USER }}/.local/bin"
$(echo -e "$vars_for_yml")
${prepare_for_sudo}
roles:
- role: ${role}
tags: ${role}
EOF
# disable outside roles to prevent names clash
export DEFAULT_ROLES_PATH=''
export ANSIBLE_ROLES_PATH=''
ansible-playbook "${INSTALL_ROLE_FILE}" -t "${role}" 2>>"${LOG_FILE}" | tee -a "${LOG_FILE}"
if [ "${PIPESTATUS[0]}" != '0' ]; then
tail -n 20 "${LOG_FILE}"
log_error "Error occurred. Last 20 lines from log above. ${APP_PATH}/${LOG_FILE}"
log_info "If ROLE requires sudo. You have to run 'sudo true' before calling 'lmm install'"
exit 1
fi
}
test() {
if ! which docker >/dev/null 2>&1; then
log_warn "Tests requires installed docker"
log_warn "Use: lmm install kato.docker"
fi
docker build -t testing-lmm -f ./test/Dockerfile .
docker run -e "TEST_ROLE=$1" --rm testing-lmm
}
#
# MAIN
#
echo " :: Start at $(date)" >>"${LOG_FILE}"
check_sudo_session
init_ansible
case "$1" in
help) # - this help
echo -e "$(grep -E '^[[:space:]]*[[:alnum:][:space:]|-]+\).*$' "$0" | sed -e 's/) # / /')"
;;
version) # - print current version
log_info "${MAJOR_VERSION}.${MINOR_VERSION}"
;;
u | update) # - get new packages and show udpated
git fetch
git rev-list HEAD..origin/master --no-commit-header --format='%C(yellow)%h %C(bold cyan)%B %Creset'
git pull -n
;;
l | list) # - list all available roles
ls -1 ./roles/
;;
s | search) # \033[0;35mSUBSTRING\033[0m - grep through the list
# shellcheck disable=SC2010
ls -1 ./roles/ | grep -i "$2"
;;
i | install) # \033[0;35mROLE_NAME\033[0m - install ansible role from ./roles
if [ "$2" = '' ]; then
log_error "please provide the ROLE_NAME"
exit 1
else
install "$2"
fi
;;
test) # \033[0;35m[ROLE_NAME]\033[0m - install given role in a docker container, otherwise installs all roles
test "$2"
;;
*)
case_error "$1" "$@"
;;
esac