Browse Source
[__rsync] rewrite See merge request ungleich-public/cdist!1007evilham-compatibility-fixes
7 changed files with 134 additions and 162 deletions
@ -1,41 +1,104 @@
|
||||
#!/bin/sh -e |
||||
# |
||||
# 2015 Dominique Roux (dominique.roux4 at gmail.com) |
||||
# |
||||
# 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/>. |
||||
# |
||||
|
||||
source=$(cat "$__object/parameter/source") |
||||
remote_user=$(cat "$__object/parameter/remote-user") |
||||
if ! command -v rsync > /dev/null |
||||
then |
||||
echo 'rsync is missing in local machine' >&2 |
||||
exit 1 |
||||
fi |
||||
|
||||
src="$( cat "$__object/parameter/source" )" |
||||
|
||||
if [ -f "$__object/parameter/destination" ]; then |
||||
destination=$(cat "$__object/parameter/destination") |
||||
if [ ! -e "$src" ] |
||||
then |
||||
echo "$src not found" >&2 |
||||
exit 1 |
||||
fi |
||||
|
||||
if [ -f "$__object/parameter/destination" ] |
||||
then |
||||
dst="$( cat "$__object/parameter/destination" )" |
||||
else |
||||
destination="/$__object_id" |
||||
dst="/$__object_id" |
||||
fi |
||||
|
||||
set -- |
||||
if [ -f "$__object/parameter/rsync-opts" ]; then |
||||
while read -r opts; do |
||||
set -- "$@" "--$opts" |
||||
done < "$__object/parameter/rsync-opts" |
||||
# if source is directory, then make sure that |
||||
# source and destination are ending with slash, |
||||
# because this is what you almost always want when |
||||
# rsyncing two directories. |
||||
|
||||
if [ -d "$src" ] |
||||
then |
||||
if ! echo "$src" | grep -Eq '/$' |
||||
then |
||||
src="$src/" |
||||
fi |
||||
|
||||
if ! echo "$dst" | grep -Eq '/$' |
||||
then |
||||
dst="$dst/" |
||||
fi |
||||
fi |
||||
|
||||
remote_user="$( cat "$__object/parameter/remote-user" )" |
||||
|
||||
options="$( cat "$__object/parameter/options" )" |
||||
|
||||
if [ -f "$__object/parameter/option" ] |
||||
then |
||||
while read -r l |
||||
do |
||||
# there's a limitation in argparse: value can't begin with '-'. |
||||
# to workaround this, let's prefix opts with '\' in manifest and remove here. |
||||
# read more about argparse issue: https://bugs.python.org/issue9334 |
||||
|
||||
options="$options $( echo "$l" | sed 's/\\//g' )" |
||||
done \ |
||||
< "$__object/parameter/option" |
||||
fi |
||||
|
||||
if [ -f "$__object/parameter/owner" ] || [ -f "$__object/parameter/group" ] |
||||
then |
||||
options="$options --chown=" |
||||
|
||||
if [ -f "$__object/parameter/owner" ] |
||||
then |
||||
owner="$( cat "$__object/parameter/owner" )" |
||||
options="$options$owner" |
||||
fi |
||||
|
||||
if [ -f "$__object/parameter/group" ] |
||||
then |
||||
group="$( cat "$__object/parameter/group" )" |
||||
options="$options:$group" |
||||
fi |
||||
fi |
||||
|
||||
if [ -f "$__object/parameter/mode" ] |
||||
then |
||||
mode="$( cat "$__object/parameter/mode" )" |
||||
options="$options --chmod=$mode" |
||||
fi |
||||
|
||||
# IMPORTANT |
||||
# |
||||
# 1. we first dry-run rsync with change summary to find out |
||||
# if there are any changes and code generation is needed. |
||||
# 2. normally, to get current state or target host, we run |
||||
# such operations in type explorers, but that's not |
||||
# possible due to how rsync works. |
||||
# 3. redirecting output of dry-run to stderr to ease debugging. |
||||
# 4. to understand how that cryptic regex works, please |
||||
# open rsync manpage and read about --itemize-changes. |
||||
|
||||
export RSYNC_RSH="$__remote_exec" |
||||
|
||||
# shellcheck disable=SC2086 |
||||
echo rsync -a \ |
||||
--no-owner --no-group \ |
||||
-e \"${__remote_exec}\" \ |
||||
-q "$@" "${source}/" "${remote_user}@${__target_host}:${destination}" |
||||
if ! rsync --dry-run --itemize-changes $options "$src" "$remote_user@$__target_host:$dst" \ |
||||
| grep -E '^(<|>|c|h|\.|\*)[fdL][cstTpogunbax\.\+\?]+\s' >&2 |
||||
then |
||||
exit 0 |
||||
fi |
||||
|
||||
echo "export RSYNC_RSH='$__remote_exec'" |
||||
|
||||
echo "rsync $options $src $remote_user@$__target_host:$dst" |
||||
|
@ -1,37 +0,0 @@
|
||||
#!/bin/sh -e |
||||
# |
||||
# 2015 Dominique Roux (dominique.roux4 at gmail.com) |
||||
# |
||||
# 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/>. |
||||
# |
||||
|
||||
if [ -f "$__object/parameter/destination" ]; then |
||||
destination=$(cat "$__object/parameter/destination") |
||||
else |
||||
destination="/$__object_id" |
||||
fi |
||||
|
||||
ownergroup="" |
||||
if [ -f "$__object/parameter/owner" ]; then |
||||
ownergroup=$(cat "$__object/parameter/owner") |
||||
fi |
||||
if [ -f "$__object/parameter/group" ]; then |
||||
ownergroup="${ownergroup}:$(cat "$__object/parameter/group")" |
||||
fi |
||||
|
||||
if [ "$ownergroup" ]; then |
||||
echo chown -R "$ownergroup" "$destination" |
||||
fi |
@ -1,21 +1,3 @@
|
||||
#!/bin/sh -e |
||||
# |
||||
# 2015 Dominique Roux (dominique.roux4 at gmail.com) |
||||
# |
||||
# 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/>. |
||||
# |
||||
|
||||
__package rsync |
||||
|
@ -0,0 +1 @@
|
||||
--recursive --links --perms --times |
@ -1,4 +1,6 @@
|
||||
destination |
||||
owner |
||||
group |
||||
mode |
||||
options |
||||
owner |
||||
remote-user |
||||
|
Loading…
Reference in new issue