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

rohit pushed a commit to branch revert-3346-template_rewrite
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit a11dc4662702b27c6f306ec12eddf9491c50c3fc
Author: Rohit Yadav <ro...@apache.org>
AuthorDate: Mon Jun 3 17:28:01 2019 +0530

    Revert "Fix template size for managed storage / refactor 
cloud-install-sys-tmplt and createtmplt.sh (#3346)"
    
    This reverts commit 6946f41784423788e71441255e67c2b9b1c3633d.
---
 scripts/storage/secondary/cloud-install-sys-tmplt | 298 +++++++++++-----------
 scripts/storage/secondary/createtmplt.sh          | 184 +++++++------
 2 files changed, 264 insertions(+), 218 deletions(-)

diff --git a/scripts/storage/secondary/cloud-install-sys-tmplt 
b/scripts/storage/secondary/cloud-install-sys-tmplt
index 6bb8d20..91b3a7c 100755
--- a/scripts/storage/secondary/cloud-install-sys-tmplt
+++ b/scripts/storage/secondary/cloud-install-sys-tmplt
@@ -1,4 +1,5 @@
 #!/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
@@ -19,35 +20,19 @@
 
 
 usage() {
-  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
+  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
 }
 
-# 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
+  echo "Installation failed"
+  exit $1
 }
-
 #set -x
 mflag=
 fflag=
 ext="vhd"
-hvm=false
 templateId=
 hyper=
 msKey=password
@@ -57,15 +42,6 @@ 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
@@ -86,9 +62,6 @@ do
         ;;
   h)    hyper="$OPTARG"
         ;;
-  H)    Hflag=1
-        hvm="true"
-        ;;
   s)    sflag=1
         msKey="$OPTARG"
         ;;
@@ -105,98 +78,121 @@ do
         dbPort="$OPTARG"
         ;;
   ?)    usage
-        exit 0
-        ;;
-  *)    usage
-        exit 0
+        failed 2
         ;;
   esac
 done
 
-if [[ "$mflag$fflag" != "11" && "$mflag$uflag" != "11" ]]; then
-  failed 2 "Please add a mount point and a system vm template file"
+if [[ "$mflag$fflag" != "11"  && "$mflag$uflag" != "11" ]]
+then
+  usage
+  failed 2
 fi
 
-if [[ -z "$hyper" ]]; then
-  failed 2 "Please add a correct hypervisor name like: 
kvm|vmware|xenserver|hyperv|ovm3"
+if [ -z "$hyper" ]
+then
+  usage
+  failed 2
 fi
 
-if [[ ! -d $mntpoint ]]; then
-  failed 2 "mount point $mntpoint doesn't exist\n"
+if [ ! -d $mntpoint ]
+then
+  echo "mount point $mntpoint doesn't exist\n"
+  failed 4
 fi
 
-if [[ "$fflag" == "1" && ! -f $tmpltimg ]]; then
-  failed 2 "template image file $tmpltimg doesn't exist"
+if [[ "$fflag" == "1" && ! -f $tmpltimg ]]
+then
+  echo "template image file $tmpltimg doesn't exist"
+  failed 3
 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 [[ "$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 [ -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
 
-  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"
+    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
-  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
+    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
-  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 )
+
+    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
     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"`)
-    if [[ $(which qemu-img) ]]; then
-      qemuimgcmd=$(which qemu-img)
+    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
-      failed 2 "Please install qemu-img: command\non CentOS run \"yum install 
qemu-img\"\non Ubuntu/Debian run \"apt-get install qemu-utils\""
+        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
-  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
-  failed 2 "Unable to get template Id from database"
+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
+fi
+
+if [ ! $templateId ]
+then
+    echo "Unable to get template Id from database"
+    failed 8
 fi
 
 _uuid=$(uuidgen)
@@ -209,31 +205,43 @@ mntpoint=`echo "$mntpoint" | sed 's|/*$||'`
 destdir=$mntpoint/template/tmpl/1/$templateId/
 
 mkdir -p $destdir
-if [[ $? -ne 0 ]]; then
-  failed 2 "Failed to write to mount point $mntpoint -- is it mounted?\n"
+if [ $? -ne 0 ]
+then
+  printf "Failed to write to mount point $mntpoint -- is it mounted?\n"
+  failed 3
 fi
 
-if [[ "$Fflag" == "1" ]]; then
+if [ "$Fflag" == "1" ]
+then
   rm -rf $destdir/*
-  if [[ $? -ne 0 ]]; then
-    failed 2 "Failed to clean up template directory $destdir -- check 
permissions?"
+  if [ $? -ne 0 ]
+  then
+    echo "Failed to clean up template directory $destdir -- check permissions?"
+    failed 2
   fi
 fi
 
-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"
+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
 fi
 
-destfiles=$(find $destdir -name \*.$ext)
-if [[ "$destfiles" != "" ]]; then
-  failed 2 "Data already exists at destination $destdir -- use -F to force 
cleanup of old template"
+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
 fi
 
-tmplfile=$(dirname $0)/$localfile
+tmpfile=$(dirname $0)/$localfile
 
-touch $tmplfile
-if [[ $? -ne 0 ]]; then
-  failed 2 "Failed to create temporary file in directory $(dirname $0) -- is 
it read-only or full?\n"
+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
 fi
 
 destcap=$(df -P $destdir | awk '{print $4}' | tail -1 )
@@ -242,47 +250,51 @@ 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 $tmplfile $url
-  if [[ $? -ne 0 ]]; then
-    failed 2 "Failed to fetch system vm template from $url"
+if [ "$uflag" == "1" ]
+then
+  wget -O $tmpfile $url
+  if [ $? -ne 0 ]
+  then
+    echo "Failed to fetch system vm template from $url"
+    failed 5
   fi
 fi
 
-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"
+
+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
   fi
 fi
 
-installrslt=$($(dirname $0)/createtmplt.sh -n $localfile -t $destdir -f 
$tmplfile)
 
-if [[ $? -ne 0 ]]; then
-  failed 2 "Failed to install system vm template $tmpltimg to $destdir: 
$installrslt"
+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
 
-tmpltfile=$destdir/$localfile
-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
+if [ "$ext" == "ova" ]
+then
+  tar xvf $destdir/$localfile -C $destdir &> /dev/null
 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
+tmpltfile=$destdir/$localfile
+tmpltsize=$(ls -l $tmpltfile| awk -F" " '{print $5}')
+
 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=$vrtmpltsize" >> $destdir/template.properties
-echo "virtualsize=$vrtmpltsize" >> $destdir/template.properties
+echo "$ext.virtualsize=$tmpltsize" >> $destdir/template.properties
+echo "virtualsize=$tmpltsize" >> $destdir/template.properties
 echo "$ext.size=$tmpltsize" >> $destdir/template.properties
 
-echo "Successfully installed system VM template $tmpltimg and 
template.properties to $destdir"
\ No newline at end of file
+echo "Successfully installed system VM template $tmpltimg to $destdir"
diff --git a/scripts/storage/secondary/createtmplt.sh 
b/scripts/storage/secondary/createtmplt.sh
index 8658608..4e8db46 100755
--- a/scripts/storage/secondary/createtmplt.sh
+++ b/scripts/storage/secondary/createtmplt.sh
@@ -1,5 +1,4 @@
-#!/bin/bash
-
+#!/usr/bin/env 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
@@ -7,9 +6,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
@@ -17,31 +16,41 @@
 # 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 "\nUsage: %s:\n\t-t templatefilesystem\n\t-n templatename\n\t-f 
templatefile\n\n" $(basename $0) >&2
+  printf "Usage: %s: -t <template-fs> -n <templatename> -f <root disk file> -d 
<descr> -h  [-u] [-v]\n" $(basename $0) >&2
 }
 
+
 #set -x
 ulimit -c 0
 
-# 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
+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
+
 }
 
 is_compressed() {
@@ -79,12 +88,12 @@ uncompress() {
        ;;
   esac
 
-  if [[ $? -gt 0 ]]; then
-    printf "Failed to uncompress file (filetype=$ft), exiting"
-    rm -f $tmpfile
-    return 1
+  if [ $? -gt 0 ] 
+  then
+    printf "Failed to uncompress file (filetype=$ft), exiting "
+    return 1 
   fi
-
+ 
   rm -f $1
   printf $tmpfile
 
@@ -96,22 +105,21 @@ create_from_file() {
   local tmpltimg=$2
   local tmpltname=$3
 
-  echo "Moving to $tmpltfs/$tmpltname...could take a while" >&2
+  [ -n "$verbose" ] && echo "Moving to $tmpltfs/$tmpltname...could take a 
while" >&2
   mv $tmpltimg /$tmpltfs/$tmpltname
+
 }
 
 tflag=
 nflag=
 fflag=
+sflag=
+hflag=
+hvm=false
+cleanup=false
+dflag=
 
-# 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
+while getopts 'vuht:n:f:s:d:S:' OPTION
 do
   case $OPTION in
   t)   tflag=1
@@ -123,6 +131,23 @@ 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
                ;;
@@ -130,59 +155,68 @@ 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 $?
 }
 
-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
+if [ "$tflag$nflag$fflag$sflag" != "1111" ]
+then
+ usage
+ exit 2
 fi
 
 mkdir -p $tmpltfs
 
-if [[ ! -f $tmpltimg ]]; then
-  printf "template file $tmpltimg doesn't exist\n"
-  exit 2
+if [ ! -f $tmpltimg ] 
+then
+  printf "root disk file $tmpltimg doesn't exist\n"
+  exit 3
 fi
 
-is_compressed $tmpltimg
+[ -n "$verbose" ] && is_compressed $tmpltimg
 tmpltimg2=$(uncompress $tmpltimg)
+rollback_if_needed $tmpltfs $? "failed to uncompress $tmpltimg\n"
 
-findtmpltype $tmpltimg2
+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}')
 
 create_from_file $tmpltfs $tmpltimg2 $tmpltname
 
-exit 0
\ No newline at end of file
+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

Reply via email to