Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Configure builds and Dockerfile to include plugins with the distribution
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Kara <[email protected]>
  • Loading branch information
Mustafa Kara committed Aug 12, 2022
1 parent 12df07f commit c8ad5bc
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 9 deletions.
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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/%:
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions config/config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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
- commands/docker/docker.yaml
- 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
Expand Down
83 changes: 83 additions & 0 deletions plugin/bash/Makefile
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c8ad5bc

Please sign in to comment.