-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup
executable file
·184 lines (151 loc) · 4.62 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
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
#!/usr/bin/env bash
set -euxo pipefail
export ANSIBLE_FORCE_COLOR=true
do_setup() {
bootstrap
clone_dotfiles
run_ansible "$1"
if [[ $(uname) == "Linux" ]]; then
ssh_save
fi
finish
}
# Preparing the system, updating packages, installing deps to run the rest.
bootstrap() {
if [[ $(uname) == "Darwin" ]]; then
echo "[setup] Warning! MacOS setup is currently only tested on Seqoia 15.1"
echo "[setup] Starting to set up homebrew"
set +ex
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
eval "$(/opt/homebrew/bin/brew shellenv)"
brew analytics off
brew install git curl poetry libyaml
rustup default stable
fi
if [[ $(uname) == "Linux" ]]; then
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 libyaml-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev liblzma-dev zlib1g-dev libbz2-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
fi
}
clone_dotfiles() {
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
}
run_ansible() {
./asdf/.bin/asdf_bootstrap.sh
# shellcheck disable=SC1091
. "$HOME/.asdf/asdf.sh"
asdf shell python 3.13.0
# From here on, everything should work the same on MacOS and Ubuntu
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_debian.yml
ansible/playbooks/base_macos.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 "Ok, not logging you out."
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 "$@"
}
all=null
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"