Make build_chroot self-contained
authorMichele Tartara <mtartara@google.com>
Thu, 20 Jun 2013 08:57:24 +0000 (08:57 +0000)
committerMichele Tartara <mtartara@google.com>
Thu, 20 Jun 2013 12:27:20 +0000 (12:27 +0000)
The configuration file are included in build_chroot, and are
automatically created by the script if it is not able to find them
already in the directory pointed by $DATA_DIR.

Note that the configuration file directory is cleaned if it's temporary,
but it is left untouched if it was esplicitly specified by the user.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Klaus Aehlig <aehlig@google.com>

devel/build_chroot
devel/data/final.schroot.conf.in [deleted file]
devel/data/temp.schroot.conf.in [deleted file]

index d134a4b..0625d9b 100755 (executable)
@@ -1,11 +1,11 @@
 #!/bin/bash
 #Configuration
-: ${DATA_DIR=`dirname $0`/data}
-: ${ARCH=amd64}
-: ${DIST_RELEASE=squeeze}
+: ${ARCH:=amd64}
+: ${DIST_RELEASE:=squeeze}
 : ${CONF_DIR:=/etc/schroot/chroot.d}
 : ${CHROOT_DIR:=/srv/chroot}
 : ${ALTERNATIVE_EDITOR:=/usr/bin/vim.basic}
+# The value of DATA_DIR is read as well from the environment.
 
 #Automatically generated variables
 CHROOTNAME=$DIST_RELEASE-$ARCH
@@ -17,6 +17,9 @@ CHDIR=$ROOT/$CHNAME
 USER=`whoami`
 COMP_FILENAME=$CHROOTNAME.tar.gz
 COMP_FILEPATH=$ROOT/$COMP_FILENAME
+TEMP_DATA_DIR=`mktemp -d`
+ACTUAL_DATA_DIR=$DATA_DIR
+ACTUAL_DATA_DIR=${ACTUAL_DATA_DIR:-$TEMP_DATA_DIR}
 
 #Runnability checks
 if [ $USER != 'root' ]
@@ -34,16 +37,46 @@ then
   exit
 fi
 
-if [ ! -d $DATA_DIR ]
+#Create configuration dir and files if they do not exist
+if [ ! -d $ACTUAL_DATA_DIR ]
 then
+  mkdir $ACTUAL_DATA_DIR
   echo "The data directory"
-  echo "  $DATA_DIR"
-  echo "does not exist."
-  echo "Please, set the DATA_DIR environment variable so that it points to the"
-  echo "data directory."
-  exit
+  echo "  $ACTUAL_DATA_DIR"
+  echo "has been created."
+fi
+
+if [ ! -f $ACTUAL_DATA_DIR/final.schroot.conf.in ]
+then
+  cat <<END >$ACTUAL_DATA_DIR/final.schroot.conf.in
+[${CHROOTNAME}]
+description=Debian ${DIST_RELEASE} ${ARCH}
+groups=src
+source-root-groups=root
+type=file
+file=${CHROOT_DIR}/${COMP_FILENAME}
+END
+  echo "The file"
+  echo " $ACTUAL_DATA_DIR/final.schroot.conf.in"
+  echo "has been created with default configurations."
+fi
+
+if [ ! -f $ACTUAL_DATA_DIR/temp.schroot.conf.in ]
+then
+  cat <<END >$ACTUAL_DATA_DIR/temp.schroot.conf.in
+[${CHNAME}]
+description=Debian ${DIST_RELEASE} ${ARCH}
+directory=${CHDIR}
+groups=src
+users=root
+type=directory
+END
+  echo "The file"
+  echo " $ACTUAL_DATA_DIR/temp.schroot.conf.in"
+  echo "has been created with default configurations."
 fi
 
+#Stop on errors
 set -e
 
 #Cleanup
@@ -65,8 +98,8 @@ alias subst_variables='sed \
   -e "s/\${DIST_RELEASE}/$DIST_RELEASE/"'
 
 #Generate chroot configurations
-cat $DATA_DIR/temp.schroot.conf.in | subst_variables > $TEMP_CHROOT_CONF
-cat $DATA_DIR/final.schroot.conf.in | subst_variables > $FINAL_CHROOT_CONF
+cat $ACTUAL_DATA_DIR/temp.schroot.conf.in | subst_variables > $TEMP_CHROOT_CONF
+cat $ACTUAL_DATA_DIR/final.schroot.conf.in | subst_variables > $FINAL_CHROOT_CONF
 
 #Install the base system
 debootstrap --arch $ARCH $DIST_RELEASE $CHDIR
@@ -183,3 +216,4 @@ echo "Done"
 
 rm -rf $CHDIR
 rm -f $TEMP_CHROOT_CONF
+rm -rf $TEMP_DATA_DIR
diff --git a/devel/data/final.schroot.conf.in b/devel/data/final.schroot.conf.in
deleted file mode 100644 (file)
index 08bb4b0..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-[${CHROOTNAME}]
-description=Debian ${DIST_RELEASE} ${ARCH}
-groups=src
-source-root-groups=root
-type=file
-file=${CHROOT_DIR}/${COMP_FILENAME}
diff --git a/devel/data/temp.schroot.conf.in b/devel/data/temp.schroot.conf.in
deleted file mode 100644 (file)
index fe9a409..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-[${CHNAME}]
-description=Debian ${DIST_RELEASE} ${ARCH}
-directory=${CHDIR}
-groups=src
-users=root
-type=directory