forked from metal-stack/metal-hammer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (78 loc) · 2.07 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
package main
import (
"fmt"
"github.com/metal-stack/v"
"os"
"time"
log "github.com/inconshreveable/log15"
"github.com/metal-stack/metal-hammer/cmd"
"github.com/metal-stack/metal-hammer/cmd/event"
"github.com/metal-stack/metal-hammer/cmd/network"
"github.com/metal-stack/metal-hammer/pkg/kernel"
"github.com/pkg/errors"
)
func main() {
fmt.Print(cmd.HammerBanner)
// Reboot if metal-hammer crashes after 60sec.
go kernel.Watchdog()
err := updateResolvConf()
if err != nil {
log.Error("error updating resolv.conf", "error", err)
os.Exit(1)
}
ip := network.InternalIP()
err = cmd.StartSSHD(ip)
if err != nil {
log.Error("sshd error", "error", err)
os.Exit(1)
}
if len(os.Args) > 1 {
log.Error("cmd args are not supported")
os.Exit(1)
}
log.Info("metal-hammer", "version", v.V)
spec := cmd.NewSpec(ip)
spec.Log()
var level log.Lvl
if spec.Debug {
level = log.LvlDebug
} else {
level = log.LvlInfo
}
h := log.CallerFileHandler(log.StdoutHandler)
h = log.LvlFilterHandler(level, h)
log.Root().SetHandler(h)
emitter, err := cmd.Run(spec)
if err != nil {
wait := 5 * time.Second
st := errors.WithStack(err)
fmt.Printf("%+v", st)
log.Error("metal-hammer failed", "rebooting in", wait, "error", err)
emitter.Emit(event.ProvisioningEventCrashed, fmt.Sprintf("%s", err))
time.Sleep(wait)
err := kernel.Reboot()
if err != nil {
log.Error("metal-hammer reboot failed", "error", err)
emitter.Emit(event.ProvisioningEventCrashed, fmt.Sprintf("%s", err))
}
}
}
func updateResolvConf() error {
// when starting the metal-hammer u-root sets a static resolv.conf file containing 8.8.8.8
// this can only be overriden by running dhclient
// however, we can use the dhcp information that the kernel used during startup
// this information is contained in /proc/net/pnp
//
// https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
symlink := "/etc/resolv.conf"
target := "/proc/net/pnp"
err := os.Remove(symlink)
if err != nil {
return err
}
err = os.Symlink(target, symlink)
if err != nil {
return err
}
return nil
}