package main import ( "os" "strconv" "time" "git.stinnesbeck.com/go/jynx/ci/woodpecker" "git.stinnesbeck.com/go/jynx/scaler" "git.stinnesbeck.com/go/log" ) func main() { checkEnv() log.PrintInfo("Starting autoscaler") w := woodpecker.NewServer( os.Getenv("WOODPECKER_HOSTNAME"), os.Getenv("WOODPECKER_PROMETHEUS_AUTH_TOKEN"), ) interval := time.Duration(5) if i := os.Getenv("SCALER_POLL_INTERVAL"); i != "" { envInterval, err := strconv.Atoi(i) if err != nil { log.PrintError("can't parse SCALER_POLL_INTERVAL", err) os.Exit(1) } // overwrite default interval with the one provided by env variable interval = time.Duration(envInterval) } ticker := time.Tick(interval * time.Second) // start scaler once if err := scaler.Start(&w); err != nil { log.PrintError(err) } // start scaler every tick for range ticker { if err := scaler.Start(&w); err != nil { log.PrintError(err) } } } func checkEnv() { switch "" { case os.Getenv("WOODPECKER_HOSTNAME"): log.PrintError("WOODPECKER_HOSTNAME not set") os.Exit(1) case os.Getenv("WOODPECKER_AGENT_SECRET"): log.PrintError("WOODPECKER_AGENT_SECRET not set") os.Exit(1) case os.Getenv("WOODPECKER_PROMETHEUS_AUTH_TOKEN"): log.PrintError("WOODPECKER_PROMETHEUS_AUTH_TOKEN not set") os.Exit(1) case os.Getenv("HETZNER_SSH_KEY_NAMES"): log.PrintError("HETZNER_SSH_KEY_NAMES not set") os.Exit(1) case os.Getenv("HETZNER_CLOUD_API_TOKEN"): log.PrintError("please specify HETZNER_CLOUD_API_TOKEN") os.Exit(1) } }