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

Proposed by Michael Vogt
Status: Merged
Approved by: Michael Vogt
Approved revision: 473
Merged at revision: 475
Proposed branch: lp:~mvo/snappy/15.04-ppp-config
Merge into: lp:~snappy-dev/snappy/15.04-deprecated
Prerequisite: lp:~mvo/snappy/15.04-network-config
Diff against target: 320 lines (+158/-30)
2 files modified
coreconfig/config.go (+61/-27)
coreconfig/config_test.go (+97/-3)
To merge this branch: bzr merge lp:~mvo/snappy/15.04-ppp-config
Reviewer Review Type Date Requested Status
Michael Vogt (community) Approve
Sergio Schvezov Needs Fixing
Review via email: mp+270624@code.launchpad.net

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

Commit message

Add configuration for ppp via ubuntu-core-config

Description of the change

This branch adds configuration for ppp via ubuntu-core-config

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
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

one nitpick

lp:~mvo/snappy/15.04-ppp-config updated
472. By Michael Vogt

address review comments

473. By Michael Vogt

merged partent

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

Thanks, fixed!

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:20:51 +0000
3+++ coreconfig/config.go 2015-09-11 16:20:51 +0000
4@@ -44,6 +44,7 @@
5 var (
6 modprobePath = "/etc/modprobe.d/ubuntu-core.conf"
7 networkRoot = "/etc/network/interfaces.d/"
8+ pppRoot = "/etc/ppp/"
9 )
10
11 // ErrInvalidUnitStatus signals that a unit is not returning a status
12@@ -51,14 +52,15 @@
13 var ErrInvalidUnitStatus = errors.New("invalid unit status")
14
15 type systemConfig struct {
16- Autopilot *bool `yaml:"autopilot,omitempty"`
17- Timezone *string `yaml:"timezone,omitempty"`
18- Hostname *string `yaml:"hostname,omitempty"`
19- Modprobe *string `yaml:"modprobe,omitempty"`
20- Network []networkConfig `yaml:"network,omitempty"`
21+ Autopilot *bool `yaml:"autopilot,omitempty"`
22+ Timezone *string `yaml:"timezone,omitempty"`
23+ Hostname *string `yaml:"hostname,omitempty"`
24+ Modprobe *string `yaml:"modprobe,omitempty"`
25+ Network []passthroughConfig `yaml:"network,omitempty"`
26+ PPP []passthroughConfig `yaml:"ppp,omitempty"`
27 }
28
29-type networkConfig struct {
30+type passthroughConfig struct {
31 Name string `yaml:"name"`
32 Content string `yaml:"content"`
33 }
34@@ -94,6 +96,10 @@
35 if err != nil {
36 return nil, err
37 }
38+ ppp, err := getPPP()
39+ if err != nil {
40+ return nil, err
41+ }
42
43 config := &systemConfig{
44 Autopilot: &autopilot,
45@@ -101,6 +107,7 @@
46 Hostname: &hostname,
47 Modprobe: &modprobe,
48 Network: network,
49+ PPP: ppp,
50 }
51
52 return config, nil
53@@ -126,6 +133,19 @@
54 return string(out), nil
55 }
56
57+func passthroughEqual(a, b []passthroughConfig) bool {
58+ if len(a) != len(b) {
59+ return false
60+ }
61+ for i, v := range a {
62+ if v != b[i] {
63+ return false
64+ }
65+ }
66+
67+ return true
68+}
69+
70 // Set is used to configure settings for the system, this is meant to
71 // be used as an interface for snappy config to satisfy the ubuntu-core
72 // hook.
73@@ -183,24 +203,21 @@
74 return "", err
75 }
76 case "Network":
77- var eq = func(a, b []networkConfig) bool {
78- if len(a) != len(b) {
79- return false
80- }
81- for i, v := range a {
82- if v != b[i] {
83- return false
84- }
85- }
86- return true
87- }
88- if eq(oldConfig.Network, newConfig.Network) {
89+ if passthroughEqual(oldConfig.Network, newConfig.Network) {
90 continue
91 }
92
93 if err := setNetwork(newConfig.Network); err != nil {
94 return "", err
95 }
96+ case "PPP":
97+ if passthroughEqual(oldConfig.PPP, newConfig.PPP) {
98+ continue
99+ }
100+
101+ if err := setPPP(newConfig.PPP); err != nil {
102+ return "", err
103+ }
104 }
105 }
106
107@@ -244,8 +261,8 @@
108 return string(modprobe), nil
109 }
110
111-var getNetwork = func() (nc []networkConfig, err error) {
112- filepath.Walk(networkRoot, func(path string, info os.FileInfo, err error) error {
113+func getPassthrough(rootDir string) (pc []passthroughConfig, err error) {
114+ filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
115 if info.IsDir() {
116 return nil
117 }
118@@ -253,23 +270,24 @@
119 if err != nil {
120 return err
121 }
122- nc = append(nc, networkConfig{
123- Name: path[len(networkRoot)+1:],
124+ pc = append(pc, passthroughConfig{
125+ Name: path[len(rootDir)+1:],
126 Content: string(content),
127 })
128 return nil
129 })
130
131- return nc, nil
132+ return pc, nil
133+
134 }
135-
136-var setNetwork = func(nc []networkConfig) error {
137- for _, c := range nc {
138- path := filepath.Join(networkRoot, c.Name)
139+func setPassthrough(rootDir string, pc []passthroughConfig) error {
140+ for _, c := range pc {
141+ path := filepath.Join(rootDir, c.Name)
142 if c.Content == "" {
143 os.Remove(path)
144 continue
145 }
146+
147 if err := ioutil.WriteFile(path, []byte(c.Content), 0644); err != nil {
148 return err
149 }
150@@ -278,6 +296,22 @@
151 return nil
152 }
153
154+var getNetwork = func() (pc []passthroughConfig, err error) {
155+ return getPassthrough(networkRoot)
156+}
157+
158+var setNetwork = func(pc []passthroughConfig) error {
159+ return setPassthrough(networkRoot, pc)
160+}
161+
162+var getPPP = func() (pc []passthroughConfig, err error) {
163+ return getPassthrough(pppRoot)
164+}
165+
166+var setPPP = func(pc []passthroughConfig) error {
167+ return setPassthrough(pppRoot, pc)
168+}
169+
170 // setModprobe sets the specified modprobe config
171 var setModprobe = func(modprobe string) error {
172 return ioutil.WriteFile(modprobePath, []byte(modprobe), 0644)
173
174=== modified file 'coreconfig/config_test.go'
175--- coreconfig/config_test.go 2015-09-11 16:20:51 +0000
176+++ coreconfig/config_test.go 2015-09-11 16:20:51 +0000
177@@ -49,6 +49,7 @@
178 originalHostnamePath = hostnamePath
179 originalModprobePath = modprobePath
180 originalNetworkRoot = networkRoot
181+ originalPppRoot = pppRoot
182 )
183
184 type ConfigTestSuite struct {
185@@ -77,6 +78,7 @@
186 }
187
188 networkRoot = c.MkDir()
189+ pppRoot = c.MkDir()
190 }
191
192 func (cts *ConfigTestSuite) TearDownTest(c *C) {
193@@ -97,6 +99,7 @@
194 cmdSystemctl = originalCmdSystemctl
195 modprobePath = originalModprobePath
196 networkRoot = originalNetworkRoot
197+ pppRoot = originalPppRoot
198 }
199
200 // TestGet is a broad test, close enough to be an integration test for
201@@ -454,13 +457,13 @@
202
203 nc, err := getNetwork()
204 c.Assert(err, IsNil)
205- c.Assert(nc, DeepEquals, []networkConfig{
206+ c.Assert(nc, DeepEquals, []passthroughConfig{
207 {Name: "eth0", Content: "auto eth0"},
208 })
209 }
210
211 func (cts *ConfigTestSuite) TestNetworkSet(c *C) {
212- nc := []networkConfig{
213+ nc := []passthroughConfig{
214 {Name: "eth0", Content: "auto eth0"},
215 }
216 path := filepath.Join(networkRoot, nc[0].Name)
217@@ -478,7 +481,7 @@
218 c.Assert(err, IsNil)
219
220 // empty content removes
221- nc := []networkConfig{
222+ nc := []passthroughConfig{
223 {Name: "eth0", Content: ""},
224 }
225 err = setNetwork(nc)
226@@ -486,3 +489,94 @@
227 _, err = ioutil.ReadFile(path)
228 c.Assert(helpers.FileExists(path), Equals, false)
229 }
230+
231+func (cts *ConfigTestSuite) TestPppGet(c *C) {
232+ path := filepath.Join(pppRoot, "chap-secrets")
233+ content := "password"
234+ err := ioutil.WriteFile(path, []byte(content), 0644)
235+ c.Assert(err, IsNil)
236+
237+ nc, err := getPPP()
238+ c.Assert(err, IsNil)
239+ c.Assert(nc, DeepEquals, []passthroughConfig{
240+ {Name: "chap-secrets", Content: "password"},
241+ })
242+}
243+
244+func (cts *ConfigTestSuite) TestPppSet(c *C) {
245+ nc := []passthroughConfig{
246+ {Name: "chap-secrets", Content: "another secret"},
247+ }
248+ path := filepath.Join(pppRoot, nc[0].Name)
249+ err := setPPP(nc)
250+ c.Assert(err, IsNil)
251+ content, err := ioutil.ReadFile(path)
252+ c.Assert(err, IsNil)
253+ c.Assert(string(content), Equals, nc[0].Content)
254+}
255+
256+func (cts *ConfigTestSuite) TestNetworkSetViaYaml(c *C) {
257+ modprobePath = filepath.Join(c.MkDir(), "test.conf")
258+
259+ input := `
260+config:
261+ ubuntu-core:
262+ network:
263+ - name: eth0
264+ content: auto dhcp
265+`
266+ _, err := Set(input)
267+ c.Assert(err, IsNil)
268+
269+ // ensure its really there
270+ content, err := ioutil.ReadFile(filepath.Join(networkRoot, "eth0"))
271+ c.Assert(err, IsNil)
272+ c.Assert(string(content), Equals, "auto dhcp")
273+}
274+
275+func (cts *ConfigTestSuite) TestPPPSetViaYaml(c *C) {
276+ modprobePath = filepath.Join(c.MkDir(), "test.conf")
277+
278+ input := `
279+config:
280+ ubuntu-core:
281+ ppp:
282+ - name: chap-secret
283+ content: password
284+`
285+ _, err := Set(input)
286+ c.Assert(err, IsNil)
287+
288+ // ensure its really there
289+ content, err := ioutil.ReadFile(filepath.Join(pppRoot, "chap-secret"))
290+ c.Assert(err, IsNil)
291+ c.Assert(string(content), Equals, "password")
292+}
293+
294+func (cts *ConfigTestSuite) TestPassthroughConfigEqual(c *C) {
295+ a := []passthroughConfig{
296+ {Name: "key", Content: "value"},
297+ }
298+ b := []passthroughConfig{
299+ {Name: "key", Content: "value"},
300+ }
301+ c.Assert(passthroughEqual(a, b), Equals, true)
302+}
303+
304+func (cts *ConfigTestSuite) TestPassthroughConfigNotEqualDifferentSize(c *C) {
305+ a := []passthroughConfig{}
306+ b := []passthroughConfig{
307+ {Name: "key", Content: "value"},
308+ }
309+ c.Assert(passthroughEqual(a, b), Equals, false)
310+}
311+
312+func (cts *ConfigTestSuite) TestPassthroughConfigNotEqualDifferentKeys(c *C) {
313+ a := []passthroughConfig{
314+ {Name: "key", Content: "value"},
315+ }
316+ b := []passthroughConfig{
317+ {Name: "other-key", Content: "value"},
318+ }
319+ c.Assert(passthroughEqual(a, b), Equals, false)
320+}

Subscribers

People subscribed via source and target branches