devstack/lib/dib

150 lines
4.3 KiB
Bash

#!/bin/bash
#
# lib/dib
# Install and build images with **diskimage-builder**
# Dependencies:
#
# - functions
# - DEST, DATA_DIR must be defined
# stack.sh
# ---------
# - install_dib
# Save trace setting
XTRACE=$(set +o | grep xtrace)
set +o xtrace
# Defaults
# --------
# set up default directories
DIB_DIR=$DEST/diskimage-builder
TIE_DIR=$DEST/tripleo-image-elements
# NOTE: Setting DIB_APT_SOURCES assumes you will be building
# Debian/Ubuntu based images. Leave unset for other flavors.
DIB_APT_SOURCES=${DIB_APT_SOURCES:-""}
DIB_BUILD_OFFLINE=$(trueorfalse False DIB_BUILD_OFFLINE)
DIB_IMAGE_CACHE=$DATA_DIR/diskimage-builder/image-create
DIB_PIP_REPO=$DATA_DIR/diskimage-builder/pip-repo
DIB_PIP_REPO_PORT=${DIB_PIP_REPO_PORT:-8899}
OCC_DIR=$DEST/os-collect-config
ORC_DIR=$DEST/os-refresh-config
OAC_DIR=$DEST/os-apply-config
# Functions
# ---------
# install_dib() - Collect source and prepare
function install_dib {
pip_install diskimage-builder
git_clone $TIE_REPO $TIE_DIR $TIE_BRANCH
git_clone $OCC_REPO $OCC_DIR $OCC_BRANCH
git_clone $ORC_REPO $ORC_DIR $ORC_BRANCH
git_clone $OAC_REPO $OAC_DIR $OAC_BRANCH
mkdir -p $DIB_IMAGE_CACHE
}
# build_dib_pip_repo() - Builds a local pip repo from local projects
function build_dib_pip_repo {
local project_dirs=$1
local projpath proj package
rm -rf $DIB_PIP_REPO
mkdir -p $DIB_PIP_REPO
echo "<html><body>" > $DIB_PIP_REPO/index.html
for projpath in $project_dirs; do
proj=$(basename $projpath)
mkdir -p $DIB_PIP_REPO/$proj
pushd $projpath
rm -rf dist
python setup.py sdist
pushd dist
package=$(ls *)
mv $package $DIB_PIP_REPO/$proj/$package
popd
echo "<html><body><a href=\"$package\">$package</a></body></html>" > $DIB_PIP_REPO/$proj/index.html
echo "<a href=\"$proj\">$proj</a><br/>" >> $DIB_PIP_REPO/index.html
popd
done
echo "</body></html>" >> $DIB_PIP_REPO/index.html
local dib_pip_repo_apache_conf=$(apache_site_config_for dib_pip_repo)
sudo cp $FILES/apache-dib-pip-repo.template $dib_pip_repo_apache_conf
sudo sed -e "
s|%DIB_PIP_REPO%|$DIB_PIP_REPO|g;
s|%DIB_PIP_REPO_PORT%|$DIB_PIP_REPO_PORT|g;
s|%APACHE_NAME%|$APACHE_NAME|g;
" -i $dib_pip_repo_apache_conf
enable_apache_site dib_pip_repo
}
# disk_image_create_upload() - Creates and uploads a diskimage-builder built image
function disk_image_create_upload {
local image_name=$1
local image_elements=$2
local elements_path=$3
local image_path=$TOP_DIR/files/$image_name.qcow2
# Include the apt-sources element in builds if we have an
# alternative sources.list specified.
if [ -n "$DIB_APT_SOURCES" ]; then
if [ ! -e "$DIB_APT_SOURCES" ]; then
die $LINENO "DIB_APT_SOURCES set but not found at $DIB_APT_SOURCES"
fi
local extra_elements="apt-sources"
fi
# Set the local pip repo as the primary index mirror so the
# image is built with local packages
local pypi_mirror_url=http://$SERVICE_HOST:$DIB_PIP_REPO_PORT/
local pypi_mirror_url_1
if [ -a $HOME/.pip/pip.conf ]; then
# Add the current pip.conf index-url as an extra-index-url
# in the image build
pypi_mirror_url_1=$(iniget $HOME/.pip/pip.conf global index-url)
else
# If no pip.conf, set upstream pypi as an extra mirror
# (this also sets the .pydistutils.cfg index-url)
pypi_mirror_url_1=http://pypi.python.org/simple
fi
# The disk-image-create command to run
ELEMENTS_PATH=$elements_path \
DIB_APT_SOURCES=$DIB_APT_SOURCES \
DIB_OFFLINE=$DIB_BUILD_OFFLINE \
PYPI_MIRROR_URL=$pypi_mirror_url \
PYPI_MIRROR_URL_1=$pypi_mirror_url_1 \
disk-image-create -a amd64 $image_elements ${extra_elements:-} \
--image-cache $DIB_IMAGE_CACHE \
-o $image_path
local token=$(keystone token-get | grep ' id ' | get_field 2)
die_if_not_set $LINENO token "Keystone fail to get token"
glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT \
image-create --name $image_name --is-public True \
--container-format=bare --disk-format qcow2 \
< $image_path
}
# Restore xtrace
$XTRACE
# Tell emacs to use shell-script-mode
## Local variables:
## mode: shell-script
## End: