-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfigure
executable file
·96 lines (76 loc) · 2.73 KB
/
configure
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
#!/usr/bin/bash
set -o errexit -o pipefail
# Source configuration file
config_file="$(dirname "${BASH_SOURCE[0]}")/configuration.bash"
if [ ! -r "$config_file" ]; then
echo "Could not read configuration file '$config_file'."
echo "Copy one from '${config_file}.example'."
exit 1
fi
. "$config_file"
usage() {
cat <<EOF
$0 [CHEF_REPO CHEF_ENVIRONMENT CHEF_SOLO_FILE]
Configure the current host using chef-solo.
CHEF_REPO the url of the chef configuration repository.
CHEF_ENVIRONMENT the chef environment for this host.
CHEF_SOLO_FILE the node template for this host, relative to the CHEF_REPO root.
Arguments may be specified in order as above or as environment variables in any order:
env CHEF_REPO='[email protected]:ros-infrastructure/chef-ros-buildfarm' CHEF_ENVIRONMENT=staging CHEF_SOLO_FILE='solo/jenkins.json' $0
When first run, all arguments must be specified or pre-populated in '$config_file'.
Subsequent runs will use argument values saved in '$config_file'.
EOF
}
save_configuration() {
cat <<CONFIGURATION > "$config_file"
CHEF_VERSION=$CHEF_VERSION
CHEF_REPO=$CHEF_REPO
CHEF_BRANCH=$CHEF_BRANCH
CHEF_ENVIRONMENT=$CHEF_ENVIRONMENT
CHEF_SOLO_FILE=$CHEF_SOLO_FILE
CHEF_MAGE_PROFILE=$CHEF_MAGE_PROFILE
CONFIGURATION
}
freshen_chef_repo() {
if [ ! -d chef_repo ]; then
git clone "$CHEF_REPO" -b "$CHEF_BRANCH" --recurse-submodules --remote-submodules chef_repo
else
pushd chef_repo
git fetch origin
git reset --hard origin/$CHEF_BRANCH
git submodule update --init --remote --recursive
popd
fi
}
decrypt_secrets() {
mage="${wd}/mage"
MAGE_KEY=$MAGE_KEY "${mage}/mage.sh" -c "${mage}/mage.yml" -P "$CHEF_MAGE_PROFILE" -d
}
# Fresh CLI args will override configured values
CHEF_REPO="${1:-$CHEF_REPO}"
CHEF_BRANCH="${2:-$CHEF_BRANCH}"
CHEF_ENVIRONMENT="${3:-$CHEF_ENVIRONMENT}"
CHEF_SOLO_FILE="${4:-$CHEF_SOLO_FILE}"
CHEF_MAGE_PROFILE="${5:-$CHEF_MAGE_PROFILE}"
# Check for valid configuration values or command line arguments
if test $# -ne 4 && test -z "$CHEF_REPO" || test -z "$CHEF_BRANCH" || test -z "$CHEF_ENVIRONMENT" || test -z "$CHEF_SOLO_FILE"; then
usage
exit 1
fi
save_configuration
freshen_chef_repo
pushd chef_repo
wd="$(pwd)"
if test \( -n "$CHEF_MAGE_PROFILE" -a -z "$MAGE_KEY" \) -o \( -z "$CHEF_MAGE_PROFILE" -a -n "$MAGE_KEY" \); then
echo "CHEF_MAGE_PROFILE or MAGE_KEY is not set, and are necessary to perform decryption."
exit 1
fi
if test -n "$CHEF_MAGE_PROFILE" && test -n "$MAGE_KEY" ; then
decrypt_secrets
fi
cinc-solo -c "${wd}/.chef/solo.rb" -E "$CHEF_ENVIRONMENT" -j "${wd}/${CHEF_SOLO_FILE}"
# Write date + commit SHA to a changelog
echo "$( date "+%Y-%m-%d %H:%M:%S"): $(git rev-parse --short HEAD)" >> "../CHEF_COMMITLOG.txt";
popd
# Remove chef repository after configuration
rm -r -f chef_repo