From eb6d934512bdf8cc3cb92e2d44460363cd729fa5 Mon Sep 17 00:00:00 2001 From: Pierre Mavro Date: Sat, 14 Nov 2020 00:09:47 +0100 Subject: [PATCH] feat: adding check interval + required args --- charts/pleco/Chart.yaml | 4 ++-- charts/pleco/values.yaml | 2 +- cmd/start.go | 6 ++++-- cmd/version.go | 2 +- {pleco => core}/daemon.go | 20 ++++++++++---------- core/validate.go | 20 ++++++++++++++++++++ 6 files changed, 38 insertions(+), 16 deletions(-) rename {pleco => core}/daemon.go (50%) create mode 100644 core/validate.go diff --git a/charts/pleco/Chart.yaml b/charts/pleco/Chart.yaml index 9d28a16..96043d9 100644 --- a/charts/pleco/Chart.yaml +++ b/charts/pleco/Chart.yaml @@ -3,6 +3,6 @@ name: pleco description: Automatically removes Cloud managed services and Kubernetes resources based on tags with TTL type: application home: https://github.com/Qovery/pleco -version: 0.1.5 -appVersion: 0.1.5 +version: 0.1.6 +appVersion: 0.1.6 icon: https://github.com/Qovery/pleco/raw/main/assets/pleco_logo.png diff --git a/charts/pleco/values.yaml b/charts/pleco/values.yaml index 2d08a73..f3cb90a 100644 --- a/charts/pleco/values.yaml +++ b/charts/pleco/values.yaml @@ -3,7 +3,7 @@ replicaCount: 1 image: repository: qoveryrd/pleco pullPolicy: IfNotPresent - plecoImageTag: "v0.1.5" + plecoImageTag: "v0.1.6" environmentVariables: LOGLEVEL: info diff --git a/cmd/start.go b/cmd/start.go index 6182540..ab0a998 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/Qovery/pleco/pleco" + "github.com/Qovery/pleco/core" "github.com/spf13/cobra" ) @@ -13,7 +13,8 @@ var startCmd = &cobra.Command{ _ = setLogLevel() dryRun, _ := cmd.Flags().GetBool("dry-run") - pleco.StartDaemon(dryRun) + interval, _ := cmd.Flags().GetInt64("check-interval") + core.StartDaemon(dryRun, interval) }, } @@ -21,4 +22,5 @@ func init() { rootCmd.AddCommand(startCmd) startCmd.Flags().BoolP("dry-run", "t", false, "Dry run mode") + startCmd.Flags().Int64P("check-interval", "i", 120, "Check interval in seconds") } \ No newline at end of file diff --git a/cmd/version.go b/cmd/version.go index 652ae15..d524616 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -19,5 +19,5 @@ func init() { } func GetCurrentVersion() string { - return "0.1.5" // ci-version-check + return "0.1.6" // ci-version-check } \ No newline at end of file diff --git a/pleco/daemon.go b/core/daemon.go similarity index 50% rename from pleco/daemon.go rename to core/daemon.go index 0b47f73..1f4215d 100644 --- a/pleco/daemon.go +++ b/core/daemon.go @@ -1,31 +1,31 @@ -package pleco +package core import ( - "fmt" "github.com/Qovery/pleco/providers/aws" log "github.com/sirupsen/logrus" + "os" "time" ) -func StartDaemon(dryRun bool) { - fmt.Println("\n ____ _ _____ ____ ___ \n| _ \\| | | ____/ ___/ _ \\ \n| |_) | | | _|| | | | | |\n| __/| |___| |__| |__| |_| |\n|_| |_____|_____\\____\\___/\nBy Qovery\n") +func StartDaemon(dryRun bool, interval int64) { + log.Info("\n\n ____ _ _____ ____ ___ \n| _ \\| | | ____/ ___/ _ \\ \n| |_) | | | _|| | | | | |\n| __/| |___| |__| |__| |_| |\n|_| |_____|_____\\____\\___/\nBy Qovery\n\n") log.Info("Starting Pleco") + if dryRun { log.Info("Dry run mode enabled") } - - region := "us-east-2" + checkEnvVars() // AWS session - currentSession, err := aws.CreateSession(region) + currentSession, err := aws.CreateSession(os.Getenv("AWS_DEFAULT_REGION")) if err != nil { log.Errorf("AWS session error: %s", err) } - // check RDS - currentRdsSession := aws.RdsSession(*currentSession, region) + currentRdsSession := aws.RdsSession(*currentSession, os.Getenv("AWS_DEFAULT_REGION")) for { + // check RDS err = aws.DeleteExpiredDatabases(*currentRdsSession, "ttl", dryRun) if err != nil { log.Error(err) @@ -36,6 +36,6 @@ func StartDaemon(dryRun bool) { log.Error(err) } - time.Sleep(10 * time.Second) + time.Sleep(time.Duration(interval) * time.Second) } } \ No newline at end of file diff --git a/core/validate.go b/core/validate.go new file mode 100644 index 0000000..3ac77ef --- /dev/null +++ b/core/validate.go @@ -0,0 +1,20 @@ +package core + +import ( + "log" + "os" +) + +func checkEnvVars() { + requiredEnvVars := []string{ + "AWS_ACCESS_KEY_ID", + "AWS_SECRET_ACCESS_KEY", + "AWS_DEFAULT_REGION", + } + + for _, envVar := range requiredEnvVars { + if os.Getenv(envVar) == "" { + log.Fatalf("%s environment variable is required and not found", envVar) + } + } +} \ No newline at end of file