Esempio n. 1
0
func (c *client) Expose() error {
	output, err := exec.Command("weave", "--local", "ps", "weave:expose").Output()
	if err != nil {
		return err
	}
	ips := ipMatch.FindAllSubmatch(output, -1)
	if ips != nil {
		// Alread exposed!
		return nil
	}
	if err := exec.Command("weave", "--local", "expose").Run(); err != nil {
		return fmt.Errorf("Error running weave expose: %v", err)
	}
	return nil
}
Esempio n. 2
0
func (w *Weave) ps() ([]psEntry, error) {
	var result []psEntry
	cmd := exec.Command("weave", "--local", "ps")
	out, err := cmd.StdoutPipe()
	if err != nil {
		return result, err
	}
	if err := cmd.Start(); err != nil {
		return result, err
	}
	defer func() {
		if err := cmd.Wait(); err != nil {
			log.Printf("Weave tagger, cmd failed: %v", err)
		}
	}()
	scanner := bufio.NewScanner(out)
	for scanner.Scan() {
		line := scanner.Text()
		groups := weavePsMatch.FindStringSubmatch(line)
		if len(groups) == 0 {
			continue
		}
		containerIDPrefix, macAddress, ips := groups[1], groups[2], []string{}
		for _, ipGroup := range ipMatch.FindAllStringSubmatch(groups[3], -1) {
			ips = append(ips, ipGroup[1])
		}
		result = append(result, psEntry{containerIDPrefix, macAddress, ips})
	}
	return result, scanner.Err()
}
Esempio n. 3
0
// NB this is not re-entrant!
func (c *Conntracker) run(args ...string) {
	args = append([]string{"-E", "-o", "xml"}, args...)
	cmd := exec.Command("conntrack", args...)
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Printf("conntrack error: %v", err)
		return
	}
	if err := cmd.Start(); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	}

	c.Lock()
	c.cmd = cmd
	c.Unlock()
	defer func() {
		if err := cmd.Wait(); err != nil {
			log.Printf("conntrack error: %v", err)
		}
	}()

	// Swallow the first two lines
	reader := bufio.NewReader(stdout)
	if line, err := reader.ReadString('\n'); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	} else if line != XMLHeader {
		log.Printf("conntrack invalid output: '%s'", line)
		return
	}
	if line, err := reader.ReadString('\n'); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	} else if line != ConntrackOpenTag {
		log.Printf("conntrack invalid output: '%s'", line)
		return
	}

	// Now loop on the output stream
	decoder := xml.NewDecoder(reader)
	for {
		var f Flow
		if err := decoder.Decode(&f); err != nil {
			log.Printf("conntrack error: %v", err)
		}
		c.handleFlow(f)
	}
}
Esempio n. 4
0
func (c *client) PS() (map[string]PSEntry, error) {
	cmd := exec.Command("weave", "--local", "ps")
	out, err := cmd.StdoutPipe()
	if err != nil {
		return nil, err
	}
	if err := cmd.Start(); err != nil {
		return nil, err
	}
	defer func() {
		if err := cmd.Wait(); err != nil {
			log.Errorf("'weave ps' cmd failed: %v", err)
		}
	}()

	psEntriesByPrefix := map[string]PSEntry{}
	scanner := bufio.NewScanner(out)
	for scanner.Scan() {
		line := scanner.Text()
		groups := weavePsMatch.FindStringSubmatch(line)
		if len(groups) == 0 {
			continue
		}
		containerIDPrefix, macAddress, ips := groups[1], groups[2], []string{}
		for _, ipGroup := range ipMatch.FindAllStringSubmatch(groups[3], -1) {
			ips = append(ips, ipGroup[1])
		}
		psEntriesByPrefix[containerIDPrefix] = PSEntry{
			ContainerIDPrefix: containerIDPrefix,
			MACAddress:        macAddress,
			IPs:               ips,
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return psEntriesByPrefix, nil
}
Esempio n. 5
0
func (c *conntrackWalker) existingConnections() ([]flow, error) {
	args := append([]string{"-L", "-o", "xml", "-p", "tcp"}, c.args...)
	cmd := exec.Command("conntrack", args...)
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return []flow{}, err
	}
	if err := cmd.Start(); err != nil {
		return []flow{}, err
	}
	defer func() {
		if err := cmd.Wait(); err != nil {
			log.Printf("conntrack existingConnections exit error: %v", err)
		}
	}()
	var result conntrack
	if err := xml.NewDecoder(stdout).Decode(&result); err == io.EOF {
		return []flow{}, nil
	} else if err != nil {
		return []flow{}, err
	}
	return result.Flows, nil
}
Esempio n. 6
0
func (c *conntrackWalker) run() {
	// Fork another conntrack, just to capture existing connections
	// for which we don't get events
	existingFlows, err := c.existingConnections()
	if err != nil {
		log.Printf("conntrack existingConnections error: %v", err)
		return
	}
	for _, flow := range existingFlows {
		c.handleFlow(flow, true)
	}

	args := append([]string{"-E", "-o", "xml", "-p", "tcp"}, c.args...)
	cmd := exec.Command("conntrack", args...)
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Printf("conntrack error: %v", err)
		return
	}

	stderr, err := cmd.StderrPipe()
	if err != nil {
		log.Printf("conntrack error: %v", err)
		return
	}
	go logPipe("conntrack stderr:", stderr)

	if err := cmd.Start(); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	}

	defer func() {
		if err := cmd.Wait(); err != nil {
			log.Printf("conntrack error: %v", err)
		}
	}()

	c.Lock()
	// We may have stopped in the mean time,
	// so check to see if the channel is open
	// under the lock.
	select {
	default:
	case <-c.quit:
		return
	}
	c.cmd = cmd
	c.Unlock()

	// Swallow the first two lines
	reader := bufio.NewReader(stdout)
	if line, err := reader.ReadString('\n'); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	} else if line != xmlHeader {
		log.Printf("conntrack invalid output: '%s'", line)
		return
	}
	if line, err := reader.ReadString('\n'); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	} else if line != conntrackOpenTag {
		log.Printf("conntrack invalid output: '%s'", line)
		return
	}

	defer log.Printf("contrack exiting")

	// Now loop on the output stream
	decoder := xml.NewDecoder(reader)
	for {
		var f flow
		if err := decoder.Decode(&f); err != nil {
			log.Printf("conntrack error: %v", err)
			return
		}
		c.handleFlow(f, false)
	}
}
Esempio n. 7
0
// NB this is not re-entrant!
func (c *conntracker) run(args ...string) {
	if c.existingConns {
		// Fork another conntrack, just to capture existing connections
		// for which we don't get events
		existingFlows, err := c.existingConnections(args...)
		if err != nil {
			log.Printf("conntrack existingConnections error: %v", err)
			return
		}
		for _, flow := range existingFlows {
			c.handleFlow(flow, true)
		}
	}

	args = append([]string{"-E", "-o", "xml", "-p", "tcp"}, args...)
	cmd := exec.Command("conntrack", args...)
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Printf("conntrack error: %v", err)
		return
	}
	if err := cmd.Start(); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	}

	c.Lock()
	c.cmd = cmd
	c.Unlock()
	defer func() {
		if err := cmd.Wait(); err != nil {
			log.Printf("conntrack error: %v", err)
		}
	}()

	// Swallow the first two lines
	reader := bufio.NewReader(stdout)
	if line, err := reader.ReadString('\n'); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	} else if line != XMLHeader {
		log.Printf("conntrack invalid output: '%s'", line)
		return
	}
	if line, err := reader.ReadString('\n'); err != nil {
		log.Printf("conntrack error: %v", err)
		return
	} else if line != ConntrackOpenTag {
		log.Printf("conntrack invalid output: '%s'", line)
		return
	}

	defer log.Printf("contrack exiting")

	// Now loop on the output stream
	decoder := xml.NewDecoder(reader)
	for {
		var f Flow
		if err := decoder.Decode(&f); err != nil {
			log.Printf("conntrack error: %v", err)
			return
		}
		c.handleFlow(f, false)
	}
}