Skip to content

Commit

Permalink
make hetzner sever type, location and firewall configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
pcallewaert committed Apr 13, 2021
1 parent 24d7d6b commit e7a4d35
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
20 changes: 13 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import (
)

var (
loglevel string
nrOfBuilders int
hcloudToken string
githubPat string
githubOwner string
imageSnapshot int
loglevel string
nrOfBuilders int
hcloudToken string
githubPat string
githubOwner string
imageSnapshot int
hcloudFirewallName string
hcloudLocation string
hcloudServerType string
)

func main() {
logrus.Info("Starting hcloud-gh-actions-provisioner...")
parseConfig()
sa := pkg.NewServerAdmin(githubPat, githubOwner, hcloudToken)
sa := pkg.NewServerAdmin(githubPat, githubOwner, hcloudToken, hcloudFirewallName, hcloudLocation, hcloudServerType)
if err := sa.ScaleTo(nrOfBuilders, imageSnapshot); err != nil {
logrus.Fatal(err)
}
Expand All @@ -29,6 +32,9 @@ func parseConfig() {
flag.IntVar(&nrOfBuilders, "number-of-builders", -1, "The number of builders that have to be scaled")
flag.IntVar(&imageSnapshot, "image-snapshot", -1, "Image ID of the snapshot to use")
flag.StringVar(&hcloudToken, "hcloud-token", "", "Hetzner Cloud API Token")
flag.StringVar(&hcloudFirewallName, "hcloud-firewall-name", "", "Hetzner Firewall Name")
flag.StringVar(&hcloudLocation, "hcloud-location", "fsn1", "Hetzner Location")
flag.StringVar(&hcloudServerType, "hcloud-server-type", "cpx21", "Hetzner Server type")
flag.StringVar(&githubPat, "github-pat", "", "Github Personal Access Token")
flag.StringVar(&githubOwner, "github-owner", "", "Github Organisation owner")

Expand Down
41 changes: 26 additions & 15 deletions pkg/server_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
const GITHUB_ACTIONS_LABEL_PREFIX = "hcloud-github-actions-"
const userdata = `
#cloud-config
disable_root: 1
ssh_pwauth: 0
write_files:
- content: |
#!/bin/bash
Expand All @@ -40,20 +42,26 @@ runcmd:
`

type ServerAdmin struct {
githubOwner string
githubPat string
githubClient *github.Client
hcloudClient *hcloud.Client
githubOwner string
githubPat string
githubClient *github.Client
hcloudClient *hcloud.Client
hcloudFirewallName string
hcloudLocation string
hcloudServerType string
}

func NewServerAdmin(githubPat string, githubOwner string, hcloudToken string) *ServerAdmin {
func NewServerAdmin(githubPat, githubOwner, hcloudToken, hcloudFirewallName, hcloudLocation, hcloudServerType string) *ServerAdmin {
githubClient := setupGithubClient(githubPat)
hcloudClient := setupHcloudClient(hcloudToken)
return &ServerAdmin{
githubClient: githubClient,
hcloudClient: hcloudClient,
githubOwner: githubOwner,
githubPat: githubPat,
githubClient: githubClient,
hcloudClient: hcloudClient,
githubOwner: githubOwner,
githubPat: githubPat,
hcloudFirewallName: hcloudFirewallName,
hcloudServerType: hcloudServerType,
hcloudLocation: hcloudLocation,
}
}

Expand Down Expand Up @@ -154,17 +162,20 @@ func (sa *ServerAdmin) spinUpServer(serverName, userdata string, imageSnapshot i
if err != nil {
logrus.Fatalf("Error retrieving image: %v", err)
}
serverType, _, err := sa.hcloudClient.ServerType.GetByName(context.Background(), "cpx21")
serverType, _, err := sa.hcloudClient.ServerType.GetByName(context.Background(), sa.hcloudServerType)
if err != nil {
logrus.Fatalf("Error retrieving servertype: %v", err)
}
location, _, err := sa.hcloudClient.Location.GetByName(context.Background(), "fsn1")
location, _, err := sa.hcloudClient.Location.GetByName(context.Background(), sa.hcloudLocation)
if err != nil {
logrus.Fatalf("Error retrieving location: %v", err)
}
firewall, _, err := sa.hcloudClient.Firewall.GetByName(context.Background(), "buildserver-firewall")
if err != nil {
logrus.Fatalf("Error retrieving firewall: %v", err)
var fw *hcloud.Firewall
if sa.hcloudFirewallName != "" {
fw, _, err = sa.hcloudClient.Firewall.GetByName(context.Background(), sa.hcloudFirewallName)
if err != nil {
logrus.Fatalf("Error retrieving firewall: %v", err)
}
}
token, _, err := sa.githubClient.Actions.CreateOrganizationRegistrationToken(context.Background(), sa.githubOwner)
if err != nil {
Expand All @@ -181,7 +192,7 @@ func (sa *ServerAdmin) spinUpServer(serverName, userdata string, imageSnapshot i
Location: location,
Name: serverName,
Labels: map[string]string{"runner": "automated"},
Firewalls: []*hcloud.ServerCreateFirewall{{Firewall: *firewall}},
Firewalls: []*hcloud.ServerCreateFirewall{{Firewall: *fw}},
UserData: formattedUserData,
}
_, _, err = sa.hcloudClient.Server.Create(context.Background(), opts)
Expand Down

0 comments on commit e7a4d35

Please sign in to comment.