Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to install unpacked artifacts in updater #562

Merged
merged 29 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
571c8ea
start artifact install logic
BinaryFissionGames Jul 6, 2022
35bb598
fix uninstall service step
BinaryFissionGames Jul 6, 2022
9f0b44a
add tests for windows service manager
BinaryFissionGames Jul 11, 2022
64210a0
remove kardiano/service dependency
BinaryFissionGames Jul 11, 2022
47e5a83
check filepath with spaces
BinaryFissionGames Jul 12, 2022
a924586
more tests, hook up to main
BinaryFissionGames Jul 12, 2022
f3c66c7
naming
BinaryFissionGames Jul 12, 2022
af35ab8
add licenses
BinaryFissionGames Jul 12, 2022
901b652
gosec fixes
BinaryFissionGames Jul 12, 2022
d1dcbdc
linux gosec + some lint issues
BinaryFissionGames Jul 12, 2022
901e60d
linter
BinaryFissionGames Jul 12, 2022
5d2d9b8
fix formatting of windows service test
BinaryFissionGames Jul 12, 2022
1a14328
actually fix formatting
BinaryFissionGames Jul 12, 2022
39dfd73
guard linux/win service tests behind tag
BinaryFissionGames Jul 12, 2022
bd794ee
run tests as sudo on linux
BinaryFissionGames Jul 12, 2022
3056f86
fix inverted conditional
BinaryFissionGames Jul 12, 2022
3da5202
split updater integration tests into separate target
BinaryFissionGames Jul 12, 2022
24da0d8
refactor package for better encapsulation
BinaryFissionGames Jul 12, 2022
25a9010
update darwin service to load/unload for start/stop
BinaryFissionGames Jul 12, 2022
f7918bd
fix installDir for windows after rename
BinaryFissionGames Jul 12, 2022
e0a4bb8
test replaceInstallDir
BinaryFissionGames Jul 12, 2022
64c0347
add license to service_test.go
BinaryFissionGames Jul 13, 2022
7bd6cc3
fix make target phony
BinaryFissionGames Jul 13, 2022
77fdb45
add some comments
BinaryFissionGames Jul 13, 2022
2327180
add start of readme
BinaryFissionGames Jul 13, 2022
42d59ab
add a (very basic) readme
BinaryFissionGames Jul 13, 2022
8d006cd
use switch instead of multiple ifs
BinaryFissionGames Jul 13, 2022
fb0355e
Add comments to moveFiles
BinaryFissionGames Jul 13, 2022
10bc78a
fix failing darwin test
BinaryFissionGames Jul 13, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,13 @@ jobs:
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run Tests
- name: Run Tests (non-linux)
if: matrix.os != 'ubuntu-20.04'
run: make test
env:
RUNNING_AS_SU: 1
- name: Run Tests (linux)
if: matrix.os == 'ubuntu-20.04'
run: sudo make test
env:
RUNNING_AS_SU: 1
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ OUTDIR=./dist
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)

ifdef RUNNING_AS_SU
TESTTAGS?=-tags superuser
else
TESTTAGS?=
endif

ifeq ($(GOOS), windows)
EXT?=.exe
else
Expand Down Expand Up @@ -102,7 +108,7 @@ misspell-fix:

.PHONY: test
test:
$(MAKE) for-all CMD="go test -race ./..."
$(MAKE) for-all CMD="go test $(TESTTAGS) -race ./..."

.PHONY: test-with-cover
test-with-cover:
Expand All @@ -128,7 +134,8 @@ tidy:
.PHONY: gosec
gosec:
gosec -exclude-dir updater ./...
cd updater; gosec ./...
# exclude the testdata dir; it contains a go program for testing.
cd updater; gosec -exclude-dir internal/install/testdata ./...

# This target performs all checks that CI will do (excluding the build itself)
.PHONY: ci-checks
Expand Down
4 changes: 4 additions & 0 deletions updater/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# logging.yaml and manager.yaml are ignored in the base module, but
# they are important in this module for test data.
!logging.yaml
!manager.yaml
14 changes: 14 additions & 0 deletions updater/cmd/updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/observiq/observiq-otel-collector/updater/internal/download"
"github.com/observiq/observiq-otel-collector/updater/internal/install"
"github.com/observiq/observiq-otel-collector/updater/internal/version"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -61,4 +63,16 @@ func main() {
log.Fatalf("Failed to download and verify update: %s", err)
}

