~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/lxc/lxd/lxc/action.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "fmt"
 
5
 
 
6
        "github.com/lxc/lxd"
 
7
        "github.com/lxc/lxd/shared"
 
8
        "github.com/lxc/lxd/shared/gnuflag"
 
9
        "github.com/lxc/lxd/shared/i18n"
 
10
)
 
11
 
 
12
type actionCmd struct {
 
13
        action     shared.ContainerAction
 
14
        hasTimeout bool
 
15
        visible    bool
 
16
        name       string
 
17
        timeout    int
 
18
        force      bool
 
19
        stateful   bool
 
20
        stateless  bool
 
21
}
 
22
 
 
23
func (c *actionCmd) showByDefault() bool {
 
24
        return c.visible
 
25
}
 
26
 
 
27
func (c *actionCmd) usage() string {
 
28
        return fmt.Sprintf(i18n.G(
 
29
                `Changes state of one or more containers to %s.
 
30
 
 
31
lxc %s <name> [<name>...]`), c.name, c.name)
 
32
}
 
33
 
 
34
func (c *actionCmd) flags() {
 
35
        if c.hasTimeout {
 
36
                gnuflag.IntVar(&c.timeout, "timeout", -1, i18n.G("Time to wait for the container before killing it."))
 
37
                gnuflag.BoolVar(&c.force, "force", false, i18n.G("Force the container to shutdown."))
 
38
                gnuflag.BoolVar(&c.stateful, "stateful", false, i18n.G("Store the container state (only for stop)."))
 
39
                gnuflag.BoolVar(&c.stateless, "stateless", false, i18n.G("Ignore the container state (only forstart)."))
 
40
        }
 
41
}
 
42
 
 
43
func (c *actionCmd) run(config *lxd.Config, args []string) error {
 
44
        if len(args) == 0 {
 
45
                return errArgs
 
46
        }
 
47
 
 
48
        state := false
 
49
 
 
50
        // Only store state if asked to
 
51
        if c.action == "stop" && c.stateful {
 
52
                state = true
 
53
        }
 
54
 
 
55
        for _, nameArg := range args {
 
56
                remote, name := config.ParseRemoteAndContainer(nameArg)
 
57
                d, err := lxd.NewClient(config, remote)
 
58
                if err != nil {
 
59
                        return err
 
60
                }
 
61
 
 
62
                if name == "" {
 
63
                        return fmt.Errorf(i18n.G("Must supply container name for: ")+"\"%s\"", nameArg)
 
64
                }
 
65
 
 
66
                if c.action == shared.Start || c.action == shared.Stop {
 
67
                        current, err := d.ContainerInfo(name)
 
68
                        if err != nil {
 
69
                                return err
 
70
                        }
 
71
 
 
72
                        // "start" for a frozen container means "unfreeze"
 
73
                        if current.StatusCode == shared.Frozen {
 
74
                                c.action = shared.Unfreeze
 
75
                        }
 
76
 
 
77
                        // Always restore state (if present) unless asked not to
 
78
                        if c.action == shared.Start && current.Stateful && !c.stateless {
 
79
                                state = true
 
80
                        }
 
81
                }
 
82
 
 
83
                resp, err := d.Action(name, c.action, c.timeout, c.force, state)
 
84
                if err != nil {
 
85
                        return err
 
86
                }
 
87
 
 
88
                if resp.Type != lxd.Async {
 
89
                        return fmt.Errorf(i18n.G("bad result type from action"))
 
90
                }
 
91
 
 
92
                if err := d.WaitForSuccess(resp.Operation); err != nil {
 
93
                        return fmt.Errorf("%s\n"+i18n.G("Try `lxc info --show-log %s` for more info"), err, name)
 
94
                }
 
95
        }
 
96
        return nil
 
97
}