Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support latest broker version and improve upgrades - WIP #21

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
21 changes: 11 additions & 10 deletions config/blobs.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
---
cf_cli/cf-cli_6.23.1_linux_x86-64.tgz:
object_id: 6c2e620a-b1f7-4f0e-bf53-f80fa60c403a
sha: !binary |-
a195e99cae5ab17b453b0a0747508c8f3eee5cae
size: 6692717
cf_cli/autopilot_plugin.tgz:
size: 3739561
object_id: 43e16160-163e-4e65-5177-59bc5c99565a
sha: b03815744c0fe801508e4f9ccc6c3c442658ddd3
cf_cli/cf-cli_6.34.1_linux_x86-64.tgz:
size: 5879397
object_id: 33a6b692-2a33-4765-6d04-bf02dd034635
sha: ab0ddec89deeacee58e26ae4469c683e61be2325
ecs_service_broker/ecs-cf-service-broker.jar:
object_id: dec7ebc0-106e-4933-8e3e-cf2aab17ad21
sha: !binary |-
1de959fd74aba2561f1aecfc534be5345e601c75
size: 24201258
size: 25981159
object_id: 4f2f4322-9a02-46fc-4a2f-83915fa0d4a4
sha: eea0d4982bf9256c1111a8f2019cb709431be5d6
9 changes: 5 additions & 4 deletions jobs/deploy-service-broker/spec
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ templates:
deploy.sh.erb: bin/run
ecs_cert.pem.erb: ecs_cert.pem
application.yml.erb: application.yml
manifest.yml.erb: manifest.yml
packages:
- cf_cli
- ecs_service_broker
Expand All @@ -18,13 +19,13 @@ properties:
description: 'UAA Client Secret'
app_name:
description: 'Name of the Service Broker Application'
app_version:
description: 'Version of the Service Broker Application'
app_uri:
description: 'Uri of the Service Broker Application'
create_open_security_group:
description: 'Open security group for the app to access outside'
default: false
app_organization:
description: CF Organization in which to deploy the Broker App
app_space:
description: CF Space in which to deploy the Broker App
org_quota:
description: 'Org Memory Quota for the Service Broker Application'
default: 1024
Expand Down
13 changes: 2 additions & 11 deletions jobs/deploy-service-broker/templates/application.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ broker:
prefix: <%= properties.prefix %>
<% end -%>
repository-service-id: <%= repo_svc.service_guid %>
repository-plan-id: <%= repo_plan.plan_guid %>
repository-plan-id: <%= repo_plan.guid %>
security:
<%= properties.security_username %>:
password: <%= properties.security_password %>
Expand Down Expand Up @@ -77,15 +77,6 @@ catalog:
<% unless s.default_retention.to_s.empty? -%>
default-retention: <%= s.default_retention %>
<% end -%>
<% unless s.quota_limit.to_s.empty? && s.quota_warn.to_s.empty? -%>
quota:
<% unless s.quota_limit.to_s.empty? -%>
limit: <%= s.quota_limit %>
<% end -%>
<% unless s.quota_warn.to_s.empty? -%>
warn: <%= s.quota_warn %>
<% end -%>
<% end -%>
<% unless s.domain_group_admins.to_s.empty? -%>
domain-group-admins:
<% s.domain_group_admins do |a| %>
Expand Down Expand Up @@ -127,7 +118,7 @@ catalog:
<% unless s.plans.empty? -%>
plans:
<% s.plans.each do |p| -%>
- id: <%= p.plan_guid %>
- id: <%= p.guid %>
name: <%= p.name %>
description: <%= p.description %>
free: <%= p.free %>
Expand Down
188 changes: 82 additions & 106 deletions jobs/deploy-service-broker/templates/deploy.sh.erb
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
#!/bin/bash
set -e -x

