-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup
executable file
·157 lines (128 loc) · 3.79 KB
/
setup
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
#!/usr/bin/env bash
set -euxo pipefail
export ANSIBLE_FORCE_COLOR=true
do_setup() {
bootstrap "$1"
ssh_save
finish
}
bootstrap()
{
if [[ "$OSTYPE" != "linux-gnu" ]]; then
echo "[setup] You're not on Linux, sorry =( This script only works on Linux for now"
exit 1
fi
print_message "[setup] Ensure prerequisites are installed"
if is_ci; then
# Make sure we don't get any readline prompts on CI
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
fi
run_with_sudo apt-get update -qq
run_with_sudo apt-get install software-properties-common git wget curl python3-apt python3-pip python3-venv python-is-python3 libdbus-glib-1-dev -qq
print_message "[setup] Upgrading and autocleaning packages..."
run_with_sudo apt-get update -qq
run_with_sudo apt-get upgrade -qq --allow-downgrades --allow-remove-essential --allow-change-held-packages
run_with_sudo apt-get autoremove -y -qq
if [ -d "$HOME/.dotfiles" ]; then
print_message "[setup] Dotfiles exist. Updating dotfiles..."
cd "$HOME/.dotfiles" && git stash && git checkout main -q && git pull -q && (git stash pop || true)
elif [ "${TESTING-false}" == "true" ]; then
print_message "[setup] We're in our testing container. Hello!"
print_message "[setup] We're about to test the code that has been mapped to the container."
cd /code || exit 1
cp -r /code "$HOME/.dotfiles"
else
print_message "[setup] Cloning dotfiles..."
git clone -q https://github.com/phansch/dotfiles.git "$HOME/.dotfiles" && cd "$HOME/.dotfiles" || exit
fi
print_message "[setup] Version info"
python3 -V
pip3 -V
print_message "[setup] Install poetry..."
curl -sSL https://install.python-poetry.org | python3 -
poetry="$HOME/.local/bin/poetry"
$poetry env info
print_message "[setup] Setting up venv and deps w/ poetry..."
$poetry install
$poetry run ansible-galaxy install -r ansible/requirements.yml --force
default_playbooks=(
ansible/playbooks/base.yml
ansible/playbooks/dotfiles.yml
)
print_message "[setup] Running default playbooks..."
$poetry run ansible-playbook "${default_playbooks[@]}" --ask-become-pass -u "$USER"
additional_playbooks=(
ansible/playbooks/additional_packages.yml
ansible/playbooks/backup.yml
# ansible/playbooks/ruby.yml
)
cmd="ansible-playbook ${additional_playbooks[*]} --ask-become-pass -u $USER"
if [ "$1" = "true" ] ; then
print_message "[setup] Running all additional playbooks"
$poetry run ansible-playbook "${additional_playbooks[@]}" --ask-become-pass -u "$USER"
else
print_message "[setup] To run the rest of the playbooks:"
print_message "$cmd"
fi
}
ssh_save()
{
if is_ci; then return; fi
ask_for_confirmation "[setup] Do you want to add your ssh key to ssh-agent? "
if answer_is_yes; then
ssh-add "$HOME/.ssh/id_rsa" &>/dev/null
fi
}
finish()
{
if is_ci; then return; fi
ask_for_confirmation "[setup] Almost done. Do you want to re-login now to complete the setup? "
if answer_is_yes; then
xfce4-session-logout --logout
else
echo "Well, then track it now!"
fi
}
is_ci() {
[[ "${CI-false}" == "true" ]] \
&& return 0 \
|| return 1
}
answer_is_yes() {
[[ "$REPLY" =~ ^[Yy]$ ]] \
&& return 0 \
|| return 1
}
ask_for_confirmation() {
printf "%b" "$1 (y/n)"
read -r -n 1
printf "%b" "\n"
}
print_message() {
printf "%b" "$1\n"
}
# This is a wrapper around sudo for docker
run_with_sudo() {
my_sudo=""
if [[ $(id -u) -ne 0 ]] ; then
print_message "We are not root, using 'sudo'"
my_sudo='sudo'
fi
$my_sudo "$@"
}
while getopts ":as" opt; do
case "$opt" in
a)
all=true
;;
s)
all=false
;;
\?)
echo >&2 \
"usage: $0 [-a] [-s]"
exit 1;;
esac
done
shift $((OPTIND-1))
do_setup "$all"