Merge lp:~wallyworld/golxc/lxc-console-log into lp:golxc

Proposed by Ian Booth on 2014-07-30
Status: Merged
Approved by: Ian Booth on 2014-07-30
Approved revision: 15
Merged at revision: 12
Proposed branch: lp:~wallyworld/golxc/lxc-console-log
Merge into: lp:golxc
Diff against target: 255 lines (+104/-64)
3 files modified
export_test.go (+0/-2)
golxc.go (+23/-47)
golxc_test.go (+81/-15)
To merge this branch: bzr merge lp:~wallyworld/golxc/lxc-console-log
Reviewer Review Type Date Requested Status
Tim Penhey 2014-07-30 Approve on 2014-07-30
Review via email: mp+228778@code.launchpad.net

Commit message

Rework the method used to detect if lxc-start supports --console-log or --console.

Description of the change

Rework the method used to detect if lxc-start supports --console-log or --console.
Try running lxc-start with --console-log and if that fails, use --console.

As a drive by change lxc args to long form.

To post a comment you must log in.
Tim Penhey (thumper) wrote :

We should probably check that the long options that we want to use are in the 0.7.5 version (precise). I think they are... actually, just checked.

lxc-create doesn't support any long options, which is why we don't do it.
lxc-start does seem to support long options.

You are going to need to fix the flags for lxc-create to be short options again.

review: Needs Fixing
lp:~wallyworld/golxc/lxc-console-log updated on 2014-07-30
13. By Ian Booth on 2014-07-30

Long args not supported for create and destroy for older versions

14. By Ian Booth on 2014-07-30

Remove Infof

15. By Ian Booth on 2014-07-30

Long args not supported for clone for older versions

Tim Penhey (thumper) wrote :

Thanks.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'export_test.go'
2--- export_test.go 2014-07-28 21:56:33 +0000
3+++ export_test.go 2014-07-30 01:57:33 +0000
4@@ -15,5 +15,3 @@
5 confPath = cp
6 return orig
7 }
8-
9-var GenerateConsoleFileArguments func(string) (string, error) = generateConsoleFileArgument
10
11=== modified file 'golxc.go'
12--- golxc.go 2014-07-28 22:23:49 +0000
13+++ golxc.go 2014-07-30 01:57:33 +0000
14@@ -12,7 +12,6 @@
15 "os"
16 "os/exec"
17 "path"
18- "regexp"
19 "strconv"
20 "strings"
21
22@@ -227,27 +226,24 @@
23 }
24 args := []string{
25 "--daemon",
26- "-n", c.name,
27+ "--name", c.name,
28 }
29 if configFile != "" {
30- args = append(args, "-f", configFile)
31- }
32+ args = append(args, "--rcfile", configFile)
33+ }
34+ if c.logFile != "" {
35+ args = append(args, "--logfile", c.logFile, "--logpriority", string(c.logLevel))
36+ }
37+ startArgs := args
38 if consoleFile != "" {
39- version, err := lxcStartVersion()
40- if err != nil {
41- return fmt.Errorf("cannot determine the lxc-start version: %v", err)
42- }
43-
44- consoleFlag, err := generateConsoleFileArgument(version)
45- if err != nil {
46- return fmt.Errorf("cannot determine the flag for specifying a console-output log file: %v", err)
47- }
48- args = append(args, consoleFlag, consoleFile)
49- }
50- if c.logFile != "" {
51- args = append(args, "-o", c.logFile, "-l", string(c.logLevel))
52- }
53- _, err := run("lxc-start", args...)
54+ startArgs = append(args, "--console-log", consoleFile)
55+ }
56+ _, err := run("lxc-start", startArgs...)
57+ // We may need to fallback to the older --console arg name.
58+ if err != nil && consoleFile != "" {
59+ startArgs = append(args, "--console", consoleFile)
60+ _, err = run("lxc-start", startArgs...)
61+ }
62 if err != nil {
63 return err
64 }
65@@ -270,10 +266,10 @@
66 return nil
67 }
68 args := []string{
69- "-n", c.name,
70+ "--name", c.name,
71 }
72 if c.logFile != "" {
73- args = append(args, "-o", c.logFile, "-l", string(c.logLevel))
74+ args = append(args, "--logfile", c.logFile, "--logpriority", string(c.logLevel))
75 }
76 _, err := run("lxc-stop", args...)
77 if err != nil {
78@@ -326,10 +322,10 @@
79 return fmt.Errorf("container %q is not running", c.name)
80 }
81 args := []string{
82- "-n", c.name,
83+ "--name", c.name,
84 }
85 if c.logFile != "" {
86- args = append(args, "-o", c.logFile, "-l", string(c.logLevel))
87+ args = append(args, "--logfile", c.logFile, "--logpriority", string(c.logLevel))
88 }
89 _, err := run("lxc-freeze", args...)
90 if err != nil {
91@@ -347,10 +343,10 @@
92 return fmt.Errorf("container %q is not frozen", c.name)
93 }
94 args := []string{
95- "-n", c.name,
96+ "--name", c.name,
97 }
98 if c.logFile != "" {
99- args = append(args, "-o", c.logFile, "-l", string(c.logLevel))
100+ args = append(args, "--logfile", c.logFile, "--logpriority", string(c.logLevel))
101 }
102 _, err := run("lxc-unfreeze", args...)
103 if err != nil {
104@@ -384,7 +380,7 @@
105 stateStrs[i] = string(state)
106 }
107 waitStates := strings.Join(stateStrs, "|")
108- _, err := run("lxc-wait", "-n", c.name, "-s", waitStates)
109+ _, err := run("lxc-wait", "--name", c.name, "--state", waitStates)
110 if err != nil {
111 return err
112 }
113@@ -393,7 +389,7 @@
114
115 // Info returns the status and the process id of the container.
116 func (c *container) Info() (State, int, error) {
117- out, err := run("lxc-info", "-n", c.name)
118+ out, err := run("lxc-info", "--name", c.name)
119 if err != nil {
120 return StateUnknown, -1, err
121 }
122@@ -444,26 +440,6 @@
123 return path.Join(c.containerHome(), "rootfs")
124 }
125
126-func generateConsoleFileArgument(lxcVersion string) (flag string, err error) {
127- // Version <= 0.7 specified a console file via the -c flag. Newer
128- // versions utilize the -L flag.
129- if matched, err := regexp.MatchString(`^0\.[0-8]`, lxcVersion); err == nil {
130- if matched {
131- flag = "-c"
132- } else {
133- flag = "-L"
134- }
135- }
136-
137- return flag, err
138-}
139-
140-// Returns the lxc-start version
141-func lxcStartVersion() (string, error) {
142- version, err := exec.Command("lxc-start", "--version").Output()
143- return string(version), err
144-}
145-
146 // run executes the passed command and returns the out.
147 func run(name string, args ...string) (string, error) {
148 logger := loggo.GetLogger(fmt.Sprintf("golxc.run.%s", name))
149
150=== modified file 'golxc_test.go'
151--- golxc_test.go 2014-07-28 22:24:12 +0000
152+++ golxc_test.go 2014-07-30 01:57:33 +0000
153@@ -368,21 +368,87 @@
154 s.IsolationSuite.SetUpSuite(c)
155 }
156
157-// Test that our function which parses versions and returns the proper
158-// console flag is working as intended.
159-func (s *commandArgs) TestGenerateCorrectConsoleFileFlag(c *C) {
160-
161- assertGood := func(desiredFlag string) func(string, error) {
162- return func(flag string, err error) {
163- c.Assert(err, IsNil)
164- c.Assert(flag, Equals, desiredFlag)
165- }
166- }
167-
168- assertGood("-c")(golxc.GenerateConsoleFileArguments("0.8"))
169- assertGood("-L")(golxc.GenerateConsoleFileArguments("0.9"))
170- assertGood("-L")(golxc.GenerateConsoleFileArguments("1.8"))
171- assertGood("-L")(golxc.GenerateConsoleFileArguments("1.0.4"))
172+func (s *commandArgs) setupLxcStart(c *C) {
173+ dir := c.MkDir()
174+ // Make the rootfs for the "test" container so it thinks it is created.
175+ rootfs := filepath.Join(dir, "test", "rootfs")
176+ err := os.MkdirAll(rootfs, 0755)
177+ c.Assert(err, IsNil)
178+ c.Assert(rootfs, jc.IsDirectory)
179+
180+ s.PatchValue(&golxc.ContainerDir, dir)
181+ s.PatchEnvironment("PATH", s.originalPath)
182+ testing.PatchExecutableAsEchoArgs(c, s, "lxc-wait")
183+ testing.PatchExecutableAsEchoArgs(c, s, "lxc-start")
184+ info := "PID: 1234\nSTATE: RUNNING"
185+ testing.PatchExecutable(c, s, "lxc-info", "#!/bin/sh\necho '"+info+"'\n")
186+}
187+
188+func (s *commandArgs) TestStartArgs(c *C) {
189+ s.setupLxcStart(c)
190+ factory := golxc.Factory()
191+ container := factory.New("test")
192+ err := container.Start("config-file", "console-log")
193+ c.Assert(err, IsNil)
194+ testing.AssertEchoArgs(
195+ c, "lxc-start",
196+ "--daemon",
197+ "--name", "test",
198+ "--rcfile", "config-file",
199+ "--console-log", "console-log")
200+}
201+
202+func (s *commandArgs) TestStartArgsNoConsoleLog(c *C) {
203+ s.setupLxcStart(c)
204+ factory := golxc.Factory()
205+ container := factory.New("test")
206+ err := container.Start("config-file", "")
207+ c.Assert(err, IsNil)
208+ testing.AssertEchoArgs(
209+ c, "lxc-start",
210+ "--daemon",
211+ "--name", "test",
212+ "--rcfile", "config-file")
213+}
214+
215+func (s *commandArgs) TestStartArgsFallback(c *C) {
216+ dir := c.MkDir()
217+ // Make the rootfs for the "test" container so it thinks it is created.
218+ rootfs := filepath.Join(dir, "test", "rootfs")
219+ err := os.MkdirAll(rootfs, 0755)
220+ c.Assert(err, IsNil)
221+ c.Assert(rootfs, jc.IsDirectory)
222+
223+ s.PatchValue(&golxc.ContainerDir, dir)
224+ s.PatchEnvironment("PATH", s.originalPath)
225+ testing.PatchExecutableAsEchoArgs(c, s, "lxc-wait")
226+ EchoQuotedArgs := `
227+name=` + "`basename $0`" + `
228+argfile="$name.out"
229+rm -f $argfile
230+printf "%s" $name | tee -a $argfile
231+for arg in "$@"; do
232+ printf " \"%s\"" "$arg" | tee -a $argfile
233+done
234+printf "\n" | tee -a $argfile
235+`
236+ // Make lxc-start error if called with --console-log but succeed otherwise.
237+ // On success, echo the args.
238+ testing.PatchExecutable(
239+ c, s, "lxc-start", "#!/bin/bash\nif [ $6 == '--console-log' ]; then\nexit 1\nelse\n"+EchoQuotedArgs+"fi")
240+ info := "PID: 1234\nSTATE: RUNNING"
241+ testing.PatchExecutable(c, s, "lxc-info", "#!/bin/sh\necho '"+info+"'\n")
242+
243+ factory := golxc.Factory()
244+ container := factory.New("test")
245+ err = container.Start("config-file", "console-log")
246+ c.Assert(err, IsNil)
247+ testing.AssertEchoArgs(
248+ c, "lxc-start",
249+ "--daemon",
250+ "--name", "test",
251+ "--rcfile", "config-file",
252+ "--console", "console-log")
253 }
254
255 func (s *commandArgs) TestCreateArgs(c *C) {

Subscribers

People subscribed via source and target branches

to all changes: