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 deleted file mode 100644 index 7510a14..0000000 --- a/bin/config +++ /dev/null @@ -1,39 +0,0 @@ -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") - -if [[ $aws_access_key_id && $aws_secret_access_key ]]; then - export AWS_ACCESS_KEY_ID=$aws_access_key_id - export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key - export AWS_DEFAULT_REGION=$aws_region -fi - -namespace=$(echo "$config" | jq -r ".source.namespace") - -if [ -z "$namespace" ]; then - log_error "mandatory configuration 'source.namespace' not set" - exit 1 -fi - -metric=$(echo "$config" | jq -r ".source.metric") -if [ -z "$metric" ]; then - log_error "mandatory configuration 'source.metric' not set" - exit 1 -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" - -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 [ "$dimensions_provided" ]; then - dimensions="${dimensions},${dimensions_provided}" -fi - -version=$(echo "$config" | jq -r ".version") diff --git a/bin/out b/bin/out index b2ef29e..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") -# load config -. "$cwd"/config +# 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") -export AWS_PAGER="" +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 -aws cloudwatch put-metric-data --namespace "$namespace" --metric-name "$metric" --value "$value" --unit "$unit" --dimensions "$dimensions" +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 + +export AWS_PAGER="" -log "placed AWS cloudwatch metric '${metric}' to namespace '${namespace}', dimensions: ${dimensions}" +# 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}" -echo "{\"version\":{\"ref\":\"${version}\"}}" -exit 0 +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 f163146..e4ef131 100644 --- a/test/stdin.json +++ b/test/stdin.json @@ -2,15 +2,14 @@ "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 + "foo" : "bar", + "fuzz" : "buzz" + } } } \ 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