From 55692691c0fedf18a8b89631a6df4dfa3cf351fc Mon Sep 17 00:00:00 2001 From: Phil Miller Date: Mon, 15 Feb 2021 15:41:51 +0000 Subject: [PATCH 1/2] wip --- Dockerfile | 2 +- bin/config | 73 +++++++++++++++++++++++++++-------------- bin/out | 8 ++--- test/stdin.json | 9 +---- test/test-pipeline.yaml | 15 +++------ 5 files changed, 60 insertions(+), 47 deletions(-) mode change 100644 => 100755 bin/config diff --git a/Dockerfile b/Dockerfile index 9c52fa6..9b47c8b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,4 +3,4 @@ RUN yum install jq -y COPY bin /opt/resource RUN chmod +x /opt/resource/check /opt/resource/config /opt/resource/in /opt/resource/out RUN rm -rf /tmp/* -CMD ["--version"] +ENTRYPOINT [ "/bin/bash" ] diff --git a/bin/config b/bin/config old mode 100644 new mode 100755 index 7510a14..fa1c064 --- a/bin/config +++ b/bin/config @@ -1,39 +1,64 @@ -config=$(cat) - -#echo $config > /tmp/source_vars - -aws_access_key_id=$(echo "$config" | jq -r ".source.access_key_id") -aws_secret_access_key=$(echo "$config" | jq -r ".source.secret_access_key") -aws_region=$(echo "$config" | jq -r ".source.region_name") +function parse_aws_credentials() { + aws_access_key_id=$(echo "$config" | jq -r '.source.aws_access_key_id') + aws_secret_access_key=$(echo "$config" | jq -r '.source.aws_secret_access_key') + aws_region=$(echo "$config" | jq -r '.source.aws_region') -if [[ $aws_access_key_id && $aws_secret_access_key ]]; then + if [ "${aws_access_key_id}" != null ]; then export AWS_ACCESS_KEY_ID=$aws_access_key_id + fi + + if [ "${aws_secret_access_key}" != null ]; then export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key + fi + + if [ "${aws_region}" != null ]; then export AWS_DEFAULT_REGION=$aws_region -fi + fi +} -namespace=$(echo "$config" | jq -r ".source.namespace") +function parse_namespace() { + namespace=$(echo "$config" | jq -r ".source.namespace") -if [ -z "$namespace" ]; then + if [ "${namespace}" == null ]; then log_error "mandatory configuration 'source.namespace' not set" exit 1 -fi + fi +} -metric=$(echo "$config" | jq -r ".source.metric") -if [ -z "$metric" ]; then +function parse_metric() { + metric=$(echo "$config" | jq -r ".source.metric") + if [ "${metric}" == null ]; then log_error "mandatory configuration 'source.metric' not set" exit 1 -fi + fi +} -value=$(echo "$config" | jq -r '.params | if has("value") then .value else 1 end') -#unit=$(echo "$config" | jq -r '.params | if has("unit") then .unit else "Count" end') -unit="Count" +function parse_dimensions() { + + # default dimensions from metadata + dimensions="build_job_name=${BUILD_JOB_NAME},build_pipeline_name=${BUILD_PIPELINE_NAME},build_team_name=$BUILD_TEAM_NAME" -dimensions="build_job_name=${BUILD_JOB_NAME},build_pipeline_name=${BUILD_PIPELINE_NAME},build_team_name=$BUILD_TEAM_NAME" -dimensions_provided=$(echo "$config" | jq -r '.params.dimensions | to_entries | map([.key,.value] | join("=")) | join(",")') + if [ $(echo "$config" | jq -r '.params | has("dimensions")') ]; then + dimensions_provided=$(echo "$config" | jq -r '.params.dimensions | to_entries | map([.key,.value] | join("=")) | join(",")') + if [ "$dimensions_provided" != null ]; then + dimensions="${dimensions},${dimensions_provided}" + fi + fi +} -if [ "$dimensions_provided" ]; then - dimensions="${dimensions},${dimensions_provided}" -fi +function parse_value() { + value=1 + if [ $(echo $config | jq -r 'has("params")') ]; then + value=$(echo "$config" | jq -r '.params | if has("value") then .value else 1 end') + fi +} -version=$(echo "$config" | jq -r ".version") +config=$(cat) +parse_aws_credentials +parse_namespace +parse_metric +log "b4 dims" +parse_dimensions +log "after dims" +parse_value +unit="Count" diff --git a/bin/out b/bin/out index b2ef29e..1768ab1 100755 --- a/bin/out +++ b/bin/out @@ -23,11 +23,11 @@ cd "$1" # load config . "$cwd"/config - +log "here5" export AWS_PAGER="" - -aws cloudwatch put-metric-data --namespace "$namespace" --metric-name "$metric" --value "$value" --unit "$unit" --dimensions "$dimensions" - +log "dimensions: ${dimensions}" +aws cloudwatch put-metric-data --namespace "$namespace" --metric-name "$metric_name" --value "$value" --unit "$unit" --dimensions "$dimensions" +log "here6" log "placed AWS cloudwatch metric '${metric}' to namespace '${namespace}', dimensions: ${dimensions}" echo "{\"version\":{\"ref\":\"${version}\"}}" diff --git a/test/stdin.json b/test/stdin.json index f163146..9ac143b 100644 --- a/test/stdin.json +++ b/test/stdin.json @@ -2,15 +2,8 @@ "source": { "aws_access_key_id": "$ACCESS_KEY_ID", "aws_secret_access_key": "$SECRET_ACCESS_KEY", - "region": "eu-west-2", + "aws_region": "eu-west-2", "namespace": "TestNamespace", "metric": "job_fails" - }, - "params": { - "dimensions": { - "foo": "bar", - "fuzz": "buzz" - }, - "value": 0 } } \ No newline at end of file diff --git a/test/test-pipeline.yaml b/test/test-pipeline.yaml index a84273c..173506d 100644 --- a/test/test-pipeline.yaml +++ b/test/test-pipeline.yaml @@ -4,16 +4,16 @@ resource_types: - name: cloudwatch-metrics type: docker-image source: - repository: ghcr.io/idrop/cloudwatch-metrics - tag: 0.0.0.3 + repository: ghcr.io/idrop/cloudwatch-metrics/cloudwatch-metrics + tag: 0.0.9 resources: - name: cloudwatch-metrics-fails type: cloudwatch-metrics source: - region_name: eu-west-2 - access_key_id: ((aws_access_key_id)) - secret_access_key: ((aws_secret_access_key)) + aws_region: eu-west-2 + aws_access_key_id: ((aws_access_key_id)) + aws_secret_access_key: ((aws_secret_access_key)) namespace: phil metric: fails @@ -34,9 +34,4 @@ jobs: - | exit 1 on_failure: - timeout: 1m put: cloudwatch-metrics-fails - params: - dimensions: - foo: bar - fuzz: buzz \ No newline at end of file From 70c24c74c7ffe132222464b847793982163b6a70 Mon Sep 17 00:00:00 2001 From: Phil Miller Date: Mon, 15 Feb 2021 22:23:36 +0000 Subject: [PATCH 2/2] rework of out --- bin/config | 64 --------------------------------- bin/out | 94 ++++++++++++++++++++++++++++++++++++------------- test/stdin.json | 6 ++++ 3 files changed, 75 insertions(+), 89 deletions(-) delete mode 100755 bin/config diff --git a/bin/config b/bin/config deleted file mode 100755 index fa1c064..0000000 --- a/bin/config +++ /dev/null @@ -1,64 +0,0 @@ -function parse_aws_credentials() { - aws_access_key_id=$(echo "$config" | jq -r '.source.aws_access_key_id') - aws_secret_access_key=$(echo "$config" | jq -r '.source.aws_secret_access_key') - aws_region=$(echo "$config" | jq -r '.source.aws_region') - - if [ "${aws_access_key_id}" != null ]; then - export AWS_ACCESS_KEY_ID=$aws_access_key_id - fi - - if [ "${aws_secret_access_key}" != null ]; then - export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key - fi - - if [ "${aws_region}" != null ]; then - export AWS_DEFAULT_REGION=$aws_region - fi -} - -function parse_namespace() { - namespace=$(echo "$config" | jq -r ".source.namespace") - - if [ "${namespace}" == null ]; then - log_error "mandatory configuration 'source.namespace' not set" - exit 1 - fi -} - -function parse_metric() { - metric=$(echo "$config" | jq -r ".source.metric") - if [ "${metric}" == null ]; then - log_error "mandatory configuration 'source.metric' not set" - exit 1 - fi -} - -function parse_dimensions() { - - # default dimensions from metadata - dimensions="build_job_name=${BUILD_JOB_NAME},build_pipeline_name=${BUILD_PIPELINE_NAME},build_team_name=$BUILD_TEAM_NAME" - - if [ $(echo "$config" | jq -r '.params | has("dimensions")') ]; then - dimensions_provided=$(echo "$config" | jq -r '.params.dimensions | to_entries | map([.key,.value] | join("=")) | join(",")') - if [ "$dimensions_provided" != null ]; then - dimensions="${dimensions},${dimensions_provided}" - fi - fi -} - -function parse_value() { - value=1 - if [ $(echo $config | jq -r 'has("params")') ]; then - value=$(echo "$config" | jq -r '.params | if has("value") then .value else 1 end') - fi -} - -config=$(cat) -parse_aws_credentials -parse_namespace -parse_metric -log "b4 dims" -parse_dimensions -log "after dims" -parse_value -unit="Count" diff --git a/bin/out b/bin/out index 1768ab1..5fa3d73 100755 --- a/bin/out +++ b/bin/out @@ -1,34 +1,78 @@ #!/usr/bin/env bash -set -e -red='\033[0;31m' -green='\033[0;32m' -nc='\033[0m' +set -eu +set -o pipefail -log() { - args=("$@") - # shellcheck disable=SC2145 - echo -e "${green} ${args[@]}${nc}" 1>&2 -} +exec 3>&1 # make stdout available as fd 3 for the result +exec 1>&2 # redirect all output to stderr for logging -log_err() { - args=("$@") - # shellcheck disable=SC2145 - echo -e "${red} ${args[@]}${nc}" 1>&2 -} +source=$1 -cwd="$(cd "$(dirname "$0")" && pwd)" +# Read inputs +#source=$1 +payload=$(mktemp -t cloudwatch-metrics-out-request-payload.XXXXXX) +cat >"$payload" <&0 -cd "$1" +# Parse source config +aws_access_key_id=$(jq -r '.source.aws_access_key_id // ""' <"$payload") +aws_secret_access_key=$(jq -r '.source.aws_secret_access_key // ""' <"$payload") +aws_region=$(jq -r '.source.aws_region // ""' <"$payload") +namespace=$(jq -r '.source.namespace // ""' <"$payload") +metric=$(jq -r '.source.metric // ""' <"$payload") + +# Parse param config +value=$(jq -r '.params.value // "1"' <"$payload") +unit=$(jq -r '.params.unit // "Count"' <"$payload") +dimensions_provided=$(jq -r '.params.dimensions // empty | to_entries | map([.key,.value] | join("=")) | join(",")' <"$payload") + +if [ -n "$aws_access_key_id" ]; then + export AWS_ACCESS_KEY_ID=$aws_access_key_id +fi + +if [ -n "$aws_secret_access_key" ]; then + export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key +fi + +if [ -n "$aws_region" ]; then + export AWS_DEFAULT_REGION=$aws_region +fi + +if [ -z "$namespace" ]; then + echo "mandatory configuration 'source.namespace' not set" + exit 1 +fi + +if [ -z "$metric" ]; then + echo "mandatory configuration 'source.metric' not set" + exit 1 +fi + +re='^[+-]?[0-9]+([.][0-9]+)?$' +if ! [[ $value =~ $re ]] ; then + echo "optional configuration params.value is not a number (value is ${value})" + exit 1 +fi + +dimensions="build_job_name=${BUILD_JOB_NAME},build_pipeline_name=${BUILD_PIPELINE_NAME},build_team_name=${BUILD_TEAM_NAME}" + +if [ -n "$dimensions_provided" ]; then + dimensions="${dimensions},${dimensions_provided}" +fi -# load config -. "$cwd"/config -log "here5" export AWS_PAGER="" -log "dimensions: ${dimensions}" -aws cloudwatch put-metric-data --namespace "$namespace" --metric-name "$metric_name" --value "$value" --unit "$unit" --dimensions "$dimensions" -log "here6" -log "placed AWS cloudwatch metric '${metric}' to namespace '${namespace}', dimensions: ${dimensions}" -echo "{\"version\":{\"ref\":\"${version}\"}}" -exit 0 +# see https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/put-metric-data.html +aws cloudwatch put-metric-data --namespace "$namespace" --metric-name "$metric" --value "$value" --unit "$unit" --dimensions "${dimensions}" +msg="placed AWS cloudwatch metric '${metric}' to namespace '${namespace}', with dimensions: ${dimensions}" + +jq -n "{ + version: { + ref: \"none\", + }, + metadata: [ + { + name: \"message\", + value: $(echo $msg | jq -R .), + } + ] +}" >&3 diff --git a/test/stdin.json b/test/stdin.json index 9ac143b..e4ef131 100644 --- a/test/stdin.json +++ b/test/stdin.json @@ -5,5 +5,11 @@ "aws_region": "eu-west-2", "namespace": "TestNamespace", "metric": "job_fails" + }, + "params": { + "dimensions": { + "foo" : "bar", + "fuzz" : "buzz" + } } } \ No newline at end of file