-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdns.go
85 lines (76 loc) · 1.81 KB
/
dns.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
package config
import (
"context"
"fmt"
"io/ioutil"
"os"
"text/template"
"github.com/containerd/containerd"
"github.com/crosbymichael/boss/systemd"
"github.com/urfave/cli"
)
const hostsFile = `127.0.0.1 localhost.localdomain localhost {{.ID}}
::1 localhost6.localdomain6 localhost6 aegis-03
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts`
type DNS struct {
ID string
}
func (s *DNS) Name() string {
return "dns"
}
func (s *DNS) Run(ctx context.Context, client *containerd.Client, clix *cli.Context) error {
if err := systemd.Command(ctx, "disable", "systemd-resolved"); err != nil {
return err
}
if err := systemd.Command(ctx, "stop", "systemd-resolved"); err != nil {
return err
}
if err := writeResolveConf("127.0.0.1"); err != nil {
return err
}
tmpl, err := template.New("hosts").Parse(hostsFile)
if err != nil {
return err
}
t, err := ioutil.TempFile("", "boss-hosts")
if err != nil {
return err
}
if err := t.Chmod(0666); err != nil {
return err
}
err = tmpl.Execute(t, s)
t.Close()
if err != nil {
return err
}
return os.Rename(t.Name(), "/etc/hosts")
}
func (s *DNS) Remove(ctx context.Context, client *containerd.Client, clix *cli.Context) error {
return writeResolveConf("8.8.8.8", "8.8.4.4")
}
func writeResolveConf(nameservers ...string) error {
t, err := ioutil.TempFile("", "boss-resolvconf")
if err != nil {
return err
}
if err := t.Chmod(0666); err != nil {
return err
}
for _, ns := range nameservers {
if _, err := t.WriteString(fmt.Sprintf("nameserver %s\n", ns)); err != nil {
t.Close()
return err
}
}
t.Close()
if err != nil {
return err
}
return os.Rename(t.Name(), "/etc/resolv.conf")
}