osbuild-service-maintenance: Explicitly enable maintenance parts

Stage and production share the GCP account. To avoid trying to delete
each GCP image twice, the maintenance script needs the ability to
selectively disable certain parts based on the config.
This commit is contained in:
Sanne Raymaekers 2022-05-13 13:35:43 +02:00 committed by Achilleas Koutsou
parent e2fe4b8de2
commit 8219dcdee8
2 changed files with 23 additions and 7 deletions

View file

@ -4,12 +4,16 @@ import (
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
"strconv"
) )
// Do not write this config to logs or stdout, it contains secrets! // Do not write this config to logs or stdout, it contains secrets!
type Config struct { type Config struct {
DryRun string `env:"DRY_RUN"` DryRun string `env:"DRY_RUN"`
MaxConcurrentRequests string `env:"MAX_CONCURRENT_REQUESTS"` MaxConcurrentRequests string `env:"MAX_CONCURRENT_REQUESTS"`
EnableDBMaintenance bool `env:"ENABLE_DB_MAINTENANCE"`
EnableGCPMaintenance bool `env:"ENABLE_GCP_MAINTENANCE"`
EnableAWSMaintenance bool `env:"ENABLE_AWS_MAINTENANCE"`
PGHost string `env:"PGHOST"` PGHost string `env:"PGHOST"`
PGPort string `env:"PGPORT"` PGPort string `env:"PGPORT"`
PGDatabase string `env:"PGDATABASE"` PGDatabase string `env:"PGDATABASE"`
@ -51,13 +55,14 @@ func LoadConfigFromEnv(intf interface{}) error {
kind := fieldV.Kind() kind := fieldV.Kind()
if ok { if ok {
switch kind { switch kind {
case reflect.Ptr:
if fieldT.Type.Elem().Kind() != reflect.String {
return fmt.Errorf("Unsupported type")
}
fieldV.Set(reflect.ValueOf(&confV))
case reflect.String: case reflect.String:
fieldV.SetString(confV) fieldV.SetString(confV)
case reflect.Bool:
value, err := strconv.ParseBool(confV)
if err != nil {
return err
}
fieldV.SetBool(value)
default: default:
return fmt.Errorf("Unsupported type") return fmt.Errorf("Unsupported type")
} }

View file

@ -26,6 +26,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
maxCReqs, err := strconv.Atoi(conf.MaxConcurrentRequests) maxCReqs, err := strconv.Atoi(conf.MaxConcurrentRequests)
if err != nil { if err != nil {
panic(err) panic(err)
@ -43,6 +44,11 @@ func main() {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
if !conf.EnableAWSMaintenance {
logrus.Info("AWS maintenance not enabled, skipping")
return
}
logrus.Info("Cleaning up AWS") logrus.Info("Cleaning up AWS")
err := AWSCleanup(maxCReqs, dryRun, conf.AWSAccessKeyID, conf.AWSSecretAccessKey, "us-east-1", cutoff) err := AWSCleanup(maxCReqs, dryRun, conf.AWSAccessKeyID, conf.AWSSecretAccessKey, "us-east-1", cutoff)
if err != nil { if err != nil {
@ -53,6 +59,11 @@ func main() {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
if !conf.EnableGCPMaintenance {
logrus.Info("GCP maintenance not enabled, skipping")
return
}
logrus.Info("Cleaning up GCP") logrus.Info("Cleaning up GCP")
var gcpConf GCPCredentialsConfig var gcpConf GCPCredentialsConfig
err := LoadConfigFromEnv(&gcpConf) err := LoadConfigFromEnv(&gcpConf)
@ -81,8 +92,8 @@ func main() {
wg.Wait() wg.Wait()
logrus.Info("🦀🦀🦀 cloud cleanup done 🦀🦀🦀") logrus.Info("🦀🦀🦀 cloud cleanup done 🦀🦀🦀")
if conf.PGHost == "" { if !conf.EnableDBMaintenance {
logrus.Info("🦀🦀🦀 db host not defined, skipping db cleanup 🦀🦀🦀") logrus.Info("🦀🦀🦀 DB maintenance not enabled, skipping 🦀🦀🦀")
return return
} }