forked from telmerobert/DocRepository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sh
More file actions
331 lines (293 loc) · 8.99 KB
/
Copy pathinit.sh
File metadata and controls
331 lines (293 loc) · 8.99 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/bin/bash
# Tru-Strap: prepare an instance for a Puppet run
export HOME="/root"
main() {
parse_args "$@"
setup_rhel7_repo
install_yum_deps
install_ruby
set_gemsources "$@"
install_gem_deps
clone_git_repo
symlink_puppet_dir
inject_eyaml_keys
fetch_puppet_modules
run_puppet
}
# Parse the commmand line arguments
parse_args() {
while [[ -n "${1}" ]] ; do
case "${1}" in
--help|-h)
print_help
exit
;;
--version|-v)
print_version "${PROGNAME}" "${VERSION}"
exit
;;
--role|-r)
set_facter init_role "${2}"
shift
;;
--environment|-e)
set_facter init_env "${2}"
shift
;;
--repouser|-u)
set_facter init_repouser "${2}"
shift
;;
--reponame|-n)
set_facter init_reponame "${2}"
shift
;;
--moduleshttpcache|-c)
set_facter init_moduleshttpcache "${2}"
shift
;;
--passwd|-p)
PASSWD="${2}"
shift
;;
--gemsources)
shift
;;
--debug)
shift
;;
*)
echo "Unknown argument: ${1}"
print_help
exit
;;
esac
shift
done
usagemessage="Error, USAGE: $(basename "${0}") --role|-r --environment|-e --repouser|-u --reponame|-n --repoprivkeyfile|-k [--repobranch|-b] [--repodir|-d] [--eyamlpubkeyfile|-j] [--eyamlprivkeyfile|-m] [--gemsources|-s] [--help|-h] [--version|-v]"
# Define required parameters.
if [[ -z "${FACTER_init_role}" || -z "${FACTER_init_env}" || -z "${FACTER_init_repouser}" || -z "${FACTER_init_reponame}" ]]; then
echo "${usagemessage}"
exit 1
fi
# Set some defaults if they aren't given on the command line.
[[ -z "${FACTER_init_repobranch}" ]] && set_facter init_repobranch master
[[ -z "${FACTER_init_repodir}" ]] && set_facter init_repodir /opt/"${FACTER_init_reponame}"
}
# Install yum packages if they're not already installed
yum_install() {
PACKAGE_LIST=""
for i in "$@"
do
yum --noplugins list installed "${i}" > /dev/null 2>&1
if [[ $? == 0 ]]; then
echo "${i} is already installed"
else
PACKAGE_LIST="${PACKAGE_LIST} ${i}"
fi
done
if [[ -n "${PACKAGE_LIST}" ]]; then
yum install -y $(echo "${PACKAGE_LIST}" | xargs)
fi
}
# Install Ruby gems if they're not already installed
gem_install() {
for i in "$@"
do
gem list --local $(echo "${i}" | cut -d ':' -f 1) | grep $(echo "${i}" | cut -d ':' -f 1) > /dev/null 2>&1
if [[ $? == 0 ]]; then
echo "${i} is already installed"
else
GEM_LIST="${GEM_LIST} ${i}"
fi
done
if [[ -n "$GEM_LIST" ]]; then
gem install $(echo "$GEM_LIST" | xargs) --no-ri --no-rdoc
fi
}
print_version() {
echo "${1}" "${2}"
}
print_help() {
echo Heeelp.
}
# Set custom facter facts
set_facter() {
export FACTER_$1="${2}"
if [[ ! -d /etc/facter ]]; then
mkdir -p /etc/facter/facts.d
fi
echo "${1}=${2}" > /etc/facter/facts.d/"${1}".txt
chmod -R 600 /etc/facter
cat /etc/facter/facts.d/"${1}".txt
}
setup_rhel7_repo() {
yum_install redhat-lsb-core
dist=$(lsb_release -is)
majorversion=$(lsb_release -rs | cut -f1 -d.)
if [[ "$majorversion" == "7" ]] && [[ "$dist" == "RedHatEnterpriseServer" ]]; then
echo "RedHat Enterprise version 7- adding extra repo for *-devel"
yum_install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-optional
yum_install ruby-devel
fi
}
install_ruby() {
majorversion=$(lsb_release -rs | cut -f1 -d.)
if [[ "$majorversion" == "6" ]]; then
echo "Linux Major Version 6"
ruby -v > /dev/null 2>&1
if [[ $? -ne 0 ]] || [[ $(ruby -v | awk '{print $2}' | cut -d '.' -f 1) -lt 2 ]]; then
yum remove -y ruby-*
https://s3-eu-west-1.amazonaws.com/bt-wlms-file-repo/ruby-2.1.5-2.el6.x86_64.rpm
fi
elif [[ "$majorversion" == "7" ]]; then
echo "Linux Major version 7"
yum_install ruby
fi
}
# Set custom gem sources
set_gemsources() {
GEM_SOURCES=
tmp_sources=false
for i in $@; do
if [[ "${tmp_sources}" == "true" ]]; then
GEM_SOURCES="${i}"
break
tmp_sources=false
fi
if [[ "${i}" == "--gemsources" ]]; then
tmp_sources=true
fi
done
if [[ ! -z "${GEM_SOURCES}" ]]; then
echo "Re-configuring gem sources"
# Remove the old sources
OLD_GEM_SOURCES=$(gem sources --list | tail -n+3 | tr '\n' ' ')
for i in $OLD_GEM_SOURCES; do
gem sources -r "$i"
done
# Add the replacement sources
OIFS=$IFS && IFS=','
for i in $GEM_SOURCES; do
MAX_RETRIES=5
export attempts=1
exit_code=1
while [[ $exit_code -ne 0 ]] && [[ $attempts -le ${MAX_RETRIES} ]]; do
gem sources -a $i
exit_code=$?
if [[ $exit_code -ne 0 ]]; then
sleep_time=$((attempts * 10))
echo Sleeping for ${sleep_time}s before retrying ${attempts}/${MAX_RETRIES}
sleep ${sleep_time}s
attempts=$((attempts + 1))
fi
done
done
IFS=$OIFS
fi
}
# Install the yum dependencies
install_yum_deps() {
echo "Installing required yum packages"
yum_install augeas-devel ncurses-devel gcc gcc-c++ curl git redhat-lsb-core
}
# Install the gem dependencies
install_gem_deps() {
echo "Installing required gems"
gem_install puppet:3.7.4 hiera facter ruby-augeas hiera-eyaml ruby-shadow
}
# Clone the git repo
clone_git_repo() {
# Clone private repo.
echo "Cloning ${FACTER_init_repouser}/${FACTER_init_reponame} repo"
rm -rf "${FACTER_init_repodir}"
git clone -b "${FACTER_init_repobranch}" git@github.com:"${FACTER_init_repouser}"/"${FACTER_init_reponame}".git "${FACTER_init_repodir}"
# Exit if the clone fails
if [[ ! -d "${FACTER_init_repodir}" ]]; then
echo "Failed to clone git@github.com:${FACTER_init_repouser}/${FACTER_init_reponame}.git" && exit 1
fi
}
# Symlink the cloned git repo to the usual location for Puppet to run
symlink_puppet_dir() {
# Link /etc/puppet to our private repo.
PUPPET_DIR="${FACTER_init_repodir}/puppet"
rm -rf /etc/puppet > /dev/null 2>&1
rm /etc/hiera.yaml > /dev/null 2>&1
ln -s "${PUPPET_DIR}" /etc/puppet
ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
}
run_librarian() {
echo -n "Installing librarian-puppet"
gem install activesupport -v 4.2.5 --no-ri --no-rdoc
gem install librarian-puppet --no-ri --no-rdoc
echo -n "Installing Puppet modules"
librarian-puppet install --verbose
librarian-puppet show
}
# Fetch the Puppet modules via the moduleshttpcache or librarian-puppet
fetch_puppet_modules() {
ENV_BASE_PUPPETFILE="${FACTER_init_env}/Puppetfile.base"
ENV_ROLE_PUPPETFILE="${FACTER_init_env}/Puppetfile.${FACTER_init_role}"
BASE_PUPPETFILE=Puppetfile.base
ROLE_PUPPETFILE=Puppetfile."${FACTER_init_role}"
if [[ -f "/etc/puppet/Puppetfiles/${ENV_BASE_PUPPETFILE}" ]]; then
BASE_PUPPETFILE="${ENV_BASE_PUPPETFILE}"
fi
if [[ -f "/etc/puppet/Puppetfiles/${ENV_ROLE_PUPPETFILE}" ]]; then
ROLE_PUPPETFILE="${ENV_ROLE_PUPPETFILE}"
fi
PUPPETFILE=/etc/puppet/Puppetfile
rm -f "${PUPPETFILE}" ; cat /etc/puppet/Puppetfiles/"${BASE_PUPPETFILE}" /etc/puppet/Puppetfiles/"${ROLE_PUPPETFILE}" > "${PUPPETFILE}"
PUPPETFILE_MD5SUM=$(md5sum "${PUPPETFILE}" | cut -d " " -f 1)
if [[ ! -z $PASSWD ]]; then
MODULE_ARCH=${FACTER_init_role}."${PUPPETFILE_MD5SUM}".tar.gz.aes
else
MODULE_ARCH=${FACTER_init_role}."${PUPPETFILE_MD5SUM}".tar.gz
fi
cd "${PUPPET_DIR}" || exit
if [[ ! -z "${FACTER_init_moduleshttpcache}" && "200" == $(curl "${FACTER_init_moduleshttpcache}"/"${MODULE_ARCH}" --head --silent | head -n 1 | cut -d ' ' -f 2) ]]; then
echo -n "Downloading pre-packed Puppet modules from cache..."
if [[ ! -z $PASSWD ]]; then
echo "================="
echo "Using Encrypted modules ${FACTER_init_moduleshttpcache}/$MODULE_ARCH "
echo "================="
curl --silent -o modules.tar.gz.aes ${FACTER_init_moduleshttpcache}/$MODULE_ARCH
openssl enc -base64 -aes-128-cbc -d -salt -in modules.tar.gz.aes -out modules.tar.gz -k $PASSWD
else
curl --silent -o modules.tar.gz ${FACTER_init_moduleshttpcache}/$MODULE_ARCH
fi
tar tf modules.tar.gz &> /dev/null
TEST_TAR=$?
if [[ $TEST_TAR -eq 0 ]]; then
tar zxpf modules.tar.gz
echo "================="
echo "Unpacked modules:"
find ./modules -maxdepth 1 -type d | cut -d '/' -f 3
echo "================="
else
echo "Seems we failed to decrypt archive file... running librarian-puppet instead"
run_librarian
fi
else
run_librarian
fi
}
# Execute the Puppet run
run_puppet() {
export LC_ALL=en_GB.utf8
echo ""
echo "Running puppet apply"
puppet apply /etc/puppet/manifests/site.pp
PUPPET_EXIT=$?
echo ""
echo "Top 10 slowest Puppet resources"
echo "==============================="
PERFORMANCE_DATA=( $(grep evaluation_time /var/lib/puppet/reports/*/*.yaml | awk '{print $3}' | sort -n | tail -10 ) )
for i in ${PERFORMANCE_DATA[*]}; do
echo -n "${i}s - "
echo "$(grep -B 3 "$i" /var/lib/puppet/reports/*/*.yaml | head -1 | awk '{print $2 $3}' )"
done | tac
exit $PUPPET_EXIT
}
main "$@"