Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hetznercloud network, firewall and ssh-key options #4

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 57 additions & 12 deletions drivers/hetznercloud/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
var (
ErrIllegalLablePrefix = errors.New("illegal label prefix")
ErrImageNotFound = errors.New("image not found")
ErrSSHKeyNotFound = errors.New("SSH key not found")
ErrNetworkNotFound = errors.New("network not found")
ErrFirewallNotFound = errors.New("firewall not found")
)

var optionUserDataDefault = `
Expand Down Expand Up @@ -72,14 +75,18 @@ type Driver struct {
ServerType string
UserData *template.Template
Image string
SSHKeyID int
SSHKeys []string
LabelPrefix string
LabelPool string
LabelImage string
DefaultLabels map[string]string
Labels map[string]string
Config *config.Config
Location string
Networks []string
Firewalls []string
EnableIPv4 bool
EnableIPv6 bool
Name string
client *hcloud.Client
}
Expand All @@ -91,7 +98,11 @@ func New(c *cli.Context, config *config.Config, name string) (engine.Provider, e
Location: c.String("hetznercloud-location"),
ServerType: c.String("hetznercloud-server-type"),
Image: c.String("hetznercloud-image"),
SSHKeyID: c.Int("hetznercloud-ssh-key-id"),
SSHKeys: c.StringSlice("hetznercloud-ssh-keys"),
Firewalls: c.StringSlice("hetznercloud-firewalls"),
Networks: c.StringSlice("hetznercloud-networks"),
EnableIPv4: c.Bool("hetznercloud-public-ipv4-enable"),
EnableIPv6: c.Bool("hetznercloud-public-ipv6-enable"),
LabelPrefix: "wp.scaler/",
Config: config,
}
Expand Down Expand Up @@ -129,14 +140,6 @@ func New(c *cli.Context, config *config.Config, name string) (engine.Provider, e
}

func (d *Driver) DeployAgent(ctx context.Context, agent *woodpecker.Agent) error {
sshKeys := []*hcloud.SSHKey{}

if d.SSHKeyID > 0 {
sshKeys = append(sshKeys, &hcloud.SSHKey{
ID: d.SSHKeyID,
})
}

labels := engine.MergeMaps(d.DefaultLabels, d.Labels)

userdataString, err := engine.RenderUserDataTemplate(d.Config, agent, d.UserData)
Expand All @@ -152,6 +155,42 @@ func (d *Driver) DeployAgent(ctx context.Context, agent *woodpecker.Agent) error
return fmt.Errorf("%s: %w: %s", d.Name, ErrImageNotFound, d.Image)
}

sshKeys := make([]*hcloud.SSHKey, 0)
for _, item := range d.SSHKeys {
key, _, err := d.client.SSHKey.GetByName(ctx, item)
if err != nil {
return fmt.Errorf("%s: %w", d.Image, err)
}
if key == nil {
return fmt.Errorf("%s: %w: %s", d.Name, ErrSSHKeyNotFound, item)
}
sshKeys = append(sshKeys, key)
}

networks := make([]*hcloud.Network, 0)
for _, item := range d.Networks {
network, _, err := d.client.Network.GetByName(ctx, item)
if err != nil {
return fmt.Errorf("%s: %w", d.Image, err)
}
if network == nil {
return fmt.Errorf("%s: %w: %s", d.Name, ErrNetworkNotFound, item)
}
networks = append(networks, network)
}

firewalls := make([]*hcloud.ServerCreateFirewall, 0)
for _, item := range d.Firewalls {
fw, _, err := d.client.Firewall.GetByName(ctx, item)
if err != nil {
return fmt.Errorf("%s: %w", d.Image, err)
}
if fw == nil {
return fmt.Errorf("%s: %w: %s", d.Name, ErrFirewallNotFound, item)
}
firewalls = append(firewalls, &hcloud.ServerCreateFirewall{Firewall: hcloud.Firewall{ID: fw.ID}})
}

_, _, err = d.client.Server.Create(ctx, hcloud.ServerCreateOpts{
Name: agent.Name,
UserData: userdataString,
Expand All @@ -162,8 +201,14 @@ func (d *Driver) DeployAgent(ctx context.Context, agent *woodpecker.Agent) error
ServerType: &hcloud.ServerType{
Name: d.ServerType,
},
SSHKeys: sshKeys,
Labels: labels,
SSHKeys: sshKeys,
Networks: networks,
Firewalls: firewalls,
Labels: labels,
PublicNet: &hcloud.ServerCreatePublicNet{
EnableIPv4: d.EnableIPv4,
EnableIPv6: d.EnableIPv6,
},
})
if err != nil {
return fmt.Errorf("%s: %w", d.Name, err)
Expand Down
35 changes: 30 additions & 5 deletions drivers/hetznercloud/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ var DriverFlags = []cli.Flag{
EnvVars: []string{"WOODPECKER_HETZNERCLOUD_SERVER_TYPE"},
Category: category,
},
&cli.IntFlag{
Name: "hetznercloud-ssh-key-id",
Value: -1,
Usage: "id of a hetzner cloud ssh key",
EnvVars: []string{"WOODPECKER_HETZNERCLOUD_SSH_KEY_ID"},
&cli.StringSliceFlag{
Name: "hetznercloud-ssh-keys",
Usage: "names of hetzner cloud ssh keys",
EnvVars: []string{"WOODPECKER_HETZNERCLOUD_SSH_KEYS"},
Category: category,
},
&cli.StringFlag{
Expand All @@ -58,4 +57,30 @@ var DriverFlags = []cli.Flag{
EnvVars: []string{"WOODPECKER_HETZNERCLOUD_LABELS"},
Category: category,
},
&cli.StringSliceFlag{
Name: "hetznercloud-firewalls",
Usage: "names of hetzner cloud firewalls",
EnvVars: []string{"WOODPECKER_HETZNERCLOUD_FIREWALLS"},
Category: category,
},
&cli.StringSliceFlag{
Name: "hetznercloud-networks",
Usage: "names of hetzner cloud networks",
EnvVars: []string{"WOODPECKER_HETZNERCLOUD_NETWORKS"},
Category: category,
},
&cli.BoolFlag{
Name: "hetznercloud-public-ipv4-enable",
Value: true,
Usage: "enables public ipv4 network for agents",
EnvVars: []string{"WOODPECKER_HETZNERCLOUD_PUBLIC_IPV4_ENABLE"},
Category: category,
},
&cli.BoolFlag{
Name: "hetznercloud-public-ipv6-enable",
Value: true,
Usage: "enables public ipv6 network for agents",
EnvVars: []string{"WOODPECKER_HETZNERCLOUD_PUBLIC_IPV6_ENABLE"},
Category: category,
},
}