Code review comment for lp://staging/~waigani/juju-core/lxc-trusty-autostart

Revision history for this message
Jesse Meek (waigani) wrote :

Reviewers: mp+202974_code.launchpad.net,

Message:
Please take a look.

Description:
Auto start for lxc containers

Auto start for lxc containers should now work on Trusty. Symlink is
created if /etc/lxc/auto exists, otherwise auto start is set to true in
lxc config.

https://code.launchpad.net/~waigani/juju-core/lxc-trusty-autostart/+merge/202974

(do not edit description out of merge proposal)

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

Affected files (+48, -6 lines):
   A [revision details]
   M container/lxc/lxc.go
   M container/lxc/lxc_test.go
   A container/lxc/restart.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: tarmac-20140124005805-5wkfrpv3q7zaknxv
+New revision: <email address hidden>

Index: container/lxc/lxc.go
=== modified file 'container/lxc/lxc.go'
--- container/lxc/lxc.go 2013-12-04 02:37:05 +0000
+++ container/lxc/lxc.go 2014-01-24 01:19:25 +0000
@@ -109,12 +109,14 @@
    return nil, nil, err
   }
   logger.Tracef("lxc container created")
- // Now symlink the config file into the restart directory.
- containerConfigFile := filepath.Join(LxcContainerDir, name, "config")
- if err := os.Symlink(containerConfigFile, restartSymlink(name)); err !=
nil {
- return nil, nil, err
+ // Now symlink the config file into the restart directory, if it exists
+ if restartDirExists() {
+ containerConfigFile := filepath.Join(LxcContainerDir, name, "config")
+ if err := os.Symlink(containerConfigFile, restartSymlink(name)); err !=
nil {
+ return nil, nil, err
+ }
+ logger.Tracef("auto-restart link created")
   }
- logger.Tracef("auto-restart link created")

   // Start the lxc container with the appropriate settings for grabbing the
   // console output and a log file.
@@ -198,7 +200,12 @@
  `

  func networkConfigTemplate(networkType, networkLink string) string {
- return fmt.Sprintf(networkTemplate, networkType, networkLink)
+ networkConfig := fmt.Sprintf(networkTemplate, networkType, networkLink)
+ if !restartDirExists() {
+ networkConfig += "lxc.start.auto = 1\n"
+ logger.Tracef("Setting auto start to true in lxc config.")
+ }
+ return networkConfig
  }

  func generateNetworkConfig(network *container.NetworkConfig) string {

Index: container/lxc/lxc_test.go
=== modified file 'container/lxc/lxc_test.go'
--- container/lxc/lxc_test.go 2013-12-16 07:20:01 +0000
+++ container/lxc/lxc_test.go 2014-01-24 01:19:25 +0000
@@ -186,6 +186,25 @@
   c.Assert(autostartLink, jc.IsSymlink)
  }

+func (s *LxcSuite) TestStartContainerNoRestartDir(c *gc.C) {
+ err := os.Remove(s.RestartDir)
+ c.Assert(err, gc.IsNil)
+
+ manager := lxc.NewContainerManager(container.ManagerConfig{})
+ instance := containertesting.StartContainer(c, manager, "1/lxc/0")
+ autostartLink := lxc.RestartSymlink(string(instance.Id()))
+
+ config := lxc.NetworkConfigTemplate("foo", "bar")
+ expected := `
+lxc.network.type = foo
+lxc.network.link = bar
+lxc.network.flags = up
+lxc.start.auto = 1
+`
+ c.Assert(config, gc.Equals, expected)
+ c.Assert(autostartLink, jc.DoesNotExist)
+}
+
  func (s *LxcSuite) TestStopContainerRemovesAutostartLink(c *gc.C) {
   manager := lxc.NewContainerManager(container.ManagerConfig{})
   instance := containertesting.StartContainer(c, manager, "1/lxc/0")

Index: container/lxc/restart.go
=== added file 'container/lxc/restart.go'
--- container/lxc/restart.go 1970-01-01 00:00:00 +0000
+++ container/lxc/restart.go 2014-01-24 01:19:25 +0000
@@ -0,0 +1,14 @@
+package lxc
+
+import (
+ "os"
+)
+
+func restartDirExists() bool {
+ if _, err := os.Stat(LxcRestartDir); err != nil {
+ if os.IsNotExist(err) {
+ return false
+ }
+ }
+ return true
+}

« Back to merge proposal