| 
									
										
										
										
											2011-11-08 13:29:11 +01:00
										 |  |  | #!/bin/sh -e | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2012-05-29 09:29:01 +02:00
										 |  |  | # 2012 Steven Armstrong (steven-cdist at armstrong.cc) | 
					
						
							| 
									
										
										
										
											2011-11-08 13:29:11 +01:00
										 |  |  | # | 
					
						
							|  |  |  | # This file is part of cdist. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # cdist is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | # it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  | # the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | # (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # cdist is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with cdist. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # __remote_{exec,copy} script to run cdist against a schroot target uri | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Usage: | 
					
						
							| 
									
										
										
										
											2012-05-29 11:14:21 +02:00
										 |  |  | #  cdist config \ | 
					
						
							|  |  |  | #     --remote-exec "/path/to/this/script exec" \ | 
					
						
							|  |  |  | #     --remote-copy "/path/to/this/script copy" \ | 
					
						
							|  |  |  | #     target_uri | 
					
						
							| 
									
										
										
										
											2011-11-08 13:29:11 +01:00
										 |  |  | # | 
					
						
							|  |  |  | # # target_uri examples: | 
					
						
							|  |  |  | # schroot:///chroot-name | 
					
						
							|  |  |  | # schroot://foo.ethz.ch/chroot-name | 
					
						
							|  |  |  | # schroot://user-name@foo.ethz.ch/chroot-name | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # # and how to match them in .../manifest/init | 
					
						
							|  |  |  | # case "$target_host" in | 
					
						
							|  |  |  | #    schroot://*) | 
					
						
							|  |  |  | #       # any schroot | 
					
						
							|  |  |  | #    ;; | 
					
						
							|  |  |  | #    schroot://foo.ethz.ch/*) | 
					
						
							|  |  |  | #       # any schroot on specific host | 
					
						
							|  |  |  | #    ;; | 
					
						
							|  |  |  | #    schroot://foo.ethz.ch/chroot-name) | 
					
						
							|  |  |  | #       # specific schroot on specific host | 
					
						
							|  |  |  | #    ;; | 
					
						
							|  |  |  | #    schroot:///chroot-name) | 
					
						
							|  |  |  | #       # specific schroot on localhost | 
					
						
							|  |  |  | #    ;; | 
					
						
							|  |  |  | # esac | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | my_name="${0##*/}" | 
					
						
							|  |  |  | mode="$1"; shift | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | log() { | 
					
						
							| 
									
										
										
										
											2012-05-29 09:29:01 +02:00
										 |  |  |    # uncomment me for debugging | 
					
						
							|  |  |  |    #echo "$@" | logger -t "cdist-$my_name-$mode" | 
					
						
							| 
									
										
										
										
											2011-11-08 13:29:11 +01:00
										 |  |  |    : | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | die() { | 
					
						
							|  |  |  |    echo "$@" >&2 | 
					
						
							|  |  |  |    exit 1 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uri="$__target_host" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | scheme="${uri%%:*}"; rest="${uri#$scheme:}"; rest="${rest#//}" | 
					
						
							|  |  |  | authority="${rest%%/*}"; rest="${rest#$authority}" | 
					
						
							|  |  |  | path="${rest%\?*}"; rest="${rest#$path}" | 
					
						
							|  |  |  | schroot_name="${path#/}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ "$scheme" = "schroot" ] || die "Failed to parse scheme from __target_host ($__target_host). Expected 'schroot', got '$scheme'" | 
					
						
							|  |  |  | [ -n "$schroot_name" ] || die "Failed to parse schroot name from __target_host: $__target_host" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | case "$authority" in | 
					
						
							|  |  |  |    '') | 
					
						
							|  |  |  |       # authority is empty, neither user nor host given | 
					
						
							|  |  |  |       user="" | 
					
						
							|  |  |  |       host="" | 
					
						
							|  |  |  |    ;;  | 
					
						
							|  |  |  |    *@*) | 
					
						
							|  |  |  |       # authority contains @, take user from authority | 
					
						
							|  |  |  |       user="${authority%@*}" | 
					
						
							|  |  |  |       host="${authority#*@}" | 
					
						
							|  |  |  |    ;;  | 
					
						
							|  |  |  |    *)  | 
					
						
							|  |  |  |       # no user in authority, default to root | 
					
						
							|  |  |  |       user="root" | 
					
						
							|  |  |  |       host="$authority" | 
					
						
							|  |  |  |    ;; | 
					
						
							|  |  |  | esac | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | log "mode: $mode" | 
					
						
							|  |  |  | log "@: $@" | 
					
						
							|  |  |  | log "uri: $uri" | 
					
						
							|  |  |  | log "scheme: $scheme" | 
					
						
							|  |  |  | log "authority: $authority" | 
					
						
							|  |  |  | log "user: $user" | 
					
						
							|  |  |  | log "host: $host" | 
					
						
							|  |  |  | log "path: $path" | 
					
						
							|  |  |  | log "schroot_name: $schroot_name" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exec_prefix="" | 
					
						
							|  |  |  | copy_prefix="" | 
					
						
							|  |  |  | if [ -n "$host" ]; then | 
					
						
							|  |  |  |    # we are working on a remote host | 
					
						
							|  |  |  |    exec_prefix="ssh -o User=$user -q $host" | 
					
						
							|  |  |  |    copy_prefix="scp -o User=$user -q" | 
					
						
							|  |  |  |    copy_destination_prefix="$host:" | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |    # working on local machine | 
					
						
							|  |  |  |    copy_prefix="cp" | 
					
						
							|  |  |  |    copy_destination_prefix="" | 
					
						
							|  |  |  | fi | 
					
						
							| 
									
										
										
										
											2012-05-29 09:29:01 +02:00
										 |  |  | log "exec_prefix: $exec_prefix" | 
					
						
							|  |  |  | log "copy_prefix: $copy_prefix" | 
					
						
							|  |  |  | log "copy_destination_prefix: $copy_destination_prefix" | 
					
						
							| 
									
										
										
										
											2011-11-08 13:29:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | case "$mode" in | 
					
						
							|  |  |  |    exec) | 
					
						
							| 
									
										
										
										
											2011-11-17 21:51:50 +01:00
										 |  |  |       # In exec mode the first argument is the __target_host which we already got from env. Get rid of it. | 
					
						
							|  |  |  |       shift | 
					
						
							|  |  |  |       code="$exec_prefix schroot -c $schroot_name -- sh -c '$@'" | 
					
						
							| 
									
										
										
										
											2011-11-08 13:29:11 +01:00
										 |  |  |    ;; | 
					
						
							|  |  |  |    copy) | 
					
						
							|  |  |  |       # get directory for given chroot_name | 
					
						
							| 
									
										
										
										
											2012-05-29 09:29:01 +02:00
										 |  |  |       schroot_directory="$($exec_prefix schroot -c $schroot_name --config | awk -F = '/directory=/ {print $2}')" | 
					
						
							| 
									
										
										
										
											2011-11-08 13:29:11 +01:00
										 |  |  |       [ -n "$schroot_directory" ] || die "Failed to retreive schroot directory for schroot: $schroot_name" | 
					
						
							| 
									
										
										
										
											2012-05-29 09:29:01 +02:00
										 |  |  |       log "schroot_directory: $schroot_directory" | 
					
						
							| 
									
										
										
										
											2011-11-08 13:29:11 +01:00
										 |  |  |       # prefix destination with chroot | 
					
						
							|  |  |  |       code="$copy_prefix $(echo "$@" | sed "s|$uri:|${copy_destination_prefix}${schroot_directory}|g")" | 
					
						
							|  |  |  |    ;; | 
					
						
							|  |  |  |    *) die "Unknown mode: $mode";; | 
					
						
							|  |  |  | esac | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | log "code: $code" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Run the code | 
					
						
							|  |  |  | $code | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | log "-----" |