Use multiple locations for elements dir.

Using ELEMENTS_DIR env variable, you can specify multiple dirs containint your elements.
It must be a ":" separated elements list.

When an element is seen in one of those lists, it will be chosen and will continue with the next element.

Change-Id: I18eca27d943139cd6ca1ebd232b419e502d7b048
This commit is contained in:
Ghe Rivero 2013-02-25 10:17:32 +01:00 committed by Ghe Rivero
parent 7d1a4adc17
commit 308eee2827
8 changed files with 61 additions and 34 deletions

View File

@ -256,6 +256,11 @@ The scripts can generally just be run. Options can be set on the command line
or by exporting variables to override those present in lib/img-defaults. -h to
get help.
Using the variable ELEMENTS_PATH will allow to specify multiple elements locations.
It's a colon (:) separated path list, and it will work in a first path/element found,
first served approach.
Copyright
=========

View File

@ -35,6 +35,8 @@ function show_options () {
echo " -c -- clear environment before starting work"
echo " -n skip the default inclusion of the 'base' element"
echo " -p package[,package,package] -- list of packages to install in the image"
echo
echo "ELEMENTS_PATH will allow you to specify multiple locations for the elements."
exit 0
}

View File

@ -20,9 +20,9 @@ import sys
def get_elements_dir():
if not os.environ.get('ELEMENTS_DIR'):
raise Exception("$ELEMENTS_DIR must be set.")
return os.environ['ELEMENTS_DIR']
if not os.environ.get('ELEMENTS_PATH'):
raise Exception("$ELEMENTS_PATH must be set.")
return os.environ['ELEMENTS_PATH']
def dependencies(element, elements_dir=None):
@ -37,15 +37,21 @@ def dependencies(element, elements_dir=None):
if elements_dir is None:
elements_dir = get_elements_dir()
element_deps_path = os.path.join(elements_dir, element, 'element-deps')
try:
with open(element_deps_path) as element_deps:
return set([line.strip() for line in element_deps])
except IOError as e:
if e.errno == 2:
return set()
else:
raise
for path in elements_dir.split(':'):
element_deps_path = (os.path.join(path, element, 'element-deps'))
try:
with open(element_deps_path) as element_deps:
return set([line.strip() for line in element_deps])
except IOError as e:
if os.path.exists(os.path.join(path, element)) and e.errno == 2:
return set()
if e.errno == 2:
continue
else:
raise
sys.stderr.write("ERROR: Element %s doesn't exists\n" % (element, ))
exit(-1)
def expand_dependencies(user_elements, elements_dir=None):

View File

@ -77,9 +77,9 @@ class TestElementDeps(TestCase):
class TestElements(TestCase):
def test_depends_on_env(self):
self.useFixture(EnvironmentVariable('ELEMENTS_DIR', '/foo/bar'))
self.useFixture(EnvironmentVariable('ELEMENTS_PATH', '/foo/bar'))
self.assertEquals('/foo/bar', get_elements_dir())
def test_env_not_set(self):
self.useFixture(EnvironmentVariable('ELEMENTS_DIR', ''))
self.useFixture(EnvironmentVariable('ELEMENTS_PATH', ''))
self.assertRaises(Exception, get_elements_dir, ())

View File

@ -39,8 +39,11 @@ function save_image () {
function generate_hooks () {
mkdir -p $TMP_HOOKS_PATH
for _ELEMENT in $IMAGE_ELEMENT ; do
[ -d $ELEMENTS_DIR/$_ELEMENT ] || die "Element $_ELEMENT does not exist." ;
cp -t $TMP_HOOKS_PATH -a $ELEMENTS_DIR/$_ELEMENT/* ;
for dir in $(echo $ELEMENTS_PATH | tr ":" " ") ; do
[ -d $dir/$_ELEMENT ] || continue
cp -t $TMP_HOOKS_PATH -a $dir/$_ELEMENT/* ;
break
done
done
}

View File

@ -22,4 +22,4 @@ IMAGE_NAME=${IMAGE_NAME:-image}
export IMAGE_SIZE=${IMAGE_SIZE:-2} # N.B. This size is in GB
# Set via the CLI normally.
# IMAGE_ELEMENT=
export ELEMENTS_DIR=$(dirname $0)/../elements
export ELEMENTS_PATH=${ELEMENTS_PATH:-(dirname $0)/../elements}

View File

@ -21,4 +21,4 @@ MODULE_ROOT=${MODULE_ROOT:-""}
LIB_UDEV_ROOT=${LIB_UDEV_ROOT:-""}
BUSYBOX=${BUSYBOX:-$(which busybox)}
IMAGE_NAME=${IMAGE_NAME:-"ramdisk"}
export ELEMENTS_DIR=$(dirname $0)/../elements
export ELEMENTS_PATH=${ELEMENTS_PATH:-(dirname $0)/../elements}

View File

@ -31,12 +31,16 @@ function cleanup () {
function ensure_binaries() {
BINARY_DEPS="${BUSYBOX}"
for _FLVR in ${RAMDISK_ELEMENT} ; do
_FILE="${ELEMENTS_DIR}/${_FLVR}/binary-deps"
if [ -a $_FILE ]; then
for _LINE in $(cat $_FILE) ; do
BINARY_DEPS="${BINARY_DEPS} $_LINE"
done
fi
for dir in $(echo $ELEMENTS_PATH | tr ":" " ") ; do
[ -d $dir/$_FLVR ] || continue
_FILE="${dir}/${_FLVR}/binary-deps"
if [ -a $_FILE ]; then
for _LINE in $(cat $_FILE) ; do
BINARY_DEPS="${BINARY_DEPS} $_LINE"
done
fi
break
done
done
for _BIN in $BINARY_DEPS ; do
@ -171,14 +175,17 @@ function populate_init () {
# Append /init with any element fragments that are present
for _FLVR in ${RAMDISK_ELEMENT} ; do
_FILE="${ELEMENTS_DIR}/${_FLVR}/init"
if [ -a $_FILE ]; then
cat >>$TMP_MOUNT_PATH/init <<EOF
for dir in $(echo $ELEMENTS_PATH | tr ":" " ") ; do
[ -d $dir/$_FLVR ] || continue
_FILE="${dir}/${_FLVR}/init"
if [ -a $_FILE ]; then
cat >>$TMP_MOUNT_PATH/init <<EOF
# init fragment from ${_FLVR}
EOF
cat <$_FILE >>$TMP_MOUNT_PATH/init
fi
cat <$_FILE >>$TMP_MOUNT_PATH/init
fi
break
done
done
# Add our final steps to /init
@ -194,10 +201,14 @@ function populate_udev () {
echo "Installing udev rules"
for _FLVR in ${RAMDISK_ELEMENT} ; do
_DIR="${ELEMENTS_DIR}/${_FLVR}/udev"
if [ -d $_DIR ]; then
find $_DIR -type f -exec cp -v {} $TMP_MOUNT_PATH/lib/udev/rules.d/ \;
fi
for dir in $(echo $ELEMENTS_PATH | tr ":" " ") ; do
[ -d $dir/$_FLVR ] || continue
_DIR="${dir}/${_FLVR}/udev"
if [ -d $_DIR ]; then
find $_DIR -type f -exec cp -v {} $TMP_MOUNT_PATH/lib/udev/rules.d/ \;
fi
break
done
done
}