installDir, err := install.InstallDir()
BinaryFissionGames marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("Failed to determine install dir: %s", err)
}

latestDir := filepath.Join(*tmpDir, "latest")
svc := install.NewService(latestDir)

if err := install.InstallArtifacts(latestDir, installDir, svc); err != nil {
log.Fatalf("Failed to install artifacts: %s", err)
}

}
3 changes: 3 additions & 0 deletions updater/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module github.com/observiq/observiq-otel-collector/updater
go 1.17

require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mholt/archiver/v3 v3.5.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.2
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211
)

require (
Expand All @@ -18,6 +20,7 @@ require (
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
github.com/ulikunitz/xz v0.5.9 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions updater/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdf
github.com/golang/snappy v0.0.2 h1:aeE13tS0IiQgFjYdoL8qN3K1N2bXXtI6Vi51/y7BpMw=
github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.11.4 h1:kz40R/YWls3iqT9zX9AHN3WoVsrAWVyui5sxuLqiXqU=
github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
Expand All @@ -25,6 +27,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
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 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
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=
Expand All @@ -33,6 +36,8 @@ github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 h1:9UQO31fZ+0aKQOFldThf7BKPMJTiBfWycGh/u3UoO88=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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=
Expand Down
142 changes: 142 additions & 0 deletions updater/internal/install/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// 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 install

import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
)

// InstallArtifacts installs the unpacked artifacts in latestDirPath to installDirPath,
// as well as installing the new service file using the provided Service interface
//revive:disable-next-line:exported it stutter but is an apt name
func InstallArtifacts(latestDirPath, installDirPath string, svc Service) error {
// Stop service
err := svc.Stop()
if err != nil {
return fmt.Errorf("failed to stop service: %w", err)
}

// install files that go to installDirPath to their correct location,
// excluding any config files (logging.yaml, config.yaml, manager.yaml)
if err := moveFiles(latestDirPath, installDirPath); err != nil {
return fmt.Errorf("failed to install new files: %w", err)
StefanKurek marked this conversation as resolved.
Show resolved Hide resolved
}

// Uninstall previous service
if err := svc.Uninstall(); err != nil {
return fmt.Errorf("failed to uninstall service: %w", err)
}

// Install new service
if err := svc.Install(); err != nil {
return fmt.Errorf("failed to install service: %w", err)
}

// Start service
if err := svc.Start(); err != nil {
return fmt.Errorf("failed to start service: %w", err)
}

return nil
}

func moveFiles(latestDirPath, installDirPath string) error {
err := filepath.WalkDir(latestDirPath, func(path string, d fs.DirEntry, err error) error {
cpheps marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

if d.IsDir() {
return nil
}

if skipFile(path) {
return nil
}
BinaryFissionGames marked this conversation as resolved.
Show resolved Hide resolved

cleanPath := filepath.Clean(path)

relPath, err := filepath.Rel(latestDirPath, cleanPath)
if err != nil {
return err
}

outPath := filepath.Clean(filepath.Join(installDirPath, relPath))
outDir := filepath.Dir(outPath)

if err := os.MkdirAll(outDir, 0750); err != nil {
return fmt.Errorf("failed to create dir: %w", err)
}

outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return fmt.Errorf("failed to open output file: %w", err)
}
defer func() {
err := outFile.Close()
if err != nil {
log.Default().Printf("installFiles: Failed to close output file: %s", err)
}
}()

inFile, err := os.Open(cleanPath)
if err != nil {
return fmt.Errorf("failed to open input file: %w", err)
}
defer func() {
err := inFile.Close()
if err != nil {
log.Default().Printf("installFiles: Failed to close input file: %s", err)
}
}()

if _, err := io.Copy(outFile, inFile); err != nil {
return fmt.Errorf("failed to copy file: %w", err)
}

return nil
})

if err != nil {
return fmt.Errorf("failed to walk latest dir: %w", err)
}

return nil
}

// skipFile returns true if the given path is a special config file.
// These files should not be overwritten.
func skipFile(path string) bool {
var configFiles = []string{
"config.yaml",
"logging.yaml",
"manager.yaml",
}

fileName := filepath.Base(path)

for _, f := range configFiles {
if fileName == f {
return true
}
}

return false
}
Loading