summaryrefslogtreecommitdiff
path: root/devstack/plugin.sh
blob: cdc24bc2c1c62e879213accb4b8ff5b38465e398 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# Install and start **Searchlight** service

# To enable Searchlight services, add the following to localrc
# enable_plugin searchlight https://git.openstack.org/openstack/searchlight
# enable_service searchlight-api
# enable_service searchlight-listener

# stack.sh
# ---------
# install_searchlight
# configure_searchlight
# init_searchlight
# start_searchlight
# stop_searchlight
# cleanup_searchlight

# Save trace setting
XTRACE=$(set +o | grep xtrace)
set +o xtrace


# Defaults
# --------
# Set up default repos
SEARCHLIGHT_REPO=${SEARCHLIGHT_REPO:-${GIT_BASE}/openstack/searchlight.git}
SEARCHLIGHT_BRANCH=${SEARCHLIGHT_BRANCH:-master}

# Set up default paths
SEARCHLIGHT_BIN_DIR=$(get_python_exec_prefix)
SEARCHLIGHT_DIR=$DEST/searchlight
SEARCHLIGHT_CONF_DIR=/etc/searchlight
SEARCHLIGHT_STATE_PATH=${SEARCHLIGHT_STATE_PATH:=$DATA_DIR/searchlight}
SEARCHLIGHT_CONF=$SEARCHLIGHT_CONF_DIR/searchlight.conf
SEARCHLIGHT_LOG_DIR=/var/log/searchlight
SEARCHLIGHT_AUTH_CACHE_DIR=${SEARCHLIGHT_AUTH_CACHE_DIR:-/var/cache/searchlight}
SEARCHLIGHT_APIPASTE_CONF=$SEARCHLIGHT_CONF_DIR/api-paste.ini
SEARCHLIGHT_UWSGI_CONF=$SEARCHLIGHT_CONF_DIR/searchlight-uwsgi.ini
SEARCHLIGHT_UWSGI_APP=$SEARCHLIGHT_BIN_DIR/searchlight-api-wsgi

if is_service_enabled tls-proxy; then
    SEARCHLIGHT_SERVICE_PROTOCOL="https"
fi

# Public IP/Port Settings
SEARCHLIGHT_SERVICE_PROTOCOL=${SEARCHLIGHT_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL}
SEARCHLIGHT_SERVICE_HOST=${SEARCHLIGHT_SERVICE_HOST:-$SERVICE_HOST}
SEARCHLIGHT_SERVICE_PORT=${SEARCHLIGHT_SERVICE_PORT:-9393}
SEARCHLIGHT_SERVICE_PORT_INT=${SEARCHLIGHT_SERVICE_PORT_INT:-19393}

ELASTICSEARCH_VERSION=${ELASTICSEARCH_VERSION:-2.3.4}
ELASTICSEARCH_BASEURL=${ELASTICSEARCH_BASEURL:-https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution}
ELASTICSEARCH_BASEURL_DEB=${ELASTICSEARCH_BASEURL}/deb/elasticsearch
ELASTICSEARCH_BASEURL_RPM=${ELASTICSEARCH_BASEURL}/rpm/elasticsearch

# Tell Tempest this project is present
TEMPEST_SERVICES+=,searchlight

# Helper Functions
# ----------------
function setup_colorized_logging_searchlight {
    local conf_file=$1
    local conf_section=$2
    local project_var=${3:-"project_name"}
    local user_var=${4:-"user_name"}

    setup_colorized_logging $conf_file $conf_section $project_var $user_var

    # Override the logging_context_format_string value chosen by
    # setup_colorized_logging.
    iniset $conf_file $conf_section logging_context_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [%(request_id)s %(user_identity)s%(color)s] %(instance)s%(color)s%(message)s"
}

# DevStack Plugin
# ---------------

# cleanup_searchlight - Remove residual data files, anything left over from previous
# runs that a clean run would need to clean up
function cleanup_searchlight {
    _stop_elasticsearch
    sudo rm -rf $SEARCHLIGHT_STATE_PATH $SEARCHLIGHT_AUTH_CACHE_DIR
    sudo rm -f $(apache_site_config_for searchlight_api)
}

