| 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 | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 36 | IRONIC_POLICY_JSON=$IRONIC_CONF_DIR/policy.json | 
|  | 37 |  | 
|  | 38 | # Support entry points installation of console scripts | 
|  | 39 | IRONIC_BIN_DIR=$(get_python_exec_prefix) | 
|  | 40 |  | 
|  | 41 | # Ironic connection info.  Note the port must be specified. | 
|  | 42 | IRONIC_SERVICE_PROTOCOL=http | 
|  | 43 | IRONIC_HOSTPORT=${IRONIC_HOSTPORT:-$SERVICE_HOST:6385} | 
|  | 44 |  | 
| Dean Troyer | 4237f59 | 2014-01-29 16:22:11 -0600 | [diff] [blame] | 45 | # Tell Tempest this project is present | 
|  | 46 | TEMPEST_SERVICES+=,ironic | 
|  | 47 |  | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 48 |  | 
|  | 49 | # Functions | 
|  | 50 | # --------- | 
|  | 51 |  | 
| Dean Troyer | 1023ff7 | 2014-01-27 14:56:44 -0600 | [diff] [blame] | 52 | # Test if any Ironic services are enabled | 
|  | 53 | # is_ironic_enabled | 
|  | 54 | function is_ironic_enabled { | 
|  | 55 | [[ ,${ENABLED_SERVICES} =~ ,"ir-" ]] && return 0 | 
|  | 56 | return 1 | 
|  | 57 | } | 
|  | 58 |  | 
| Roman Prykhodchenko | 43e0066 | 2013-10-15 17:03:15 +0300 | [diff] [blame] | 59 | # install_ironic() - Collect source and prepare | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 60 | function install_ironic { | 
| Roman Prykhodchenko | 43e0066 | 2013-10-15 17:03:15 +0300 | [diff] [blame] | 61 | git_clone $IRONIC_REPO $IRONIC_DIR $IRONIC_BRANCH | 
|  | 62 | setup_develop $IRONIC_DIR | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | # install_ironicclient() - Collect sources and prepare | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 66 | function install_ironicclient { | 
| Roman Prykhodchenko | 43e0066 | 2013-10-15 17:03:15 +0300 | [diff] [blame] | 67 | git_clone $IRONICCLIENT_REPO $IRONICCLIENT_DIR $IRONICCLIENT_BRANCH | 
|  | 68 | setup_develop $IRONICCLIENT_DIR | 
|  | 69 | } | 
|  | 70 |  | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 71 | # cleanup_ironic() - Remove residual data files, anything left over from previous | 
|  | 72 | # runs that would need to clean up. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 73 | function cleanup_ironic { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 74 | sudo rm -rf $IRONIC_AUTH_CACHE_DIR | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | # configure_ironic() - Set config files, create data dirs, etc | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 78 | function configure_ironic { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 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. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 104 | function configure_ironic_api { | 
| Roman Prykhodchenko | c48c312 | 2013-10-01 17:19:05 +0300 | [diff] [blame] | 105 | iniset $IRONIC_CONF_FILE DEFAULT auth_strategy keystone | 
|  | 106 | iniset $IRONIC_CONF_FILE DEFAULT policy_file $IRONIC_POLICY_JSON | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 107 | 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 Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 110 | iniset $IRONIC_CONF_FILE keystone_authtoken cafile $KEYSTONE_SSL_CA | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 111 | 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 | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 115 | iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT | 
|  | 116 | iniset $IRONIC_CONF_FILE keystone_authtoken signing_dir $IRONIC_AUTH_CACHE_DIR/api | 
|  | 117 |  | 
|  | 118 | cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | # configure_ironic_conductor() - Is used by configure_ironic(). | 
|  | 122 | # Sets conductor specific settings. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 123 | function configure_ironic_conductor { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 124 | cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF | 
| Lucas Alvares Gomes | 279295c | 2014-01-14 11:37:51 +0000 | [diff] [blame] | 125 | cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_CONF_DIR | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 126 |  | 
| Alexander Gordeev | a67cb1a | 2014-03-04 18:38:33 +0400 | [diff] [blame^] | 127 | iniset $IRONIC_CONF_FILE DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
|  | 130 | # create_ironic_cache_dir() - Part of the init_ironic() process | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 131 | function create_ironic_cache_dir { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 132 | # Create cache dir | 
|  | 133 | sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api | 
|  | 134 | sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api | 
|  | 135 | rm -f $IRONIC_AUTH_CACHE_DIR/api/* | 
|  | 136 | sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry | 
|  | 137 | sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry | 
|  | 138 | rm -f $IRONIC_AUTH_CACHE_DIR/registry/* | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | # create_ironic_accounts() - Set up common required ironic accounts | 
|  | 142 |  | 
|  | 143 | # Tenant               User       Roles | 
|  | 144 | # ------------------------------------------------------------------ | 
|  | 145 | # service              ironic     admin        # if enabled | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 146 | function create_ironic_accounts { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 147 |  | 
| Steve Martinelli | 1968542 | 2014-01-24 13:02:26 -0600 | [diff] [blame] | 148 | SERVICE_TENANT=$(openstack project list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }") | 
|  | 149 | ADMIN_ROLE=$(openstack role list | awk "/ admin / { print \$2 }") | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 150 |  | 
|  | 151 | # Ironic | 
|  | 152 | if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then | 
| Steve Martinelli | 1968542 | 2014-01-24 13:02:26 -0600 | [diff] [blame] | 153 | IRONIC_USER=$(openstack user create \ | 
|  | 154 | ironic \ | 
|  | 155 | --password "$SERVICE_PASSWORD" \ | 
|  | 156 | --project $SERVICE_TENANT \ | 
|  | 157 | --email ironic@example.com \ | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 158 | | grep " id " | get_field 2) | 
| Steve Martinelli | 1968542 | 2014-01-24 13:02:26 -0600 | [diff] [blame] | 159 | openstack role add \ | 
|  | 160 | $ADMIN_ROLE \ | 
|  | 161 | --project $SERVICE_TENANT \ | 
|  | 162 | --user $IRONIC_USER | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 163 | if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then | 
| Steve Martinelli | 1968542 | 2014-01-24 13:02:26 -0600 | [diff] [blame] | 164 | IRONIC_SERVICE=$(openstack service create \ | 
|  | 165 | ironic \ | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 166 | --type=baremetal \ | 
|  | 167 | --description="Ironic baremetal provisioning service" \ | 
|  | 168 | | grep " id " | get_field 2) | 
| Steve Martinelli | 1968542 | 2014-01-24 13:02:26 -0600 | [diff] [blame] | 169 | openstack endpoint create \ | 
|  | 170 | $IRONIC_SERVICE \ | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 171 | --region RegionOne \ | 
| Roman Prykhodchenko | f5002ef | 2013-09-24 19:09:26 +0300 | [diff] [blame] | 172 | --publicurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \ | 
|  | 173 | --adminurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" \ | 
|  | 174 | --internalurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT" | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 175 | fi | 
|  | 176 | fi | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 |  | 
|  | 180 | # init_ironic() - Initialize databases, etc. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 181 | function init_ironic { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 182 | # (Re)create  ironic database | 
|  | 183 | recreate_database ironic utf8 | 
|  | 184 |  | 
|  | 185 | # Migrate ironic database | 
|  | 186 | $IRONIC_BIN_DIR/ironic-dbsync | 
|  | 187 |  | 
|  | 188 | create_ironic_cache_dir | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 191 | # start_ironic() - Start running processes, including screen | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 192 | function start_ironic { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 193 | # Start Ironic API server, if enabled. | 
|  | 194 | if is_service_enabled ir-api; then | 
|  | 195 | start_ironic_api | 
|  | 196 | fi | 
|  | 197 |  | 
|  | 198 | # Start Ironic conductor, if enabled. | 
|  | 199 | if is_service_enabled ir-cond; then | 
|  | 200 | start_ironic_conductor | 
|  | 201 | fi | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | # start_ironic_api() - Used by start_ironic(). | 
|  | 205 | # Starts Ironic API server. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 206 | function start_ironic_api { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 207 | screen_it ir-api "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE" | 
|  | 208 | echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..." | 
| JUN JIE NAN | 0aa8534 | 2013-09-13 15:47:09 +0800 | [diff] [blame] | 209 | 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] | 210 | die $LINENO "ir-api did not start" | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 211 | fi | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | # start_ironic_conductor() - Used by start_ironic(). | 
|  | 215 | # Starts Ironic conductor. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 216 | function start_ironic_conductor { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 217 | screen_it ir-cond "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE" | 
|  | 218 | # TODO(romcheg): Find a way to check whether the conductor has started. | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | # stop_ironic() - Stop running processes | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 222 | function stop_ironic { | 
| Roman Prykhodchenko | ce696b6 | 2013-08-09 10:40:45 +0300 | [diff] [blame] | 223 | # Kill the Ironic screen windows | 
|  | 224 | screen -S $SCREEN_NAME -p ir-api -X kill | 
|  | 225 | screen -S $SCREEN_NAME -p ir-cond -X kill | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 |  | 
|  | 229 | # Restore xtrace | 
|  | 230 | $XTRACE | 
|  | 231 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 232 | # Tell emacs to use shell-script-mode | 
|  | 233 | ## Local variables: | 
|  | 234 | ## mode: shell-script | 
|  | 235 | ## End: |