Debian Codegraph @

other/examples/remote/schroot-uri

raw symbols SCIP
1 #!/bin/sh -e
2 #
3 # 2012 Steven Armstrong (steven-cdist at armstrong.cc)
4 #
5 # This file is part of cdist.
6 #
7 # cdist is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # cdist is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with cdist. If not, see <http://www.gnu.org/licenses/>.
19 #
20 #
21 # __remote_{exec,copy} script to run cdist against a schroot target uri
22 #
23 # Usage:
24 # cdist config \
25 # --remote-exec "/path/to/this/script exec" \
26 # --remote-copy "/path/to/this/script copy" \
27 # target_uri
28 #
29 # # target_uri examples:
30 # schroot:///chroot-name
31 # schroot://foo.ethz.ch/chroot-name
32 # schroot://[email protected]/chroot-name
33 #
34 # # and how to match them in .../manifest/init
35 # case "$target_host" in
36 # schroot://*)
37 # # any schroot
38 # ;;
39 # schroot://foo.ethz.ch/*)
40 # # any schroot on specific host
41 # ;;
42 # schroot://foo.ethz.ch/chroot-name)
43 # # specific schroot on specific host
44 # ;;
45 # schroot:///chroot-name)
46 # # specific schroot on localhost
47 # ;;
48 # esac
49
50 my_name="${0##*/}"
51 mode="$1"; shift
52
53 log() {
54 # uncomment me for debugging
55 #echo "$@" | logger -t "cdist-$my_name-$mode"
56 :
57 }
58
59 die() {
60 echo "$@" >&2
61 exit 1
62 }
63
64
65 uri="$__target_host"
66
67 scheme="${uri%%:*}"; rest="${uri#$scheme:}"; rest="${rest#//}"
68 authority="${rest%%/*}"; rest="${rest#$authority}"
69 path="${rest%\?*}"; rest="${rest#$path}"
70 schroot_name="${path#/}"
71
72 [ "$scheme" = "schroot" ] || die "Failed to parse scheme from __target_host ($__target_host). Expected 'schroot', got '$scheme'"
73 [ -n "$schroot_name" ] || die "Failed to parse schroot name from __target_host: $__target_host"
74
75 case "$authority" in
76 '')
77 # authority is empty, neither user nor host given
78 user=""
79 host=""
80 ;;
81 *@*)
82 # authority contains @, take user from authority
83 user="${authority%@*}"
84 host="${authority#*@}"
85 ;;
86 *)
87 # no user in authority, default to root
88 user="root"
89 host="$authority"
90 ;;
91 esac
92
93 log "mode: $mode"
94 log "@: $@"
95 log "uri: $uri"
96 log "scheme: $scheme"
97 log "authority: $authority"
98 log "user: $user"
99 log "host: $host"
100 log "path: $path"
101 log "schroot_name: $schroot_name"
102
103 exec_prefix=""
104 copy_prefix=""
105 if [ -n "$host" ]; then
106 # we are working on a remote host
107 exec_prefix="ssh -o User=$user -q $host"
108 copy_prefix="scp -o User=$user -q"
109 copy_destination_prefix="$host:"
110 else
111 # working on local machine
112 copy_prefix="cp"
113 copy_destination_prefix=""
114 fi
115 log "exec_prefix: $exec_prefix"
116 log "copy_prefix: $copy_prefix"
117 log "copy_destination_prefix: $copy_destination_prefix"
118
119 case "$mode" in
120 exec)
121 # In exec mode the first argument is the __target_host which we already got from env. Get rid of it.
122 shift
123 code="$exec_prefix schroot -c $schroot_name -- sh -c '$@'"
124 ;;
125 copy)
126 # get directory for given chroot_name
127 schroot_directory="$($exec_prefix schroot -c $schroot_name --config | awk -F = '/directory=/ {print $2}')"
128 [ -n "$schroot_directory" ] || die "Failed to retreive schroot directory for schroot: $schroot_name"
129 log "schroot_directory: $schroot_directory"
130 # prefix destination with chroot
131 code="$copy_prefix $(echo "$@" | sed "s|$uri:|${copy_destination_prefix}${schroot_directory}|g")"
132 ;;
133 *) die "Unknown mode: $mode";;
134 esac
135
136 log "code: $code"
137
138 # Run the code
139 $code
140
141 log "-----"