blob: e62951bbb1287fa3edf5ea7f27fcb9435fae707c [file] [log] [blame]
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +03001# lib/ironic
2# Functions to control the configuration and operation of the **Ironic** service
3
4# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# - ``functions`` file
7# - ``DEST``, ``DATA_DIR``, ``STACK_USER`` must be defined
8# - ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
9# - ``SERVICE_HOST``
10# - ``KEYSTONE_TOKEN_FORMAT`` must be defined
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030011
12# ``stack.sh`` calls the entry points in this order:
13#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010014# - install_ironic
15# - install_ironicclient
16# - init_ironic
17# - start_ironic
18# - stop_ironic
19# - cleanup_ironic
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030020
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040021# Save trace and pipefail settings
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030022XTRACE=$(set +o | grep xtrace)
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040023PIPEFAIL=$(set +o | grep pipefail)
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030024set +o xtrace
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040025set +o pipefail
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030026
27# Defaults
28# --------
29
30# Set up default directories
31IRONIC_DIR=$DEST/ironic
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040032IRONIC_DATA_DIR=$DATA_DIR/ironic
33IRONIC_STATE_PATH=/var/lib/ironic
Roman Prykhodchenko43e00662013-10-15 17:03:15 +030034IRONICCLIENT_DIR=$DEST/python-ironicclient
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030035IRONIC_AUTH_CACHE_DIR=${IRONIC_AUTH_CACHE_DIR:-/var/cache/ironic}
36IRONIC_CONF_DIR=${IRONIC_CONF_DIR:-/etc/ironic}
37IRONIC_CONF_FILE=$IRONIC_CONF_DIR/ironic.conf
38IRONIC_ROOTWRAP_CONF=$IRONIC_CONF_DIR/rootwrap.conf
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030039IRONIC_POLICY_JSON=$IRONIC_CONF_DIR/policy.json
40
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040041# Set up defaults for functional / integration testing
42IRONIC_SCRIPTS_DIR=${IRONIC_SCRIPTS_DIR:-$TOP_DIR/tools/ironic/scripts}
43IRONIC_TEMPLATES_DIR=${IRONIC_TEMPLATES_DIR:-$TOP_DIR/tools/ironic/templates}
44IRONIC_BAREMETAL_BASIC_OPS=$(trueorfalse False $IRONIC_BAREMETAL_BASIC_OPS)
45IRONIC_SSH_USERNAME=${IRONIC_SSH_USERNAME:-`whoami`}
46IRONIC_SSH_KEY_DIR=${IRONIC_SSH_KEY_DIR:-$IRONIC_DATA_DIR/ssh_keys}
47IRONIC_SSH_KEY_FILENAME=${IRONIC_SSH_KEY_FILENAME:-ironic_key}
48IRONIC_KEY_FILE=$IRONIC_SSH_KEY_DIR/$IRONIC_SSH_KEY_FILENAME
49IRONIC_SSH_VIRT_TYPE=${IRONIC_SSH_VIRT_TYPE:-virsh}
50IRONIC_TFTPBOOT_DIR=${IRONIC_TFTPBOOT_DIR:-$IRONIC_DATA_DIR/tftpboot}
51IRONIC_VM_SSH_PORT=${IRONIC_VM_SSH_PORT:-2222}
52IRONIC_VM_SSH_ADDRESS=${IRONIC_VM_SSH_ADDRESS:-$HOST_IP}
53IRONIC_VM_COUNT=${IRONIC_VM_COUNT:-1}
54IRONIC_VM_SPECS_CPU=${IRONIC_VM_SPECS_CPU:-1}
55IRONIC_VM_SPECS_RAM=${IRONIC_VM_SPECS_RAM:-256}
56IRONIC_VM_SPECS_DISK=${IRONIC_VM_SPECS_DISK:-10}
57IRONIC_VM_EMULATOR=${IRONIC_VM_EMULATOR:-/usr/bin/qemu-system-x86_64}
58IRONIC_VM_NETWORK_BRIDGE=${IRONIC_VM_NETWORK_BRIDGE:-brbm}
59IRONIC_VM_NETWORK_RANGE=${IRONIC_VM_NETWORK_RANGE:-192.0.2.0/24}
60IRONIC_VM_MACS_CSV_FILE=${IRONIC_VM_MACS_CSV_FILE:-$IRONIC_DATA_DIR/ironic_macs.csv}
61IRONIC_AUTHORIZED_KEYS_FILE=${IRONIC_AUTHORIZED_KEYS_FILE:-$HOME/.ssh/authorized_keys}
62
Alexander Gordeevf177f722014-03-14 18:44:48 +040063DIB_DIR=${DIB_DIR:-$DEST/diskimage-builder}
64
65# Use DIB to create deploy ramdisk and kernel.
66IRONIC_BUILD_DEPLOY_RAMDISK=`trueorfalse True $IRONIC_BUILD_DEPLOY_RAMDISK`
67# If not use DIB, these files are used as deploy ramdisk/kernel.
68# (The value must be a absolute path)
69IRONIC_DEPLOY_RAMDISK=${IRONIC_DEPLOY_RAMDISK:-}
70IRONIC_DEPLOY_KERNEL=${IRONIC_DEPLOY_KERNEL:-}
71IRONIC_DEPLOY_ELEMENT=${IRONIC_DEPLOY_ELEMENT:-deploy-ironic}
72
73#TODO(agordeev): replace 'ubuntu' with host distro name getting
74IRONIC_DEPLOY_FLAVOR=${IRONIC_DEPLOY_FLAVOR:-ubuntu $IRONIC_DEPLOY_ELEMENT}
75
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030076# Support entry points installation of console scripts
77IRONIC_BIN_DIR=$(get_python_exec_prefix)
78
79# Ironic connection info. Note the port must be specified.
80IRONIC_SERVICE_PROTOCOL=http
81IRONIC_HOSTPORT=${IRONIC_HOSTPORT:-$SERVICE_HOST:6385}
82
Dean Troyer4237f592014-01-29 16:22:11 -060083# Tell Tempest this project is present
84TEMPEST_SERVICES+=,ironic
85
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030086
87# Functions
88# ---------
89
Dean Troyer1023ff72014-01-27 14:56:44 -060090# Test if any Ironic services are enabled
91# is_ironic_enabled
92function is_ironic_enabled {
93 [[ ,${ENABLED_SERVICES} =~ ,"ir-" ]] && return 0
94 return 1
95}
96
Roman Prykhodchenko43e00662013-10-15 17:03:15 +030097# install_ironic() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +110098function install_ironic {
Roman Prykhodchenko43e00662013-10-15 17:03:15 +030099 git_clone $IRONIC_REPO $IRONIC_DIR $IRONIC_BRANCH
100 setup_develop $IRONIC_DIR
101}
102
103# install_ironicclient() - Collect sources and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100104function install_ironicclient {
Roman Prykhodchenko43e00662013-10-15 17:03:15 +0300105 git_clone $IRONICCLIENT_REPO $IRONICCLIENT_DIR $IRONICCLIENT_BRANCH
106 setup_develop $IRONICCLIENT_DIR
107}
108
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300109# cleanup_ironic() - Remove residual data files, anything left over from previous
110# runs that would need to clean up.
Ian Wienandaee18c72014-02-21 15:35:08 +1100111function cleanup_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300112 sudo rm -rf $IRONIC_AUTH_CACHE_DIR
113}
114
115# configure_ironic() - Set config files, create data dirs, etc
Ian Wienandaee18c72014-02-21 15:35:08 +1100116function configure_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300117 if [[ ! -d $IRONIC_CONF_DIR ]]; then
118 sudo mkdir -p $IRONIC_CONF_DIR
119 fi
120 sudo chown $STACK_USER $IRONIC_CONF_DIR
121
122 # Copy over ironic configuration file and configure common parameters.
123 cp $IRONIC_DIR/etc/ironic/ironic.conf.sample $IRONIC_CONF_FILE
124 iniset $IRONIC_CONF_FILE DEFAULT debug True
125 inicomment $IRONIC_CONF_FILE DEFAULT log_file
126 iniset $IRONIC_CONF_FILE DEFAULT sql_connection `database_connection_url ironic`
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400127 iniset $IRONIC_CONF_FILE DEFAULT state_path $IRONIC_STATE_PATH
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300128 iniset $IRONIC_CONF_FILE DEFAULT use_syslog $SYSLOG
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300129 # Configure Ironic conductor, if it was enabled.
130 if is_service_enabled ir-cond; then
131 configure_ironic_conductor
132 fi
133
134 # Configure Ironic API, if it was enabled.
135 if is_service_enabled ir-api; then
136 configure_ironic_api
137 fi
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400138
139 if [[ "$IRONIC_BAREMETAL_BASIC_OPS" == "True" ]]; then
140 configure_ironic_auxiliary
141 fi
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300142}
143
144# configure_ironic_api() - Is used by configure_ironic(). Performs
145# API specific configuration.
Ian Wienandaee18c72014-02-21 15:35:08 +1100146function configure_ironic_api {
Roman Prykhodchenkoc48c3122013-10-01 17:19:05 +0300147 iniset $IRONIC_CONF_FILE DEFAULT auth_strategy keystone
148 iniset $IRONIC_CONF_FILE DEFAULT policy_file $IRONIC_POLICY_JSON
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300149 iniset $IRONIC_CONF_FILE keystone_authtoken auth_host $KEYSTONE_AUTH_HOST
150 iniset $IRONIC_CONF_FILE keystone_authtoken auth_port $KEYSTONE_AUTH_PORT
151 iniset $IRONIC_CONF_FILE keystone_authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000152 iniset $IRONIC_CONF_FILE keystone_authtoken cafile $KEYSTONE_SSL_CA
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300153 iniset $IRONIC_CONF_FILE keystone_authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/
154 iniset $IRONIC_CONF_FILE keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME
155 iniset $IRONIC_CONF_FILE keystone_authtoken admin_user ironic
156 iniset $IRONIC_CONF_FILE keystone_authtoken admin_password $SERVICE_PASSWORD
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300157 iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT
158 iniset $IRONIC_CONF_FILE keystone_authtoken signing_dir $IRONIC_AUTH_CACHE_DIR/api
159
160 cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON
161}
162
163# configure_ironic_conductor() - Is used by configure_ironic().
164# Sets conductor specific settings.
Ian Wienandaee18c72014-02-21 15:35:08 +1100165function configure_ironic_conductor {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300166 cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF
Lucas Alvares Gomes279295c2014-01-14 11:37:51 +0000167 cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_CONF_DIR
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300168
Alexander Gordeeva67cb1a2014-03-04 18:38:33 +0400169 iniset $IRONIC_CONF_FILE DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400170 iniset $IRONIC_CONF_FILE conductor api_url http://$SERVICE_HOST:6385
171 iniset $IRONIC_CONF_FILE pxe tftp_server $SERVICE_HOST
172 iniset $IRONIC_CONF_FILE pxe tftp_root $IRONIC_TFTPBOOT_DIR
173 iniset $IRONIC_CONF_FILE pxe tftp_master_path $IRONIC_TFTPBOOT_DIR/master_images
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300174}
175
176# create_ironic_cache_dir() - Part of the init_ironic() process
Ian Wienandaee18c72014-02-21 15:35:08 +1100177function create_ironic_cache_dir {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300178 # Create cache dir
179 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api
180 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api
181 rm -f $IRONIC_AUTH_CACHE_DIR/api/*
182 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry
183 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry
184 rm -f $IRONIC_AUTH_CACHE_DIR/registry/*
185}
186
187# create_ironic_accounts() - Set up common required ironic accounts
188
189# Tenant User Roles
190# ------------------------------------------------------------------
191# service ironic admin # if enabled
Ian Wienandaee18c72014-02-21 15:35:08 +1100192function create_ironic_accounts {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300193
Steve Martinelli19685422014-01-24 13:02:26 -0600194 SERVICE_TENANT=$(openstack project list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }")
195 ADMIN_ROLE=$(openstack role list | awk "/ admin / { print \$2 }")
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300196
197 # Ironic
198 if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then
Steve Martinelli19685422014-01-24 13:02:26 -0600199 IRONIC_USER=$(openstack user create \
200 ironic \
201 --password "$SERVICE_PASSWORD" \
202 --project $SERVICE_TENANT \
203 --email ironic@example.com \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300204 | grep " id " | get_field 2)
Steve Martinelli19685422014-01-24 13:02:26 -0600205 openstack role add \
206 $ADMIN_ROLE \
207 --project $SERVICE_TENANT \
208 --user $IRONIC_USER
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300209 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
Steve Martinelli19685422014-01-24 13:02:26 -0600210 IRONIC_SERVICE=$(openstack service create \
211 ironic \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300212 --type=baremetal \
213 --description="Ironic baremetal provisioning service" \
214 | grep " id " | get_field 2)
Steve Martinelli19685422014-01-24 13:02:26 -0600215 openstack endpoint create \
216 $IRONIC_SERVICE \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300217 --region RegionOne \
Roman Prykhodchenkof5002ef2013-09-24 19:09:26 +0300218 --publicurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
219 --adminurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
220 --internalurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300221 fi
222 fi
223}
224
225
226# init_ironic() - Initialize databases, etc.
Ian Wienandaee18c72014-02-21 15:35:08 +1100227function init_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300228 # (Re)create ironic database
229 recreate_database ironic utf8
230
231 # Migrate ironic database
232 $IRONIC_BIN_DIR/ironic-dbsync
233
234 create_ironic_cache_dir
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300235}
236
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300237# start_ironic() - Start running processes, including screen
Ian Wienandaee18c72014-02-21 15:35:08 +1100238function start_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300239 # Start Ironic API server, if enabled.
240 if is_service_enabled ir-api; then
241 start_ironic_api
242 fi
243
244 # Start Ironic conductor, if enabled.
245 if is_service_enabled ir-cond; then
246 start_ironic_conductor
247 fi
248}
249
250# start_ironic_api() - Used by start_ironic().
251# Starts Ironic API server.
Ian Wienandaee18c72014-02-21 15:35:08 +1100252function start_ironic_api {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300253 screen_it ir-api "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE"
254 echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..."
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800255 if ! timeout $SERVICE_TIMEOUT sh -c "while ! wget --no-proxy -q -O- http://$IRONIC_HOSTPORT; do sleep 1; done"; then
Sean Dague101b4242013-10-22 08:47:11 -0400256 die $LINENO "ir-api did not start"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300257 fi
258}
259
260# start_ironic_conductor() - Used by start_ironic().
261# Starts Ironic conductor.
Ian Wienandaee18c72014-02-21 15:35:08 +1100262function start_ironic_conductor {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300263 screen_it ir-cond "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE"
264 # TODO(romcheg): Find a way to check whether the conductor has started.
265}
266
267# stop_ironic() - Stop running processes
Ian Wienandaee18c72014-02-21 15:35:08 +1100268function stop_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300269 # Kill the Ironic screen windows
270 screen -S $SCREEN_NAME -p ir-api -X kill
271 screen -S $SCREEN_NAME -p ir-cond -X kill
272}
273
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400274function is_ironic {
275 if ( is_service_enabled ir-cond && is_service_enabled ir-api ); then
276 return 0
277 fi
278 return 1
279}
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300280
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400281function configure_ironic_dirs {
282 sudo mkdir -p $IRONIC_DATA_DIR
283 sudo mkdir -p $IRONIC_STATE_PATH
284 sudo mkdir -p $IRONIC_TFTPBOOT_DIR
285 sudo chown -R $STACK_USER $IRONIC_DATA_DIR $IRONIC_STATE_PATH
286 sudo chown -R $STACK_USER:$LIBVIRT_GROUP $IRONIC_TFTPBOOT_DIR
287 if is_ubuntu; then
288 PXEBIN=/usr/lib/syslinux/pxelinux.0
289 elif is_fedora; then
290 PXEBIN=/usr/share/syslinux/pxelinux.0
291 fi
292 if [ ! -f $PXEBIN ]; then
293 die $LINENO "pxelinux.0 (from SYSLINUX) not found."
294 fi
295
296 cp $PXEBIN $IRONIC_TFTPBOOT_DIR
297 mkdir -p $IRONIC_TFTPBOOT_DIR/pxelinux.cfg
298}
299
300function ironic_ensure_libvirt_group {
301 groups $STACK_USER | grep -q $LIBVIRT_GROUP || adduser $STACK_USER $LIBVIRT_GROUP
302}
303
304function create_bridge_and_vms {
305 ironic_ensure_libvirt_group
306
307 # Call libvirt setup scripts in a new shell to ensure any new group membership
308 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/setup-network"
309
310 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/create-nodes \
311 $IRONIC_VM_SPECS_CPU $IRONIC_VM_SPECS_RAM $IRONIC_VM_SPECS_DISK \
312 amd64 $IRONIC_VM_COUNT $IRONIC_VM_NETWORK_BRIDGE $IRONIC_VM_EMULATOR" >> $IRONIC_VM_MACS_CSV_FILE
313
314}
315
316function enroll_vms {
317
318 CHASSIS_ID=$(ironic chassis-create -d "ironic test chassis" | grep " uuid " | get_field 2)
319 IRONIC_NET_ID=$(neutron net-list | grep private | get_field 1)
320 local idx=0
321
322 # work around; need to know what netns neutron uses for private network
323 neutron port-create private
324
325 while read MAC; do
326
327 NODE_ID=$(ironic node-create --chassis_uuid $CHASSIS_ID --driver pxe_ssh \
328 -i ssh_virt_type=$IRONIC_SSH_VIRT_TYPE \
329 -i ssh_address=$IRONIC_VM_SSH_ADDRESS \
330 -i ssh_port=$IRONIC_VM_SSH_PORT \
331 -i ssh_username=$IRONIC_SSH_USERNAME \
332 -i ssh_key_filename=$IRONIC_SSH_KEY_DIR/$IRONIC_SSH_KEY_FILENAME \
333 -p cpus=$IRONIC_VM_SPECS_CPU \
334 -p memory_mb=$IRONIC_VM_SPECS_RAM \
335 -p local_gb=$IRONIC_VM_SPECS_DISK \
336 -p cpu_arch=x86_64 \
337 | grep " uuid " | get_field 2)
338
339 ironic port-create --address $MAC --node_uuid $NODE_ID
340
341 idx=$((idx+1))
342
343 done < $IRONIC_VM_MACS_CSV_FILE
344
345 # create the nova flavor
346 nova flavor-create baremetal auto $IRONIC_VM_SPECS_RAM $IRONIC_VM_SPECS_DISK $IRONIC_VM_SPECS_CPU
Alexander Gordeevf177f722014-03-14 18:44:48 +0400347 nova flavor-key baremetal set "cpu_arch"="x86_64" "baremetal:deploy_kernel_id"="$IRONIC_DEPLOY_KERNEL_ID" "baremetal:deploy_ramdisk_id"="$IRONIC_DEPLOY_RAMDISK_ID"
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400348
349 # intentional sleep to make sure the tag has been set to port
350 sleep 10
351 TAPDEV=$(sudo ip netns exec qdhcp-${IRONIC_NET_ID} ip link list | grep tap | cut -d':' -f2 | cut -b2-)
352 TAG_ID=$(sudo ovs-vsctl show |grep ${TAPDEV} -A1 -m1 | grep tag | cut -d':' -f2 | cut -b2-)
353
354 # make sure veth pair is not existing, otherwise delete its links
355 sudo ip link show ovs-tap1 && sudo ip link delete ovs-tap1
356 sudo ip link show brbm-tap1 && sudo ip link delete brbm-tap1
357 # create veth pair for future interconnection between br-int and brbm
358 sudo ip link add brbm-tap1 type veth peer name ovs-tap1
359 sudo ip link set dev brbm-tap1 up
360 sudo ip link set dev ovs-tap1 up
361
362 sudo ovs-vsctl -- --if-exists del-port ovs-tap1 -- add-port br-int ovs-tap1 tag=$TAG_ID
363 sudo ovs-vsctl -- --if-exists del-port brbm-tap1 -- add-port $IRONIC_VM_NETWORK_BRIDGE brbm-tap1
364}
365
366function configure_tftpd {
367 # enable tftp natting for allowing connections to SERVICE_HOST's tftp server
368 sudo modprobe nf_conntrack_tftp
369 sudo modprobe nf_nat_tftp
370
371 if is_ubuntu; then
372 PXEBIN=/usr/lib/syslinux/pxelinux.0
373 elif is_fedora; then
374 PXEBIN=/usr/share/syslinux/pxelinux.0
375 fi
376 if [ ! -f $PXEBIN ]; then
377 die $LINENO "pxelinux.0 (from SYSLINUX) not found."
378 fi
379
380 # stop tftpd and setup serving via xinetd
381 stop_service tftpd-hpa || true
382 [ -f /etc/init/tftpd-hpa.conf ] && echo "manual" | sudo tee /etc/init/tftpd-hpa.override
383 sudo cp $IRONIC_TEMPLATES_DIR/tftpd-xinetd.template /etc/xinetd.d/tftp
384 sudo sed -e "s|%TFTPBOOT_DIR%|$IRONIC_TFTPBOOT_DIR|g" -i /etc/xinetd.d/tftp
385
386 # setup tftp file mapping to satisfy requests at the root (booting) and
387 # /tftpboot/ sub-dir (as per deploy-ironic elements)
388 echo "r ^([^/]) $IRONIC_TFTPBOOT_DIR/\1" >$IRONIC_TFTPBOOT_DIR/map-file
389 echo "r ^(/tftpboot/) $IRONIC_TFTPBOOT_DIR/\2" >>$IRONIC_TFTPBOOT_DIR/map-file
390
391 chmod -R 0755 $IRONIC_TFTPBOOT_DIR
392 restart_service xinetd
393}
394
395function configure_ironic_ssh_keypair {
396 # Generating ssh key pair for stack user
397 if [[ ! -d $IRONIC_SSH_KEY_DIR ]]; then
398 mkdir -p $IRONIC_SSH_KEY_DIR
399 fi
400 if [[ ! -d $HOME/.ssh ]]; then
401 mkdir -p $HOME/.ssh
402 chmod 700 $HOME/.ssh
403 fi
404 echo -e 'n\n' | ssh-keygen -q -t rsa -P '' -f $IRONIC_KEY_FILE
405 cat $IRONIC_KEY_FILE.pub | tee -a $IRONIC_AUTHORIZED_KEYS_FILE
406}
407
408function ironic_ssh_check {
409 local KEY_FILE=$1
410 local FLOATING_IP=$2
411 local PORT=$3
412 local DEFAULT_INSTANCE_USER=$4
413 local ACTIVE_TIMEOUT=$5
414 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
415 die $LINENO "server didn't become ssh-able!"
416 fi
417}
418
419function configure_ironic_sshd {
420 # Ensure sshd server accepts connections from localhost only
421
422 SSH_CONFIG=/etc/ssh/sshd_config
423 HOST_PORT=$IRONIC_VM_SSH_ADDRESS:$IRONIC_VM_SSH_PORT
424 if ! sudo grep ListenAddress $SSH_CONFIG | grep $HOST_PORT; then
425 echo "ListenAddress $HOST_PORT" | sudo tee -a $SSH_CONFIG
426 fi
427
428 SSH_SERVICE_NAME=sshd
429 if is_ubuntu; then
430 SSH_SERVICE_NAME=ssh
431 fi
432
433 restart_service $SSH_SERVICE_NAME
434 # to ensure ssh service is up and running
435 sleep 3
436 ironic_ssh_check $IRONIC_SSH_KEY_DIR/$IRONIC_SSH_KEY_FILENAME $IRONIC_VM_SSH_ADDRESS $IRONIC_VM_SSH_PORT $IRONIC_SSH_USERNAME 10
437
438}
439
440function configure_ironic_auxiliary {
441 configure_ironic_dirs
442 configure_ironic_ssh_keypair
443 configure_ironic_sshd
444}
445
Alexander Gordeevf177f722014-03-14 18:44:48 +0400446# build deploy kernel+ramdisk, then upload them to glance
447# this function sets IRONIC_DEPLOY_KERNEL_ID and IRONIC_DEPLOY_RAMDISK_ID
448function upload_baremetal_ironic_deploy {
449 token=$1
450
451 if [ -z "$IRONIC_DEPLOY_KERNEL" -o -z "$IRONIC_DEPLOY_RAMDISK" ]; then
452 IRONIC_DEPLOY_KERNEL_PATH=$TOP_DIR/files/ir-deploy.kernel
453 IRONIC_DEPLOY_RAMDISK_PATH=$TOP_DIR/files/ir-deploy.initramfs
454 else
455 IRONIC_DEPLOY_KERNEL_PATH=$IRONIC_DEPLOY_KERNEL
456 IRONIC_DEPLOY_RAMDISK_PATH=$IRONIC_DEPLOY_RAMDISK
457 fi
458
459 if [ ! -e "$IRONIC_DEPLOY_RAMDISK_PATH" -o ! -e "$IRONIC_DEPLOY_KERNEL_PATH" ]; then
460 # files don't exist, need to build them
461 if [ "$IRONIC_BUILD_DEPLOY_RAMDISK" = "True" ]; then
462 # we can build them only if we're not offline
463 if [ "$OFFLINE" != "True" ]; then
464 $DIB_DIR/bin/ramdisk-image-create $IRONIC_DEPLOY_FLAVOR \
465 -o $TOP_DIR/files/ir-deploy
466 else
467 die $LINENO "Deploy kernel+ramdisk files don't exist and cannot be build in OFFLINE mode"
468 fi
469 else
470 die $LINENO "Deploy kernel+ramdisk files don't exist and their building was disabled explicitly by IRONIC_BUILD_DEPLOY_RAMDISK"
471 fi
472 fi
473
474 # load them into glance
475 IRONIC_DEPLOY_KERNEL_ID=$(glance \
476 --os-auth-token $token \
477 --os-image-url http://$GLANCE_HOSTPORT \
478 image-create \
479 --name $(basename $IRONIC_DEPLOY_KERNEL_PATH) \
480 --is-public True --disk-format=aki \
481 < $IRONIC_DEPLOY_KERNEL_PATH | grep ' id ' | get_field 2)
482 IRONIC_DEPLOY_RAMDISK_ID=$(glance \
483 --os-auth-token $token \
484 --os-image-url http://$GLANCE_HOSTPORT \
485 image-create \
486 --name $(basename $IRONIC_DEPLOY_RAMDISK_PATH) \
487 --is-public True --disk-format=ari \
488 < $IRONIC_DEPLOY_RAMDISK_PATH | grep ' id ' | get_field 2)
489}
490
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400491function prepare_baremetal_basic_ops {
492
493 # install diskimage-builder
Alexander Gordeevf177f722014-03-14 18:44:48 +0400494 git_clone $DIB_REPO $DIB_DIR $DIB_BRANCH
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400495
496 # make sure all needed service were enabled
497 for srv in nova glance key neutron; do
498 if ! is_service_enabled "$srv"; then
499 die $LINENO "$srv should be enabled for ironic tests"
500 fi
501 done
502
503 SCREEN_NAME=${SCREEN_NAME:-stack}
504 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
505
506 # stop all nova services
507 stop_nova || true
508
509 # remove any nova services failure status
510 find $SERVICE_DIR/$SCREEN_NAME -name 'n-*.failure' -exec rm -f '{}' \;
511
512 # start them again
513 start_nova_api
514 start_nova
515
516 TOKEN=$(keystone token-get | grep ' id ' | get_field 2)
517 die_if_not_set $LINENO TOKEN "Keystone fail to get token"
518
519 echo_summary "Creating and uploading baremetal images for ironic"
520
521 # build and upload separate deploy kernel & ramdisk
Alexander Gordeevf177f722014-03-14 18:44:48 +0400522 upload_baremetal_ironic_deploy $TOKEN
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400523
524 create_bridge_and_vms
525 enroll_vms
526 configure_tftpd
527}
528
529function cleanup_baremetal_basic_ops {
530 rm -f $IRONIC_VM_MACS_CSV_FILE
531 if [ -f $IRONIC_KEY_FILE ]; then
532 KEY=`cat $IRONIC_KEY_FILE.pub`
533 # remove public key from authorized_keys
534 grep -v "$KEY" $IRONIC_AUTHORIZED_KEYS_FILE > temp && mv temp $IRONIC_AUTHORIZED_KEYS_FILE
535 chmod 0600 $IRONIC_AUTHORIZED_KEYS_FILE
536 fi
537 sudo rm -rf $IRONIC_DATA_DIR $IRONIC_STATE_PATH
538 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/cleanup-nodes $IRONIC_VM_COUNT $IRONIC_VM_NETWORK_BRIDGE"
539 sudo rm -rf /etc/xinetd.d/tftp /etc/init/tftpd-hpa.override
540 restart_service xinetd
541}
542
543# Restore xtrace + pipefail
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300544$XTRACE
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400545$PIPEFAIL
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300546
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100547# Tell emacs to use shell-script-mode
548## Local variables:
549## mode: shell-script
550## End: