From b2c86be893c17b6c55b2a82641d594c79117f2a0 Mon Sep 17 00:00:00 2001 From: Olivier ORAND Date: Fri, 30 Nov 2018 10:47:31 +0100 Subject: [PATCH] TODO: - document usage in reference dataset - add minimal tests WIP-tasks(cf_push): support variable substitution for cf apps a file ending with `vars.yml` in application template dir is detected as file with `variables` to be injected into application manifest. fix #188 --- concourse/tasks/cf_push.yml | 1 + .../generic-app/enable-cf-app.yml | 2 +- .../generic-app/template/index.html | 2 +- scripts/cf/push.sh | 43 +++++++++++++------ spec/tasks/cf_push/task_spec.rb | 25 ++++++----- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/concourse/tasks/cf_push.yml b/concourse/tasks/cf_push.yml index ed204f446..0ff08e67a 100644 --- a/concourse/tasks/cf_push.yml +++ b/concourse/tasks/cf_push.yml @@ -19,3 +19,4 @@ params: SECRETS_DIR: CUSTOM_SCRIPT_DIR: CF_MANIFEST: + VARS_FILES_SUFFIX: vars.yml diff --git a/docs/reference_dataset/config_repository/hello-world-root-depls/cf-apps-deployments/generic-app/enable-cf-app.yml b/docs/reference_dataset/config_repository/hello-world-root-depls/cf-apps-deployments/generic-app/enable-cf-app.yml index a2176b5d0..a2c8a1436 100644 --- a/docs/reference_dataset/config_repository/hello-world-root-depls/cf-apps-deployments/generic-app/enable-cf-app.yml +++ b/docs/reference_dataset/config_repository/hello-world-root-depls/cf-apps-deployments/generic-app/enable-cf-app.yml @@ -1,6 +1,6 @@ cf-app: generic-app: - cf_api_url: https://my-cloudfroundry.org + cf_api_url: https://api.run.pivotal.io cf_username: a-test-User cf_password: a-test-Password cf_organization: my-test-org diff --git a/docs/reference_dataset/template_repository/hello-world-root-depls/cf-apps-deployments/generic-app/template/index.html b/docs/reference_dataset/template_repository/hello-world-root-depls/cf-apps-deployments/generic-app/template/index.html index 7d8ae2e8d..d67dfe5ba 100644 --- a/docs/reference_dataset/template_repository/hello-world-root-depls/cf-apps-deployments/generic-app/template/index.html +++ b/docs/reference_dataset/template_repository/hello-world-root-depls/cf-apps-deployments/generic-app/template/index.html @@ -1,5 +1,5 @@ - Hello World + Hello World, I'm COA sample app diff --git a/scripts/cf/push.sh b/scripts/cf/push.sh index edb3f3e0c..024960a54 100755 --- a/scripts/cf/push.sh +++ b/scripts/cf/push.sh @@ -23,13 +23,13 @@ validate(){ FAILURE=$((4 + $FAILURE)) fi - if [ $FAILURE -ne 0 ] + if [ ${FAILURE} -ne 0 ] then - exit $FAILURE + exit ${FAILURE} fi } -validate +#validate CURRENT_DIR=$(pwd) OUTPUT_DIR=${OUTPUT_DIR:-${CURRENT_DIR}/generated-files} @@ -38,34 +38,53 @@ ADDITIONAL_RESSOURCE=${ADDITIONAL_RESSOURCE:-additional-resource} CF_MANIFEST=${CF_MANIFEST:-manifest.yml} +API_OPTIONS="--skip-ssl-validation" +echo "copying file from $ADDITIONAL_RESSOURCE to $OUTPUT_DIR" +cp -r "${ADDITIONAL_RESSOURCE}/." "${OUTPUT_DIR}/" + +VARS_FILES="" +ls "${OUTPUT_DIR}/*" +for a_vars_file in $(ls "${OUTPUT_DIR}/*${VARS_FILES_SUFFIX}"); do + VARS_FILES="${VARS_FILES} --vars-file ${a_vars_file}" +done + +echo "Vars files detected: <${VARS_FILES}>" -API_OPTIONS="--skip-ssl-validation" #TODO add an option to manage ssl validation +cf --version cf api "$CF_API_URL" $API_OPTIONS cf auth "$CF_USERNAME" "$CF_PASSWORD" -echo "copying file from $ADDITIONAL_RESSOURCE to $OUTPUT_DIR" -cp -r $ADDITIONAL_RESSOURCE/. $OUTPUT_DIR/ if [ -n "$CUSTOM_SCRIPT_DIR" -a -f "$CUSTOM_SCRIPT_DIR/pre-cf-push.sh" ] then echo "pre CF push script detected" - chmod +x $CUSTOM_SCRIPT_DIR/pre-cf-push.sh - GENERATE_DIR=$OUTPUT_DIR BASE_TEMPLATE_DIR=$CUSTOM_SCRIPT_DIR $CUSTOM_SCRIPT_DIR/pre-cf-push.sh + chmod +x ${CUSTOM_SCRIPT_DIR}/pre-cf-push.sh + GENERATE_DIR="${OUTPUT_DIR}" BASE_TEMPLATE_DIR="${CUSTOM_SCRIPT_DIR}" "${CUSTOM_SCRIPT_DIR}/pre-cf-push.sh" else echo "ignoring pre CF push. No $CUSTOM_SCRIPT_DIR/pre-cf-push.sh detected" fi +VARS_FILES="" +for a_vars_file in $(ls "./${OUTPUT_DIR}/*${VARS_FILES_SUFFIX}"); do + VARS_FILES="${VARS_FILES} --vars-file ${a_vars_file}" +done + +echo "Vars files detected: <${VARS_FILES}>" + + cf target -o "$CF_ORG" -s "$CF_SPACE" set +e -cf push -f ${CF_MANIFEST} |tee /tmp/cf-push.log +# we need VARS_FILES to be expanded with spaces +# shellcheck disable=SC2086 +cf push -f "${CF_MANIFEST}" ${VARS_FILES}|tee /tmp/cf-push.log ret_code=$? -if [ $ret_code -ne 0 ] +if [ ${ret_code} -ne 0 ] then DISPLAY_LOG_CMD=$(grep "TIP: use 'cf logs" /tmp/cf-push.log |cut -d\' -f2) - eval $DISPLAY_LOG_CMD - exit $ret_code + eval ${DISPLAY_LOG_CMD} + exit ${ret_code} fi diff --git a/spec/tasks/cf_push/task_spec.rb b/spec/tasks/cf_push/task_spec.rb index fbe97c429..b3d0ee51e 100644 --- a/spec/tasks/cf_push/task_spec.rb +++ b/spec/tasks/cf_push/task_spec.rb @@ -1,7 +1,6 @@ -# encoding: utf-8 -# require 'spec_helper.rb' require 'yaml' require 'tmpdir' +require_relative '../task_spec_helper' describe 'cf push task' do @@ -15,23 +14,23 @@ before(:context) do generated_files = Dir.mktmpdir - # @output = execute('-c concourse/tasks/cf_push.yml ' \ - # '-i scripts-resource=. ' \ - # '-i templates-resource=spec/tasks/cf_push/template-resource ' \ - # '-i credentials-resource=spec/tasks/cf_push/credentials-resource ' \ - # '-i additional-resource=spec/tasks/cf_push/additional-resource ' \ - # "-o generated-files=#{generated_files} ", - # 'CUSTOM_SCRIPT_DIR' =>'', - # 'SECRETS_DIR' => '' ) + @output = execute('-c concourse/tasks/cf_push.yml ' \ + '-i scripts-resource=. ' \ + '-i templates-resource=spec/tasks/cf_push/template-resource ' \ + '-i credentials-resource=spec/tasks/cf_push/credentials-resource ' \ + '-i additional-resource=spec/tasks/cf_push/additional-resource ' \ + "-o generated-files=#{generated_files} ", + 'CUSTOM_SCRIPT_DIR' =>'', + 'SECRETS_DIR' => '' ) end after(:context) do FileUtils.rm_rf generated_files end - it 'displays an ignore message' - # expect(@output).to include('ignoring pre CF push. No /pre-cf-push.sh detected') - + it 'displays an ignore message' do + expect(@output).to include('ignoring pre CF push. No /pre-cf-push.sh detected') + end end context 'when a custom pre-push script script is detected' do