blob: 55272b9a1d224b373a5f5d460131d320c58b47fb [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +03003# lib/ironic
4# Functions to control the configuration and operation of the **Ironic** service
5
6# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - ``functions`` file
9# - ``DEST``, ``DATA_DIR``, ``STACK_USER`` must be defined
10# - ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
11# - ``SERVICE_HOST``
12# - ``KEYSTONE_TOKEN_FORMAT`` must be defined
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030013
14# ``stack.sh`` calls the entry points in this order:
15#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010016# - install_ironic
17# - install_ironicclient
18# - init_ironic
19# - start_ironic
20# - stop_ironic
21# - cleanup_ironic
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030022
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040023# Save trace and pipefail settings
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030024XTRACE=$(set +o | grep xtrace)
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040025PIPEFAIL=$(set +o | grep pipefail)
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030026set +o xtrace
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040027set +o pipefail
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030028
29# Defaults
30# --------
31
32# Set up default directories
Sean Daguee08ab102014-11-13 17:09:28 -050033GITDIR["python-ironicclient"]=$DEST/python-ironicclient
Sean Dague5cb19062014-11-01 01:37:45 +010034
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030035IRONIC_DIR=$DEST/ironic
Jim Rollenhagenf00766b2014-08-15 01:08:11 +000036IRONIC_PYTHON_AGENT_DIR=$DEST/ironic-python-agent
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040037IRONIC_DATA_DIR=$DATA_DIR/ironic
38IRONIC_STATE_PATH=/var/lib/ironic
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030039IRONIC_AUTH_CACHE_DIR=${IRONIC_AUTH_CACHE_DIR:-/var/cache/ironic}
40IRONIC_CONF_DIR=${IRONIC_CONF_DIR:-/etc/ironic}
41IRONIC_CONF_FILE=$IRONIC_CONF_DIR/ironic.conf
42IRONIC_ROOTWRAP_CONF=$IRONIC_CONF_DIR/rootwrap.conf
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030043IRONIC_POLICY_JSON=$IRONIC_CONF_DIR/policy.json
44
yunhong jiangae9ee6b2014-10-08 07:01:02 -070045# Deploy to hardware platform
46IRONIC_HW_NODE_CPU=${IRONIC_HW_NODE_CPU:-1}
47IRONIC_HW_NODE_RAM=${IRONIC_HW_NODE_RAM:-512}
48IRONIC_HW_NODE_DISK=${IRONIC_HW_NODE_DISK:-10}
49IRONIC_HW_EPHEMERAL_DISK=${IRONIC_HW_EPHEMERAL_DISK:-0}
50# The file is composed of multiple lines, each line includes four field
51# separated by white space: IPMI address, MAC address, IPMI username
52# and IPMI password.
53# An example:
54# 192.168.110.107 00:1e:67:57:50:4c root otc123
55IRONIC_IPMIINFO_FILE=${IRONIC_IPMIINFO_FILE:-$IRONIC_DATA_DIR/hardware_info}
56
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040057# Set up defaults for functional / integration testing
58IRONIC_SCRIPTS_DIR=${IRONIC_SCRIPTS_DIR:-$TOP_DIR/tools/ironic/scripts}
59IRONIC_TEMPLATES_DIR=${IRONIC_TEMPLATES_DIR:-$TOP_DIR/tools/ironic/templates}
60IRONIC_BAREMETAL_BASIC_OPS=$(trueorfalse False $IRONIC_BAREMETAL_BASIC_OPS)
Devananda van der Veen83eeed02014-04-11 10:08:41 -070061IRONIC_ENABLED_DRIVERS=${IRONIC_ENABLED_DRIVERS:-fake,pxe_ssh,pxe_ipmitool}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040062IRONIC_SSH_USERNAME=${IRONIC_SSH_USERNAME:-`whoami`}
63IRONIC_SSH_KEY_DIR=${IRONIC_SSH_KEY_DIR:-$IRONIC_DATA_DIR/ssh_keys}
64IRONIC_SSH_KEY_FILENAME=${IRONIC_SSH_KEY_FILENAME:-ironic_key}
Adam Gandelman6c749322014-12-02 16:20:50 -080065IRONIC_KEY_FILE=${IRONIC_KEY_FILE:-$IRONIC_SSH_KEY_DIR/$IRONIC_SSH_KEY_FILENAME}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040066IRONIC_SSH_VIRT_TYPE=${IRONIC_SSH_VIRT_TYPE:-virsh}
67IRONIC_TFTPBOOT_DIR=${IRONIC_TFTPBOOT_DIR:-$IRONIC_DATA_DIR/tftpboot}
yunhong jiangae9ee6b2014-10-08 07:01:02 -070068IRONIC_TFTPSERVER_IP=${IRONIC_TFTPSERVER_IP:-$HOST_IP}
Adam Gandelman86765102014-04-14 11:57:13 -070069IRONIC_VM_SSH_PORT=${IRONIC_VM_SSH_PORT:-22}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040070IRONIC_VM_SSH_ADDRESS=${IRONIC_VM_SSH_ADDRESS:-$HOST_IP}
71IRONIC_VM_COUNT=${IRONIC_VM_COUNT:-1}
72IRONIC_VM_SPECS_CPU=${IRONIC_VM_SPECS_CPU:-1}
Adam Gandelmanea335f42014-07-03 17:57:49 -070073IRONIC_VM_SPECS_RAM=${IRONIC_VM_SPECS_RAM:-512}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040074IRONIC_VM_SPECS_DISK=${IRONIC_VM_SPECS_DISK:-10}
David Shrewsbury3e4612b2014-05-01 11:38:21 -040075IRONIC_VM_EPHEMERAL_DISK=${IRONIC_VM_EPHEMERAL_DISK:-0}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040076IRONIC_VM_EMULATOR=${IRONIC_VM_EMULATOR:-/usr/bin/qemu-system-x86_64}
77IRONIC_VM_NETWORK_BRIDGE=${IRONIC_VM_NETWORK_BRIDGE:-brbm}
78IRONIC_VM_NETWORK_RANGE=${IRONIC_VM_NETWORK_RANGE:-192.0.2.0/24}
79IRONIC_VM_MACS_CSV_FILE=${IRONIC_VM_MACS_CSV_FILE:-$IRONIC_DATA_DIR/ironic_macs.csv}
80IRONIC_AUTHORIZED_KEYS_FILE=${IRONIC_AUTHORIZED_KEYS_FILE:-$HOME/.ssh/authorized_keys}
81
Adam Gandelman8af6fae2014-04-11 17:06:14 -070082# By default, baremetal VMs will console output to file.
83IRONIC_VM_LOG_CONSOLE=${IRONIC_VM_LOG_CONSOLE:-True}
84IRONIC_VM_LOG_DIR=${IRONIC_VM_LOG_DIR:-$IRONIC_DATA_DIR/logs/}
85
Alexander Gordeevf177f722014-03-14 18:44:48 +040086# Use DIB to create deploy ramdisk and kernel.
87IRONIC_BUILD_DEPLOY_RAMDISK=`trueorfalse True $IRONIC_BUILD_DEPLOY_RAMDISK`
88# If not use DIB, these files are used as deploy ramdisk/kernel.
89# (The value must be a absolute path)
90IRONIC_DEPLOY_RAMDISK=${IRONIC_DEPLOY_RAMDISK:-}
91IRONIC_DEPLOY_KERNEL=${IRONIC_DEPLOY_KERNEL:-}
92IRONIC_DEPLOY_ELEMENT=${IRONIC_DEPLOY_ELEMENT:-deploy-ironic}
93
Jim Rollenhagen792dc5f2014-08-27 17:03:00 -070094IRONIC_AGENT_KERNEL_URL=${IRONIC_AGENT_KERNEL_URL:-http://tarballs.openstack.org/ironic-python-agent/coreos/files/coreos_production_pxe.vmlinuz}
Jim Rollenhagen4cb0d8c2014-09-11 16:45:58 -070095IRONIC_AGENT_RAMDISK_URL=${IRONIC_AGENT_RAMDISK_URL:-http://tarballs.openstack.org/ironic-python-agent/coreos/files/coreos_production_pxe_image-oem.cpio.gz}
Jim Rollenhagena6a45462014-08-05 18:08:29 +000096
97# Which deploy driver to use - valid choices right now
yunhong jiangfe96ed62014-11-05 03:30:25 -070098# are 'pxe_ssh', 'pxe_ipmitool', 'agent_ssh' and 'agent_ipmitool'.
Jim Rollenhagena6a45462014-08-05 18:08:29 +000099IRONIC_DEPLOY_DRIVER=${IRONIC_DEPLOY_DRIVER:-pxe_ssh}
100
Alexander Gordeevf177f722014-03-14 18:44:48 +0400101#TODO(agordeev): replace 'ubuntu' with host distro name getting
102IRONIC_DEPLOY_FLAVOR=${IRONIC_DEPLOY_FLAVOR:-ubuntu $IRONIC_DEPLOY_ELEMENT}
103
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300104# Support entry points installation of console scripts
105IRONIC_BIN_DIR=$(get_python_exec_prefix)
106
107# Ironic connection info. Note the port must be specified.
108IRONIC_SERVICE_PROTOCOL=http
Adam Gandelman4b45fca2014-11-17 09:59:23 -0800109IRONIC_SERVICE_PORT=${IRONIC_SERVICE_PORT:-6385}
110IRONIC_HOSTPORT=${IRONIC_HOSTPORT:-$SERVICE_HOST:$IRONIC_SERVICE_PORT}
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300111
Dean Troyer4237f592014-01-29 16:22:11 -0600112# Tell Tempest this project is present
113TEMPEST_SERVICES+=,ironic
114
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100115# Enable iPXE
116IRONIC_IPXE_ENABLED=$(trueorfalse False $IRONIC_IPXE_ENABLED)
117IRONIC_HTTP_DIR=${IRONIC_HTTP_DIR:-$IRONIC_DATA_DIR/httpboot}
118IRONIC_HTTP_SERVER=${IRONIC_HTTP_SERVER:-$HOST_IP}
119IRONIC_HTTP_PORT=${IRONIC_HTTP_PORT:-8088}
120
121# get_pxe_boot_file() - Get the PXE/iPXE boot file path
122function get_pxe_boot_file {
123 local relpath=syslinux/pxelinux.0
124 if [[ "$IRONIC_IPXE_ENABLED" == "True" ]] ; then
125 relpath=ipxe/undionly.kpxe
126 fi
127
128 local pxe_boot_file
129 if is_ubuntu; then
130 pxe_boot_file=/usr/lib/$relpath
131 elif is_fedora || is_suse; then
132 pxe_boot_file=/usr/share/$relpath
133 fi
134
135 echo $pxe_boot_file
136}
137
138# PXE boot image
139IRONIC_PXE_BOOT_IMAGE=${IRONIC_PXE_BOOT_IMAGE:-$(get_pxe_boot_file)}
140
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300141
142# Functions
143# ---------
144
Dean Troyer1023ff72014-01-27 14:56:44 -0600145# Test if any Ironic services are enabled
146# is_ironic_enabled
147function is_ironic_enabled {
148 [[ ,${ENABLED_SERVICES} =~ ,"ir-" ]] && return 0
149 return 1
150}
151
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700152function is_ironic_hardware {
153 is_ironic_enabled && [[ -n "${IRONIC_DEPLOY_DRIVER##*_ssh}" ]] && return 0
154 return 1
155}
156
yunhong jiangfe96ed62014-11-05 03:30:25 -0700157function is_deployed_by_agent {
158 [[ -z "${IRONIC_DEPLOY_DRIVER%%agent*}" ]] && return 0
159 return 1
160}
161
Roman Prykhodchenko43e00662013-10-15 17:03:15 +0300162# install_ironic() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100163function install_ironic {
Adam Gandelman6d271482014-08-05 18:12:29 -0700164 # make sure all needed service were enabled
165 for srv in nova glance key; do
166 if ! is_service_enabled "$srv"; then
167 die $LINENO "$srv should be enabled for Ironic."
168 fi
169 done
Roman Prykhodchenko43e00662013-10-15 17:03:15 +0300170 git_clone $IRONIC_REPO $IRONIC_DIR $IRONIC_BRANCH
171 setup_develop $IRONIC_DIR
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100172
173 if [[ "$IRONIC_IPXE_ENABLED" == "True" ]] ; then
174 install_apache_wsgi
175 fi
Roman Prykhodchenko43e00662013-10-15 17:03:15 +0300176}
177
178# install_ironicclient() - Collect sources and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100179function install_ironicclient {
Sean Daguee08ab102014-11-13 17:09:28 -0500180 if use_library_from_git "python-ironicclient"; then
181 git_clone_by_name "python-ironicclient"
182 setup_dev_lib "python-ironicclient"
183 sudo install -D -m 0644 -o $STACK_USER {${GITDIR["python-ironicclient"]}/tools/,/etc/bash_completion.d/}ironic.bash_completion
Sean Dague5cb19062014-11-01 01:37:45 +0100184 else
185 # nothing actually "requires" ironicclient, so force instally from pypi
186 pip_install python-ironicclient
187 fi
Roman Prykhodchenko43e00662013-10-15 17:03:15 +0300188}
189
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100190# _cleanup_ironic_apache_wsgi() - Remove wsgi files, disable and remove apache vhost file
191function _cleanup_ironic_apache_wsgi {
192 sudo rm -rf $IRONIC_HTTP_DIR
193 disable_apache_site ironic
194 sudo rm -f $(apache_site_config_for ironic)
195 restart_apache_server
196}
197
198# _config_ironic_apache_wsgi() - Set WSGI config files of Ironic
199function _config_ironic_apache_wsgi {
200 local ironic_apache_conf=$(apache_site_config_for ironic)
201 sudo cp $FILES/apache-ironic.template $ironic_apache_conf
202 sudo sed -e "
203 s|%PUBLICPORT%|$IRONIC_HTTP_PORT|g;
204 s|%HTTPROOT%|$IRONIC_HTTP_DIR|g;
205 " -i $ironic_apache_conf
206 enable_apache_site ironic
207}
208
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300209# cleanup_ironic() - Remove residual data files, anything left over from previous
210# runs that would need to clean up.
Ian Wienandaee18c72014-02-21 15:35:08 +1100211function cleanup_ironic {
lokesh683ed9e2014-11-10 15:07:59 +0530212 sudo rm -rf $IRONIC_AUTH_CACHE_DIR $IRONIC_CONF_DIR
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300213}
214
Adam Gandelman6d271482014-08-05 18:12:29 -0700215# configure_ironic_dirs() - Create all directories required by Ironic and
216# associated services.
217function configure_ironic_dirs {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300218 if [[ ! -d $IRONIC_CONF_DIR ]]; then
219 sudo mkdir -p $IRONIC_CONF_DIR
220 fi
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100221
222 if [[ "$IRONIC_IPXE_ENABLED" == "True" ]] ; then
223 sudo mkdir -p $IRONIC_HTTP_DIR
224 sudo chown -R $STACK_USER:$LIBVIRT_GROUP $IRONIC_HTTP_DIR
225 fi
226
Adam Gandelman6d271482014-08-05 18:12:29 -0700227 sudo mkdir -p $IRONIC_DATA_DIR
228 sudo mkdir -p $IRONIC_STATE_PATH
229 sudo mkdir -p $IRONIC_TFTPBOOT_DIR
230 sudo chown -R $STACK_USER $IRONIC_DATA_DIR $IRONIC_STATE_PATH
231 sudo chown -R $STACK_USER:$LIBVIRT_GROUP $IRONIC_TFTPBOOT_DIR
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100232 mkdir -p $IRONIC_TFTPBOOT_DIR/pxelinux.cfg
233
234 if [ ! -f $IRONIC_PXE_BOOT_IMAGE ]; then
235 die $LINENO "PXE boot file $IRONIC_PXE_BOOT_IMAGE not found."
Adam Gandelman6d271482014-08-05 18:12:29 -0700236 fi
237
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100238 cp $IRONIC_PXE_BOOT_IMAGE $IRONIC_TFTPBOOT_DIR
Adam Gandelman6d271482014-08-05 18:12:29 -0700239}
240
241# configure_ironic() - Set config files, create data dirs, etc
242function configure_ironic {
243 configure_ironic_dirs
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300244 sudo chown $STACK_USER $IRONIC_CONF_DIR
245
246 # Copy over ironic configuration file and configure common parameters.
247 cp $IRONIC_DIR/etc/ironic/ironic.conf.sample $IRONIC_CONF_FILE
248 iniset $IRONIC_CONF_FILE DEFAULT debug True
249 inicomment $IRONIC_CONF_FILE DEFAULT log_file
250 iniset $IRONIC_CONF_FILE DEFAULT sql_connection `database_connection_url ironic`
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400251 iniset $IRONIC_CONF_FILE DEFAULT state_path $IRONIC_STATE_PATH
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300252 iniset $IRONIC_CONF_FILE DEFAULT use_syslog $SYSLOG
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300253 # Configure Ironic conductor, if it was enabled.
254 if is_service_enabled ir-cond; then
255 configure_ironic_conductor
256 fi
257
258 # Configure Ironic API, if it was enabled.
259 if is_service_enabled ir-api; then
260 configure_ironic_api
261 fi
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400262
Adam Gandelmanab592932014-06-06 15:12:32 -0700263 # Format logging
264 if [ "$LOG_COLOR" == "True" ] && [ "$SYSLOG" == "False" ]; then
265 setup_colorized_logging $IRONIC_CONF_FILE DEFAULT
266 fi
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100267
268 if [[ "$IRONIC_IPXE_ENABLED" == "True" ]]; then
269 _config_ironic_apache_wsgi
270 fi
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300271}
272
273# configure_ironic_api() - Is used by configure_ironic(). Performs
274# API specific configuration.
Ian Wienandaee18c72014-02-21 15:35:08 +1100275function configure_ironic_api {
Roman Prykhodchenkoc48c3122013-10-01 17:19:05 +0300276 iniset $IRONIC_CONF_FILE DEFAULT auth_strategy keystone
277 iniset $IRONIC_CONF_FILE DEFAULT policy_file $IRONIC_POLICY_JSON
Brant Knudson05952372014-09-19 17:22:22 -0500278 configure_auth_token_middleware $IRONIC_CONF_FILE ironic $IRONIC_AUTH_CACHE_DIR/api
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300279 iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT
Adam Gandelman4b45fca2014-11-17 09:59:23 -0800280 iniset $IRONIC_CONF_FILE api port $IRONIC_SERVICE_PORT
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300281
282 cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON
283}
284
285# configure_ironic_conductor() - Is used by configure_ironic().
286# Sets conductor specific settings.
Ian Wienandaee18c72014-02-21 15:35:08 +1100287function configure_ironic_conductor {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300288 cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF
Lucas Alvares Gomes279295c2014-01-14 11:37:51 +0000289 cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_CONF_DIR
Dean Troyerb52caa32014-07-25 13:04:13 -0500290 local ironic_rootwrap=$(get_rootwrap_location ironic)
291 local rootwrap_isudoer_cmd="$ironic_rootwrap $IRONIC_CONF_DIR/rootwrap.conf *"
Adam Gandelman3f2a7b72014-04-14 16:14:33 -0700292
293 # Set up the rootwrap sudoers for ironic
Dean Troyerb52caa32014-07-25 13:04:13 -0500294 local tempfile=`mktemp`
295 echo "$STACK_USER ALL=(root) NOPASSWD: $rootwrap_isudoer_cmd" >$tempfile
296 chmod 0440 $tempfile
297 sudo chown root:root $tempfile
298 sudo mv $tempfile /etc/sudoers.d/ironic-rootwrap
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300299
Alexander Gordeeva67cb1a2014-03-04 18:38:33 +0400300 iniset $IRONIC_CONF_FILE DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF
Devananda van der Veen83eeed02014-04-11 10:08:41 -0700301 iniset $IRONIC_CONF_FILE DEFAULT enabled_drivers $IRONIC_ENABLED_DRIVERS
Adam Gandelman4b45fca2014-11-17 09:59:23 -0800302 iniset $IRONIC_CONF_FILE conductor api_url $IRONIC_SERVICE_PROTOCOL://$HOST_IP:$IRONIC_SERVICE_PORT
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700303 iniset $IRONIC_CONF_FILE pxe tftp_server $IRONIC_TFTPSERVER_IP
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400304 iniset $IRONIC_CONF_FILE pxe tftp_root $IRONIC_TFTPBOOT_DIR
305 iniset $IRONIC_CONF_FILE pxe tftp_master_path $IRONIC_TFTPBOOT_DIR/master_images
Adam Gandelman8af6fae2014-04-11 17:06:14 -0700306 if [[ "$IRONIC_VM_LOG_CONSOLE" == "True" ]] ; then
307 iniset $IRONIC_CONF_FILE pxe pxe_append_params "nofb nomodeset vga=normal console=ttyS0"
308 fi
yunhong jiangfe96ed62014-11-05 03:30:25 -0700309 if is_deployed_by_agent; then
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000310 if [[ "$SWIFT_ENABLE_TEMPURLS" == "True" ]] ; then
311 iniset $IRONIC_CONF_FILE glance swift_temp_url_key $SWIFT_TEMPURL_KEY
312 else
313 die $LINENO "SWIFT_ENABLE_TEMPURLS must be True to use agent_ssh driver in Ironic."
314 fi
Jim Rollenhagen74b28bc2014-10-29 14:15:22 -0700315 iniset $IRONIC_CONF_FILE glance swift_endpoint_url http://${HOST_IP}:${SWIFT_DEFAULT_BIND_PORT:-8080}
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000316 iniset $IRONIC_CONF_FILE glance swift_api_version v1
Adam Gandelmanab50c702014-10-22 13:21:22 -0700317 local tenant_id=$(get_or_create_project $SERVICE_TENANT_NAME)
318 iniset $IRONIC_CONF_FILE glance swift_account AUTH_${tenant_id}
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000319 iniset $IRONIC_CONF_FILE glance swift_container glance
320 iniset $IRONIC_CONF_FILE glance swift_temp_url_duration 3600
321 iniset $IRONIC_CONF_FILE agent heartbeat_timeout 30
322 if [[ "$IRONIC_VM_LOG_CONSOLE" == "True" ]] ; then
Jay Faulkner7f8a27d2014-11-24 11:12:20 -0800323 iniset $IRONIC_CONF_FILE agent agent_pxe_append_params "nofb nomodeset vga=normal console=ttyS0 systemd.journald.forward_to_console=yes"
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000324 fi
325 fi
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100326
327 if [[ "$IRONIC_IPXE_ENABLED" == "True" ]] ; then
328 local pxebin=`basename $IRONIC_PXE_BOOT_IMAGE`
329 iniset $IRONIC_CONF_FILE pxe ipxe_enabled True
330 iniset $IRONIC_CONF_FILE pxe pxe_config_template '\$pybasedir/drivers/modules/ipxe_config.template'
331 iniset $IRONIC_CONF_FILE pxe pxe_bootfile_name $pxebin
332 iniset $IRONIC_CONF_FILE pxe http_root $IRONIC_HTTP_DIR
333 iniset $IRONIC_CONF_FILE pxe http_url "http://$IRONIC_HTTP_SERVER:$IRONIC_HTTP_PORT"
334 fi
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300335}
336
337# create_ironic_cache_dir() - Part of the init_ironic() process
Ian Wienandaee18c72014-02-21 15:35:08 +1100338function create_ironic_cache_dir {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300339 # Create cache dir
340 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api
341 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api
342 rm -f $IRONIC_AUTH_CACHE_DIR/api/*
343 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry
344 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry
345 rm -f $IRONIC_AUTH_CACHE_DIR/registry/*
346}
347
348# create_ironic_accounts() - Set up common required ironic accounts
349
350# Tenant User Roles
351# ------------------------------------------------------------------
352# service ironic admin # if enabled
Ian Wienandaee18c72014-02-21 15:35:08 +1100353function create_ironic_accounts {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300354
Dean Troyerb52caa32014-07-25 13:04:13 -0500355 local service_tenant=$(openstack project list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }")
356 local admin_role=$(openstack role list | awk "/ admin / { print \$2 }")
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300357
358 # Ironic
359 if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100360 # Get ironic user if exists
361
Dean Troyerb52caa32014-07-25 13:04:13 -0500362 local ironic_user=$(get_or_create_user "ironic" \
363 "$SERVICE_PASSWORD" $service_tenant)
364 get_or_add_user_role $admin_role $ironic_user $service_tenant
Bartosz Górski0abde392014-02-28 14:15:19 +0100365
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300366 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100367
Dean Troyerb52caa32014-07-25 13:04:13 -0500368 local ironic_service=$(get_or_create_service "ironic" \
Bartosz Górski0abde392014-02-28 14:15:19 +0100369 "baremetal" "Ironic baremetal provisioning service")
Dean Troyerb52caa32014-07-25 13:04:13 -0500370 get_or_create_endpoint $ironic_service \
Bartosz Górski0abde392014-02-28 14:15:19 +0100371 "$REGION_NAME" \
372 "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
373 "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
374 "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300375 fi
376 fi
377}
378
379
380# init_ironic() - Initialize databases, etc.
Ian Wienandaee18c72014-02-21 15:35:08 +1100381function init_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300382 # (Re)create ironic database
383 recreate_database ironic utf8
384
385 # Migrate ironic database
Dmitry Tantsur0c6a6542014-06-02 20:19:33 +0200386 $IRONIC_BIN_DIR/ironic-dbsync --config-file=$IRONIC_CONF_FILE
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300387
388 create_ironic_cache_dir
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300389}
390
Adam Gandelman22ec45e2014-10-16 17:41:22 -0700391# _ironic_bm_vm_names() - Generates list of names for baremetal VMs.
392function _ironic_bm_vm_names {
393 local idx
394 local num_vms=$(($IRONIC_VM_COUNT - 1))
395 for idx in $(seq 0 $num_vms); do
396 echo "baremetal${IRONIC_VM_NETWORK_BRIDGE}_${idx}"
397 done
398}
399
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300400# start_ironic() - Start running processes, including screen
Ian Wienandaee18c72014-02-21 15:35:08 +1100401function start_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300402 # Start Ironic API server, if enabled.
403 if is_service_enabled ir-api; then
404 start_ironic_api
405 fi
406
407 # Start Ironic conductor, if enabled.
408 if is_service_enabled ir-cond; then
409 start_ironic_conductor
410 fi
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100411
412 # Start Apache if iPXE is enabled
413 if [[ "$IRONIC_IPXE_ENABLED" == "True" ]] ; then
414 restart_apache_server
415 fi
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300416}
417
418# start_ironic_api() - Used by start_ironic().
419# Starts Ironic API server.
Ian Wienandaee18c72014-02-21 15:35:08 +1100420function start_ironic_api {
Chris Dent2f27a0e2014-09-09 13:46:02 +0100421 run_process ir-api "$IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300422 echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..."
Adam Gandelman4b45fca2014-11-17 09:59:23 -0800423 if ! timeout $SERVICE_TIMEOUT sh -c "while ! wget --no-proxy -q -O- $IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT; do sleep 1; done"; then
Sean Dague101b4242013-10-22 08:47:11 -0400424 die $LINENO "ir-api did not start"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300425 fi
426}
427
428# start_ironic_conductor() - Used by start_ironic().
429# Starts Ironic conductor.
Ian Wienandaee18c72014-02-21 15:35:08 +1100430function start_ironic_conductor {
Chris Dent2f27a0e2014-09-09 13:46:02 +0100431 run_process ir-cond "$IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300432 # TODO(romcheg): Find a way to check whether the conductor has started.
433}
434
435# stop_ironic() - Stop running processes
Ian Wienandaee18c72014-02-21 15:35:08 +1100436function stop_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300437 # Kill the Ironic screen windows
438 screen -S $SCREEN_NAME -p ir-api -X kill
439 screen -S $SCREEN_NAME -p ir-cond -X kill
Lucas Alvares Gomes5851e5f2014-08-19 10:01:45 +0100440
441 # Cleanup the WSGI files
442 if [[ "$IRONIC_IPXE_ENABLED" == "True" ]] ; then
443 _cleanup_ironic_apache_wsgi
444 fi
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300445}
446
Adam Gandelman6d271482014-08-05 18:12:29 -0700447function create_ovs_taps {
448 local ironic_net_id=$(neutron net-list | grep private | get_field 1)
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400449
Adam Gandelman6d271482014-08-05 18:12:29 -0700450 # Work around: No netns exists on host until a Neutron port is created. We
451 # need to create one in Neutron to know what netns to tap into prior to the
452 # first node booting.
453 local port_id=$(neutron port-create private | grep " id " | get_field 2)
454
455 # intentional sleep to make sure the tag has been set to port
456 sleep 10
457
Adam Gandelman9bc34592014-12-02 16:49:14 -0800458 local tapdev=$(sudo ip netns exec qdhcp-${ironic_net_id} ip link list | grep " tap" | cut -d':' -f2 | cut -b2-)
Adam Gandelman6d271482014-08-05 18:12:29 -0700459 local tag_id=$(sudo ovs-vsctl show |grep ${tapdev} -A1 -m1 | grep tag | cut -d':' -f2 | cut -b2-)
460
461 # make sure veth pair is not existing, otherwise delete its links
462 sudo ip link show ovs-tap1 && sudo ip link delete ovs-tap1
463 sudo ip link show brbm-tap1 && sudo ip link delete brbm-tap1
464 # create veth pair for future interconnection between br-int and brbm
465 sudo ip link add brbm-tap1 type veth peer name ovs-tap1
466 sudo ip link set dev brbm-tap1 up
467 sudo ip link set dev ovs-tap1 up
468
469 sudo ovs-vsctl -- --if-exists del-port ovs-tap1 -- add-port br-int ovs-tap1 tag=$tag_id
470 sudo ovs-vsctl -- --if-exists del-port brbm-tap1 -- add-port $IRONIC_VM_NETWORK_BRIDGE brbm-tap1
471
472 # Remove the port needed only for workaround.
473 neutron port-delete $port_id
Adam Gandelmancec97922014-07-29 18:43:18 -0700474
475 # Finally, share the fixed tenant network across all tenants. This allows the host
476 # to serve TFTP to a single network namespace via the tap device created above.
477 neutron net-update $ironic_net_id --shared true
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400478}
479
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400480function create_bridge_and_vms {
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400481 # Call libvirt setup scripts in a new shell to ensure any new group membership
482 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/setup-network"
Adam Gandelman8af6fae2014-04-11 17:06:14 -0700483 if [[ "$IRONIC_VM_LOG_CONSOLE" == "True" ]] ; then
Dean Troyerb52caa32014-07-25 13:04:13 -0500484 local log_arg="$IRONIC_VM_LOG_DIR"
Adam Gandelman8af6fae2014-04-11 17:06:14 -0700485 else
Dean Troyerb52caa32014-07-25 13:04:13 -0500486 local log_arg=""
Adam Gandelman8af6fae2014-04-11 17:06:14 -0700487 fi
Adam Gandelman22ec45e2014-10-16 17:41:22 -0700488 local vm_name
489 for vm_name in $(_ironic_bm_vm_names); do
490 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/create-node $vm_name \
491 $IRONIC_VM_SPECS_CPU $IRONIC_VM_SPECS_RAM $IRONIC_VM_SPECS_DISK \
492 amd64 $IRONIC_VM_NETWORK_BRIDGE $IRONIC_VM_EMULATOR \
493 $log_arg" >> $IRONIC_VM_MACS_CSV_FILE
494 done
Adam Gandelman6d271482014-08-05 18:12:29 -0700495 create_ovs_taps
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400496}
497
Adam Gandelmanc78e4672014-12-01 11:24:37 -0800498function wait_for_nova_resources {
Adam Gandelman0c99e2f2014-12-09 14:44:24 -0800499 # After nodes have been enrolled, we need to wait for both ironic and
500 # nova's periodic tasks to populate the resource tracker with available
501 # nodes and resources. Wait up to 2 minutes for a given resource before
502 # timing out.
503 local resource=$1
504 local expected_count=$2
505 echo_summary "Waiting 2 minutes for Nova resource tracker to pick up $resource >= $expected_count"
Adam Gandelmanc78e4672014-12-01 11:24:37 -0800506 for i in $(seq 1 120); do
Adam Gandelman0c99e2f2014-12-09 14:44:24 -0800507 if [ $(nova hypervisor-stats | grep " $resource " | get_field 2) -ge $expected_count ]; then
Adam Gandelmanc78e4672014-12-01 11:24:37 -0800508 return 0
509 fi
510 sleep 1
511 done
Adam Gandelman0c99e2f2014-12-09 14:44:24 -0800512 die $LINENO "Timed out waiting for Nova hypervisor-stats $resource >= $expected_count"
Adam Gandelmanc78e4672014-12-01 11:24:37 -0800513}
514
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700515function enroll_nodes {
Dean Troyerb52caa32014-07-25 13:04:13 -0500516 local chassis_id=$(ironic chassis-create -d "ironic test chassis" | grep " uuid " | get_field 2)
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000517
518 if [[ "$IRONIC_DEPLOY_DRIVER" == "pxe_ssh" ]] ; then
519 local _IRONIC_DEPLOY_KERNEL_KEY=pxe_deploy_kernel
520 local _IRONIC_DEPLOY_RAMDISK_KEY=pxe_deploy_ramdisk
yunhong jiangfe96ed62014-11-05 03:30:25 -0700521 elif is_deployed_by_agent; then
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000522 local _IRONIC_DEPLOY_KERNEL_KEY=deploy_kernel
523 local _IRONIC_DEPLOY_RAMDISK_KEY=deploy_ramdisk
524 fi
525
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700526 if ! is_ironic_hardware; then
527 local ironic_node_cpu=$IRONIC_VM_SPECS_CPU
528 local ironic_node_ram=$IRONIC_VM_SPECS_RAM
529 local ironic_node_disk=$IRONIC_VM_SPECS_DISK
530 local ironic_ephemeral_disk=$IRONIC_VM_EPHEMERAL_DISK
531 local ironic_hwinfo_file=$IRONIC_VM_MACS_CSV_FILE
532 local node_options="\
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000533 -i $_IRONIC_DEPLOY_KERNEL_KEY=$IRONIC_DEPLOY_KERNEL_ID \
534 -i $_IRONIC_DEPLOY_RAMDISK_KEY=$IRONIC_DEPLOY_RAMDISK_ID \
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400535 -i ssh_virt_type=$IRONIC_SSH_VIRT_TYPE \
536 -i ssh_address=$IRONIC_VM_SSH_ADDRESS \
537 -i ssh_port=$IRONIC_VM_SSH_PORT \
538 -i ssh_username=$IRONIC_SSH_USERNAME \
Adam Gandelmand4a6e352014-12-11 14:53:17 -0800539 -i ssh_key_filename=$IRONIC_KEY_FILE"
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700540 else
541 local ironic_node_cpu=$IRONIC_HW_NODE_CPU
542 local ironic_node_ram=$IRONIC_HW_NODE_RAM
543 local ironic_node_disk=$IRONIC_HW_NODE_DISK
544 local ironic_ephemeral_disk=$IRONIC_HW_EPHEMERAL_DISK
545 if [[ -z "${IRONIC_DEPLOY_DRIVER##*_ipmitool}" ]]; then
546 local ironic_hwinfo_file=$IRONIC_IPMIINFO_FILE
547 fi
548 fi
549
Adam Gandelmanc78e4672014-12-01 11:24:37 -0800550 local total_nodes=0
Adam Gandelman0c99e2f2014-12-09 14:44:24 -0800551 local total_cpus=0
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700552 while read hardware_info; do
553 if ! is_ironic_hardware; then
554 local mac_address=$hardware_info
555 elif [[ -z "${IRONIC_DEPLOY_DRIVER##*_ipmitool}" ]]; then
556 local ipmi_address=$(echo $hardware_info |awk '{print $1}')
557 local mac_address=$(echo $hardware_info |awk '{print $2}')
558 local ironic_ipmi_username=$(echo $hardware_info |awk '{print $3}')
559 local ironic_ipmi_passwd=$(echo $hardware_info |awk '{print $4}')
560 # Currently we require all hardware platform have same CPU/RAM/DISK info
561 # in future, this can be enhanced to support different type, and then
562 # we create the bare metal flavor with minimum value
563 local node_options="-i ipmi_address=$ipmi_address -i ipmi_password=$ironic_ipmi_passwd\
564 -i ipmi_username=$ironic_ipmi_username"
Yuiko Takadab7d5bf62014-11-20 18:23:06 +0900565 node_options+=" -i $_IRONIC_DEPLOY_KERNEL_KEY=$IRONIC_DEPLOY_KERNEL_ID"
566 node_options+=" -i $_IRONIC_DEPLOY_RAMDISK_KEY=$IRONIC_DEPLOY_RAMDISK_ID"
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700567 fi
568
569 local node_id=$(ironic node-create --chassis_uuid $chassis_id \
570 --driver $IRONIC_DEPLOY_DRIVER \
571 -p cpus=$ironic_node_cpu\
572 -p memory_mb=$ironic_node_ram\
573 -p local_gb=$ironic_node_disk\
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400574 -p cpu_arch=x86_64 \
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700575 $node_options \
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400576 | grep " uuid " | get_field 2)
577
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700578 ironic port-create --address $mac_address --node_uuid $node_id
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400579
Adam Gandelmanc78e4672014-12-01 11:24:37 -0800580 total_nodes=$((total_nodes+1))
Adam Gandelman0c99e2f2014-12-09 14:44:24 -0800581 total_cpus=$((total_cpus+$ironic_node_cpu))
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700582 done < $ironic_hwinfo_file
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400583
584 # create the nova flavor
Adam Gandelman29c3d632014-07-17 12:53:21 -0700585 # NOTE(adam_g): Attempting to use an autogenerated UUID for flavor id here uncovered
586 # bug (LP: #1333852) in Trove. This can be changed to use an auto flavor id when the
587 # bug is fixed in Juno.
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700588 local adjusted_disk=$(($ironic_node_disk - $ironic_ephemeral_disk))
589 nova flavor-create --ephemeral $ironic_ephemeral_disk baremetal 551 $ironic_node_ram $adjusted_disk $ironic_node_cpu
Adam Gandelman29c3d632014-07-17 12:53:21 -0700590
Yuiko Takadab7d5bf62014-11-20 18:23:06 +0900591 nova flavor-key baremetal set "cpu_arch"="x86_64"
Adam Gandelmanc78e4672014-12-01 11:24:37 -0800592
593 if [ "$VIRT_DRIVER" == "ironic" ]; then
Adam Gandelman0c99e2f2014-12-09 14:44:24 -0800594 wait_for_nova_resources "count" $total_nodes
595 wait_for_nova_resources "vcpus" $total_cpus
Adam Gandelmanc78e4672014-12-01 11:24:37 -0800596 fi
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400597}
598
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700599function configure_iptables {
600 # enable tftp natting for allowing connections to HOST_IP's tftp server
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400601 sudo modprobe nf_conntrack_tftp
602 sudo modprobe nf_nat_tftp
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700603 # nodes boot from TFTP and callback to the API server listening on $HOST_IP
604 sudo iptables -I INPUT -d $HOST_IP -p udp --dport 69 -j ACCEPT || true
Adam Gandelman4b45fca2014-11-17 09:59:23 -0800605 sudo iptables -I INPUT -d $HOST_IP -p tcp --dport $IRONIC_SERVICE_PORT -j ACCEPT || true
yunhong jiangfe96ed62014-11-05 03:30:25 -0700606 if is_deployed_by_agent; then
Jim Rollenhagen74b28bc2014-10-29 14:15:22 -0700607 # agent ramdisk gets instance image from swift
608 sudo iptables -I INPUT -d $HOST_IP -p tcp --dport ${SWIFT_DEFAULT_BIND_PORT:-8080} -j ACCEPT || true
609 fi
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700610}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400611
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700612function configure_tftpd {
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400613 # stop tftpd and setup serving via xinetd
614 stop_service tftpd-hpa || true
615 [ -f /etc/init/tftpd-hpa.conf ] && echo "manual" | sudo tee /etc/init/tftpd-hpa.override
616 sudo cp $IRONIC_TEMPLATES_DIR/tftpd-xinetd.template /etc/xinetd.d/tftp
617 sudo sed -e "s|%TFTPBOOT_DIR%|$IRONIC_TFTPBOOT_DIR|g" -i /etc/xinetd.d/tftp
618
619 # setup tftp file mapping to satisfy requests at the root (booting) and
620 # /tftpboot/ sub-dir (as per deploy-ironic elements)
621 echo "r ^([^/]) $IRONIC_TFTPBOOT_DIR/\1" >$IRONIC_TFTPBOOT_DIR/map-file
622 echo "r ^(/tftpboot/) $IRONIC_TFTPBOOT_DIR/\2" >>$IRONIC_TFTPBOOT_DIR/map-file
623
624 chmod -R 0755 $IRONIC_TFTPBOOT_DIR
625 restart_service xinetd
626}
627
628function configure_ironic_ssh_keypair {
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400629 if [[ ! -d $HOME/.ssh ]]; then
630 mkdir -p $HOME/.ssh
631 chmod 700 $HOME/.ssh
632 fi
Adam Gandelman6c749322014-12-02 16:20:50 -0800633 if [[ ! -e $IRONIC_KEY_FILE ]]; then
634 if [[ ! -d $(dirname $IRONIC_KEY_FILE) ]]; then
635 mkdir -p $(dirname $IRONIC_KEY_FILE)
636 fi
637 echo -e 'n\n' | ssh-keygen -q -t rsa -P '' -f $IRONIC_KEY_FILE
638 fi
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400639 cat $IRONIC_KEY_FILE.pub | tee -a $IRONIC_AUTHORIZED_KEYS_FILE
640}
641
642function ironic_ssh_check {
Dean Troyerb52caa32014-07-25 13:04:13 -0500643 local key_file=$1
644 local floating_ip=$2
645 local port=$3
646 local default_instance_user=$4
647 local active_timeout=$5
648 if ! timeout $active_timeout sh -c "while ! ssh -p $port -o StrictHostKeyChecking=no -i $key_file ${default_instance_user}@$floating_ip echo success; do sleep 1; done"; then
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400649 die $LINENO "server didn't become ssh-able!"
650 fi
651}
652
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400653function configure_ironic_auxiliary {
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400654 configure_ironic_ssh_keypair
Adam Gandelman6c749322014-12-02 16:20:50 -0800655 ironic_ssh_check $IRONIC_KEY_FILE $IRONIC_VM_SSH_ADDRESS $IRONIC_VM_SSH_PORT $IRONIC_SSH_USERNAME 10
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400656}
657
Jim Rollenhagenf00766b2014-08-15 01:08:11 +0000658function build_ipa_coreos_ramdisk {
659 echo "Building ironic-python-agent deploy ramdisk"
660 local kernel_path=$1
661 local ramdisk_path=$2
662 git_clone $IRONIC_PYTHON_AGENT_REPO $IRONIC_PYTHON_AGENT_DIR $IRONIC_PYTHON_AGENT_BRANCH
663 cd $IRONIC_PYTHON_AGENT_DIR
664 imagebuild/coreos/build_coreos_image.sh
665 cp imagebuild/coreos/UPLOAD/coreos_production_pxe_image-oem.cpio.gz $ramdisk_path
666 cp imagebuild/coreos/UPLOAD/coreos_production_pxe.vmlinuz $kernel_path
667 sudo rm -rf UPLOAD
668 cd -
669}
670
Alexander Gordeevf177f722014-03-14 18:44:48 +0400671# build deploy kernel+ramdisk, then upload them to glance
Dean Troyerb52caa32014-07-25 13:04:13 -0500672# this function sets ``IRONIC_DEPLOY_KERNEL_ID``, ``IRONIC_DEPLOY_RAMDISK_ID``
Alexander Gordeevf177f722014-03-14 18:44:48 +0400673function upload_baremetal_ironic_deploy {
Dean Troyerb52caa32014-07-25 13:04:13 -0500674 declare -g IRONIC_DEPLOY_KERNEL_ID IRONIC_DEPLOY_RAMDISK_ID
Adam Gandelman6d271482014-08-05 18:12:29 -0700675 echo_summary "Creating and uploading baremetal images for ironic"
676
677 # install diskimage-builder
Steve Bakercbfb3ae2014-05-05 16:06:47 +1200678 if [[ $(type -P ramdisk-image-create) == "" ]]; then
679 pip_install diskimage_builder
680 fi
Alexander Gordeevf177f722014-03-14 18:44:48 +0400681
682 if [ -z "$IRONIC_DEPLOY_KERNEL" -o -z "$IRONIC_DEPLOY_RAMDISK" ]; then
yunhong jiangfe96ed62014-11-05 03:30:25 -0700683 local IRONIC_DEPLOY_KERNEL_PATH=$TOP_DIR/files/ir-deploy-$IRONIC_DEPLOY_DRIVER.kernel
684 local IRONIC_DEPLOY_RAMDISK_PATH=$TOP_DIR/files/ir-deploy-$IRONIC_DEPLOY_DRIVER.initramfs
Alexander Gordeevf177f722014-03-14 18:44:48 +0400685 else
Dean Troyerb52caa32014-07-25 13:04:13 -0500686 local IRONIC_DEPLOY_KERNEL_PATH=$IRONIC_DEPLOY_KERNEL
687 local IRONIC_DEPLOY_RAMDISK_PATH=$IRONIC_DEPLOY_RAMDISK
Alexander Gordeevf177f722014-03-14 18:44:48 +0400688 fi
689
690 if [ ! -e "$IRONIC_DEPLOY_RAMDISK_PATH" -o ! -e "$IRONIC_DEPLOY_KERNEL_PATH" ]; then
691 # files don't exist, need to build them
692 if [ "$IRONIC_BUILD_DEPLOY_RAMDISK" = "True" ]; then
693 # we can build them only if we're not offline
694 if [ "$OFFLINE" != "True" ]; then
yunhong jiangfe96ed62014-11-05 03:30:25 -0700695 if is_deployed_by_agent; then
Jim Rollenhagenf00766b2014-08-15 01:08:11 +0000696 build_ipa_coreos_ramdisk $IRONIC_DEPLOY_KERNEL_PATH $IRONIC_DEPLOY_RAMDISK_PATH
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000697 else
Steve Bakercbfb3ae2014-05-05 16:06:47 +1200698 ramdisk-image-create $IRONIC_DEPLOY_FLAVOR \
yunhong jiangfe96ed62014-11-05 03:30:25 -0700699 -o $TOP_DIR/files/ir-deploy-$IRONIC_DEPLOY_DRIVER
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000700 fi
Alexander Gordeevf177f722014-03-14 18:44:48 +0400701 else
702 die $LINENO "Deploy kernel+ramdisk files don't exist and cannot be build in OFFLINE mode"
703 fi
704 else
yunhong jiangfe96ed62014-11-05 03:30:25 -0700705 if is_deployed_by_agent; then
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000706 # download the agent image tarball
Jim Rollenhagen792dc5f2014-08-27 17:03:00 -0700707 wget "$IRONIC_AGENT_KERNEL_URL" -O $IRONIC_DEPLOY_KERNEL_PATH
708 wget "$IRONIC_AGENT_RAMDISK_URL" -O $IRONIC_DEPLOY_RAMDISK_PATH
Jim Rollenhagena6a45462014-08-05 18:08:29 +0000709 else
710 die $LINENO "Deploy kernel+ramdisk files don't exist and their building was disabled explicitly by IRONIC_BUILD_DEPLOY_RAMDISK"
711 fi
Alexander Gordeevf177f722014-03-14 18:44:48 +0400712 fi
713 fi
714
Adam Gandelman6d271482014-08-05 18:12:29 -0700715 local token=$(keystone token-get | grep ' id ' | get_field 2)
716 die_if_not_set $LINENO token "Keystone fail to get token"
717
Alexander Gordeevf177f722014-03-14 18:44:48 +0400718 # load them into glance
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400719 IRONIC_DEPLOY_KERNEL_ID=$(openstack \
720 --os-token $token \
721 --os-url http://$GLANCE_HOSTPORT \
722 image create \
723 $(basename $IRONIC_DEPLOY_KERNEL_PATH) \
724 --public --disk-format=aki \
Jim Rollenhagen722284f2014-08-15 18:04:02 +0000725 --container-format=aki \
Alexander Gordeevf177f722014-03-14 18:44:48 +0400726 < $IRONIC_DEPLOY_KERNEL_PATH | grep ' id ' | get_field 2)
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400727 IRONIC_DEPLOY_RAMDISK_ID=$(openstack \
728 --os-token $token \
729 --os-url http://$GLANCE_HOSTPORT \
730 image create \
731 $(basename $IRONIC_DEPLOY_RAMDISK_PATH) \
732 --public --disk-format=ari \
Jim Rollenhagen722284f2014-08-15 18:04:02 +0000733 --container-format=ari \
Alexander Gordeevf177f722014-03-14 18:44:48 +0400734 < $IRONIC_DEPLOY_RAMDISK_PATH | grep ' id ' | get_field 2)
735}
736
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400737function prepare_baremetal_basic_ops {
Adam Gandelman03546402014-12-05 16:49:12 -0800738 if ! is_ironic_hardware; then
739 configure_ironic_auxiliary
740 fi
Adam Gandelman6d271482014-08-05 18:12:29 -0700741 upload_baremetal_ironic_deploy
yunhong jiangae9ee6b2014-10-08 07:01:02 -0700742 if ! is_ironic_hardware; then
743 create_bridge_and_vms
744 fi
745 enroll_nodes
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400746 configure_tftpd
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700747 configure_iptables
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400748}
749
750function cleanup_baremetal_basic_ops {
751 rm -f $IRONIC_VM_MACS_CSV_FILE
752 if [ -f $IRONIC_KEY_FILE ]; then
Dean Troyerb52caa32014-07-25 13:04:13 -0500753 local key=$(cat $IRONIC_KEY_FILE.pub)
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400754 # remove public key from authorized_keys
Dean Troyerb52caa32014-07-25 13:04:13 -0500755 grep -v "$key" $IRONIC_AUTHORIZED_KEYS_FILE > temp && mv temp $IRONIC_AUTHORIZED_KEYS_FILE
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400756 chmod 0600 $IRONIC_AUTHORIZED_KEYS_FILE
757 fi
758 sudo rm -rf $IRONIC_DATA_DIR $IRONIC_STATE_PATH
Adam Gandelman22ec45e2014-10-16 17:41:22 -0700759
760 local vm_name
761 for vm_name in $(_ironic_bm_vm_names); do
762 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/cleanup-node $vm_name $IRONIC_VM_NETWORK_BRIDGE"
763 done
764
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400765 sudo rm -rf /etc/xinetd.d/tftp /etc/init/tftpd-hpa.override
766 restart_service xinetd
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700767 sudo iptables -D INPUT -d $HOST_IP -p udp --dport 69 -j ACCEPT || true
Adam Gandelman4b45fca2014-11-17 09:59:23 -0800768 sudo iptables -D INPUT -d $HOST_IP -p tcp --dport $IRONIC_SERVICE_PORT -j ACCEPT || true
yunhong jiangfe96ed62014-11-05 03:30:25 -0700769 if is_deployed_by_agent; then
Jim Rollenhagen74b28bc2014-10-29 14:15:22 -0700770 # agent ramdisk gets instance image from swift
771 sudo iptables -D INPUT -d $HOST_IP -p tcp --dport ${SWIFT_DEFAULT_BIND_PORT:-8080} -j ACCEPT || true
772 fi
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700773 sudo rmmod nf_conntrack_tftp || true
774 sudo rmmod nf_nat_tftp || true
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400775}
776
777# Restore xtrace + pipefail
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300778$XTRACE
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400779$PIPEFAIL
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300780
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100781# Tell emacs to use shell-script-mode
782## Local variables:
783## mode: shell-script
784## End: