-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtumbleweed
executable file
·368 lines (320 loc) · 8.93 KB
/
tumbleweed
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env bash
VERSION="development"
URL_PRE_MIGRATE="http://download.tumbleweed.boombatower.com"
URL_POST_MIGRATE="http://download.opensuse.org/history"
CONFIG_DIR="/etc/zypp"
VARS_DIR="$CONFIG_DIR/vars.d"
VAR_NAME="snapshotVersion"
VAR_FILE="$VARS_DIR/$VAR_NAME"
SNAPSHOT_HISTORY="$VARS_DIR/.snapshotVersion.history"
REPOS_DIR="/etc/zypp/repos.d"
# REPO_PATTERN="https?://download.opensuse.org/(?:[^u][^/]+/)?tumbleweed/[^$]+"
# Disable matching debug and source URLs for the time being.
REPO_PATTERN="https?://download.opensuse.org/tumbleweed/[^$]+"
tumbleweed_sudo()
{
# Obtain sudo before issuing real commands to avoid half completion.
if ! sudo cat /dev/null ; then
echo "unable to obtain sudo"
exit 1
fi
}
tumbleweed_inited()
{
if [ -f "$VAR_FILE" ] ; then
return 0
fi
return 1
}
tumbleweed_init()
{
if tumbleweed_inited && [ "$force" != "1" ] ; then
echo "already initialized"
return
fi
tumbleweed_sudo
tumbleweed_history_init
tumbleweed_variable "$(tumbleweed_installed)"
tumbleweed_repo_init
}
tumbleweed_variable()
{
if [ $# -eq 1 ] ; then
local version="$1"
echo "$version" | sudo tee "$VAR_FILE" > /dev/null
if [ $? -ne 0 ] ; then
echo "failed to set version to $version in $VARS_DIR/$VAR_NAME"
exit 1
fi
tumbleweed_history "$version"
else
cat "$VAR_FILE"
fi
}
tumbleweed_repo_init()
{
if [ ! -d "$REPOS_DIR/.previous" ] ; then
sudo mkdir "$REPOS_DIR/.previous"
fi
local IFS=$'\n' # Handle repo files with space in name. :(
local files=($(grep -lP "$REPO_PATTERN" "$REPOS_DIR"/*))
local file
for file in ${files[@]} ; do
echo "backup $file"
sudo cp --backup=numbered "$file" "$REPOS_DIR/.previous/$(basename "$file")"
sudo sed -i -r 's|(name=.*)|\1 ($snapshotVersion)|' "$file"
sudo sed -i -r 's|(baseurl=).*tumbleweed/(.*)|\1'$URL'/$snapshotVersion/tumbleweed/\2|' "$file"
done
}
tumbleweed_history_init()
{
if [ ! -f "$SNAPSHOT_HISTORY" ] ; then
# Need an actual line for sed insert to work.
echo | sudo tee "$SNAPSHOT_HISTORY" > /dev/null
fi
}
tumbleweed_history()
{
if [ $# -eq 1 ] ; then
if [ "$(head -n1 "$SNAPSHOT_HISTORY")" != "$1" ] ; then
sudo sed -i "1i$1" "$SNAPSHOT_HISTORY"
fi
else
# Exclude empty lines necessary for data structure.
grep -v -e '^$' "$SNAPSHOT_HISTORY"
fi
}
tumbleweed_history_pop()
{
if [ $# -eq 1 ] ; then
sudo sed -i "1d" "$SNAPSHOT_HISTORY"
else
head -n2 "$SNAPSHOT_HISTORY" | tail -n1
fi
}
tumbleweed_status()
{
echo "latest : $(tumbleweed_latest)"
echo "target : $(tumbleweed_target)"
echo "installed: $(tumbleweed_installed)"
}
tumbleweed_latest()
{
curl --fail --silent -L "$URL/latest"
}
tumbleweed_target()
{
tumbleweed_variable
}
tumbleweed_installed()
{
cat /etc/os-release | grep -oP "VERSION_ID=\"\K(\d+)"
}
tumbleweed_list()
{
curl --fail --silent -L "$URL/list"
}
tumbleweed_update()
{
tumbleweed_switch
install=1
}
tumbleweed_prompt()
{
echo -n "$1 [y/n] (y): "
local response
read response
if [ "$response" != "" ] && [ "$response" != "y" ] ; then
echo "exiting"
exit 1
fi
}
tumbleweed_switch()
{
if [ $# -eq 1 ] ; then
local version="$1"
if ! tumbleweed_list | grep -Fx "$version" > /dev/null ; then
echo "invalid version $version, not in available list"
return
fi
else
local version="$(tumbleweed_latest)"
if [ -z "$version" ] ; then
echo "Unable to determine latest version (likely due to network connection issue)"
exit 1
fi
echo "choosing latest version"
fi
if [ "$version" == "$(tumbleweed_target)" ] && [ "$force" != "1" ] ; then
echo "already on $version"
exit
fi
tumbleweed_prompt "switching from $(tumbleweed_target) to $version?"
tumbleweed_sudo
tumbleweed_variable "$version"
}
tumbleweed_revert()
{
local version="$(tumbleweed_history_pop)"
if [ "$version" == "" ] ; then
echo "no previous version in history"
exit 1
fi
tumbleweed_prompt "switching from $(tumbleweed_target) to $version?"
tumbleweed_sudo
tumbleweed_history_pop "indeed"
tumbleweed_variable "$version"
}
tumbleweed_uninit()
{
if [ ! -d "$REPOS_DIR/.previous" ] ; then
echo "nothing to revert"
exit 1
fi
# Prompt for confirmation.
local repos=($(ls "$REPOS_DIR/.previous"))
tumbleweed_prompt "revert ${#repos[@]} repos?"
tumbleweed_sudo
sudo mv "$REPOS_DIR/.previous"/* $REPOS_DIR/
sudo rm -r "$REPOS_DIR/.previous"
sudo rm "$VAR_FILE"
}
tumbleweed_install()
{
sudo zypper ref
sudo zypper dup
}
tumbleweed_migrate_check()
{
if grep -l "$URL_PRE_MIGRATE" "$REPOS_DIR"/* > /dev/null ; then
MIGRATED=0
URL="$URL_PRE_MIGRATE"
else
MIGRATED=1
URL="$URL_POST_MIGRATE"
fi
# Issue notice of official hosting and migration if target is available.
if [ $MIGRATED -eq 0 ] && \
curl --fail --silent -L "$URL_POST_MIGRATE/list" | grep -Fx "$(tumbleweed_target)" > /dev/null ; then
echo "NOTICE: Official snapshot hosting is now available. The unofficial hosting is" >&2
echo " deprecated and will be discontinued at a future date. Consider" >&2
echo " migrating to the official hosting via \`tumbleweed migrate\`." >&2
fi
}
tumbleweed_migrate()
{
if [ $MIGRATED -eq 1 ] ; then
echo "already migrated, exiting"
exit 1
fi
echo "A backup of repos to be migrated will be kept in $REPOS_DIR/.migrated"
echo "which may be restored by invoking the unmigrate command."
echo
echo "Be aware that the official hosting snapshot count differs from the"
echo "unofficial hosting that you are currently using."
echo
local count_pre=$(curl --fail --silent "$URL_PRE_MIGRATE/list" | wc -l)
local count_post=$(curl --fail --silent -L "$URL_POST_MIGRATE/list" | wc -l)
echo "- unofficial, $URL_PRE_MIGRATE: $count_pre snapshots"
echo "- official, $URL_POST_MIGRATE: $count_post snapshots"
echo
echo "Eventually the unofficial hosting will be phased out."
echo
tumbleweed_prompt "Do you wish to continue?"
tumbleweed_sudo
tumbleweed_repo_migrate
}
tumbleweed_repo_migrate()
{
if [ ! -d "$REPOS_DIR/.migrated" ] ; then
sudo mkdir "$REPOS_DIR/.migrated"
fi
local IFS=$'\n' # Handle repo files with space in name. :(
local files=($(grep -l "\$snapshotVersion" "$REPOS_DIR"/*))
for file in ${files[@]} ; do
sudo cp "$file" "$REPOS_DIR/.migrated"
sudo sed -i -r 's|(baseurl=).*(\$snapshotVersion)/(.*)|\1'$URL_POST_MIGRATE'/\2/tumbleweed/\3|' "$file"
echo "migrated $file"
done
}
tumbleweed_unmigrate()
{
if [ ! -d "$REPOS_DIR/.migrated" ] ; then
echo "nothing to unmigrate"
exit 1
fi
# Prompt for confirmation.
local repos=($(ls "$REPOS_DIR/.migrated"))
tumbleweed_prompt "unmigrate ${#repos[@]} repos?"
tumbleweed_sudo
sudo mv "$REPOS_DIR/.migrated"/* $REPOS_DIR/
sudo rm -r "$REPOS_DIR/.migrated"
}
tumbleweed_usage()
{
cat <<_EOF_
Usage: $0 [options] command [arguments]
Options:
--version Print version string and exit
--force Force on operation to occur regardless of checks.
--install Initiate install after command.
-h, --help Display this message and exit
Commands:
init Initialize repos to point to snapshot repos.
status Show status information (latest, target, and installed).
latest Show latest snapshot available.
installed|version Show current installed snapshot.
target Show the target of the repositories.
list List available snapshots.
history List history of snapshots targetted.
update|upgrade Switch to and install the latest available snapshot.
switch %version Switch to a new snapshot (none for latest).
revert Revert to the previous snapshot or repo state.
uninit Revert back to a snapshotless repository setup.
migrate Migrate from boombatower hosting to download.opensuse.org.
unmigrate Revert migration to official hosting.
_EOF_
}
tumbleweed_handle()
{
case "$1" in
--version) echo "$VERSION" ; exit 0 ; ;;
--force) force=1 ; ;;
--install) install=1 ; ;;
-h|--help) command="usage" ; ;;
history|init|installed|latest|list|revert|status|target|uninit|update|migrate|unmigrate)
command="$1" ; ;;
version)
command="installed" ; ;;
upgrade)
command="update" ; ;;
switch)
command="$1" ; args_expected=1 ;
;;
-*) echo "unknown option $1" ; exit 1 ; ;;
*)
if [ $args_expected -eq 0 ] ; then
return 1
fi
args+=("$1")
((args_expected--))
;;
esac
return 0
}
command="usage"
args_expected=0
args=()
while tumbleweed_handle $1 ; do
shift
done
if [ "$command" != "usage" ] && [ "$command" != "init" ] && ! tumbleweed_inited ; then
echo "repositories have not been initialized for snapshots"
echo " Try $0 init"
exit 1
fi
tumbleweed_migrate_check
tumbleweed_$command ${args[@]}
if [ "$install" == "1" ] ; then
tumbleweed_install
fi