blob: 88d9fd8434d3cef11f76c6ca74df5ba8b425f3ab [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
38# Functions
39# ---------
40
41# install_dib() - Collect source and prepare
42function install_dib {
Monty Taylor50495b02014-09-30 09:53:34 -070043 pip_install diskimage-builder
Steve Baker122ab702014-05-05 16:06:17 +120044
45 git_clone $TIE_REPO $TIE_DIR $TIE_BRANCH
46 git_clone $OCC_REPO $OCC_DIR $OCC_BRANCH
47 git_clone $ORC_REPO $ORC_DIR $ORC_BRANCH
48 git_clone $OAC_REPO $OAC_DIR $OAC_BRANCH
49 mkdir -p $DIB_IMAGE_CACHE
50}
51
Steve Bakerda786b22014-05-27 12:24:40 +120052# build_dib_pip_repo() - Builds a local pip repo from local projects
53function build_dib_pip_repo {
54 local project_dirs=$1
55 local projpath proj package
56
57 rm -rf $DIB_PIP_REPO
58 mkdir -p $DIB_PIP_REPO
59
60 echo "<html><body>" > $DIB_PIP_REPO/index.html
61 for projpath in $project_dirs; do
62 proj=$(basename $projpath)
63 mkdir -p $DIB_PIP_REPO/$proj
64 pushd $projpath
65 rm -rf dist
66 python setup.py sdist
67 pushd dist
68 package=$(ls *)
69 mv $package $DIB_PIP_REPO/$proj/$package
70 popd
71
72 echo "<html><body><a href=\"$package\">$package</a></body></html>" > $DIB_PIP_REPO/$proj/index.html
73 echo "<a href=\"$proj\">$proj</a><br/>" >> $DIB_PIP_REPO/index.html
74
75 popd
76 done
77
78 echo "</body></html>" >> $DIB_PIP_REPO/index.html
79
80 local dib_pip_repo_apache_conf=$(apache_site_config_for dib_pip_repo)
81
82 sudo cp $FILES/apache-dib-pip-repo.template $dib_pip_repo_apache_conf
83 sudo sed -e "
84 s|%DIB_PIP_REPO%|$DIB_PIP_REPO|g;
85 s|%DIB_PIP_REPO_PORT%|$DIB_PIP_REPO_PORT|g;
86 s|%APACHE_NAME%|$APACHE_NAME|g;
87 " -i $dib_pip_repo_apache_conf
88 enable_apache_site dib_pip_repo
89}
90
91# disk_image_create_upload() - Creates and uploads a diskimage-builder built image
92function disk_image_create_upload {
93
94 local image_name=$1
95 local image_elements=$2
96 local elements_path=$3
97
98 local image_path=$TOP_DIR/files/$image_name.qcow2
99
Steve Baker88741152014-12-23 13:13:05 +1300100 # Include the apt-sources element in builds if we have an
101 # alternative sources.list specified.
102 if [ -n "$DIB_APT_SOURCES" ]; then
103 if [ ! -e "$DIB_APT_SOURCES" ]; then
104 die $LINENO "DIB_APT_SOURCES set but not found at $DIB_APT_SOURCES"
105 fi
106 local extra_elements="apt-sources"
107 fi
108
Steve Bakerda786b22014-05-27 12:24:40 +1200109 # 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 \
Steve Baker88741152014-12-23 13:13:05 +1300130 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: