diff --git a/Makefile b/Makefile index 8038812..d3e2f8a 100644 --- a/Makefile +++ b/Makefile @@ -82,6 +82,8 @@ GITHUB_ORG := mattermost # Most probably the name of the repo GITHUB_REPO := ${APP_NAME} +# Plugins in the repository to be included in the releases and docker images +OPS_TOOL_PLUGINS ?= bash # Command line arguments of ops tool when executed OPS_TOOL_ARGS ?= -c config/config.sample.yaml @@ -116,6 +118,13 @@ AT_0 := @ AT_1 := AT = $(AT_$(VERBOSE)) +# ==================================================================================== +# Functions +define BuildPlugin + $(AT)$(INFO) Building ${BLUE}$(1)${CNone} plugin. + $(AT)cd plugin/$(1);make build || ${FAIL} +endef + # ==================================================================================== # Targets @@ -249,7 +258,7 @@ docker-login: ## to login to a container registry $(AT) echo "${DOCKER_PASSWORD}" | $(DOCKER) login --password-stdin -u ${DOCKER_USER} $(DOCKER_REGISTRY) || ${FAIL} @$(OK) Dockerd login to container registry ${DOCKER_REGISTRY}... -go-build: $(GO_BUILD_PLATFORMS_ARTIFACTS) ## to build binaries +go-build: $(GO_BUILD_PLATFORMS_ARTIFACTS) go-build-plugins ## to build binaries .PHONY: go-build go-build/%: @@ -262,13 +271,16 @@ go-build/%: export GOARCH="$${platform#*-}"; \ echo export GOOS=$${GOOS}; \ echo export GOARCH=$${GOARCH}; \ - CGO_ENABLED=0 \ $(GO) build ${GO_BUILD_OPTS} \ -ldflags '${GO_LDFLAGS}' \ -o ${GO_OUT_BIN_DIR}/$* \ ${CONFIG_APP_CODE} || ${FAIL} @$(OK) go build $* +.PHONY: go-build-plugins +go-build-plugins: + $(foreach plugin, $(OPS_TOOL_PLUGINS), $(call BuildPlugin,$(plugin))) + .PHONY: go-build-docker go-build-docker: # to build binaries under a controlled docker dedicated go container using DOCKER_IMAGE_GO @$(INFO) go build docker diff --git a/build/Dockerfile b/build/Dockerfile index 93b4fe0..fdc2603 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,6 +1,7 @@ -# This dockerfile is used to build Mattermost ops-tool +# This dockerfile is used to build Mattermost ops-tool will all plugins. +# See makefile to configure plugins # A multi stage build, with golang used as a builder -# and gcr.io/distroless/static as runner +# and dockerhub.io/debian:bullseye-20220801 as runner ARG GO_IMAGE=golang:1.18@sha256:90c06f42c1aa2b6b96441c0e6192aff48815cf5e7950cd661ed316fdbfb06ed4 # hadolint ignore=DL3006 FROM ${GO_IMAGE} as builder @@ -13,10 +14,11 @@ COPY . /src WORKDIR /src RUN make go-build -# Shrink final image since we only need the ops-tool binary -# and use distroless container image as runner for security -FROM gcr.io/distroless/static@sha256:d6fa9db9548b5772860fecddb11d84f9ebd7e0321c0cb3c02870402680cc315f as runner +# Cause of bash plugins we need to execute bash and shell commands. +# Instead of distroless image use debian slim image as base +FROM docker.io/debian:bullseye-20220801@sha256:82bab30ed448b8e2509aabe21f40f0607d905b7fd0dec72802627a20274eba55 as runner COPY --from=builder /src/dist/ops_tool-linux-amd64 /opt/ops-tool/bin/ops-tool +COPY --from=builder /src/dist/plugins /opt/ops-tool/plugins # We should refrain from running as privileged user # Run as UID for nobody diff --git a/config/config.sample.yaml b/config/config.sample.yaml index 18fe84a..7ece86d 100644 --- a/config/config.sample.yaml +++ b/config/config.sample.yaml @@ -4,7 +4,7 @@ listen: "0.0.0.0:8080" base_url: "https://op-tools-address.com" plugins: - name: bash_useful - file: plugins/bash.so + file: dist/plugins/bash.so config: files: - commands/gitlab/gitlab.yaml @@ -12,7 +12,7 @@ plugins: - commands/k8s/k8s.yaml - commands/release/release.yaml - name: bash_fun - file: plugins/bash.so + file: dist/plugins/bash.so config: files: - commands/helloworld/helloworld.yaml diff --git a/plugin/bash/Makefile b/plugin/bash/Makefile new file mode 100644 index 0000000..01b30e8 --- /dev/null +++ b/plugin/bash/Makefile @@ -0,0 +1,83 @@ +# ==================================================================================== +# Variables + +## General Variables +APP_NAME := ops-tool-bash-plugin + +# Get current date and format like: 2022-04-27 11:32 +BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M) + +## General Configuration Variables +# We don't need make's built-in rules. +MAKEFLAGS += --no-builtin-rules +# Be pedantic about undefined variables. +MAKEFLAGS += --warn-undefined-variables +# Set help as default target +.DEFAULT_GOAL := help + +# App Code location +CONFIG_APP_CODE += ./ + +## Go Variables +# Go executable +GO := $(shell which go) +# Extract GO version from go.mod file +GO_VERSION ?= $(shell grep -E '^go' go.mod | awk {'print $$2'}) +# Build options +GO_BUILD_OPTS := -mod=readonly -trimpath -buildmode=plugin +GO_TEST_OPTS := -mod=readonly -failfast -race +# Plugin generation dir +GO_OUT_SO_PATH := ../../dist/plugins/bash.so + +# ==================================================================================== +# Colors + +BLUE := $(shell printf "\033[34m") +YELLOW := $(shell printf "\033[33m") +RED := $(shell printf "\033[31m") +GREEN := $(shell printf "\033[32m") +CYAN := $(shell printf "\033[36m") +CNone := $(shell printf "\033[0m") + +# ==================================================================================== +# Logger + +TIME_LONG = `date +%Y-%m-%d' '%H:%M:%S` +TIME_SHORT = `date +%H:%M:%S` +TIME = $(TIME_SHORT) + +INFO = echo ${TIME} ${BLUE}[ .. ]${CNone} +WARN = echo ${TIME} ${YELLOW}[WARN]${CNone} +ERR = echo ${TIME} ${RED}[FAIL]${CNone} +OK = echo ${TIME} ${GREEN}[ OK ]${CNone} +FAIL = (echo ${TIME} ${RED}[FAIL]${CNone} && false) + +# ==================================================================================== +# Verbosity control hack + +VERBOSE ?= 0 +AT_0 := @ +AT_1 := +AT = $(AT_$(VERBOSE)) + +# ==================================================================================== +# Targets + +help: ## to get help + @echo "Usage:" + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) |\ + awk 'BEGIN {FS = ":.*?## "}; {printf "make ${CYAN}%-30s${CNone} %s\n", $$1, $$2}' + +.PHONY: build +build: ## to build plugin + @$(INFO) go build + $(AT)$(GO) build ${GO_BUILD_OPTS} \ + -o ${GO_OUT_SO_PATH} \ + ${CONFIG_APP_CODE} || ${FAIL} + @$(OK) go build $* + +.PHONY: test +test: ## to test plugin + @$(INFO) testing... + $(AT)$(GO) test ${GO_TEST_OPTS} ./... || ${FAIL} + @$(OK) testing