-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupgrade.sh
More file actions
executable file
·306 lines (222 loc) · 8.93 KB
/
Copy pathupgrade.sh
File metadata and controls
executable file
·306 lines (222 loc) · 8.93 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
set -Eeo pipefail
if [[ "$(dirname "$(realpath -s "${0}")")" != "${PWD}" ]]; then
echo 'Unable to run script out of its parent directory.'
exit 1
fi
source src/commons/error.sh
source src/commons/logger.sh
source src/commons/validators.sh
# Syncs the package databases.
sync_package_databases () {
log INFO 'Syncing the package databases...'
sudo pacman -Syy 2>&1 ||
abort ERROR 'Failed to synchronize package databases.'
log INFO 'Package databases have been synced.'
}
# Installs or upgrades packages from the official repositories.
install_packages () {
log INFO 'Installing official packages...'
local pkgs=()
pkgs+=($(grep -E '(stp|all):pac' packages.x86_64 | cut -d ':' -f 3)) ||
abort ERROR 'Failed to read packages from packages.x86_64 file.'
# Set yes as default to prompt on replacing conflicted packages
local yes=4
sudo pacman -S --needed --noconfirm --ask ${yes} ${pkgs[@]} 2>&1 ||
abort ERROR 'Failed to install official packages.'
log INFO 'Official packages have been installed.'
}
# Installs or upgrades packages from the AUR repositories.
install_aur_packages () {
log INFO 'Installing AUR packages...'
local pkgs=()
pkgs+=($(grep -E '(stp|all):aur' packages.x86_64 | cut -d ':' -f 3)) ||
abort ERROR 'Failed to read packages from packages.x86_64 file.'
# Set yes as default to prompt on replacing conflicted packages
local yes=4
yay -S --needed --noconfirm --removemake --ask ${yes} ${pkgs[@]} 2>&1 ||
abort ERROR 'Failed to install AUR packages.'
log INFO 'AUR packages have been installed.'
}
# Installs or upgrades packages from third party source repositories.
install_source_packages () {
local install_smenu
install_smenu () {
log INFO 'Installing smenu package...'
local previous_dir=${PWD}
git clone https://github.com/p-gen/smenu.git /tmp/smenu 2>&1 ||
abort ERROR 'Failed to clone smenu git repository.'
cd /tmp/smenu
./build.sh 2>&1 ||
abort ERROR 'Failed to build smenu package.'
sudo make install 2>&1 ||
abort ERROR 'Failed to install smenu package.'
cd ${previous_dir} && rm -rf /tmp/smenu
log INFO 'Package smenu has been installed.'
}
log "Installing source packages..."
install_smenu
log "Source packages have been installed."
}
# Fixes global configuration variables.
fix_config_values () {
local branch=''
branch="$(git branch --show-current)" ||
abort ERROR 'Failed to read the current branch.'
local commit_date=''
commit_date="$(git log -1 --format='%at' | jq -cer 'strftime("%Y-%m-%d")')" ||
abort ERROR 'Failed to read the commit date'
local commit=''
commit="$(git log --pretty=format:'%H' -n 1)" ||
abort ERROR 'Failed to read the last commit id.'
local version="${commit_date} ${branch} ${commit:0:5}"
sed -i "s;#VERSION#;${version};" airootfs/etc/os-release ||
abort ERROR 'Failed to set release version.'
log INFO "New release version set to ${version}."
sed -i 's/#TERMINAL#/alacritty/' airootfs/home/user/.stackrc ||
abort ERROR 'Failed to set default terminal.'
log INFO 'Default terminal set to alacritty.'
sed -i 's/#EDITOR#/helix/' airootfs/home/user/.stackrc ||
abort ERROR 'Failed to set default editor.'
log INFO 'Default editor set to helix.'
local user_id=''
user_id="$(id -u "${USER}" 2>&1)" ||
abort ERROR 'Failed to get the user id.'
sed -i "s/#USER_ID#/${user_id}/g" airootfs/etc/systemd/system/lock@.service ||
abort ERROR 'Failed to set the user id in lock service.'
log INFO "Lock user id set to ${user_id} for user ${USER}."
sed -i "s;#HOME#;/home/${USER};g" \
airootfs/home/user/.config/systemd/user/fix-layout.service ||
abort ERROR 'Failed to set the home in fix layout service.'
log INFO "Fix layout service home set to /home/${USER}."
}
# Fixes up the stack logs directories and permissions.
fix_logs_home () {
sudo mkdir -p \
/var/log/stack \
/var/log/stack/tools \
/var/log/stack/bars &&
sudo chmod -R 775 /var/log/stack/tools &&
sudo chmod -R 775 /var/log/stack/bars &&
sudo chown -R :stack /var/log/stack ||
abort ERROR 'Failed to set /var/log/stack directories.'
log INFO 'Log directories /var/log/stack have been set.'
}
# Updates the root file system.
update_root_files () {
log INFO 'Updating the root file system...'
# Rename airootfs user home to align with the current system
if not_equals "${USER}" 'user'; then
mv airootfs/home/user "airootfs/home/${USER}" ||
abort ERROR "Failed to rename user home for ${USER}."
fi
# Restore root privileged file system
sudo chown -R root:root airootfs/etc airootfs/usr src/commons src/tools
sudo rsync -av airootfs/ / \
--exclude usr/local/bin/stack \
--exclude etc/X11/xorg.conf.d/00-keyboard.conf \
--exclude etc/hostname \
--exclude etc/hosts \
--exclude etc/vconsole.conf \
--exclude "home/${USER}/.config/gtk-3.0" \
--exclude "home/${USER}/.config/stack" ||
abort ERROR 'Failed to update the root file system.'
log INFO 'Root file system has been updated.'
}
# Updates the commons script files.
update_commons () {
log INFO 'Updating the commons files...'
sudo mkdir -p /opt/stack ||
abort ERROR 'Failed to create the /opt/stack folder.'
sudo rsync -av --delete src/commons/ /opt/stack/commons ||
abort ERROR 'Failed to update the commons files.'
sudo sed -i 's;source src;source /opt/stack;' /opt/stack/commons/* ||
abort ERROR 'Failed to fix source paths to /opt/stack.'
log INFO 'Source paths fixed to /opt/stack.'
log INFO 'Commons files have been updated.'
}
# Updates the tools script files.
update_tools () {
log INFO 'Updating the tools files...'
sudo mkdir -p /opt/stack ||
abort ERROR 'Failed to create the /opt/stack folder.'
sudo rsync -av --delete src/tools/ /opt/stack/tools ||
abort ERROR 'Failed to update the tools files.'
sudo sed -i 's;source src;source /opt/stack;' /opt/stack/tools/**/* ||
abort ERROR 'Failed to fix source paths to /opt/stack.'
log INFO 'Source paths fixed to /opt/stack.'
# Create and restore all symlinks for every tool
local symlink_home='/usr/local/stack'
sudo rm -rf "${symlink_home}" &&
sudo mkdir "${symlink_home}" ||
abort ERROR "Failed to create the ${symlink_home} folder."
local main_files
main_files=($(find /opt/stack/tools -type f -name 'main.sh')) ||
abort ERROR 'Failed to get the list of main script file paths.'
local main_file=''
for main_file in "${main_files[@]}"; do
# Extrack the tool handle name
local tool_name=''
tool_name="$(echo "${main_file}" | sed 's;/opt/stack/tools/\(.*\)/main.sh;\1;')" ||
abort ERROR 'Failed to extract tool handle name.'
sudo ln -sf "${main_file}" "${symlink_home}/${tool_name}" ||
abort ERROR "Failed to create symlink for ${main_file} file."
done
log INFO 'Tools symlinks have been restored.'
log INFO 'Tools files have been updated.'
}
# Fixes the root bash configuration and environment.
fix_root_bash () {
log INFO 'Fixing root bash shell environment...'
sudo cp "/home/${USER}/.prompt" /root ||
abort ERROR 'Failed to copy root .prompt file.'
log INFO 'Bash shell environment has been fixed.'
}
# Fixes user and system services.
fix_services () {
log INFO 'Fixing user and system services...'
sudo systemctl disable lock@${USER}.service 2>&1 ||
log WARN 'Unable to disable lock service.'
sudo systemctl enable lock@${USER}.service 2>&1 ||
log WARN 'Unable to enable lock service.'
log INFO 'Lock service has been fixed.'
log INFO 'Services have been fixed.'
}
# Restores the user permissions under the home directory.
restore_user_permissions () {
chown -R ${USER}:${USER} "/home/${USER}" ||
abort ERROR 'Failed to restore user permissions.'
log INFO "Permissions under /home/${user} restored."
sudo chown -R root:root /tmp/.X11-unix ||
abort ERROR 'Failed to restore xorg temp files permissions.'
log INFO 'Xorg temp files permissions have been restored.'
}
# Updates the stack hash file.
update_hash_file () {
local branch=''
branch="$(git branch --show-current)" ||
abort ERROR 'Failed to read the current branch.'
local commit=''
commit="$(git log --pretty=format:'%H' -n 1)" ||
abort ERROR 'Failed to read the last commit id.'
echo "{\"branch\": \"${branch}\", \"commit\": \"${commit}\"}" | jq . |
sudo tee /opt/stack/.hash &> /dev/null ||
abort ERROR 'Failed to update the stack hash file.'
log INFO "Stack hash file updated to ${branch} [${commit:0:5}]."
}
log INFO 'Starting the upgrade process...'
sync_package_databases &&
install_packages &&
install_aur_packages &&
install_source_packages &&
fix_config_values &&
fix_logs_home &&
update_root_files &&
update_commons &&
update_tools &&
fix_root_bash &&
fix_services &&
restore_user_permissions &&
update_hash_file
log INFO 'Upgrade process has been completed.'
log INFO 'Please reboot your system!'