"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/devstack/plugin.sh" (13 May 2020, 48514 Bytes) of package /linux/misc/openstack/monasca-api-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "plugin.sh": 3.1.0_vs_4.0.0.

    1 #
    2 # (C) Copyright 2015-2017 Hewlett Packard Enterprise Development LP
    3 # Copyright 2017 FUJITSU LIMITED
    4 # (C) Copyright 2017 SUSE LLC
    5 #
    6 # Licensed under the Apache License, Version 2.0 (the "License");
    7 # you may not use this file except in compliance with the License.
    8 # You may obtain a copy of the License at
    9 #
   10 #    http://www.apache.org/licenses/LICENSE-2.0
   11 #
   12 # Unless required by applicable law or agreed to in writing, software
   13 # distributed under the License is distributed on an "AS IS" BASIS,
   14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   15 # implied.
   16 # See the License for the specific language governing permissions and
   17 # limitations under the License.
   18 #
   19 
   20 # Monasca DevStack plugin
   21 #
   22 # Install and start Monasca service in devstack
   23 #
   24 # To enable Monasca in devstack add an entry to local.conf that
   25 # looks like
   26 #
   27 # [[local|localrc]]
   28 # enable_plugin monasca https://git.openstack.org/openstack/monasca-api
   29 #
   30 # By default all Monasca services are started (see
   31 # devstack/settings). To disable a specific service use the
   32 # disable_service function. For example to turn off notification:
   33 #
   34 # disable_service monasca-notification
   35 #
   36 # Several variables set in the localrc section adjust common behaviors
   37 # of Monasca (see within for additional settings):
   38 #
   39 # EXAMPLE VARS HERE
   40 
   41 # Save trace setting
   42 XTRACE=$(set +o | grep xtrace)
   43 set -o xtrace
   44 
   45 ERREXIT=$(set +o | grep errexit)
   46 set -o errexit
   47 
   48 # source lib/*
   49 source ${MONASCA_API_DIR}/devstack/lib/constants.sh
   50 source ${MONASCA_API_DIR}/devstack/lib/zookeeper.sh
   51 source ${MONASCA_API_DIR}/devstack/lib/ui.sh
   52 source ${MONASCA_API_DIR}/devstack/lib/notification.sh
   53 source ${MONASCA_API_DIR}/devstack/lib/profile.sh
   54 source ${MONASCA_API_DIR}/devstack/lib/client.sh
   55 source ${MONASCA_API_DIR}/devstack/lib/persister.sh
   56 source ${MONASCA_API_DIR}/devstack/lib/storm.sh
   57 source ${MONASCA_API_DIR}/devstack/lib/monasca-log.sh
   58 # source lib/*
   59 
   60 # Set default implementations to python
   61 export MONASCA_API_IMPLEMENTATION_LANG=${MONASCA_API_IMPLEMENTATION_LANG:-python}
   62 
   63 # Set default persistent layer settings
   64 export MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-influxdb}
   65 # Make sure we use ORM mapping as default if postgresql is enabled
   66 if is_service_enabled mysql; then
   67     MONASCA_DATABASE_USE_ORM=${MONASCA_DATABASE_USE_ORM:-false}
   68 elif is_service_enabled postgresql; then
   69     MONASCA_DATABASE_USE_ORM=true
   70 fi
   71 MONASCA_DATABASE_USE_ORM=$(trueorfalse False MONASCA_DATABASE_USE_ORM)
   72 
   73 # Set INFLUXDB_VERSION
   74 if [[ "${MONASCA_API_IMPLEMENTATION_LANG,,}" == 'java' ]]; then
   75 
   76     INFLUXDB_VERSION=${INFLUXDB_VERSION:-${INFLUXDB_JAVA_VERSION}}
   77 
   78 elif [[ "${MONASCA_API_IMPLEMENTATION_LANG,,}" == 'python' ]]; then
   79 
   80     INFLUXDB_VERSION=${INFLUXDB_VERSION:-${INFLUXDB_PYTHON_VERSION}}
   81 
   82 else
   83 
   84     echo "Found invalid value for variable MONASCA_API_IMPLEMENTATION_LANG: $MONASCA_API_IMPLEMENTATION_LANG"
   85     echo "Valid values for MONASCA_API_IMPLEMENTATION_LANG are \"java\" and \"python\""
   86     die "Please set MONASCA_API_IMPLEMENTATION_LANG to either \"java'' or \"python\""
   87 
   88 fi
   89 
   90 # monasca-api settings
   91 if [[ ${USE_VENV} = True ]]; then
   92     PROJECT_VENV["monasca-api"]=${MONASCA_API_DIR}.venv
   93     MONASCA_API_BIN_DIR=${PROJECT_VENV["monasca-api"]}/bin
   94 else
   95     MONASCA_API_BIN_DIR=$(get_python_exec_prefix)
   96 fi
   97 
   98 if [[ "${MONASCA_API_USE_MOD_WSGI}" == 'True' && "${MONASCA_API_IMPLEMENTATION_LANG}" == "python" ]]; then
   99     MONASCA_API_BASE_URI=${MONASCA_API_SERVICE_PROTOCOL}://${MONASCA_API_SERVICE_HOST}/metrics
  100 else
  101     MONASCA_API_BASE_URI=${MONASCA_API_SERVICE_PROTOCOL}://${MONASCA_API_SERVICE_HOST}:${MONASCA_API_SERVICE_PORT}
  102 fi
  103 
  104 
  105 MONASCA_API_URI_V2=${MONASCA_API_BASE_URI}/v2.0
  106 
  107 # Files inside this directory will be visible in gates log
  108 MON_API_GATE_CONFIGURATION_DIR=/etc/monasca-api
  109 
  110 function pre_install_monasca {
  111     echo_summary "Pre-Installing Monasca Components"
  112     find_nearest_apache_mirror
  113     install_gate_config_holder
  114     configure_system_encoding_format
  115     install_kafka
  116     install_zookeeper
  117     install_storm
  118 
  119     install_monasca_virtual_env
  120     install_monasca_$MONASCA_METRICS_DB
  121 
  122     pre_monasca-persister
  123 }
  124 
  125 function install_monasca {
  126 
  127     echo_summary "Installing Monasca"
  128 
  129     install_monasca_common_java
  130     if is_service_enabled monasca-persister; then
  131         stack_install_service monasca-persister
  132     fi
  133     if is_service_enabled monasca-notification; then
  134         stack_install_service monasca-notification
  135     fi
  136 
  137     if is_service_enabled monasca-thresh; then
  138         if ! is_storm_enabled; then
  139             die "monasca-thresh requires monasca-storm service to be enabled"
  140         fi
  141         install_monasca_thresh
  142     fi
  143 
  144     if is_service_enabled monasca-api; then
  145         if [ "$MONASCA_API_IMPLEMENTATION_LANG" == "python" ]; then
  146             stack_install_service monasca-api
  147         else
  148             install_monasca_api_java
  149             sudo systemctl enable monasca-api
  150         fi
  151     fi
  152 
  153     install_ui
  154 }
  155 
  156 function configure_monasca {
  157     echo_summary "Configuring Monasca"
  158 
  159     configure_storm
  160     configure_ui
  161     configure_monasca_api
  162     configure_monasca-notification
  163     configure_monasca-persister
  164     install_schema
  165 }
  166 
  167 function configure_system_encoding_format {
  168     # This is needed to build monasca-common
  169     export LANGUAGE=en_US.UTF-8
  170     export LC_ALL=en_US.UTF-8
  171     export LANG=en_US.UTF-8
  172     export LC_TYPE=en_US.UTF-8
  173 }
  174 
  175 function extra_monasca {
  176     echo_summary "Installing additional monasca components"
  177 
  178     create_accounts
  179     install_monasca_agent
  180     install_monascaclient
  181     install_monasca_profile
  182 
  183     if is_service_enabled horizon; then
  184         install_nodejs
  185         install_go
  186         install_monasca_grafana
  187     fi
  188 
  189     start_monasca_services
  190     init_collector_service
  191     post_storm
  192 
  193     if is_service_enabled horizon; then
  194         init_monasca_grafana
  195     fi
  196 }
  197 
  198 function start_monasca_services {
  199     start_storm
  200     if is_service_enabled monasca-api; then
  201         start_monasca_api
  202     fi
  203     start_monasca-notification
  204     start_monasca-persister
  205     if is_service_enabled monasca-thresh; then
  206         start_service monasca-thresh || restart_service monasca-thresh
  207     fi
  208     if is_service_enabled horizon; then
  209         start_service grafana-server || restart_service grafana-server
  210     fi
  211     if is_service_enabled monasca-agent; then
  212         sudo /usr/local/bin/monasca-reconfigure
  213         if is_service_enabled nova && [ "$VIRT_DRIVER" = "libvirt" ]; then
  214             sudo /opt/monasca-agent/bin/monasca-setup -d libvirt
  215         fi
  216     fi
  217 }
  218 
  219 function delete_kafka_topics {
  220 
  221         /opt/kafka/bin/kafka-topics.sh --delete --zookeeper localhost:2181 \
  222                 --topic metrics || true
  223         /opt/kafka/bin/kafka-topics.sh --delete --zookeeper localhost:2181 \
  224                 --topic events || true
  225         /opt/kafka/bin/kafka-topics.sh --delete --zookeeper localhost:2181 \
  226                 --topic alarm-state-transitions || true
  227         /opt/kafka/bin/kafka-topics.sh --delete --zookeeper localhost:2181 \
  228                 --topic alarm-notifications || true
  229         /opt/kafka/bin/kafka-topics.sh --delete --zookeeper localhost:2181 \
  230                 --topic retry-notifications || true
  231         /opt/kafka/bin/kafka-topics.sh --delete --zookeeper localhost:2181 \
  232                 --topic 60-seconds-notifications || true
  233 }
  234 
  235 function unstack_monasca {
  236     stop_service grafana-server || true
  237 
  238     [[ -f /etc/systemd/system/monasca-agent.target ]] && stop_service monasca-agent.target || true
  239 
  240     stop_service monasca-thresh || true
  241 
  242     stop_storm
  243     stop_monasca-notification
  244     stop_monasca-persister
  245     stop_monasca_api
  246 
  247     delete_kafka_topics
  248     stop_service kafka || true
  249 
  250     stop_service influxdb || true
  251 
  252     stop_service verticad || true
  253 
  254     stop_service vertica_agent || true
  255 
  256     stop_service cassandra || true
  257 }
  258 
  259 function clean_monasca {
  260 
  261     set +o errexit
  262 
  263     unstack_monasca
  264     clean_ui
  265 
  266     if is_service_enabled horizon; then
  267         clean_nodejs
  268         clean_monasca_grafana
  269         clean_go
  270     fi
  271 
  272     if is_service_enabled monasca-agent; then
  273         clean_monasca_agent
  274     fi
  275     if is_service_enabled monasca-thresh; then
  276         clean_monasca_thresh
  277     fi
  278     clean_storm
  279     if is_service_enabled monasca-api; then
  280         clean_monasca_api_$MONASCA_API_IMPLEMENTATION_LANG
  281     fi
  282 
  283     clean_monasca-persister
  284     clean_monasca-notification
  285     clean_monasca_common_java
  286 
  287     clean_schema
  288 
  289     clean_monasca_profile
  290     clean_monascaclient
  291 
  292     clean_monasca_$MONASCA_METRICS_DB
  293 
  294     clean_kafka
  295 
  296     clean_zookeeper
  297 
  298     clean_monasca_virtual_env
  299 
  300     #Restore errexit
  301     set -o errexit
  302 }
  303 
  304 function install_monasca_virtual_env {
  305 
  306     echo_summary "Install Monasca Virtual Environment"
  307 
  308     sudo groupadd --system monasca || true
  309 
  310     sudo mkdir -p /opt/monasca || true
  311 
  312     sudo chown $STACK_USER:monasca /opt/monasca
  313 
  314     (cd /opt/monasca ; virtualenv .)
  315 }
  316 
  317 function clean_monasca_virtual_env {
  318 
  319     echo_summary "Clean Monasca Virtual Environment"
  320 
  321     sudo rm -rf /opt/monasca
  322 
  323     sudo groupdel monasca
  324 
  325 }
  326 
  327 function install_kafka {
  328 
  329     echo_summary "Install Monasca Kafka"
  330 
  331     local kafka_tarball=kafka_${KAFKA_VERSION}.tgz
  332     local kafka_tarball_url=${APACHE_ARCHIVES}kafka/${BASE_KAFKA_VERSION}/${kafka_tarball}
  333 
  334     local kafka_tarball_dest
  335     kafka_tarball_dest=`get_extra_file ${kafka_tarball_url}`
  336 
  337     sudo groupadd --system kafka || true
  338 
  339     sudo useradd --system -g kafka kafka || true
  340 
  341     sudo tar -xzf ${kafka_tarball_dest} -C /opt
  342 
  343     sudo ln -sf /opt/kafka_${KAFKA_VERSION} /opt/kafka
  344 
  345     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/kafka/kafka-server-start.sh /opt/kafka_${KAFKA_VERSION}/bin/kafka-server-start.sh
  346 
  347     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/kafka/kafka.service /etc/systemd/system/kafka.service
  348 
  349     sudo chown root:root /etc/systemd/system/kafka.service
  350 
  351     sudo chmod 644 /etc/systemd/system/kafka.service
  352 
  353     sudo mkdir -p /var/kafka || true
  354 
  355     sudo chown kafka:kafka /var/kafka
  356 
  357     sudo chmod 755 /var/kafka
  358 
  359     sudo rm -rf /var/kafka/lost+found
  360 
  361     sudo mkdir -p /var/log/kafka || true
  362 
  363     sudo chown kafka:kafka /var/log/kafka
  364 
  365     sudo chmod 755 /var/log/kafka
  366 
  367     sudo ln -sf /opt/kafka/config /etc/kafka
  368 
  369     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/kafka/server.properties /etc/kafka/server.properties
  370 
  371     sudo chown kafka:kafka /etc/kafka/server.properties
  372 
  373     sudo chmod 644 /etc/kafka/server.properties
  374 
  375     # set kafka listeners address.
  376     sudo sed -i "s/listeners = PLAINTEXT:\/\/your.host.name:9092/listeners = PLAINTEXT:\/\/your.host.name:9092\nlisteners=PLAINTEXT:\/\/${SERVICE_HOST}:9092/"\
  377         /etc/kafka/server.properties
  378 
  379     sudo systemctl enable kafka
  380 
  381     sudo systemctl start kafka || sudo systemctl restart kafka
  382 
  383 }
  384 
  385 function clean_kafka {
  386 
  387     echo_summary "Clean Monasca Kafka"
  388 
  389     sudo rm -rf /var/kafka
  390 
  391     sudo rm -rf /var/log/kafka
  392 
  393     sudo rm -rf /etc/kafka
  394 
  395     sudo rm -rf /opt/kafka
  396 
  397     sudo systemctl disable kafka
  398 
  399     sudo rm -rf /etc/systemd/system/kafka.service
  400 
  401     sudo userdel kafka
  402 
  403     sudo groupdel kafka
  404 
  405     sudo rm -rf /opt/kafka_${KAFKA_VERSION}
  406 
  407     sudo rm -rf ${FILES}/kafka_${KAFKA_VERSION}.tgz
  408 
  409 }
  410 
  411 function install_monasca_influxdb {
  412 
  413     if is_service_enabled monasca-persister; then
  414         echo_summary "Install Monasca Influxdb"
  415 
  416         local influxdb_deb=influxdb_${INFLUXDB_VERSION}_amd64.deb
  417         local influxdb_deb_url=https://dl.influxdata.com/influxdb/releases/${influxdb_deb}
  418 
  419         local influxdb_deb_dest
  420         influxdb_deb_dest=`get_extra_file ${influxdb_deb_url}`
  421 
  422         sudo dpkg --skip-same-version -i ${influxdb_deb_dest}
  423 
  424         # Validate INFLUXDB_VERSION
  425         validate_version ${INFLUXDB_VERSION}
  426 
  427         if [[ $? -ne 0 ]]; then
  428             echo "Found invalid value for variable INFLUXDB_VERSION: $INFLUXDB_VERSION"
  429             echo "Valid values for INFLUXDB_VERSION must be in the form of 1.0.0"
  430             die "Please set INFLUXDB_VERSION to a correct value"
  431         fi
  432 
  433         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/influxdb/influxdb.conf /etc/influxdb/influxdb.conf
  434 
  435         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/influxdb/influxdb /etc/default/influxdb
  436 
  437         sudo systemctl start influxdb || sudo systemctl restart influxdb
  438     fi
  439 
  440 }
  441 
  442 function install_monasca_vertica {
  443 
  444     echo_summary "Install Monasca Vertica"
  445 
  446     apt_get install dialog
  447 
  448     sudo dpkg --skip-same-version -i /vagrant_home/vertica_${VERTICA_VERSION}_amd64.deb
  449 
  450     # Download Vertica JDBC driver
  451     # local vertica_jar=vertica-jdbc-${VERTICA_VERSION}.jar
  452     # local vertica_jar_url=https://my.vertica.com/client_drivers/7.2.x/${VERTICA_VERSION}/${vertica_jar}
  453 
  454     # local vertica_jar_dest
  455     # vertica_jar_dest=`get_extra_file ${vertica_jar_url}`
  456 
  457     # Current version of Vertica 8.0.0 doesn't support Ubuntu Xenial, so fake a version
  458     sudo cp -p /etc/debian_version /etc/debian_version.org
  459     sudo sh -c "echo 'jessie/sid' > /etc/debian_version"
  460 
  461     sudo /opt/vertica/sbin/install_vertica --hosts "127.0.0.1" --deb /vagrant_home/vertica_${VERTICA_VERSION}_amd64.deb --dba-user-password password --license CE --accept-eula --failure-threshold NONE
  462 
  463     sudo su dbadmin -c '/opt/vertica/bin/admintools -t create_db -s "127.0.0.1" -d mon -p password'
  464 
  465     # Bring back Ubuntu version
  466     sudo mv /etc/debian_version.org /etc/debian_version
  467 
  468     # Copy Vertica JDBC driver to /opt/monasca
  469     # sudo cp ${FILES}/vertica-jdbc-${VERTICA_VERSION}.jar /opt/monasca/vertica-jdbc-${VERTICA_VERSION}.jar
  470     sudo cp /vagrant_home/vertica-jdbc-${VERTICA_VERSION}.jar /opt/monasca/vertica-jdbc-${VERTICA_VERSION}.jar
  471 
  472 }
  473 
  474 function install_monasca_cassandra {
  475 
  476     if is_service_enabled monasca-persister; then
  477         echo_summary "Install Monasca Cassandra"
  478 
  479         if [[ "$OFFLINE" != "True" ]]; then
  480             sudo sh -c "echo 'deb http://www.apache.org/dist/cassandra/debian ${CASSANDRA_VERSION} main' > /etc/apt/sources.list.d/cassandra.sources.list"
  481             REPOS_UPDATED=False
  482             curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
  483             PUBLIC_KEY=`sudo apt_get update 2>&1 | awk '/NO_PUBKEY/ {print $NF}'`
  484             if [ -n "${PUBLIC_KEY}" ]; then
  485                 sudo apt-key adv --keyserver pool.sks-keyservers.net --recv-key  ${PUBLIC_KEY}
  486             fi
  487         fi
  488 
  489         REPOS_UPDATED=False
  490         apt_get_update
  491         apt_get install cassandra
  492 
  493         if [[ ${SERVICE_HOST} ]]; then
  494 
  495             # set cassandra server listening ip address
  496             sudo sed -i "s/^rpc_address: localhost/rpc_address: ${SERVICE_HOST}/g" /etc/cassandra/cassandra.yaml
  497 
  498         fi
  499 
  500         # set batch size larger
  501         sudo sed -i "s/^batch_size_warn_threshold_in_kb: 5/batch_size_warn_threshold_in_kb: 50/g" /etc/cassandra/cassandra.yaml
  502 
  503         sudo sed -i "s/^batch_size_fail_threshold_in_kb: 50/batch_size_fail_threshold_in_kb: 500/g" /etc/cassandra/cassandra.yaml
  504 
  505         sudo service cassandra restart
  506 
  507         echo "Sleep for 15 seconds to wait starting up Cassandra"
  508         sleep 15s
  509 
  510         export CQLSH_NO_BUNDLED=true
  511 
  512         # always needed for Monasca api
  513         pip_install_gr cassandra-driver
  514     fi
  515 }
  516 
  517 function clean_monasca_influxdb {
  518 
  519     echo_summary "Clean Monasca Influxdb"
  520 
  521     sudo rm -f /etc/default/influxdb
  522 
  523     sudo rm -f /etc/influxdb/influxdb.conf
  524 
  525     sudo dpkg --purge influxdb
  526 
  527     sudo rm -rf /var/log/influxdb
  528 
  529     sudo rm -rf /tmp/influxdb
  530 
  531     sudo rm -rf /var/lib/influxdb
  532 
  533     sudo rm -rf /etc/init.d/influxdb
  534 
  535     sudo rm -rf /opt/staging/influxdb/influxdb-package
  536 
  537     sudo rm -rf /etc/influxdb
  538 
  539     sudo rm -rf /tmp/bootstrap*
  540 
  541     sudo rm -rf /run/influxdb
  542 
  543     sudo rm -f  ${FILES}/influxdb_${INFLUXDB_VERSION}_amd64.deb
  544 
  545     sudo rm -f /etc/init.d/influxdb
  546 }
  547 
  548 function clean_monasca_vertica {
  549 
  550     echo_summary "Clean Monasca Vertica"
  551 
  552     sudo rm -rf /opt/vertica
  553 
  554     sudo dpkg --purge vertica
  555 
  556     sudo userdel dbadmin
  557 
  558     sudo groupdel verticadba
  559 
  560     sudo rm -rf /home/dbadmin
  561 
  562     apt_get purge dialog
  563 }
  564 
  565 function clean_monasca_cassandra {
  566 
  567     echo_summary "Clean Monasca Cassandra"
  568 
  569     apt_get purge cassandra
  570 
  571     apt_get autoremove
  572 
  573     sudo rm -rf /var/lib/cassandra
  574 
  575     sudo rm -rf /var/log/cassandra
  576 
  577     sudo rm -rf /etc/cassandra
  578 
  579     sudo rm -f /etc/apt/sources.list.d/cassandra.list
  580 
  581     sudo rm -f /etc/apt/trusted.gpg.d/cassandra.gpg
  582 }
  583 
  584 function install_schema {
  585     echo_summary "Install Monasca Schema"
  586 
  587     sudo mkdir -p $MONASCA_SCHEMA_DIR || true
  588     sudo chmod 0755 $MONASCA_SCHEMA_DIR
  589 
  590     install_schema_metric_database_$MONASCA_METRICS_DB
  591     install_schema_alarm_database
  592     install_schema_kafka_topics
  593 }
  594 
  595 function install_schema_metric_database_influxdb {
  596     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/schema/influxdb_setup.py $MONASCA_SCHEMA_DIR/influxdb_setup.py
  597     sudo chmod 0750 $MONASCA_SCHEMA_DIR/influxdb_setup.py
  598     sudo chown root:root $MONASCA_SCHEMA_DIR/influxdb_setup.py
  599     if python3_enabled; then
  600         sudo python3 $MONASCA_SCHEMA_DIR/influxdb_setup.py
  601     else
  602         sudo python $MONASCA_SCHEMA_DIR/influxdb_setup.py
  603     fi
  604 }
  605 
  606 function install_schema_metric_database_vertica {
  607     /opt/vertica/bin/vsql -U dbadmin -w password < "${MONASCA_API_DIR}"/devstack/files/vertica/mon_metrics.sql
  608     /opt/vertica/bin/vsql -U dbadmin -w password < "${MONASCA_API_DIR}"/devstack/files/vertica/mon_alarms.sql
  609     /opt/vertica/bin/vsql -U dbadmin -w password < "${MONASCA_API_DIR}"/devstack/files/vertica/roles.sql
  610     /opt/vertica/bin/vsql -U dbadmin -w password < "${MONASCA_API_DIR}"/devstack/files/vertica/users.sql
  611 }
  612 
  613 function install_schema_metric_database_cassandra {
  614     if is_service_enabled monasca-persister; then
  615         local CASSANDRA_CONNECT_TIMEOUT=300
  616         local CASSANDRA_REQUEST_TIMEOUT=300
  617         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/cassandra/*.cql $MONASCA_SCHEMA_DIR
  618         /usr/bin/cqlsh ${SERVICE_HOST} -f $MONASCA_SCHEMA_DIR/monasca_schema.cql \
  619             --connect-timeout="${CASSANDRA_CONNECT_TIMEOUT}" \
  620             --request-timeout="${CASSANDRA_REQUEST_TIMEOUT}"
  621     fi
  622 }
  623 
  624 function install_schema_kafka_topics {
  625     sudo mkdir -p /opt/kafka/logs || true
  626     sudo chown kafka:kafka /opt/kafka/logs
  627     sudo chmod 0766 /opt/kafka/logs
  628     # Right number of partition is crucial for performance optimization, 
  629     # in high load(real world) deployment this number should be increased.  
  630     /opt/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  631         --replication-factor 1 --partitions 3 --topic metrics
  632     /opt/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  633         --replication-factor 1 --partitions 2 --topic events
  634     /opt/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  635         --replication-factor 1 --partitions 2 --topic alarm-state-transitions
  636     /opt/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  637         --replication-factor 1 --partitions 2 --topic alarm-notifications
  638     /opt/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  639         --replication-factor 1 --partitions 2 --topic retry-notifications
  640     /opt/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  641         --replication-factor 1 --partitions 2 --topic 60-seconds-notifications
  642 }
  643 
  644 function install_schema_alarm_database {
  645     local databaseName="mon"
  646 
  647     if is_service_enabled mysql postgresql; then
  648         recreate_database $databaseName
  649         $MONASCA_API_BIN_DIR/monasca_db upgrade
  650     fi
  651 }
  652 
  653 function clean_schema {
  654 
  655     echo_summary "Clean Monasca Schema"
  656 
  657     if is_service_enabled mysql; then
  658         sudo echo "drop database mon;" | mysql -u$DATABASE_USER -p$DATABASE_PASSWORD
  659     elif is_service_enabled postgresql; then
  660         sudo -u postgres psql -c "DROP DATABASE mon;"
  661     fi
  662 
  663     sudo rm -rf $MONASCA_SCHEMA_DIR
  664 
  665 }
  666 
  667 function install_monasca_common_java {
  668     echo_summary "Install monasca_common Java"
  669 
  670     git_clone $MONASCA_COMMON_REPO $MONASCA_COMMON_DIR $MONASCA_COMMON_BRANCH
  671     (cd "${MONASCA_COMMON_DIR}"/java ; sudo mvn clean install -DskipTests)
  672 }
  673 
  674 function clean_monasca_common_java {
  675     echo_summary "Clean Monasca monasca_common"
  676 
  677     (cd "${MONASCA_COMMON_DIR}" ; sudo mvn clean)
  678 }
  679 
  680 function install_monasca_api_java {
  681 
  682     echo_summary "Install Monasca monasca_api_java"
  683 
  684     (cd "${MONASCA_API_DIR}"/java ; sudo mvn clean package -DskipTests)
  685 
  686     local version=""
  687     version="$(get_version_from_pom "${MONASCA_API_DIR}"/java)"
  688 
  689     sudo cp -f "${MONASCA_API_DIR}"/java/target/monasca-api-${version}-shaded.jar \
  690         /opt/monasca/monasca-api.jar
  691 
  692     sudo useradd --system -g monasca mon-api || true
  693 
  694     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-api/monasca-api.service /etc/systemd/system/monasca-api.service
  695 
  696     if [[ "${MONASCA_METRICS_DB,,}" == 'vertica' ]]; then
  697 
  698         # Add the Vertica JDBC to the class path.
  699         sudo sed -i "s/-cp \/opt\/monasca\/monasca-api.jar/-cp \/opt\/monasca\/monasca-api.jar:\/opt\/monasca\/vertica-jdbc-${VERTICA_VERSION}.jar/g" /etc/systemd/system/monasca-api.service
  700 
  701         sudo sed -i "s/influxdb.service/vertica.service/g" /etc/systemd/system/monasca-api.service
  702 
  703     fi
  704 
  705     sudo chown root:root /etc/systemd/system/monasca-api.service
  706 
  707     sudo chmod 0644 /etc/systemd/system/monasca-api.service
  708 
  709     sudo mkdir -p /var/log/monasca || true
  710 
  711     sudo chown root:monasca /var/log/monasca
  712 
  713     sudo chmod 0755 /var/log/monasca
  714 
  715     sudo mkdir -p /var/log/monasca/api || true
  716 
  717     sudo chown root:monasca /var/log/monasca/api
  718 
  719     sudo chmod 0775 /var/log/monasca/api
  720 
  721     sudo mkdir -p /etc/monasca || true
  722 
  723     sudo chown root:monasca /etc/monasca
  724 
  725     sudo chmod 0775 /etc/monasca
  726 
  727     local dbEngine="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
  728     local dbPort=3306
  729 
  730     if [[ ${MONASCA_DATABASE_USE_ORM} == "True" ]]; then
  731         if is_service_enabled postgresql; then
  732             dbEngine="org.postgresql.ds.PGPoolingDataSource"
  733             dbPort=5432
  734         fi
  735     fi
  736 
  737     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-api/api-config.yml /etc/monasca/api-config.yml
  738     sudo chown mon-api:root /etc/monasca/api-config.yml
  739     sudo chmod 0640 /etc/monasca/api-config.yml
  740 
  741     sudo sed -e "
  742         s|%KAFKA_HOST%|$SERVICE_HOST|g;
  743         s|%MONASCA_DATABASE_USE_ORM%|$MONASCA_DATABASE_USE_ORM|g;
  744         s|%MONASCA_API_DATABASE_ENGINE%|$dbEngine|g;
  745         s|%MONASCA_API_SERVICE_HOST%|$MONASCA_API_SERVICE_HOST|g;
  746         s|%MONASCA_API_SERVICE_PORT%|$MONASCA_API_SERVICE_PORT|g;
  747         s|%MONASCA_API_ADMIN_PORT%|$MONASCA_API_ADMIN_PORT|g;
  748         s|%DATABASE_USER%|$DATABASE_USER|g;
  749         s|%DATABASE_HOST%|$DATABASE_HOST|g;
  750         s|%DATABASE_PORT%|$dbPort|g;
  751         s|%MYSQL_HOST%|$MYSQL_HOST|g;
  752         s|%MYSQL_PORT%|$dbPort|g;
  753         s|%DATABASE_PASSWORD%|$DATABASE_PASSWORD|g;
  754         s|%MONASCA_METRICS_DB%|$MONASCA_METRICS_DB|g;
  755         s|%INFLUXDB_HOST%|$SERVICE_HOST|g;
  756         s|%INFLUXDB_PORT%|8086|g;
  757         s|%VERTICA_HOST%|$SERVICE_HOST|g;
  758         s|%ADMIN_PASSWORD%|$ADMIN_PASSWORD|g;
  759         s|%KEYSTONE_SERVICE_PORT%|$KEYSTONE_SERVICE_PORT|g;
  760         s|%KEYSTONE_SERVICE_HOST%|$KEYSTONE_SERVICE_HOST|g;
  761     " -i /etc/monasca/api-config.yml
  762 
  763 }
  764 function install_monasca-api {
  765     echo_summary "Install Monasca monasca_api "
  766 
  767     git_clone $MONASCA_API_REPO $MONASCA_API_DIR $MONASCA_API_BRANCH
  768 
  769     if python3_enabled; then
  770         enable_python3_package monasca-api
  771     fi
  772     setup_develop $MONASCA_API_DIR
  773 
  774     install_monasca_common
  775 
  776     if [[ "${MONASCA_API_USE_MOD_WSGI}" == 'True' ]]; then
  777         pip_install uwsgi
  778     else
  779         pip_install_gr gunicorn
  780     fi
  781 
  782     if [[ "${MONASCA_METRICS_DB,,}" == 'influxdb' ]]; then
  783         pip_install_gr influxdb
  784     fi
  785     if [[ "${MONASCA_METRICS_DB,,}" == 'cassandra' ]]; then
  786         pip_install_gr cassandra-driver
  787     fi
  788     if is_service_enabled postgresql; then
  789         apt_get install libpq-dev
  790         pip_install_gr psycopg2
  791     elif is_service_enabled mysql; then
  792         apt_get install libmysqlclient-dev
  793         pip_install_gr PyMySQL
  794     fi
  795 
  796 }
  797 
  798 function configure_monasca_api_python {
  799     if is_service_enabled monasca-api; then
  800         echo_summary "Configuring monasca-api python"
  801         sudo install -d -o $STACK_USER $MONASCA_API_CONF_DIR
  802 
  803         sudo mkdir -p /var/log/monasca || true
  804 
  805         sudo chown $STACK_USER:monasca /var/log/monasca
  806 
  807         sudo chmod 0755 /var/log/monasca
  808 
  809         sudo mkdir -p /var/log/monasca/api || true
  810 
  811         sudo chown $STACK_USER:monasca /var/log/monasca/api
  812 
  813         sudo chmod 0775 /var/log/monasca/api
  814 
  815         # create configuration files in target locations
  816         rm -rf $MONASCA_API_CONF $MONASCA_API_PASTE_INI $MONASCA_API_LOGGING_CONF
  817         $MONASCA_API_BIN_DIR/oslo-config-generator \
  818             --config-file $MONASCA_API_DIR/config-generator/monasca-api.conf \
  819             --output-file /tmp/monasca-api.conf
  820 
  821         install -m 600 /tmp/monasca-api.conf $MONASCA_API_CONF && rm -rf /tmp/monasca-api.conf
  822         install -m 600 $MONASCA_API_DIR/etc/api-logging.conf $MONASCA_API_LOGGING_CONF
  823         install -m 600 $MONASCA_API_DIR/etc/api-config.ini $MONASCA_API_PASTE_INI
  824         # create configuration files in target locations
  825 
  826         local dbAlarmUrl
  827         local dbMetricDriver
  828         if [[ "${MONASCA_METRICS_DB,,}" == 'cassandra' ]]; then
  829             dbMetricDriver="monasca_api.common.repositories.cassandra.metrics_repository:MetricsRepository"
  830         else
  831             dbMetricDriver="monasca_api.common.repositories.influxdb.metrics_repository:MetricsRepository"
  832         fi
  833         dbAlarmUrl=`database_connection_url mon`
  834 
  835         # default settings
  836         iniset "$MONASCA_API_CONF" DEFAULT region $REGION_NAME
  837         iniset "$MONASCA_API_CONF" DEFAULT log_config_append $MONASCA_API_LOGGING_CONF
  838         if $USE_OLD_LOG_API = true; then
  839             iniset "$MONASCA_API_CONF" DEFAULT enable_logs_api false
  840         else
  841             if is_service_enabled monasca-log; then
  842                 iniset "$MONASCA_API_CONF" DEFAULT enable_logs_api true
  843             else
  844                 iniset "$MONASCA_API_CONF" DEFAULT enable_logs_api false
  845             fi
  846         fi
  847 
  848         # logging
  849         iniset "$MONASCA_API_LOGGING_CONF" handler_file args "('$MONASCA_API_LOG_DIR/monasca-api.log', 'a', 104857600, 5)"
  850 
  851         # messaging
  852         iniset "$MONASCA_API_CONF" messaging driver "monasca_api.common.messaging.kafka_publisher:KafkaPublisher"
  853         iniset "$MONASCA_API_CONF" kafka uri "$SERVICE_HOST:9092"
  854         iniset "$MONASCA_API_CONF" kafka legacy_kafka_client_enabled false
  855 
  856         # databases
  857         iniset "$MONASCA_API_CONF" database connection $dbAlarmUrl
  858         iniset "$MONASCA_API_CONF" repositories metrics_driver $dbMetricDriver
  859         iniset "$MONASCA_API_CONF" cassandra contact_points $(ipv6_unquote $SERVICE_HOST)
  860         iniset "$MONASCA_API_CONF" influxdb ip_address $(ipv6_unquote $SERVICE_HOST)
  861         iniset "$MONASCA_API_CONF" influxdb port 8086
  862 
  863         # keystone & security
  864         configure_auth_token_middleware $MONASCA_API_CONF "admin"
  865         iniset "$MONASCA_API_CONF" keystone_authtoken region_name $REGION_NAME
  866         iniset "$MONASCA_API_CONF" keystone_authtoken project_name "admin"
  867         iniset "$MONASCA_API_CONF" keystone_authtoken password $ADMIN_PASSWORD
  868 
  869         iniset "$MONASCA_API_CONF" security default_authorized_roles "monasca-user"
  870         iniset "$MONASCA_API_CONF" security agent_authorized_roles "monasca-agent"
  871         iniset "$MONASCA_API_CONF" security read_only_authorized_roles "monasca-read-only-user"
  872         iniset "$MONASCA_API_CONF" security delegate_authorized_roles "monasca-agent"
  873 
  874         # server setup
  875         iniset "$MONASCA_API_PASTE_INI" server:main host $MONASCA_API_SERVICE_HOST
  876         iniset "$MONASCA_API_PASTE_INI" server:main port $MONASCA_API_SERVICE_PORT
  877         iniset "$MONASCA_API_PASTE_INI" server:main workers $API_WORKERS
  878 
  879         # link configuration for the gate
  880         ln -sf $MONASCA_API_CONF $MON_API_GATE_CONFIGURATION_DIR
  881         ln -sf $MONASCA_API_PASTE_INI $MON_API_GATE_CONFIGURATION_DIR
  882         ln -sf $MONASCA_API_LOGGING_CONF $MON_API_GATE_CONFIGURATION_DIR
  883 
  884         if [ "${MONASCA_API_USE_MOD_WSGI}" == 'True' ]; then
  885             configure_monasca_api_python_uwsgi
  886         fi
  887 
  888     fi
  889 }
  890 
  891 function configure_monasca_api_python_uwsgi {
  892     rm -rf $MONASCA_API_UWSGI_CONF
  893 
  894     install -m 600 $MONASCA_API_DIR/etc/api-uwsgi.ini $MONASCA_API_UWSGI_CONF
  895     write_uwsgi_config "$MONASCA_API_UWSGI_CONF" "$MONASCA_API_BIN_DIR/monasca-api-wsgi" "/metrics"
  896 }
  897 
  898 function start_monasca_api_python {
  899     if is_service_enabled monasca-api; then
  900         echo_summary "Starting monasca-api"
  901 
  902         local service_port=$MONASCA_API_SERVICE_PORT
  903         local service_protocol=$MONASCA_API_SERVICE_PROTOCOL
  904         local gunicorn="$MONASCA_API_BIN_DIR/gunicorn"
  905 
  906         restart_service memcached
  907         if [ "${MONASCA_API_USE_MOD_WSGI}" == 'True' ]; then
  908             service_uri=$service_protocol://$MONASCA_API_SERVICE_HOST/api/v2.0
  909             run_process "monasca-api" "$MONASCA_API_BIN_DIR/uwsgi --ini $MONASCA_API_UWSGI_CONF" ""
  910         else
  911             service_uri=$service_protocol://$MONASCA_API_SERVICE_HOST:$service_port
  912             run_process "monasca-api" "$gunicorn --paste $MONASCA_API_PASTE_INI"
  913         fi
  914 
  915         echo "Waiting for monasca-api to start..."
  916         if ! wait_for_service $SERVICE_TIMEOUT $service_uri; then
  917             die $LINENO "monasca-api did not start"
  918         fi
  919     fi
  920 }
  921 
  922 function stop_monasca_api_python {
  923     if is_service_enabled monasca-api; then
  924         stop_process "monasca-api" || true
  925     fi
  926 }
  927 
  928 function clean_monasca_api_java {
  929 
  930     echo_summary "Clean Monasca monasca_api_java"
  931 
  932     (cd "${MONASCA_API_DIR}" ; sudo mvn clean)
  933 
  934     sudo rm /etc/monasca/api-config.yml
  935 
  936     sudo rm -rf /var/log/monasca/api
  937 
  938     sudo systemctl disable monasca-api
  939 
  940     sudo rm /etc/systemd/system/monasca-api.service
  941 
  942     sudo rm /opt/monasca/monasca-api.jar
  943 
  944     sudo rm /var/log/upstart/monasca-api.log*
  945 
  946     sudo userdel mon-api
  947 }
  948 
  949 function clean_monasca_api_python {
  950 
  951     echo_summary "Clean Monasca monasca_api_python"
  952 
  953     sudo rm -rf /etc/monasca/monasca-api.conf
  954     sudo rm -rf /etc/monasca/api-logging.conf
  955     sudo rm -rf /etc/monasca/api-config.ini
  956     sudo rm -rf $MON_API_GATE_CONFIGURATION_DIR
  957     sudo rm -rf $MONASCA_API_LOG_DIR
  958 
  959     if is_service_enabled postgresql; then
  960         apt_get purge libpq-dev
  961     elif is_service_enabled mysql; then
  962         apt_get purge libmysqlclient-dev
  963     fi
  964 
  965     if [ "$MONASCA_API_USE_MOD_WSGI" == "True" ]; then
  966         clean_monasca_api_uwsgi
  967     fi
  968 
  969 }
  970 
  971 function clean_monasca_api_uwsgi {
  972     sudo rm -rf $MONASCA_API_UWSGI_CONF
  973 }
  974 
  975 function start_monasca_api {
  976     if [[ "${MONASCA_API_IMPLEMENTATION_LANG,,}" == 'java' ]]; then
  977         start_service monasca-api || restart_service monasca-api
  978     elif [[ "${MONASCA_API_IMPLEMENTATION_LANG,,}" == 'python' ]]; then
  979         start_monasca_api_python
  980     fi
  981 }
  982 
  983 function stop_monasca_api {
  984     if [[ "${MONASCA_API_IMPLEMENTATION_LANG,,}" == 'java' ]]; then
  985         stop_service monasca-api || true
  986     elif [[ "${MONASCA_API_IMPLEMENTATION_LANG,,}" == 'python' ]]; then
  987         stop_monasca_api_python
  988     fi
  989 }
  990 
  991 function configure_monasca_api {
  992     if [[ "${MONASCA_API_IMPLEMENTATION_LANG,,}" == 'python' ]]; then
  993         configure_monasca_api_python
  994     fi
  995     #NOTE(basiaka) Refactor of monasca-api in Java version will be handled in another change
  996 }
  997 
  998 function install_monasca_thresh {
  999 
 1000     echo_summary "Install Monasca monasca_thresh"
 1001 
 1002     git_clone $MONASCA_THRESH_REPO $MONASCA_THRESH_DIR $MONASCA_THRESH_BRANCH
 1003     (cd "${MONASCA_THRESH_DIR}"/thresh ; sudo mvn clean package -DskipTests)
 1004 
 1005     local version=""
 1006     version="$(get_version_from_pom "${MONASCA_THRESH_DIR}"/thresh)"
 1007 
 1008     sudo cp -f "${MONASCA_THRESH_DIR}"/thresh/target/monasca-thresh-${version}-shaded.jar \
 1009         /opt/monasca/monasca-thresh.jar
 1010 
 1011     sudo useradd --system -g monasca mon-thresh || true
 1012 
 1013     sudo mkdir -p /etc/monasca || true
 1014 
 1015     sudo chown root:monasca /etc/monasca
 1016 
 1017     sudo chmod 0775 /etc/monasca
 1018 
 1019     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-thresh/thresh-config.yml /etc/monasca/thresh-config.yml
 1020 
 1021     sudo chown root:monasca /etc/monasca/thresh-config.yml
 1022 
 1023     sudo chmod 0640 /etc/monasca/thresh-config.yml
 1024 
 1025     local dbEngine="org.drizzle.jdbc.DrizzleDriver"
 1026     local dbPort=3306
 1027 
 1028     if [[ ${MONASCA_DATABASE_USE_ORM} == "True" ]]; then
 1029         if is_service_enabled postgresql; then
 1030             dbEngine="org.postgresql.ds.PGPoolingDataSource"
 1031             dbPort=5432
 1032         fi
 1033     fi
 1034 
 1035     sudo sed -e "
 1036         s|%KAFKA_HOST%|$SERVICE_HOST|g;
 1037         s|%MONASCA_THRESH_DATABASE_ENGINE%|$dbEngine|g;
 1038         s|%DATABASE_USER%|$DATABASE_USER|g;
 1039         s|%MONASCA_DATABASE_USE_ORM%|$MONASCA_DATABASE_USE_ORM|g;
 1040         s|%DATABASE_TYPE%|$DATABASE_TYPE|g;
 1041         s|%DATABASE_HOST%|$DATABASE_HOST|g;
 1042         s|%DATABASE_PASSWORD%|$DATABASE_PASSWORD|g;
 1043         s|%DATABASE_PORT%|$dbPort|g;
 1044         s|%MONASCA_STATSD_PORT%|$MONASCA_STATSD_PORT|g;
 1045     " -i /etc/monasca/thresh-config.yml
 1046 
 1047     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-thresh/monasca-thresh /etc/init.d/monasca-thresh
 1048 
 1049     sudo chown root:root /etc/init.d/monasca-thresh
 1050 
 1051     sudo chmod 0744 /etc/init.d/monasca-thresh
 1052 
 1053     sudo systemctl enable monasca-thresh
 1054 
 1055 }
 1056 
 1057 function clean_monasca_thresh {
 1058 
 1059     echo_summary "Clean Monasca monasca_thresh"
 1060 
 1061     (cd "${MONASCA_THRESH_DIR}"/thresh ; sudo mvn clean)
 1062 
 1063     sudo systemctl disable monasca-thresh
 1064 
 1065     sudo rm /etc/init.d/monasca-thresh
 1066 
 1067     sudo rm /etc/monasca/thresh-config.yml
 1068 
 1069     sudo userdel mon-thresh || true
 1070 
 1071     sudo rm /opt/monasca/monasca-thresh.jar
 1072 
 1073 }
 1074 
 1075 function create_accounts {
 1076 
 1077     local projects=("mini-mon" "admin" "demo")
 1078     declare -A users=(
 1079         ["mini-mon"]="password"
 1080         ["monasca-agent"]="password"
 1081         ["admin"]="${ADMIN_PASSWORD}"
 1082         ["demo"]="${ADMIN_PASSWORD}"
 1083         ["monasca-read-only-user"]="password"
 1084     )
 1085     local roles=("monasca-user" "monasca-agent" "admin" "monasca-read-only-user")
 1086 
 1087     for project in "${projects[@]}"; do
 1088         get_or_create_project "${project}"
 1089     done
 1090     for user in "${!users[@]}"; do
 1091         local password
 1092         password="${users[$user]}"
 1093         get_or_create_user "${user}" "${password}"
 1094     done
 1095     for role in "${roles[@]}"; do
 1096         get_or_create_role "${role}"
 1097     done
 1098 
 1099     # create assignments
 1100     # args=> <role> <user> <project>
 1101     get_or_add_user_project_role "monasca-user" "mini-mon" "mini-mon"
 1102     get_or_add_user_project_role "monasca-user" "admin" "admin"
 1103     get_or_add_user_project_role "monasca-user" "demo" "demo"
 1104 
 1105     get_or_add_user_project_role "admin" "mini-mon" "mini-mon"
 1106 
 1107     get_or_add_user_project_role "monasca-agent" "monasca-agent" "mini-mon"
 1108 
 1109     get_or_add_user_project_role "monasca-read-only-user" "monasca-read-only-user" "mini-mon"
 1110 
 1111     # crate service
 1112     get_or_create_service "monasca" "${MONASCA_SERVICE_TYPE}" "Monasca Monitoring Service"
 1113 
 1114     # create endpoint
 1115     get_or_create_endpoint \
 1116             "monasca" \
 1117             "${REGION_NAME}" \
 1118             "${MONASCA_API_URI_V2}" \
 1119             "${MONASCA_API_URI_V2}" \
 1120             "${MONASCA_API_URI_V2}"
 1121 
 1122     if is_service_enabled monasca-log; then
 1123         local log_search_url="http://$KIBANA_SERVICE_HOST:$KIBANA_SERVICE_PORT/"
 1124 
 1125         get_or_create_service "logs" "logs" "Monasca Log service"
 1126         get_or_create_endpoint \
 1127             "logs" \
 1128             "{$REGION_NAME}" \
 1129             "{$MONASCA_API_URI_V2}" \
 1130             "{$MONASCA_API_URI_V2}" \
 1131             "{$MONASCA_API_URI_V2}"
 1132 
 1133         get_or_create_service "logs-search" "logs-search" "Monasca Log search service"
 1134         get_or_create_endpoint \
 1135             "logs-search" \
 1136             "$REGION_NAME" \
 1137             "$log_search_url" \
 1138             "$log_search_url" \
 1139             "$log_search_url"
 1140 
 1141     fi
 1142 }
 1143 
 1144 function install_keystone_client {
 1145     PIP_VIRTUAL_ENV=/opt/monasca
 1146 
 1147     install_keystoneclient
 1148     install_keystoneauth
 1149 
 1150     unset PIP_VIRTUAL_ENV
 1151 }
 1152 
 1153 function install_monasca_agent {
 1154 
 1155     if is_service_enabled monasca-agent; then
 1156         echo_summary "Install Monasca monasca_agent"
 1157 
 1158         apt_get install python-yaml libxml2-dev libxslt1-dev
 1159 
 1160         MONASCA_AGENT_EXTRAS="kafka_plugin"
 1161         if is_service_enabled nova && [ "$VIRT_DRIVER" = "libvirt" ]; then
 1162             apt_get install libvirt-dev
 1163             MONASCA_AGENT_EXTRAS=${MONASCA_AGENT_EXTRAS},libvirt
 1164         fi
 1165 
 1166         git_clone $MONASCA_CLIENT_REPO $MONASCA_CLIENT_DIR $MONASCA_CLIENT_BRANCH
 1167         git_clone $MONASCA_AGENT_REPO $MONASCA_AGENT_DIR $MONASCA_AGENT_BRANCH
 1168 
 1169         sudo mkdir -p /opt/monasca-agent || true
 1170         sudo chown $STACK_USER:monasca /opt/monasca-agent
 1171 
 1172         if python3_enabled; then
 1173             (cd /opt/monasca-agent ; virtualenv -p python3 .)
 1174             sudo rm -rf /opt/stack/monasca-common/.eggs/
 1175         else
 1176             (cd /opt/monasca-agent ; virtualenv .)
 1177         fi
 1178 
 1179         PIP_VIRTUAL_ENV=/opt/monasca-agent
 1180 
 1181         setup_install $MONASCA_AGENT_DIR $MONASCA_AGENT_EXTRAS
 1182         setup_dev_lib "python-monascaclient"
 1183 
 1184         unset PIP_VIRTUAL_ENV
 1185 
 1186         sudo mkdir -p /etc/monasca/agent/conf.d || true
 1187 
 1188         sudo chown root:root /etc/monasca/agent/conf.d
 1189 
 1190         sudo chmod 0755 /etc/monasca/agent/conf.d
 1191 
 1192         sudo mkdir -p /usr/lib/monasca/agent/custom_checks.d || true
 1193 
 1194         sudo chown root:root /usr/lib/monasca/agent/custom_checks.d
 1195 
 1196         sudo chmod 0755 /usr/lib/monasca/agent/custom_checks.d
 1197 
 1198         sudo mkdir -p /usr/lib/monasca/agent/custom_detect.d || true
 1199 
 1200         sudo chown root:root /usr/lib/monasca/agent/custom_detect.d
 1201 
 1202         sudo chmod 0755 /usr/lib/monasca/agent/custom_detect.d
 1203 
 1204         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-agent/host_alive.yaml /etc/monasca/agent/conf.d/host_alive.yaml
 1205         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-agent/http_check.yaml /etc/monasca/agent/conf.d/http_check.yaml
 1206         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-agent/kafka_consumer.yaml /etc/monasca/agent/conf.d/kafka_consumer.yaml
 1207         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-agent/mysql.yaml /etc/monasca/agent/conf.d/mysql.yaml
 1208         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-agent/process.yaml /etc/monasca/agent/conf.d/process.yaml
 1209         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-agent/zk.yaml /etc/monasca/agent/conf.d/zk.yaml
 1210 
 1211         sudo sed -i "s/127\.0\.0\.1/$(hostname)/" /etc/monasca/agent/conf.d/*.yaml
 1212 
 1213         sudo cp -f "${MONASCA_API_DIR}"/devstack/files/monasca-agent/monasca-reconfigure /usr/local/bin/monasca-reconfigure
 1214 
 1215         sudo chown root:root /usr/local/bin/monasca-reconfigure
 1216 
 1217         sudo chmod 0750 /usr/local/bin/monasca-reconfigure
 1218 
 1219         sudo sed -e "
 1220             s|%MONASCA_STATSD_PORT%|$MONASCA_STATSD_PORT|g;
 1221             s|%MONASCA_SERVICE_TYPE%|$MONASCA_SERVICE_TYPE|g;
 1222             s|%KEYSTONE_AUTH_URI%|$KEYSTONE_AUTH_URI|g;
 1223             s|%SERVICE_DOMAIN_NAME%|$SERVICE_DOMAIN_NAME|g;
 1224             s|%REGION_NAME%|$REGION_NAME|g;
 1225         " -i /usr/local/bin/monasca-reconfigure
 1226     fi
 1227 }
 1228 
 1229 function clean_monasca_agent {
 1230 
 1231     if is_service_enabled monasca-agent; then
 1232         echo_summary "Clean Monasca monasca_agent"
 1233 
 1234         sudo rm /etc/init.d/monasca-agent
 1235 
 1236         sudo rm /usr/local/bin/monasca-reconfigure
 1237 
 1238         sudo rm /etc/monasca/agent/conf.d/host_alive.yaml
 1239 
 1240         sudo chown root:root /etc/monasca/agent/conf.d/host_alive.yaml
 1241 
 1242         chmod 0644 /etc/monasca/agent/conf.d/host_alive.yaml
 1243 
 1244         sudo rm -rf /usr/lib/monasca/agent/custom_detect.d
 1245 
 1246         sudo rm -rf  /usr/lib/monasca/agent/custom_checks.d
 1247 
 1248         sudo rm -rf /etc/monasca/agent/conf.d
 1249 
 1250         sudo rm -rf /etc/monasca/agent
 1251 
 1252         sudo rm -rf /opt/monasca-agent
 1253 
 1254         [[ -f /etc/systemd/system/monasca-agent.target ]] && sudo rm /etc/systemd/system/monasca-agent.target
 1255         [[ -f /etc/systemd/system/monasca-collector.service ]] && sudo rm /etc/systemd/system/monasca-collector.service
 1256         [[ -f /etc/systemd/system/monasca-forwarder.service ]] && sudo rm /etc/systemd/system/monasca-forwarder.service
 1257         [[ -f /etc/systemd/system/monasca-statsd.service ]] && sudo rm /etc/systemd/system/monasca-statsd.service
 1258 
 1259         apt_get purge libxslt1-dev
 1260         apt_get purge libxml2-dev
 1261         apt_get purge python-yaml
 1262     fi
 1263 }
 1264 
 1265 # install nodejs and npm packages, works behind corporate proxy
 1266 # and does not result in gnutsl_handshake error
 1267 function install_nodejs {
 1268 
 1269     echo_summary "Install Node.js"
 1270     curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
 1271 
 1272     apt_get install nodejs
 1273     npm config set registry "http://registry.npmjs.org/"; \
 1274     npm config set proxy "${HTTP_PROXY}"; \
 1275     npm set strict-ssl false;
 1276 }
 1277 
 1278 function init_monasca_grafana {
 1279     echo_summary "Init Grafana"
 1280 
 1281     sudo cp -f -r "${MONASCA_API_DIR}"/devstack/files/grafana/dashboards.d "${DASHBOARDS_DIR}"
 1282     sudo chown -R root:root "${DASHBOARDS_DIR}"
 1283     sudo chmod -R 0644 "${DASHBOARDS_DIR}"
 1284 
 1285 
 1286     if python3_enabled; then
 1287         sudo python3 "${MONASCA_API_DIR}"/devstack/files/grafana/grafana-init.py
 1288     else
 1289         sudo python "${MONASCA_API_DIR}"/devstack/files/grafana/grafana-init.py
 1290     fi
 1291 
 1292     sudo rm -rf "${DASHBOARDS_DIR}"
 1293 }
 1294 
 1295 function install_monasca_grafana {
 1296 
 1297     echo_summary "Install Grafana"
 1298 
 1299     if [ ! -d "${GRAFANA_DIR}" ]; then
 1300         git_timed clone $GRAFANA_REPO $GRAFANA_DIR --branch $GRAFANA_BRANCH --depth 1
 1301     fi
 1302 
 1303     npm config set python /usr/bin/python3
 1304 
 1305     cd "${MONASCA_BASE}"
 1306 
 1307     mkdir grafana-build || true
 1308     cd grafana-build
 1309     export GOPATH=`pwd`
 1310     mkdir -p $GOPATH/src/github.com/grafana
 1311     cd $GOPATH/src/github.com/grafana
 1312     cp -rf "${GRAFANA_DIR}" .
 1313 
 1314     cd grafana
 1315     cp "${MONASCA_UI_DIR}"/grafana-dashboards/* ./public/dashboards/
 1316 
 1317     go run build.go build
 1318 
 1319     npm config set unsafe-perm true
 1320     npm install
 1321     sudo npm install -g grunt-cli
 1322     grunt --force
 1323 
 1324     cd "${MONASCA_BASE}"
 1325     sudo rm -r grafana
 1326 
 1327     sudo useradd grafana || true
 1328     sudo mkdir /etc/grafana || true
 1329     sudo mkdir /var/lib/grafana || true
 1330     sudo mkdir /var/lib/grafana/plugins || true
 1331     sudo mkdir /var/log/grafana || true
 1332 
 1333     git_clone $MONASCA_GRAFANA_DATASOURCE_REPO $MONASCA_GRAFANA_DATASOURCE_DIR $MONASCA_GRAFANA_DATASOURCE_BRANCH
 1334     sudo ln -sfF "${MONASCA_GRAFANA_DATASOURCE_DIR}" /var/lib/grafana/plugins/monasca-grafana-datasource
 1335 
 1336     sudo chown -R grafana:grafana /var/lib/grafana /var/log/grafana
 1337 
 1338     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/grafana/grafana.ini /etc/grafana/grafana.ini
 1339     sudo sed -e "
 1340         s|%KEYSTONE_AUTH_URI%|$KEYSTONE_AUTH_URI|g;
 1341     " -i /etc/grafana/grafana.ini
 1342 
 1343     sudo cp -f "${MONASCA_API_DIR}"/devstack/files/grafana/grafana-server /etc/init.d/grafana-server
 1344     sudo sed -i "s#/usr/sbin#"${MONASCA_BASE}"/grafana-build/src/github.com/grafana/grafana/bin#g" /etc/init.d/grafana-server
 1345     sudo sed -i "s#/usr/share#"${MONASCA_BASE}"/grafana-build/src/github.com/grafana#g" /etc/init.d/grafana-server
 1346 
 1347     sudo systemctl enable grafana-server
 1348 }
 1349 
 1350 function clean_nodejs {
 1351     apt_get purge nodejs npm
 1352 }
 1353 
 1354 function clean_monasca_grafana {
 1355 
 1356     sudo rm -f "${MONASCA_BASE}"/grafana-build
 1357 
 1358     sudo systemctl disable grafana-server
 1359 
 1360     sudo rm /etc/init.d/grafana-server
 1361 
 1362     sudo rm -r /etc/grafana
 1363 
 1364     sudo rm -r /var/lib/grafana
 1365 
 1366     sudo rm -r /var/log/grafana
 1367 
 1368 }
 1369 
 1370 function install_go {
 1371     echo_summary "Install Go ${GO_VERSION}"
 1372 
 1373     local go_tarball=go${GO_VERSION}.linux-amd64.tar.gz
 1374     local go_tarball_url=https://storage.googleapis.com/golang/${go_tarball}
 1375 
 1376     local go_tarball_dest
 1377     go_tarball_dest=`get_extra_file ${go_tarball_url}`
 1378 
 1379     sudo tar -C /usr/local -xzf ${go_tarball_dest}
 1380     export PATH=$PATH:/usr/local/go/bin
 1381 }
 1382 
 1383 function clean_go {
 1384     echo_summary "Clean Go ${GO_VERSION}"
 1385 
 1386     sudo rm -f ${FILES}/go${GO_VERSION}*
 1387     sudo rm -rf /usr/local/go*
 1388     export PATH=$(echo $PATH | sed -e 's|:/usr/local/go/bin||')
 1389 }
 1390 
 1391 ###### extra functions
 1392 
 1393 # Validate a program version string is of the form 1.0.0.
 1394 # Return 0 if a valid program version string, otherwise 1.
 1395 function validate_version {
 1396     version_regex="^([0-9]+\.)?([0-9]+\.)?([0-9]+)$"
 1397 
 1398     if [[ $1 =~ $version_regex ]]; then
 1399         return 0
 1400     else
 1401         return 1
 1402     fi
 1403 }
 1404 
 1405 # Prints the version specified in the pom.xml file in the directory given by
 1406 # the argument
 1407 function get_version_from_pom {
 1408     python -c "import xml.etree.ElementTree as ET; \
 1409         print(ET.parse(open('$1/pom.xml')).getroot().find( \
 1410         '{http://maven.apache.org/POM/4.0.0}version').text)"
 1411 }
 1412 
 1413 function install_monasca_common {
 1414     git_clone $MONASCA_COMMON_REPO $MONASCA_COMMON_DIR $MONASCA_COMMON_BRANCH
 1415     setup_dev_lib "monasca-common"
 1416 }
 1417 
 1418 function install_monasca_statsd {
 1419     git_clone $MONASCA_STATSD_REPO $MONASCA_STATSD_DIR $MONASCA_STATSD_BRANCH
 1420     setup_dev_lib "monasca-statsd"
 1421 }
 1422 
 1423 function install_gate_config_holder {
 1424     sudo install -d -o $STACK_USER $MON_API_GATE_CONFIGURATION_DIR
 1425 }
 1426 
 1427 function find_nearest_apache_mirror {
 1428     if [ -z $APACHE_MIRROR ]; then
 1429         local mirror;
 1430         mirror=`curl -s 'https://www.apache.org/dyn/closer.cgi?as_json=1' | jq --raw-output '.preferred'`
 1431         APACHE_MIRROR=$mirror
 1432     fi
 1433 }
 1434 
 1435 # This solution fixes problem with privileges for agent
 1436 # to gather metrics from services started as root user.
 1437 function init_collector_service {
 1438     if is_service_enabled monasca-agent; then
 1439         echo_summary "Init Monasca collector service"
 1440         sudo systemctl stop monasca-collector
 1441         sudo sed -i "s/User=mon-agent/User=root/g" /etc/systemd/system/monasca-collector.service
 1442         sudo sed -i "s/Group=mon-agent/Group=root/g" /etc/systemd/system/monasca-collector.service
 1443         sudo systemctl daemon-reload
 1444         sudo systemctl restart monasca-collector
 1445     fi
 1446 }
 1447 
 1448 # check for service enabled
 1449 if is_service_enabled monasca; then
 1450 
 1451     if [[ "$1" == "stack" && "$2" == "pre-install" ]]; then
 1452         # Set up system services
 1453         echo_summary "Configuring Monasca system services"
 1454         pre_install_monasca
 1455 
 1456     elif [[ "$1" == "stack" && "$2" == "install" ]]; then
 1457         # Perform installation of service source
 1458         echo_summary "Installing Monasca"
 1459         install_monasca
 1460 
 1461     elif [[ "$1" == "stack" && "$2" == "post-config" ]]; then
 1462         # Configure after the other layer 1 and 2 services have been configured
 1463         echo_summary "Configuring Monasca"
 1464         configure_monasca
 1465 
 1466     elif [[ "$1" == "stack" && "$2" == "extra" ]]; then
 1467         # Initialize and start the Monasca service
 1468         echo_summary "Initializing Monasca"
 1469         extra_monasca
 1470     fi
 1471 
 1472     if [[ "$1" == "unstack" ]]; then
 1473         # Shut down Monasca services
 1474         echo_summary "Unstacking Monasca"
 1475         unstack_monasca
 1476     fi
 1477 
 1478     if [[ "$1" == "clean" ]]; then
 1479         # Remove state and transient data
 1480         # Remember clean.sh first calls unstack.sh
 1481         echo_summary "Cleaning Monasca"
 1482         clean_monasca
 1483     fi
 1484 fi
 1485 
 1486 # check for service enabled
 1487 if is_service_enabled monasca-log; then
 1488 
 1489     if [[ "$1" == "stack" && "$2" == "pre-install" ]]; then
 1490         # Set up system services
 1491         echo_summary "Configuring Monasca Log Management system services"
 1492         pre_install_logs_services
 1493 
 1494     elif [[ "$1" == "stack" && "$2" == "install" ]]; then
 1495         # Perform installation of service source
 1496         echo_summary "Installing Monasca Log Management"
 1497         install_monasca_log
 1498 
 1499     elif [[ "$1" == "stack" && "$2" == "post-config" ]]; then
 1500         # Configure after the other layer 1 and 2 services have been configured
 1501         echo_summary "Configuring Monasca Log Management"
 1502         configure_monasca_log
 1503 
 1504     elif [[ "$1" == "stack" && "$2" == "extra" ]]; then
 1505         # Initialize and start the Monasca service
 1506         echo_summary "Initializing Monasca Log Management"
 1507         init_monasca_log
 1508         init_monasca_grafana_dashboards
 1509         if is_service_enabled monasca-agent; then
 1510             init_agent
 1511         fi
 1512         start_monasca_log
 1513     fi
 1514 
 1515     if [[ "$1" == "unstack" ]]; then
 1516         # Shut down Monasca services
 1517         echo_summary "Unstacking Monasca Log Management"
 1518         stop_monasca_log
 1519         delete_kafka_topics
 1520     fi
 1521 
 1522     if [[ "$1" == "clean" ]]; then
 1523         # Remove state and transient data
 1524         # Remember clean.sh first calls unstack.sh
 1525         echo_summary "Cleaning Monasca Log Management"
 1526         clean_monasca_log
 1527     fi
 1528 fi
 1529 
 1530 #Restore errexit
 1531 $ERREXIT
 1532 
 1533 # Restore xtrace
 1534 $XTRACE