# configure_searchlight - Set config files, create data dirs, etc
function configure_searchlight {
    [ ! -d $SEARCHLIGHT_CONF_DIR ] && sudo mkdir -m 755 -p $SEARCHLIGHT_CONF_DIR
    sudo chown $STACK_USER $SEARCHLIGHT_CONF_DIR

    [ ! -d $SEARCHLIGHT_LOG_DIR ] &&  sudo mkdir -m 755 -p $SEARCHLIGHT_LOG_DIR
    sudo chown $STACK_USER $SEARCHLIGHT_LOG_DIR

    # (Re)create ``searchlight.conf``
    rm -f $SEARCHLIGHT_CONF

    # General Configuration
    iniset_rpc_backend searchlight $SEARCHLIGHT_CONF DEFAULT

    iniset $SEARCHLIGHT_CONF DEFAULT debug $ENABLE_DEBUG_LOG_LEVEL
    iniset $SEARCHLIGHT_CONF DEFAULT state_path $SEARCHLIGHT_STATE_PATH

    # Install the policy file for the API server
    cp $SEARCHLIGHT_DIR/etc/policy.json $SEARCHLIGHT_CONF_DIR/policy.json
    iniset $SEARCHLIGHT_CONF oslo_policy policy_file $SEARCHLIGHT_CONF_DIR/policy.json

    # API Configuration
    sudo cp $SEARCHLIGHT_DIR/etc/api-paste.ini $SEARCHLIGHT_APIPASTE_CONF
    iniset $SEARCHLIGHT_CONF api public_endpoint $SEARCHLIGHT_SERVICE_PROTOCOL://$SEARCHLIGHT_SERVICE_HOST/search

    # OpenStack users
    iniset $SEARCHLIGHT_CONF service_credentials auth_type password
    iniset $SEARCHLIGHT_CONF service_credentials username searchlight
    iniset $SEARCHLIGHT_CONF service_credentials user_domain_id default
    iniset $SEARCHLIGHT_CONF service_credentials project_domain_id default
    iniset $SEARCHLIGHT_CONF service_credentials password $SERVICE_PASSWORD
    iniset $SEARCHLIGHT_CONF service_credentials project_name $SERVICE_PROJECT_NAME
    iniset $SEARCHLIGHT_CONF service_credentials auth_url $KEYSTONE_SERVICE_URI
    iniset $SEARCHLIGHT_CONF service_credentials os_region_name $REGION_NAME

    # Keystone Middleware
    iniset $SEARCHLIGHT_CONF paste_deploy flavor keystone
    configure_auth_token_middleware $SEARCHLIGHT_CONF searchlight $SEARCHLIGHT_AUTH_CACHE_DIR

    # Oslo Concurrency
    iniset $SEARCHLIGHT_CONF oslo_concurrency lock_path "$SEARCHLIGHT_STATE_PATH"

    # TLS Proxy Configuration
    if is_service_enabled tls-proxy; then
        # Set the service port for a proxy to take the original
        iniset $SEARCHLIGHT_CONF api bind_port $SEARCHLIGHT_SERVICE_PORT_INT
    else
        iniset $SEARCHLIGHT_CONF api bind_port $SEARCHLIGHT_SERVICE_PORT
    fi

    # Logging Configuration
    if [ "$SYSLOG" != "False" ]; then
        iniset $SEARCHLIGHT_CONF DEFAULT use_syslog True
    fi

    # Format logging
    if [ "$LOG_COLOR" == "True" ] && [ "$SYSLOG" == "False" ]; then
        setup_colorized_logging_searchlight $SEARCHLIGHT_CONF DEFAULT "tenant" "user"
    fi

    # Plugin config - disable designate by default since it's not typically installed
    iniset $SEARCHLIGHT_CONF resource_plugin:os_designate_zone enabled False
    iniset $SEARCHLIGHT_CONF resource_plugin:os_designate_recordset enabled False

    # Plugin config - disable ironic by default since it's not typically installed
    iniset $SEARCHLIGHT_CONF resource_plugin:os_ironic_chassis enabled False
    iniset $SEARCHLIGHT_CONF resource_plugin:os_ironic_chassis notifications_topics_exchanges ironic_versioned_notifications,ironic
    iniset $SEARCHLIGHT_CONF resource_plugin:os_ironic_node enabled False
    iniset $SEARCHLIGHT_CONF resource_plugin:os_ironic_node notifications_topics_exchanges ironic_versioned_notifications,ironic
    iniset $SEARCHLIGHT_CONF resource_plugin:os_ironic_port enabled False
    iniset $SEARCHLIGHT_CONF resource_plugin:os_ironic_port notifications_topics_exchanges ironic_versioned_notifications,ironic

    # Plugin config - enable versioned notifications for flavor
    iniset $SEARCHLIGHT_CONF resource_plugin:os_nova_flavor enabled True
    iniset $SEARCHLIGHT_CONF resource_plugin:os_nova_flavor notifications_topics_exchanges versioned_notifications,nova

    # Plugin config - disable swift by default since it's not typically installed
    iniset $SEARCHLIGHT_CONF resource_plugin:os_swift_account enabled False
    iniset $SEARCHLIGHT_CONF resource_plugin:os_swift_container enabled False
    iniset $SEARCHLIGHT_CONF resource_plugin:os_swift_object enabled False

    # uWSGI configuration
    write_uwsgi_config "$SEARCHLIGHT_UWSGI_CONF" "$SEARCHLIGHT_UWSGI_APP" "/search"
}

# create_searchlight_accounts - Set up common required searchlight accounts

# Tenant               User       Roles
# ------------------------------------------------------------------
# service              searchlight  admin        # if enabled
function create_searchlight_accounts {
    if [[ "$ENABLED_SERVICES" =~ "searchlight-" ]]; then
        create_service_user "searchlight" "admin"

        if is_service_enabled searchlight-api; then
            get_or_create_service "searchlight" "search" "Searchlight Service"
            get_or_create_endpoint "search" \
                "$REGION_NAME" \
                "$SEARCHLIGHT_SERVICE_PROTOCOL://$SEARCHLIGHT_SERVICE_HOST/search" \
                "$SEARCHLIGHT_SERVICE_PROTOCOL://$SEARCHLIGHT_SERVICE_HOST/search" \
                "$SEARCHLIGHT_SERVICE_PROTOCOL://$SEARCHLIGHT_SERVICE_HOST/search"
        fi
    fi
}

# init_searchlight - Initialize etc.
function init_searchlight {
    # Create cache dir
    sudo mkdir -p $SEARCHLIGHT_AUTH_CACHE_DIR
    sudo chown $STACK_USER $SEARCHLIGHT_AUTH_CACHE_DIR
    rm -f $SEARCHLIGHT_AUTH_CACHE_DIR/*

    _start_elasticsearch

    $SEARCHLIGHT_BIN_DIR/searchlight-manage --config-file $SEARCHLIGHT_CONF index sync --force
}

# install_searchlight - Collect source and prepare
function install_searchlight {
    git_clone $SEARCHLIGHT_REPO $SEARCHLIGHT_DIR $SEARCHLIGHT_BRANCH
    setup_develop $SEARCHLIGHT_DIR
    _download_elasticsearch
    _install_elasticsearch
    pip_install uwsgi
}

# install_searchlightclient - Collect source and prepare
function install_searchlightclient {
    git_clone $SEARCHLIGHTCLIENT_REPO $SEARCHLIGHTCLIENT_DIR $SEARCHLIGHTCLIENT_BRANCH
    setup_develop $SEARCHLIGHTCLIENT_DIR
}

# start_searchlight - Start running processes, including screen
function start_searchlight {
    if is_service_enabled searchlight-api; then
        run_process searchlight-api "$SEARCHLIGHT_BIN_DIR/uwsgi --ini $SEARCHLIGHT_UWSGI_CONF"
    fi
    if is_service_enabled searchlight-listener; then
        run_process searchlight-listener "$SEARCHLIGHT_BIN_DIR/searchlight-listener --config-file $SEARCHLIGHT_CONF"
    fi
}

# stop_searchlight - Stop running processes
function stop_searchlight {
    # Kill the searchlight screen windows
    stop_process searchlight-api
    stop_process searchlight-listener
    remove_uwsgi_config "$SEARCHLIGHT_UWSGI_CONF" "$SEARCHLIGHT_UWSGI_APP"
}


###############
# ELASTICSEARCH
# Moving this here because the devstack team has determined that only
# services supporting devstack core projects should live in devstack

function _wget_elasticsearch {
    local baseurl=${1}
    local file=${2}
    if [ ! -f ${FILES}/${file} ]; then
        wget ${baseurl}/${file} -O ${FILES}/${file}
    fi

    if [ ! -f ${FILES}/${file}.sha1 ]; then
        # Starting with 2.0.0, sha1 files dropped the .txt extension and changed
        # the format slightly; need the leading spaces to comply with sha1sum
        ( wget ${baseurl}/${file}.sha1 -O ${FILES}/${file}.sha1 &&
          echo "  ${file}" >> ${FILES}/${file}.sha1 )
    fi

    pushd ${FILES};  sha1sum ${file} > ${file}.sha1.gen;  popd

    if ! diff ${FILES}/${file}.sha1.gen ${FILES}/${file}.sha1; then
        echo "Invalid elasticsearch download. Could not install."
        return 1
    else
        echo "SHA1 for ${file} matches downloaded ${FILES}/${file}.sha1"
    fi
    return 0
}

function _download_elasticsearch {
    echo "Downloading elasticsearch"
    if is_ubuntu; then
        _wget_elasticsearch $ELASTICSEARCH_BASEURL_DEB/${ELASTICSEARCH_VERSION} elasticsearch-${ELASTICSEARCH_VERSION}.deb
    elif is_fedora; then
        _wget_elasticsearch $ELASTICSEARCH_BASEURL_RPM/${ELASTICSEARCH_VERSION} elasticsearch-${ELASTICSEARCH_VERSION}.rpm
    fi
}

function _check_elasticsearch_ready {
    # poll elasticsearch to see if it's started
    if ! wait_for_service 30 http://localhost:9200; then
        die $LINENO "Maximum timeout reached. Could not connect to ElasticSearch"
    fi
}

function _start_elasticsearch {
    echo "Starting elasticsearch"
    if is_ubuntu; then
        sudo /etc/init.d/elasticsearch start
        _check_elasticsearch_ready
    elif is_fedora; then
        sudo /bin/systemctl start elasticsearch.service
        _check_elasticsearch_ready
    else
        echo "Unsupported architecture...can not start elasticsearch."
    fi
}

function _stop_elasticsearch {
    echo "Stopping elasticsearch"
    if is_ubuntu; then
        sudo /etc/init.d/elasticsearch stop
    elif is_fedora; then
        sudo /bin/systemctl stop elasticsearch.service
    else
        echo "Unsupported architecture...can not stop elasticsearch."
    fi
}

function _install_elasticsearch {
    echo "Installing elasticsearch"
    pip_install_gr elasticsearch
    if is_package_installed elasticsearch; then
        echo "Note: elasticsearch was already installed."
        return
    fi
    if is_ubuntu; then
        is_package_installed default-jdk-headless || install_package default-jdk-headless

        sudo dpkg -i ${FILES}/elasticsearch-${ELASTICSEARCH_VERSION}.deb
        sudo update-rc.d elasticsearch defaults 95 10
    elif is_fedora; then
        is_package_installed java-1.8.0-openjdk-headless || install_package java-1.8.0-openjdk-headless
        yum_install ${FILES}/elasticsearch-${ELASTICSEARCH_VERSION}.rpm
        sudo /bin/systemctl daemon-reload
        sudo /bin/systemctl enable elasticsearch.service
    else
        echo "Unsupported install of elasticsearch on this architecture."
    fi
}

function _uninstall_elasticsearch {
    echo "Uninstalling elasticsearch"
    if is_package_installed elasticsearch; then
        if is_ubuntu; then
            sudo apt-get purge elasticsearch
        elif is_fedora; then
            sudo yum remove elasticsearch
        else
            echo "Unsupported install of elasticsearch on this architecture."
        fi
    fi
}

#
# END OF ELASTICSEARCH
######################


# check for service enabled
if is_service_enabled searchlight; then

    if [[ "$1" == "source" ]]; then
        # Initial source of lib script
        source $TOP_DIR/lib/searchlight
    fi

    if [[ "$1" == "stack" && "$2" == "install" ]]; then
        echo_summary "Installing Searchlight"
        install_searchlight

        echo_summary "Installing Searchlight client"
        install_searchlightclient
    elif [[ "$1" == "stack" && "$2" == "post-config" ]]; then
        echo_summary "Configuring Searchlight"
        configure_searchlight

        if is_service_enabled key; then
            echo_summary "Creating Searchlight Keystone Accounts"
            create_searchlight_accounts
        fi

    elif [[ "$1" == "stack" && "$2" == "extra" ]]; then
        echo_summary "Initializing Searchlight"
        init_searchlight

        echo_summary "Starting Searchlight"
        start_searchlight
    fi

    if [[ "$1" == "unstack" ]]; then
        stop_searchlight
    fi

    if [[ "$1" == "clean" ]]; then
        echo_summary "Cleaning Searchlight"
        cleanup_searchlight
    fi
fi

# Restore xtrace
$XTRACE