Skip to content

Commit

Permalink
Add deployment-badge-server
Browse files Browse the repository at this point in the history
A server for showing a svg badge with the deployment reference
  • Loading branch information
tantalic committed Mar 29, 2019
1 parent 99bd5fe commit c40fe8e
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 9 deletions.
17 changes: 13 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
DOCKER_IMAGE := toolhouse/verify-deployment-manifest
COMMIT := $(strip $(shell git rev-parse --short HEAD))
VERSION := $(strip $(shell git describe --always --dirty))

.PHONY: docker-build docker-push help
.DEFAULT_GOAL := help

docker-image: ## Build a docker image
docker-images: ## Build a docker image
docker build \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
--build-arg VERSION=$(VERSION) \
--build-arg VCS_REF=$(COMMIT) \
-t $(DOCKER_IMAGE):$(VERSION) \
-t toolhouse/verify-deployment-manifest:$(VERSION) \
-f ./cmd/verify-deployment-manifest/Dockerfile \
.

docker build \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
--build-arg VERSION=$(VERSION) \
--build-arg VCS_REF=$(COMMIT) \
-t toolhouse/deployment-badge-server:$(VERSION) \
-f ./cmd/deployment-badge-server/Dockerfile \
.

docker-push: ## Push the docker image to DockerHub
docker push $(DOCKER_IMAGE):$(VERSION)
docker push toolhouse/verify-deployment-manifest:$(VERSION)
docker push toolhouse/deployment-badge-server:$(VERSION)

help: ## Print available commands
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Deployment Manifest

![GitHub release](https://img.shields.io/github/release/toolhouse/deployment-manifest.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/toolhouse/deployment-manifest)](https://goreportcard.com/report/github.com/toolhouse/deployment-manifest) [![codebeat badge](https://codebeat.co/badges/0c41e0d2-9cdd-4388-9bbb-0d431c6cb3e8)](https://codebeat.co/projects/github.conef.uk-toolhouse-deployment-manifest-master) [![](https://images.microbadger.com/badges/image/toolhouse/verify-deployment-manifest.svg)](https://microbadger.com/images/toolhouse/verify-deployment-manifest "Docker Image") [![license](https://img.shields.io/github/license/toolhouse/deployment-manifest.svg)](https://github.com/toolhouse/deployment-manifest/blob/master/LICENSE)
![GitHub release](https://img.shields.io/github/release/toolhouse/deployment-manifest.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/toolhouse/deployment-manifest)](https://goreportcard.com/report/github.com/toolhouse/deployment-manifest) [![codebeat badge](https://codebeat.co/badges/0c41e0d2-9cdd-4388-9bbb-0d431c6cb3e8)](https://codebeat.co/projects/github.conef.uk-toolhouse-deployment-manifest-master) [![license](https://img.shields.io/github/license/toolhouse/deployment-manifest.svg)](https://github.com/toolhouse/deployment-manifest/blob/master/LICENSE)

A simple method to query information about the deployed version of a site or
application. This is accomplished by providing a JSON deployment manifest that
Expand Down Expand Up @@ -34,8 +34,12 @@ This was developed with two primary use-cases in mind:
This repository includes the following tools for working with deployment
manifests:

- `verify-deployment-manifest` - Validate that the expected commit/ref is
deployed to an environment by checking a deployment manifest for expected
versions.
- [`verify-deployment-manifest`][verify] - Validate that the expected commit/
ref is deployed to an environment by checking a deployment manifest for
expected versions.

- [`deployment-badge-server`][badge] - Show SVG badge with deployment
information, suitable for including in a README or wiki page.

[verify]: ./cmd/verify-deployment-manifest/
[badge]: ./cmd/deployment-badge-server/
44 changes: 44 additions & 0 deletions cmd/deployment-badge-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ARG GO_VERSION=1.12.1

FROM golang:${GO_VERSION}-alpine as builder

# Install git (required to fetch dependencies)
RUN apk update && apk add git && rm -rf /var/cache/apk/*

# Build Settings
ENV CGO_ENABLED=0
ENV GOOS=linux

WORKDIR /src

# Install dependencies
COPY ./go.mod ./go.sum ./
RUN go mod download

# Build go binary
COPY . .
RUN go build -installsuffix 'static' ./cmd/deployment-badge-server/

FROM alpine:3.9.2

# SSL CA Root Certs
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
COPY --from=builder /src/deployment-badge-server /deployment-badge-server

# Run as non-root user on port 8000
EXPOSE 8000
USER nobody
CMD ["/deployment-badge-server"]

# Labels: http://label-schema.org
ARG BUILD_DATE
ARG VCS_REF
ARG VERSION
LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="deployment-badge-server" \
org.label-schema.description="A server to display image badges with deployment information" \
org.label-schema.url="https://github.com/toolhouse/deployment-manifest/tree/master/cmd/deployment-badge-server" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url="https://github.com/toolhouse/deployment-manifest" \
org.label-schema.version=$VERSION \
org.label-schema.schema-version="1.0"
12 changes: 12 additions & 0 deletions cmd/deployment-badge-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# `deployment-badge-server`

[![](https://images.microbadger.com/badges/image/toolhouse/deployment-badge-server.svg)](https://microbadger.com/images/toolhouse/deployment-badge-server "Docker Image")

A server for showing the deployment reference from a deployment manifest in a
badge suitable for README files and wiki pages.

## How to use

The application is primarily designed to be run inside a Docker container
(although it can also be run as a standalone binary) and will serve content
on port `8000`.
65 changes: 65 additions & 0 deletions cmd/deployment-badge-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"html"
"net/http"
"net/url"

"github.com/toolhouse/deployment-manifest/pkg/deployment"

badge "github.com/narqo/go-badge"
)

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
http.ListenAndServe(":8000", mux)
}

func handler(w http.ResponseWriter, r *http.Request) {

u, err := manifestURLFromReq(r)
if err != nil {
errorResp(w, "error", "invalid url")
return
}

env := environmentURLFromReq(r)

manifest, err := deployment.FetchManifest(u)
if err != nil {
errorResp(w, env, "no manifest")
return
}

validResp(w, env, manifest.Ref)
}

func manifestURLFromReq(r *http.Request) (string, error) {
u := "https:/" + r.URL.Path
_, err := url.ParseRequestURI(u)

return u, err
}

func environmentURLFromReq(r *http.Request) string {
env := r.URL.Query().Get("env")
if env == "" {
return "deployed"
}

return env
}

func errorResp(w http.ResponseWriter, label, value string) {
resp(w, label, value, badge.ColorYellow)
}

func validResp(w http.ResponseWriter, env, value string) {
resp(w, env, value, badge.ColorBlue)
}

func resp(w http.ResponseWriter, label, value string, color badge.Color) {
w.Header().Set("Content-Type", "image/svg+xml")
badge.Render(html.EscapeString(label), html.EscapeString(value), color, w)
}
2 changes: 1 addition & 1 deletion cmd/verify-deployment-manifest/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ARG VERSION
LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="verify-deployment-manifest" \
org.label-schema.description="A tool for verifying a deployment manifest JSON file from a server" \
org.label-schema.url="https://github.com/toolhouse/verify-deployment-manifest" \
org.label-schema.url="https://github.com/toolhouse/deployment-manifest/tree/master/cmd/verify-deployment-manifest" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url="https://github.com/toolhouse/verify-deployment-manifest" \
org.label-schema.version=$VERSION \
Expand Down
2 changes: 2 additions & 0 deletions cmd/verify-deployment-manifest/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `verify-deployment-manifest`

[![](https://images.microbadger.com/badges/image/toolhouse/verify-deployment-manifest.svg)](https://microbadger.com/images/toolhouse/verify-deployment-manifest "Docker Image")

A tool for verifying a site/application is running the desired version by
checking the deployment manifest.

Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ replace (
)

require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-multierror v1.0.0
github.com/narqo/go-badge v0.0.0-20190124110329-d9415e4e1e9f
github.com/pkg/errors v0.8.1
github.com/the42/badge v0.0.0-20170523112329-0280203be5ca
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce h1:prjrVgOk2Yg6w+PflHoszQNLTUh4kaByUcEWM/9uin4=
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
Expand All @@ -6,7 +8,14 @@ github.com/hashicorp/go-multierror v0.0.0-20161216184304-ed905158d874 h1:cAv7ZbS
github.com/hashicorp/go-multierror v0.0.0-20161216184304-ed905158d874/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I=
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/narqo/go-badge v0.0.0-20190124110329-d9415e4e1e9f h1:lSUO7QQDM/xQhU8S8dztFkwbuMeArUK6Ymxh3qja5HM=
github.com/narqo/go-badge v0.0.0-20190124110329-d9415e4e1e9f/go.mod h1:7RbRBw88E4ePWAyz1EJI8iRMx68olFjSCnEjQeb9nJ8=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/the42/badge v0.0.0-20170523112329-0280203be5ca h1:yobz+VIm/MNGcVOVKCz9PN4hguhwFAIPyJMdfHSNLzM=
github.com/the42/badge v0.0.0-20170523112329-0280203be5ca/go.mod h1:nUh0aAhp4sHxVTuSJrYloWwA971PIWzRKZqSJK9rxjo=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f h1:FO4MZ3N56GnxbqxGKqh+YTzUWQ2sDwtFQEZgLOxh9Jc=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 comments on commit c40fe8e

Please sign in to comment.