Skip to content

Commit

Permalink
Feature/red 191 auto update of rc agent (#4)
Browse files Browse the repository at this point in the history
* feat: auto update

* feat: skip in dev

* feat: auto update

* feat: add cancel ctx

* feat: add fatal in case of missing configuration

* fix: golangci issue

* fix goreleaser
  • Loading branch information
davideimola authored May 2, 2023
1 parent a41562d commit 60dfa61
Show file tree
Hide file tree
Showing 15 changed files with 458 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*.so
*.dylib
.bin
bin

# Test binary, built with `go test -c`
*.test
Expand Down
17 changes: 12 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ builds:
binary: bin/redcarbon
main: ./cmd
ldflags:
- -s -w -X pkg.redcarbon.ai/internal/build.Version={{.Version}} -X pkg.redcarbon.ai/internal/build.DefaultHost={{.Env.API_HOST}}
- -s -w -X pkg.redcarbon.ai/internal/build.Version={{.Version}} -X pkg.redcarbon.ai/internal/build.Architecture={{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }} -X pkg.redcarbon.ai/internal/build.DefaultHost={{.Env.API_HOST}}
env:
- CGO_ENABLED=0
id: linux
goos: [linux]
goarch: [amd64, arm64]

- <<: *build_defaults
id: macos
goos: [darwin]
Expand All @@ -33,11 +33,18 @@ archives:
format: tar.gz

nfpms:
- license: MIT
maintainer: RedCarbon
- license: Apache 2.0
maintainer: RedCarbon SA
vendor: RedCarbon SA
homepage: https://redcarbon.ai
bindir: /usr
bindir: /usr/bin
description: RedCarbon's Agent
formats:
- deb
- rpm
contents:
- src: ./service/redcarbon.service
dst: /etc/systemd/system/redcarbon.service
type: config
scripts:
postinstall: ./scripts/post_install.sh
49 changes: 43 additions & 6 deletions cmd/start/cmd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package start

import (
"context"
"crypto/tls"
"os"
"os/signal"
"path"
"time"

"github.com/jasonlvhit/gocron"
"github.com/go-co-op/gocron"
"github.com/google/go-github/v50/github"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -14,6 +18,7 @@ import (
"google.golang.org/grpc/credentials/insecure"

"pkg.redcarbon.ai/internal/auth"
"pkg.redcarbon.ai/internal/build"
"pkg.redcarbon.ai/internal/routines"
agentsExternalApiV1 "pkg.redcarbon.ai/proto/redcarbon/external_api/agents/api/v1"
)
Expand All @@ -22,6 +27,7 @@ const (
hzRoutineInterval = 5
refreshRoutineInterval = 30
configRoutineInterval = 10
updateRoutineInterval = 1
)

func NewStartCmd() *cobra.Command {
Expand All @@ -33,6 +39,15 @@ func NewStartCmd() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
logrus.Infof("Starting RedCarbon Agent v%s on %s", build.Version, build.Architecture)

if viper.GetString("auth.access_token") == "" {
logrus.Fatal("No access token found. Please run `redcarbon config` to configure the agent")
}

ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()

agentsCli := mustCreateAgentCli()
defer agentsCli.Close()

Expand All @@ -43,13 +58,35 @@ func run(cmd *cobra.Command, args []string) {

client := agentsExternalApiV1.NewAgentsExternalV1SrvClient(agentsCli)
a := auth.NewAuthService(client, path.Join(confDir, "redcarbon", "config.yaml"))
r := routines.NewRoutineJobs(client, a)
gh := github.NewClient(nil)
done := make(chan bool)
r := routines.NewRoutineJobs(client, a, gh, done)

s := gocron.NewScheduler(time.UTC)

s.Every(updateRoutineInterval).Day().StartImmediately().SingletonMode().Do(r.UpdateRoutine, ctx)
s.Every(hzRoutineInterval).Seconds().StartImmediately().Do(r.HZRoutine, ctx)
s.Every(refreshRoutineInterval).Minutes().StartImmediately().SingletonMode().Do(r.Refresh, ctx)
s.Every(configRoutineInterval).Minutes().StartImmediately().SingletonMode().Do(r.ConfigRoutine, ctx)

s.StartAsync()

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

go func() {
<-c
cancelFn()
}()

select {
case <-ctx.Done():
case <-done:
}

gocron.Every(hzRoutineInterval).Seconds().From(gocron.NextTick()).Do(r.HZRoutine)
gocron.Every(refreshRoutineInterval).Minutes().From(gocron.NextTick()).Do(r.Refresh)
gocron.Every(configRoutineInterval).Minutes().From(gocron.NextTick()).Do(r.ConfigRoutine)
s.Stop()

<-gocron.Start()
logrus.Info("RedCarbon Agent stopped")
}

func mustCreateAgentCli() *grpc.ClientConn {
Expand Down
27 changes: 18 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ go 1.19

require (
github.com/bufbuild/buf v1.12.0
github.com/jasonlvhit/gocron v0.0.1
github.com/go-co-op/gocron v1.19.0
github.com/gocarina/gocsv v0.0.0-20230123225133-763e25b40669
github.com/google/go-github/v50 v50.2.0
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.2
golang.org/x/mod v0.8.0
google.golang.org/grpc v1.52.3
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0
google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8
Expand All @@ -17,8 +21,10 @@ require (
require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/bufbuild/connect-go v1.4.1 // indirect
github.com/bufbuild/protocompile v0.1.0 // indirect
github.com/cloudflare/circl v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.21+incompatible // indirect
Expand All @@ -30,13 +36,13 @@ require (
github.com/felixge/fgprof v0.9.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/gocarina/gocsv v0.0.0-20230123225133-763e25b40669 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gofrs/uuid v4.3.1+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-containerregistry v0.12.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20221203041831-ce31453925ec // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -56,6 +62,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/profile v1.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
Expand All @@ -71,13 +78,15 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/tools v0.4.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 60dfa61

Please sign in to comment.