Skip to content

Commit

Permalink
Switch from datacenter to location parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Thor77 committed May 22, 2022
1 parent 1065fd3 commit 8acc762
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ For authentication it uses the SSH agent, so ensure the private counterpart to t
[hcloud]
token = "<hetzner cloud token>"
server_type = "cx11"
datacenter = "nbg1"
location = "nbg1"
ssh_key = "<name of ssh key used for rescue and passed to template>"
private_network = "<private network server is attached to>"

Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type hcloudConfig struct {
SSHKeyPrivatePath string `toml:"ssh_key_private_path"`
PrivateNetwork string `toml:"private_network"`
ServerType string `toml:"server_type"`
Datacenter string
Location string
Image string
}

Expand Down Expand Up @@ -41,8 +41,8 @@ func verifyConfig(conf *config) error {
if conf.HCloud.ServerType == "" {
return errors.New("server type missing")
}
if conf.HCloud.Datacenter == "" {
return errors.New("datacenter missing")
if conf.HCloud.Location == "" {
return errors.New("location missing")
}
if conf.HCloud.Image == "" {
conf.HCloud.Image = "debian-11"
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ func main() {
serverExists = false
}

serverTypeName := cfg.HCloud.ServerType
datacenterName := cfg.HCloud.Datacenter
if serverExists {
log.Printf("server '%s' (id %d) already exists, checking for necessary changes\n", serverName, server.ID)
// check if redeploy is necessary -- fetching user data afterwards not possible, maybe cache locally/connect to server?
Expand Down Expand Up @@ -150,21 +148,24 @@ func main() {
log.Printf("creating server '%s'", serverName)
// create server
startAfterCreate := false
serverType, _, err := client.ServerType.GetByName(context.Background(), serverTypeName)
serverType, _, err := client.ServerType.GetByName(context.Background(), cfg.HCloud.ServerType)
if err != nil {
log.Fatalf("error finding server type: %v\n", err)
}
image, _, err := client.Image.Get(context.Background(), cfg.HCloud.Image)
if err != nil {
log.Fatalf("error finding image: %v\n", err)
}
datacenter, _, err := client.Datacenter.GetByName(context.Background(), datacenterName)
location, _, err := client.Location.GetByName(context.Background(), cfg.HCloud.Location)
if err != nil {
log.Fatalf("error finding location: %v\n", err)
}
createOpts := hcloud.ServerCreateOpts{
Name: serverName,
StartAfterCreate: &startAfterCreate,
ServerType: serverType,
Image: image,
Datacenter: datacenter,
Location: location,
SSHKeys: []*hcloud.SSHKey{sshKey},
Networks: []*hcloud.Network{privateNetwork},
}
Expand Down

0 comments on commit 8acc762

Please sign in to comment.