Ubuntu Pastebin

Paste from dimitern at Fri, 10 Jun 2016 16:01:31 +0000

Download as text
  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
diff --git a/cloudconfig/containerinit/container_userdata.go b/cloudconfig/containerinit/container_userdata.go
index b05e50f..905701f 100644
--- a/cloudconfig/containerinit/container_userdata.go
+++ b/cloudconfig/containerinit/container_userdata.go
@@ -65,8 +65,7 @@ var networkInterfacesFile = "/etc/network/interfaces"
 // Interfaces field.
 func GenerateNetworkConfig(networkConfig *container.NetworkConfig) (string, error) {
 	if networkConfig == nil || len(networkConfig.Interfaces) == 0 {
-		logger.Tracef("no network config to generate")
-		return "", nil
+		return "", errors.Errorf("missing container network config")
 	}
 	logger.Debugf("generating network config from %#v", *networkConfig)
 
@@ -100,6 +99,9 @@ func GenerateNetworkConfig(networkConfig *container.NetworkConfig) (string, erro
 		if !hasAddress {
 			output.WriteString("iface " + name + " inet manual\n")
 			continue
+		} else if address == string(network.ConfigDHCP) {
+			output.WriteString("iface " + name + " inet dhcp\n")
+			continue
 		}
 
 		output.WriteString("iface " + name + " inet static\n")
@@ -148,6 +150,8 @@ func PrepareNetworkConfigFromInterfaces(interfaces []network.InterfaceInfo) *Pre
 
 		if cidr := info.CIDRAddress(); cidr != "" {
 			nameToAddress[info.InterfaceName] = cidr
+		} else if info.ConfigType == network.ConfigDHCP {
+			nameToAddress[info.InterfaceName] = string(network.ConfigDHCP)
 		}
 
 		for _, dns := range info.DNSServers {
@@ -181,13 +185,13 @@ func PrepareNetworkConfigFromInterfaces(interfaces []network.InterfaceInfo) *Pre
 // might include per-interface networking config if both networkConfig
 // is not nil and its Interfaces field is not empty.
 func newCloudInitConfigWithNetworks(series string, networkConfig *container.NetworkConfig) (cloudinit.CloudConfig, error) {
-	cloudConfig, err := cloudinit.New(series)
+	config, err := GenerateNetworkConfig(networkConfig)
 	if err != nil {
 		return nil, errors.Trace(err)
 	}
-	config, err := GenerateNetworkConfig(networkConfig)
-	if err != nil || len(config) == 0 {
-		return cloudConfig, errors.Trace(err)
+	cloudConfig, err := cloudinit.New(series)
+	if err != nil {
+		return nil, errors.Trace(err)
 	}
 
 	cloudConfig.AddBootTextFile(networkInterfacesFile, config, 0644)
diff --git a/cloudconfig/containerinit/container_userdata_test.go b/cloudconfig/containerinit/container_userdata_test.go
index 1949c64..04d3af9 100644
--- a/cloudconfig/containerinit/container_userdata_test.go
+++ b/cloudconfig/containerinit/container_userdata_test.go
@@ -66,9 +66,17 @@ func (s *UserDataSuite) SetUpTest(c *gc.C) {
 		InterfaceName: "eth2",
 		ConfigType:    network.ConfigDHCP,
 		NoAutoStart:   true,
+	}, {
+		InterfaceName: "eth3",
+		ConfigType:    network.ConfigDHCP,
+		NoAutoStart:   false,
+	}, {
+		InterfaceName: "eth4",
+		ConfigType:    network.ConfigManual,
+		NoAutoStart:   true,
 	}}
 	s.expectedNetConfig = `
-auto eth0 eth1 lo
+auto eth0 eth1 eth3 lo
 
 iface lo inet loopback
   dns-nameservers ns1.invalid ns2.invalid
@@ -81,20 +89,23 @@ iface eth0 inet static
 iface eth1 inet static
   address 0.1.2.4/24
 
-iface eth2 inet manual
+iface eth2 inet dhcp
+
+iface eth3 inet dhcp
+
+iface eth4 inet manual
 `
 	s.PatchValue(containerinit.NetworkInterfacesFile, s.networkInterfacesFile)
 }
 
 func (s *UserDataSuite) TestGenerateNetworkConfig(c *gc.C) {
-	// No config or no interfaces - no error, but also noting to generate.
 	data, err := containerinit.GenerateNetworkConfig(nil)
-	c.Assert(err, jc.ErrorIsNil)
-	c.Assert(data, gc.HasLen, 0)
+	c.Assert(err, gc.ErrorMatches, "missing container network config")
+	c.Assert(data, gc.Equals, "")
 	netConfig := container.BridgeNetworkConfig("foo", 0, nil)
 	data, err = containerinit.GenerateNetworkConfig(netConfig)
-	c.Assert(err, jc.ErrorIsNil)
-	c.Assert(data, gc.HasLen, 0)
+	c.Assert(err, gc.ErrorMatches, "missing container network config")
+	c.Assert(data, gc.Equals, "")
 
 	// Test with all interface types.
 	netConfig = container.BridgeNetworkConfig("foo", 0, s.fakeInterfaces)
@@ -118,7 +129,7 @@ bootcmd:
 - install -D -m 644 /dev/null '%[1]s'
 - |-
   printf '%%s\n' '
-  auto eth0 eth1 lo
+  auto eth0 eth1 eth3 lo
 
   iface lo inet loopback
     dns-nameservers ns1.invalid ns2.invalid
@@ -131,7 +142,11 @@ bootcmd:
   iface eth1 inet static
     address 0.1.2.4/24
 
-  iface eth2 inet manual
+  iface eth2 inet dhcp
+
+  iface eth3 inet dhcp
+
+  iface eth4 inet manual
   ' > '%[1]s'
 runcmd:
 - ifup -a || true
@@ -142,9 +157,8 @@ runcmd:
 func (s *UserDataSuite) TestNewCloudInitConfigWithNetworksNoConfig(c *gc.C) {
 	netConfig := container.BridgeNetworkConfig("foo", 0, nil)
 	cloudConf, err := containerinit.NewCloudInitConfigWithNetworks("quantal", netConfig)
-	c.Assert(err, jc.ErrorIsNil)
-	expected := "#cloud-config\n{}\n"
-	assertUserData(c, cloudConf, expected)
+	c.Assert(err, gc.ErrorMatches, "missing container network config")
+	c.Assert(cloudConf, gc.IsNil)
 }
 
 func (s *UserDataSuite) TestCloudInitUserData(c *gc.C) {
@@ -152,10 +166,8 @@ func (s *UserDataSuite) TestCloudInitUserData(c *gc.C) {
 	c.Assert(err, jc.ErrorIsNil)
 	networkConfig := container.BridgeNetworkConfig("foo", 0, nil)
 	data, err := containerinit.CloudInitUserData(instanceConfig, networkConfig)
-	c.Assert(err, jc.ErrorIsNil)
-	// No need to test the exact contents here, as they are already
-	// tested separately.
-	c.Assert(string(data), jc.HasPrefix, "#cloud-config\n")
+	c.Assert(err, gc.ErrorMatches, "missing container network config")
+	c.Assert(data, gc.IsNil)
 }
 
 func assertUserData(c *gc.C, cloudConf cloudinit.CloudConfig, expected string) {
diff --git a/worker/provisioner/lxc-broker.go b/worker/provisioner/lxc-broker.go
index cc7c84c..35c97ff 100644
--- a/worker/provisioner/lxc-broker.go
+++ b/worker/provisioner/lxc-broker.go
@@ -644,19 +644,26 @@ func prepareOrGetContainerInterfaceInfo(
 
 	log.Debugf("address allocation feature flag not enabled; using multi-bridge networking for container %q", machineID)
 
-	// In case we're running on MAAS 1.8+ with devices support, we'll still
-	// call PrepareContainerInterfaceInfo(), but we'll ignore a NotSupported
-	// error if we get it (which means we're not using MAAS 1.8+).
+	fallbackInfo := []network.InterfaceInfo{{
+		InterfaceName: "eth0",
+		ConfigType:    network.ConfigDHCP,
+	}}
+
 	containerTag := names.NewMachineTag(machineID)
 	preparedInfo, err := api.PrepareContainerInterfaceInfo(containerTag)
 	if err != nil && errors.IsNotSupported(err) {
-		log.Warningf("new container %q not registered as device: not running on MAAS 1.8+", machineID)
-		return nil, nil
+		log.Warningf("%v (using fallback config)", err)
 	} else if err != nil {
 		return nil, errors.Trace(err)
 	}
 	log.Tracef("PrepareContainerInterfaceInfo returned %+v", preparedInfo)
 
+	// Use the fallback network config as a last resort.
+	if len(preparedInfo) == 0 {
+		log.Infof("using fallback network config for container %q", machineID)
+		preparedInfo = fallbackInfo
+	}
+
 	dnsServersFound := false
 	for _, info := range preparedInfo {
 		if len(info.DNSServers) > 0 {
Download as text