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
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): lxc/lxc. go lxc/lxc_ test.go lxc/restart. go
A [revision details]
M container/
M container/
A container/
Index: [revision details] 20140124005805- 5wkfrpv3q7zaknx v
=== 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-
+New revision: <email address hidden>
Index: container/ lxc/lxc. go lxc/lxc. go' lxc/lxc. go 2013-12-04 02:37:05 +0000 lxc/lxc. go 2014-01-24 01:19:25 +0000 Tracef( "lxc container created") Join(LxcContain erDir, name, "config") containerConfig File, restartSymlink( name)); err != Join(LxcContain erDir, name, "config") containerConfig File, restartSymlink( name)); err != Tracef( "auto-restart link created") Tracef( "auto-restart link created")
=== modified file 'container/
--- container/
+++ container/
@@ -109,12 +109,14 @@
return nil, nil, err
}
logger.
- // Now symlink the config file into the restart directory.
- containerConfigFile := filepath.
- if err := os.Symlink(
nil {
- return nil, nil, err
+ // Now symlink the config file into the restart directory, if it exists
+ if restartDirExists() {
+ containerConfigFile := filepath.
+ if err := os.Symlink(
nil {
+ return nil, nil, err
+ }
+ logger.
}
- logger.
// Start the lxc container with the appropriate settings for grabbing the
// console output and a log file.
@@ -198,7 +200,12 @@
`
func networkConfigTe mplate( networkType, networkLink string) string { networkTemplate , networkType, networkLink) networkTemplate , networkType, networkLink) Tracef( "Setting auto start to true in lxc config.")
- return fmt.Sprintf(
+ networkConfig := fmt.Sprintf(
+ if !restartDirExists() {
+ networkConfig += "lxc.start.auto = 1\n"
+ logger.
+ }
+ return networkConfig
}
func generateNetwork Config( network *container. NetworkConfig) string {
Index: container/ lxc/lxc_ test.go lxc/lxc_ test.go' lxc/lxc_ test.go 2013-12-16 07:20:01 +0000 lxc/lxc_ test.go 2014-01-24 01:19:25 +0000 autostartLink, jc.IsSymlink)
=== modified file 'container/
--- container/
+++ container/
@@ -186,6 +186,25 @@
c.Assert(
}
+func (s *LxcSuite) TestStartContai nerNoRestartDir (c *gc.C) { s.RestartDir) rManager( container. ManagerConfig{ }) g.StartContaine r(c, manager, "1/lxc/0") ink(string( instance. Id())) igTemplate( "foo", "bar") autostartLink, jc.DoesNotExist) erRemovesAutost artLink( c *gc.C) { rManager( container. ManagerConfig{ }) g.StartContaine r(c, manager, "1/lxc/0")
+ err := os.Remove(
+ c.Assert(err, gc.IsNil)
+
+ manager := lxc.NewContaine
+ instance := containertestin
+ autostartLink := lxc.RestartSyml
+
+ config := lxc.NetworkConf
+ 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(
+}
+
func (s *LxcSuite) TestStopContain
manager := lxc.NewContaine
instance := containertestin
Index: container/ lxc/restart. go lxc/restart. go' lxc/restart. go 1970-01-01 00:00:00 +0000 lxc/restart. go 2014-01-24 01:19:25 +0000 LxcRestartDir) ; err != nil {
=== added file 'container/
--- container/
+++ container/
@@ -0,0 +1,14 @@
+package lxc
+
+import (
+ "os"
+)
+
+func restartDirExists() bool {
+ if _, err := os.Stat(
+ if os.IsNotExist(err) {
+ return false
+ }
+ }
+ return true
+}