Merge lp:~mvo/snappy/15.04-network-config into lp:~snappy-dev/snappy/15.04-deprecated

Proposed by Michael Vogt
Status: Merged
Approved by: Michael Vogt
Approved revision: 471
Merged at revision: 474
Proposed branch: lp:~mvo/snappy/15.04-network-config
Merge into: lp:~snappy-dev/snappy/15.04-deprecated
Prerequisite: lp:~mvo/snappy/15.04-modprobe-config
Diff against target: 207 lines (+116/-4)
2 files modified
coreconfig/config.go (+70/-4)
coreconfig/config_test.go (+46/-0)
To merge this branch: bzr merge lp:~mvo/snappy/15.04-network-config
Reviewer Review Type Date Requested Status
Michael Vogt (community) Approve
Sergio Schvezov Needs Fixing
Review via email: mp+270622@code.launchpad.net

This proposal supersedes a proposal from 2015-09-08.

Commit message

Unblock network configuration with pass-through networking suppport

Description of the change

Unblock network configuration with pass-through networking suppport

To post a comment you must log in.
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

Looks good, you just have the same error as in the modprobe one:
var [name] [type] = "string" (type is cruft)

review: Needs Fixing
lp:~mvo/snappy/15.04-network-config updated
471. By Michael Vogt

merged lp:snappy/15.04

Revision history for this message
Michael Vogt (mvo) wrote :

