-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (49 loc) · 1.18 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/alecthomas/template"
"github.com/jbonachera/scaleway-coreos-custom-metadata/metadata"
"github.com/spf13/cobra"
)
const mdFile = "/run/metadata/coreos"
func main() {
app := cobra.Command{
Use: "scaleway-coreos-custom-metadata",
Short: "Fetch server metadata from scaleway API",
Run: func(cmd *cobra.Command, _ []string) {
client := http.DefaultClient
client.Timeout = 10 * time.Second
md, err := metadata.Self(client)
if err != nil {
log.Fatal(err)
}
templateStr := `COREOS_CUSTOM_HOSTNAME={{ .Hostname }}
COREOS_CUSTOM_PRIVATE_IPV4={{ .PrivateIP }}
COREOS_CUSTOM_PUBLIC_IPV4={{ .PublicIP.Address }}
`
template, err := template.New("").Parse(templateStr)
if err != nil {
fmt.Fprintln(cmd.OutOrStderr(), err)
}
mdDest, err := os.Create(mdFile)
if err != nil {
log.Fatal(err)
}
template.Execute(mdDest, md)
mdDest.Close()
sshDest, err := os.Create("/home/core/.ssh/authorized_keys.d/scw-metadata")
if err != nil {
log.Fatal(err)
}
for _, keyMD := range md.SSHPublicKeys {
fmt.Fprint(sshDest, keyMD.Key)
}
sshDest.Close()
},
}
app.Execute()
}