"Fossies" - the Fresh Open Source Software Archive

Member "ec2-api-12.0.0/install.sh" (14 Apr 2021, 11049 Bytes) of package /linux/misc/openstack/ec2-api-12.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 last Fossies "Diffs" side-by-side code changes report for "install.sh": 9.0.0_vs_10.0.0.

    1 #!/bin/bash -e
    2 
    3 #Parameters to configure
    4 SERVICE_USERNAME=ec2api
    5 SERVICE_PASSWORD=ec2api
    6 SERVICE_TENANT=service
    7 # this domain name will be used for project and user
    8 SERVICE_DOMAIN_NAME=Default
    9 EC2API_PORT=8788
   10 CONNECTION="mysql://ec2api:ec2api@127.0.0.1/ec2api?charset=utf8"
   11 LOG_DIR=/var/log/ec2api
   12 CONF_DIR=/etc/ec2api
   13 NOVA_CONF=/etc/nova/nova.conf
   14 CONF_FILE=$CONF_DIR/ec2api.conf
   15 APIPASTE_FILE=$CONF_DIR/api-paste.ini
   16 
   17 DATA_DIR=${DATA_DIR:-/var/lib/ec2api}
   18 AUTH_CACHE_DIR=${AUTH_CACHE_DIR:-/var/cache/ec2api}
   19 
   20 #Check for environment
   21 if [[ -z "$OS_AUTH_URL" || -z "$OS_USERNAME" || -z "$OS_PASSWORD" ]]; then
   22     echo "Please set OS_AUTH_URL, OS_USERNAME, OS_PASSWORD"
   23     exit 1
   24 fi
   25 if [[ -z "$OS_TENANT_NAME" && -z "$OS_PROJECT_NAME" ]]; then
   26     echo "Please set OS_TENANT_NAME or OS_PROJECT_NAME"
   27     exit 1
   28 fi
   29 
   30 
   31 #### utilities functions merged from devstack to check required parameter is not empty
   32 # Prints line number and "message" in error format
   33 # err $LINENO "message"
   34 function err() {
   35     local exitcode=$?
   36     errXTRACE=$(set +o | grep xtrace)
   37     set +o xtrace
   38     local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
   39     echo $msg 1>&2;
   40     $errXTRACE
   41     return $exitcode
   42 }
   43 # Prints backtrace info
   44 # filename:lineno:function
   45 function backtrace {
   46     local level=$1
   47     local deep=$((${#BASH_SOURCE[@]} - 1))
   48     echo "[Call Trace]"
   49     while [ $level -le $deep ]; do
   50         echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
   51         deep=$((deep - 1))
   52     done
   53 }
   54 
   55 
   56 # Prints line number and "message" then exits
   57 # die $LINENO "message"
   58 function die() {
   59     local exitcode=$?
   60     set +o xtrace
   61     local line=$1; shift
   62     if [ $exitcode == 0 ]; then
   63         exitcode=1
   64     fi
   65     backtrace 2
   66     err $line "$*"
   67     exit $exitcode
   68 }
   69 
   70 
   71 # Checks an environment variable is not set or has length 0 OR if the
   72 # exit code is non-zero and prints "message" and exits
   73 # NOTE: env-var is the variable name without a '$'
   74 # die_if_not_set $LINENO env-var "message"
   75 function die_if_not_set() {
   76     local exitcode=$?
   77     FXTRACE=$(set +o | grep xtrace)
   78     set +o xtrace
   79     local line=$1; shift
   80     local evar=$1; shift
   81     if ! is_set $evar || [ $exitcode != 0 ]; then
   82         die $line "$*"
   83     fi
   84     $FXTRACE
   85 }
   86 
   87 # Test if the named environment variable is set and not zero length
   88 # is_set env-var
   89 function is_set() {
   90     local var=\$"$1"
   91     eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
   92 }
   93 
   94 #######################################
   95 
   96 get_data() {
   97     local match_column=$(($1 + 1))
   98     local regex="$2"
   99     local output_column=$(($3 + 1))
  100     shift 3
  101 
  102     output=$("$@" | \
  103            awk -F'|' \
  104                "! /^\+/ && \$${match_column} ~ \"^ *${regex} *\$\" \
  105                 { print \$${output_column} }")
  106 
  107     echo "$output"
  108 }
  109 
  110 get_id () {
  111     get_data 1 id 2 "$@"
  112 }
  113 
  114 get_user() {
  115     local username=$1
  116 
  117     local user_id=$(openstack user show $username -f value -c id 2>/dev/null)
  118 
  119     if [ -n "$user_id" ]; then
  120         echo "Found existing $username user" >&2
  121         echo $user_id
  122     else
  123         echo "Creating $username user..." >&2
  124         openstack user create -f value -c id \
  125                               $username \
  126                               --password "$SERVICE_PASSWORD" \
  127                               --project $SERVICE_TENANT \
  128                               --email $username@example.com
  129     fi
  130 }
  131 
  132 add_role() {
  133     local user_id=$1
  134     local tenant=$2
  135     local role_id=$3
  136     local username=$4
  137     local domain=$5
  138 
  139     if [[ -n $domain ]]
  140     then
  141         domain_args="--project-domain $domain --user-domain $domain"
  142     fi
  143 
  144     # Gets user role id
  145     existing_role=$(openstack role assignment list -f value -c User \
  146         --role $role_id \
  147         --user $user_id \
  148         --project $tenant \
  149         $domain_args)
  150     if [ -n "$existing_role" ]
  151     then
  152         echo "User $username already has role $role_id" >&2
  153         return
  154     fi
  155 
  156     # Adds role to user
  157     openstack role add $role_id \
  158                        --user $user_id \
  159                        --project $tenant \
  160                        $domain_args
  161 }
  162 
  163 
  164 # Determines if the given option is present in the INI file
  165 # ini_has_option config-file section option
  166 function ini_has_option() {
  167     local file=$1
  168     local section=$2
  169     local option=$3
  170     local line
  171     line=$(sudo sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
  172     [ -n "$line" ]
  173 }
  174 
  175 # Set an option in an INI file
  176 # iniset config-file section option value
  177 function iniset() {
  178     local file=$1
  179     local section=$2
  180     local option=$3
  181     local value=$4
  182     if ! sudo grep -q "^\[$section\]" "$file"; then
  183         # Add section at the end
  184         sudo bash -c "echo -e \"\n[$section]\" >>\"$file\""
  185     fi
  186     if ! ini_has_option "$file" "$section" "$option"; then
  187         # Add it
  188         sudo sed -i -e "/^\[$section\]/ a\\
  189 $option = $value
  190 " "$file"
  191     else
  192         # Replace it
  193         sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
  194     fi
  195 }
  196 
  197 # Get an option from an INI file
  198 # iniget config-file section option
  199 function iniget() {
  200     local file=$1
  201     local section=$2
  202     local option=$3
  203     local line
  204     line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
  205     echo ${line#*=}
  206 }
  207 
  208 # Copy an option from Nova INI file or from environment if it's set
  209 function copynovaopt() {
  210     local option_name=$1
  211     local option_group=$2
  212     local env_var
  213     local option
  214     env_var=${option_name^^}
  215     if [ ${!env_var+x} ]; then
  216         option=${!env_var}
  217     elif ini_has_option "$NOVA_CONF" $option_group $option_name; then
  218         option=$(iniget $NOVA_CONF $option_group $option_name)
  219     else
  220         return 0
  221     fi
  222     iniset $CONF_FILE $option_group $option_name $option
  223 }
  224 
  225 if [[ -n $(openstack catalog show network) ]]; then
  226     VPC_SUPPORT="True"
  227     DISABLE_EC2_CLASSIC="True"
  228 else
  229     VPC_SUPPORT="False"
  230     DISABLE_EC2_CLASSIC="False"
  231 fi
  232 if [[ "$VPC_SUPPORT" == "True" && -z "$EXTERNAL_NETWORK" ]]; then
  233     declare -a neutron_output
  234     readarray -s 3 -t neutron_output < <(openstack network list --external)
  235     if ((${#neutron_output[@]} < 2)); then
  236         reason="No external network is declared in Neutron."
  237     elif ((${#neutron_output[@]} > 2)); then
  238         reason="More than one external networks are declared in Neutron."
  239     else
  240         EXTERNAL_NETWORK=$(echo $neutron_output | awk -F '|' '{ print $3 }')
  241     fi
  242     die_if_not_set $LINENO EXTERNAL_NETWORK "$reason. Please set EXTERNAL_NETWORK environment variable to the external network dedicated to EC2 elastic IP operations"
  243 fi
  244 
  245 #create keystone user with admin and service privileges
  246 ADMIN_ROLE=$(openstack role show admin -c id -f value)
  247 die_if_not_set $LINENO ADMIN_ROLE "Fail to get ADMIN_ROLE by 'openstack role show' "
  248 SERVICE_ROLE=$(openstack role show service -c id -f value)
  249 die_if_not_set $LINENO ADMIN_ROLE "Fail to get SERVICE_ROLE by 'openstack role show' "
  250 SERVICE_TENANT_ID=$(openstack project show service -c id -f value)
  251 die_if_not_set $LINENO SERVICE_TENANT_ID "Fail to get service tenant 'openstack project show' "
  252 
  253 echo ADMIN_ROLE $ADMIN_ROLE
  254 echo SERVICE_ROLE $SERVICE_ROLE
  255 echo SERVICE_TENANT $SERVICE_TENANT
  256 
  257 SERVICE_USERID=$(get_user $SERVICE_USERNAME)
  258 die_if_not_set $LINENO SERVICE_USERID "Fail to get user for $SERVICE_USERNAME"
  259 echo SERVICE_USERID $SERVICE_USERID
  260 SERVICE_DOMAIN_NAME=${SERVICE_DOMAIN_NAME:-Default}
  261 add_role $SERVICE_USERID $SERVICE_TENANT $ADMIN_ROLE $SERVICE_USERNAME
  262 add_role $SERVICE_USERID $SERVICE_TENANT $SERVICE_ROLE $SERVICE_USERNAME $SERVICE_DOMAIN_NAME
  263 
  264 #create log dir
  265 echo Creating log dir
  266 sudo install -d $LOG_DIR --owner=$USER
  267 
  268 #copy conf files (do not override it)
  269 echo Creating configs
  270 sudo mkdir -p /etc/ec2api > /dev/null
  271 if [ ! -s $CONF_FILE ]; then
  272     sudo touch $CONF_FILE
  273 fi
  274 if [ ! -s $APIPASTE_FILE ]; then
  275     sudo cp etc/ec2api/api-paste.ini $APIPASTE_FILE
  276 fi
  277 
  278 
  279 #update default config with some values
  280 iniset $CONF_FILE DEFAULT ec2api_listen_port "$EC2API_PORT"
  281 iniset $CONF_FILE DEFAULT ec2_port "$EC2API_PORT"
  282 iniset $CONF_FILE DEFAULT api_paste_config $APIPASTE_FILE
  283 iniset $CONF_FILE DEFAULT logging_context_format_string "%(asctime)s.%(msecs)03d %(levelname)s %(name)s [%(request_id)s %(user_name)s %(project_name)s] %(instance)s%(message)s"
  284 iniset $CONF_FILE DEFAULT log_dir "$LOG_DIR"
  285 iniset $CONF_FILE DEFAULT verbose True
  286 iniset $CONF_FILE DEFAULT keystone_ec2_tokens_url "$OS_AUTH_URL/v3/ec2tokens"
  287 iniset $CONF_FILE database connection "$CONNECTION"
  288 iniset $CONF_FILE DEFAULT disable_ec2_classic "$DISABLE_EC2_CLASSIC"
  289 iniset $CONF_FILE DEFAULT external_network "$EXTERNAL_NETWORK"
  290 iniset $CONF_FILE oslo_concurrency lock_path "$EC2API_STATE_PATH"
  291 iniset $CONF_FILE DEFAULT state_path "$DATA_DIR"
  292 
  293 GROUP_AUTHTOKEN="keystone_authtoken"
  294 iniset $CONF_FILE $GROUP_AUTHTOKEN signing_dir "$AUTH_CACHE_DIR"
  295 iniset $CONF_FILE $GROUP_AUTHTOKEN www_authenticate_uri "$OS_AUTH_URL"
  296 iniset $CONF_FILE $GROUP_AUTHTOKEN auth_url "$OS_AUTH_URL"
  297 iniset $CONF_FILE $GROUP_AUTHTOKEN username $SERVICE_USERNAME
  298 iniset $CONF_FILE $GROUP_AUTHTOKEN password $SERVICE_PASSWORD
  299 iniset $CONF_FILE $GROUP_AUTHTOKEN project_name $SERVICE_TENANT
  300 iniset $CONF_FILE $GROUP_AUTHTOKEN project_domain_name $SERVICE_DOMAIN_NAME
  301 iniset $CONF_FILE $GROUP_AUTHTOKEN user_domain_name $SERVICE_DOMAIN_NAME
  302 iniset $CONF_FILE $GROUP_AUTHTOKEN auth_type password
  303 
  304 GROUP_CACHE="cache"
  305 iniset $CONF_FILE $GROUP_CACHE enabled True
  306 
  307 if [[ -f "$NOVA_CONF" ]]; then
  308     # NOTE(ft): use swift instead internal s3 server if enabled
  309     if [[ -n $(openstack catalog show object-store 2>/dev/null) ]] &&
  310             [[ -n $(openstack catalog show s3 2>/dev/null) ]]; then
  311         s3_host="127.0.0.1"
  312         if ini_has_option "$NOVA_CONF" DEFAULT "s3_host"; then
  313             s3_host=$(iniget $NOVA_CONF DEFAULT $option_name)
  314         fi
  315         s3_port="3334"
  316         if ini_has_option "$NOVA_CONF" DEFAULT "s3_port"; then
  317             s3_port=$(iniget $NOVA_CONF DEFAULT $option_name)
  318         fi
  319         s3_proto="http"
  320         if ini_has_option "$NOVA_CONF" DEFAULT "s3_use_ssl"; then
  321             s3_use_ssl=$(iniget $NOVA_CONF DEFAULT $option_name)
  322             s3_use_ssl=`echo $s3_use_ssl | awk '{print toupper($0)}'`
  323             if [[ $s3_use_ssl == "TRUE" ]]; then
  324                 s3_proto="https"
  325             fi
  326         fi
  327         iniset $CONF_FILE DEFAULT s3_url "$s3_proto://$s3_host:$s3_port"
  328 
  329     fi
  330 fi
  331 
  332 #init cache dir
  333 echo Creating signing dir
  334 sudo mkdir -p $AUTH_CACHE_DIR
  335 sudo chown $USER $AUTH_CACHE_DIR
  336 sudo rm -f $AUTH_CACHE_DIR/*
  337 
  338 #init data dir
  339 echo Creating data dir
  340 sudo mkdir -p $DATA_DIR
  341 sudo chown $USER $DATA_DIR
  342 sudo rm -f $DATA_DIR/*
  343 
  344 #install it
  345 echo Installing package
  346 if [[ -z "$VIRTUAL_ENV" ]]; then
  347   SUDO_PREFIX="sudo"
  348   if ! command -v pip >/dev/null; then
  349     sudo apt-get install python-pip
  350   fi
  351 fi
  352 $SUDO_PREFIX pip install -e ./
  353 $SUDO_PREFIX rm -rf build ec2_api.egg-info
  354 
  355 #recreate database
  356 echo Setuping database
  357 $SUDO_PREFIX tools/db/ec2api-db-setup deb