从一个应用程序到另一个应用程序的Cloud Foundry复制路线

时间:2018-08-31 10:27:25

标签: java bash shell cloudfoundry pivotal-cloud-foundry

Cloud Foundry是否可以在进行蓝绿色部署时将丢失的路由从一个应用复制到另一个应用?

我有一个几乎没有手动添加路由的应用程序,而在进行蓝绿色部署(通过脚本自动进行)的同时,我想将丢失/手动添加的路由复制到新应用程序中。有可能吗?

脚本:

#!/bin/bash

path="C:/Users/.../Desktop/cf_through_sh/appName.jar"

spaceName="development"
appBlue="appName"
appGreen="${appName}-dev"
manifestFile="C:/Users/.../Desktop/cf_through_sh/manifest-dev.yml"
domains=("domain1.com" "domain2.com")
appHosts=("host-v1" "host-v2")

evaluate_return_code (){
  ret=$1
  if [[ $ret != 0 ]]
  then
    exit $ret
  fi
}

switch_to_target_space() {
    space="development"
    echo "Change space to ${space}"
    cf t -s ${space}
    evaluate_return_code $?
}

push_new_release() {
    appGreen=$1
    if [ ! -f "${manifestFile}" ]; then
        echo "Missing manifest: ${manifestFile}";
        exit 1;
    fi

    if [ ! -f "${path}" ]; then
        echo "Missing artifact: ${path}";
        exit 1;
    fi
    echo "Deploying ${path} as ${appGreen}"
    cf push ${appGreen} -f ${manifestFile} -p ${path} --no-route
    evaluate_return_code $?
}

map_routes() {
    app=$1
    domains=$2
    shift
    appHosts=$3

    for  host in  ${appHosts[*]}; do
        echo "Mapping ${host} to ${app}"
        for  domain in  ${domains[*]}; do
            cf map-route ${app} ${domain} -n ${host}
            evaluate_return_code $?
        done
    done
}


unmap_routes() {
    app=$1
    domains=$2
    shift
    appHosts=$3

    for  host in  ${appHosts[*]}; do
        echo "Unmapping ${host} from ${app}"
        for  domain in  ${domains[*]}; do
            cf unmap-route ${app} ${domain} -n ${host}
        evaluate_return_code $?
        done
    done
}

rename_app() {
    oldName=$1
    newName=$2
    echo "Renaming ${oldName} to ${newName}"
    cf rename ${oldName} ${newName}
    evaluate_return_code $?
}

switch_names() {
    appBlue=$1
    appGreen=$2
    appTemp="${appBlue}-old"
    rename_app ${appBlue} ${appTemp}
    rename_app ${appGreen} ${appBlue}
    rename_app ${appTemp} ${appGreen}
}

stop_old_release() {
    echo "Stopping old ${appGreen} app"
    cf stop ${appGreen}
    evaluate_return_code $?
}

switch_to_target_space ${spaceName}
push_new_release ${appGreen}
map_routes ${appGreen} ${domains[*]} ${appHosts[*]}
unmap_routes ${appBlue} ${domains[*]} ${appHosts[*]}
switch_names ${appBlue} ${appGreen}
stop_old_release

echo "DONE"
exit 0;

例如: appblue有5卢比

 1. host-v1.domain1.com
 2. host-v2.domain1.com
 3. host-v1.domain2.com
 4. host-v2.domain2.com
 5. manual-add.domain1.com //manually added route through admin UI

通过脚本应用程序部署蓝绿色后,仅包含4条路由

 1. host-v1.domain1.com
 2. host-v2.domain1.com
 3. host-v1.domain2.com
 4. host-v2.domain2.com

如何复制缺少的第五条路线?我不想通过脚本传递主机 manual-add ,因为它是手动添加的。

通常,如果未映射,是否可以将路由从一个应用复制到另一应用?

1 个答案:

答案 0 :(得分:1)

这只能通过Jenkins(或任何CI-CD工具)完成。我们在本例中所做的是,我们有一个CF-Manifest-Template.ymlCF-Manifest-settings.json,并且有一个gradle任务,该任务将应用JSON中的设置并填充Manifest-temple并生成一个cf-manifest-generated.yml < / p>

gradle文件将具有一个任务,该任务将通过使用此生成的清单文件进行蓝绿色部署,并且所有路由都将被硬编码在清单文件中。这是这样做的标准方法。

但是,如果要从运行在Cloud Foundry中的应用程序复制路由并将此路由复制到另一个应用程序,则需要编写一个REST Client连接到Cloud Foundry CloudController并获取App-A的所有路由,然后创建到APP-B

的路由

这很简单!

编写执行此命令的REST客户端

  

cf应用APP-A

这将带回APP-A的详细信息作为JSON响应。响应将具有这些参数

Showing health and status for app APP-A in org Org-A / space DEV as arun2381985@yahoo.com...

name:              APP-A
requested state:   started
instances:         1/1
usage:             1G x 1 instances
routes:            ********
last uploaded:     Sat 25 Aug 00:25:45 IST 2018
stack:             cflinuxfs2
buildpack:         java_buildpack

阅读此JSON响应并收集APP-A的路由,然后将其映射到APP-B。它非常简单