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: |
| 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 |
| 21 | XTRACE=$(set +o | grep xtrace) |
| 22 | set +o xtrace |
| 23 | |
| 24 | |
| 25 | # Defaults |
| 26 | # -------- |
| 27 | |
| 28 | # Set up default directories |
| 29 | IRONIC_DIR=$DEST/ironic |
| 30 | IRONIC_AUTH_CACHE_DIR=${IRONIC_AUTH_CACHE_DIR:-/var/cache/ironic} |
| 31 | IRONIC_CONF_DIR=${IRONIC_CONF_DIR:-/etc/ironic} |
| 32 | IRONIC_CONF_FILE=$IRONIC_CONF_DIR/ironic.conf |
| 33 | IRONIC_ROOTWRAP_CONF=$IRONIC_CONF_DIR/rootwrap.conf |
| 34 | IRONIC_ROOTWRAP_FILTERS=$IRONIC_CONF_DIR/rootwrap.d |
| 35 | IRONIC_POLICY_JSON=$IRONIC_CONF_DIR/policy.json |
| 36 | |
| 37 | # Support entry points installation of console scripts |
| 38 | IRONIC_BIN_DIR=$(get_python_exec_prefix) |
| 39 | |
| 40 | # Ironic connection info. Note the port must be specified. |
| 41 | IRONIC_SERVICE_PROTOCOL=http |
| 42 | IRONIC_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. |
| 50 | function cleanup_ironic() { |
| 51 | sudo rm -rf $IRONIC_AUTH_CACHE_DIR |
| 52 | } |
| 53 | |
| 54 | # configure_ironic() - Set config files, create data dirs, etc |
| 55 | function 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. |
| 81 | function configure_ironic_api() { |
| 82 | iniset $IRONIC_CONF_FILE keystone_authtoken auth_host $KEYSTONE_AUTH_HOST |
| 83 | iniset $IRONIC_CONF_FILE keystone_authtoken auth_port $KEYSTONE_AUTH_PORT |
| 84 | iniset $IRONIC_CONF_FILE keystone_authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL |
| 85 | iniset $IRONIC_CONF_FILE keystone_authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/ |
| 86 | iniset $IRONIC_CONF_FILE keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME |
| 87 | iniset $IRONIC_CONF_FILE keystone_authtoken admin_user ironic |
| 88 | iniset $IRONIC_CONF_FILE keystone_authtoken admin_password $SERVICE_PASSWORD |
| 89 | if is_service_enabled qpid; then |
| 90 | iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy qpid |
| 91 | elif [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; then |
| 92 | iniset $IRONIC_CONF_FILE DEFAULT notifier_strategy rabbit |
| 93 | fi |
| 94 | iniset_rpc_backend ironic $IRONIC_CONF_FILE DEFAULT |
| 95 | iniset $IRONIC_CONF_FILE keystone_authtoken signing_dir $IRONIC_AUTH_CACHE_DIR/api |
| 96 | |
| 97 | cp -p $IRONIC_DIR/etc/ironic/policy.json $IRONIC_POLICY_JSON |
| 98 | } |
| 99 | |
| 100 | # configure_ironic_conductor() - Is used by configure_ironic(). |
| 101 | # Sets conductor specific settings. |
| 102 | function configure_ironic_conductor() { |
| 103 | cp $IRONIC_DIR/etc/ironic/rootwrap.conf $IRONIC_ROOTWRAP_CONF |
| 104 | cp -r $IRONIC_DIR/etc/ironic/rootwrap.d $IRONIC_ROOTWRAP_FILTERS |
| 105 | |
| 106 | iniset $IRONIC_CONF DEFAULT rootwrap_config $IRONIC_ROOTWRAP_CONF |
| 107 | } |
| 108 | |
| 109 | # create_ironic_cache_dir() - Part of the init_ironic() process |
| 110 | function create_ironic_cache_dir() { |
| 111 | # Create cache dir |
| 112 | sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/api |
| 113 | sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/api |
| 114 | rm -f $IRONIC_AUTH_CACHE_DIR/api/* |
| 115 | sudo mkdir -p $IRONIC_AUTH_CACHE_DIR/registry |
| 116 | sudo chown $STACK_USER $IRONIC_AUTH_CACHE_DIR/registry |
| 117 | rm -f $IRONIC_AUTH_CACHE_DIR/registry/* |
| 118 | } |
| 119 | |
| 120 | # create_ironic_accounts() - Set up common required ironic accounts |
| 121 | |
| 122 | # Tenant User Roles |
| 123 | # ------------------------------------------------------------------ |
| 124 | # service ironic admin # if enabled |
| 125 | create_ironic_accounts() { |
| 126 | |
| 127 | SERVICE_TENANT=$(keystone tenant-list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }") |
| 128 | ADMIN_ROLE=$(keystone role-list | awk "/ admin / { print \$2 }") |
| 129 | |
| 130 | # Ironic |
| 131 | if [[ "$ENABLED_SERVICES" =~ "ir-api" ]]; then |
| 132 | IRONIC_USER=$(keystone user-create \ |
| 133 | --name=ironic \ |
| 134 | --pass="$SERVICE_PASSWORD" \ |
| 135 | --tenant_id $SERVICE_TENANT \ |
| 136 | --email=ironic@example.com \ |
| 137 | | grep " id " | get_field 2) |
| 138 | keystone user-role-add \ |
| 139 | --tenant_id $SERVICE_TENANT \ |
| 140 | --user_id $IRONIC_USER \ |
| 141 | --role_id $ADMIN_ROLE |
| 142 | if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then |
| 143 | IRONIC_SERVICE=$(keystone service-create \ |
| 144 | --name=ironic \ |
| 145 | --type=baremetal \ |
| 146 | --description="Ironic baremetal provisioning service" \ |
| 147 | | grep " id " | get_field 2) |
| 148 | keystone endpoint-create \ |
| 149 | --region RegionOne \ |
| 150 | --service_id $IRONIC_SERVICE \ |
| 151 | --publicurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT/v1/" \ |
| 152 | --adminurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT/v1/" \ |
| 153 | --internalurl "$IRONIC_SERVICE_PROTOCOL://$IRONIC_HOSTPORT/v1/" |
| 154 | fi |
| 155 | fi |
| 156 | } |
| 157 | |
| 158 | |
| 159 | # init_ironic() - Initialize databases, etc. |
| 160 | function init_ironic() { |
| 161 | # (Re)create ironic database |
| 162 | recreate_database ironic utf8 |
| 163 | |
| 164 | # Migrate ironic database |
| 165 | $IRONIC_BIN_DIR/ironic-dbsync |
| 166 | |
| 167 | create_ironic_cache_dir |
| 168 | |
| 169 | # Create keystone artifacts for Ironic. |
| 170 | create_ironic_accounts |
| 171 | } |
| 172 | |
| 173 | # install_ironic() - Collect source and prepare |
| 174 | function install_ironic() { |
| 175 | git_clone $IRONIC_REPO $IRONIC_DIR $IRONIC_BRANCH |
| 176 | setup_develop $IRONIC_DIR |
| 177 | } |
| 178 | |
| 179 | # start_ironic() - Start running processes, including screen |
| 180 | function start_ironic() { |
| 181 | # Start Ironic API server, if enabled. |
| 182 | if is_service_enabled ir-api; then |
| 183 | start_ironic_api |
| 184 | fi |
| 185 | |
| 186 | # Start Ironic conductor, if enabled. |
| 187 | if is_service_enabled ir-cond; then |
| 188 | start_ironic_conductor |
| 189 | fi |
| 190 | } |
| 191 | |
| 192 | # start_ironic_api() - Used by start_ironic(). |
| 193 | # Starts Ironic API server. |
| 194 | function start_ironic_api() { |
| 195 | screen_it ir-api "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-api --config-file=$IRONIC_CONF_FILE" |
| 196 | echo "Waiting for ir-api ($IRONIC_HOSTPORT) to start..." |
| 197 | if ! timeout $SERVICE_TIMEOUT sh -c "while ! http_proxy= wget -q -O- http://$IRONIC_HOSTPORT; do sleep 1; done"; then |
| 198 | die $LINENO "ir-api did not start" |
| 199 | fi |
| 200 | } |
| 201 | |
| 202 | # start_ironic_conductor() - Used by start_ironic(). |
| 203 | # Starts Ironic conductor. |
| 204 | function start_ironic_conductor() { |
| 205 | screen_it ir-cond "cd $IRONIC_DIR; $IRONIC_BIN_DIR/ironic-conductor --config-file=$IRONIC_CONF_FILE" |
| 206 | # TODO(romcheg): Find a way to check whether the conductor has started. |
| 207 | } |
| 208 | |
| 209 | # stop_ironic() - Stop running processes |
| 210 | function stop_ironic() { |
| 211 | # Kill the Ironic screen windows |
| 212 | screen -S $SCREEN_NAME -p ir-api -X kill |
| 213 | screen -S $SCREEN_NAME -p ir-cond -X kill |
| 214 | } |
| 215 | |
| 216 | |
| 217 | # Restore xtrace |
| 218 | $XTRACE |
| 219 | |
| 220 | # Local variables: |
| 221 | # mode: shell-script |
| 222 | # End: |