From 129ef67ed5c9ca8c300afc97ddf18bc7cbf2aae4 Mon Sep 17 00:00:00 2001 From: Brandon Johnson Date: Tue, 28 Jun 2022 14:43:16 -0400 Subject: [PATCH] feat: Create Updater artifact (#529) * add new binary to everything but windows * add to windows msi * add version flag to updater * build using combined target * fix manual build * add license header * break updater into separate module * add new module to dependabot * copy version pkg to new updater internal pkg To not have a dependency on the root module * Workaround for https://github.com/securego/gosec/issues/501 --- .github/dependabot.yml | 11 +++++++ .github/workflows/manual_msi_build.yml | 4 ++- .github/workflows/release.yml | 4 ++- .goreleaser.yml | 26 +++++++++++++++++ Makefile | 29 ++++++++++++++----- updater/cmd/updater/main.go | 35 ++++++++++++++++++++++ updater/go.mod | 14 +++++++++ updater/go.sum | 14 +++++++++ updater/internal/version/version.go | 37 ++++++++++++++++++++++++ updater/internal/version/version_test.go | 27 +++++++++++++++++ windows/scripts/build-msi.sh | 1 + windows/wix.json | 3 ++ 12 files changed, 195 insertions(+), 10 deletions(-) create mode 100644 updater/cmd/updater/main.go create mode 100644 updater/go.mod create mode 100644 updater/go.sum create mode 100644 updater/internal/version/version.go create mode 100644 updater/internal/version/version_test.go diff --git a/.github/dependabot.yml b/.github/dependabot.yml index b746f1ac3..c6c4203f6 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -45,3 +45,14 @@ updates: commit-message: prefix: "deps" include: "scope" + - package-ecosystem: "gomod" + directory: "/updater" + schedule: + interval: "weekly" + ignore: + # Opentelemetry updates will be done manually + - dependency-name: "github.com/open-telemetry/opentelemetry-collector*" + - dependency-name: "go.opentelemetry.io/collector/*" + commit-message: + prefix: "deps" + include: "scope" diff --git a/.github/workflows/manual_msi_build.yml b/.github/workflows/manual_msi_build.yml index 8e28b4013..481ea393b 100644 --- a/.github/workflows/manual_msi_build.yml +++ b/.github/workflows/manual_msi_build.yml @@ -36,8 +36,10 @@ jobs: args: build --single-target --skip-validate --rm-dist --snapshot env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Copy Windows Binary + - name: Copy Windows Collector Binary run: cp dist/collector_windows_amd64_v1/observiq-otel-collector.exe windows/observiq-otel-collector.exe + - name: Copy Windows Updater Binary + run: cp dist/updater_windows_amd64_v1/updater.exe windows/updater.exe - name: Copy Plugins to MSI Build Directory run: cp -r release_deps/plugins windows/ - name: Copy Example Config diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 98439ff2a..3b03e4121 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -33,8 +33,10 @@ jobs: args: build --single-target --skip-validate --rm-dist env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Copy Windows Binary + - name: Copy Windows Collector Binary run: cp dist/collector_windows_amd64_v1/observiq-otel-collector.exe windows/observiq-otel-collector.exe + - name: Copy Windows Updater Binary + run: cp dist/updater_windows_amd64_v1/updater.exe windows/updater.exe - name: Copy Plugins to MSI Build Directory run: cp -r release_deps/plugins windows/ - name: Copy Example Config diff --git a/.goreleaser.yml b/.goreleaser.yml index 258781e49..229fccb4b 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -31,6 +31,32 @@ builds: - -X github.com/observiq/observiq-otel-collector/internal/version.gitHash={{ .FullCommit }} - -X github.com/observiq/observiq-otel-collector/internal/version.date={{ .Date }} no_unique_dist_dir: false + - id: updater + binary: updater + dir: ./updater/ + main: ./cmd/updater + env: + - CGO_ENABLED=0 + mod_timestamp: "{{ .CommitTimestamp }}" + goos: + - windows + - linux + - darwin + goarch: + - amd64 + - arm64 + - arm + ignore: + - goos: windows + goarch: arm + - goos: windows + goarch: arm64 + ldflags: + - -s -w + - -X github.com/observiq/observiq-otel-collector/updater/internal/version.version=v{{ .Version }} + - -X github.com/observiq/observiq-otel-collector/updater/internal/version.gitHash={{ .FullCommit }} + - -X github.com/observiq/observiq-otel-collector/updater/internal/version.date={{ .Date }} + no_unique_dist_dir: false # https://goreleaser.com/customization/archive/ archives: diff --git a/Makefile b/Makefile index 09d164fc2..2cde3f598 100644 --- a/Makefile +++ b/Makefile @@ -21,11 +21,23 @@ CURRENT_TAG := $(shell git tag --sort=v:refname --points-at HEAD | grep -E "v[0- # Version will be the tag pointing to the current commit, or the previous version tag if there is no such tag VERSION ?= $(if $(CURRENT_TAG),$(CURRENT_TAG),$(PREVIOUS_TAG)) -# Default build target; making this should build for the current os/arch +# Build binaries for current GOOS/GOARCH by default +.DEFAULT_GOAL := build-binaries + +# Builds just the collector for current GOOS/GOARCH pair .PHONY: collector collector: go build -ldflags "-s -w -X github.com/observiq/observiq-otel-collector/internal/version.version=$(VERSION)" -o $(OUTDIR)/collector_$(GOOS)_$(GOARCH)$(EXT) ./cmd/collector +# Builds just the updater for current GOOS/GOARCH pair +.PHONY: updater +updater: + cd ./updater/; go build -ldflags "-s -w -X github.com/observiq/observiq-otel-collector/internal/version.version=$(VERSION)" -o ../$(OUTDIR)/updater_$(GOOS)_$(GOARCH)$(EXT) ./cmd/updater + +# Builds the updater + collector for current GOOS/GOARCH pair +.PHONY: build-binaries +build-binaries: collector updater + .PHONY: build-all build-all: build-linux build-darwin build-windows @@ -40,27 +52,27 @@ build-windows: build-windows-amd64 .PHONY: build-linux-amd64 build-linux-amd64: - GOOS=linux GOARCH=amd64 $(MAKE) collector + GOOS=linux GOARCH=amd64 $(MAKE) build-binaries -j2 .PHONY: build-linux-arm64 build-linux-arm64: - GOOS=linux GOARCH=arm64 $(MAKE) collector + GOOS=linux GOARCH=arm64 $(MAKE) build-binaries -j2 .PHONY: build-linux-arm build-linux-arm: - GOOS=linux GOARCH=arm $(MAKE) collector + GOOS=linux GOARCH=arm $(MAKE) build-binaries -j2 .PHONY: build-darwin-amd64 build-darwin-amd64: - GOOS=darwin GOARCH=amd64 $(MAKE) collector + GOOS=darwin GOARCH=amd64 $(MAKE) build-binaries -j2 .PHONY: build-darwin-arm64 build-darwin-arm64: - GOOS=darwin GOARCH=arm64 $(MAKE) collector + GOOS=darwin GOARCH=arm64 $(MAKE) build-binaries -j2 .PHONY: build-windows-amd64 build-windows-amd64: - GOOS=windows GOARCH=amd64 $(MAKE) collector + GOOS=windows GOARCH=amd64 $(MAKE) build-binaries -j2 # tool-related commands .PHONY: install-tools @@ -115,7 +127,8 @@ tidy: .PHONY: gosec gosec: - gosec ./... + gosec -exclude-dir updater ./... + cd updater; gosec ./... # This target performs all checks that CI will do (excluding the build itself) .PHONY: ci-checks diff --git a/updater/cmd/updater/main.go b/updater/cmd/updater/main.go new file mode 100644 index 000000000..9f9caab72 --- /dev/null +++ b/updater/cmd/updater/main.go @@ -0,0 +1,35 @@ +// Copyright observIQ, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + + "github.com/observiq/observiq-otel-collector/updater/internal/version" + "github.com/spf13/pflag" +) + +// Unimplemented +func main() { + var showVersion = pflag.BoolP("version", "v", false, "prints the version of the collector") + pflag.Parse() + + if *showVersion { + fmt.Println("observiq-otel-collector updater version", version.Version()) + fmt.Println("commit:", version.GitHash()) + fmt.Println("built at:", version.Date()) + return + } +} diff --git a/updater/go.mod b/updater/go.mod new file mode 100644 index 000000000..b8f3f15c9 --- /dev/null +++ b/updater/go.mod @@ -0,0 +1,14 @@ +module github.com/observiq/observiq-otel-collector/updater + +go 1.17 + +require ( + github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.7.2 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/updater/go.sum b/updater/go.sum new file mode 100644 index 000000000..2d2de69f9 --- /dev/null +++ b/updater/go.sum @@ -0,0 +1,14 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= +github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/updater/internal/version/version.go b/updater/internal/version/version.go new file mode 100644 index 000000000..beef0ef2e --- /dev/null +++ b/updater/internal/version/version.go @@ -0,0 +1,37 @@ +// Copyright observIQ, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package version + +// these will be replaced at link time by make. +var ( + version = "latest" // Semantic version, or "latest" by default + gitHash = "unknown" // Commit hash from which this build was generated + date = "unknown" // Date the build was generated +) + +// Version returns the version of the collector. +func Version() string { + return version +} + +// GitHash returns the githash associated with the collector's version. +func GitHash() string { + return gitHash +} + +// Date returns the publish date associated with the collector's version. +func Date() string { + return date +} diff --git a/updater/internal/version/version_test.go b/updater/internal/version/version_test.go new file mode 100644 index 000000000..2c7956d5d --- /dev/null +++ b/updater/internal/version/version_test.go @@ -0,0 +1,27 @@ +// Copyright observIQ, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package version + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDefaults(t *testing.T) { + require.Equal(t, version, Version()) + require.Equal(t, gitHash, GitHash()) + require.Equal(t, date, Date()) +} diff --git a/windows/scripts/build-msi.sh b/windows/scripts/build-msi.sh index 3c740c72a..a6d1c7fe1 100755 --- a/windows/scripts/build-msi.sh +++ b/windows/scripts/build-msi.sh @@ -22,6 +22,7 @@ mkdir -p storage touch storage/.include cp "$PROJECT_BASE/dist/collector_windows_amd64.exe" "observiq-otel-collector.exe" +cp "$PROJECT_BASE/dist/updater_windows_amd64.exe" "updater.exe" vagrant winrm -c \ "cd C:/vagrant; go-msi.exe make -m observiq-otel-collector.msi --version v0.0.1 --arch amd64" diff --git a/windows/wix.json b/windows/wix.json index f79a7fbde..b29305224 100644 --- a/windows/wix.json +++ b/windows/wix.json @@ -18,6 +18,9 @@ "arguments": "--config "[INSTALLDIR]config.yaml" --logging "[INSTALLDIR]logging.yaml" --manager "[INSTALLDIR]manager.yaml"" } }, + { + "path": "updater.exe" + }, { "path": "config.yaml", "never_overwrite": true