-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbootstrap.sh
executable file
·135 lines (121 loc) · 4.35 KB
/
bootstrap.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
#!/bin/bash
# bootstrap.sh
# Bootstrap script for training-vm based systems
# - reads local settings
# - installs prerequisites: git, ansible
# - clones collection repo into collection subdir (with submodules)
# - creates/links Ansible configuration local.yml
# $1 = settings file (default: ./bootstrap_setup or ~/bootstrap_setup)
if [ "$(whoami)" == "root" ]; then
echo "This script must be run by a regular user (with sudo privileges)."
exit 1
fi
# Associative arrays to configure developer mode
declare -A REPO BRANCH
# Read configuration file "$1", ./bootstrap_setup or ~/bootstrap_setup
echo -n "Reading configuration"
if [ "$1" ]; then
if [ -e "$1" ]; then
source "$1"
echo " from $1"
else
echo " [ERROR: file $1 not found]"
exit 1
fi
elif [ -e ./bootstrap_setup ]; then
source ./bootstrap_setup
echo " from ./bootstrap_setup"
elif [ -e ~/bootstrap_setup ]; then
source ~/bootstrap_setup
echo " from ~/bootstrap_setup"
fi
# Override these defaults in your 'bootstrap_setup' file
# Switch submodule $COLLECTION/xyz to direct checkout by setting
# REPO[xyz] (and BRANCH[xyz]
COLLECTION=${COLLECTION:-"training"}
SLUGFILE=${SLUGFILE:-"/etc/epics-training"}
COLLECTION_REPO=${COLLECTION_REPO:-"https://github.com/epics-training/training-collection"}
# Install prerequisites: Git, Ansible
if ! command -v git >/dev/null; then
packages="git"
fi
if ! command -v ansible >/dev/null; then
packages="${packages} ansible"
fi
if [ "${packages}" ]; then
echo "Installing prerequisites: ${packages}..."
sudo dnf update
sudo dnf install -y ${packages}
else
echo "All prerequisites are installed."
fi
# if SLUG is not set, read it from SLUGFILE or ask for it
if [ ! "${SLUG}" ]; then
if [ ! -e "${SLUGFILE}" ]; then
slugs=$(git ls-remote --heads ${COLLECTION_REPO} | awk -F / '{ print "\\t" $3 "\\n" }')
echo "Using collection repo at ${COLLECTION_REPO}"
echo "Please specify the slug (short name) of the event"
echo "that you want to configure this machine for."
echo "Valid strings (branches in the collection repo) are:"
echo -en $slugs
echo "Leaving the slug empty will use the default branch with all available submodules."
echo "For development, you can switch submodules to use direct checkouts"
echo "using settings in the 'bootstrap_setup' file."
echo "The slug will be written to ${SLUGFILE}."
read -p "Event name []: " SLUG
TMP_FILE=$(mktemp -q /tmp/epics.XXXXXX)
echo "$SLUG" > $TMP_FILE
sudo cp $TMP_FILE ${SLUGFILE}
sudo chmod 644 ${SLUGFILE}
else
SLUG=$(<${SLUGFILE})
fi
fi
# Clone the collection
if [ ! -e "${COLLECTION}" ]; then
branch=""
if [ "$SLUG" ]; then
branch="-b $SLUG"
fi
echo "Cloning collection (for $SLUG) into ./${COLLECTION}..."
git clone $branch ${COLLECTION_REPO} ${COLLECTION}
else
echo "Update the existing VM configuration by running 'git pull' in ./${COLLECTION_DIR}."
echo "Switch the setup with 'git checkout \$(<${SLUGFILE})' in ./${COLLECTION_DIR}."
fi
cd ${COLLECTION}
modules=$(git submodule | awk '{ print $2 }')
echo "Adding submodules into ${COLLECTION} ..."
for mod in ${modules}; do
repo="${REPO[${mod}]}"
branch="${BRANCH[${mod}]}"
if [ "$repo" ]; then
if [ "$branch" ]; then
extra="-b $branch"
fi
echo "Removing submodule $mod ..."
git submodule deinit -f $mod
rm -rf .git/modules/$mod $mod
git rm -f $mod
echo "Cloning $mod from $repo ..."
git clone $extra $repo $mod
else
git submodule update --init $mod
fi
done
# Point out missing local.yml configuration
if [ ! -e "vm-setup/ansible/group_vars/local.yml" ]; then
if [ ! -e "local.yml" ]; then
echo "No local configuration found. Creating one from local.yml.sample"
cp "vm-setup/ansible/group_vars/local.yml.sample" "local.yml"
fi
ln -s "../../../local.yml" "vm-setup/ansible/group_vars/local.yml"
else
echo -n "Verify your existing local configuration"
if [ -h "vm-setup/ansible/group_vars/local.yml" ]; then
echo " in ${COLLECTION}/local.yml"
else
echo " in ${COLLECTION}/vm-setup/ansible/group_vars/local.yml"
fi
fi
echo "Run ${COLLECTION}/vm-setup/update.sh at any time to finish or update your installation."