"Fossies" - the Fresh Open Source Software Archive

Member "zun-4.0.0/devstack/lib/zun" (16 Oct 2019, 11396 Bytes) of package /linux/misc/openstack/zun-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. 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 "zun": 3.0.0_vs_4.0.0.

    1 #!/bin/bash
    2 #
    3 # lib/zun
    4 # Functions to control the configuration and operation of the **zun** service
    5 
    6 # Dependencies:
    7 #
    8 # - ``functions`` file
    9 # - ``DEST``, ``DATA_DIR``, ``STACK_USER`` must be defined
   10 # - ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
   11 
   12 # ``stack.sh`` calls the entry points in this order:
   13 #
   14 # - install_zun
   15 # - configure_zun
   16 # - create_zun_conf
   17 # - create_zun_accounts
   18 # - init_zun
   19 # - start_zun
   20 # - stop_zun
   21 # - cleanup_zun
   22 
   23 # Save trace setting
   24 XTRACE=$(set +o | grep xtrace)
   25 set +o xtrace
   26 
   27 
   28 # Defaults
   29 # --------
   30 
   31 # Set up default directories
   32 ZUN_REPO=${ZUN_REPO:-${GIT_BASE}/openstack/zun.git}
   33 ZUN_BRANCH=${ZUN_BRANCH:-$TARGET_BRANCH}
   34 ZUN_DIR=$DEST/zun
   35 ZUN_TEMPEST_PLUGIN_REPO=${ZUN_TEMPEST_PLUGIN_REPO:-${GIT_BASE}/openstack/zun-tempest-plugin.git}
   36 ZUN_TEMPEST_PLUGIN_BRANCH=${ZUN_TEMPEST_PLUGIN_BRANCH:-master}
   37 ZUN_TEMPEST_PLUGIN_DIR=$DEST/zun-tempest-plugin
   38 
   39 GITREPO["python-zunclient"]=${ZUNCLIENT_REPO:-${GIT_BASE}/openstack/python-zunclient.git}
   40 GITBRANCH["python-zunclient"]=${ZUNCLIENT_BRANCH:-master}
   41 GITDIR["python-zunclient"]=$DEST/python-zunclient
   42 
   43 ZUN_STATE_PATH=${ZUN_STATE_PATH:=$DATA_DIR/zun}
   44 ZUN_AUTH_CACHE_DIR=${ZUN_AUTH_CACHE_DIR:-/var/cache/zun}
   45 
   46 ZUN_CONF_DIR=/etc/zun
   47 ZUN_CONF=$ZUN_CONF_DIR/zun.conf
   48 ZUN_API_PASTE=$ZUN_CONF_DIR/api-paste.ini
   49 
   50 if is_ssl_enabled_service "zun" || is_service_enabled tls-proxy; then
   51     ZUN_SERVICE_PROTOCOL="https"
   52 fi
   53 
   54 # Toggle for deploying ZUN-API under a wsgi server
   55 ZUN_USE_UWSGI=${ZUN_USE_UWSGI:-True}
   56 
   57 
   58 # Public facing bits
   59 ZUN_SERVICE_HOST=${ZUN_SERVICE_HOST:-$SERVICE_HOST}
   60 ZUN_SERVICE_PORT=${ZUN_SERVICE_PORT:-9517}
   61 ZUN_SERVICE_PORT_INT=${ZUN_SERVICE_PORT_INT:-19517}
   62 ZUN_SERVICE_PROTOCOL=${ZUN_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL}
   63 
   64 ZUN_TRUSTEE_DOMAIN_ADMIN_PASSWORD=${ZUN_TRUSTEE_DOMAIN_ADMIN_PASSWORD:-secret}
   65 
   66 # Support entry points installation of console scripts
   67 if [[ -d $ZUN_DIR/bin ]]; then
   68     ZUN_BIN_DIR=$ZUN_DIR/bin
   69 else
   70     ZUN_BIN_DIR=$(get_python_exec_prefix)
   71 fi
   72 
   73 ZUN_UWSGI=$ZUN_BIN_DIR/zun-api-wsgi
   74 ZUN_UWSGI_CONF=$ZUN_CONF_DIR/zun-api-uwsgi.ini
   75 
   76 DOCKER_REMOTE_API_PORT=2375
   77 ZUN_DRIVER=${ZUN_DRIVER:-docker}
   78 ZUN_DB_TYPE=${ZUN_DB_TYPE:-sql}
   79 
   80 ZUN_ETCD_VERSION=${ZUN_ETCD_VERSION:-v3.0.13}
   81 ZUN_ETCD_PORT=${ZUN_ETCD_PORT:-2379}
   82 ZUN_WEBSOCKET_PROXY_PORT=${ZUN_WEBSOCKET_PROXY_PORT:-6784}
   83 ZUN_WEBSOCKET_PROXY_IP=${ZUN_WEBSOCKET_PROXY_IP:-$(ipv6_unquote $SERVICE_LISTEN_ADDRESS)}
   84 if is_ubuntu; then
   85     UBUNTU_RELEASE_BASE_NUM=`lsb_release -r | awk '{print $2}' | cut -d '.' -f 1`
   86 fi
   87 
   88 ZUN_DOCKER_IMAGES=${ZUN_DOCKER_IMAGES:-kubernetes/pause}
   89 
   90 # Functions
   91 # ---------
   92 
   93 # cleanup_zun() - Remove residual data files, anything left over from previous
   94 # runs that a clean run would need to clean up
   95 function cleanup_zun {
   96     sudo rm -rf $ZUN_STATE_PATH $ZUN_AUTH_CACHE_DIR
   97 
   98     # Destroy old containers
   99     local container_name_prefix=${CONTAINER_NAME_PREFIX:-zun-}
  100     local containers
  101     containers=`sudo docker ps -a | grep $container_name_prefix | sed "s/.*\($container_name_prefix[0-9a-zA-Z-]*\).*/\1/g"`
  102     if [ ! "$containers" = "" ]; then
  103         sudo docker rm -f $containers || true
  104     fi
  105 
  106     remove_uwsgi_config "$ZUN_UWSGI_CONF" "$ZUN_UWSGI"
  107     sudo rm -rf $ZUN_CONF_DIR
  108 }
  109 
  110 # configure_zun() - Set config files, create data dirs, etc
  111 function configure_zun {
  112     # Put config files in ``/etc/zun`` for everyone to find
  113     if [[ ! -d $ZUN_CONF_DIR ]]; then
  114         sudo mkdir -p $ZUN_CONF_DIR
  115         sudo chown $STACK_USER $ZUN_CONF_DIR
  116     fi
  117 
  118     configure_rootwrap zun
  119 
  120     # Rebuild the config file from scratch
  121     create_zun_conf
  122 
  123     if is_service_enabled zun-api; then
  124         create_api_paste_conf
  125 
  126         write_uwsgi_config "$ZUN_UWSGI_CONF" "$ZUN_UWSGI" "/container"
  127     fi
  128 }
  129 
  130 # upload_images() - Upload docker images to glance
  131 function upload_images {
  132     echo_summary "Uploading images"
  133 
  134     if "${ZUN_DRIVER}" == "docker" && is_service_enabled g-api; then
  135         for image in ${ZUN_DOCKER_IMAGES//,/ }; do
  136             sudo docker pull $image
  137             sudo docker save $image | openstack image create $image --public --container-format docker --disk-format raw
  138         done
  139     fi
  140 }
  141 
  142 # create_zun_accounts() - Set up common required ZUN accounts
  143 #
  144 # Project              User         Roles
  145 # ------------------------------------------------------------------
  146 # SERVICE_PROJECT_NAME  zun         service
  147 function create_zun_accounts {
  148 
  149     create_service_user "zun" "admin"
  150 
  151     if is_service_enabled zun-api; then
  152 
  153         local zun_api_url
  154         if [[ "$ZUN_USE_UWSGI" == "True" ]]; then
  155             zun_api_url="$ZUN_SERVICE_PROTOCOL://$ZUN_SERVICE_HOST/container"
  156         else
  157             zun_api_url="$ZUN_SERVICE_PROTOCOL://$ZUN_SERVICE_HOST:$ZUN_SERVICE_PORT"
  158         fi
  159 
  160         local zun_service=$(get_or_create_service "zun" \
  161             "container" "Container As Service")
  162         get_or_create_endpoint $zun_service \
  163             "$REGION_NAME" \
  164             "$zun_api_url/v1" \
  165             "$zun_api_url/v1" \
  166             "$zun_api_url/v1"
  167     fi
  168 
  169 }
  170 
  171 # create_zun_conf() - Create a new zun.conf file
  172 function create_zun_conf {
  173 
  174     # (Re)create ``zun.conf``
  175     rm -f $ZUN_CONF
  176     if [[ ${ZUN_DRIVER} == "docker" ]]; then
  177         iniset $ZUN_CONF DEFAULT container_driver docker.driver.DockerDriver
  178     fi
  179     iniset $ZUN_CONF DEFAULT debug "$ENABLE_DEBUG_LOG_LEVEL"
  180     if [[ $SERVICE_IP_VERSION == 6 ]]; then
  181         iniset $ZUN_CONF DEFAULT my_ip "$HOST_IPV6"
  182         iniset $ZUN_CONF docker docker_remote_api_host "$HOST_IPV6"
  183     else
  184         iniset $ZUN_CONF DEFAULT my_ip "$HOST_IP"
  185         iniset $ZUN_CONF docker docker_remote_api_host "$HOST_IP"
  186     fi
  187     iniset $ZUN_CONF DEFAULT default_cpu 0
  188     iniset $ZUN_CONF DEFAULT default_memory 0
  189     iniset $ZUN_CONF DEFAULT default_disk 0
  190     iniset_rpc_backend zun $ZUN_CONF
  191     iniset $ZUN_CONF database connection `database_connection_url zun`
  192     iniset $ZUN_CONF websocket_proxy wsproxy_port "$ZUN_WEBSOCKET_PROXY_PORT"
  193     iniset $ZUN_CONF websocket_proxy wsproxy_host "$ZUN_WEBSOCKET_PROXY_IP"
  194     iniset $ZUN_CONF websocket_proxy base_url "ws://${SERVICE_HOST}:${ZUN_WEBSOCKET_PROXY_PORT}/"
  195     iniset $ZUN_CONF api host_ip "$(ipv6_unquote $ZUN_SERVICE_HOST)"
  196     iniset $ZUN_CONF api port "$ZUN_SERVICE_PORT"
  197 
  198     iniset $ZUN_CONF keystone_auth auth_type password
  199     iniset $ZUN_CONF keystone_auth username zun
  200     iniset $ZUN_CONF keystone_auth password $SERVICE_PASSWORD
  201     iniset $ZUN_CONF keystone_auth project_name $SERVICE_PROJECT_NAME
  202     iniset $ZUN_CONF keystone_auth project_domain_id default
  203     iniset $ZUN_CONF keystone_auth user_domain_id default
  204 
  205     configure_auth_token_middleware $ZUN_CONF zun $ZUN_AUTH_CACHE_DIR
  206 
  207     iniset $ZUN_CONF keystone_auth auth_url $KEYSTONE_AUTH_URI_V3
  208     # FIXME(pauloewerton): keystone_authtoken section is deprecated. Remove it
  209     # after deprecation period.
  210     iniset $ZUN_CONF keystone_authtoken www_authenticate_uri $KEYSTONE_SERVICE_URI_V3
  211     iniset $ZUN_CONF keystone_authtoken auth_url $KEYSTONE_AUTH_URI_V3
  212     iniset $ZUN_CONF keystone_authtoken auth_version v3
  213 
  214     iniset $ZUN_CONF glance images_directory $ZUN_STATE_PATH/images
  215 
  216     if is_fedora || is_suse; then
  217         # zun defaults to /usr/local/bin, but fedora and suse pip like to
  218         # install things in /usr/bin
  219         iniset $ZUN_CONF DEFAULT bindir "/usr/bin"
  220     fi
  221 
  222     if [ -n "$ZUN_STATE_PATH" ]; then
  223         iniset $ZUN_CONF DEFAULT state_path "$ZUN_STATE_PATH"
  224         iniset $ZUN_CONF oslo_concurrency lock_path "$ZUN_STATE_PATH"
  225     fi
  226 
  227     if [ "$SYSLOG" != "False" ]; then
  228         iniset $ZUN_CONF DEFAULT use_syslog "True"
  229     fi
  230 
  231     # Format logging
  232     setup_logging $ZUN_CONF
  233 
  234     # Register SSL certificates if provided
  235     if is_ssl_enabled_service zun; then
  236         ensure_certificates zun
  237 
  238         iniset $ZUN_CONF DEFAULT ssl_cert_file "$ZUN_SSL_CERT"
  239         iniset $ZUN_CONF DEFAULT ssl_key_file "$ZUN_SSL_KEY"
  240 
  241         iniset $ZUN_CONF DEFAULT enabled_ssl_apis "$ZUN_ENABLED_APIS"
  242     fi
  243 
  244     if is_service_enabled n-cpu; then
  245         iniset $ZUN_CONF compute host_shared_with_nova "True"
  246     fi
  247 }
  248 
  249 function create_api_paste_conf {
  250     # copy api_paste.ini
  251     cp $ZUN_DIR/etc/zun/api-paste.ini $ZUN_API_PASTE
  252 }
  253 
  254 # create_zun_cache_dir() - Part of the init_ZUN() process
  255 function create_zun_cache_dir {
  256     # Create cache dir
  257     sudo mkdir -p $ZUN_AUTH_CACHE_DIR
  258     sudo chown $STACK_USER $ZUN_AUTH_CACHE_DIR
  259     rm -f $ZUN_AUTH_CACHE_DIR/*
  260 }
  261 
  262 
  263 # init_zun() - Initialize databases, etc.
  264 function init_zun {
  265     # Only do this step once on the API node for an entire cluster.
  266     if is_service_enabled zun-api; then
  267         if is_service_enabled $DATABASE_BACKENDS; then
  268             # (Re)create zun database
  269             recreate_database zun
  270 
  271             # Migrate zun database
  272             $ZUN_BIN_DIR/zun-db-manage upgrade
  273         fi
  274 
  275         create_zun_cache_dir
  276     fi
  277 }
  278 
  279 # install_zunclient() - Collect source and prepare
  280 function install_zunclient {
  281     if use_library_from_git "python-zunclient"; then
  282         git_clone_by_name "python-zunclient"
  283         setup_dev_lib "python-zunclient"
  284         sudo install -D -m 0644 -o $STACK_USER {${GITDIR["python-zunclient"]}/tools/,/etc/bash_completion.d/}zun.bash_completion
  285     fi
  286 }
  287 
  288 # install_zun() - Collect source and prepare
  289 function install_zun {
  290     git_clone $ZUN_REPO $ZUN_DIR $ZUN_BRANCH
  291     setup_develop $ZUN_DIR
  292 
  293     git_clone $ZUN_TEMPEST_PLUGIN_REPO $ZUN_TEMPEST_PLUGIN_DIR $ZUN_TEMPEST_PLUGIN_BRANCH
  294     setup_develop $ZUN_TEMPEST_PLUGIN_DIR
  295 }
  296 
  297 # start_zun_api() - Start the API process ahead of other things
  298 function start_zun_api {
  299     # Get right service port for testing
  300     local service_port=$ZUN_SERVICE_PORT
  301     local service_protocol=$ZUN_SERVICE_PROTOCOL
  302     if is_service_enabled tls-proxy; then
  303         service_port=$ZUN_SERVICE_PORT_INT
  304         service_protocol="http"
  305     fi
  306 
  307     local zun_url
  308     if [ "$ZUN_USE_UWSGI" == "True" ]; then
  309         run_process zun-api "$ZUN_BIN_DIR/uwsgi --procname-prefix zun-api --ini $ZUN_UWSGI_CONF"
  310         zun_url=$service_protocol://$ZUN_SERVICE_HOST/container
  311     else
  312         run_process zun-api "$ZUN_BIN_DIR/zun-api"
  313         zun_url=$service_protocol://$ZUN_SERVICE_HOST:$service_port
  314     fi
  315 
  316     echo "Waiting for zun-api to start..."
  317     if ! wait_for_service $SERVICE_TIMEOUT $zun_url; then
  318         die $LINENO "zun-api did not start"
  319     fi
  320 
  321     # Start proxies if enabled
  322     if is_service_enabled tls-proxy; then
  323         start_tls_proxy zun '*' $ZUN_SERVICE_PORT $ZUN_SERVICE_HOST $ZUN_SERVICE_PORT_INT
  324     fi
  325 
  326     # Start websocket proxy for interactive mode
  327     if is_service_enabled zun-wsproxy; then
  328         run_process zun-wsproxy "$ZUN_BIN_DIR/zun-wsproxy"
  329     fi
  330 }
  331 
  332 # start_zun_compute() - Start Zun compute agent
  333 function start_zun_compute {
  334     echo "Start zun compute..."
  335     run_process zun-compute "$ZUN_BIN_DIR/zun-compute"
  336 }
  337 
  338 # start_zun() - Start running processes, including screen
  339 function start_zun {
  340 
  341     # ``run_process`` checks ``is_service_enabled``, it is not needed here
  342     start_zun_api
  343     start_zun_compute
  344 }
  345 
  346 # stop_zun() - Stop running processes (non-screen)
  347 function stop_zun {
  348     if [ "$ZUN_USE_UWSGI" == "True" ]; then
  349         disable_apache_site zun
  350         restart_apache_server
  351     else
  352         stop_process zun-api
  353     fi
  354     stop_process zun-compute
  355     if is_service_enabled zun-wsproxy; then
  356         stop_process zun-wsproxy
  357     fi
  358 }
  359 
  360 # Restore xtrace
  361 $XTRACE