Fixed, thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'coreconfig/config.go'
2--- coreconfig/config.go 2015-09-11 16:00:10 +0000
3+++ coreconfig/config.go 2015-09-11 16:19:08 +0000
4@@ -22,6 +22,7 @@
5 "io/ioutil"
6 "os"
7 "os/exec"
8+ "path/filepath"
9 "reflect"
10 "strings"
11 "syscall"
12@@ -42,6 +43,7 @@
13
14 var (
15 modprobePath = "/etc/modprobe.d/ubuntu-core.conf"
16+ networkRoot = "/etc/network/interfaces.d/"
17 )
18
19 // ErrInvalidUnitStatus signals that a unit is not returning a status
20@@ -49,10 +51,16 @@
21 var ErrInvalidUnitStatus = errors.New("invalid unit status")
22
23 type systemConfig struct {
24- Autopilot *bool `yaml:"autopilot,omitempty"`
25- Timezone *string `yaml:"timezone,omitempty"`
26- Hostname *string `yaml:"hostname,omitempty"`
27- Modprobe *string `yaml:"modprobe,omitempty"`
28+ Autopilot *bool `yaml:"autopilot,omitempty"`
29+ Timezone *string `yaml:"timezone,omitempty"`
30+ Hostname *string `yaml:"hostname,omitempty"`
31+ Modprobe *string `yaml:"modprobe,omitempty"`
32+ Network []networkConfig `yaml:"network,omitempty"`
33+}
34+
35+type networkConfig struct {
36+ Name string `yaml:"name"`
37+ Content string `yaml:"content"`
38 }
39
40 type coreConfig struct {
41@@ -82,12 +90,17 @@
42 if err != nil {
43 return nil, err
44 }
45+ network, err := getNetwork()
46+ if err != nil {
47+ return nil, err
48+ }
49
50 config := &systemConfig{
51 Autopilot: &autopilot,
52 Timezone: &tz,
53 Hostname: &hostname,
54 Modprobe: &modprobe,
55+ Network: network,
56 }
57
58 return config, nil
59@@ -169,6 +182,25 @@
60 if err := setModprobe(*newConfig.Modprobe); err != nil {
61 return "", err
62 }
63+ case "Network":
64+ var eq = func(a, b []networkConfig) bool {
65+ if len(a) != len(b) {
66+ return false
67+ }
68+ for i, v := range a {
69+ if v != b[i] {
70+ return false
71+ }
72+ }
73+ return true
74+ }
75+ if eq(oldConfig.Network, newConfig.Network) {
76+ continue
77+ }
78+
79+ if err := setNetwork(newConfig.Network); err != nil {
80+ return "", err
81+ }
82 }
83 }
84
85@@ -212,6 +244,40 @@
86 return string(modprobe), nil
87 }
88
89+var getNetwork = func() (nc []networkConfig, err error) {
90+ filepath.Walk(networkRoot, func(path string, info os.FileInfo, err error) error {
91+ if info.IsDir() {
92+ return nil
93+ }
94+ content, err := ioutil.ReadFile(path)
95+ if err != nil {
96+ return err
97+ }
98+ nc = append(nc, networkConfig{
99+ Name: path[len(networkRoot)+1:],
100+ Content: string(content),
101+ })
102+ return nil
103+ })
104+
105+ return nc, nil
106+}
107+
108+var setNetwork = func(nc []networkConfig) error {
109+ for _, c := range nc {
110+ path := filepath.Join(networkRoot, c.Name)
111+ if c.Content == "" {
112+ os.Remove(path)
113+ continue
114+ }
115+ if err := ioutil.WriteFile(path, []byte(c.Content), 0644); err != nil {
116+ return err
117+ }
118+ }
119+
120+ return nil
121+}
122+
123 // setModprobe sets the specified modprobe config
124 var setModprobe = func(modprobe string) error {
125 return ioutil.WriteFile(modprobePath, []byte(modprobe), 0644)
126
127=== modified file 'coreconfig/config_test.go'
128--- coreconfig/config_test.go 2015-09-08 14:36:11 +0000
129+++ coreconfig/config_test.go 2015-09-11 16:19:08 +0000
130@@ -25,6 +25,7 @@
131 "testing"
132
133 . "gopkg.in/check.v1"
134+ "launchpad.net/snappy/helpers"
135 )
136
137 // Hook up check.v1 into the "go test" runner.
138@@ -47,6 +48,7 @@
139 originalCmdSystemctl = cmdSystemctl
140 originalHostnamePath = hostnamePath
141 originalModprobePath = modprobePath
142+ originalNetworkRoot = networkRoot
143 )
144
145 type ConfigTestSuite struct {
146@@ -73,6 +75,8 @@
147 hostname = host
148 return nil
149 }
150+
151+ networkRoot = c.MkDir()
152 }
153
154 func (cts *ConfigTestSuite) TearDownTest(c *C) {
155@@ -92,6 +96,7 @@
156 cmdAutopilotEnabled = originalCmdAutopilotEnabled
157 cmdSystemctl = originalCmdSystemctl
158 modprobePath = originalModprobePath
159+ networkRoot = originalNetworkRoot
160 }
161
162 // TestGet is a broad test, close enough to be an integration test for
163@@ -440,3 +445,44 @@
164 c.Assert(err, IsNil)
165 c.Assert(string(content), Equals, "blacklist floppy\nsoftdep mlx4_core post: mlx4_en\n")
166 }
167+
168+func (cts *ConfigTestSuite) TestNetworkGet(c *C) {
169+ path := filepath.Join(networkRoot, "eth0")
170+ content := "auto eth0"
171+ err := ioutil.WriteFile(path, []byte(content), 0644)
172+ c.Assert(err, IsNil)
173+
174+ nc, err := getNetwork()
175+ c.Assert(err, IsNil)
176+ c.Assert(nc, DeepEquals, []networkConfig{
177+ {Name: "eth0", Content: "auto eth0"},
178+ })
179+}
180+
181+func (cts *ConfigTestSuite) TestNetworkSet(c *C) {
182+ nc := []networkConfig{
183+ {Name: "eth0", Content: "auto eth0"},
184+ }
185+ path := filepath.Join(networkRoot, nc[0].Name)
186+ err := setNetwork(nc)
187+ c.Assert(err, IsNil)
188+ content, err := ioutil.ReadFile(path)
189+ c.Assert(err, IsNil)
190+ c.Assert(string(content), Equals, nc[0].Content)
191+}
192+
193+func (cts *ConfigTestSuite) TestNetworkSetEmptyRemoves(c *C) {
194+ path := filepath.Join(networkRoot, "eth0")
195+ content := "auto eth0"
196+ err := ioutil.WriteFile(path, []byte(content), 0644)
197+ c.Assert(err, IsNil)
198+
199+ // empty content removes
200+ nc := []networkConfig{
201+ {Name: "eth0", Content: ""},
202+ }
203+ err = setNetwork(nc)
204+ c.Assert(err, IsNil)
205+ _, err = ioutil.ReadFile(path)
206+ c.Assert(helpers.FileExists(path), Equals, false)
207+}

Subscribers

People subscribed via source and target branches