Commit baec3519 authored by Pascal Déchamboux's avatar Pascal Déchamboux
Browse files

Correct bash scripting pb

parent d1367432
Loading
Loading
Loading
Loading
+63 −68
Original line number Diff line number Diff line
@@ -670,8 +670,8 @@ stages:
    pre_delete

    # delete app
    local sfound
    sfound=$(cf apps | sed -e 1,3d | grep "^${appname} " | cat)
    tobesearched="/^${appname}\$/p"
    sfound=`cf apps | sed -e 1,3d | cut -d " " -f 1 | sed -ne ${tobesearched}`
    if [[ -z "$sfound" ]]; then
      log_info "--- nothing to delete"
    else
@@ -767,6 +767,11 @@ stages:
    if [[ "${field_value}" == "null" ]]; then
      if [[ "${notnull}" == "true" ]]; then
   	    desc_field_error="${2} should not be null: creation aborted"
      if [[ ! -z "$desc_field_error" ]]; then
        log_info "${smsg}${COLOR_RED}KO - skipped${COLOR_NONE} (${COLOR_BLUE}${desc_field_error}${COLOR_NONE})"
        manage_services_errors=$((manage_services_errors+1))
        return
      fi
   	    return
   	  else
   	    desc_field=""
@@ -782,31 +787,28 @@ stages:

  function is_cups() {
    export res_is_cups
    if [[ "$1" = "cups" && "$2" = "cups" ]]; then
    if [[ "$1" = "cups" ]]; then
      if [[ "$2" = "cups" ]]; then
        res_is_cups="CUPS"
      return
    fi
    if [[ "$1" = "CUPS" && "$2" != "CUPS" ]]; then
      elif [[ "$2" = "CUPS" ]]; then
        res_is_cups="CUPS"
      return
      else
        res_is_cups="SERV"
      fi
    if [[ "$1" = "cups" && "$2" != "CUPS" ]]; then
    elif [[ "$1" = "CUPS" ]]; then
      if [[ "$2" = "cups" ]]; then
        res_is_cups="CUPS"
      return
    fi
    if [[ "$1" = "CUPS" && "$2" = "cups" ]]; then
      elif [[ "$2" = "CUPS" ]]; then
        res_is_cups="CUPS"
      return
      else
        res_is_cups="SERV"
      fi
    else
      res_is_cups="SERV"
    fi
  }

  function create_service() {
    local fields
    local mandatory
    local fvalues
    local service_desc
    local smsg
    fields=("cfServiceName" "cfServiceOffering" "cfServicePlan" "cfServiceBroker" "cfServiceArgs")
    mandatory=("true" "true" "true" "false" "false")
    fvalues=("" "" "" "" "")
@@ -815,41 +817,37 @@ stages:
    for fi in "${!fields[@]}"
    do
      get_desc_field $service_desc "${fields[$fi]}" "${mandatory[$fi]}"
      if [[ ! -z "$desc_field_error" ]]; then
        log_info "${smsg}${COLOR_RED}KO - skipped${COLOR_NONE} (${COLOR_BLUE}${desc_field_error}${COLOR_NONE})"
        manage_services_errors=$((manage_services_errors+1))
        return
      fi
      fvalues[$fi]=$(echo ${desc_field} | tr -d '\r' | envsubst)
    done
    log_info "${smsg}service definition OK - proceed"
    for fi in "${!fields[@]}"
    do
      log_info "--- ${fields[$fi]}: ${COLOR_YELLOW}${fvalues[$fi]}${COLOR_NONE}"
    done
    local sfound
    sfound=$(cf services | sed -e 1,3d | grep "^${fvalues[0]}" | cat)
    if [[ -z "$sfound" ]]; then
    log_info "${smsg}service definition OK - proceed"
    tobesearched="/^${fvalues[0]}\$/p"
    sfound=`cf services | sed -e 1,3d | cut -d " " -f 1 | sed -ne ${tobesearched}`
    if [[ -z "${sfound}" ]]; then
      is_cups "${fvalues[1]}" "${fvalues[2]}"
      argfile=`mktemp`
      if [[ "$res_is_cups" = "CUPS" ]]; then
        cf cups "${fvalues[0]}" -p "$(echo "'${fvalues[4]}'" | tr -d ' ')" >/dev/null 2>&1
      else
        if [[ -z "${fvalues[3]}" ]]; then
          broker=""
        narg="-p "
      else
          broker="-b ${fvalues[3]}"
        narg="-c "
      fi
        local argfile
        argfile=`mktemp`
      if [[ -z "${fvalues[4]}" ]]; then
        params=""
      else
        echo "${fvalues[4]}" >"${argfile}"
          params="-c ${argfile}"
        params="${narg}${argfile}"
      fi
         cf create-service "${fvalues[1]}" "${fvalues[2]}" "${fvalues[0]}" "${broker}" "${params}" -w >/dev/null 2>&1
        rm -f ${argfile}
      if [[ "$res_is_cups" = "CUPS" ]]; then
        cf cups ${fvalues[0]} ${params} >/dev/null
      else
        if [[ -z "${fvalues[3]}" ]]; then
          broker=""
        else
          broker="-b ${fvalues[3]}"
        fi
        cf create-service ${fvalues[1]} ${fvalues[2]} ${fvalues[0]} ${broker} ${params} -w >/dev/null
      fi
      rm -f ${argfile}
      log_info "${COLOR_GREEN}... created${COLOR_NONE}"
    else
      log_info "${COLOR_BLUE}... service instance \"${fvalues[0]}\" already exist - skipped${COLOR_NONE}"
@@ -865,7 +863,7 @@ stages:
    *)
      smsg="Delete service instance ${COLOR_YELLOW}${service_desc}${COLOR_NONE}: "
      get_desc_field $service_desc "cfServiceName" "true"
      if [[ ! -z "$desc_field_error" ]]; then
      if [[ ! -z "${desc_field_error}" ]]; then
        log_info "${smsg}${COLOR_RED}KO - skipped${COLOR_NONE} (${COLOR_BLUE}${desc_field_error}${COLOR_NONE})"
        manage_services_errors=$((manage_services_errors+1))
        return
