Skip to content

Commit

Permalink
Merge pull request #23 from idrop/rework-of-out
Browse files Browse the repository at this point in the history
rework of out
  • Loading branch information
idrop authored Feb 15, 2021
2 parents f190c89 + 70c24c7 commit aabadc0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
39 changes: 0 additions & 39 deletions bin/config

This file was deleted.

90 changes: 67 additions & 23 deletions bin/out
Original file line number Diff line number Diff line change
@@ -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
9 changes: 4 additions & 5 deletions test/stdin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
15 changes: 5 additions & 10 deletions test/test-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -34,9 +34,4 @@ jobs:
- |
exit 1
on_failure:
timeout: 1m
put: cloudwatch-metrics-fails
params:
dimensions:
foo: bar
fuzz: buzz

0 comments on commit aabadc0

Please sign in to comment.