]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd-aptenable
499a3e8c307639473c629648220054610a12ba3f
[neurodebian.git] / tools / nd-aptenable
1 #!/bin/bash
2 #emacs: -*- mode: shell-script; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
3 #ex: set sts=4 ts=4 sw=4 et:
4
5 # Depends:    apt, python, wget
6 # Recommends: netselect
7
8 # play safe
9 set -e
10 set -u
11
12 ############
13 # Defaults #
14 ############
15
16 nd_aptenable_version=0.1
17
18 nd_key_id=0x2649A5A9
19 nd_config_file=/etc/neurodebian/neurodebian.cfg
20 nd_mirror_origin=http://neuro.debian.net/debian
21 nd_mirror_default=$nd_mirror_origin # or may be AWS?
22
23 # To be set by cmdline args
24 ae_release=
25 ae_components=software,data
26 ae_flavor=
27 ae_mirror=best
28 ae_suffix=
29 ae_verbose=1
30 ae_overwrite=
31 ae_sources=
32 ae_dry_run=
33
34 ae_sudo=
35 exe_dir=$(dirname $0)
36
37 # TODOs:
38 # - apt priority! (so we could avoid automagic upgrades etc)
39 # - multiarch setups
40
41 ae_tempdir=$(mktemp -d)
42 trap "rm -rf \"$ae_tempdir\"" TERM INT EXIT
43
44
45 print_verbose()
46 {
47     level=$1; shift
48         if [ "$ae_verbose" -ge $level ]; then
49         # use stderr for printing within functions stdout of which might be used
50         echo -e "I: $*" >&2
51     fi
52 }
53
54 error()
55 {
56     code=$1; shift
57         echo -e "E: $*" >&2
58     exit $code
59 }
60
61 print_version()
62 {
63     cat << EOT
64 nd-aptenable $nd_aptenable_version
65
66 Copyright (C) 2014 Yaroslav Halchenko <debian@onerussian.com>
67
68 Licensed under GNU Public License version 3 or later.
69 This is free software; see the source for copying conditions.  There is NO
70 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
71
72 Written by Yaroslav Halchenko for the NeuroDebian project.
73
74 EOT
75 }
76
77 eval_dry()
78 {
79     if [ -z "$ae_dry_run" ]; then
80         eval "$ae_sudo $@"
81     else
82         echo "DRY: $@"
83     fi
84 }
85
86 print_help()
87 {
88     cat << EOT
89
90 Usage:  nd-aptenable [options]
91
92 Enables NeuroDebian repository for the current Debian or Ubuntu release.
93
94 Options:
95
96   -r, --release=RELEASE
97     Name of the Debian/Ubuntu release to be used.  If not specified,
98     it is deduced from the  apt-cache policy  output, by taking repository
99     of Debian or Ubuntu origin with highest priority.
100
101   -f, --flavor=full|libre
102     Which flavor of the repository should be enabled:
103      libre -- Only  main  component, containing only DFSG-compliant content.
104      full -- Includes main, contrib, and non-free.
105     If not specified -- deduced from the output of apt-cache policy
106
107   -c, --components=c1,c2,c3
108     Comma separated list of components to enable among:
109      software -- primary software repository
110      data -- data packages
111      devel -- "overlay" of development versions (like Debians' "experimental").
112               Not sufficient on its own and available only from the main site
113     If not specified -- "software,data"
114
115   -m, --mirror=NAME|URL
116     Which mirror to use.  Could be a mirror code-name (as specified in
117     /etc/neurodebian/neurodebian.cfg), or a URL.
118
119   --overwrite,
120     If apt file already present, it would not be overriden (by default).
121     Use this option to overwrite.
122
123   --suffix=SUFFIX
124     Which suffix to add to the apt file, in case you are trying to enable
125     multiple repositories
126
127   --sources, --no-sources
128     Either to enable deb-src lines.  If none specified -- would be enabled if
129     sources for a core package (apt) are available.
130
131   -n, --dry-run
132     Do not perform any changes -- generated configurations and commands will
133     simply be printed to stdout
134
135   -v, --verbose
136     Enable additional progress messages.  Could be used multiple times
137
138   -q, --quiet
139     Make operation quiet -- only error messages would be output
140
141   -h, --help
142     Print short description, usage summary and option list.
143
144   --version
145     Print version information and exit.
146
147 Exit status:
148
149   non-0 exit status in case of error.  Error exit code would depend
150   on which command has failed
151
152 Examples:
153
154   - Enable software and data components from the optimal (according to
155     netselect) mirror.  Some information about progress will be printed
156
157     nd-aptenable
158
159   - Quietly enable -devel repository for the current release, and place apt
160     configuration into /etc/apt/sources.list.d/neurodebian.sources-devel.list
161
162     nd-aptenable -q --suffix=-devel -c devel
163
164   - Force sid distribution, all the components, from the Japan mirror:
165
166     nd-aptenable -q --suffix=-de-sid-full -c software,data,devel -m jp
167
168 EOT
169 }
170
171 get_neurodebian_cfg()
172 {
173     # First we try to fetch the most recent version from the github
174     print_verbose 3 "Fetching config file from the github repository"
175     cfgfile_temp="$ae_tempdir/neurodebian.cfg"
176     wget -c -q -O$cfgfile_temp https://raw.githubusercontent.com/neurodebian/neurodebian/master/neurodebian.cfg \
177         && { echo $cfgfile_temp; } \
178         || { [ -e "$nd_config_file" ] && return "$nd_config_file"; }
179     # if not -- should blow up
180 }
181
182 query_cfg_section()
183 {
184     config_file="$1"
185     section="$2"
186     python -c "from ConfigParser import SafeConfigParser as SP; cfg = SP(); cfg.read('$config_file'); print('\n'.join([' '.join(x) for x in cfg.items('$section')]))"
187 }
188
189 get_mirrors()
190 {
191     nd_config=`get_neurodebian_cfg`
192 #    $exe_dir/nd_querycfg -F" " --config-file="$nd_config" "mirrors" \
193     query_cfg_section "$nd_config" "mirrors" \
194     | while read mirror_name mirror_url; do
195         # verify that url is just a url
196         if echo "$mirror_url" | grep -v -e '^[a-z0-9:+]*://[-+_%.a-z0-9/]*$'; then
197             print_verbose 1 "Mirror $mirror_name has 'illegit' URL: $mirror_url.  Skipping"
198         fi
199         echo "$mirror_name $mirror_url"
200     done
201 }
202
203 netselect_mirror() {
204     # select "closest" mirror according to netselect.
205     print_verbose 2 "Selecting the 'best' mirror using netselect"
206     if ! which netselect >/dev/null; then
207         print_verbose 1 "netselect (apt-get install netselect) needed to select the 'best' mirror was not found"
208         print_verbose 1 "Selecting the default repository: $nd_mirror_default"
209         echo $nd_mirror_default
210     else
211         get_mirrors | awk '{print $2;}' | $ae_sudo xargs netselect -D -s 1 | awk '{print $2;}'
212     fi
213 }
214
215 get_mirror_url() {
216     # given mirror alias -- find its url
217     url=$(get_mirrors | awk "/^$1 /{print \$2;}")
218     if [ -z "$url" ]; then
219         error 9 "Cannot resolve mirror $1 to the URL"
220     fi
221     echo $url
222 }
223
224 get_apt_policy()
225 {
226     # Get apt-cache policy output in a single list for matching suites
227     # (could be a separated with \| or , for multiple choices, e.g.
228     #
229     # get_apt_policy Debian,Ubuntu
230     # or
231     # get_apt_policy NeuroDebian
232     suites="$1"
233     apt-cache policy | grep -B1 -e "o=\(${suites//,/\\|}\)" | tr '\n' ' ' | sed -e 's, -- ,\n,g' | grep -v -e '-\(updates\|security\)' | sort -nr
234 }
235
236 include_component()
237 {
238     echo "$ae_components" | tr ',' '\n' | grep -q "^$1\$"
239 }
240
241
242 #
243 # Commandline options handling
244 #
245
246 # Parse commandline options (taken from the getopt examples from the Debian util-linux package)
247 # Note that we use `"$@"' to let each command-line parameter expand to a
248 # separate word. The quotes around `$@' are essential!
249 # We need CLOPTS as the `eval set --' would nuke the return value of getopt.
250 CLOPTS=`getopt -o h,r:,m:,f:,c:,q,v,n --long help,version,quiet,verbose,mirror:,release:,flavor:,components:,suffix:,overwrite,sources,no-sources,dry-run,print-mirrors,print-best-mirror -n 'nd-aptenable' -- "$@"`
251
252 if [ $? != 0 ] ; then
253   error 2 "Problem with parsing cmdline.  Terminating..."
254 fi
255
256 # Note the quotes around `$CLOPTS': they are essential!
257 eval set -- "$CLOPTS"
258
259 if [ `whoami` != "root" ]; then
260     ae_sudo=sudo
261 fi
262
263 while true ; do
264   case "$1" in
265           -r|--release) shift; ae_release="$1"; shift;;
266           -f|--flavor) shift;  ae_flavor="$1"; shift;;
267           -c|--components) shift; ae_components="$1"; shift;;
268       -m|--mirror) shift;  ae_mirror="$1"; shift;;
269          --print-mirrors)  get_mirrors; exit 0;;
270          --print-best-mirror)  netselect_mirror; exit 0;;
271       -n|--dry-run)        ae_dry_run=1; shift;;
272          --suffix) shift;  ae_suffix="$1"; shift;;
273          --overwrite)      ae_overwrite="$1"; shift;;
274          --sources)        ae_sources=1; shift;;
275          --no-sources)     ae_sources=0; shift;;
276           -q|--quiet)          ae_verbose=0; shift;;
277           -v|--verbose)        ae_verbose=$(($ae_verbose+1)); shift;;
278           -h|--help) print_help; exit 0;;
279           --version) print_version; exit 0;;
280           --) shift ; break ;;
281           *) error 1 "Internal error! ($1)";;
282   esac
283 done
284
285
286 if [ $# -gt 0 ] ; then
287     print_help >&2
288     exit 2
289 fi
290
291 # Inform!
292 [ -z "$ae_sudo" ] || print_verbose 1 "This script requires root access.  Since current user is not root, sudo will be used"
293
294 #
295 # Basic system/environment knowledge
296 #
297
298 ae_output_file=/etc/apt/sources.list.d/neurodebian.sources${ae_suffix}.list
299
300 apt_policy=$(get_apt_policy "Debian,Ubuntu" )
301
302 if [ -z "$ae_release" ]; then
303     ae_release=$(echo "$apt_policy" | head -1 | sed -e 's/.*,n=\([^,]*\),.*/\1/g')
304 fi
305
306 if [ -z "$ae_flavor" ]; then
307     ae_flavor=$(echo "$apt_policy" | grep -e ",n=$ae_release," | grep -qe 'c=\(non-free\|restricted\)' && echo "full" || echo "libre")
308 fi
309
310 #
311 # Determine which mirror to use
312 #
313
314 # knowing mirror is not necessary for -devel available only from the main site
315 if include_component software || include_component data; then
316     # for now just use default
317     if [ -z "$ae_mirror" ]; then # none specified
318         ae_mirror_url=$nd_mirror_origin
319     else
320         if ! [[ "$ae_mirror" =~ ".*://.*" ]]; then
321             case "$ae_mirror" in
322                 best)    ae_mirror_url=$(netselect_mirror);;
323                 default) ae_mirror_url=$nd_mirror_default;;
324                 origin)  ae_mirror_url=$nd_mirror_origin;;
325                 *)       ae_mirror_url=$(get_mirror_url "$ae_mirror");;
326             esac
327         else
328             ae_mirror_url="$ae_mirror" # it was some kind of a URL already
329         fi
330     fi
331 fi
332
333 #
334 # Prepare APT file
335 #
336
337 case $ae_flavor in
338  full)  apt_flavor="contrib non-free";;
339  libre) apt_flavor="";;
340  *) error 5 "Unknown value of flavor $apt_flavor.  Must be full or libre"
341 esac
342
343 if [ -z "$ae_sources" ]; then
344     apt-cache showsrc apt >&/dev/null && ae_sources=1 || ae_sources=0
345 fi
346
347 if [ $ae_sources -eq 0 ]; then
348     sources_comment="#"
349 else
350     sources_comment=""
351 fi
352
353 apt_list=
354
355 if include_component software; then
356     apt_list+="
357 # NeuroDebian software repository
358 deb     $ae_mirror_url $ae_release main $apt_flavor
359 ${sources_comment}deb-src $ae_mirror_url $ae_release main $apt_flavor
360 "
361 fi
362
363 if include_component data; then
364     apt_list+="
365 # NeuroDebian data repository
366 deb     $ae_mirror_url data main $apt_flavor
367 ${sources_comment}deb-src $ae_mirror_url data main $apt_flavor
368 "
369 fi
370
371 if include_component devel; then
372     apt_list+="
373 # NeuroDebian -devel repository
374 deb     http://neuro.debian.net/debian-devel $ae_release main $apt_flavor
375 ${sources_comment}deb-src http://neuro.debian.net/debian-devel $ae_release main $apt_flavor
376 "
377 fi
378
379 if [ -e "$ae_output_file" ] && [ -z "$ae_overwrite" ]; then
380     if diff "$ae_output_file" <(echo "$apt_list") | grep -q .; then
381         # error 3
382         print_verbose 1 "File $ae_output_file already exists, containing:\n`cat \"$ae_output_file\"`\n\nI: New configuration is different:\n$apt_list"
383         if get_apt_policy NeuroDebian >/dev/null; then
384             print_verbose 1 "NeuroDebian repositories are already available, thus skipping the rest."
385             print_verbose 1 "Rerun with --overwrite if you would like to reconfigure."
386             exit 0
387         else
388             print_verbose 1 "NeuroDebian configuration is found but not yet available -- continuing with new configuration."
389         fi
390     else
391         print_verbose 1 "New configuration is identical to existing and NeuroDebian repository is already enabled."
392         print_verbose 1 "Skiping the rest. Rerun with --overwrite if you would like to reconfigure."
393         exit 0
394     fi
395 else
396     print_verbose 1 "Generating $ae_output_file"
397     if [ -z "$ae_dry_run" ]; then
398         echo "$apt_list" | $ae_sudo bash -c "cat - >| '$ae_output_file'"
399     else
400         echo "DRY:"
401         echo "$apt_list"
402     fi
403 fi
404
405
406 #
407 # Assure present archive GPG key for APT system
408 #
409
410 # Figure out if key needs to be imported (if ran within package,
411 # should already be there due to neurodebian-keyring package)
412 if LANG=C eval $ae_sudo apt-key export $nd_key_id 2>&1 1>/dev/null | grep -qe "nothing exported"; then
413     print_verbose 1 "Fetching the key from the server"
414     eval_dry apt-key adv --recv-keys --keyserver pgp.mit.edu $nd_key_id
415 fi
416
417 #
418 # Finalizing (apt-get update etc)
419 #
420
421 print_verbose 1 "Updating APT listings, might take a few minutes"
422 if [ -z "$ae_dry_run" ]; then
423     apt_logfile="$ae_tempdir/apt.log"
424     $ae_sudo apt-get update 1>"$apt_logfile" 2>&1 \
425         && rm -f "$apt_logfile" \
426         || {
427              cat "$apt_logfile"
428              error 5 "E: Update failed with exit code $? (above output logged into $apt_logfile)."
429              }
430 else
431     eval_dry apt-get update
432 fi
433
434 if [ "$ae_verbose" -ge 2 ]; then
435     print_verbose 2 "Currently enabled NeuroDebian suites/mirrors:"
436     get_apt_policy NeuroDebian
437 fi