|
84 | 84 | done
|
85 | 85 | }
|
86 | 86 |
|
87 |
| -do_exec() |
| 87 | +maybe_get_current() |
| 88 | +{ |
| 89 | + echo $([ -f "$basedir/current" ] && cat "$basedir/current" || true) |
| 90 | + [ ! "$current" ] || [ -d "$basedir/$current" ] \ |
| 91 | + || abort "Current generation does not exist" |
| 92 | +} |
| 93 | + |
| 94 | +get_current() |
88 | 95 | {
|
89 |
| - local action="$1" |
90 | 96 | local current=
|
91 | 97 |
|
92 |
| - current=$([ -f "$basedir/current" ] && cat "$basedir/current") |
| 98 | + current=$(maybe_get_current) |
93 | 99 | [ "$current" ] && [ -d "$basedir/$current" ] \
|
94 | 100 | || abort "Current generation does not exist"
|
95 | 101 |
|
96 |
| - action_exec "$1" "$basedir/$current" |
| 102 | + echo "$current" |
97 | 103 | }
|
98 | 104 |
|
99 |
| -do_abandon() |
| 105 | +get_next() |
100 | 106 | {
|
101 | 107 | local next=
|
102 | 108 |
|
103 | 109 | next=$([ -f "$basedir/next" ] && cat "$basedir/next" || true)
|
104 | 110 | [ "$next" ] && [ -d "$basedir/$next" ] \
|
105 | 111 | || abort "Next generation does not exist"
|
106 | 112 |
|
107 |
| - mv "$basedir/$next" "$basedir/$next-ABANDONED-$(date +%F-%T)" |
108 |
| - rm "$basedir/next" |
109 |
| - inform info "Abandoned generation $next" |
| 113 | + echo "$next" |
110 | 114 | }
|
111 | 115 |
|
112 |
| -do_evolve() |
| 116 | +do_exec() |
113 | 117 | {
|
114 |
| - local current= |
115 |
| - local next= |
| 118 | + local action="$1" |
116 | 119 |
|
117 |
| - current=$([ -f "$basedir/current" ] && cat "$basedir/current" || true) |
118 |
| - [ ! "$current" ] || [ -d "$basedir/$current" ] \ |
119 |
| - || abort "Current generation does not exist" |
| 120 | + action_exec "$1" "$basedir/$(get_current)" |
| 121 | +} |
120 | 122 |
|
121 |
| - next=$([ -f "$basedir/next" ] && cat "$basedir/next" || true) |
122 |
| - [ "$next" ] && [ -d "$basedir/$next" ] \ |
123 |
| - || abort "Next generation does not exist" |
| 123 | +do_abandon() |
| 124 | +{ |
| 125 | + local next=$(get_next) |
| 126 | + |
| 127 | + date +%F-%T >"$basedir/$next/ABANDONED" |
| 128 | + |
| 129 | + mv "$basedir/next" "$basedir/current" |
| 130 | + inform info "Abandoned generation $next" |
| 131 | +} |
| 132 | + |
| 133 | +do_evolve() |
| 134 | +{ |
| 135 | + local current=$(maybe_get_current) |
| 136 | + local next=$(get_next) |
| 137 | + local ar= |
124 | 138 |
|
125 | 139 | [ "$current" ] && action_exec exit "$basedir/$current"
|
126 | 140 |
|
127 | 141 | action_exec init "$basedir/$next"
|
128 | 142 |
|
129 | 143 | mv "$basedir/next" "$basedir/current"
|
130 | 144 | inform info "Evolved to generation $next"
|
| 145 | + |
| 146 | + if [ "$current" ]; then |
| 147 | + ar="$current.tar.gz" |
| 148 | + [ -f "$basedir/$current/ABANDONED" ] && ar="$current-ABANDONED.tar.gz" |
| 149 | + |
| 150 | + (cd "$basedir" && tar caf "$ar.tar.gz" "$current") |
| 151 | + rm -rf "$basedir/$current" |
| 152 | + fi |
| 153 | +} |
| 154 | + |
| 155 | +do_prune() |
| 156 | +{ |
| 157 | + local keep=${1:-10} |
| 158 | + |
| 159 | + cd "$basedir" |
| 160 | + rm -f old=$(ls -rv *.tar.gz | tail "+$((keep + 1))") |
131 | 161 | }
|
132 | 162 |
|
133 | 163 | usage()
|
@@ -158,6 +188,10 @@ Commands:
|
158 | 188 | exec <action>
|
159 | 189 | Run the specified action of the current generation.
|
160 | 190 |
|
| 191 | + prune [<num-generations>] |
| 192 | + Remove all but the last <num-generations>, or 10 by default, |
| 193 | + archived generations. |
| 194 | +
|
161 | 195 | help
|
162 | 196 | Show this message.
|
163 | 197 |
|
@@ -201,6 +235,9 @@ case $cmd in
|
201 | 235 | "exec")
|
202 | 236 | do_exec "$@"
|
203 | 237 | ;;
|
| 238 | + "prune") |
| 239 | + do_prune "$@" |
| 240 | + ;; |
204 | 241 |
|
205 | 242 | *)
|
206 | 243 | usage && exit 1
|
|
0 commit comments