Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to the cookbook syncer #244

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 108 additions & 75 deletions scripts/sync_fb_cookbooks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,50 @@

set -u

# constants
CONF="$HOME/.config/sync_fb_cookbooks.conf"

# defaults
upstreamdir="$HOME/src/chef-cookbooks"
internaldir="$HOME/src/chef"
config="$HOME/.config/sync_fb_cookbooks.conf"
default_upstreamdir="$HOME/src/chef-cookbooks"
default_internaldir="$HOME/src/chef"

# methods
error() {
echo "ERROR: $*"
echo "ERROR: $*"
}

warn() {
echo "WARNING: $*"
echo "WARNING: $*"
}

info() {
echo "INFO: $*"
}

die() {
error "$@"
exit 1
error "$@"
exit 1
}

ourhelp() {
cat <<EOF
cat <<EOF
Usage: $0 [<options>]

OPTIONS
-c <cookbook>
Compare just <cookbook>

-C <config_file>
Path to config file
Default: $config

-d
Diff mode.
Diff mode

-h
This.
This

-i <dir>
Where your clone of the INTERNAL repo is.
Default: $internaldir
Where your clone of the INTERNAL repo is
Default: $default_internaldir

-p
Push changes internal -> upstream
Expand All @@ -67,11 +73,11 @@ OPTIONS
Sync changes upstream -> internal

-u <dir>
Where your clone of the UPSTREAM repo is.
Default: $internaldir
Where your clone of the UPSTREAM repo is
Default: $default_internaldir

CONFIG FILE
You can also tell this script where your repos are by creating $CONF
You can also tell this script where your repos are by creating $config
in shell format and defining 'upstreamdir' and 'internaldir', like so:

upstreamdir="\$HOME/mycheckouts/chef-cookbooks"
Expand All @@ -94,75 +100,102 @@ GENERAL USAGE

# OR.... push out internal changes
$ $0 -c fb_sysfs -p

EOF
}

# people put repos in different places, don't make them pass in
# -u and -i every time.
if [ -e "$CONF" ]; then
# shellcheck disable=SC1090
source "$CONF" || die "Configuration file $CONF malformed"
fi

mode=''
cookbook=''
while getopts c:dhi:psu: opt; do
case $opt in
c)
cookbook="$OPTARG"
;;
d)
mode='diff'
;;
h)
ourhelp
exit
;;
i)
internaldir="$OPTARG"
;;
p)
mode='push'
;;
s)
mode='sync'
;;
u)
upstreamdir="$OPTARG"
;;
?)
ourhelp
exit 1
;;
esac
while getopts 'c:C:dhi:psu:' opt; do
case $opt in
c)
cookbook="$OPTARG"
;;
C)
config="$OPTARG"
;;
d)
mode='diff'
;;
h)
ourhelp
exit
;;
i)
internaldir="$OPTARG"
;;
p)
mode='push'
;;
s)
mode='sync'
;;
u)
upstreamdir="$OPTARG"
;;
?)
ourhelp
exit 1
;;
esac
done

# save what was passed in either through env
# or command-line...
save_internaldir=""
save_upstreamdir=""
if [ -n "$internaldir" ]; then
save_internaldir="$internaldir"
fi
if [ -n "$upstreamdir" ]; then
save_upstreamdir="$upstreamdir"
fi

# initialize with defaults
internaldir="$default_internaldir"
upstreamdir="$default_upstreamdir"

# now read the config file, if we have one
if [ -e "$config" ]; then
# shellcheck disable=SC1090
info "Loading config from $config"
source "$config" || die "Configuration file $config malformed"
fi

# Now merge in passed-in config
if [ -n "$save_internaldir" ]; then
internaldir="$save_internaldir"
fi
if [ -n "$save_upstreamdir" ]; then
upstreamdir="$save_upstreamdir"
fi

info "Using upstream: $upstreamdir | internal: $internaldir"

if [ -z "$mode" ]; then
if [ -n "$cookbook" ]; then
mode='diff'
else
mode='status'
fi
if [ -n "$cookbook" ]; then
mode='diff'
else
mode='status'
fi
fi

cd "$internaldir/cookbooks" || die "where am I?"
[ -z "$cookbook" ] && cookbook="$(ls -d fb_*)"
for cb in $cookbook; do
ours="$cb/"
upstream="$upstreamdir/cookbooks/$cb/"
if [ "$cb" = 'fb_init' ]; then
upstream="$upstreamdir/cookbooks/fb_init_sample/"
fi
if [ "$mode" = 'status' ]; then
diff -Nru "$ours" "$upstream" &>/dev/null || echo "$cb does not match"
elif [ "$mode" = 'diff' ]; then
diff -Nru "$ours" "$upstream"
elif [ "$mode" = 'push' ]; then
rsync -avz "$ours" "$upstream"
elif [ "$mode" = 'sync' ]; then
rsync -avz "$upstream" "$ours"
else
die "wut? wut mode is '$mode'"
fi
ours="$cb/"
upstream="$upstreamdir/cookbooks/$cb/"
if [ "$cb" = 'fb_init' ]; then
upstream="$upstreamdir/cookbooks/fb_init_sample/"
fi
if [ "$mode" = 'status' ]; then
diff -Nru "$ours" "$upstream" &>/dev/null || echo "$cb does not match"
elif [ "$mode" = 'diff' ]; then
diff -Nru "$ours" "$upstream"
elif [ "$mode" = 'push' ]; then
rsync -avz "$ours" "$upstream"
elif [ "$mode" = 'sync' ]; then
rsync -avz "$upstream" "$ours"
else
die "wut? wut mode is '$mode'"
fi
done
Loading