Skip to content

Instantly share code, notes, and snippets.

@marcransome
Last active October 29, 2015 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcransome/75cc25c48a55f00ca420 to your computer and use it in GitHub Desktop.
Save marcransome/75cc25c48a55f00ca420 to your computer and use it in GitHub Desktop.
Box packing script for Vagrant VMWare plugin.
#!/bin/bash
#
# Copyright (c) 2015 Marc Ransome <marc.ransome@fidgetbox.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
program_name=`basename "$0"`
number_args="$#"
metadata="{
\"provider\" : \"vmware_fusion\"
}"
# --FUNCTIONS------------------------------------------------------------------
function usage {
echo "usage: $program_name vmwarevm_path target_box"
}
function log {
local message=$1
echo $message
}
function bail_on_failure {
local status=$1
local error_message=$2
if [ $status -ne 0 ]; then
log "$error_message"
exit 1
fi
}
function bail_on_success {
local status=$1
local error_message=$2
if [ $1 -eq 0 ]; then
log "$error_message"
exit 1
fi
}
function package {
local vmwarevm=$(echo $1 | sed 's/\/*$//g')
local box=$2
if [ ! -e "$vmwarevm/metadata.json" ]; then
log "--> creating metadata.json"
touch "$vmwarevm/metadata.json" >/dev/null 2>&1
bail_on_failure "$?" "Cannot create metadata.json"
echo "$metadata" > "$vmwarevm"/metadata.json 2>/dev/null
bail_on_failure "$?" "Cannot create metadata.json"
else
log "--> found metadata.json"
fi
log "--> archiving data files"
cd "$vmwarevm" >/dev/null 2>&1
bail_on_failure "$?" "Unable to cd to vmwarevm directory"
find . \( -name '*.nvram' -or -name '*.vmx' -or -name '*.vmxf' -or -name '*.vmdk' -or -name '*.vmsn' -or -name 'metadata.json' \) -print0 | xargs -0 tar -czf "$box" >/dev/null 2>&1
bail_on_failure "$?" "Unable to create archive"
}
function check_locks {
local vmwarevm=$1
ls "$vmwarevm"/*.lck >/dev/null 2>&1
bail_on_success "$?" "Lock file(s) exist (VM running?). Unable to proceed."
}
# --MAIN-----------------------------------------------------------------------
if [ $number_args -ne 2 ]; then
usage
exit 1
fi
check_locks "$1"
log "Packing box.."
package "$1" "$2"
log "Done."
# -----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment