func (c *NatCommand) ExecSsh(m *state.Machine, script string) error { host := instance.SelectPublicAddress(m.Addresses()) if host == "" { return fmt.Errorf("could not resolve machine's public address") } log.Println("Configuring NAT routing on machine ", m.Id()) var options ssh.Options cmd := ssh.Command("ubuntu@"+host, []string{"sh -c 'NATCMD=$(mktemp); cat >${NATCMD}; sudo sh -x ${NATCMD}'"}, &options) cmd.Stdin = strings.NewReader(script) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return cmd.Run() }
func MatchNetworks(host, gateway *state.Machine) (string, string, error) { var bestPrefix, bestHost, bestGw string for _, hostAddr := range host.Addresses() { if hostAddr.Type != instance.Ipv4Address || isLoopback(hostAddr.Value) { continue } for _, gwAddr := range gateway.Addresses() { if gwAddr.Type != instance.Ipv4Address || isLoopback(hostAddr.Value) { continue } prefix := greatestCommonPrefix(hostAddr.Value, gwAddr.Value) if len(prefix) > len(bestPrefix) { bestPrefix = prefix bestHost = hostAddr.Value bestGw = gwAddr.Value } } } if bestHost != "" && bestGw != "" { return bestHost, bestGw, nil } else { return "", "", fmt.Errorf("failed to find common network for %s and %s", host.Id(), gateway.Id()) } }