blob: 0b9df57c9e3a2aac781d27a42d2f8846440e917c [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
21# Save trace setting
22XTRACE=$(set +o | grep xtrace)
23set +o xtrace
24
25
26# Defaults
27# --------
28
29# Set up default directories
30IRONIC_DIR=$DEST/ironic
Roman Prykhodchenko43e00662013-10-15 17:03:15 +030031IRONICCLIENT_DIR=$DEST/python-ironicclient
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030032IRONIC_AUTH_CACHE_DIR=${IRONIC_AUTH_CACHE_DIR:-/var/cache/ironic}
33IRONIC_CONF_DIR=${IRONIC_CONF_DIR:-/etc/ironic}
34IRONIC_CONF_FILE=$IRONIC_CONF_DIR/ironic.conf
35IRONIC_ROOTWRAP_CONF=$IRONIC_CONF_DIR/rootwrap.conf
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030036IRONIC_POLICY_JSON=$IRONIC_CONF_DIR/policy.json
37
38# Support entry points installation of console scripts
39IRONIC_BIN_DIR=$(get_python_exec_prefix)
40
41# Ironic connection info. Note the port must be specified.
42IRONIC_SERVICE_PROTOCOL=http
43IRONIC_HOSTPORT=${IRONIC_HOSTPORT:-$SERVICE_HOST:6385}
44
Dean Troyer4237f592014-01-29 16:22:11 -060045# Tell Tempest this project is present
46TEMPEST_SERVICES+=,ironic
47
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030048
49# Functions
50# ---------
51
Dean Troyer1023ff72014-01-27 14:56:44 -060052# Test if any Ironic services are enabled
53# is_ironic_enabled
54function is_ironic_enabled {
55 [[ ,${ENABLED_SERVICES} =~ ,"ir-" ]] && return 0
56 return 1
57}
58
Roman Prykhodchenko43e00662013-10-15 17:03:15 +030059# install_ironic() - Collect source and prepare
60function install_ironic() {
61 git_clone $IRONIC_REPO $IRONIC_DIR $IRONIC_BRANCH
62 setup_develop $IRONIC_DIR
63}
64
65# install_ironicclient() - Collect sources and prepare
66function install_ironicclient() {
67 git_clone $IRONICCLIENT_REPO $IRONICCLIENT_DIR $IRONICCLIENT_BRANCH
68 setup_develop $IRONICCLIENT_DIR
69}
70
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030071# cleanup_ironic() - Remove residual data files, anything left over from previous
72# runs that would need to clean up.
73function cleanup_ironic() {
74 sudo rm -rf $IRONIC_AUTH_CACHE_DIR
75}
76
77# configure_ironic() - Set config files, create data dirs, etc
78function configure_ironic() {
79 if [[ ! -d $IRONIC_CONF_DIR ]]; then
80 sudo mkdir -p $IRONIC_CONF_DIR
81 fi
82 sudo chown $STACK_USER $IRONIC_CONF_DIR
83
84 # Copy over ironic configuration file and configure common parameters.
85 cp $IRONIC_DIR/etc/ironic/ironic.conf.sample $IRONIC_CONF_FILE
86 iniset $IRONIC_CONF_FILE DEFAULT debug True
87 inicomment $IRONIC_CONF_FILE DEFAULT log_file
88 iniset $IRONIC_CONF_FILE DEFAULT sql_connection `database_connection_url ironic`
89 iniset $IRONIC_CONF_FILE DEFAULT use_syslog $SYSLOG
90
91 # Configure Ironic conductor, if it was enabled.
92 if is_service_enabled ir-cond; then
93 configure_ironic_conductor
94 fi
95
96 # Configure Ironic API, if it was enabled.
97 if is_service_enabled ir-api; then
98 configure_ironic_api
99 fi
100}
101
102# configure_ironic_api() - Is used by configure_ironic(). Performs
103# API specific configuration.
104function configure_ironic_api() {
Roman Prykhodchenkoc48c3122013-10-01 17:19:05 +0300105 iniset $IRONIC_CONF_FILE DEFAULT auth_strategy keystone
106 iniset $IRONIC_CONF_FILE DEFAULT policy_file $IRONIC_POLICY_JSON
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300107 iniset $IRONIC_CONF_FILE keystone_authtoken auth_host $KEYSTONE_AUTH_HOST
108 iniset $IRONIC_CONF_FILE keystone_authtoken auth_port $KEYSTONE_AUTH_PORT
109 iniset $IRONIC_CONF_FILE keystone_authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000110 iniset $IRONIC_CONF_FILE keystone_authtoken cafile $KEYSTONE_SSL_CA
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300111 iniset $IRONIC_CONF_FILE keystone_authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/
112 iniset $IRONIC_CONF_FILE keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME
113 iniset $IRONIC_CONF_FILE keystone_authtoken admin_user ironic
114 iniset $IRONIC_CONF_FILE keystone_authtoken admin_password $SERVICE_PASSWORD
115 if is_service_enabled qpid; then
116 iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy qpid
117 elif [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; then
118 iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy rabbit
119 fi
120 iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT
121 iniset $IRONIC_CONF_FILE keystone_authtoken signing_dir $IRONIC_AUTH_CACHE_DIR/api
122
123 cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON
124}
125
126# configure_ironic_conductor() - Is used by configure_ironic().
127# Sets conductor specific settings.
128function configure_ironic_conductor() {
129 cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF
Lucas Alvares Gomes279295c2014-01-14 11:37:51 +0000130 cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_CONF_DIR
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300131
132 iniset $IRONIC_CONF DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF
133}
134
135# create_ironic_cache_dir() - Part of the init_ironic() process
136function create_ironic_cache_dir() {
137 # Create cache dir
138 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api
139 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api
140 rm -f $IRONIC_AUTH_CACHE_DIR/api/*
141 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry
142 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry
143 rm -f $IRONIC_AUTH_CACHE_DIR/registry/*
144}
145
146# create_ironic_accounts() - Set up common required ironic accounts
147
148# Tenant User Roles
149# ------------------------------------------------------------------
150# service ironic admin # if enabled
151create_ironic_accounts() {
152
153 SERVICE_TENANT=$(keystone tenant-list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }")
154 ADMIN_ROLE=$(keystone role-list | awk "/ admin / { print \$2 }")
155
156 # Ironic
157 if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then
158 IRONIC_USER=$(keystone user-create \
159 --name=ironic \
160 --pass="$SERVICE_PASSWORD" \
Dirk Mueller25049cd2014-01-09 13:53:52 +0100161 --tenant-id $SERVICE_TENANT \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300162 --email=ironic@example.com \
163 | grep " id " | get_field 2)
164 keystone user-role-add \
Dirk Mueller25049cd2014-01-09 13:53:52 +0100165 --tenant-id $SERVICE_TENANT \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300166 --user_id $IRONIC_USER \
167 --role_id $ADMIN_ROLE
168 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
169 IRONIC_SERVICE=$(keystone service-create \
170 --name=ironic \
171 --type=baremetal \
172 --description="Ironic baremetal provisioning service" \
173 | grep " id " | get_field 2)
174 keystone endpoint-create \
175 --region RegionOne \
176 --service_id $IRONIC_SERVICE \
Roman Prykhodchenkof5002ef2013-09-24 19:09:26 +0300177 --publicurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
178 --adminurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
179 --internalurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300180 fi
181 fi
182}
183
184
185# init_ironic() - Initialize databases, etc.
186function init_ironic() {
187 # (Re)create ironic database
188 recreate_database ironic utf8
189
190 # Migrate ironic database
191 $IRONIC_BIN_DIR/ironic-dbsync
192
193 create_ironic_cache_dir
194
195 # Create keystone artifacts for Ironic.
196 create_ironic_accounts
197}
198
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300199# start_ironic() - Start running processes, including screen
200function start_ironic() {
201 # Start Ironic API server, if enabled.
202 if is_service_enabled ir-api; then
203 start_ironic_api
204 fi
205
206 # Start Ironic conductor, if enabled.
207 if is_service_enabled ir-cond; then
208 start_ironic_conductor
209 fi
210}
211
212# start_ironic_api() - Used by start_ironic().
213# Starts Ironic API server.
214function start_ironic_api() {
215 screen_it ir-api "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE"
216 echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..."
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800217 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 -0400218 die $LINENO "ir-api did not start"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300219 fi
220}
221
222# start_ironic_conductor() - Used by start_ironic().
223# Starts Ironic conductor.
224function start_ironic_conductor() {
225 screen_it ir-cond "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE"
226 # TODO(romcheg): Find a way to check whether the conductor has started.
227}
228
229# stop_ironic() - Stop running processes
230function stop_ironic() {
231 # Kill the Ironic screen windows
232 screen -S $SCREEN_NAME -p ir-api -X kill
233 screen -S $SCREEN_NAME -p ir-cond -X kill
234}
235
236
237# Restore xtrace
238$XTRACE
239
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100240# Tell emacs to use shell-script-mode
241## Local variables:
242## mode: shell-script
243## End: