This commit is contained in:
parent
2d03c2dcc4
commit
9349af6ad8
@ -21,7 +21,7 @@ type Config = struct {
|
|||||||
HcloudInstanceType string `default:"cpx21" env:"WOODPECKER_AUTOSCALER_HCLOUD_INSTANCE_TYPE"`
|
HcloudInstanceType string `default:"cpx21" env:"WOODPECKER_AUTOSCALER_HCLOUD_INSTANCE_TYPE"`
|
||||||
HcloudRegion string `default:"" env:"WOODPECKER_AUTOSCALER_HCLOUD_REGION"`
|
HcloudRegion string `default:"" env:"WOODPECKER_AUTOSCALER_HCLOUD_REGION"`
|
||||||
HcloudDatacenter string `default:"" env:"WOODPECKER_AUTOSCALER_HCLOUD_DATACENTER"`
|
HcloudDatacenter string `default:"" env:"WOODPECKER_AUTOSCALER_HCLOUD_DATACENTER"`
|
||||||
HcloudSSHKey string `default:"" env:"WOODPECKER_AUTOSCALER_HCLOUD_SSH_KEY"`
|
HcloudSSHKeys string `default:"" env:"WOODPECKER_AUTOSCALER_HCLOUD_SSH_KEYS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenConfig() (cfg *Config, err error) {
|
func GenConfig() (cfg *Config, err error) {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
||||||
@ -69,10 +70,20 @@ func CreateNewAgent(cfg *config.Config) (*hcloud.Server, error) {
|
|||||||
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
||||||
name := fmt.Sprintf("woodpecker-autoscaler-agent-%s", utils.RandStringBytes(5))
|
name := fmt.Sprintf("woodpecker-autoscaler-agent-%s", utils.RandStringBytes(5))
|
||||||
userdata, err := generateConfig(cfg, name)
|
userdata, err := generateConfig(cfg, name)
|
||||||
|
keys := []*hcloud.SSHKey{}
|
||||||
|
for _, keyName := range strings.Split(cfg.HcloudSSHKeys, ",") {
|
||||||
|
key, _, err := client.SSHKey.GetByName(context.Background(), keyName)
|
||||||
|
if err != nil {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"Caller": "CreateNewAgent",
|
||||||
|
}).Warnf("Failed to look up ssh key %s: %s", keyName, err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
img, _, err := client.Image.GetByNameAndArchitecture(context.Background(), "docker-ce", "x86")
|
img, _, err := client.Image.GetByNameAndArchitecture(context.Background(), "docker-ce", "x86")
|
||||||
loc, _, err := client.Location.GetByName(context.Background(), cfg.HcloudRegion)
|
loc, _, err := client.Location.GetByName(context.Background(), cfg.HcloudRegion)
|
||||||
pln, _, err := client.ServerType.GetByName(context.Background(), cfg.HcloudInstanceType)
|
pln, _, err := client.ServerType.GetByName(context.Background(), cfg.HcloudInstanceType)
|
||||||
key, _, err := client.SSHKey.GetByName(context.Background(), cfg.HcloudSSHKey)
|
|
||||||
dc, _, err := client.Datacenter.GetByName(context.Background(), cfg.HcloudDatacenter)
|
dc, _, err := client.Datacenter.GetByName(context.Background(), cfg.HcloudDatacenter)
|
||||||
labels := map[string]string{}
|
labels := map[string]string{}
|
||||||
labels["Role"] = "WoodpeckerAgent"
|
labels["Role"] = "WoodpeckerAgent"
|
||||||
@ -91,7 +102,7 @@ func CreateNewAgent(cfg *config.Config) (*hcloud.Server, error) {
|
|||||||
Name: name,
|
Name: name,
|
||||||
ServerType: pln,
|
ServerType: pln,
|
||||||
Image: img,
|
Image: img,
|
||||||
SSHKeys: []*hcloud.SSHKey{key},
|
SSHKeys: keys,
|
||||||
Location: loc,
|
Location: loc,
|
||||||
Datacenter: dc,
|
Datacenter: dc,
|
||||||
UserData: userdata,
|
UserData: userdata,
|
||||||
|
Loading…
Reference in New Issue
Block a user