Move rewrite /etc/hosts from shell to Go
This commit is contained in:
1 parent
be917eb86b
commit
edbf85655a
4 files changed
+97
-40
No files matched your search
@@ -3,13 +3,16 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
docker "github.com/fsouza/go-dockerclient"
|
||||
"github.com/vishvananda/netns"
|
||||
|
||||
weavenet "github.com/weaveworks/weave/net"
|
||||
"github.com/weaveworks/weave/proxy"
|
||||
)
|
||||
|
||||
func attach(args []string) error {
|
||||
@@ -110,3 +113,30 @@ func detach(args []string) error {
|
||||
}
|
||||
return weavenet.DetachContainer(weavenet.NSPathByPid(pid), args[0], weavenet.VethName, cidrs)
|
||||
}
|
||||
|
||||
func rewriteEtcHosts(args []string) error {
|
||||
if len(args) < 4 {
|
||||
cmdUsage("rewrite-etc-hosts", "<hosts-path> <fqdn> <image> <cidr>... [name:addr...]")
|
||||
}
|
||||
hostsPath := args[0]
|
||||
fqdn := args[1]
|
||||
image := args[2]
|
||||
var ips []*net.IPNet
|
||||
for _, cidr := range strings.Fields(args[3]) {
|
||||
_, ipnet, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ips = append(ips, ipnet)
|
||||
}
|
||||
extraHosts := args[4:]
|
||||
docker := os.Getenv("DOCKER_HOST")
|
||||
if docker == "" {
|
||||
docker = "unix:///var/run/docker.sock"
|
||||
}
|
||||
p, err := proxy.StubProxy(proxy.Config{DockerHost: docker, Image: image})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return p.RewriteEtcHosts(hostsPath, fqdn, ips, extraHosts)
|
||||
}
|
||||
@@ -40,6 +40,7 @@ func init() {
|
||||
"unique-id": uniqueID,
|
||||
"swarm-manager-peers": swarmManagerPeers,
|
||||
"is-docker-plugin-enabled": isDockerPluginEnabled,
|
||||
"rewrite-etc-hosts": rewriteEtcHosts,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user