blob: 30b31ec0ee66498d31e05395ca5b663108cfe14a [file] [log] [blame]
Steve Baker122ab702014-05-05 16:06:17 +12001# lib/dib
2# Install and build images with **diskimage-builder**
3
4# Dependencies:
5#
6# - functions
7# - DEST, DATA_DIR must be defined
8
9# stack.sh
10# ---------
11# - install_dib
12
13# Save trace setting
14XTRACE=$(set +o | grep xtrace)
15set +o xtrace
16
17# Defaults
18# --------
19
20# set up default directories
21DIB_DIR=$DEST/diskimage-builder
22TIE_DIR=$DEST/tripleo-image-elements
Adam Gandelman0f726252014-10-01 17:06:19 -070023
24# NOTE: Setting DIB_APT_SOURCES assumes you will be building
25# Debian/Ubuntu based images. Leave unset for other flavors.
26DIB_APT_SOURCES=${DIB_APT_SOURCES:-""}
27DIB_BUILD_OFFLINE=$(trueorfalse False $DIB_BUILD_OFFLINE)
Steve Baker122ab702014-05-05 16:06:17 +120028DIB_IMAGE_CACHE=$DATA_DIR/diskimage-builder/image-create
Steve Bakerda786b22014-05-27 12:24:40 +120029DIB_PIP_REPO=$DATA_DIR/diskimage-builder/pip-repo
30DIB_PIP_REPO_PORT=${DIB_PIP_REPO_PORT:-8899}
Adam Gandelman0f726252014-10-01 17:06:19 -070031
Steve Baker122ab702014-05-05 16:06:17 +120032OCC_DIR=$DEST/os-collect-config
33ORC_DIR=$DEST/os-refresh-config
34OAC_DIR=$DEST/os-apply-config
35
Adam Gandelman0f726252014-10-01 17:06:19 -070036# Include the apt-sources element in builds if we have an
37# alternative sources.list specified.
38if [ -n "$DIB_APT_SOURCES" ]; then
39 if [ ! -e "$DIB_APT_SOURCES" ]; then
40 die $LINENO "DIB_APT_SOURCES set but not found at $DIB_APT_SOURCES"
41 fi
42 EXTRA_ELEMENTS="apt-sources"
43fi
44
Steve Baker122ab702014-05-05 16:06:17 +120045# Functions
46# ---------
47
48# install_dib() - Collect source and prepare
49function install_dib {
Monty Taylor50495b02014-09-30 09:53:34 -070050 pip_install diskimage-builder
Steve Baker122ab702014-05-05 16:06:17 +120051
52 git_clone $TIE_REPO $TIE_DIR $TIE_BRANCH
53 git_clone $OCC_REPO $OCC_DIR $OCC_BRANCH
54 git_clone $ORC_REPO $ORC_DIR $ORC_BRANCH
55 git_clone $OAC_REPO $OAC_DIR $OAC_BRANCH
56 mkdir -p $DIB_IMAGE_CACHE
57}
58
Steve Bakerda786b22014-05-27 12:24:40 +120059# build_dib_pip_repo() - Builds a local pip repo from local projects
60function build_dib_pip_repo {
61 local project_dirs=$1
62 local projpath proj package
63
64 rm -rf $DIB_PIP_REPO
65 mkdir -p $DIB_PIP_REPO
66
67 echo "<html><body>" > $DIB_PIP_REPO/index.html
68 for projpath in $project_dirs; do
69 proj=$(basename $projpath)
70 mkdir -p $DIB_PIP_REPO/$proj
71 pushd $projpath
72 rm -rf dist
73 python setup.py sdist
74 pushd dist
75 package=$(ls *)
76 mv $package $DIB_PIP_REPO/$proj/$package
77 popd
78
79 echo "<html><body><a href=\"$package\">$package</a></body></html>" > $DIB_PIP_REPO/$proj/index.html
80 echo "<a href=\"$proj\">$proj</a><br/>" >> $DIB_PIP_REPO/index.html
81
82 popd
83 done
84
85 echo "</body></html>" >> $DIB_PIP_REPO/index.html
86
87 local dib_pip_repo_apache_conf=$(apache_site_config_for dib_pip_repo)
88
89 sudo cp $FILES/apache-dib-pip-repo.template $dib_pip_repo_apache_conf
90 sudo sed -e "
91 s|%DIB_PIP_REPO%|$DIB_PIP_REPO|g;
92 s|%DIB_PIP_REPO_PORT%|$DIB_PIP_REPO_PORT|g;
93 s|%APACHE_NAME%|$APACHE_NAME|g;
94 " -i $dib_pip_repo_apache_conf
95 enable_apache_site dib_pip_repo
96}
97
98# disk_image_create_upload() - Creates and uploads a diskimage-builder built image
99function disk_image_create_upload {
100
101 local image_name=$1
102 local image_elements=$2
103 local elements_path=$3
104
105 local image_path=$TOP_DIR/files/$image_name.qcow2
106
107 # Set the local pip repo as the primary index mirror so the
108 # image is built with local packages
109 local pypi_mirror_url=http://$SERVICE_HOST:$DIB_PIP_REPO_PORT/
110 local pypi_mirror_url_1
111
112 if [ -a $HOME/.pip/pip.conf ]; then
113 # Add the current pip.conf index-url as an extra-index-url
114 # in the image build
115 pypi_mirror_url_1=$(iniget $HOME/.pip/pip.conf global index-url)
116 else
117 # If no pip.conf, set upstream pypi as an extra mirror
118 # (this also sets the .pydistutils.cfg index-url)
119 pypi_mirror_url_1=http://pypi.python.org/simple
120 fi
121
122 # The disk-image-create command to run
123 ELEMENTS_PATH=$elements_path \
Adam Gandelman0f726252014-10-01 17:06:19 -0700124 DIB_APT_SOURCES=$DIB_APT_SOURCES \
125 DIB_OFFLINE=$DIB_BUILD_OFFLINE \
Steve Bakerda786b22014-05-27 12:24:40 +1200126 PYPI_MIRROR_URL=$pypi_mirror_url \
127 PYPI_MIRROR_URL_1=$pypi_mirror_url_1 \
Adam Gandelman0f726252014-10-01 17:06:19 -0700128 disk-image-create -a amd64 $image_elements $EXTRA_ELEMENTS \
Steve Bakerda786b22014-05-27 12:24:40 +1200129 --image-cache $DIB_IMAGE_CACHE \
130 -o $image_path
131
132 local token=$(keystone token-get | grep ' id ' | get_field 2)
133 die_if_not_set $LINENO token "Keystone fail to get token"
134
135 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT \
136 image-create --name $image_name --is-public True \
137 --container-format=bare --disk-format qcow2 \
138 < $image_path
139}
140
Steve Baker122ab702014-05-05 16:06:17 +1200141# Restore xtrace
142$XTRACE
143
144# Tell emacs to use shell-script-mode
145## Local variables:
146## mode: shell-script
147## End: