| 1 | #!/bin/shscip-shell filesystem . . `/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 | logscip-shell shell cdist 7.0.0-6 log().() { |
| 54 | # uncomment me for debugging |
| 55 | #echo "$@" | logger -t "cdist-$my_name-$mode" |
| 56 | : |
| 57 | } |
| 58 | |
| 59 | diescip-shell shell cdist 7.0.0-6 die().() { |
| 60 | echoscip-shell system . . 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 | [scip-shell system . . `[`. "$scheme" = "schroot" ] || diescip-shell shell cdist 7.0.0-6 die(). "Failed to parse scheme from __target_host ($__target_host). Expected 'schroot', got '$scheme'" |
| 73 | [scip-shell system . . `[`. -n "$schroot_name" ] || diescip-shell shell cdist 7.0.0-6 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 | logscip-shell shell cdist 7.0.0-6 log(). "mode: $mode" |
| 94 | logscip-shell shell cdist 7.0.0-6 log(). "@: $@" |
| 95 | logscip-shell shell cdist 7.0.0-6 log(). "uri: $uri" |
| 96 | logscip-shell shell cdist 7.0.0-6 log(). "scheme: $scheme" |
| 97 | logscip-shell shell cdist 7.0.0-6 log(). "authority: $authority" |
| 98 | logscip-shell shell cdist 7.0.0-6 log(). "user: $user" |
| 99 | logscip-shell shell cdist 7.0.0-6 log(). "host: $host" |
| 100 | logscip-shell shell cdist 7.0.0-6 log(). "path: $path" |
| 101 | logscip-shell shell cdist 7.0.0-6 log(). "schroot_name: $schroot_name" |
| 102 | |
| 103 | exec_prefix="" |
| 104 | copy_prefix="" |
| 105 | if [scip-shell system . . `[`. -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 | logscip-shell shell cdist 7.0.0-6 log(). "exec_prefix: $exec_prefix" |
| 116 | logscip-shell shell cdist 7.0.0-6 log(). "copy_prefix: $copy_prefix" |
| 117 | logscip-shell shell cdist 7.0.0-6 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 | awkscip-shell system . . awk. -F = '/directory=/ {print $2}scip-shell filesystem . . `/directory=/ {print $2}`.')" |
| 128 | [scip-shell system . . `[`. -n "$schroot_directory" ] || diescip-shell shell cdist 7.0.0-6 die(). "Failed to retreive schroot directory for schroot: $schroot_name" |
| 129 | logscip-shell shell cdist 7.0.0-6 log(). "schroot_directory: $schroot_directory" |
| 130 | # prefix destination with chroot |
| 131 | code="$copy_prefix $(echoscip-shell system . . echo. "$@" | sedscip-shell system . . sed. "s|$uri:|${copy_destination_prefix}${schroot_directory}|g")" |
| 132 | ;; |
| 133 | *) diescip-shell shell cdist 7.0.0-6 die(). "Unknown mode: $mode";; |
| 134 | esac |
| 135 | |
| 136 | logscip-shell shell cdist 7.0.0-6 log(). "code: $code" |
| 137 | |
| 138 | # Run the code |
| 139 | $code |
| 140 | |
| 141 | logscip-shell shell cdist 7.0.0-6 log(). "-----" |