Compare commits
38 Commits
Author | SHA1 | Date | |
---|---|---|---|
de3df61608 | |||
208749d01e | |||
6124f0454b | |||
565cf4d664 | |||
db2d719f34 | |||
cd1165690a | |||
03a88e01b2 | |||
c324ab03bb | |||
bf204c8fb4 | |||
1175a38d8b | |||
d7bf6542ce | |||
56945759a6 | |||
2d59428b1e | |||
5f747d5e8b | |||
27dddfb1b7 | |||
7043dcb2bf | |||
d0f97044c0 | |||
6d421cefd1 | |||
a177d6145e | |||
bbbacf6666 | |||
0152d90502 | |||
9ef9c150b1 | |||
93faaeb6ce | |||
cf558ada66 | |||
ef69c37109 | |||
b6e34c53c1 | |||
af7bebdc53 | |||
478899bb66 | |||
6588aa823e | |||
785cddf02c | |||
146d1c7a64 | |||
cb8ae01ca4 | |||
67f352cf2e | |||
ececa1b075 | |||
4903ac275e | |||
5a1db73f33 | |||
477b03efd3 | |||
bfb60003de |
63
.ci/ecr_public_lifecycle.py
Executable file
63
.ci/ecr_public_lifecycle.py
Executable file
@ -0,0 +1,63 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import boto3
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='Implement basic public ECR lifecycle policy')
|
||||||
|
parser.add_argument('--repo', dest='repositoryName', action='store', required=True,
|
||||||
|
help='Name of the public ECR repository')
|
||||||
|
parser.add_argument('--keep', dest='keep', action='store', default=10, type=int,
|
||||||
|
help='number of tagged images to keep, default 10')
|
||||||
|
parser.add_argument('--dev', dest='delete_dev', action='store_true',
|
||||||
|
help='also delete in-development images only having tags like v0.1.1-commitNr-githash')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
client = boto3.client('ecr-public', region_name='us-east-1')
|
||||||
|
|
||||||
|
images = client.describe_images(repositoryName=args.repositoryName)[
|
||||||
|
"imageDetails"]
|
||||||
|
|
||||||
|
untagged = []
|
||||||
|
kept = 0
|
||||||
|
|
||||||
|
# actual Image
|
||||||
|
# imageManifestMediaType: 'application/vnd.oci.image.manifest.v1+json'
|
||||||
|
# image Index
|
||||||
|
# imageManifestMediaType: 'application/vnd.oci.image.index.v1+json'
|
||||||
|
|
||||||
|
# Sort by date uploaded
|
||||||
|
for image in sorted(images, key=lambda d: d['imagePushedAt'], reverse=True):
|
||||||
|
# Remove all untagged
|
||||||
|
# if registry uses image index all actual images will be untagged anyways
|
||||||
|
if 'imageTags' not in image:
|
||||||
|
untagged.append({"imageDigest": image['imageDigest']})
|
||||||
|
# print("Delete untagged image {}".format(image["imageDigest"]))
|
||||||
|
continue
|
||||||
|
|
||||||
|
# check for dev tags
|
||||||
|
if args.delete_dev:
|
||||||
|
_delete = True
|
||||||
|
for tag in image["imageTags"]:
|
||||||
|
# Look for at least one tag NOT beign a SemVer dev tag
|
||||||
|
if "-" not in tag:
|
||||||
|
_delete = False
|
||||||
|
if _delete:
|
||||||
|
print("Deleting development image {}".format(image["imageTags"]))
|
||||||
|
untagged.append({"imageDigest": image['imageDigest']})
|
||||||
|
continue
|
||||||
|
|
||||||
|
if kept < args.keep:
|
||||||
|
kept = kept+1
|
||||||
|
print("Keeping tagged image {}".format(image["imageTags"]))
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
print("Deleting tagged image {}".format(image["imageTags"]))
|
||||||
|
untagged.append({"imageDigest": image['imageDigest']})
|
||||||
|
|
||||||
|
deleted_images = client.batch_delete_image(
|
||||||
|
repositoryName=args.repositoryName, imageIds=untagged)
|
||||||
|
|
||||||
|
if deleted_images["imageIds"]:
|
||||||
|
print("Deleted images: {}".format(deleted_images["imageIds"]))
|
@ -1,25 +1,26 @@
|
|||||||
# Parse version from latest git semver tag
|
# Parse version from latest git semver tag
|
||||||
|
GIT_TAG ?= $(shell git describe --tags --match v*.*.* 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
|
||||||
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
|
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
|
||||||
GIT_TAG := $(shell git describe --tags --match v*.*.* 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
|
|
||||||
|
|
||||||
TAG := $(GIT_TAG)
|
TAG ::= $(GIT_TAG)
|
||||||
# append branch name to tag if NOT main nor master
|
# append branch name to tag if NOT main nor master
|
||||||
ifeq (,$(filter main master, $(GIT_BRANCH)))
|
ifeq (,$(filter main master, $(GIT_BRANCH)))
|
||||||
# If branch is substring of tag, omit branch name
|
# If branch is substring of tag, omit branch name
|
||||||
ifeq ($(findstring $(GIT_BRANCH), $(GIT_TAG)),)
|
ifeq ($(findstring $(GIT_BRANCH), $(GIT_TAG)),)
|
||||||
# only append branch name if not equal tag
|
# only append branch name if not equal tag
|
||||||
ifneq ($(GIT_TAG), $(GIT_BRANCH))
|
ifneq ($(GIT_TAG), $(GIT_BRANCH))
|
||||||
TAG = $(GIT_TAG)-$(GIT_BRANCH)
|
# Sanitize GIT_BRANCH to allowed Docker tag character set
|
||||||
|
TAG = $(GIT_TAG)-$(shell echo $$GIT_BRANCH | sed -e 's/[^a-zA-Z0-9]/-/g')
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ARCH := amd64
|
ARCH ::= amd64
|
||||||
ALL_ARCHS := amd64 arm64
|
ALL_ARCHS ::= amd64 arm64
|
||||||
_ARCH = $(or $(filter $(ARCH),$(ALL_ARCHS)),$(error $$ARCH [$(ARCH)] must be exactly one of "$(ALL_ARCHS)"))
|
_ARCH = $(or $(filter $(ARCH),$(ALL_ARCHS)),$(error $$ARCH [$(ARCH)] must be exactly one of "$(ALL_ARCHS)"))
|
||||||
|
|
||||||
ifneq ($(TRIVY_REMOTE),)
|
ifneq ($(TRIVY_REMOTE),)
|
||||||
TRIVY_OPTS := --server $(TRIVY_REMOTE)
|
TRIVY_OPTS ::= --server $(TRIVY_REMOTE)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
.SILENT: ; # no need for @
|
.SILENT: ; # no need for @
|
||||||
@ -32,18 +33,20 @@ endif
|
|||||||
help: ## Show Help
|
help: ## Show Help
|
||||||
grep -E '^[a-zA-Z_-]+:.*?## .*$$' .ci/podman.mk | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
grep -E '^[a-zA-Z_-]+:.*?## .*$$' .ci/podman.mk | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
||||||
|
|
||||||
|
prepare:: ## custom step on the build agent before building
|
||||||
|
|
||||||
|
fmt:: ## auto format source
|
||||||
|
|
||||||
|
lint:: ## Lint source
|
||||||
|
|
||||||
build: ## Build the app
|
build: ## Build the app
|
||||||
buildah build --rm --layers -t $(IMAGE):$(TAG)-$(_ARCH) --build-arg TAG=$(TAG) --build-arg ARCH=$(_ARCH) --platform linux/$(_ARCH) .
|
buildah build --rm --layers -t $(IMAGE):$(TAG)-$(_ARCH) --build-arg TAG=$(TAG) --build-arg ARCH=$(_ARCH) --platform linux/$(_ARCH) .
|
||||||
|
|
||||||
test: rm-test-image ## Execute Dockerfile.test
|
test:: ## test built artificats
|
||||||
test -f Dockerfile.test && \
|
|
||||||
{ buildah build --rm --layers -t $(REGISTRY)/$(IMAGE):$(TAG)-$(_ARCH)-test --from=$(REGISTRY)/$(IMAGE):$(TAG) -f Dockerfile.test --platform linux/$(_ARCH) . && \
|
|
||||||
podman run --rm --env-host -t $(REGISTRY)/$(IMAGE):$(TAG)-$(_ARCH)-test; } || \
|
|
||||||
echo "No Dockerfile.test found, skipping test"
|
|
||||||
|
|
||||||
scan: ## Scan image using trivy
|
scan: ## Scan image using trivy
|
||||||
echo "Scanning $(IMAGE):$(TAG)-$(_ARCH) using Trivy $(TRIVY_REMOTE)"
|
echo "Scanning $(IMAGE):$(TAG)-$(_ARCH) using Trivy $(TRIVY_REMOTE)"
|
||||||
trivy image $(TRIVY_OPTS) localhost/$(IMAGE):$(TAG)-$(_ARCH)
|
trivy image $(TRIVY_OPTS) --quiet --no-progress localhost/$(IMAGE):$(TAG)-$(_ARCH)
|
||||||
|
|
||||||
# first tag and push all actual images
|
# first tag and push all actual images
|
||||||
# create new manifest for each tag and add all available TAG-ARCH before pushing
|
# create new manifest for each tag and add all available TAG-ARCH before pushing
|
||||||
@ -63,24 +66,19 @@ push: ecr-login ## push images to registry
|
|||||||
ecr-login: ## log into AWS ECR public
|
ecr-login: ## log into AWS ECR public
|
||||||
aws ecr-public get-login-password --region $(REGION) | podman login --username AWS --password-stdin $(REGISTRY)
|
aws ecr-public get-login-password --region $(REGION) | podman login --username AWS --password-stdin $(REGISTRY)
|
||||||
|
|
||||||
clean: rm-test-image rm-image ## delete local built container and test images
|
rm-remote-untagged: ## delete all remote untagged and in-dev images, keep 10 tagged
|
||||||
|
echo "Removing all untagged and in-dev images from $(IMAGE) in $(REGION)"
|
||||||
|
.ci/ecr_public_lifecycle.py --repo $(IMAGE) --dev
|
||||||
|
|
||||||
rm-remote-untagged: ## delete all remote untagged images
|
clean:: ## clean up source folder
|
||||||
echo "Removing all untagged images from $(IMAGE) in $(REGION)"
|
|
||||||
IMAGE_IDS=$$(for image in $$(aws ecr-public describe-images --repository-name $(IMAGE) --region $(REGION) --output json | jq -r '.imageDetails[] | select(.imageTags | not ).imageDigest'); do echo -n "imageDigest=$$image "; done) ; \
|
|
||||||
[ -n "$$IMAGE_IDS" ] && aws ecr-public batch-delete-image --repository-name $(IMAGE) --region $(REGION) --image-ids $$IMAGE_IDS || echo "No image to remove"
|
|
||||||
|
|
||||||
rm-image:
|
rm-image:
|
||||||
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH))" || podman image rm -f $(IMAGE):$(TAG)-$(_ARCH) > /dev/null
|
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH))" || podman image rm -f $(IMAGE):$(TAG)-$(_ARCH) > /dev/null
|
||||||
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH))" || echo "Error: Removing image failed"
|
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH))" || echo "Error: Removing image failed"
|
||||||
|
|
||||||
# Ensure we run the tests by removing any previous runs
|
## some useful tasks during development
|
||||||
rm-test-image:
|
|
||||||
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH)-test)" || podman image rm -f $(IMAGE):$(TAG)-$(_ARCH)-test > /dev/null
|
|
||||||
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH)-test)" || echo "Error: Removing test image failed"
|
|
||||||
|
|
||||||
ci-pull-upstream: ## pull latest shared .ci subtree
|
ci-pull-upstream: ## pull latest shared .ci subtree
|
||||||
git stash && git subtree pull --prefix .ci ssh://git@git.zero-downtime.net/ZeroDownTime/ci-tools-lib.git master --squash && git stash pop
|
git subtree pull --prefix .ci ssh://git@git.zero-downtime.net/ZeroDownTime/ci-tools-lib.git master --squash -m "Merge latest ci-tools-lib"
|
||||||
|
|
||||||
create-repo: ## create new AWS ECR public repository
|
create-repo: ## create new AWS ECR public repository
|
||||||
aws ecr-public create-repository --repository-name $(IMAGE) --region $(REGION)
|
aws ecr-public create-repository --repository-name $(IMAGE) --region $(REGION)
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
def call(Map config=[:]) {
|
def call(Map config=[:]) {
|
||||||
pipeline {
|
pipeline {
|
||||||
|
options {
|
||||||
|
disableConcurrentBuilds()
|
||||||
|
}
|
||||||
agent {
|
agent {
|
||||||
node {
|
node {
|
||||||
label 'podman-aws-trivy'
|
label 'podman-aws-trivy'
|
||||||
@ -10,18 +13,22 @@ def call(Map config=[:]) {
|
|||||||
stages {
|
stages {
|
||||||
stage('Prepare') {
|
stage('Prepare') {
|
||||||
steps {
|
steps {
|
||||||
|
sh 'mkdir -p reports'
|
||||||
|
|
||||||
|
// we set pull tags as project adv. options
|
||||||
// pull tags
|
// pull tags
|
||||||
withCredentials([gitUsernamePassword(credentialsId: 'gitea-jenkins-user')]) {
|
//withCredentials([gitUsernamePassword(credentialsId: 'gitea-jenkins-user')]) {
|
||||||
sh 'git fetch -q --tags ${GIT_URL}'
|
// sh 'git fetch -q --tags ${GIT_URL}'
|
||||||
}
|
//}
|
||||||
sh 'make prepare || true'
|
// Optional project specific preparations
|
||||||
|
sh 'make prepare'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build using rootless podman
|
// Build using rootless podman
|
||||||
stage('Build') {
|
stage('Build') {
|
||||||
steps {
|
steps {
|
||||||
sh 'make build'
|
sh 'make build GIT_BRANCH=$GIT_BRANCH'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,12 +40,13 @@ def call(Map config=[:]) {
|
|||||||
|
|
||||||
// Scan via trivy
|
// Scan via trivy
|
||||||
stage('Scan') {
|
stage('Scan') {
|
||||||
environment {
|
|
||||||
TRIVY_FORMAT = "template"
|
|
||||||
TRIVY_OUTPUT = "reports/trivy.html"
|
|
||||||
}
|
|
||||||
steps {
|
steps {
|
||||||
sh 'mkdir -p reports && make scan'
|
// we always scan and create the full json report
|
||||||
|
sh 'TRIVY_FORMAT=json TRIVY_OUTPUT="reports/trivy.json" make scan'
|
||||||
|
|
||||||
|
// render custom full html report
|
||||||
|
sh 'trivy convert -f template -t @/home/jenkins/html.tpl -o reports/trivy.html reports/trivy.json'
|
||||||
|
|
||||||
publishHTML target: [
|
publishHTML target: [
|
||||||
allowMissing: true,
|
allowMissing: true,
|
||||||
alwaysLinkToLastBuild: true,
|
alwaysLinkToLastBuild: true,
|
||||||
@ -48,26 +56,33 @@ def call(Map config=[:]) {
|
|||||||
reportName: 'TrivyScan',
|
reportName: 'TrivyScan',
|
||||||
reportTitles: 'TrivyScan'
|
reportTitles: 'TrivyScan'
|
||||||
]
|
]
|
||||||
|
sh 'echo "Trivy report at: $BUILD_URL/TrivyScan"'
|
||||||
|
|
||||||
// Scan again and fail on CRITICAL vulns, if not overridden
|
// fail build if issues found above trivy threshold
|
||||||
script {
|
script {
|
||||||
if (config.trivyFail == 'NONE') {
|
if ( config.trivyFail ) {
|
||||||
echo 'trivyFail == NONE, review Trivy report manually. Proceeding ...'
|
sh "TRIVY_SEVERITY=${config.trivyFail} trivy convert --report summary --exit-code 1 reports/trivy.json"
|
||||||
} else {
|
|
||||||
sh "TRIVY_EXIT_CODE=1 TRIVY_SEVERITY=${config.trivyFail} make scan"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push to container registry, skip if PR
|
// Push to container registry if not PR
|
||||||
|
// incl. basic registry retention removing any untagged images
|
||||||
stage('Push') {
|
stage('Push') {
|
||||||
when { not { changeRequest() } }
|
when { not { changeRequest() } }
|
||||||
steps {
|
steps {
|
||||||
sh 'make push'
|
sh 'make push'
|
||||||
|
sh 'make rm-remote-untagged'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generic clean
|
||||||
|
stage('cleanup') {
|
||||||
|
steps {
|
||||||
|
sh 'make clean'
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -59,3 +59,5 @@ reports/
|
|||||||
# virtualenv
|
# virtualenv
|
||||||
venv/
|
venv/
|
||||||
ENV/
|
ENV/
|
||||||
|
|
||||||
|
aws-lambda-rie
|
||||||
|
28
Dockerfile
28
Dockerfile
@ -1,15 +1,13 @@
|
|||||||
# https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/
|
|
||||||
ARG RUNTIME_VERSION="3.9"
|
|
||||||
|
|
||||||
# libexec is missing from >=3.17
|
|
||||||
ARG DISTRO_VERSION="3.16"
|
|
||||||
|
|
||||||
# Stage 1 - bundle base image + runtime
|
# Stage 1 - bundle base image + runtime
|
||||||
FROM python:${RUNTIME_VERSION}-alpine${DISTRO_VERSION} AS python-alpine
|
FROM python:3.12-alpine3.20 AS python-alpine
|
||||||
|
ARG ALPINE="v3.20"
|
||||||
|
|
||||||
# Install GCC (Alpine uses musl but we compile and link dependencies with GCC)
|
# Install GCC (Alpine uses musl but we compile and link dependencies with GCC)
|
||||||
RUN apk upgrade -U --available --no-cache && \
|
RUN echo "@kubezero https://cdn.zero-downtime.net/alpine/${ALPINE}/kubezero" >> /etc/apk/repositories && \
|
||||||
apk add --no-cache \
|
wget -q -O /etc/apk/keys/stefan@zero-downtime.net-61bb6bfb.rsa.pub https://cdn.zero-downtime.net/alpine/stefan@zero-downtime.net-61bb6bfb.rsa.pub
|
||||||
|
|
||||||
|
RUN apk -U --no-cache upgrade && \
|
||||||
|
apk --no-cache add \
|
||||||
libstdc++
|
libstdc++
|
||||||
|
|
||||||
|
|
||||||
@ -18,8 +16,7 @@ FROM python-alpine AS build-image
|
|||||||
ARG TAG="latest"
|
ARG TAG="latest"
|
||||||
|
|
||||||
# Install aws-lambda-cpp build dependencies
|
# Install aws-lambda-cpp build dependencies
|
||||||
RUN apk upgrade -U --available --no-cache && \
|
RUN apk --no-cache add \
|
||||||
apk add --no-cache \
|
|
||||||
build-base \
|
build-base \
|
||||||
libtool \
|
libtool \
|
||||||
autoconf \
|
autoconf \
|
||||||
@ -28,8 +25,8 @@ RUN apk upgrade -U --available --no-cache && \
|
|||||||
cmake \
|
cmake \
|
||||||
libcurl \
|
libcurl \
|
||||||
libffi-dev \
|
libffi-dev \
|
||||||
libexecinfo-dev \
|
openssl-dev \
|
||||||
openssl-dev
|
elfutils-dev
|
||||||
# cargo
|
# cargo
|
||||||
|
|
||||||
# Install requirements
|
# Install requirements
|
||||||
@ -40,12 +37,15 @@ RUN export MAKEFLAGS="-j$(nproc)" && \
|
|||||||
# Install our app
|
# Install our app
|
||||||
COPY app.py /app
|
COPY app.py /app
|
||||||
|
|
||||||
# Ser version to our TAG
|
# Set internal __version__ to our own container TAG
|
||||||
RUN sed -i -e "s/^__version__ =.*/__version__ = \"${TAG}\"/" /app/app.py
|
RUN sed -i -e "s/^__version__ =.*/__version__ = \"${TAG}\"/" /app/app.py
|
||||||
|
|
||||||
# Stage 3 - final runtime image
|
# Stage 3 - final runtime image
|
||||||
FROM python-alpine
|
FROM python-alpine
|
||||||
|
|
||||||
|
RUN apk --no-cache add \
|
||||||
|
zstd-libs
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=build-image /app /app
|
COPY --from=build-image /app /app
|
||||||
|
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
FROM setviacmdline:latest
|
|
||||||
|
|
||||||
# Install additional tools for tests
|
|
||||||
COPY dev-requirements.txt .flake8 .
|
|
||||||
RUN export MAKEFLAGS="-j$(nproc)" && \
|
|
||||||
pip install -r dev-requirements.txt
|
|
||||||
|
|
||||||
# Unit Tests / Static / Style etc.
|
|
||||||
COPY tests/ tests/
|
|
||||||
RUN flake8 app.py tests && \
|
|
||||||
codespell app.py tests
|
|
||||||
|
|
||||||
# Get aws-lambda run time emulator
|
|
||||||
ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/local/bin/aws-lambda-rie
|
|
||||||
RUN chmod 0755 /usr/local/bin/aws-lambda-rie && \
|
|
||||||
mkdir -p tests
|
|
||||||
|
|
||||||
# Install pytest
|
|
||||||
RUN pip install pytest --target /app
|
|
||||||
|
|
||||||
# Add our tests
|
|
||||||
ADD tests /app/tests
|
|
||||||
|
|
||||||
# Run tests
|
|
||||||
ENTRYPOINT []
|
|
||||||
CMD /usr/local/bin/python -m pytest tests -c tests/pytest.ini --capture=tee-sys
|
|
18
Makefile
18
Makefile
@ -3,3 +3,21 @@ IMAGE := sns-alert-hub
|
|||||||
REGION := us-east-1
|
REGION := us-east-1
|
||||||
|
|
||||||
include .ci/podman.mk
|
include .ci/podman.mk
|
||||||
|
|
||||||
|
SOURCE := app.py tests/test_aws-lambda-rie.py
|
||||||
|
|
||||||
|
test:: aws-lambda-rie
|
||||||
|
./run_tests.sh "$(IMAGE):$(TAG)-$(_ARCH)"
|
||||||
|
|
||||||
|
fmt::
|
||||||
|
autopep8 -i -a $(SOURCE)
|
||||||
|
|
||||||
|
lint::
|
||||||
|
flake8 $(SOURCE)
|
||||||
|
codespell $(SOURCE)
|
||||||
|
|
||||||
|
clean::
|
||||||
|
rm -rf .pytest_cache __pycache__ aws-lambda-rie
|
||||||
|
|
||||||
|
aws-lambda-rie:
|
||||||
|
wget https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && chmod 0755 aws-lambda-rie
|
||||||
|
15
app.py
15
app.py
@ -106,7 +106,8 @@ def handler(event, context):
|
|||||||
if "AlarmName" in msg:
|
if "AlarmName" in msg:
|
||||||
title = "AWS Cloudwatch Alarm"
|
title = "AWS Cloudwatch Alarm"
|
||||||
|
|
||||||
# Discard NewStateValue == OK && OldStateValue == INSUFFICIENT_DATA as these are triggered by installing new Alarms and only cause confusion
|
# Discard NewStateValue == OK && OldStateValue == INSUFFICIENT_DATA as
|
||||||
|
# these are triggered by installing new Alarms and only cause confusion
|
||||||
if msg["NewStateValue"] == "OK" and msg["OldStateValue"] == "INSUFFICIENT_DATA":
|
if msg["NewStateValue"] == "OK" and msg["OldStateValue"] == "INSUFFICIENT_DATA":
|
||||||
logger.info(
|
logger.info(
|
||||||
"Discarding Cloudwatch Metrics Alarm as state is OK and previous state was insufficient data, most likely new alarm being installed"
|
"Discarding Cloudwatch Metrics Alarm as state is OK and previous state was insufficient data, most likely new alarm being installed"
|
||||||
@ -260,6 +261,15 @@ def handler(event, context):
|
|||||||
title = "ElastiCache fail over complete"
|
title = "ElastiCache fail over complete"
|
||||||
body = "for node {}".format(msg["ElastiCache:FailoverComplete"])
|
body = "for node {}".format(msg["ElastiCache:FailoverComplete"])
|
||||||
|
|
||||||
|
# ElasticCache update notifications
|
||||||
|
elif "ElastiCache:ServiceUpdateAvailableForNode" in msg:
|
||||||
|
title = "ElastiCache update available"
|
||||||
|
body = "for node {}".format(msg["ElastiCache:ServiceUpdateAvailableForNode"])
|
||||||
|
|
||||||
|
elif "ElastiCache:ServiceUpdateAvailable" in msg:
|
||||||
|
title = "ElastiCache update available"
|
||||||
|
body = "for Group {}".format(msg["ElastiCache:ServiceUpdateAvailable"])
|
||||||
|
|
||||||
# known RDS events
|
# known RDS events
|
||||||
elif "Event Source" in msg and msg['Event Source'] in ["db-instance", "db-cluster-snapshot", "db-snapshot"]:
|
elif "Event Source" in msg and msg['Event Source'] in ["db-instance", "db-cluster-snapshot", "db-snapshot"]:
|
||||||
try:
|
try:
|
||||||
@ -298,4 +308,5 @@ def handler(event, context):
|
|||||||
msg_type = apprise.NotifyType.WARNING
|
msg_type = apprise.NotifyType.WARNING
|
||||||
body = sns["Message"]
|
body = sns["Message"]
|
||||||
|
|
||||||
apobj.notify(body=body, title=title, notify_type=msg_type)
|
if not apobj.notify(body=body, title=title, notify_type=msg_type):
|
||||||
|
logger.error("Error during notify!")
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
pytest
|
pytest
|
||||||
|
autopep8
|
||||||
flake8
|
flake8
|
||||||
codespell
|
codespell
|
||||||
|
10
renovate.json
Normal file
10
renovate.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
|
"extends": [
|
||||||
|
"config:recommended",
|
||||||
|
":label(renovate)",
|
||||||
|
":semanticCommits",
|
||||||
|
"group:allNonMajor"
|
||||||
|
],
|
||||||
|
"prHourlyLimit": 0
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
boto3
|
boto3==1.35.17
|
||||||
apprise
|
apprise==1.9.0
|
||||||
humanize
|
humanize==4.10.0
|
||||||
awslambdaric
|
awslambdaric==2.2.1
|
||||||
|
17
run_tests.sh
Executable file
17
run_tests.sh
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/sh -ex
|
||||||
|
|
||||||
|
IMAGE=$1
|
||||||
|
|
||||||
|
ctr=$(buildah from $IMAGE)
|
||||||
|
trap "buildah rm $ctr" EXIT
|
||||||
|
|
||||||
|
buildah copy $ctr dev-requirements.txt .flake8 .
|
||||||
|
buildah copy $ctr aws-lambda-rie
|
||||||
|
buildah copy $ctr tests/ tests/
|
||||||
|
|
||||||
|
buildah run $ctr pip install -r dev-requirements.txt --target .
|
||||||
|
|
||||||
|
buildah run $ctr python -m flake8 app.py
|
||||||
|
buildah run $ctr python -m codespell_lib app.py
|
||||||
|
|
||||||
|
buildah run $ctr python -m pytest tests -c tests/pytest.ini --capture=tee-sys
|
@ -9,8 +9,13 @@ from requests.packages.urllib3.util.retry import Retry
|
|||||||
|
|
||||||
s = requests.Session()
|
s = requests.Session()
|
||||||
retries = Retry(
|
retries = Retry(
|
||||||
total=3, backoff_factor=1, status_forcelist=[502, 503, 504], allowed_methods="POST"
|
total=3,
|
||||||
)
|
backoff_factor=1,
|
||||||
|
status_forcelist=[
|
||||||
|
502,
|
||||||
|
503,
|
||||||
|
504],
|
||||||
|
allowed_methods="POST")
|
||||||
s.mount("http://", HTTPAdapter(max_retries=retries))
|
s.mount("http://", HTTPAdapter(max_retries=retries))
|
||||||
|
|
||||||
|
|
||||||
@ -18,7 +23,7 @@ class Test:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def setup_class(cls):
|
def setup_class(cls):
|
||||||
cls.p = subprocess.Popen(
|
cls.p = subprocess.Popen(
|
||||||
"aws-lambda-rie python -m awslambdaric app.handler", shell=True
|
"./aws-lambda-rie python -m awslambdaric app.handler", shell=True
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -71,7 +76,38 @@ class Test:
|
|||||||
# RDS
|
# RDS
|
||||||
def test_rds_event(self):
|
def test_rds_event(self):
|
||||||
event = json.loads(
|
event = json.loads(
|
||||||
r' {"Records": [{"Event Source":"db-instance","Event Time":"2023-06-15 06:13:18.237","Identifier Link":"https://console.aws.amazon.com/rds/home?region=us-west-2#dbinstance:id=vrv6b014c5jbdf","Source ID":"vrv6b014c5jbdf","Source ARN":"arn:aws:rds:us-west-2:123456789012:db:vrv6b014c5jbdf","Event ID":"http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Events.Messages.html#RDS-EVENT-0001","Event Message":"Backing up DB instance","Tags":{"aws:cloudformation:stack-name":"postgres-rds","aws:cloudformation:stack-id":"arn:aws:cloudformation:us-west-2:123456789012:stack/postgres-rds/c7382650-46a7-11ea-bd3f-064fbe1c973c","Conglomerate":"test","aws:cloudformation:logical-id":"RdsDBInstance","Artifact":"postgres-rds","Name":"postgres-rds.DBInstance"}}]}'
|
r''' {
|
||||||
|
"Records": [
|
||||||
|
{
|
||||||
|
"EventSource": "aws:sns",
|
||||||
|
"EventVersion": "1.0",
|
||||||
|
"EventSubscriptionArn": "arn:aws:sns:us-west-2:123456789012:AlertHub:63470449-620d-44ce-971f-ad9582804b13",
|
||||||
|
"Sns": {
|
||||||
|
"Type": "Notification",
|
||||||
|
"MessageId": "ef1f821c-a04f-5c5c-9dff-df498532069b",
|
||||||
|
"TopicArn": "arn:aws:sns:us-west-2:123456789012:AlertHub",
|
||||||
|
"Subject": "RDS Notification Message",
|
||||||
|
"Message": "{\"Event Source\":\"db-cluster-snapshot\",\"Event Time\":\"2023-08-15 07:03:24.491\",\"Identifier Link\":\"https://console.aws.amazon.com/rds/home?region=us-west-2#snapshot:engine=aurora;id=rds:projectdb-cluster-2023-08-15-07-03\",\"Source ID\":\"rds:projectdb-cluster-2023-08-15-07-03\",\"Source ARN\":\"arn:aws:rds:us-west-2:123456789012:cluster-snapshot:rds:projectdb-cluster-2023-08-15-07-03\",\"Event ID\":\"http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Events.Messages.html#RDS-EVENT-0168\",\"Event Message\":\"Creating automated cluster snapshot\",\"Tags\":{}}",
|
||||||
|
"Timestamp": "2023-08-15T07:03:25.289Z",
|
||||||
|
"SignatureVersion": "1",
|
||||||
|
"Signature": "mRtx+ddS1uzF3alGDWnDtUkAz+Gno8iuv0wPwkeBJPe1LAcKTXVteYhQdP2BB5ZunPlWXPSDsNtFl8Eh6v4/fcdukxH/czc6itqgGiciQ3DCICLvOJrvrVVgsVvHgOA/Euh8wryzxeQ3HJ/nmF9sg/PtuKyxvGxyO7NSFJrRKkqwkuG1Wr/8gcN3nrenqNTzKiC16kzVuKISWgXM1jqbsleQ4MyBcjq61LRwODKB8tc8vJ6PLGOs4Lrc3qeruCqF3Tzpl43680RsaRBBn1SLycwFVdB1kpHSXuk+YJQ6BS7s6rbMoyhPOpSCFHMZXC/eEb09wTzgpop0KDE/koiUsg==",
|
||||||
|
"SigningCertUrl": "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-01d088a6f77103d0fe307c0069e40ed6.pem",
|
||||||
|
"UnsubscribeUrl": "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:AlertHub:63470449-620d-44ce-971f-ad9582804b13",
|
||||||
|
"MessageAttributes": {
|
||||||
|
"Resource": {
|
||||||
|
"Type": "String",
|
||||||
|
"Value": "arn:aws:rds:us-west-2:123456789012:cluster-snapshot:rds:projectdb-cluster-2023-08-15-07-03"
|
||||||
|
},
|
||||||
|
"EventID": {
|
||||||
|
"Type": "String",
|
||||||
|
"Value": "RDS-EVENT-0168"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
'''
|
||||||
)
|
)
|
||||||
self.send_event(event)
|
self.send_event(event)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user