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