| 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() { | 
| Roman Prykhodchenko | c48c312 | 2013-10-01 17:19:05 +0300 | [diff] [blame] | 96 | iniset $IRONIC_CONF_FILE DEFAULT auth_strategy keystone | 
|  | 97 | iniset $IRONIC_CONF_FILE DEFAULT policy_file $IRONIC_POLICY_JSON | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 98 | iniset $IRONIC_CONF_FILE keystone_authtoken auth_host $KEYSTONE_AUTH_HOST | 
|  | 99 | iniset $IRONIC_CONF_FILE keystone_authtoken auth_port $KEYSTONE_AUTH_PORT | 
|  | 100 | iniset $IRONIC_CONF_FILE keystone_authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 101 | iniset $IRONIC_CONF_FILE keystone_authtoken cafile $KEYSTONE_SSL_CA | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 102 | iniset $IRONIC_CONF_FILE keystone_authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/ | 
|  | 103 | iniset $IRONIC_CONF_FILE keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME | 
|  | 104 | iniset $IRONIC_CONF_FILE keystone_authtoken admin_user ironic | 
|  | 105 | iniset $IRONIC_CONF_FILE keystone_authtoken admin_password $SERVICE_PASSWORD | 
|  | 106 | if is_service_enabled qpid; then | 
|  | 107 | iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy qpid | 
|  | 108 | elif [ -n "$RABBIT_HOST" ] &&  [ -n "$RABBIT_PASSWORD" ]; then | 
|  | 109 | iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy rabbit | 
|  | 110 | fi | 
|  | 111 | iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT | 
|  | 112 | iniset $IRONIC_CONF_FILE keystone_authtoken signing_dir $IRONIC_AUTH_CACHE_DIR/api | 
|  | 113 |  | 
|  | 114 | cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | # configure_ironic_conductor() - Is used by configure_ironic(). | 
|  | 118 | # Sets conductor specific settings. | 
|  | 119 | function configure_ironic_conductor() { | 
|  | 120 | cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF | 
|  | 121 | cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_ROOTWRAP_FILTERS | 
|  | 122 |  | 
|  | 123 | iniset $IRONIC_CONF DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | # create_ironic_cache_dir() - Part of the init_ironic() process | 
|  | 127 | function create_ironic_cache_dir() { | 
|  | 128 | # Create cache dir | 
|  | 129 | sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api | 
|  | 130 | sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api | 
|  | 131 | rm -f $IRONIC_AUTH_CACHE_DIR/api/* | 
|  | 132 | sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry | 
|  | 133 | sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry | 
|  | 134 | rm -f $IRONIC_AUTH_CACHE_DIR/registry/* | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | # create_ironic_accounts() - Set up common required ironic accounts | 
|  | 138 |  | 
|  | 139 | # Tenant               User       Roles | 
|  | 140 | # ------------------------------------------------------------------ | 
|  | 141 | # service              ironic     admin        # if enabled | 
|  | 142 | create_ironic_accounts() { | 
|  | 143 |  | 
|  | 144 | SERVICE_TENANT=$(keystone tenant-list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }") | 
|  | 145 | ADMIN_ROLE=$(keystone role-list | awk "/ admin / { print \$2 }") | 
|  | 146 |  | 
|  | 147 | # Ironic | 
|  | 148 | if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then | 
|  | 149 | IRONIC_USER=$(keystone user-create \ | 
|  | 150 | --name=ironic \ | 
|  | 151 | --pass="$SERVICE_PASSWORD" \ | 
| Dirk Mueller | 25049cd | 2014-01-09 13:53:52 +0100 | [diff] [blame] | 152 | --tenant-id $SERVICE_TENANT \ | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 153 | --email=ironic@example.com \ | 
|  | 154 | | grep " id " | get_field 2) | 
|  | 155 | keystone user-role-add \ | 
| Dirk Mueller | 25049cd | 2014-01-09 13:53:52 +0100 | [diff] [blame] | 156 | --tenant-id $SERVICE_TENANT \ | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 157 | --user_id $IRONIC_USER \ | 
|  | 158 | --role_id $ADMIN_ROLE | 
|  | 159 | if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then | 
|  | 160 | IRONIC_SERVICE=$(keystone service-create \ | 
|  | 161 | --name=ironic \ | 
|  | 162 | --type=baremetal \ | 
|  | 163 | --description="Ironic baremetal provisioning service" \ | 
|  | 164 | | grep " id " | get_field 2) | 
|  | 165 | keystone endpoint-create \ | 
|  | 166 | --region RegionOne \ | 
|  | 167 | --service_id $IRONIC_SERVICE \ | 
| Roman Prykhodchenko | f5002ef | 2013-09-24 19:09:26 +0300 | [diff] [blame] | 168 | --publicurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \ | 
|  | 169 | --adminurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \ | 
|  | 170 | --internalurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 171 | fi | 
|  | 172 | fi | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 |  | 
|  | 176 | # init_ironic() - Initialize databases, etc. | 
|  | 177 | function init_ironic() { | 
|  | 178 | # (Re)create  ironic database | 
|  | 179 | recreate_database ironic utf8 | 
|  | 180 |  | 
|  | 181 | # Migrate ironic database | 
|  | 182 | $IRONIC_BIN_DIR/ironic-dbsync | 
|  | 183 |  | 
|  | 184 | create_ironic_cache_dir | 
|  | 185 |  | 
|  | 186 | # Create keystone artifacts for Ironic. | 
|  | 187 | create_ironic_accounts | 
|  | 188 | } | 
|  | 189 |  | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 190 | # start_ironic() - Start running processes, including screen | 
|  | 191 | function start_ironic() { | 
|  | 192 | # Start Ironic API server, if enabled. | 
|  | 193 | if is_service_enabled ir-api; then | 
|  | 194 | start_ironic_api | 
|  | 195 | fi | 
|  | 196 |  | 
|  | 197 | # Start Ironic conductor, if enabled. | 
|  | 198 | if is_service_enabled ir-cond; then | 
|  | 199 | start_ironic_conductor | 
|  | 200 | fi | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | # start_ironic_api() - Used by start_ironic(). | 
|  | 204 | # Starts Ironic API server. | 
|  | 205 | function start_ironic_api() { | 
|  | 206 | screen_it ir-api "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE" | 
|  | 207 | echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..." | 
| JUN JIE NAN | 0aa8534 | 2013-09-13 15:47:09 +0800 | [diff] [blame] | 208 | 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] | 209 | die $LINENO "ir-api did not start" | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 210 | fi | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | # start_ironic_conductor() - Used by start_ironic(). | 
|  | 214 | # Starts Ironic conductor. | 
|  | 215 | function start_ironic_conductor() { | 
|  | 216 | screen_it ir-cond "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE" | 
|  | 217 | # TODO(romcheg): Find a way to check whether the conductor has started. | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | # stop_ironic() - Stop running processes | 
|  | 221 | function stop_ironic() { | 
|  | 222 | # Kill the Ironic screen windows | 
|  | 223 | screen -S $SCREEN_NAME -p ir-api -X kill | 
|  | 224 | screen -S $SCREEN_NAME -p ir-cond -X kill | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 |  | 
|  | 228 | # Restore xtrace | 
|  | 229 | $XTRACE | 
|  | 230 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 231 | # Tell emacs to use shell-script-mode | 
|  | 232 | ## Local variables: | 
|  | 233 | ## mode: shell-script | 
|  | 234 | ## End: |