blob: afb7c23d2c693380664297c44b4dd71ed6928ff2 [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
45
46# Functions
47# ---------
48
Dean Troyer1023ff72014-01-27 14:56:44 -060049# Test if any Ironic services are enabled
50# is_ironic_enabled
51function is_ironic_enabled {
52 [[ ,${ENABLED_SERVICES} =~ ,"ir-" ]] && return 0
53 return 1
54}
55
Roman Prykhodchenko43e00662013-10-15 17:03:15 +030056# install_ironic() - Collect source and prepare
57function install_ironic() {
58 git_clone $IRONIC_REPO $IRONIC_DIR $IRONIC_BRANCH
59 setup_develop $IRONIC_DIR
60}
61
62# install_ironicclient() - Collect sources and prepare
63function install_ironicclient() {
64 git_clone $IRONICCLIENT_REPO $IRONICCLIENT_DIR $IRONICCLIENT_BRANCH
65 setup_develop $IRONICCLIENT_DIR
66}
67
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030068# cleanup_ironic() - Remove residual data files, anything left over from previous
69# runs that would need to clean up.
70function cleanup_ironic() {
71 sudo rm -rf $IRONIC_AUTH_CACHE_DIR
72}
73
74# configure_ironic() - Set config files, create data dirs, etc
75function configure_ironic() {
76 if [[ ! -d $IRONIC_CONF_DIR ]]; then
77 sudo mkdir -p $IRONIC_CONF_DIR
78 fi
79 sudo chown $STACK_USER $IRONIC_CONF_DIR
80
81 # Copy over ironic configuration file and configure common parameters.
82 cp $IRONIC_DIR/etc/ironic/ironic.conf.sample $IRONIC_CONF_FILE
83 iniset $IRONIC_CONF_FILE DEFAULT debug True
84 inicomment $IRONIC_CONF_FILE DEFAULT log_file
85 iniset $IRONIC_CONF_FILE DEFAULT sql_connection `database_connection_url ironic`
86 iniset $IRONIC_CONF_FILE DEFAULT use_syslog $SYSLOG
87
88 # Configure Ironic conductor, if it was enabled.
89 if is_service_enabled ir-cond; then
90 configure_ironic_conductor
91 fi
92
93 # Configure Ironic API, if it was enabled.
94 if is_service_enabled ir-api; then
95 configure_ironic_api
96 fi
97}
98
99# configure_ironic_api() - Is used by configure_ironic(). Performs
100# API specific configuration.
101function configure_ironic_api() {
Roman Prykhodchenkoc48c3122013-10-01 17:19:05 +0300102 iniset $IRONIC_CONF_FILE DEFAULT auth_strategy keystone
103 iniset $IRONIC_CONF_FILE DEFAULT policy_file $IRONIC_POLICY_JSON
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300104 iniset $IRONIC_CONF_FILE keystone_authtoken auth_host $KEYSTONE_AUTH_HOST
105 iniset $IRONIC_CONF_FILE keystone_authtoken auth_port $KEYSTONE_AUTH_PORT
106 iniset $IRONIC_CONF_FILE keystone_authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000107 iniset $IRONIC_CONF_FILE keystone_authtoken cafile $KEYSTONE_SSL_CA
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300108 iniset $IRONIC_CONF_FILE keystone_authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/
109 iniset $IRONIC_CONF_FILE keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME
110 iniset $IRONIC_CONF_FILE keystone_authtoken admin_user ironic
111 iniset $IRONIC_CONF_FILE keystone_authtoken admin_password $SERVICE_PASSWORD
112 if is_service_enabled qpid; then
113 iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy qpid
114 elif [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; then
115 iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy rabbit
116 fi
117 iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT
118 iniset $IRONIC_CONF_FILE keystone_authtoken signing_dir $IRONIC_AUTH_CACHE_DIR/api
119
120 cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON
121}
122
123# configure_ironic_conductor() - Is used by configure_ironic().
124# Sets conductor specific settings.
125function configure_ironic_conductor() {
126 cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF
Lucas Alvares Gomes279295c2014-01-14 11:37:51 +0000127 cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_CONF_DIR
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300128
129 iniset $IRONIC_CONF DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF
130}
131
132# create_ironic_cache_dir() - Part of the init_ironic() process
133function create_ironic_cache_dir() {
134 # Create cache dir
135 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api
136 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api
137 rm -f $IRONIC_AUTH_CACHE_DIR/api/*
138 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry
139 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry
140 rm -f $IRONIC_AUTH_CACHE_DIR/registry/*
141}
142
143# create_ironic_accounts() - Set up common required ironic accounts
144
145# Tenant User Roles
146# ------------------------------------------------------------------
147# service ironic admin # if enabled
148create_ironic_accounts() {
149
150 SERVICE_TENANT=$(keystone tenant-list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }")
151 ADMIN_ROLE=$(keystone role-list | awk "/ admin / { print \$2 }")
152
153 # Ironic
154 if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then
155 IRONIC_USER=$(keystone user-create \
156 --name=ironic \
157 --pass="$SERVICE_PASSWORD" \
Dirk Mueller25049cd2014-01-09 13:53:52 +0100158 --tenant-id $SERVICE_TENANT \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300159 --email=ironic@example.com \
160 | grep " id " | get_field 2)
161 keystone user-role-add \
Dirk Mueller25049cd2014-01-09 13:53:52 +0100162 --tenant-id $SERVICE_TENANT \
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300163 --user_id $IRONIC_USER \
164 --role_id $ADMIN_ROLE
165 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
166 IRONIC_SERVICE=$(keystone service-create \
167 --name=ironic \
168 --type=baremetal \
169 --description="Ironic baremetal provisioning service" \
170 | grep " id " | get_field 2)
171 keystone endpoint-create \
172 --region RegionOne \
173 --service_id $IRONIC_SERVICE \
Roman Prykhodchenkof5002ef2013-09-24 19:09:26 +0300174 --publicurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
175 --adminurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
176 --internalurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300177 fi
178 fi
179}
180
181
182# init_ironic() - Initialize databases, etc.
183function init_ironic() {
184 # (Re)create ironic database
185 recreate_database ironic utf8
186
187 # Migrate ironic database
188 $IRONIC_BIN_DIR/ironic-dbsync
189
190 create_ironic_cache_dir
191
192 # Create keystone artifacts for Ironic.
193 create_ironic_accounts
194}
195
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300196# start_ironic() - Start running processes, including screen
197function start_ironic() {
198 # Start Ironic API server, if enabled.
199 if is_service_enabled ir-api; then
200 start_ironic_api
201 fi
202
203 # Start Ironic conductor, if enabled.
204 if is_service_enabled ir-cond; then
205 start_ironic_conductor
206 fi
207}
208
209# start_ironic_api() - Used by start_ironic().
210# Starts Ironic API server.
211function start_ironic_api() {
212 screen_it ir-api "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE"
213 echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..."
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800214 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 -0400215 die $LINENO "ir-api did not start"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300216 fi
217}
218
219# start_ironic_conductor() - Used by start_ironic().
220# Starts Ironic conductor.
221function start_ironic_conductor() {
222 screen_it ir-cond "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE"
223 # TODO(romcheg): Find a way to check whether the conductor has started.
224}
225
226# stop_ironic() - Stop running processes
227function stop_ironic() {
228 # Kill the Ironic screen windows
229 screen -S $SCREEN_NAME -p ir-api -X kill
230 screen -S $SCREEN_NAME -p ir-cond -X kill
231}
232
233
234# Restore xtrace
235$XTRACE
236
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100237# Tell emacs to use shell-script-mode
238## Local variables:
239## mode: shell-script
240## End: