-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdate-vm.sh
More file actions
executable file
·113 lines (97 loc) · 2.88 KB
/
update-vm.sh
File metadata and controls
executable file
·113 lines (97 loc) · 2.88 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
#!/bin/bash
# fail early
set -e -o pipefail
# variables
REPO_ROOT=~/vm-setup
ANSIBLE_VERSION=8.6.0
# exports
export ANSIBLE_FORCE_COLOR=true
check_prerequisites() {
big_step "Checking prerequisites..."
if [[ $(cat /etc/lsb-release | grep 'DISTRIB_RELEASE=22.04') ]]; then
echo "On Ubuntu 22.04, OK to run update"
else
echo "Unsupported operating system, this script may run on Ubuntu 22.04 only!"
cat /etc/lsb-release
exit 1
fi
}
check_ansible() {
big_step "Checking Ansible..."
if [[ $(ansible --version | grep "$ANSIBLE_VERSION") ]]; then
echo "Ansible $ANSIBLE_VERSION already installed"
else
step "Installing Ansible version $ANSIBLE_VERSION"
sudo apt-get update
sudo apt-get install python3-pip -y
# use the -H flag to so pip uses /root/.cache and does not leave root-owned files inside ~/.cache
sudo -H pip3 install ansible==$ANSIBLE_VERSION
fi
}
check_git() {
big_step "Checking Git..."
if [[ $(which git) ]]; then
echo "Git already installed"
else
step "Installing Git"
sudo apt-get update
sudo apt-get install git -y
fi
}
copy_repo_and_symlink_self() {
big_step "Copying repo into the VM..."
if mountpoint -q /vagrant; then
step "Copy /vagrant to $REPO_ROOT"
sudo apt-get install rsync -y
rsync -avh --progress /vagrant/ $REPO_ROOT/ --delete --exclude-from /vagrant/.gitignore
step "Fixing permissions..."
chmod 0755 "$REPO_ROOT/scripts/update-vm.sh"
step "Symlinking 'update-vm' script"
sudo ln -sf $REPO_ROOT/scripts/update-vm.sh /usr/local/bin/update-vm
else
echo "Skipped because /vagrant not mounted"
fi
}
update_repo() {
big_step "Pulling latest changes from git..."
cd $REPO_ROOT
git pull
}
update_vm() {
big_step "Updating the VM via Ansible..."
cd $REPO_ROOT
# append extra vars and role tags if exist
local role_tags=$([[ -n "$ROLE_TAGS" ]] && echo "--tags $ROLE_TAGS" || echo "")
local extra_vars=$([[ -f "site.local.yml" ]] && echo "--extra-vars @site.local.yml" || echo "")
step "trigger the Ansible run with $role_tags and $extra_vars"
/usr/local/bin/ansible-playbook -i "localhost," -c local site.yml -vv $role_tags $extra_vars
}
verify_vm() {
big_step "Verifying the VM..."
cd $REPO_ROOT
step "run ansible linting"
ansible-lint --force-color
step "run integration tests"
py.test --color=yes --junitxml=out/report.xml --html=out/report.html --self-contained-html --spec spec/*.py
}
big_step() {
echo -e "\n=====================================\n>>>>>> $1\n=====================================\n"
}
step() {
echo -e "\n\n>>>>>> $1\n-------------------------------------\n"
}
#
# main flow
#
if [[ "$1" == "--verify-only" ]]; then
copy_repo_and_symlink_self
verify_vm
else
check_prerequisites
check_git
check_ansible
copy_repo_and_symlink_self
[[ "$1" == "--pull" ]] && update_repo
update_vm
[[ "$1" == "--provision-only" ]] || verify_vm
fi