Merge lp:~thumper/juju-core/auto-restart-containers into lp:~go-bot/juju-core/trunk

Proposed by Tim Penhey
Status: Merged
Approved by: Tim Penhey
Approved revision: no longer in the source branch.
Merged at revision: 1487
Proposed branch: lp:~thumper/juju-core/auto-restart-containers
Merge into: lp:~go-bot/juju-core/trunk
Prerequisite: lp:~thumper/juju-core/test-import-fix
Diff against target: 134 lines (+43/-0)
3 files modified
container/lxc/lxc.go (+17/-0)
container/lxc/lxc_test.go (+14/-0)
container/lxc/test.go (+12/-0)
To merge this branch: bzr merge lp:~thumper/juju-core/auto-restart-containers
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+175435@code.launchpad.net

Commit message

Add the autostart symlink for containers.

In order for containers to autostart, there needs to be
a symlink in /etc/lxc/auto called CN.conf where CN is
the container name, which points to the config file for
the container, which is by default /var/lib/lxc/CN/config.

https://codereview.appspot.com/11491043/

Description of the change

Add the autostart symlink for containers.

In order for containers to autostart, there needs to be
a symlink in /etc/lxc/auto called CN.conf where CN is
the container name, which points to the config file for
the container, which is by default /var/lib/lxc/CN/config.

https://codereview.appspot.com/11491043/

To post a comment you must log in.
Revision history for this message
Tim Penhey (thumper) wrote :
Download full text (5.4 KiB)

Reviewers: mp+175435_code.launchpad.net,

Message:
Please take a look.

Description:
Add the autostart symlink for containers.

In order for containers to autostart, there needs to be
a symlink in /etc/lxc/auto called CN.conf where CN is
the container name, which points to the config file for
the container, which is by default /var/lib/lxc/CN/config.

https://code.launchpad.net/~thumper/juju-core/auto-restart-containers/+merge/175435

Requires:
https://code.launchpad.net/~thumper/juju-core/test-import-fix/+merge/175430

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/11491043/

Affected files:
   A [revision details]
   M container/lxc/lxc.go
   M container/lxc/lxc_test.go
   M container/lxc/test.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: <email address hidden>

Index: container/lxc/lxc.go
=== modified file 'container/lxc/lxc.go'
--- container/lxc/lxc.go 2013-07-15 01:16:52 +0000
+++ container/lxc/lxc.go 2013-07-17 23:38:15 +0000
@@ -28,6 +28,7 @@
   containerDir = "/var/lib/juju/containers"
   removedContainerDir = "/var/lib/juju/removed-containers"
   lxcContainerDir = "/var/lib/lxc"
+ lxcRestartDir = "/etc/lxc/auto"
   lxcObjectFactory = golxc.Factory()
  )

@@ -125,6 +126,13 @@
    return nil, err
   }
   logger.Tracef("lxc container created")
+ // Now symlink the config file into the restart directory.
+ containerConfigFile := filepath.Join(lxcContainerDir, name, "config")
+ linkLocation := filepath.Join(lxcRestartDir, name+".conf")
+ if err := os.Symlink(containerConfigFile, linkLocation); err != nil {
+ return nil, err
+ }
+ logger.Tracef("auto-restart link created")

   // Start the lxc container with the appropriate settings for grabbing the
   // console output and a log file.
@@ -154,6 +162,13 @@
    logger.Errorf("failed to destroy lxc container: %v", err)
    return err
   }
+ // Remove the autostart symlink
+ linkLocation := filepath.Join(lxcRestartDir, name+".conf")
+ if err := os.Remove(linkLocation); err != nil {
+ return err
+ }
+ logger.Tracef("auto-restart link removed")
+
   // Move the directory.
   logger.Tracef("create old container dir: %s", removedContainerDir)
   if err := os.MkdirAll(removedContainerDir, 0755); err != nil {

Index: container/lxc/lxc_test.go
=== modified file 'container/lxc/lxc_test.go'
--- container/lxc/lxc_test.go 2013-07-17 22:41:36 +0000
+++ container/lxc/lxc_test.go 2013-07-17 23:38:15 +0000
@@ -12,6 +12,7 @@

   gc "launchpad.net/gocheck"
   "launchpad.net/goyaml"
+ "launchpad.net/loggo"

   "launchpad.net/juju-core/container/lxc"
   "launchpad.net/juju-core/instance"
@@ -46,6 +47,7 @@
  func (s *LxcSuite) SetUpTest(c *gc.C) {
   s.LoggingSuite.SetUpTest(c)
   s.TestSuite.SetUpTest(c)
+ loggo.GetLogger("juju.container.lxc").SetLogLevel(loggo.TRACE)
  }

  func (s *LxcSuite) TearDownTest(c *gc.C) {
@@ -96,6 +98,16 @@

   // Check the mount point has been created i...

Read more...

Revision history for this message
Ian Booth (wallyworld) wrote :

LGTM

https://codereview.appspot.com/11491043/diff/1/container/lxc/lxc.go
File container/lxc/lxc.go (right):

https://codereview.appspot.com/11491043/diff/1/container/lxc/lxc.go#newcode131
container/lxc/lxc.go:131: linkLocation := filepath.Join(lxcRestartDir,
name+".conf")
perhaps make name+".conf" a function as it's used twice

https://codereview.appspot.com/11491043/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'container/lxc/lxc.go'
2--- container/lxc/lxc.go 2013-07-15 01:16:52 +0000
3+++ container/lxc/lxc.go 2013-07-18 02:57:28 +0000
4@@ -28,6 +28,7 @@
5 containerDir = "/var/lib/juju/containers"
6 removedContainerDir = "/var/lib/juju/removed-containers"
7 lxcContainerDir = "/var/lib/lxc"
8+ lxcRestartDir = "/etc/lxc/auto"
9 lxcObjectFactory = golxc.Factory()
10 )
11
12@@ -125,6 +126,12 @@
13 return nil, err
14 }
15 logger.Tracef("lxc container created")
16+ // Now symlink the config file into the restart directory.
17+ containerConfigFile := filepath.Join(lxcContainerDir, name, "config")
18+ if err := os.Symlink(containerConfigFile, restartSymlink(name)); err != nil {
19+ return nil, err
20+ }
21+ logger.Tracef("auto-restart link created")
22
23 // Start the lxc container with the appropriate settings for grabbing the
24 // console output and a log file.
25@@ -154,6 +161,12 @@
26 logger.Errorf("failed to destroy lxc container: %v", err)
27 return err
28 }
29+ // Remove the autostart symlink
30+ if err := os.Remove(restartSymlink(name)); err != nil {
31+ return err
32+ }
33+ logger.Tracef("auto-restart link removed")
34+
35 // Move the directory.
36 logger.Tracef("create old container dir: %s", removedContainerDir)
37 if err := os.MkdirAll(removedContainerDir, 0755); err != nil {
38@@ -206,6 +219,10 @@
39 return fmt.Sprintf(internalLogDirTemplate, lxcContainerDir, containerName)
40 }
41
42+func restartSymlink(name string) string {
43+ return filepath.Join(lxcRestartDir, name+".conf")
44+}
45+
46 const localConfig = `
47 lxc.network.type = veth
48 lxc.network.link = lxcbr0
49
50=== modified file 'container/lxc/lxc_test.go'
51--- container/lxc/lxc_test.go 2013-07-17 22:41:36 +0000
52+++ container/lxc/lxc_test.go 2013-07-18 02:57:28 +0000
53@@ -12,6 +12,7 @@
54
55 gc "launchpad.net/gocheck"
56 "launchpad.net/goyaml"
57+ "launchpad.net/loggo"
58
59 "launchpad.net/juju-core/container/lxc"
60 "launchpad.net/juju-core/instance"
61@@ -46,6 +47,7 @@
62 func (s *LxcSuite) SetUpTest(c *gc.C) {
63 s.LoggingSuite.SetUpTest(c)
64 s.TestSuite.SetUpTest(c)
65+ loggo.GetLogger("juju.container.lxc").SetLogLevel(loggo.TRACE)
66 }
67
68 func (s *LxcSuite) TearDownTest(c *gc.C) {
69@@ -96,6 +98,16 @@
70
71 // Check the mount point has been created inside the container.
72 c.Assert(filepath.Join(s.LxcDir, name, "rootfs/var/log/juju"), jc.IsDirectory)
73+ // Check that the config file is linked in the restart dir.
74+ expectedLinkLocation := filepath.Join(s.RestartDir, name+".conf")
75+ expectedTarget := filepath.Join(s.LxcDir, name, "config")
76+ linkInfo, err := os.Lstat(expectedLinkLocation)
77+ c.Assert(err, gc.IsNil)
78+ c.Assert(linkInfo.Mode()&os.ModeSymlink, gc.Equals, os.ModeSymlink)
79+
80+ location, err := os.Readlink(expectedLinkLocation)
81+ c.Assert(err, gc.IsNil)
82+ c.Assert(location, gc.Equals, expectedTarget)
83 }
84
85 func (s *LxcSuite) TestStopContainer(c *gc.C) {
86@@ -110,6 +122,8 @@
87 c.Assert(filepath.Join(s.ContainerDir, name), jc.DoesNotExist)
88 // but instead, in the removed container dir
89 c.Assert(filepath.Join(s.RemovedDir, name), jc.IsDirectory)
90+ // Check that the restart link has been removed.
91+ c.Assert(filepath.Join(s.RestartDir, name+".conf"), jc.DoesNotExist)
92 }
93
94 func (s *LxcSuite) TestStopContainerNameClash(c *gc.C) {
95
96=== modified file 'container/lxc/test.go'
97--- container/lxc/test.go 2013-07-17 22:42:53 +0000
98+++ container/lxc/test.go 2013-07-18 02:57:28 +0000
99@@ -28,6 +28,14 @@
100 return
101 }
102
103+// SetLxcRestartDir allows tests in other packages to override the
104+// lxcRestartDir, which contains the symlinks to the config files so
105+// containers can be auto-restarted on reboot.
106+func SetLxcRestartDir(dir string) (old string) {
107+ old, lxcRestartDir = lxcRestartDir, dir
108+ return
109+}
110+
111 // SetRemovedContainerDir allows tests in other packages to override the
112 // removedContainerDir.
113 func SetRemovedContainerDir(dir string) (old string) {
114@@ -50,9 +58,11 @@
115 ContainerDir string
116 RemovedDir string
117 LxcDir string
118+ RestartDir string
119 oldContainerDir string
120 oldRemovedDir string
121 oldLxcContainerDir string
122+ oldRestartDir string
123 }
124
125 func (s *TestSuite) SetUpSuite(c *gc.C) {}
126@@ -66,6 +76,8 @@
127 s.oldRemovedDir = SetRemovedContainerDir(s.RemovedDir)
128 s.LxcDir = c.MkDir()
129 s.oldLxcContainerDir = SetLxcContainerDir(s.LxcDir)
130+ s.RestartDir = c.MkDir()
131+ s.oldRestartDir = SetLxcRestartDir(s.RestartDir)
132 s.Factory = mock.MockFactory()
133 s.oldFactory = SetLxcFactory(s.Factory)
134 }

Subscribers

People subscribed via source and target branches

to status/vote changes: