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

Proposed by Ian Booth
Status: Merged
Approved by: Ian Booth
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 (community) Approve
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.
Revision history for this message
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
13. By Ian Booth

Long args not supported for create and destroy for older versions

14. By Ian Booth

Remove Infof

15. By Ian Booth

Long args not supported for clone for older versions

Revision history for this message
Tim Penhey (thumper) wrote :

Thanks.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'export_test.go'
--- export_test.go 2014-07-28 21:56:33 +0000
+++ export_test.go 2014-07-30 01:57:33 +0000
@@ -15,5 +15,3 @@
15 confPath = cp15 confPath = cp
16 return orig16 return orig
17}17}
18
19var GenerateConsoleFileArguments func(string) (string, error) = generateConsoleFileArgument
2018
=== modified file 'golxc.go'
--- golxc.go 2014-07-28 22:23:49 +0000
+++ golxc.go 2014-07-30 01:57:33 +0000
@@ -12,7 +12,6 @@
12 "os"12 "os"
13 "os/exec"13 "os/exec"
14 "path"14 "path"
15 "regexp"
16 "strconv"15 "strconv"
17 "strings"16 "strings"
1817
@@ -227,27 +226,24 @@
227 }226 }
228 args := []string{227 args := []string{
229 "--daemon",228 "--daemon",
230 "-n", c.name,229 "--name", c.name,
231 }230 }
232 if configFile != "" {231 if configFile != "" {
233 args = append(args, "-f", configFile)232 args = append(args, "--rcfile", configFile)
234 }233 }
234 if c.logFile != "" {
235 args = append(args, "--logfile", c.logFile, "--logpriority", string(c.logLevel))
236 }
237 startArgs := args
235 if consoleFile != "" {238 if consoleFile != "" {
236 version, err := lxcStartVersion()239 startArgs = append(args, "--console-log", consoleFile)
237 if err != nil {240 }
238 return fmt.Errorf("cannot determine the lxc-start version: %v", err)241 _, err := run("lxc-start", startArgs...)
239 }242 // We may need to fallback to the older --console arg name.
240243 if err != nil && consoleFile != "" {
241 consoleFlag, err := generateConsoleFileArgument(version)244 startArgs = append(args, "--console", consoleFile)
242 if err != nil {245 _, err = run("lxc-start", startArgs...)
243 return fmt.Errorf("cannot determine the flag for specifying a console-output log file: %v", err)246 }
244 }
245 args = append(args, consoleFlag, consoleFile)
246 }
247 if c.logFile != "" {
248 args = append(args, "-o", c.logFile, "-l", string(c.logLevel))
249 }
250 _, err := run("lxc-start", args...)
251 if err != nil {247 if err != nil {
252 return err248 return err
253 }249 }
@@ -270,10 +266,10 @@
270 return nil266 return nil
271 }267 }
272 args := []string{268 args := []string{
273 "-n", c.name,269 "--name", c.name,
274 }270 }
275 if c.logFile != "" {271 if c.logFile != "" {
276 args = append(args, "-o", c.logFile, "-l", string(c.logLevel))272 args = append(args, "--logfile", c.logFile, "--logpriority", string(c.logLevel))
277 }273 }
278 _, err := run("lxc-stop", args...)274 _, err := run("lxc-stop", args...)
279 if err != nil {275 if err != nil {
@@ -326,10 +322,10 @@
326 return fmt.Errorf("container %q is not running", c.name)322 return fmt.Errorf("container %q is not running", c.name)
327 }323 }
328 args := []string{324 args := []string{
329 "-n", c.name,325 "--name", c.name,
330 }326 }
331 if c.logFile != "" {327 if c.logFile != "" {
332 args = append(args, "-o", c.logFile, "-l", string(c.logLevel))328 args = append(args, "--logfile", c.logFile, "--logpriority", string(c.logLevel))
333 }329 }
334 _, err := run("lxc-freeze", args...)330 _, err := run("lxc-freeze", args...)
335 if err != nil {331 if err != nil {
@@ -347,10 +343,10 @@
347 return fmt.Errorf("container %q is not frozen", c.name)343 return fmt.Errorf("container %q is not frozen", c.name)
348 }344 }
349 args := []string{345 args := []string{
350 "-n", c.name,346 "--name", c.name,
351 }347 }
352 if c.logFile != "" {348 if c.logFile != "" {
353 args = append(args, "-o", c.logFile, "-l", string(c.logLevel))349 args = append(args, "--logfile", c.logFile, "--logpriority", string(c.logLevel))
354 }350 }
355 _, err := run("lxc-unfreeze", args...)351 _, err := run("lxc-unfreeze", args...)
356 if err != nil {352 if err != nil {
@@ -384,7 +380,7 @@
384 stateStrs[i] = string(state)380 stateStrs[i] = string(state)
385 }381 }
386 waitStates := strings.Join(stateStrs, "|")382 waitStates := strings.Join(stateStrs, "|")
387 _, err := run("lxc-wait", "-n", c.name, "-s", waitStates)383 _, err := run("lxc-wait", "--name", c.name, "--state", waitStates)
388 if err != nil {384 if err != nil {
389 return err385 return err
390 }386 }
@@ -393,7 +389,7 @@
393389
394// Info returns the status and the process id of the container.390// Info returns the status and the process id of the container.
395func (c *container) Info() (State, int, error) {391func (c *container) Info() (State, int, error) {
396 out, err := run("lxc-info", "-n", c.name)392 out, err := run("lxc-info", "--name", c.name)
397 if err != nil {393 if err != nil {
398 return StateUnknown, -1, err394 return StateUnknown, -1, err
399 }395 }
@@ -444,26 +440,6 @@
444 return path.Join(c.containerHome(), "rootfs")440 return path.Join(c.containerHome(), "rootfs")
445}441}
446442
447func generateConsoleFileArgument(lxcVersion string) (flag string, err error) {
448 // Version <= 0.7 specified a console file via the -c flag. Newer
449 // versions utilize the -L flag.
450 if matched, err := regexp.MatchString(`^0\.[0-8]`, lxcVersion); err == nil {
451 if matched {
452 flag = "-c"
453 } else {
454 flag = "-L"
455 }
456 }
457
458 return flag, err
459}
460
461// Returns the lxc-start version
462func lxcStartVersion() (string, error) {
463 version, err := exec.Command("lxc-start", "--version").Output()
464 return string(version), err
465}
466
467// run executes the passed command and returns the out.443// run executes the passed command and returns the out.
468func run(name string, args ...string) (string, error) {444func run(name string, args ...string) (string, error) {
469 logger := loggo.GetLogger(fmt.Sprintf("golxc.run.%s", name))445 logger := loggo.GetLogger(fmt.Sprintf("golxc.run.%s", name))
470446
=== modified file 'golxc_test.go'
--- golxc_test.go 2014-07-28 22:24:12 +0000
+++ golxc_test.go 2014-07-30 01:57:33 +0000
@@ -368,21 +368,87 @@
368 s.IsolationSuite.SetUpSuite(c)368 s.IsolationSuite.SetUpSuite(c)
369}369}
370370
371// Test that our function which parses versions and returns the proper371func (s *commandArgs) setupLxcStart(c *C) {
372// console flag is working as intended.372 dir := c.MkDir()
373func (s *commandArgs) TestGenerateCorrectConsoleFileFlag(c *C) {373 // Make the rootfs for the "test" container so it thinks it is created.
374374 rootfs := filepath.Join(dir, "test", "rootfs")
375 assertGood := func(desiredFlag string) func(string, error) {375 err := os.MkdirAll(rootfs, 0755)
376 return func(flag string, err error) {376 c.Assert(err, IsNil)
377 c.Assert(err, IsNil)377 c.Assert(rootfs, jc.IsDirectory)
378 c.Assert(flag, Equals, desiredFlag)378
379 }379 s.PatchValue(&golxc.ContainerDir, dir)
380 }380 s.PatchEnvironment("PATH", s.originalPath)
381381 testing.PatchExecutableAsEchoArgs(c, s, "lxc-wait")
382 assertGood("-c")(golxc.GenerateConsoleFileArguments("0.8"))382 testing.PatchExecutableAsEchoArgs(c, s, "lxc-start")
383 assertGood("-L")(golxc.GenerateConsoleFileArguments("0.9"))383 info := "PID: 1234\nSTATE: RUNNING"
384 assertGood("-L")(golxc.GenerateConsoleFileArguments("1.8"))384 testing.PatchExecutable(c, s, "lxc-info", "#!/bin/sh\necho '"+info+"'\n")
385 assertGood("-L")(golxc.GenerateConsoleFileArguments("1.0.4"))385}
386
387func (s *commandArgs) TestStartArgs(c *C) {
388 s.setupLxcStart(c)
389 factory := golxc.Factory()
390 container := factory.New("test")
391 err := container.Start("config-file", "console-log")
392 c.Assert(err, IsNil)
393 testing.AssertEchoArgs(
394 c, "lxc-start",
395 "--daemon",
396 "--name", "test",
397 "--rcfile", "config-file",
398 "--console-log", "console-log")
399}
400
401func (s *commandArgs) TestStartArgsNoConsoleLog(c *C) {
402 s.setupLxcStart(c)
403 factory := golxc.Factory()
404 container := factory.New("test")
405 err := container.Start("config-file", "")
406 c.Assert(err, IsNil)
407 testing.AssertEchoArgs(
408 c, "lxc-start",
409 "--daemon",
410 "--name", "test",
411 "--rcfile", "config-file")
412}
413
414func (s *commandArgs) TestStartArgsFallback(c *C) {
415 dir := c.MkDir()
416 // Make the rootfs for the "test" container so it thinks it is created.
417 rootfs := filepath.Join(dir, "test", "rootfs")
418 err := os.MkdirAll(rootfs, 0755)
419 c.Assert(err, IsNil)
420 c.Assert(rootfs, jc.IsDirectory)
421
422 s.PatchValue(&golxc.ContainerDir, dir)
423 s.PatchEnvironment("PATH", s.originalPath)
424 testing.PatchExecutableAsEchoArgs(c, s, "lxc-wait")
425 EchoQuotedArgs := `
426name=` + "`basename $0`" + `
427argfile="$name.out"
428rm -f $argfile
429printf "%s" $name | tee -a $argfile
430for arg in "$@"; do
431 printf " \"%s\"" "$arg" | tee -a $argfile
432done
433printf "\n" | tee -a $argfile
434`
435 // Make lxc-start error if called with --console-log but succeed otherwise.
436 // On success, echo the args.
437 testing.PatchExecutable(
438 c, s, "lxc-start", "#!/bin/bash\nif [ $6 == '--console-log' ]; then\nexit 1\nelse\n"+EchoQuotedArgs+"fi")
439 info := "PID: 1234\nSTATE: RUNNING"
440 testing.PatchExecutable(c, s, "lxc-info", "#!/bin/sh\necho '"+info+"'\n")
441
442 factory := golxc.Factory()
443 container := factory.New("test")
444 err = container.Start("config-file", "console-log")
445 c.Assert(err, IsNil)
446 testing.AssertEchoArgs(
447 c, "lxc-start",
448 "--daemon",
449 "--name", "test",
450 "--rcfile", "config-file",
451 "--console", "console-log")
386}452}
387453
388func (s *commandArgs) TestCreateArgs(c *C) {454func (s *commandArgs) TestCreateArgs(c *C) {

Subscribers

People subscribed via source and target branches

to all changes: