Merge lp:~chipaca/snappy/setActiveClick into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by John Lenton on 2015-05-29
Status: Merged
Approved by: Sergio Schvezov on 2015-06-09
Approved revision: 486
Merged at revision: 496
Proposed branch: lp:~chipaca/snappy/setActiveClick
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~chipaca/snappy/removeClick
Diff against target: 433 lines (+111/-110)
8 files modified
snappy/click.go (+0/-64)
snappy/click_test.go (+10/-4)
snappy/install_test.go (+3/-1)
snappy/parts_test.go (+4/-1)
snappy/purge.go (+1/-1)
snappy/purge_test.go (+22/-22)
snappy/snapp.go (+70/-16)
snappy/snapp_test.go (+1/-1)
To merge this branch: bzr merge lp:~chipaca/snappy/setActiveClick
Reviewer Review Type Date Requested Status
Sergio Schvezov 2015-05-29 Approve on 2015-06-09
Review via email: mp+260569@code.launchpad.net

This proposal supersedes a proposal from 2015-05-29.

Commit Message

moved setActivateClick to be a method of SnapPart

Description of the Change

moved setActivateClick to be a SnapPart.activate.

And now, to share my pain, you get to watch https://www.youtube.com/watch?v=7_NL0SnpesM

To post a comment you must log in.
Sergio Schvezov (sergiusens) wrote :

nice cleanup, since you put that ugly video in the description you get a needs fixing! :-P

review: Needs Fixing
lp:~chipaca/snappy/setActiveClick updated on 2015-06-08
483. By John Lenton on 2015-05-29

Merged removeClick into setActiveClick.

484. By John Lenton on 2015-06-08

Merged removeClick into setActiveClick.

485. By John Lenton on 2015-06-08

Merged removeClick into setActiveClick.

486. By John Lenton on 2015-06-08

addressed issues raised in review

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snappy/click.go'
2--- snappy/click.go 2015-06-08 18:57:10 +0000
3+++ snappy/click.go 2015-06-08 18:57:10 +0000
4@@ -866,70 +866,6 @@
5 return nil
6 }
7
8-func setActiveClick(baseDir string, inhibitHooks bool, inter interacter) error {
9- currentActiveSymlink := filepath.Join(baseDir, "..", "current")
10- currentActiveDir, _ := filepath.EvalSymlinks(currentActiveSymlink)
11-
12- // already active, nothing to do
13- if baseDir == currentActiveDir {
14- return nil
15- }
16-
17- // there is already an active part
18- if currentActiveDir != "" {
19- unsetActiveClick(currentActiveDir, inhibitHooks, inter)
20- }
21-
22- // make new part active
23- newActiveManifest, err := readClickManifestFromClickDir(baseDir)
24- if err != nil {
25- return err
26- }
27-
28- // yes, its confusing, we have two manifests, this is the important
29- // one, the YAML one
30- m, err := parsePackageYamlFile(filepath.Join(baseDir, "meta", "package.yaml"))
31- if err != nil {
32- return err
33- }
34-
35- origin := originFromBasedir(baseDir)
36-
37- if newActiveManifest.Type == pkg.TypeFramework {
38- if err := policy.Install(m.Name, baseDir); err != nil {
39- return err
40- }
41- }
42-
43- if err := installClickHooks(baseDir, m, origin, inhibitHooks); err != nil {
44- // cleanup the failed hooks
45- removeClickHooks(m, origin, inhibitHooks)
46- return err
47- }
48-
49- // generate the security policy from the package.yaml
50- if err := m.addSecurityPolicy(baseDir); err != nil {
51- return err
52- }
53-
54- // add the "binaries:" from the package.yaml
55- if err := addPackageBinaries(baseDir); err != nil {
56- return err
57- }
58- // add the "services:" from the package.yaml
59- if err := addPackageServices(baseDir, inhibitHooks, inter); err != nil {
60- return err
61- }
62-
63- // FIXME: we want to get rid of the current symlink
64- if err := os.Remove(currentActiveSymlink); err != nil && !os.IsNotExist(err) {
65- logger.Noticef("Failed to remove %q: %v", currentActiveSymlink, err)
66- }
67-
68- // symlink is relative to parent dir
69- return os.Symlink(filepath.Base(baseDir), currentActiveSymlink)
70-}
71-
72 // RunHooks will run all click system hooks
73 func RunHooks() error {
74 systemHooks, err := systemClickHooks()
75
76=== modified file 'snappy/click_test.go'
77--- snappy/click_test.go 2015-06-08 18:57:10 +0000
78+++ snappy/click_test.go 2015-06-08 18:57:10 +0000
79@@ -299,7 +299,9 @@
80 pkgdir := filepath.Dir(filepath.Dir(yamlFile))
81 c.Assert(os.MkdirAll(filepath.Join(pkgdir, ".click", "info"), 0755), IsNil)
82 c.Assert(ioutil.WriteFile(filepath.Join(pkgdir, ".click", "info", "foox."+testOrigin+".manifest"), []byte(`{"name": "foox"}`), 0644), IsNil)
83- c.Assert(setActiveClick(pkgdir, true, ag), IsNil)
84+ part, err := NewInstalledSnapPart(yamlFile, testOrigin)
85+ c.Assert(err, IsNil)
86+ c.Assert(part.activate(true, ag), IsNil)
87
88 pkg := makeTestSnapPackage(c, yaml+"version: 2")
89 _, err = installClick(pkg, 0, ag, testOrigin)
90@@ -317,7 +319,9 @@
91 pkgdir := filepath.Dir(filepath.Dir(yamlFile))
92 c.Assert(os.MkdirAll(filepath.Join(pkgdir, ".click", "info"), 0755), IsNil)
93 c.Assert(ioutil.WriteFile(filepath.Join(pkgdir, ".click", "info", "foox."+testOrigin+".manifest"), []byte(`{"name": "foox"}`), 0644), IsNil)
94- c.Assert(setActiveClick(pkgdir, true, ag), IsNil)
95+ part, err := NewInstalledSnapPart(yamlFile, testOrigin)
96+ c.Assert(err, IsNil)
97+ c.Assert(part.activate(true, ag), IsNil)
98
99 pkg := makeTestSnapPackage(c, yaml+"version: 2\nexplicit-license-agreement: Y")
100 _, err = installClick(pkg, 0, ag, testOrigin)
101@@ -334,7 +338,9 @@
102 pkgdir := filepath.Dir(filepath.Dir(yamlFile))
103 c.Assert(os.MkdirAll(filepath.Join(pkgdir, ".click", "info"), 0755), IsNil)
104 c.Assert(ioutil.WriteFile(filepath.Join(pkgdir, ".click", "info", "foox."+testOrigin+".manifest"), []byte(`{"name": "foox"}`), 0644), IsNil)
105- c.Assert(setActiveClick(pkgdir, true, ag), IsNil)
106+ part, err := NewInstalledSnapPart(yamlFile, testOrigin)
107+ c.Assert(err, IsNil)
108+ c.Assert(part.activate(true, ag), IsNil)
109
110 pkg := makeTestSnapPackage(c, yaml+"license-version: 3\nversion: 2")
111 _, err = installClick(pkg, 0, ag, testOrigin)
112@@ -541,7 +547,7 @@
113 c.Assert(parts[1].IsActive(), Equals, true)
114
115 // set v1 active
116- err = setActiveClick(parts[0].(*SnapPart).basedir, false, nil)
117+ err = parts[0].(*SnapPart).activate(false, nil)
118 parts, err = repo.Installed()
119 c.Assert(err, IsNil)
120 c.Assert(parts[0].Version(), Equals, "1.0")
121
122=== modified file 'snappy/install_test.go'
123--- snappy/install_test.go 2015-06-02 20:46:07 +0000
124+++ snappy/install_test.go 2015-06-08 18:57:10 +0000
125@@ -153,7 +153,9 @@
126 c.Assert(os.MkdirAll(filepath.Join(pkgdir, ".click", "info"), 0755), IsNil)
127 c.Assert(ioutil.WriteFile(filepath.Join(pkgdir, ".click", "info", "hello-app.manifest"), []byte(`{"name": "hello-app"}`), 0644), IsNil)
128 ag := &progress.NullProgress{}
129- c.Assert(setActiveClick(pkgdir, true, ag), IsNil)
130+ part, err := NewInstalledSnapPart(yamlFile, "potato")
131+ c.Assert(err, IsNil)
132+ c.Assert(part.activate(true, ag), IsNil)
133 current := ActiveSnapByName("hello-app")
134 c.Assert(current, NotNil)
135
136
137=== modified file 'snappy/parts_test.go'
138--- snappy/parts_test.go 2015-06-02 20:46:07 +0000
139+++ snappy/parts_test.go 2015-06-08 18:57:10 +0000
140@@ -127,7 +127,10 @@
141 c.Assert(ioutil.WriteFile(filepath.Join(pkgdir, ".click", "info", "hello-app.manifest"), []byte(`{"name": "hello-app"}`), 0644), IsNil)
142 ag := &progress.NullProgress{}
143
144- c.Assert(setActiveClick(pkgdir, true, ag), IsNil)
145+ part, err := NewInstalledSnapPart(yamlFile, testOrigin)
146+ c.Assert(err, IsNil)
147+
148+ c.Assert(part.activate(true, ag), IsNil)
149
150 c.Check(PackageNameActive("hello-app"), Equals, true)
151 c.Assert(unsetActiveClick(pkgdir, true, ag), IsNil)
152
153=== modified file 'snappy/purge.go'
154--- snappy/purge.go 2015-05-20 17:24:29 +0000
155+++ snappy/purge.go 2015-06-08 18:57:10 +0000
156@@ -84,7 +84,7 @@
157 if pkg == nil {
158 continue
159 }
160- if err := setActiveClick(pkg.basedir, false, meter); err != nil {
161+ if err := pkg.activate(false, meter); err != nil {
162 meter.Notify(fmt.Sprintf("Unable to activate %s: %s", pkg.Name(), err))
163 }
164 }
165
166=== modified file 'snappy/purge_test.go'
167--- snappy/purge_test.go 2015-06-08 18:57:10 +0000
168+++ snappy/purge_test.go 2015-06-08 18:57:10 +0000
169@@ -57,7 +57,7 @@
170 c.Check(inter.notified, HasLen, 0)
171 }
172
173-func (s *purgeSuite) mkpkg(c *C, args ...string) (string, string) {
174+func (s *purgeSuite) mkpkg(c *C, args ...string) (dataDir string, part *SnapPart) {
175 version := "1.10"
176 extra := ""
177 switch len(args) {
178@@ -78,19 +78,22 @@
179 c.Assert(os.MkdirAll(filepath.Join(pkgdir, ".click", "info"), 0755), IsNil)
180 c.Assert(ioutil.WriteFile(filepath.Join(pkgdir, ".click", "info", app+".manifest"), []byte(`{"name": "`+app+`"}`), 0644), IsNil)
181
182- ddir := filepath.Join(snapDataDir, app, version)
183- c.Assert(os.MkdirAll(ddir, 0755), IsNil)
184- canaryDataFile := filepath.Join(ddir, "canary.txt")
185+ dataDir = filepath.Join(snapDataDir, app, version)
186+ c.Assert(os.MkdirAll(dataDir, 0755), IsNil)
187+ canaryDataFile := filepath.Join(dataDir, "canary.txt")
188 err = ioutil.WriteFile(canaryDataFile, []byte(""), 0644)
189 c.Assert(err, IsNil)
190
191- return pkgdir, ddir
192+ part, err = NewInstalledSnapPart(yamlFile, testOrigin)
193+ c.Assert(err, IsNil)
194+
195+ return dataDir, part
196 }
197
198 func (s *purgeSuite) TestPurgeActiveRaisesError(c *C) {
199 inter := &MockProgressMeter{}
200- pkgdir, _ := s.mkpkg(c)
201- c.Assert(setActiveClick(pkgdir, true, inter), IsNil)
202+ _, part := s.mkpkg(c)
203+ c.Assert(part.activate(true, inter), IsNil)
204
205 err := Purge("hello-app", 0, inter)
206 c.Check(err, Equals, ErrStillActive)
207@@ -99,7 +102,7 @@
208
209 func (s *purgeSuite) TestPurgeInactiveOK(c *C) {
210 inter := &MockProgressMeter{}
211- _, ddir := s.mkpkg(c)
212+ ddir, _ := s.mkpkg(c)
213
214 err := Purge("hello-app", 0, inter)
215 c.Check(err, IsNil)
216@@ -109,8 +112,8 @@
217
218 func (s *purgeSuite) TestPurgeActiveExplicitOK(c *C) {
219 inter := &MockProgressMeter{}
220- pkgdir, ddir := s.mkpkg(c)
221- c.Assert(setActiveClick(pkgdir, true, inter), IsNil)
222+ ddir, part := s.mkpkg(c)
223+ c.Assert(part.activate(true, inter), IsNil)
224
225 err := Purge("hello-app", DoPurgeActive, inter)
226 c.Check(err, IsNil)
227@@ -120,8 +123,8 @@
228
229 func (s *purgeSuite) TestPurgeActiveRestartServices(c *C) {
230 inter := &MockProgressMeter{}
231- pkgdir, ddir := s.mkpkg(c, "v1", "services:\n - name: svc")
232- c.Assert(setActiveClick(pkgdir, true, inter), IsNil)
233+ ddir, part := s.mkpkg(c, "v1", "services:\n - name: svc")
234+ c.Assert(part.activate(true, inter), IsNil)
235
236 called := [][]string{}
237 systemd.SystemctlCmd = func(cmd ...string) ([]byte, error) {
238@@ -143,8 +146,8 @@
239
240 func (s *purgeSuite) TestPurgeMultiOK(c *C) {
241 inter := &MockProgressMeter{}
242- _, ddir0 := s.mkpkg(c, "v0")
243- _, ddir1 := s.mkpkg(c, "v1")
244+ ddir0, _ := s.mkpkg(c, "v0")
245+ ddir1, _ := s.mkpkg(c, "v1")
246
247 err := Purge("hello-app", 0, inter)
248 c.Check(err, IsNil)
249@@ -155,9 +158,9 @@
250
251 func (s *purgeSuite) TestPurgeMultiContinuesOnFail(c *C) {
252 inter := &MockProgressMeter{}
253- _, ddir0 := s.mkpkg(c, "v0")
254- _, ddir1 := s.mkpkg(c, "v1")
255- _, ddir2 := s.mkpkg(c, "v2")
256+ ddir0, _ := s.mkpkg(c, "v0")
257+ ddir1, _ := s.mkpkg(c, "v1")
258+ ddir2, _ := s.mkpkg(c, "v2")
259
260 count := 0
261 anError := errors.New("fail")
262@@ -182,12 +185,9 @@
263
264 func (s *purgeSuite) TestPurgeRemovedWorks(c *C) {
265 inter := &MockProgressMeter{}
266- pkgdir, ddir := s.mkpkg(c)
267+ ddir, part := s.mkpkg(c)
268
269- yamlPath := filepath.Join(pkgdir, "meta", "package.yaml")
270- part, err := NewInstalledSnapPart(yamlPath, testOrigin)
271- c.Assert(err, IsNil)
272- err = part.remove(inter)
273+ err := part.remove(inter)
274 c.Assert(err, IsNil)
275 c.Check(helpers.FileExists(ddir), Equals, true)
276
277
278=== modified file 'snappy/snapp.go'
279--- snappy/snapp.go 2015-06-08 18:57:10 +0000
280+++ snappy/snapp.go 2015-06-08 18:57:10 +0000
281@@ -675,9 +675,16 @@
282 }
283
284 fullName := QualifiedName(s)
285- currentActiveDir, _ := filepath.EvalSymlinks(filepath.Join(s.basedir, "..", "current"))
286 dataDir := filepath.Join(snapDataDir, fullName, s.Version())
287
288+ var oldPart *SnapPart
289+ if currentActiveDir, _ := filepath.EvalSymlinks(filepath.Join(s.basedir, "..", "current")); currentActiveDir != "" {
290+ oldPart, err = NewInstalledSnapPart(filepath.Join(currentActiveDir, "meta", "package.yaml"), s.origin)
291+ if err != nil {
292+ return "", err
293+ }
294+ }
295+
296 if err := os.MkdirAll(s.basedir, 0755); err != nil {
297 logger.Noticef("Can not create %q: %v", s.basedir, err)
298 return "", err
299@@ -720,17 +727,12 @@
300 // started then copy the data
301 //
302 // otherwise just create a empty data dir
303- if currentActiveDir != "" {
304- oldM, err := parsePackageYamlFile(filepath.Join(currentActiveDir, "meta", "package.yaml"))
305- if err != nil {
306- return "", err
307- }
308-
309+ if oldPart != nil {
310 // we need to stop making it active
311- err = unsetActiveClick(currentActiveDir, inhibitHooks, inter)
312+ err = unsetActiveClick(oldPart.basedir, inhibitHooks, inter)
313 defer func() {
314 if err != nil {
315- if cerr := setActiveClick(currentActiveDir, inhibitHooks, inter); cerr != nil {
316+ if cerr := oldPart.activate(inhibitHooks, inter); cerr != nil {
317 logger.Noticef("Setting old version back to active failed: %v", cerr)
318 }
319 }
320@@ -739,7 +741,7 @@
321 return "", err
322 }
323
324- err = copySnapData(fullName, oldM.Version, s.Version())
325+ err = copySnapData(fullName, oldPart.Version(), s.Version())
326 } else {
327 err = os.MkdirAll(dataDir, 0755)
328 }
329@@ -757,10 +759,10 @@
330 }
331
332 // and finally make active
333- err = setActiveClick(s.basedir, inhibitHooks, inter)
334+ err = s.activate(inhibitHooks, inter)
335 defer func() {
336- if err != nil && currentActiveDir != "" {
337- if cerr := setActiveClick(currentActiveDir, inhibitHooks, inter); cerr != nil {
338+ if err != nil && oldPart != nil {
339+ if cerr := oldPart.activate(inhibitHooks, inter); cerr != nil {
340 logger.Noticef("When setting old %s version back to active: %v", s.Name(), cerr)
341 }
342 }
343@@ -803,7 +805,7 @@
344 }
345 }
346
347- if err := s.RefreshDependentsSecurity(currentActiveDir, inter); err != nil {
348+ if err := s.RefreshDependentsSecurity(oldPart, inter); err != nil {
349 return "", err
350 }
351
352@@ -831,7 +833,55 @@
353
354 // SetActive sets the snap active
355 func (s *SnapPart) SetActive(pb progress.Meter) (err error) {
356- return setActiveClick(s.basedir, false, pb)
357+ return s.activate(false, pb)
358+}
359+
360+func (s *SnapPart) activate(inhibitHooks bool, inter interacter) error {
361+ currentActiveSymlink := filepath.Join(s.basedir, "..", "current")
362+ currentActiveDir, _ := filepath.EvalSymlinks(currentActiveSymlink)
363+
364+ // already active, nothing to do
365+ if s.basedir == currentActiveDir {
366+ return nil
367+ }
368+
369+ // there is already an active part
370+ if currentActiveDir != "" {
371+ unsetActiveClick(currentActiveDir, inhibitHooks, inter)
372+ }
373+
374+ if s.Type() == pkg.TypeFramework {
375+ if err := policy.Install(s.Name(), s.basedir); err != nil {
376+ return err
377+ }
378+ }
379+
380+ if err := installClickHooks(s.basedir, s.m, s.origin, inhibitHooks); err != nil {
381+ // cleanup the failed hooks
382+ removeClickHooks(s.m, s.origin, inhibitHooks)
383+ return err
384+ }
385+
386+ // generate the security policy from the package.yaml
387+ if err := s.m.addSecurityPolicy(s.basedir); err != nil {
388+ return err
389+ }
390+
391+ // add the "binaries:" from the package.yaml
392+ if err := addPackageBinaries(s.basedir); err != nil {
393+ return err
394+ }
395+ // add the "services:" from the package.yaml
396+ if err := addPackageServices(s.basedir, inhibitHooks, inter); err != nil {
397+ return err
398+ }
399+
400+ if err := os.Remove(currentActiveSymlink); err != nil && !os.IsNotExist(err) {
401+ logger.Noticef("Failed to remove %q: %v", currentActiveSymlink, err)
402+ }
403+
404+ // symlink is relative to parent dir
405+ return os.Symlink(filepath.Base(s.basedir), currentActiveSymlink)
406 }
407
408 // Uninstall remove the snap from the system
409@@ -1035,7 +1085,11 @@
410 }
411
412 // RefreshDependentsSecurity refreshes the security policies of dependent snaps
413-func (s *SnapPart) RefreshDependentsSecurity(oldBaseDir string, inter interacter) (err error) {
414+func (s *SnapPart) RefreshDependentsSecurity(oldPart *SnapPart, inter interacter) (err error) {
415+ oldBaseDir := ""
416+ if oldPart != nil {
417+ oldBaseDir = oldPart.basedir
418+ }
419 upPol, upTpl := policy.AppArmorDelta(oldBaseDir, s.basedir, s.Name()+"_")
420
421 deps, err := s.Dependents()
422
423=== modified file 'snappy/snapp_test.go'
424--- snappy/snapp_test.go 2015-06-04 13:34:57 +0000
425+++ snappy/snapp_test.go 2015-06-08 18:57:10 +0000
426@@ -1158,7 +1158,7 @@
427 pb := &MockProgressMeter{}
428 m, err := parsePackageYamlData([]byte(yaml))
429 part := &SnapPart{m: m, origin: testOrigin, basedir: d1}
430- c.Assert(part.RefreshDependentsSecurity(d2, pb), IsNil)
431+ c.Assert(part.RefreshDependentsSecurity(&SnapPart{basedir: d2}, pb), IsNil)
432 c.Check(touched, DeepEquals, []string{fn})
433 }
434

Subscribers

People subscribed via source and target branches