diff --git a/src/mad/sh/scripts_common.sh b/src/mad/sh/scripts_common.sh index 8de8df234d..a3a4254b21 100755 --- a/src/mad/sh/scripts_common.sh +++ b/src/mad/sh/scripts_common.sh @@ -40,6 +40,21 @@ WGET=wget # Used for log messages SCRIPT_NAME=`basename $0` +# ------------------------------------------------------------------------------ +# Path manipulation functions +# ------------------------------------------------------------------------------ + +# Takes out unneeded slashes. Repeated and final directory slashes: +# /some//path///somewhere/ -> /some/path/somewhere +function fix_dir_slashes +{ + dirname "$1/file" | $SED 's/\/+/\//g' +} + +# ------------------------------------------------------------------------------ +# Log functions +# ------------------------------------------------------------------------------ + # Formats date for logs function log_date { @@ -108,8 +123,6 @@ function exec_and_log log "Executed \"$1\"." } - - # Like exec_and_log but the first argument is the number of seconds # before here is timeout and kills the command # diff --git a/src/tm_mad/shared/clone b/src/tm_mad/shared/clone index 5cc373b86e..e2453c2d97 100755 --- a/src/tm_mad/shared/clone +++ b/src/tm_mad/shared/clone @@ -16,6 +16,12 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +# clone fe:SOURCE host:remote_system_ds/disk.i size +# - fe is the front-end hostname +# - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk +# - host is the target host to deploy the VM +# - remote_system_ds is the path for the system datastore in the host + SRC=$1 DST=$2 @@ -27,35 +33,35 @@ fi . $TMCOMMON -get_vmdir - +#------------------------------------------------------------------------------- +# Set dst path and dir +#------------------------------------------------------------------------------- SRC_PATH=`arg_path $SRC` DST_PATH=`arg_path $DST` -fix_paths +set_ds_location -log_debug "$1 $2" -log_debug "DST: $DST_PATH" +REL_DST_PATH=${DS_PATH##"$DS_LOCATION/"} +DST_PATH="$ONE_LOCAL_VAR/datastore/$REL_DST_PATH" DST_DIR=`dirname $DST_PATH` -log "Creating directory $DST_DIR" -exec_and_log "mkdir -p $DST_DIR" -exec_and_log "chmod a+w $DST_DIR" +if [ ! -d $DST_DIR ]; then + log "Creating directory $DST_DIR" + exec_and_log "mkdir -p $DST_DIR" +fi +#------------------------------------------------------------------------------- +# Clone (cp) SRC into DST +#------------------------------------------------------------------------------- case $SRC in http://*) - log "Downloading $SRC" - exec_and_log "$WGET -O $DST_PATH $SRC" \ - "Error downloading $SRC" + log "Downloading $SRC into $DST_PATH" + exec_and_log "$WGET -O $DST_PATH $SRC" "Error downloading $SRC" ;; *) - log "Cloning $SRC_PATH" - exec_and_log "cp -r $SRC_PATH $DST_PATH" \ - "Error copying $SRC to $DST" + log "Cloning $SRC_PATH in $DST_PATH" + exec_and_log "cp -r $SRC_PATH $DST_PATH" "Error copying $SRC to $DST" ;; esac - -exec_and_log "chmod a+rw $DST_PATH" - diff --git a/src/tm_mad/shared/ln b/src/tm_mad/shared/ln index 056fd4bc0c..07ddc88544 100755 --- a/src/tm_mad/shared/ln +++ b/src/tm_mad/shared/ln @@ -16,6 +16,12 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +# ln fe:SOURCE host:remote_system_ds/disk.i size +# - fe is the front-end hostname +# - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk +# - host is the target host to deploy the VM +# - remote_system_ds is the path for the system datastore in the host + SRC=$1 DST=$2 @@ -27,20 +33,32 @@ fi . $TMCOMMON -get_vmdir - +#------------------------------------------------------------------------------- +# Set dst path and dir +#------------------------------------------------------------------------------- SRC_PATH=`arg_path $SRC` DST_PATH=`arg_path $DST` -fix_dst_path +set_ds_location +REL_DST_PATH=${DS_PATH##"$DS_LOCATION/"} +REL_SRC_PATH=${SRC_PATH##"$ONE_LOCAL_VAR/datastores/"} + +DST_PATH="$ONE_LOCAL_VAR/datastore/$REL_DST_PATH" DST_DIR=`dirname $DST_PATH` -log "Creating directory $DST_DIR" -exec_and_log "mkdir -p $DST_DIR" \ - "Could not create directory $DST_DIR" -exec_and_log "chmod a+w $DST_DIR" +if [ ! -d $DST_DIR ]; then + log "Creating directory $DST_DIR" + exec_and_log "mkdir -p $DST_DIR" +fi -log "Link $SRC_PATH" -exec_and_log "ln -s $SRC_PATH $DST_PATH" +DST_FILE=`basename $DST_DIR` +#------------------------------------------------------------------------------- +# Link (ln) SRC into DST +#------------------------------------------------------------------------------- + +log "Linking $SRC_PATH in $DST_PATH" + +exec_and_log "cd $DST_DIR; ln -s ../$REL_SRC_PATH ./$DST_FILE" \ + "Error linking $SRC to $DST" diff --git a/src/tm_mad/tm_common.sh b/src/tm_mad/tm_common.sh index b5d204afe8..f5d045ec4c 100644 --- a/src/tm_mad/tm_common.sh +++ b/src/tm_mad/tm_common.sh @@ -16,6 +16,13 @@ export LANG=C +ONE_SH=$ONE_LIB/sh + +. $ONE_SH/scripts_common.sh + +# ------------------------------------------------------------------------------ +# Set enviroment for the tm drivers (bash-based) +# ------------------------------------------------------------------------------ if [ -z "$ONE_LOCATION" ]; then ONE_LOCAL_VAR=/var/lib/one ONE_LIB=/usr/lib/one @@ -24,76 +31,15 @@ else ONE_LIB=$ONE_LOCATION/lib fi -ONE_SH=$ONE_LIB/sh - -. $ONE_SH/scripts_common.sh - - - if [ "x$(uname -s)" = "xLinux" ]; then SED="$SED -r" else SED="/usr/bin/sed -E" fi -function get_vmdir -{ - VMDIR=`grep '^VM_DIR=' $ONE_LOCAL_VAR/config | cut -d= -f2` - fix_var_slashes -} - -# Takes out uneeded slashes. Repeated and final directory slashes: -# /some//path///somewhere/ -> /some/path/somewhere -function fix_dir_slashes -{ - dirname "$1/file" | $SED 's/\/+/\//g' -} - -function get_compare_target -{ - echo "$1" | $SED 's/\/+/\//g' | $SED 's/\/images$//' -} - -function full_src_and_dst_equal -{ - s=`get_compare_target "$SRC"` - d=`get_compare_target "$DST"` - - [ "$s" == "$d" ] - -} - -function fix_var_slashes -{ - ONE_LOCAL_VAR=`fix_dir_slashes "$ONE_LOCAL_VAR"` - VMDIR=`fix_dir_slashes "$VMDIR"` -} - -function fix_paths -{ - if [ "x$ONE_LOCAL_VAR" != "x$VMDIR" ]; then - SRC_PATH=`fix_dir_slashes "$SRC_PATH"` - SRC_PATH=${SRC_PATH/$VMDIR/$ONE_LOCAL_VAR} - DST_PATH=`fix_dir_slashes "$DST_PATH"` - DST_PATH=${DST_PATH/$VMDIR/$ONE_LOCAL_VAR} - fi -} - -function fix_src_path -{ - if [ "x$ONE_LOCAL_VAR" != "x$VMDIR" ]; then - SRC_PATH=`fix_dir_slashes "$SRC_PATH"` - SRC_PATH=${SRC_PATH/$VMDIR/$ONE_LOCAL_VAR} - fi -} - -function fix_dst_path -{ - if [ "x$ONE_LOCAL_VAR" != "x$VMDIR" ]; then - DST_PATH=`fix_dir_slashes "$DST_PATH"` - DST_PATH=${DST_PATH/$VMDIR/$ONE_LOCAL_VAR} - fi -} +# ------------------------------------------------------------------------------ +# Function to get hosts and paths from arguments +# ------------------------------------------------------------------------------ # Gets the host from an argument function arg_host @@ -107,5 +53,9 @@ function arg_path echo $1 | $SED 's/^[^:]*:(.*)$/\1/' } - - +#Return the DATASTORE_LOCATION from OpenNebula configuration +function set_ds_location +{ + DS_LOCATION=`grep '^DATASTORE_LOCATION=' $ONE_LOCAL_VAR/config | cut -d= -f2` + DS_LOCATION=`fix_var_slashes $DS_LOCATION` +}