This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
101 lines (81 loc) · 2.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"flag"
"fmt"
"github.com/hetznercloud/hcloud-go/hcloud"
"log"
"os"
"os/signal"
"syscall"
)
func main() {
// Get System Hostname
hostname, _ := os.Hostname()
var (
hcloudApiKey string
serverName string = hostname
floatingIpName string
)
// Define flags (with env fallback)
flag.StringVar(&hcloudApiKey, "hcloud-api-key", LookupEnvOrString("HCLOUD_API_KEY", hcloudApiKey), "Hetzner Cloud API key (env \"HCLOUD_API_KEY\")")
flag.StringVar(&serverName, "server-name", LookupEnvOrString("SERVER_NAME", serverName), "Server name (env \"SERVER_NAME\")")
flag.StringVar(&floatingIpName, "floating-ip-name", LookupEnvOrString("FLOATING_IP_NAME", floatingIpName), "Floating IP name (env \"FLOATING_IP_NAME\")")
// Parse flags
flag.Parse()
// Check for HCloud API key
if hcloudApiKey == "" {
log.Fatalf("Hetzner Cloud API Key not specified! Use the \"-hcloud-api-key\" flag or the \"HCLOUD_API_KEY\" environment variable to specify it.")
}
// Check for server name
if serverName == "" {
log.Fatalf("Server name not specified! Use the \"-server-name\" flag or the \"SERVER_NAME\" environment variable to specify it. Defaults to hostname.")
}
// Check for floating IP
if floatingIpName == "" {
log.Fatalf("Floating IP name not specified! Use the \"-floating-ip-name\" flag or the \"FLOATING_IP_NAME\" environment variable to specify it.")
}
// Initialize HCloud Client
client := hcloud.NewClient(hcloud.WithToken(hcloudApiKey))
// Try to get server by name
server, _, err := client.Server.GetByName(context.Background(), serverName)
if err != nil {
// Log request error
log.Fatalf("Error retrieving server: %s", err)
}
if server != nil {
fmt.Println(fmt.Sprintf("Found server \"%v\".", server.Name))
// Get floating IP
ip, _, err := client.FloatingIP.Get(context.Background(), floatingIpName)
if err != nil {
// Log request error
log.Fatalf("Error retrieving floating IP: %s", err)
}
if ip != nil {
fmt.Println(fmt.Sprintf("Found floating IP \"%v\".", ip.Name))
// Assign floating IP to current server
_, res, err := client.FloatingIP.Assign(context.Background(), ip, server)
if err != nil {
// Log request error
log.Fatalf("Error assigning floating IP to server: %s", res.Body)
}
fmt.Println(fmt.Sprintf("Assigned floating IP \"%v\" to server \"%v\".", ip.Name, server.Name))
} else {
// Handle floating IP not found
fmt.Println(fmt.Sprintf("Unable to find floating IP named \"%v\"!", floatingIpName))
}
} else {
// Handle server not found
fmt.Println(fmt.Sprintf("Unable to find server named \"%v\"!", serverName))
}
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
<-exitSignal
fmt.Println("Stopping...")
}
func LookupEnvOrString(env string, defaultVal string) string {
if val, ok := os.LookupEnv(env); ok {
return val
}
return defaultVal
}