blob: 46eae7efce572c48b181091111b6343a0414be36 [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)
Devananda van der Veen83eeed02014-04-11 10:08:41 -070045IRONIC_ENABLED_DRIVERS=${IRONIC_ENABLED_DRIVERS:-fake,pxe_ssh,pxe_ipmitool}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040046IRONIC_SSH_USERNAME=${IRONIC_SSH_USERNAME:-`whoami`}
47IRONIC_SSH_KEY_DIR=${IRONIC_SSH_KEY_DIR:-$IRONIC_DATA_DIR/ssh_keys}
48IRONIC_SSH_KEY_FILENAME=${IRONIC_SSH_KEY_FILENAME:-ironic_key}
49IRONIC_KEY_FILE=$IRONIC_SSH_KEY_DIR/$IRONIC_SSH_KEY_FILENAME
50IRONIC_SSH_VIRT_TYPE=${IRONIC_SSH_VIRT_TYPE:-virsh}
51IRONIC_TFTPBOOT_DIR=${IRONIC_TFTPBOOT_DIR:-$IRONIC_DATA_DIR/tftpboot}
Adam Gandelman86765102014-04-14 11:57:13 -070052IRONIC_VM_SSH_PORT=${IRONIC_VM_SSH_PORT:-22}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040053IRONIC_VM_SSH_ADDRESS=${IRONIC_VM_SSH_ADDRESS:-$HOST_IP}
54IRONIC_VM_COUNT=${IRONIC_VM_COUNT:-1}
55IRONIC_VM_SPECS_CPU=${IRONIC_VM_SPECS_CPU:-1}
Adam Gandelmanea335f42014-07-03 17:57:49 -070056IRONIC_VM_SPECS_RAM=${IRONIC_VM_SPECS_RAM:-512}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040057IRONIC_VM_SPECS_DISK=${IRONIC_VM_SPECS_DISK:-10}
David Shrewsbury3e4612b2014-05-01 11:38:21 -040058IRONIC_VM_EPHEMERAL_DISK=${IRONIC_VM_EPHEMERAL_DISK:-0}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040059IRONIC_VM_EMULATOR=${IRONIC_VM_EMULATOR:-/usr/bin/qemu-system-x86_64}
60IRONIC_VM_NETWORK_BRIDGE=${IRONIC_VM_NETWORK_BRIDGE:-brbm}
61IRONIC_VM_NETWORK_RANGE=${IRONIC_VM_NETWORK_RANGE:-192.0.2.0/24}
62IRONIC_VM_MACS_CSV_FILE=${IRONIC_VM_MACS_CSV_FILE:-$IRONIC_DATA_DIR/ironic_macs.csv}
63IRONIC_AUTHORIZED_KEYS_FILE=${IRONIC_AUTHORIZED_KEYS_FILE:-$HOME/.ssh/authorized_keys}
64
Adam Gandelman8af6fae2014-04-11 17:06:14 -070065# By default, baremetal VMs will console output to file.
66IRONIC_VM_LOG_CONSOLE=${IRONIC_VM_LOG_CONSOLE:-True}
67IRONIC_VM_LOG_DIR=${IRONIC_VM_LOG_DIR:-$IRONIC_DATA_DIR/logs/}
68
Alexander Gordeevf177f722014-03-14 18:44:48 +040069DIB_DIR=${DIB_DIR:-$DEST/diskimage-builder}
70
71# Use DIB to create deploy ramdisk and kernel.
72IRONIC_BUILD_DEPLOY_RAMDISK=`trueorfalse True $IRONIC_BUILD_DEPLOY_RAMDISK`
73# If not use DIB, these files are used as deploy ramdisk/kernel.
74# (The value must be a absolute path)
75IRONIC_DEPLOY_RAMDISK=${IRONIC_DEPLOY_RAMDISK:-}
76IRONIC_DEPLOY_KERNEL=${IRONIC_DEPLOY_KERNEL:-}
77IRONIC_DEPLOY_ELEMENT=${IRONIC_DEPLOY_ELEMENT:-deploy-ironic}
78
79#TODO(agordeev): replace 'ubuntu' with host distro name getting
80IRONIC_DEPLOY_FLAVOR=${IRONIC_DEPLOY_FLAVOR:-ubuntu $IRONIC_DEPLOY_ELEMENT}
81
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030082# Support entry points installation of console scripts
83IRONIC_BIN_DIR=$(get_python_exec_prefix)
84
85# Ironic connection info. Note the port must be specified.
86IRONIC_SERVICE_PROTOCOL=http
87IRONIC_HOSTPORT=${IRONIC_HOSTPORT:-$SERVICE_HOST:6385}
88
Dean Troyer4237f592014-01-29 16:22:11 -060089# Tell Tempest this project is present
90TEMPEST_SERVICES+=,ironic
91
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030092
93# Functions
94# ---------
95
Dean Troyer1023ff72014-01-27 14:56:44 -060096# Test if any Ironic services are enabled
97# is_ironic_enabled
98function is_ironic_enabled {
99 [[ ,${ENABLED_SERVICES} =~ ,"ir-" ]] && return 0
100 return 1
101}
102
Roman Prykhodchenko43e00662013-10-15 17:03:15 +0300103# install_ironic() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100104function install_ironic {
Roman Prykhodchenko43e00662013-10-15 17:03:15 +0300105 git_clone $IRONIC_REPO $IRONIC_DIR $IRONIC_BRANCH
106 setup_develop $IRONIC_DIR
107}
108
109# install_ironicclient() - Collect sources and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100110function install_ironicclient {
Roman Prykhodchenko43e00662013-10-15 17:03:15 +0300111 git_clone $IRONICCLIENT_REPO $IRONICCLIENT_DIR $IRONICCLIENT_BRANCH
112 setup_develop $IRONICCLIENT_DIR
113}
114
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300115# cleanup_ironic() - Remove residual data files, anything left over from previous
116# runs that would need to clean up.
Ian Wienandaee18c72014-02-21 15:35:08 +1100117function cleanup_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300118 sudo rm -rf $IRONIC_AUTH_CACHE_DIR
119}
120
121# configure_ironic() - Set config files, create data dirs, etc
Ian Wienandaee18c72014-02-21 15:35:08 +1100122function configure_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300123 if [[ ! -d $IRONIC_CONF_DIR ]]; then
124 sudo mkdir -p $IRONIC_CONF_DIR
125 fi
126 sudo chown $STACK_USER $IRONIC_CONF_DIR
127
128 # Copy over ironic configuration file and configure common parameters.
129 cp $IRONIC_DIR/etc/ironic/ironic.conf.sample $IRONIC_CONF_FILE
130 iniset $IRONIC_CONF_FILE DEFAULT debug True
131 inicomment $IRONIC_CONF_FILE DEFAULT log_file
132 iniset $IRONIC_CONF_FILE DEFAULT sql_connection `database_connection_url ironic`
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400133 iniset $IRONIC_CONF_FILE DEFAULT state_path $IRONIC_STATE_PATH
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300134 iniset $IRONIC_CONF_FILE DEFAULT use_syslog $SYSLOG
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300135 # Configure Ironic conductor, if it was enabled.
136 if is_service_enabled ir-cond; then
137 configure_ironic_conductor
138 fi
139
140 # Configure Ironic API, if it was enabled.
141 if is_service_enabled ir-api; then
142 configure_ironic_api
143 fi
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400144
Adam Gandelmanab592932014-06-06 15:12:32 -0700145 # Format logging
146 if [ "$LOG_COLOR" == "True" ] && [ "$SYSLOG" == "False" ]; then
147 setup_colorized_logging $IRONIC_CONF_FILE DEFAULT
148 fi
149
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400150 if [[ "$IRONIC_BAREMETAL_BASIC_OPS" == "True" ]]; then
151 configure_ironic_auxiliary
152 fi
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300153}
154
155# configure_ironic_api() - Is used by configure_ironic(). Performs
156# API specific configuration.
Ian Wienandaee18c72014-02-21 15:35:08 +1100157function configure_ironic_api {
Roman Prykhodchenkoc48c3122013-10-01 17:19:05 +0300158 iniset $IRONIC_CONF_FILE DEFAULT auth_strategy keystone
159 iniset $IRONIC_CONF_FILE DEFAULT policy_file $IRONIC_POLICY_JSON
Jamie Lennox3561d7f2014-05-21 17:18:43 +1000160 iniset $IRONIC_CONF_FILE keystone_authtoken identity_uri $KEYSTONE_AUTH_URI
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000161 iniset $IRONIC_CONF_FILE keystone_authtoken cafile $KEYSTONE_SSL_CA
Jamie Lennox3561d7f2014-05-21 17:18:43 +1000162 iniset $IRONIC_CONF_FILE keystone_authtoken auth_uri $KEYSTONE_SERVICE_URI
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300163 iniset $IRONIC_CONF_FILE keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME
164 iniset $IRONIC_CONF_FILE keystone_authtoken admin_user ironic
165 iniset $IRONIC_CONF_FILE keystone_authtoken admin_password $SERVICE_PASSWORD
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300166 iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT
167 iniset $IRONIC_CONF_FILE keystone_authtoken signing_dir $IRONIC_AUTH_CACHE_DIR/api
168
169 cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON
170}
171
172# configure_ironic_conductor() - Is used by configure_ironic().
173# Sets conductor specific settings.
Ian Wienandaee18c72014-02-21 15:35:08 +1100174function configure_ironic_conductor {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300175 cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF
Lucas Alvares Gomes279295c2014-01-14 11:37:51 +0000176 cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_CONF_DIR
Adam Gandelman3f2a7b72014-04-14 16:14:33 -0700177 IRONIC_ROOTWRAP=$(get_rootwrap_location ironic)
178 ROOTWRAP_ISUDOER_CMD="$IRONIC_ROOTWRAP $IRONIC_CONF_DIR/rootwrap.conf *"
179
180 # Set up the rootwrap sudoers for ironic
181 TEMPFILE=`mktemp`
182 echo "$STACK_USER ALL=(root) NOPASSWD: $ROOTWRAP_ISUDOER_CMD" >$TEMPFILE
183 chmod 0440 $TEMPFILE
184 sudo chown root:root $TEMPFILE
185 sudo mv $TEMPFILE /etc/sudoers.d/ironic-rootwrap
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300186
Alexander Gordeeva67cb1a2014-03-04 18:38:33 +0400187 iniset $IRONIC_CONF_FILE DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF
Devananda van der Veen83eeed02014-04-11 10:08:41 -0700188 iniset $IRONIC_CONF_FILE DEFAULT enabled_drivers $IRONIC_ENABLED_DRIVERS
Adam Gandelmand3011982014-03-24 13:55:34 -0700189 iniset $IRONIC_CONF_FILE conductor api_url http://$HOST_IP:6385
190 iniset $IRONIC_CONF_FILE pxe tftp_server $HOST_IP
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400191 iniset $IRONIC_CONF_FILE pxe tftp_root $IRONIC_TFTPBOOT_DIR
192 iniset $IRONIC_CONF_FILE pxe tftp_master_path $IRONIC_TFTPBOOT_DIR/master_images
Adam Gandelman8af6fae2014-04-11 17:06:14 -0700193 if [[ "$IRONIC_VM_LOG_CONSOLE" == "True" ]] ; then
194 iniset $IRONIC_CONF_FILE pxe pxe_append_params "nofb nomodeset vga=normal console=ttyS0"
195 fi
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300196}
197
198# create_ironic_cache_dir() - Part of the init_ironic() process
Ian Wienandaee18c72014-02-21 15:35:08 +1100199function create_ironic_cache_dir {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300200 # Create cache dir
201 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api
202 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api
203 rm -f $IRONIC_AUTH_CACHE_DIR/api/*
204 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry
205 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry
206 rm -f $IRONIC_AUTH_CACHE_DIR/registry/*
207}
208
209# create_ironic_accounts() - Set up common required ironic accounts
210
211# Tenant User Roles
212# ------------------------------------------------------------------
213# service ironic admin # if enabled
Ian Wienandaee18c72014-02-21 15:35:08 +1100214function create_ironic_accounts {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300215
Steve Martinelli19685422014-01-24 13:02:26 -0600216 SERVICE_TENANT=$(openstack project list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }")
217 ADMIN_ROLE=$(openstack role list | awk "/ admin / { print \$2 }")
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300218
219 # Ironic
220 if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then
Steve Martinelli19685422014-01-24 13:02:26 -0600221 IRONIC_USER=$(openstack user create \
222 ironic \
223 --password "$SERVICE_PASSWORD" \
224 --project $SERVICE_TENANT \
225 --email ironic@example.com \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300226 | grep " id " | get_field 2)
Steve Martinelli19685422014-01-24 13:02:26 -0600227 openstack role add \
228 $ADMIN_ROLE \
229 --project $SERVICE_TENANT \
230 --user $IRONIC_USER
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300231 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
Steve Martinelli19685422014-01-24 13:02:26 -0600232 IRONIC_SERVICE=$(openstack service create \
233 ironic \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300234 --type=baremetal \
235 --description="Ironic baremetal provisioning service" \
236 | grep " id " | get_field 2)
Steve Martinelli19685422014-01-24 13:02:26 -0600237 openstack endpoint create \
238 $IRONIC_SERVICE \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300239 --region RegionOne \
Roman Prykhodchenkof5002ef2013-09-24 19:09:26 +0300240 --publicurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
241 --adminurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
242 --internalurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300243 fi
244 fi
245}
246
247
248# init_ironic() - Initialize databases, etc.
Ian Wienandaee18c72014-02-21 15:35:08 +1100249function init_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300250 # (Re)create ironic database
251 recreate_database ironic utf8
252
253 # Migrate ironic database
Dmitry Tantsur0c6a6542014-06-02 20:19:33 +0200254 $IRONIC_BIN_DIR/ironic-dbsync --config-file=$IRONIC_CONF_FILE
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300255
256 create_ironic_cache_dir
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300257}
258
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300259# start_ironic() - Start running processes, including screen
Ian Wienandaee18c72014-02-21 15:35:08 +1100260function start_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300261 # Start Ironic API server, if enabled.
262 if is_service_enabled ir-api; then
263 start_ironic_api
264 fi
265
266 # Start Ironic conductor, if enabled.
267 if is_service_enabled ir-cond; then
268 start_ironic_conductor
269 fi
270}
271
272# start_ironic_api() - Used by start_ironic().
273# Starts Ironic API server.
Ian Wienandaee18c72014-02-21 15:35:08 +1100274function start_ironic_api {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300275 screen_it ir-api "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE"
276 echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..."
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800277 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 -0400278 die $LINENO "ir-api did not start"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300279 fi
280}
281
282# start_ironic_conductor() - Used by start_ironic().
283# Starts Ironic conductor.
Ian Wienandaee18c72014-02-21 15:35:08 +1100284function start_ironic_conductor {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300285 screen_it ir-cond "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE"
286 # TODO(romcheg): Find a way to check whether the conductor has started.
287}
288
289# stop_ironic() - Stop running processes
Ian Wienandaee18c72014-02-21 15:35:08 +1100290function stop_ironic {
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300291 # Kill the Ironic screen windows
292 screen -S $SCREEN_NAME -p ir-api -X kill
293 screen -S $SCREEN_NAME -p ir-cond -X kill
294}
295
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400296function is_ironic {
297 if ( is_service_enabled ir-cond && is_service_enabled ir-api ); then
298 return 0
299 fi
300 return 1
301}
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300302
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400303function configure_ironic_dirs {
304 sudo mkdir -p $IRONIC_DATA_DIR
305 sudo mkdir -p $IRONIC_STATE_PATH
306 sudo mkdir -p $IRONIC_TFTPBOOT_DIR
307 sudo chown -R $STACK_USER $IRONIC_DATA_DIR $IRONIC_STATE_PATH
308 sudo chown -R $STACK_USER:$LIBVIRT_GROUP $IRONIC_TFTPBOOT_DIR
309 if is_ubuntu; then
310 PXEBIN=/usr/lib/syslinux/pxelinux.0
311 elif is_fedora; then
312 PXEBIN=/usr/share/syslinux/pxelinux.0
313 fi
314 if [ ! -f $PXEBIN ]; then
315 die $LINENO "pxelinux.0 (from SYSLINUX) not found."
316 fi
317
318 cp $PXEBIN $IRONIC_TFTPBOOT_DIR
319 mkdir -p $IRONIC_TFTPBOOT_DIR/pxelinux.cfg
320}
321
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400322function create_bridge_and_vms {
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400323 # Call libvirt setup scripts in a new shell to ensure any new group membership
324 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/setup-network"
Adam Gandelman8af6fae2014-04-11 17:06:14 -0700325 if [[ "$IRONIC_VM_LOG_CONSOLE" == "True" ]] ; then
326 LOG_ARG="$IRONIC_VM_LOG_DIR"
327 else
328 LOG_ARG=""
329 fi
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400330 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/create-nodes \
331 $IRONIC_VM_SPECS_CPU $IRONIC_VM_SPECS_RAM $IRONIC_VM_SPECS_DISK \
Adam Gandelman8af6fae2014-04-11 17:06:14 -0700332 amd64 $IRONIC_VM_COUNT $IRONIC_VM_NETWORK_BRIDGE $IRONIC_VM_EMULATOR \
333 $LOG_ARG" >> $IRONIC_VM_MACS_CSV_FILE
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400334}
335
336function enroll_vms {
337
338 CHASSIS_ID=$(ironic chassis-create -d "ironic test chassis" | grep " uuid " | get_field 2)
339 IRONIC_NET_ID=$(neutron net-list | grep private | get_field 1)
340 local idx=0
341
Alexander Gordeev1ab92d92014-04-03 16:17:58 +0400342 # work around; need to know what netns neutron uses for private network.
343 # Without knowing how to interconnect the networks, PXE won't work properly
344 # for fake baremetal instances. The network should be configured prior all
345 # the instances operation. If we don't do this, the first port creation
346 # only happens in the middle of fake baremetal instance's spawning by nova,
347 # so we'll end up with unbootable fake baremetal VM due to broken PXE.
348 PORT_ID=$(neutron port-create private | grep " id " | get_field 2)
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400349
350 while read MAC; do
351
352 NODE_ID=$(ironic node-create --chassis_uuid $CHASSIS_ID --driver pxe_ssh \
Lucas Alvares Gomescd7cfef2014-05-26 14:58:37 +0100353 -i pxe_deploy_kernel=$IRONIC_DEPLOY_KERNEL_ID \
354 -i pxe_deploy_ramdisk=$IRONIC_DEPLOY_RAMDISK_ID \
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400355 -i ssh_virt_type=$IRONIC_SSH_VIRT_TYPE \
356 -i ssh_address=$IRONIC_VM_SSH_ADDRESS \
357 -i ssh_port=$IRONIC_VM_SSH_PORT \
358 -i ssh_username=$IRONIC_SSH_USERNAME \
359 -i ssh_key_filename=$IRONIC_SSH_KEY_DIR/$IRONIC_SSH_KEY_FILENAME \
360 -p cpus=$IRONIC_VM_SPECS_CPU \
361 -p memory_mb=$IRONIC_VM_SPECS_RAM \
362 -p local_gb=$IRONIC_VM_SPECS_DISK \
363 -p cpu_arch=x86_64 \
364 | grep " uuid " | get_field 2)
365
366 ironic port-create --address $MAC --node_uuid $NODE_ID
367
368 idx=$((idx+1))
369
370 done < $IRONIC_VM_MACS_CSV_FILE
371
372 # create the nova flavor
David Shrewsbury3e4612b2014-05-01 11:38:21 -0400373 adjusted_disk=$(($IRONIC_VM_SPECS_DISK - $IRONIC_VM_EPHEMERAL_DISK))
374 nova flavor-create --ephemeral $IRONIC_VM_EPHEMERAL_DISK baremetal auto $IRONIC_VM_SPECS_RAM $adjusted_disk $IRONIC_VM_SPECS_CPU
Lucas Alvares Gomescd7cfef2014-05-26 14:58:37 +0100375 # TODO(lucasagomes): Remove the 'baremetal:deploy_kernel_id'
376 # and 'baremetal:deploy_ramdisk_id' parameters
377 # from the flavor after the completion of
378 # https://blueprints.launchpad.net/ironic/+spec/add-node-instance-info
Alexander Gordeevf177f722014-03-14 18:44:48 +0400379 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 +0400380
381 # intentional sleep to make sure the tag has been set to port
382 sleep 10
383 TAPDEV=$(sudo ip netns exec qdhcp-${IRONIC_NET_ID} ip link list | grep tap | cut -d':' -f2 | cut -b2-)
384 TAG_ID=$(sudo ovs-vsctl show |grep ${TAPDEV} -A1 -m1 | grep tag | cut -d':' -f2 | cut -b2-)
385
386 # make sure veth pair is not existing, otherwise delete its links
387 sudo ip link show ovs-tap1 && sudo ip link delete ovs-tap1
388 sudo ip link show brbm-tap1 && sudo ip link delete brbm-tap1
389 # create veth pair for future interconnection between br-int and brbm
390 sudo ip link add brbm-tap1 type veth peer name ovs-tap1
391 sudo ip link set dev brbm-tap1 up
392 sudo ip link set dev ovs-tap1 up
393
394 sudo ovs-vsctl -- --if-exists del-port ovs-tap1 -- add-port br-int ovs-tap1 tag=$TAG_ID
395 sudo ovs-vsctl -- --if-exists del-port brbm-tap1 -- add-port $IRONIC_VM_NETWORK_BRIDGE brbm-tap1
Alexander Gordeev1ab92d92014-04-03 16:17:58 +0400396
397 # Remove the port needed only for workaround. For additional info read the
398 # comment at the beginning of this function
399 neutron port-delete $PORT_ID
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400400}
401
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700402function configure_iptables {
403 # enable tftp natting for allowing connections to HOST_IP's tftp server
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400404 sudo modprobe nf_conntrack_tftp
405 sudo modprobe nf_nat_tftp
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700406 # nodes boot from TFTP and callback to the API server listening on $HOST_IP
407 sudo iptables -I INPUT -d $HOST_IP -p udp --dport 69 -j ACCEPT || true
408 sudo iptables -I INPUT -d $HOST_IP -p tcp --dport 6385 -j ACCEPT || true
409}
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400410
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700411function configure_tftpd {
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400412 if is_ubuntu; then
413 PXEBIN=/usr/lib/syslinux/pxelinux.0
414 elif is_fedora; then
415 PXEBIN=/usr/share/syslinux/pxelinux.0
416 fi
417 if [ ! -f $PXEBIN ]; then
418 die $LINENO "pxelinux.0 (from SYSLINUX) not found."
419 fi
420
421 # stop tftpd and setup serving via xinetd
422 stop_service tftpd-hpa || true
423 [ -f /etc/init/tftpd-hpa.conf ] && echo "manual" | sudo tee /etc/init/tftpd-hpa.override
424 sudo cp $IRONIC_TEMPLATES_DIR/tftpd-xinetd.template /etc/xinetd.d/tftp
425 sudo sed -e "s|%TFTPBOOT_DIR%|$IRONIC_TFTPBOOT_DIR|g" -i /etc/xinetd.d/tftp
426
427 # setup tftp file mapping to satisfy requests at the root (booting) and
428 # /tftpboot/ sub-dir (as per deploy-ironic elements)
429 echo "r ^([^/]) $IRONIC_TFTPBOOT_DIR/\1" >$IRONIC_TFTPBOOT_DIR/map-file
430 echo "r ^(/tftpboot/) $IRONIC_TFTPBOOT_DIR/\2" >>$IRONIC_TFTPBOOT_DIR/map-file
431
432 chmod -R 0755 $IRONIC_TFTPBOOT_DIR
433 restart_service xinetd
434}
435
436function configure_ironic_ssh_keypair {
437 # Generating ssh key pair for stack user
438 if [[ ! -d $IRONIC_SSH_KEY_DIR ]]; then
439 mkdir -p $IRONIC_SSH_KEY_DIR
440 fi
441 if [[ ! -d $HOME/.ssh ]]; then
442 mkdir -p $HOME/.ssh
443 chmod 700 $HOME/.ssh
444 fi
445 echo -e 'n\n' | ssh-keygen -q -t rsa -P '' -f $IRONIC_KEY_FILE
446 cat $IRONIC_KEY_FILE.pub | tee -a $IRONIC_AUTHORIZED_KEYS_FILE
447}
448
449function ironic_ssh_check {
450 local KEY_FILE=$1
451 local FLOATING_IP=$2
452 local PORT=$3
453 local DEFAULT_INSTANCE_USER=$4
454 local ACTIVE_TIMEOUT=$5
455 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
456 die $LINENO "server didn't become ssh-able!"
457 fi
458}
459
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400460function configure_ironic_auxiliary {
461 configure_ironic_dirs
462 configure_ironic_ssh_keypair
Adam Gandelman86765102014-04-14 11:57:13 -0700463 ironic_ssh_check $IRONIC_SSH_KEY_DIR/$IRONIC_SSH_KEY_FILENAME $IRONIC_VM_SSH_ADDRESS $IRONIC_VM_SSH_PORT $IRONIC_SSH_USERNAME 10
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400464}
465
Alexander Gordeevf177f722014-03-14 18:44:48 +0400466# build deploy kernel+ramdisk, then upload them to glance
467# this function sets IRONIC_DEPLOY_KERNEL_ID and IRONIC_DEPLOY_RAMDISK_ID
468function upload_baremetal_ironic_deploy {
469 token=$1
470
471 if [ -z "$IRONIC_DEPLOY_KERNEL" -o -z "$IRONIC_DEPLOY_RAMDISK" ]; then
472 IRONIC_DEPLOY_KERNEL_PATH=$TOP_DIR/files/ir-deploy.kernel
473 IRONIC_DEPLOY_RAMDISK_PATH=$TOP_DIR/files/ir-deploy.initramfs
474 else
475 IRONIC_DEPLOY_KERNEL_PATH=$IRONIC_DEPLOY_KERNEL
476 IRONIC_DEPLOY_RAMDISK_PATH=$IRONIC_DEPLOY_RAMDISK
477 fi
478
479 if [ ! -e "$IRONIC_DEPLOY_RAMDISK_PATH" -o ! -e "$IRONIC_DEPLOY_KERNEL_PATH" ]; then
480 # files don't exist, need to build them
481 if [ "$IRONIC_BUILD_DEPLOY_RAMDISK" = "True" ]; then
482 # we can build them only if we're not offline
483 if [ "$OFFLINE" != "True" ]; then
484 $DIB_DIR/bin/ramdisk-image-create $IRONIC_DEPLOY_FLAVOR \
485 -o $TOP_DIR/files/ir-deploy
486 else
487 die $LINENO "Deploy kernel+ramdisk files don't exist and cannot be build in OFFLINE mode"
488 fi
489 else
490 die $LINENO "Deploy kernel+ramdisk files don't exist and their building was disabled explicitly by IRONIC_BUILD_DEPLOY_RAMDISK"
491 fi
492 fi
493
494 # load them into glance
495 IRONIC_DEPLOY_KERNEL_ID=$(glance \
496 --os-auth-token $token \
497 --os-image-url http://$GLANCE_HOSTPORT \
498 image-create \
499 --name $(basename $IRONIC_DEPLOY_KERNEL_PATH) \
500 --is-public True --disk-format=aki \
501 < $IRONIC_DEPLOY_KERNEL_PATH | grep ' id ' | get_field 2)
502 IRONIC_DEPLOY_RAMDISK_ID=$(glance \
503 --os-auth-token $token \
504 --os-image-url http://$GLANCE_HOSTPORT \
505 image-create \
506 --name $(basename $IRONIC_DEPLOY_RAMDISK_PATH) \
507 --is-public True --disk-format=ari \
508 < $IRONIC_DEPLOY_RAMDISK_PATH | grep ' id ' | get_field 2)
509}
510
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400511function prepare_baremetal_basic_ops {
512
513 # install diskimage-builder
Alexander Gordeevf177f722014-03-14 18:44:48 +0400514 git_clone $DIB_REPO $DIB_DIR $DIB_BRANCH
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400515
516 # make sure all needed service were enabled
517 for srv in nova glance key neutron; do
518 if ! is_service_enabled "$srv"; then
519 die $LINENO "$srv should be enabled for ironic tests"
520 fi
521 done
522
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400523 TOKEN=$(keystone token-get | grep ' id ' | get_field 2)
524 die_if_not_set $LINENO TOKEN "Keystone fail to get token"
525
526 echo_summary "Creating and uploading baremetal images for ironic"
527
528 # build and upload separate deploy kernel & ramdisk
Alexander Gordeevf177f722014-03-14 18:44:48 +0400529 upload_baremetal_ironic_deploy $TOKEN
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400530
531 create_bridge_and_vms
532 enroll_vms
533 configure_tftpd
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700534 configure_iptables
Adam Gandelman5f060e62014-04-08 11:52:05 -0700535
536 # restart nova-compute to ensure its resource tracking is up to
537 # date with newly enrolled nodes
538 stop_nova_compute || true
539 start_nova_compute
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400540}
541
542function cleanup_baremetal_basic_ops {
543 rm -f $IRONIC_VM_MACS_CSV_FILE
544 if [ -f $IRONIC_KEY_FILE ]; then
545 KEY=`cat $IRONIC_KEY_FILE.pub`
546 # remove public key from authorized_keys
547 grep -v "$KEY" $IRONIC_AUTHORIZED_KEYS_FILE > temp && mv temp $IRONIC_AUTHORIZED_KEYS_FILE
548 chmod 0600 $IRONIC_AUTHORIZED_KEYS_FILE
549 fi
550 sudo rm -rf $IRONIC_DATA_DIR $IRONIC_STATE_PATH
551 sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/cleanup-nodes $IRONIC_VM_COUNT $IRONIC_VM_NETWORK_BRIDGE"
552 sudo rm -rf /etc/xinetd.d/tftp /etc/init/tftpd-hpa.override
553 restart_service xinetd
Adam Gandelmanc1f0db22014-04-14 13:21:22 -0700554 sudo iptables -D INPUT -d $HOST_IP -p udp --dport 69 -j ACCEPT || true
555 sudo iptables -D INPUT -d $HOST_IP -p tcp --dport 6385 -j ACCEPT || true
556 sudo rmmod nf_conntrack_tftp || true
557 sudo rmmod nf_nat_tftp || true
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400558}
559
560# Restore xtrace + pipefail
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300561$XTRACE
Alexander Gordeev06fb29c2014-01-31 18:02:07 +0400562$PIPEFAIL
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300563
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100564# Tell emacs to use shell-script-mode
565## Local variables:
566## mode: shell-script
567## End: