Browse code

New syntax: action argument.

Add TODO list for issues.

Dmitrii Kashin authored on 09/10/2014 17:24:49
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,9 @@
0
+
1
+* Issues & wishes
2
+** TODO omitting flag -g cause problems with signing.
3
+   aptly supports unsigned repos, shall I use it in this case?
4
+** TODO gpg in cron doesn't work
5
+   Signing file 'Release' with gpg, please enter your passphrase when prompted:
6
+   gpg: cannot open tty `/dev/tty': Нет такого устройства или адреса
7
+   ERROR: unable to publish: unable to detached sign file: exit status 2
8
+
... ...
@@ -17,30 +17,35 @@
17 17
 # 3) create new snapshots and publish them
18 18
 
19 19
 # It's originally written by Dmitrii Kashin <freehck@freehck.ru>, and is
20
-# distributed under GNU GPLv2[2]. Fell free to improve it.
20
+# distributed under GNU GPLv2[2]. Feel free to improve it.
21 21
 
22 22
 set -e
23 23
 
24 24
 usage () {
25 25
     cat <<EOF
26
-repo-update.sh [-d] [-g <gpg-key-passphrase>] [-l <log-file>]
27
-  
26
+repo-update.sh [-d] [-g <gpg-key-passphrase>] [-l <log-file>] <action>
27
+
28 28
 -d	Activates debug mode.
29 29
 -g	Set a passphrase for your gpg key (it can be useful if you run script
30 30
         with cron.
31 31
 -l	File to store log. Type '-' for output to screen.
32
+
33
+Action could be one of:
34
+update	update mirrors and publications
32 35
 EOF
33 36
 }
34 37
 
35
-# Defaults:
38
+## Defaults:
36 39
 SCRIPT="$0"
37 40
 LOGFILE="$HOME/.aptly/repo-update.log"
38 41
 SHELL=/bin/sh
39
-PROMPT="%"
40
-DATE=`date +%F`
42
+PROMPT="% "
43
+DATE=`date +%F-%T`
41 44
 SUFFIX="$DATE"
42 45
 GPG_PASSWD=""
46
+DEBUG=0
43 47
 
48
+## Configuration:
44 49
 while getopts hdg:l: option
45 50
 do
46 51
     case "$option" in
... ...
@@ -51,14 +56,15 @@ do
51 51
 	*) usage; exit 1;;
52 52
     esac
53 53
 done
54
+ACTION=$(eval "echo \"\$$OPTIND\"") # bit-hack: Dash doesn't have ${!OPTIND} construction
54 55
 
55
-if [ "x$LOGFILE" = "x-" ]
56
+# output everything into $LOGFILE
57
+if [ "x$LOGFILE" != "x-" ]
56 58
 then
57
-    TO_LOGFILE=""
58
-else 
59
-    TO_LOGFILE=">>$LOGFILE"
59
+    exec 3>>"$LOGFILE" 1>&3 2>&1
60 60
 fi
61 61
 
62
+# debug flag enables time measuring 
62 63
 if [ $DEBUG -eq 1 ]
63 64
 then
64 65
     WITH_TIMER=time
... ...
@@ -66,64 +72,129 @@ else
66 66
     WITH_TIMER=
67 67
 fi
68 68
 
69
-# Additional functions
69
+## Additional functions:
70 70
 
71
+# control output: delete all passwords from it
71 72
 logger () {
72
-    STR=$1
73
-    CMD=$(echo "echo \"$STR\" $TO_LOGFILE" | sed 's/\$/\\\$/g' | sed "s/$GPG_PASSWD/\*\*\*/g")
74
-    echo $CMD | $SHELL
73
+    echo "$@" | if [ -z "$GPG_PASSWD" ]
74
+    then
75
+	cat
76
+    else
77
+	sed "s/$GPG_PASSWD/\*\*\*/g"
78
+    fi
79
+}
80
+
81
+# same as logger but append $PROMPT before each line to show it's a separate command
82
+plogger () {
83
+    STR=$(
84
+	echo "$@" | if [ -z "$PROMPT" ]
85
+	then
86
+	    cat
87
+	else
88
+	    sed "s/^/$PROMPT/"
89
+	fi)
90
+    logger "$STR"
75 91
 }
76 92
 
93
+# log command and run it
94
+run_command () {
95
+    plogger "$1"
96
+    eval "$WITH_TIMER $1"
97
+    echo
98
+}
99
+
100
+## aptly commands:
101
+
77 102
 # update all mirrors
103
+CMD_UPDATE_MIRRORS="aptly mirror list -raw | xargs -n 1 -r aptly mirror update $TO_LOGFILE"
78 104
 cmd_update_mirrors () {
79
-    CMD_MIRRORS_UPDATE="for mirror in \$(aptly mirror list -raw); do aptly mirror update \$mirror $TO_LOGFILE; done"
80
-    #CMD_MIRRORS_UPDATE="aptly mirror list -raw | xargs -n 1 -r aptly mirror update $TO_LOGFILE"
81
-    logger "$PROMPT $CMD_MIRRORS_UPDATE"
82
-    echo "$CMD_MIRRORS_UPDATE" | $WITH_TIMER $SHELL; echo
105
+    logger "RUN: CMD_UPDATE_MIRRORS"
106
+    run_command "$CMD_UPDATE_MIRRORS"
83 107
 }
84 108
 
85
-# Create snapshots for each mirror with the same name and a new SUFFIX
86
-cmd_snapshots_create () {
87
-    CMD_SNAPSHOTS_CREATE="for mirror in \$(aptly mirror list -raw); do aptly snapshot create \$mirror-$DATE from mirror \$mirror $TO_LOGFILE; done"
88
-    #CMD_SNAPSHOTS_CREATE="aptly mirror list -raw | xargs -n 1 -I{} aptly snapshot create {}-\"$SUFFIX\" from mirror {} $TO_LOGFILE"
89
-    logger "$PROMPT $CMD_SNAPSHOTS_CREATE"
90
-    echo "$CMD_SNAPSHOTS_CREATE" | $WITH_TIMER $SHELL; echo
109
+# create snapshots named '$mirror-$suffix'
110
+CMD_CREATE_SNAPSHOTS="aptly mirror list -raw | xargs -n 1 -I{} aptly snapshot create {}-\"$SUFFIX\" from mirror {} $TO_LOGFILE"
111
+cmd_create_snapshots () {
112
+    logger "RUN: CMD_CREATE_SNAPSHOTS"
113
+    run_command "$CMD_CREATE_SNAPSHOTS"
91 114
 }
92 115
 
93
-# Drop all publications made previous time
94
-cmd_publications_drop () {
95
-    # TODO: learn with author whether it's normal with the dot in the first field; I suppose it's a bug
96
-    CMD_PUBLICATIONS_DROP="for publication in \$(aptly publish list -raw | awk '{print \$2}'); do aptly publish drop \$publication $TO_LOGFILE; done"
97
-    #CMD_PUBLICATIONS_DROP="aptly publish list -raw | awk '{print \$2}' | xargs -n 1 -r aptly publish drop $TO_LOGFILE"
98
-    logger "$PROMPT $CMD_PUBLICATIONS_DROP"
99
-    echo "$CMD_PUBLICATIONS_DROP" | $WITH_TIMER $SHELL; echo
116
+# drop all your publications
117
+CMD_DROP_PUBLICATIONS="aptly publish list -raw | awk '{print \$2}' | xargs -n 1 -r aptly publish drop $TO_LOGFILE"
118
+cmd_drop_publications () {
119
+    logger "RUN: CMD_DROP_PUBLICATIONS"
120
+    run_command "$CMD_DROP_PUBLICATIONS"
100 121
 }
101 122
 
102
-# Drop all snapshots made previous time
103
-cmd_snapshots_drop () {
104
-    CMD_SNAPSHOTS_DROP="for snapshot in \$(aptly snapshot list -raw); do aptly snapshot drop \$snapshot $TO_LOGFILE; done"
105
-    #CMD_SNAPSHOTS_DROP="aptly snapshot list -raw | xargs -n 1 -r aptly snapshot drop $TO_LOGFILE"
106
-    logger "$PROMPT $CMD_SNAPSHOTS_DROP"
107
-    echo "$CMD_SNAPSHOTS_DROP" | $WITH_TIMER $SHELL; echo
123
+# drop all your snapshots
124
+CMD_DROP_SNAPSHOTS="aptly snapshot list -raw | xargs -n 1 -r aptly snapshot drop $TO_LOGFILE"
125
+cmd_drop_snapshots () {
126
+    logger "RUN: CMD_DROP_PUBLICATIONS"
127
+    run_command "$CMD_DROP_SNAPSHOTS"
108 128
 }
109 129
 
110
-# Publish & sign all the mirrors
111
-cmd_publish () {
112
-    # Run berserk, spreading fear and pain!
113
-    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\"; }")
114
-    logger "$PROMPT $CMD_PUBLISH"
115
-    echo "$CMD_PUBLISH" | $WITH_TIMER $SHELL; echo
130
+# create new publications using last available snapshots
131
+cmd_create_publications () {
132
+    logger "RUN: CMD_CREATE_PUBLICATIONS"
133
+    # check here for available snapshots maybe?
134
+    CMD_CREATE_PUBLICATIONS=$(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;\"; }")
135
+    run_command "$CMD_CREATE_PUBLICATIONS"
116 136
 }
117 137
 
138
+# update all your publications
139
+cmd_update_publications () {
140
+    logger "RUN: CMD_UPRATE_PUBLICATIONS"
141
+    # check here for available publications maybe?
142
+    CMD_UPRATE_PUBLICATIONS=$(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 switch -component=\"comp\" -passphrase=$GPG_PASSWD \"release\" \"snapshots\" $TO_LOGFILE;\"; }")
143
+    run_command "$CMD_UPRATE_PUBLICATIONS"
144
+}
145
+
146
+## Actions:
147
+
148
+act_update () {
149
+    cmd_update_mirrors
150
+    cmd_create_snapshots
151
+    cmd_update_publications
152
+    exit 0
153
+}
154
+
155
+act_create () {
156
+    cmd_update_mirrors
157
+    cmd_create_snapshots
158
+    cmd_create_publications
159
+    exit 0
160
+}    
161
+
162
+act_clear () {
163
+    cmd_drop_publications
164
+    cmd_drop_snapshots
165
+    exit 0
166
+}
167
+
168
+act_test () {
169
+    # this function is a sandbox for experiments
170
+    exit 0
171
+}
118 172
 
173
+## Runtime:
119 174
 logger "Script $SCRIPT's started at $(LANG=C date)"
120
-cmd_update_mirrors
121
-cmd_publications_drop
122
-cmd_snapshots_drop
123
-cmd_snapshots_create
124
-cmd_publish
125 175
 
126
-exit 0;
176
+case "$ACTION" in
177
+    test)
178
+	act_test;;
179
+    create)
180
+	act_create;;
181
+    update)
182
+	act_update;;
183
+    clear)
184
+	act_clear;;
185
+    help)
186
+	usage; exit 0;;
187
+esac
188
+
189
+# Default behaviour
190
+logger "And immediately finished! =)"
191
+exit 2
127 192
 
128 193
 # [1] https://github.com/smira/aptly.git
129 194
 # [2] http://www.gnu.org/licenses/gpl-2.0.html