<%
def grab_app_domain_name
app_domains_str = properties.app_domains.to_s
array_start = ( app_domains_str =~ /^\[/ )
if (array_start == 0)
app_domain_entry = app_domains_str.gsub(/^\[/, '').gsub(/\]/,'').gsub(/,.*/, '')
else
app_domain_entry = app_domains_str
end
app_domain_entry
#!/bin/bash -exu

<%# TODO support variable instance count %>

<%
def grab_app_domain_name
app_domains_str = properties.app_domains.to_s
array_start = ( app_domains_str =~ /^\[/ )
if (array_start == 0)
app_domain_entry = app_domains_str.gsub(/^\[/, '').gsub(/\]/,'').gsub(/,.*/, '')
else
app_domain_entry = app_domains_str
end
app_domain = grab_app_domain_name
-%>
app_domain_entry
end
app_domain = grab_app_domain_name
-%>

SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

export PATH="/var/vcap/packages/cf_cli/bin:$PATH"

export SCHEME=https
<%# TODO use credhub for password storage/retreival %>
export ADMIN_USER=<%= properties.cf.admin_user %>
export ADMIN_PASSWORD=<%= properties.cf.admin_password %>
export DOMAIN=<%= properties.domain %>
export APP_DOMAIN=<%="#{app_domain}"%>
export API_ENDPOINT=$SCHEME://api.${DOMAIN}
export APP_NAME=<%= properties.app_name %>
export APP_VERSION=<%= properties.app_version %>
export APP_URI=<%= properties.app_uri %>
export ORG=${APP_NAME}-service-org
export SPACE=${APP_NAME}-service-space
export QUOTA=${APP_NAME}-service-quota
export ORG=<%= properties.app_organization %>
export SPACE=<%= properties.app_space %>
export CF_TARGET=$API_ENDPOINT
export CF_ORG=$ORG
export CF_SPACE=$SPACE
export CF_USER=$ADMIN_USER
export CF_PASSWORD=$ADMIN_PASSWORD
export CF_SKIP_SSL=<%= properties.ssl.skip_cert_verify %>
export ORG_QUOTA=<%= properties.org_quota %>
export APP_PUSH_TIMEOUT=<%= properties.timeout %>
export APP_PUSH_MEMORY=<%= properties.memory %>

<%# TODO use credhub for password storage/retreival %>
export SECURITY_USERNAME=<%= properties.security_username %>
export SECURITY_PASSWORD=<%= properties.security_password %>
export BROKER_CERTIFICATE="<%= properties.certificate %>"

export CREATE_OPEN_SECURITY_GROUP=<%= properties.create_open_security_group %>
export DELETE_PREV_APP_INSTANCE=<%= properties.delete_prev_app_instance %>

export EXPLODED_APP_FOLDER="exploded-app-contents"

Expand Down Expand Up @@ -103,130 +100,109 @@ function create_open_security_group() {
}

function restart_app() {
CF_TRACE=true cf restart ${APP_NAME}-${APP_VERSION}
cf logs ${APP_NAME}-${APP_VERSION} --recent
CF_TRACE=true cf restart ${APP_NAME}
cf logs ${APP_NAME} --recent
}

function wait_till_running() {
for i in `seq 1 6`
do
CF_TRACE=true cf app ${APP_NAME}-${APP_VERSION} | grep RUNNING | grep ${APP_NAME}-${APP_VERSION}
CF_TRACE=true cf app ${APP_NAME} | grep running
if [ "$?" -ne "0" ]; then
log_debug "App not running yet..."
sleep 10
else
DEPLOYMENT_STATUS=0
log_debug "App now up and running ..."
break
fi
done
}

function modify_app() {
pushd $PACKAGE_PATH/$SUBDIR

mkdir -p ${EXPLODED_APP_FOLDER}

cd ${EXPLODED_APP_FOLDER}
unzip -o ${PACKAGE_PATH}/lib/${APP_PREFIX_NAME}*.${EXTENSION_TYPE}
cp "$SCRIPT_PATH/../ecs_cert.pem" ecs_cert.pem
cp "$SCRIPT_PATH/../application.yml" application.yml
zip -r ${PACKAGE_PATH}/lib/${TARGET_APP} *

popd
}

function delete_prev_app_instance() {
function push_app() {
local exit_code

cf apps | grep ${APP_NAME}-${APP_VERSION}
existing_app_check=$?
mkdir -p /var/vcap/data/tmp
export TMPDIR=/var/vcap/data/tmp

if [ "$existing_app_check" == "0" ]; then
pushd ${PACKAGE_PATH} > /dev/null
set +e
cf app $APP_NAME --guid
exit_code=$?
set -e

# Look for User-Provided env variables and stop after the first blank line after that
cf env ${APP_NAME}-${APP_VERSION} | awk 'f{print}/User-Provided/{f=1};/^ *$/{f=0}' \
| grep '.' | awk -F ':' '{print $1}' | while read env_variable_name
do
cf unset-env $env_variable_name
done
cf delete -f ${APP_NAME}-${APP_VERSION} > /dev/null
fi
if [[ $exit_code -ne 0 ]]; then
set +e
cf push "${APP_NAME}" -p ${PACKAGE_PATH}/lib/${TARGET_APP} -d ${APP_DOMAIN}
exit_code=$?
set -e

if [[ $exit_code -ne 0 ]]; then
cf logs "${APP_NAME}" --recent
exit "${exit_code}"
fi

else
cf install-plugin /var/vcap/packages/cf_cli/plugins/autopilot -f
set +e
cf zero-downtime-push "${APP_NAME}" -f manifest.yml -p ${PACKAGE_PATH}/lib/${TARGET_APP}
exit_code=$?
set -e
if [[ $exit_code -ne 0 ]]; then
handle_failed_blue_green_deploy
fi
fi
popd > /dev/null
}

function push_app() {
pushd $PACKAGE_PATH
mkdir -p /var/vcap/data/tmp
export TMPDIR=/var/vcap/data/tmp
function handle_failed_blue_green_deploy() {
DEPLOYMENT_STATUS=1
local notifications_state=$(cf curl /v2/apps/$(cf app $APP_NAME --guid)/stats | jq -r '.["0"].state')
local venerable_state=$(cf curl /v2/apps/$(cf app $APP_NAME-venerable --guid)/stats | jq -r '.["0"].state')

# TODO -- add back in the timeouts and quotas for the app push? Or remove them from the errand props?
# if venerable is up, and new app is down
if [[ $notifications_state = "DOWN" && $venerable_state != "DOWN" ]]; then
cf delete $APP_NAME -f
cf rename $APP_NAME-venerable $APP_NAME
fi

cf push ${APP_NAME}-${APP_VERSION} -p ${PACKAGE_PATH}/lib/${TARGET_APP} -n ${APP_URI} -d ${APP_DOMAIN} --no-start
cf logs ${APP_NAME}-${APP_VERSION} --recent
popd
# if venerable is up, and new app is up
if [[ $notifications_state != "DOWN" && $venerable_state != "DOWN" ]]; then
cf delete $APP_NAME-venerable -f
fi
}

function register_on_demand_plans() {
pushd $PACKAGE_PATH
mkdir -p /var/vcap/data/tmp
# Sleep for a bit so the app is up
sleep 30
app_endpoint=http://${SECURITY_USERNAME}:${SECURITY_PASSWORD}@${APP_URI}.${APP_DOMAIN}
app_catalog_url=${app_endpoint}/v2/catalog
catalog_output=`curl -X GET ${app_catalog_url} 2>/dev/null`
log_debug "Service Broker App Catalog output : $catalog_output"

service_id=`echo $catalog_output | sed -e 's/"//g' | sed -e "s/name:/\nname:/g" | grep "id:" | grep services | sed -e "s/^.*id://g;s/,//" `
#plan_id=`echo $catalog_output | sed -e 's/"//g' | sed -e "s/name:/\nname:/g" | grep "id:" | grep plans | sed -e "s/^.*id://g;s/,//" `
log_debug "Catalog Service Id : $service_id "

<% if properties.on_demand_service_plans %>
<% p("on_demand_service_plans").each do |plan|
plan_name=plan['plan_name']
plan_descrp=plan['plan_description']
plan_details=plan['plan_details'].gsub('\n','')
%>
export PLAN_ID=<%="#{plan_name}"%>
export PLAN_DESCRP=<%="#{plan_descrp}"%>
export PLAN_DETAILS='<%="#{plan_details}"%>'
log_debug "Service plan name ${PLAN_ID}, described as ${PLAN_DESCRP} and details: ${PLAN_DETAILS}"

bullets_name_value_multiline=`echo $PLAN_DETAILS | sed -e "s/;/,/g" | awk -F ":" '{print "\"" $1 "\" : \"" $2 "\", " }' `
bullets_value_descrp_singleline=`echo $PLAN_DETAILS | sed -e "s/;/,\n/g;" | awk -F ":" '{print "\""$2 $3"\"" }' | sed -e 's/,"/",/g' `

cat > /var/vcap/data/tmp/${PLAN_ID}.service_plan.json << EOF
{
"name" : "$PLAN_ID",
"description" : "$PLAN_DESCRP",
"metadata" : {
"name" : "$PLAN_ID",
"displayName" : "$PLAN_ID",
"description" : "$PLAN_DESCRP",
$bullets_name_value_multiline
"bullets" : [
"$PLAN_ID $PLAN_DESCRP",
$bullets_value_descrp_singleline ]
}
function print_deployment_status() {
if [[ $DEPLOYMENT_STATUS != 0 ]]; then
echo "Deployment failed!"
exit 1
fi

echo "Deployment succeeded!"
}
EOF

log_debug "Copied over default service plan file template with ${PLAN_ID} and updated it"
new_plan=`cat /var/vcap/data/tmp/${PLAN_ID}.service_plan.json`
log_debug "Contents of the plan: ${new_plan} "
plan_creation_output=`curl ${app_catalog_url}/services/${service_id}/plans -X POST -H "Content-Type:application/json" -d @/var/vcap/data/tmp/${PLAN_ID}.service_plan.json `
log_debug "Service Plan created!!"
log_debug "Service Plan details: ${plan_creation_output}"
<% end %>
<% end %>

popd

function cleanup_cf_home() {
if [[ -d "${CF_HOME}" ]]; then
rm -rf "${CF_HOME}"
fi
}

cf -v
authenticate_and_target
create_open_security_group
create_quota

modify_app
push_app

restart_app
wait_till_running
print_deployment_status
cleanup_cf_home
23 changes: 23 additions & 0 deletions jobs/deploy-service-broker/templates/manifest.yml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
<%
def grab_app_domain_name
app_domains_str = properties.app_domains.to_s
array_start = ( app_domains_str =~ /^\[/ )
if (array_start == 0)
app_domain_entry = app_domains_str.gsub(/^\[/, '').gsub(/\]/,'').gsub(/,.*/, '')
else
app_domain_entry = app_domains_str
end
app_domain_entry
end
app_domain = grab_app_domain_name
-%>

applications:
- name: <%= properties.app_name %>
command: bin/notifications
memory: 1G
domain: "<%= app_domain %>"
host: <%= properties.app_name %>
path: /var/vcap/packages/ecs_service_broker/ecs-cf-service-broker.jar
instances: 1
8 changes: 4 additions & 4 deletions jobs/destroy-broker/spec
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ properties:
description: 'CloudFoundry application domains'
app_name:
description: 'App Name'
app_version:
description: 'Version of the Service Broker Application'
app_uri:
description: 'Uri of the Service Broker Application'
app_organization:
description: CF Organization in which to deploy the Broker App
app_space:
description: CF Space in which to deploy the Broker App
cf.admin_user:
description: 'Username of the CF admin user'
cf.admin_password:
Expand Down
Loading