blob: 8c1f52b330215fd2bf9ad803260be7fb1e5f3692 [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:
5# ``functions`` file
6# ``DEST``, ``DATA_DIR``, ``STACK_USER`` must be defined
7# ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
8# ``SERVICE_HOST``
9# ``KEYSTONE_TOKEN_FORMAT`` must be defined
10
11# ``stack.sh`` calls the entry points in this order:
12#
13# install_ironic
14# configure_ironic
15# init_ironic
16# start_ironic
17# stop_ironic
18# cleanup_ironic
19
20# Save trace setting
21XTRACE=$(set +o | grep xtrace)
22set +o xtrace
23
24
25# Defaults
26# --------
27
28# Set up default directories
29IRONIC_DIR=$DEST/ironic
30IRONIC_AUTH_CACHE_DIR=${IRONIC_AUTH_CACHE_DIR:-/var/cache/ironic}
31IRONIC_CONF_DIR=${IRONIC_CONF_DIR:-/etc/ironic}
32IRONIC_CONF_FILE=$IRONIC_CONF_DIR/ironic.conf
33IRONIC_ROOTWRAP_CONF=$IRONIC_CONF_DIR/rootwrap.conf
34IRONIC_ROOTWRAP_FILTERS=$IRONIC_CONF_DIR/rootwrap.d
35IRONIC_POLICY_JSON=$IRONIC_CONF_DIR/policy.json
36
37# Support entry points installation of console scripts
38IRONIC_BIN_DIR=$(get_python_exec_prefix)
39
40# Ironic connection info. Note the port must be specified.
41IRONIC_SERVICE_PROTOCOL=http
42IRONIC_HOSTPORT=${IRONIC_HOSTPORT:-$SERVICE_HOST:6385}
43
44
45# Functions
46# ---------
47
48# cleanup_ironic() - Remove residual data files, anything left over from previous
49# runs that would need to clean up.
50function cleanup_ironic() {
51 sudo rm -rf $IRONIC_AUTH_CACHE_DIR
52}
53
54# configure_ironic() - Set config files, create data dirs, etc
55function configure_ironic() {
56 if [[ ! -d $IRONIC_CONF_DIR ]]; then
57 sudo mkdir -p $IRONIC_CONF_DIR
58 fi
59 sudo chown $STACK_USER $IRONIC_CONF_DIR
60
61 # Copy over ironic configuration file and configure common parameters.
62 cp $IRONIC_DIR/etc/ironic/ironic.conf.sample $IRONIC_CONF_FILE
63 iniset $IRONIC_CONF_FILE DEFAULT debug True
64 inicomment $IRONIC_CONF_FILE DEFAULT log_file
65 iniset $IRONIC_CONF_FILE DEFAULT sql_connection `database_connection_url ironic`
66 iniset $IRONIC_CONF_FILE DEFAULT use_syslog $SYSLOG
67
68 # Configure Ironic conductor, if it was enabled.
69 if is_service_enabled ir-cond; then
70 configure_ironic_conductor
71 fi
72
73 # Configure Ironic API, if it was enabled.
74 if is_service_enabled ir-api; then
75 configure_ironic_api
76 fi
77}
78
79# configure_ironic_api() - Is used by configure_ironic(). Performs
80# API specific configuration.
81function configure_ironic_api() {
Roman Prykhodchenkoc48c3122013-10-01 17:19:05 +030082 iniset $IRONIC_CONF_FILE DEFAULT auth_strategy keystone
83 iniset $IRONIC_CONF_FILE DEFAULT policy_file $IRONIC_POLICY_JSON
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +030084 iniset $IRONIC_CONF_FILE keystone_authtoken auth_host $KEYSTONE_AUTH_HOST
85 iniset $IRONIC_CONF_FILE keystone_authtoken auth_port $KEYSTONE_AUTH_PORT
86 iniset $IRONIC_CONF_FILE keystone_authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL
87 iniset $IRONIC_CONF_FILE keystone_authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/
88 iniset $IRONIC_CONF_FILE keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME
89 iniset $IRONIC_CONF_FILE keystone_authtoken admin_user ironic
90 iniset $IRONIC_CONF_FILE keystone_authtoken admin_password $SERVICE_PASSWORD
91 if is_service_enabled qpid; then
92 iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy qpid
93 elif [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; then
94 iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy rabbit
95 fi
96 iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT
97 iniset $IRONIC_CONF_FILE keystone_authtoken signing_dir $IRONIC_AUTH_CACHE_DIR/api
98
99 cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON
100}
101
102# configure_ironic_conductor() - Is used by configure_ironic().
103# Sets conductor specific settings.
104function configure_ironic_conductor() {
105 cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF
106 cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_ROOTWRAP_FILTERS
107
108 iniset $IRONIC_CONF DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF
109}
110
111# create_ironic_cache_dir() - Part of the init_ironic() process
112function create_ironic_cache_dir() {
113 # Create cache dir
114 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api
115 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api
116 rm -f $IRONIC_AUTH_CACHE_DIR/api/*
117 sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry
118 sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry
119 rm -f $IRONIC_AUTH_CACHE_DIR/registry/*
120}
121
122# create_ironic_accounts() - Set up common required ironic accounts
123
124# Tenant User Roles
125# ------------------------------------------------------------------
126# service ironic admin # if enabled
127create_ironic_accounts() {
128
129 SERVICE_TENANT=$(keystone tenant-list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }")
130 ADMIN_ROLE=$(keystone role-list | awk "/ admin / { print \$2 }")
131
132 # Ironic
133 if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then
134 IRONIC_USER=$(keystone user-create \
135 --name=ironic \
136 --pass="$SERVICE_PASSWORD" \
137 --tenant_id $SERVICE_TENANT \
138 --email=ironic@example.com \
139 | grep " id " | get_field 2)
140 keystone user-role-add \
141 --tenant_id $SERVICE_TENANT \
142 --user_id $IRONIC_USER \
143 --role_id $ADMIN_ROLE
144 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
145 IRONIC_SERVICE=$(keystone service-create \
146 --name=ironic \
147 --type=baremetal \
148 --description="Ironic baremetal provisioning service" \
149 | grep " id " | get_field 2)
150 keystone endpoint-create \
151 --region RegionOne \
152 --service_id $IRONIC_SERVICE \
Roman Prykhodchenkof5002ef2013-09-24 19:09:26 +0300153 --publicurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
154 --adminurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \
155 --internalurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT"
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300156 fi
157 fi
158}
159
160
161# init_ironic() - Initialize databases, etc.
162function init_ironic() {
163 # (Re)create ironic database
164 recreate_database ironic utf8
165
166 # Migrate ironic database
167 $IRONIC_BIN_DIR/ironic-dbsync
168
169 create_ironic_cache_dir
170
171 # Create keystone artifacts for Ironic.
172 create_ironic_accounts
173}
174
175# install_ironic() - Collect source and prepare
176function install_ironic() {
177 git_clone $IRONIC_REPO $IRONIC_DIR $IRONIC_BRANCH
178 setup_develop $IRONIC_DIR
179}
180
181# start_ironic() - Start running processes, including screen
182function start_ironic() {
183 # Start Ironic API server, if enabled.
184 if is_service_enabled ir-api; then
185 start_ironic_api
186 fi
187
188 # Start Ironic conductor, if enabled.
189 if is_service_enabled ir-cond; then
190 start_ironic_conductor
191 fi
192}
193
194# start_ironic_api() - Used by start_ironic().
195# Starts Ironic API server.
196function start_ironic_api() {
197 screen_it ir-api "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE"
198 echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..."
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800199 if ! timeout $SERVICE_TIMEOUT sh -c "while ! wget --no-proxy -q -O- http://$IRONIC_HOSTPORT; do sleep 1; done"; then
Roman Prykhodchenkoce696b62013-08-09 10:40:45 +0300200 die $LINENO "ir-api did not start"
201 fi
202}
203
204# start_ironic_conductor() - Used by start_ironic().
205# Starts Ironic conductor.
206function start_ironic_conductor() {
207 screen_it ir-cond "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE"
208 # TODO(romcheg): Find a way to check whether the conductor has started.
209}
210
211# stop_ironic() - Stop running processes
212function stop_ironic() {
213 # Kill the Ironic screen windows
214 screen -S $SCREEN_NAME -p ir-api -X kill
215 screen -S $SCREEN_NAME -p ir-cond -X kill
216}
217
218
219# Restore xtrace
220$XTRACE
221
222# Local variables:
223# mode: shell-script
224# End: