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