This is an automated email from the ASF dual-hosted git repository.

dkonrad pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/master by this push:
     new 6946f41  Fix template size for managed storage / refactor 
cloud-install-sys-tmplt and createtmplt.sh (#3346)
6946f41 is described below

commit 6946f41784423788e71441255e67c2b9b1c3633d
Author: Sven Vogel <s.vo...@ewerk.com>
AuthorDate: Mon Jun 3 09:30:45 2019 +0200

    Fix template size for managed storage / refactor cloud-install-sys-tmplt 
and createtmplt.sh (#3346)
    
    * refactor cloud-install-sys-tmplt and createtmplt.sh
    
    * move qemu-img check to kvm / add more comments if qemu-img not available 
/ set virtual size only for qcow2
    
    * add comments to the failed and get options block / rename vars in failed 
block / typo Fix
    
    *  add comments to the failed and get options block / rename vars in failed 
block / typo Fix to cloudtmplt.sh
---
 scripts/storage/secondary/cloud-install-sys-tmplt | 298 +++++++++++-----------
 scripts/storage/secondary/createtmplt.sh          | 184 ++++++-------
 2 files changed, 218 insertions(+), 264 deletions(-)

diff --git a/scripts/storage/secondary/cloud-install-sys-tmplt 
b/scripts/storage/secondary/cloud-install-sys-tmplt
index 91b3a7c..6bb8d20 100755
--- a/scripts/storage/secondary/cloud-install-sys-tmplt
+++ b/scripts/storage/secondary/cloud-install-sys-tmplt
@@ -1,5 +1,4 @@
 #!/bin/bash
-# $Id: installrtng.sh 11251 2010-07-23 23:40:44Z abhishek $ $HeadURL: 
svn://svn.lab.vmops.com/repos/vmdev/java/scripts/storage/secondary/installrtng.sh
 $
 
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
@@ -20,19 +19,35 @@
 
 
 usage() {
-  printf "Usage: %s: -m <secondary storage mount point> -f <system vm template 
file> [-h <hypervisor name: kvm|vmware|xenserver|hyperv|ovm3> ] [ -s <mgmt 
server secret key, if you specified any when running cloudstack-setup-database, 
default is password>][-u <Url to system vm template>] [-F <clean up system 
templates of specified hypervisor>] [-e <Template suffix, e.g vhd, ova, qcow2>] 
[-o <Database server hostname or ip, e.g localhost>] [-r <Database user name, 
e.g root>] [-p <mysql dat [...]
-  printf "or\n" >&2
-  printf "%s: -m <secondary storage mount point> -u <http url for system vm 
template> [-h <hypervisor name: kvm|vmware|xenserver|hyperv|ovm3> ] [ -s <mgmt 
server secret key>]\n" $(basename $0) >&2
+  printf "\nUsage: %s:\n\t-m secondary storage mount point\n\t-f system vm 
template file\n\t-h hypervisor name: kvm|vmware|xenserver|hyperv|ovm3\n\t-s 
mgmt server secret key, if you specified any when running 
cloudstack-setup-database, default is password\n\t-u Url to system vm 
template\n\t-F clean up system templates of specified hypervisor\n\t-e Template 
suffix, e.g vhd, ova, qcow2\n\t-o Database server hostname or ip, e.g 
localhost\n\t-r Database user name, e.g root\n\t-p mysql databa [...]
+  printf "\tor\n"
+  printf "\nUsage: %s:\n\t-m secondary storage mount point\n\t-u http url for 
system vm template\n\t-h hypervisor name: 
kvm|vmware|xenserver|hyperv|ovm3\n\t-s mgmt server secret key\n\n" $(basename 
$0) >&2
 }
 
+# Usage: e.g. failed $? "this is an error"
 failed() {
-  echo "Installation failed"
-  exit $1
+  local returnval=$1
+  local returnmsg=$2
+  
+  # check for an message, if there is no one dont print anything
+  if [[ -z $returnmsg ]]; then
+    :
+  else
+    echo -e $returnmsg
+  fi
+  if [[ $returnval -eq 0 ]]; then
+    return 0
+  else
+    echo "Installation failed"
+    exit $returnval
+  fi
 }
+
 #set -x
 mflag=
 fflag=
 ext="vhd"
+hvm=false
 templateId=
 hyper=
 msKey=password
@@ -42,6 +57,15 @@ dbUser="root"
 dbPassword=
 dbPort=3306
 jasypt='/usr/share/cloudstack-common/lib/jasypt-1.9.2.jar'
+tmpldescr='SystemVM Template'
+
+# check if first parameter is not a dash (-) then print the usage block
+if [[ ! $@ =~ ^\-.+ ]]; then
+       usage
+       exit 0
+fi
+
+OPTERR=0
 while getopts 'm:h:f:u:Ft:e:s:o:r:d:p:'# OPTION
 do
   case $OPTION in
@@ -62,6 +86,9 @@ do
         ;;
   h)    hyper="$OPTARG"
         ;;
+  H)    Hflag=1
+        hvm="true"
+        ;;
   s)    sflag=1
         msKey="$OPTARG"
         ;;
@@ -78,121 +105,98 @@ do
         dbPort="$OPTARG"
         ;;
   ?)    usage
-        failed 2
+        exit 0
+        ;;
+  *)    usage
+        exit 0
         ;;
   esac
 done
 
-if [[ "$mflag$fflag" != "11"  && "$mflag$uflag" != "11" ]]
-then
-  usage
-  failed 2
+if [[ "$mflag$fflag" != "11" && "$mflag$uflag" != "11" ]]; then
+  failed 2 "Please add a mount point and a system vm template file"
 fi
 
-if [ -z "$hyper" ]
-then
-  usage
-  failed 2
+if [[ -z "$hyper" ]]; then
+  failed 2 "Please add a correct hypervisor name like: 
kvm|vmware|xenserver|hyperv|ovm3"
 fi
 
-if [ ! -d $mntpoint ]
-then
-  echo "mount point $mntpoint doesn't exist\n"
-  failed 4
+if [[ ! -d $mntpoint ]]; then
+  failed 2 "mount point $mntpoint doesn't exist\n"
 fi
 
-if [[ "$fflag" == "1" && ! -f $tmpltimg ]]
-then
-  echo "template image file $tmpltimg doesn't exist"
-  failed 3
+if [[ "$fflag" == "1" && ! -f $tmpltimg ]]; then
+  failed 2 "template image file $tmpltimg doesn't exist"
 fi
 
-if [ -f /etc/cloudstack/management/db.properties ]
-then
-    if [ "$pflag" != 1 ]
-    then
-        dbPort=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.port'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//')
-    fi
+if [[ -f /etc/cloudstack/management/db.properties ]]; then
+  if [[ "$pflag" != 1 ]]; then
+    dbPort=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.port'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//')
+  fi
 
-    if [ "$oflag" != 1 ]
-    then
-        dbHost=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.host'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//')
-    fi
+  if [[ "$oflag" != 1 ]]; then
+    dbHost=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.host'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//')
+  fi
 
-    if [ "$rflag" != 1 ]
-    then
-        dbUser=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.username'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//')
-    fi
+  if [[ "$rflag" != 1 ]]; then
+    dbUser=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.username'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//')
+  fi
 
-    encType=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.encryption.type'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//')
-    if [ "$encType" == "file" ]
-    then
-        msKey=$(cat /etc/cloudstack/management/key)
-    elif [ "$encType" == "web" ]
-    then
-        if [ ! "$sflag" == "1" ]
-        then
-            echo "Encryption type web requires mgmt secret key using -s option"
-            failed 9
-        fi
+  encType=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.encryption.type'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//')
+  if [[ "$encType" == "file" ]]; then
+    msKey=$(cat /etc/cloudstack/management/key)
+  elif [[ "$encType" == "web" ]]; then
+    if [[ ! "$sflag" == "1" ]]; then
+      failed 2 "Encryption type web requires mgmt secret key using -s option"
     fi
+  fi
 
-    if [[ "$encType" == "file" || "$encType" == "web" ]]
-    then
-        encPassword=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | 
grep 'db.cloud.password'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//'i | sed 's/^ENC(\(.*\))/\1/')
-        if [ ! $encPassword == "" ]
-        then
-            dbPassword=(`java -classpath $jasypt 
org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI decrypt.sh input=$encPassword 
password=$msKey verbose=false`)
-            if [ ! $dbPassword ]
-            then
-                echo "Failed to decrypt DB password from db.properties"
-                failed 9
-            fi
-        fi
-    else
-        if [ "$dflag" != 1 ]
-        then
-            dbPassword=$(sed '/^\#/d' /etc/cloudstack/management/db.properties 
| grep 'db.cloud.password'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//'i )
-        fi
+  if [[ "$encType" == "file" || "$encType" == "web" ]]; then
+    encPassword=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | grep 
'db.cloud.password'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//'i | sed 's/^ENC(\(.*\))/\1/')
+    if [[ ! $encPassword == "" ]]; then
+      dbPassword=(`java -classpath $jasypt 
org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI decrypt.sh input=$encPassword 
password=$msKey verbose=false`)
+      if [[ ! $dbPassword ]]; then
+        failed 2 "Failed to decrypt DB password from db.properties"
+      fi
+    fi
+  else
+    if [[ "$dflag" != 1 ]]; then
+      dbPassword=$(sed '/^\#/d' /etc/cloudstack/management/db.properties | 
grep 'db.cloud.password'  | tail -n 1 | cut -d "=" -f2- | sed 
's/^[[:space:]]*//;s/[[:space:]]*$//'i )
     fi
+  fi
 fi
 
-if [ "$templateId" == "" ]
-then
-   if [ "$hyper" == "kvm" ]
-   then
-      ext="qcow2"
-      templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"KVM\" and 
removed is null"`)
-   elif [ "$hyper" == "xenserver" ]
-   then
-      ext="vhd"
-      templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"XenServer\" 
and removed is null"`)
-   elif [ "$hyper" == "vmware" ]
-   then
-      ext="ova"
-      templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"VMware\" and 
removed is null"`)
-   elif [ "$hyper" == "lxc" ]
-   then
-      ext="qcow2"
-      templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"LXC\" and 
removed is null"`)
-   elif [ "$hyper" == "hyperv" ]
-   then
-      ext="vhd"
-      templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"Hyperv\" and 
removed is null"`)
-   elif [ "$hyper" == "ovm3" ]
-   then
-      ext="raw"
-      templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"Ovm3\" and 
removed is null"`)
-   else
-      usage
-      failed 2
-   fi
+if [[ "$templateId" == "" ]]; then
+  if [[ "$hyper" == "kvm" ]]; then
+    ext="qcow2"
+    templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"KVM\" and 
removed is null"`)
+    if [[ $(which qemu-img) ]]; then
+      qemuimgcmd=$(which qemu-img)
+    else
+      failed 2 "Please install qemu-img: command\non CentOS run \"yum install 
qemu-img\"\non Ubuntu/Debian run \"apt-get install qemu-utils\""
+    fi
+  elif [[ "$hyper" == "xenserver" ]]; then
+    ext="vhd"
+    templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"XenServer\" 
and removed is null"`)
+  elif [[ "$hyper" == "vmware" ]]; then
+    ext="ova"
+    templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"VMware\" and 
removed is null"`)
+  elif [[ "$hyper" == "lxc" ]]; then
+    ext="qcow2"
+    templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"LXC\" and 
removed is null"`)
+  elif [[ "$hyper" == "hyperv" ]]; then
+    ext="vhd"
+    templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"Hyperv\" and 
removed is null"`)
+  elif [[ "$hyper" == "ovm3" ]]; then
+    ext="raw"
+    templateId=(`mysql -P $dbPort -h $dbHost --user=$dbUser 
--password=$dbPassword --skip-column-names -U cloud -e "select max(id) from 
cloud.vm_template where type = \"SYSTEM\" and hypervisor_type = \"Ovm3\" and 
removed is null"`)
+  else
+    failed 2 "Please add a correct hypervisor name like: 
kvm|vmware|xenserver|hyperv|ovm3"
+  fi
 fi
 
-if [ ! $templateId ]
-then
-    echo "Unable to get template Id from database"
-    failed 8
+if [[ ! $templateId ]]; then
+  failed 2 "Unable to get template Id from database"
 fi
 
 _uuid=$(uuidgen)
@@ -205,43 +209,31 @@ mntpoint=`echo "$mntpoint" | sed 's|/*$||'`
 destdir=$mntpoint/template/tmpl/1/$templateId/
 
 mkdir -p $destdir
-if [ $? -ne 0 ]
-then
-  printf "Failed to write to mount point $mntpoint -- is it mounted?\n"
-  failed 3
+if [[ $? -ne 0 ]]; then
+  failed 2 "Failed to write to mount point $mntpoint -- is it mounted?\n"
 fi
 
-if [ "$Fflag" == "1" ]
-then
+if [[ "$Fflag" == "1" ]]; then
   rm -rf $destdir/*
-  if [ $? -ne 0 ]
-  then
-    echo "Failed to clean up template directory $destdir -- check permissions?"
-    failed 2
+  if [[ $? -ne 0 ]]; then
+    failed 2 "Failed to clean up template directory $destdir -- check 
permissions?"
   fi
 fi
 
-if [ -f $destdir/template.properties ]
-then
-  echo "Data already exists at destination $destdir -- use -F to force cleanup 
of old template"
-  echo "IF YOU ARE ATTEMPTING AN UPGRADE, YOU MAY NEED TO SPECIFY A TEMPLATE 
ID USING THE -t FLAG"
-  failed 4
+if [[ -f $destdir/template.properties ]]; then
+  failed 2 "Data already exists at destination $destdir -- use -F to force 
cleanup of old template\nIF YOU ARE ATTEMPTING AN UPGRADE, YOU MAY NEED TO 
SPECIFY A TEMPLATE ID USING THE -t FLAG"
 fi
 
-destvhdfiles=$(find $destdir -name \*.$ext)
-if [ "$destvhdfiles" != "" ]
-then
-  echo "Data already exists at destination $destdir -- use -F to force cleanup 
of old template"
-  failed 5
+destfiles=$(find $destdir -name \*.$ext)
+if [[ "$destfiles" != "" ]]; then
+  failed 2 "Data already exists at destination $destdir -- use -F to force 
cleanup of old template"
 fi
 
-tmpfile=$(dirname $0)/$localfile
+tmplfile=$(dirname $0)/$localfile
 
-touch $tmpfile
-if [ $? -ne 0 ]
-then
-  printf "Failed to create temporary file in directory $(dirname $0) -- is it 
read-only or full?\n"
-  failed 4
+touch $tmplfile
+if [[ $? -ne 0 ]]; then
+  failed 2 "Failed to create temporary file in directory $(dirname $0) -- is 
it read-only or full?\n"
 fi
 
 destcap=$(df -P $destdir | awk '{print $4}' | tail -1 )
@@ -250,51 +242,47 @@ destcap=$(df -P $destdir | awk '{print $4}' | tail -1 )
 localcap=$(df -P $(dirname $0) | awk '{print $4}' | tail -1 )
 [ $localcap -lt $DISKSPACE ] && echo "Insufficient free disk space for local 
temporary folder $(dirname $0): avail=${localcap}k req=${DISKSPACE}k" && failed 
4
 
-if [ "$uflag" == "1" ]
-then
-  wget -O $tmpfile $url
-  if [ $? -ne 0 ]
-  then
-    echo "Failed to fetch system vm template from $url"
-    failed 5
+if [[ "$uflag" == "1" ]]; then
+  wget -O $tmplfile $url
+  if [[ $? -ne 0 ]]; then
+    failed 2 "Failed to fetch system vm template from $url"
   fi
 fi
 
-
-if [ "$fflag" == "1" ]
-then
-  cp $tmpltimg $tmpfile
-  if [ $? -ne 0 ]
-  then
-    printf "Failed to create temporary file in directory $(dirname $0) -- is 
it read-only or full?\n"
-    failed 6
+if [[ "$fflag" == "1" ]]; then
+  cp $tmpltimg $tmplfile
+  if [[ $? -ne 0 ]]; then
+    failed 2 "Failed to create temporary file in directory $(dirname $0) -- is 
it read-only or full?\n"
   fi
 fi
 
+installrslt=$($(dirname $0)/createtmplt.sh -n $localfile -t $destdir -f 
$tmplfile)
 
-installrslt=$($(dirname $0)/createtmplt.sh -s 2 -d 'SystemVM Template' -n 
$localfile -t $destdir/ -f $tmpfile -u -v)
-
-if [ $? -ne 0 ]
-then
-  echo "Failed to install system vm template $tmpltimg to $destdir: 
$installrslt"
-  failed 7
-fi
-
-if [ "$ext" == "ova" ]
-then
-  tar xvf $destdir/$localfile -C $destdir &> /dev/null
+if [[ $? -ne 0 ]]; then
+  failed 2 "Failed to install system vm template $tmpltimg to $destdir: 
$installrslt"
 fi
 
 tmpltfile=$destdir/$localfile
-tmpltsize=$(ls -l $tmpltfile| awk -F" " '{print $5}')
+tmpltsize=$(ls -l $tmpltfile | awk -F" " '{print $5}')
+if [[ "$ext" == "qcow2" ]]; then
+  vrtmpltsize=$($qemuimgcmd info $tmpltfile | grep -i 'virtual size' | sed -ne 
's/.*(\([0-9]*\).*/\1/p' | xargs)
+else
+  vrtmpltsize=$tmpltsize
+fi
 
+touch $destdir/template.properties
+echo -n "" > $destdir/template.properties
+echo "filename=$localfile" > $destdir/template.properties
+echo "description=$tmpldescr" >> $destdir/template.properties
+echo "hvm=$hvm" >> $destdir/template.properties
+echo "size=$tmpltsize" >> $destdir/template.properties
 echo "$ext=true" >> $destdir/template.properties
 echo "id=$templateId" >> $destdir/template.properties
 echo "public=true" >> $destdir/template.properties
 echo "$ext.filename=$localfile" >> $destdir/template.properties
 echo "uniquename=routing-$templateId" >> $destdir/template.properties
-echo "$ext.virtualsize=$tmpltsize" >> $destdir/template.properties
-echo "virtualsize=$tmpltsize" >> $destdir/template.properties
+echo "$ext.virtualsize=$vrtmpltsize" >> $destdir/template.properties
+echo "virtualsize=$vrtmpltsize" >> $destdir/template.properties
 echo "$ext.size=$tmpltsize" >> $destdir/template.properties
 
-echo "Successfully installed system VM template $tmpltimg to $destdir"
+echo "Successfully installed system VM template $tmpltimg and 
template.properties to $destdir"
\ No newline at end of file
diff --git a/scripts/storage/secondary/createtmplt.sh 
b/scripts/storage/secondary/createtmplt.sh
index 4e8db46..8658608 100755
--- a/scripts/storage/secondary/createtmplt.sh
+++ b/scripts/storage/secondary/createtmplt.sh
@@ -1,4 +1,5 @@
-#!/usr/bin/env bash
+#!/bin/bash
+
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -6,9 +7,9 @@
 # to you under the Apache License, Version 2.0 (the
 # "License"); you may not use this file except in compliance
 # with the License.  You may obtain a copy of the License at
-# 
+#
 #   http://www.apache.org/licenses/LICENSE-2.0
-# 
+#
 # Unless required by applicable law or agreed to in writing,
 # software distributed under the License is distributed on an
 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -16,41 +17,31 @@
 # specific language governing permissions and limitations
 # under the License.
 
- 
-
-# $Id: createtmplt.sh 9132 2010-06-04 20:17:43Z manuel $ $HeadURL: 
svn://svn.lab.vmops.com/repos/vmdev/java/scripts/storage/secondary/createtmplt.sh
 $
-# createtmplt.sh -- install a template
 
 usage() {
-  printf "Usage: %s: -t <template-fs> -n <templatename> -f <root disk file> -d 
<descr> -h  [-u] [-v]\n" $(basename $0) >&2
+  printf "\nUsage: %s:\n\t-t templatefilesystem\n\t-n templatename\n\t-f 
templatefile\n\n" $(basename $0) >&2
 }
 
-
 #set -x
 ulimit -c 0
 
-rollback_if_needed() {
-  if [ $2 -gt 0 ]
-  then
-    printf "$3\n"
-    #back out all changes
-    rm -rf $1
-    exit 2
-fi
-}
-
-untar() {
-  local ft=$(file $1| awk -F" " '{print $2}')
-  case $ft in
-  USTAR) 
-     printf "tar archives not supported\n"  >&2
-     return 1
-          ;;
-  *) printf "$1"
-     return 0
-         ;;
-  esac
-
+# Usage: e.g. failed $? "this is an error"
+failed() {
+  local returnval=$1
+  local returnmsg=$2
+  
+  # check for an message, if there is no one dont print anything
+  if [[ -z $returnmsg ]]; then
+    :
+  else
+    echo -e $returnmsg
+  fi
+  if [[ $returnval -eq 0 ]]; then
+    return 0
+  else
+    echo "Installation failed"
+    exit $returnval
+  fi
 }
 
 is_compressed() {
@@ -88,12 +79,12 @@ uncompress() {
        ;;
   esac
 
-  if [ $? -gt 0 ] 
-  then
-    printf "Failed to uncompress file (filetype=$ft), exiting "
-    return 1 
+  if [[ $? -gt 0 ]]; then
+    printf "Failed to uncompress file (filetype=$ft), exiting"
+    rm -f $tmpfile
+    return 1
   fi
- 
+
   rm -f $1
   printf $tmpfile
 
@@ -105,21 +96,22 @@ create_from_file() {
   local tmpltimg=$2
   local tmpltname=$3
 
-  [ -n "$verbose" ] && echo "Moving to $tmpltfs/$tmpltname...could take a 
while" >&2
+  echo "Moving to $tmpltfs/$tmpltname...could take a while" >&2
   mv $tmpltimg /$tmpltfs/$tmpltname
-
 }
 
 tflag=
 nflag=
 fflag=
-sflag=
-hflag=
-hvm=false
-cleanup=false
-dflag=
 
-while getopts 'vuht:n:f:s:d:S:' OPTION
+# check if first parameter is not a dash (-) then print the usage block
+if [[ ! $@ =~ ^\-.+ ]]; then
+       usage
+       exit 0
+fi
+
+OPTERR=0
+while getopts 't:n:f:' OPTION
 do
   case $OPTION in
   t)   tflag=1
@@ -131,23 +123,6 @@ do
   f)   fflag=1
                tmpltimg="$OPTARG"
                ;;
-  s)   sflag=1
-               ;;
-  d)   dflag=1
-               descr="$OPTARG"
-               ;;
-  S)   Sflag=1
-               size=$OPTARG
-                let "size>>=10"
-               ulimit -f $size
-               ;;
-  h)   hflag=1
-               hvm="true"
-               ;;
-  u)   cleanup="true"
-               ;;
-  v)   verbose="true"
-               ;;
   ?)   usage
                exit 2
                ;;
@@ -155,68 +130,59 @@ do
 done
 
 isCifs() {
-   #TO:DO incase of multiple zone where cifs and nfs exists, 
+   #TO:DO incase of multiple zone where cifs and nfs exists,
    #then check if the template file is from cifs using df -P filename
    #Currently only cifs is supported in hyperv zone.
    mount | grep "type cifs" > /dev/null
    echo $?
 }
 
-if [ "$tflag$nflag$fflag$sflag" != "1111" ]
-then
- usage
- exit 2
+findtmpltype() {
+  local ft=$(file $1| awk -F" " '{print $2}')
+
+  case $ft in
+  Microsoft)
+    if [[ $(isCifs) -ne 0 ]]; then
+      if [[ $(which vhd-util &>/dev/null) ]]; then
+        vhd-util read -p -n ${tmpltimg2} > /dev/null
+        failed $? "vhd check of $tmpltimg2 failed\n"
+        vhd-util set -n ${tmpltimg2} -f "hidden" -v "0" > /dev/null
+        failed $? "vhd remove $tmpltimg2 hidden failed\n"
+      else
+        failed 2 "Please install vhd-util command"
+      fi
+    fi
+    ;;
+  POSIX)
+    tar xvf $tmpltimg2 -C $tmpltfs &> /dev/null
+    failed $? "untar $tmpltimg2 to $tmpltfs"
+    ;;
+  x86)
+    :
+    ;;
+  QEMU)
+    :
+    ;;
+  esac
+}
+
+if [[ "$tflag$nflag$fflag" != "111" ]]; then
+  usage
+  exit 2
 fi
 
 mkdir -p $tmpltfs
 
-if [ ! -f $tmpltimg ] 
-then
-  printf "root disk file $tmpltimg doesn't exist\n"
-  exit 3
+if [[ ! -f $tmpltimg ]]; then
+  printf "template file $tmpltimg doesn't exist\n"
+  exit 2
 fi
 
-[ -n "$verbose" ] && is_compressed $tmpltimg
+is_compressed $tmpltimg
 tmpltimg2=$(uncompress $tmpltimg)
-rollback_if_needed $tmpltfs $? "failed to uncompress $tmpltimg\n"
 
-tmpltimg2=$(untar $tmpltimg2)
-rollback_if_needed $tmpltfs $? "tar archives not supported\n"
-
-if [ ${tmpltname%.vhd} != ${tmpltname} ]
-then
-  if [ $(isCifs) -ne 0 ] ;
-  then
-      if  which  vhd-util &>/dev/null
-      then
-        vhd-util read -p -n ${tmpltimg2} > /dev/null
-        rollback_if_needed $tmpltfs $? "vhd check of $tmpltimg2 failed\n"
-        vhd-util set -n ${tmpltimg2} -f "hidden" -v "0" > /dev/null
-        rollback_if_needed $tmpltfs $? "vhd remove $tmpltimg2 hidden failed\n"
-     fi
-  fi
-fi
-
-imgsize=$(ls -l $tmpltimg2| awk -F" " '{print $5}')
+findtmpltype $tmpltimg2
 
 create_from_file $tmpltfs $tmpltimg2 $tmpltname
 
-touch /$tmpltfs/template.properties
-rollback_if_needed $tmpltfs $? "Failed to create template.properties file"
-echo -n "" > /$tmpltfs/template.properties
-
-today=$(date '+%m_%d_%Y')
-echo "filename=$tmpltname" > /$tmpltfs/template.properties
-echo "description=$descr" >> /$tmpltfs/template.properties
-# we need to rethink this property as it might get changed after download due 
to decompression
-# option is to recalcutate it here
-echo "checksum=$cksum" >> /$tmpltfs/template.properties
-echo "hvm=$hvm" >> /$tmpltfs/template.properties
-echo "size=$imgsize" >> /$tmpltfs/template.properties
-
-if [ "$cleanup" == "true" ]
-then
-  rm -f $tmpltimg
-fi
-
-exit 0
+exit 0
\ No newline at end of file

Reply via email to