-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubuntu-server-setup
executable file
·176 lines (129 loc) · 4.69 KB
/
ubuntu-server-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
#!/usr/bin/env bash
# Init
source $(dirname $0)/_functions
if [ -z "$1" ]; then
fail "Usage: $(basename $0) hostname"
fi
rootHost="root@$1"
statePath=/root/.local/state/setup
userName=rafal
userHost=$userName@$1
home=/home/$userName
###########
# As root #
###########
# Ensuring state file
step-header "State"
ssh $rootHost mkdir -p $(dirname $statePath) &&
ssh $rootHost touch $statePath ||
fail "State init failed"
# Pre-configuration
step-header "Color prompt"
ssh $rootHost sed -i 's/#force_color_prompt/force_color_prompt/' /root/.bashrc ||
fail "Forcing color prompt failed"
# Installation
if ! ssh $rootHost grep -q update $statePath; then
step-header "Update"
ssh $rootHost apt update ||
fail "Update failed"
# -t because we might need to answer a prompt about newer versions of configuration
# files. I didn't find a way to assume one of the anwers.
ssh -t $rootHost apt upgrade -y ||
fail "Upgrade failed"
ssh $rootHost "echo update >> $statePath" ||
fail "Saving state failed"
ssh $rootHost reboot
step-header "Rebooting"
while ! ssh $rootHost echo "Reboot successful!"; do sleep 2; done
fi
step-header "Packages"
ssh $rootHost apt install -y \
btop \
ripgrep \
s3fs \
zsh ||
fail "Packages installation failed"
if ssh $rootHost ! which docker; then
ssh $rootHost "curl -fsSL https://get.docker.com | sh" ||
fail "Docker installation failed"
fi
step-header "Delta"
if ! ssh $rootHost which delta; then
deltaUrl=$(
curl -o- https://api.github.com/repos/dandavison/delta/releases |
grep 'browser_download_url.*/git-delta_.*_amd64' |
perl -pe 's#^.*?browser_download_url.*(https://.*?/git-delta_.*_amd64.deb).*#$1#' |
head -n1
) &&
ssh $rootHost wget -O /tmp/delta.deb $deltaUrl &&
ssh $rootHost dpkg -i /tmp/delta.deb ||
fail "Delta installation error"
fi
# Configuration
step-header "User"
if ! ssh $rootHost grep -q "^$userName:" /etc/passwd; then
step-subheader "User: creating"
ssh -t $rootHost adduser $userName &&
ssh $rootHost usermod -aG sudo $userName ||
fail "User creation failed"
fi
step-subheader "Ensuring SSH directory"
ssh $rootHost mkdir -p $home/.ssh ||
fail "SSH directory creation failed"
if ssh $rootHost "[ ! -f $home/.ssh/authorized_keys ]"; then
step-subheader "Authorized keys"
ssh $rootHost cp /root/.ssh/authorized_keys $home/.ssh &&
ssh $rootHost chown -R $userName:$userName $home/.ssh ||
fail "Copying authorized keys failed"
fi
step-subheader "Setting shell"
ssh $rootHost chsh -s "$(which zsh)" $userName ||
fail "User configuration failed"
step-header "Firewall"
ssh $rootHost ufw allow OpenSSH &&
ssh $rootHost ufw allow https &&
ssh $rootHost ufw allow http &&
ssh $rootHost ufw allow 3478/tcp &&
ssh $rootHost ufw allow 3478/udp &&
ssh $rootHost ufw --force enable ||
fail "Firewall config failed"
step-header "Swap file"
if [ ! -f /swapfile ]; then
ssh $rootHost fallocate -l 2G /swapfile &&
ssh $rootHost chmod 600 /swapfile &&
ssh $rootHost mkswap /swapfile &&
ssh $rootHost swapon /swapfile &&
ssh $rootHost "echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab" ||
fail "Swap file setup failed"
fi
###########
# As user #
###########
step-header "ZSH"
if ssh $userHost "[ ! -d $HOME/.oh-my-zsh ]"; then
ssh $userHost \
'sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended' ||
fail 'OMZ error'
fi
if ssh $userHost "[ ! -d $HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions ]"; then
ssh $userHost git clone https://github.com/zsh-users/zsh-autosuggestions \
$home/.oh-my-zsh/custom/plugins/zsh-autosuggestions ||
fail 'ZSH autosuggestions error'
fi
if ssh $userHost "[ ! -d $HOME/.oh-my-zsh/custom/plugins/command-time ]"; then
ssh $userHost git clone https://github.com/popstas/zsh-command-time.git \
$HOME/.oh-my-zsh/custom/plugins/command-time ||
fail 'ZSH command time error'
fi
if ssh $userHost "[ ! -d $HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting ]"; then
ssh $userHost git clone https://github.com/zsh-users/zsh-syntax-highlighting \
$HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting ||
fail 'ZSH syntax highlighting error'
fi
# FZF must be after OMZ because it backs up the .zshrc file.
step-header 'FZF'
if ssh $userHost "[ ! -d $HOME/.fzf ]"; then
ssh $userHost git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf &&
ssh $userHost $HOME/.fzf/install --key-bindings --completion --update-rc ||
fail 'FZF error'
fi