cloudbuild/pkg/config/config.go

88 lines
2.6 KiB
Go

// Package config provides utilities for reading and storing configuration
// values.
package config
import (
"os"
"strings"
)
func isFalsy(s string) bool {
s = strings.ToLower(s)
return s == "false" || s == "no" || s == "0"
}
// A Config holds all of the configuration values needed for the program.
type Config struct {
CloudstackApiKey string
CloudstackSecretKey string
CloudstackApiBaseUrl string
CloudstackZoneName string
CloudstackServiceOfferingName string
CloudstackKeypairName string
SSHKeyPath string
DeleteOldTemplates bool
// These must be keys of the distrosInfo map in cloudbuilder.go
DistrosToCheck []string
UploadDirectory string
UploadBaseUrl string
MirrorHost string
EmailServer string
EmailSender string
EmailSenderName string
EmailRecipient string
EmailReplyTo string
}
// New returns a Config filled with values read from environment variables.
// It panics if a required environment variable is empty or not set.
func New() *Config {
cfg := &Config{
CloudstackApiKey: os.Getenv("CLOUDSTACK_API_KEY"),
CloudstackSecretKey: os.Getenv("CLOUDSTACK_SECRET_KEY"),
SSHKeyPath: os.Getenv("SSH_KEY_PATH"),
UploadDirectory: os.Getenv("UPLOAD_DIRECTORY"),
UploadBaseUrl: os.Getenv("UPLOAD_BASE_URL"),
EmailRecipient: os.Getenv("EMAIL_RECIPIENT"),
DeleteOldTemplates: true,
}
if isFalsy(os.Getenv("DELETE_OLD_TEMPLATES")) {
cfg.DeleteOldTemplates = false
}
if cfg.CloudstackApiKey == "" {
panic("CLOUDSTACK_API_KEY is empty or not set")
}
if cfg.CloudstackSecretKey == "" {
panic("CLOUDSTACK_SECRET_KEY is empty or not set")
}
if cfg.SSHKeyPath == "" {
panic("SSH_KEY_PATH is empty or not set")
}
if val, ok := os.LookupEnv("DISTROS_TO_CHECK"); ok {
cfg.DistrosToCheck = strings.Split(val, ",")
} else {
panic("DISTROS_TO_CHECK is not set")
}
if cfg.UploadDirectory == "" {
panic("UPLOAD_DIRECTORY is empty or not set")
}
if cfg.UploadBaseUrl == "" {
panic("UPLOAD_BASE_URL is empty or not set")
}
if cfg.EmailRecipient == "" {
panic("EMAIL_RECIPIENT is empty or not set")
}
// These should never change
cfg.CloudstackApiBaseUrl = "https://cloud.csclub.uwaterloo.ca/client/api"
cfg.CloudstackZoneName = "Zone1"
cfg.CloudstackServiceOfferingName = "Small Instance"
cfg.CloudstackKeypairName = "management-keypair"
cfg.MirrorHost = "mirror.csclub.uwaterloo.ca"
cfg.EmailServer = "mail.csclub.uwaterloo.ca:25"
cfg.EmailSender = "cloudbuild@csclub.uwaterloo.ca"
cfg.EmailSenderName = "cloudbuild"
cfg.EmailReplyTo = "no-reply@csclub.uwaterloo.ca"
return cfg
}