@@ -873,11 +871,12 @@ stages:
      name=$(echo ${desc_field} | tr -d '\r')
      log_info "${smsg}service definition OK - proceed"
      log_info "--- cfServiceName: ${COLOR_YELLOW}${name}${COLOR_NONE}"
      sfound=$(cf services | sed -e 1,3d | grep "^${name}" | cat)
      if [[ -z "$sfound" ]]; then
      tobesearched="/^${name}\$/p"
      sfound=`cf services | sed -e 1,3d | cut -d " " -f 1 | sed -ne ${tobesearched}`
      if [[ -z "${sfound}" ]]; then
        log_info "${COLOR_BLUE}.... service instance \"${name}\" does not exist - skipped${COLOR_NONE}"
      else
        cf delete-service ${name} -f >/dev/null 2>&1
        cf delete-service ${name} -f >/dev/null
        log_info "${COLOR_GREEN}.... deleted${COLOR_NONE}"
      fi
      ;;
@@ -885,7 +884,7 @@ stages:
  }

  function manage_services() {
    mgt_action=${1}
    local mgt_action=${1}
    export manage_services_errors
    manage_services_errors=0
    # service_files service descriptors for service instances to be created before deployment
@@ -899,11 +898,9 @@ stages:
    if [[ -z "$service_files" ]]
    then
      log_info "No service found to manage, expected files are: \\e[33;1m${service_dir}/*.cf-service.json\\e[0m"
      return 0
    else
      log_info "Services to be deleted: \\e[33;1m${service_files}\\e[0m"
    fi
    for service_desc in $service_files
      log_info "Services to be managed: \\e[33;1m${service_files}\\e[0m"
      for service_desc in ${service_files}
      do
        case ${mgt_action} in
        create)
@@ -914,9 +911,6 @@ stages:
          ;;
        esac
      done
    if (( manage_services_errors > 0 )); then
      log_info "${COLOR_RED}Errors while managing service instances - aborted${COLOR_NONE}"
      return 1
    fi
  }

@@ -971,6 +965,7 @@ stages:
    # use $CI_ENVIRONMENT_SLUG for appname to avoid service name constraints (<=50 chars)
    # use $CI_ENVIRONMENT_SLUG for hostname to avoid constraints (<=63 chars)
    - manage_services "create"
    - echo "Manage_services terminated with ${manage_services_errors} errors"
    - deploy "$ENV_TYPE" "${ENV_ZERODOWNTIME:-false}" "${ENV_APP_NAME:-${CF_BASE_APP_NAME}${ENV_APP_SUFFIX}}" "${ENV_HOST_NAME:-${CF_BASE_APP_NAME}${ENV_APP_SUFFIX}}" "${ENV_DOMAIN:-${CF_DEFAULT_DOMAIN}}" "${ENV_ROUTE_PATH:-${CF_DEFAULT_ROUTE_PATH}}"
  artifacts:
    name: "$ENV_TYPE env url or cf logs for $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"