~wallyworld/golxc/lxc-console-log

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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see COPYING and COPYING.LESSER file for details.

package golxc

import (
	"fmt"
	"strings"
)

const (
	defaultAddr   = "10.0.3.1"
	defaultBridge = "lxcbr0"
)

// StartNetwork starts the lxc network subsystem.
func StartNetwork() error {
	goal, _, err := networkStatus()
	if err != nil {
		return err
	}
	if goal != "start" {
		_, err := run("start", "lxc-net")
		if err != nil {
			return err
		}
	}
	return nil
}

// StopNetwork stops the lxc network subsystem.
func StopNetwork() error {
	goal, _, err := networkStatus()
	if err != nil {
		return err
	}
	if goal != "stop" {
		_, err := run("stop", "lxc-net")
		if err != nil {
			return err
		}
	}
	return nil
}

// IsNotworkRunning checks if the lxc network subsystem
// is running.
func IsNetworkRunning() (bool, error) {
	_, status, err := networkStatus()
	if err != nil {
		return false, err
	}
	return status == "running", nil
}

// NetworkAttributes returns the lxc network attributes:
// starting IP address and bridge name.
func NetworkAttributes() (addr, bridge string, err error) {
	config, err := ReadConf()
	if err != nil {
		return "", "", err
	}
	addr = config["address"]
	if addr == "" {
		addr = defaultAddr
	}
	bridge = config["bridge"]
	if bridge == "" {
		bridge = defaultBridge
	}
	return addr, bridge, nil
}

// networkStatus returns the status of the lxc network subsystem.
func networkStatus() (goal, status string, err error) {
	output, err := run("status", "lxc-net")
	if err != nil {
		return "", "", err
	}
	fields := strings.Fields(output)
	if len(fields) != 2 {
		return "", "", fmt.Errorf("unexpected status output: %q", output)
	}
	fields = strings.Split(fields[1], "/")
	if len(fields) != 2 {
		return "", "", fmt.Errorf("unexpected status output: %q", output)
	}
	return fields[0], fields[1], nil
}