Skip to content

Commit

Permalink
Update to the cookbook syncer (#244)
Browse files Browse the repository at this point in the history
Summary:
The syncer would silently overwrite your cli or env options
with a config file in a way that was confusing.

This properly handles an expected hierarchy of:
default -> config -> env -> cli

Also, the vim modeline was set to 2-space indentation, but the code
was 4-space, so update the code to match the vim modeline.

Signed-off-by: Phil Dibowitz <phil@ipom.com>

Pull Request resolved: #244

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

Note: we don't use this script internally, it's for the benefit of our downstream open-source users (like Phil)

Differential Revision: D65949355

fbshipit-source-id: e49c3f519bdc0e80dfd3fa0c492139775ac86ed1
  • Loading branch information
jaymzh authored and facebook-github-bot committed Dec 5, 2024
1 parent aeb3bba commit 84dd137
Showing 1 changed file with 108 additions and 75 deletions.
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

0 comments on commit 84dd137

Please sign in to comment.