Browse code

Initial commit

Script for scheduled mirror updates with aptly[1].

[1] https://github.com/smira/aptly.git

Dmitrii Kashin authored on 06/10/2014 23:31:17
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,128 @@
0
+#!/bin/sh
1
+
2
+# Main dependencies: aptly, grep, awk, sed, date
3
+# Debug dependencies: time
4
+
5
+# This shell script was written in order to help you to update your mirrors
6
+# every day via crontab. All you need is to name your mirrors in the appropriate
7
+# format: '$release/$component';
8
+
9
+# For example, do this:
10
+# % aptly mirror create sid/main http://mirror.yandex.ru/debian/ sid main
11
+# % aptly mirror create sid/contrib http://mirror.yandex.ru/debian/ sid contrib
12
+
13
+# When you run this script it does:
14
+# 1) update all your mirrors
15
+# 2) *drop* *all* your publications and snapshots
16
+# 3) create new snapshots and publish them
17
+
18
+# It's originally written by Dmitrii Kashin <freehck@freehck.ru>, and is
19
+# distributed under GNU GPLv2[1]. Fell free to improve it.
20
+
21
+set -e
22
+
23
+usage () {
24
+    cat <<EOF
25
+repo-update.sh [-d] [-g <gpg-key-passphrase>] [-l <log-file>]
26
+  
27
+-d	Activates debug mode.
28
+-g	Set a passphrase for your gpg key (it can be useful if you run script
29
+        with cron.
30
+-l	File to store log. Type '-' for output to screen.
31
+EOF
32
+}
33
+
34
+# Defaults:
35
+SCRIPT="$0"
36
+LOGFILE="$HOME/.aptly/repo-update.log"
37
+SHELL=/bin/sh
38
+PROMPT="%"
39
+DATE=`date +%F`
40
+SUFFIX="$DATE"
41
+GPG_PASSWD=""
42
+
43
+while getopts hdg:l: option
44
+do
45
+    case "$option" in
46
+        d) DEBUG=1;;
47
+        g) GPG_PASSWD="$OPTARG";;
48
+	l) LOGFILE="$OPTARG";;
49
+	h) usage; exit 0;;
50
+	*) usage; exit 1;;
51
+    esac
52
+done
53
+
54
+if [ "x$LOGFILE" = "x-" ]
55
+then
56
+    TO_LOGFILE=""
57
+else 
58
+    TO_LOGFILE=">>$LOGFILE"
59
+fi
60
+
61
+if [ $DEBUG -eq 1 ]
62
+then
63
+    WITH_TIMER=time
64
+else
65
+    WITH_TIMER=
66
+fi
67
+
68
+# Additional functions
69
+
70
+logger () {
71
+    STR=$1
72
+    CMD=$(echo "echo \"$STR\" $TO_LOGFILE" | sed 's/\$/\\\$/g' | sed "s/$GPG_PASSWD/\*\*\*/g")
73
+    echo $CMD | $SHELL
74
+}
75
+
76
+# update all mirrors
77
+cmd_update_mirrors () {
78
+    CMD_MIRRORS_UPDATE="for mirror in \$(aptly mirror list -raw); do aptly mirror update \$mirror $TO_LOGFILE; done"
79
+    #CMD_MIRRORS_UPDATE="aptly mirror list -raw | xargs -n 1 -r aptly mirror update $TO_LOGFILE"
80
+    logger "$PROMPT $CMD_MIRRORS_UPDATE"
81
+    echo "$CMD_MIRRORS_UPDATE" | $WITH_TIMER $SHELL; echo
82
+}
83
+
84
+# Create snapshots for each mirror with the same name and a new SUFFIX
85
+cmd_snapshots_create () {
86
+    CMD_SNAPSHOTS_CREATE="for mirror in \$(aptly mirror list -raw); do aptly snapshot create \$mirror-$DATE from mirror \$mirror $TO_LOGFILE; done"
87
+    #CMD_SNAPSHOTS_CREATE="aptly mirror list -raw | xargs -n 1 -I{} aptly snapshot create {}-\"$SUFFIX\" from mirror {} $TO_LOGFILE"
88
+    logger "$PROMPT $CMD_SNAPSHOTS_CREATE"
89
+    echo "$CMD_SNAPSHOTS_CREATE" | $WITH_TIMER $SHELL; echo
90
+}
91
+
92
+# Drop all publications made previous time
93
+cmd_publications_drop () {
94
+    # TODO: learn with author whether it's normal with the dot in the first field; I suppose it's a bug
95
+    CMD_PUBLICATIONS_DROP="for publication in \$(aptly publish list -raw | awk '{print \$2}'); do aptly publish drop \$publication $TO_LOGFILE; done"
96
+    #CMD_PUBLICATIONS_DROP="aptly publish list -raw | awk '{print \$2}' | xargs -n 1 -r aptly publish drop $TO_LOGFILE"
97
+    logger "$PROMPT $CMD_PUBLICATIONS_DROP"
98
+    echo "$CMD_PUBLICATIONS_DROP" | $WITH_TIMER $SHELL; echo
99
+}
100
+
101
+# Drop all snapshots made previous time
102
+cmd_snapshots_drop () {
103
+    CMD_SNAPSHOTS_DROP="for snapshot in \$(aptly snapshot list -raw); do aptly snapshot drop \$snapshot $TO_LOGFILE; done"
104
+    #CMD_SNAPSHOTS_DROP="aptly snapshot list -raw | xargs -n 1 -r aptly snapshot drop $TO_LOGFILE"
105
+    logger "$PROMPT $CMD_SNAPSHOTS_DROP"
106
+    echo "$CMD_SNAPSHOTS_DROP" | $WITH_TIMER $SHELL; echo
107
+}
108
+
109
+# Publish & sign all the mirrors
110
+cmd_publish () {
111
+    # Run berserk, spreading fear and pain!
112
+    CMD_PUBLISH=$(aptly mirror list -raw | awk -F/ "{targets[\$1]=targets[\$1]\" \"\$2;} END{for (tar in targets) print tar, targets[tar];}" | awk "{release=\$1; comp=snapshots=\"\"; for(i=2; i<=NF; i++) {(comp!=\"\") ? comp=comp\",\"\$i : comp=\$i; snapshots=snapshots\" \"release\"/\"\$i\"-$SUFFIX\"}; print \"aptly publish snapshot -distribution=\"release\" -component=\"comp\" -passphrase=$GPG_PASSWD \"snapshots\" $TO_LOGFILE\"; }")
113
+    logger "$PROMPT $CMD_PUBLISH"
114
+    echo "$CMD_PUBLISH" | $WITH_TIMER $SHELL; echo
115
+}
116
+
117
+
118
+logger "Script $SCRIPT's started at $(LANG=C date)"
119
+cmd_update_mirrors
120
+cmd_publications_drop
121
+cmd_snapshots_drop
122
+cmd_snapshots_create
123
+cmd_publish
124
+
125
+exit 0;
126
+
127
+# [1] http://www.gnu.org/licenses/gpl-2.0.html