Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 package container | 1 package container |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "launchpad.net/juju-core/state" | 5 "launchpad.net/juju-core/state" |
6 "launchpad.net/juju-core/upstart" | 6 "launchpad.net/juju-core/upstart" |
7 "os" | 7 "os" |
8 "os/exec" | 8 "os/exec" |
9 "path/filepath" | 9 "path/filepath" |
10 "strings" | 10 "strings" |
11 ) | 11 ) |
12 | 12 |
13 // Container contains running juju service units. | |
14 type Container interface { | |
15 // Deploy deploys the unit into a new container. | |
16 Deploy(unit *state.Unit) error | |
17 | |
18 // Destroy destroys the unit's container. | |
19 Destroy(unit *state.Unit) error | |
20 } | |
21 | |
22 // Simple is a Container that knows how deploy units within····· | |
23 // the current machine. | |
24 type Simple struct { | |
25 DataDir string | |
26 InitDir string | |
27 } | |
28 | |
13 // Config holds information about where containers should | 29 // Config holds information about where containers should |
14 // be started. | 30 // be started. |
15 type Config struct { | 31 type Config struct { |
16 » VarDir string | 32 » DataDir string |
17 // InitDir holds the directory where upstart scripts | 33 // InitDir holds the directory where upstart scripts |
18 // will be deployed. If blank, the system default will | 34 // will be deployed. If blank, the system default will |
19 // be used. | 35 // be used. |
fwereade
2012/09/12 15:17:26
Apropos of not-very-much, how would you feel about
| |
20 InitDir string | 36 InitDir string |
21 | 37 |
22 // TODO(rog) add LogDir? | 38 // TODO(rog) add LogDir? |
23 } | 39 } |
24 | 40 |
25 func deslash(s string) string { | 41 func deslash(s string) string { |
26 return strings.Replace(s, "/", "-", -1) | 42 return strings.Replace(s, "/", "-", -1) |
27 } | 43 } |
28 | 44 |
29 func (c *simple) dirName(unit *state.Unit) string { | 45 func (c *Simple) dirName(unit *state.Unit) string { |
30 » return filepath.Join(c.cfg.VarDir, "units", deslash(unit.Name())) | 46 » return filepath.Join(c.DataDir, "units", deslash(unit.Name())) |
31 } | 47 } |
32 | 48 |
33 func (c *simple) service(unit *state.Unit) *upstart.Service { | 49 func (c *Simple) service(unit *state.Unit) *upstart.Service { |
34 svc := upstart.NewService("juju-agent-" + deslash(unit.Name())) | 50 svc := upstart.NewService("juju-agent-" + deslash(unit.Name())) |
35 » if c.cfg.InitDir != "" { | 51 » if c.InitDir != "" { |
36 » » svc.InitDir = c.cfg.InitDir | 52 » » svc.InitDir = c.InitDir |
37 } | 53 } |
38 return svc | 54 return svc |
39 } | 55 } |
40 | 56 |
41 // Deploy deploys the unit into a new container. | 57 func (c *Simple) Deploy(unit *state.Unit) (err error) { |
42 func Deploy(cfg Config, unit *state.Unit) (err error) { | |
43 » // TODO choose an LXC container when the unit requires isolation. | |
fwereade
2012/09/12 15:17:26
s/choose/create/
| |
44 » cont := &simple{cfg} | |
45 » return cont.deploy(unit) | |
46 } | |
47 | |
48 // Destroy destroys the unit's container. | |
49 func Destroy(cfg Config, unit *state.Unit) error { | |
50 » cont := &simple{cfg} | |
51 » return cont.destroy(unit) | |
52 } | |
53 | |
54 // simple knows how deploy units within the current machine. | |
55 type simple struct { | |
56 » cfg Config | |
57 } | |
58 | |
59 func (c *simple) deploy(unit *state.Unit) (err error) { | |
60 exe, err := exec.LookPath("jujud") | 58 exe, err := exec.LookPath("jujud") |
61 if err != nil { | 59 if err != nil { |
62 return fmt.Errorf("cannot find executable: %v", err) | 60 return fmt.Errorf("cannot find executable: %v", err) |
63 } | 61 } |
64 conf := &upstart.Conf{ | 62 conf := &upstart.Conf{ |
65 Service: *c.service(unit), | 63 Service: *c.service(unit), |
66 Desc: "juju unit agent for " + unit.Name(), | 64 Desc: "juju unit agent for " + unit.Name(), |
67 Cmd: exe + " unit --unit-name " + unit.Name(), | 65 Cmd: exe + " unit --unit-name " + unit.Name(), |
68 // TODO: Out | 66 // TODO: Out |
69 } | 67 } |
70 dir := c.dirName(unit) | 68 dir := c.dirName(unit) |
71 if err := os.MkdirAll(dir, 0755); err != nil { | 69 if err := os.MkdirAll(dir, 0755); err != nil { |
72 return err | 70 return err |
73 } | 71 } |
74 err = conf.Install() | 72 err = conf.Install() |
75 if err != nil { | 73 if err != nil { |
76 os.Remove(dir) | 74 os.Remove(dir) |
77 return err | 75 return err |
78 } | 76 } |
79 return nil | 77 return nil |
80 } | 78 } |
81 | 79 |
82 func (c *simple) destroy(unit *state.Unit) error { | 80 func (c *Simple) Destroy(unit *state.Unit) error { |
83 if err := c.service(unit).Remove(); err != nil { | 81 if err := c.service(unit).Remove(); err != nil { |
84 return err | 82 return err |
85 } | 83 } |
86 return os.RemoveAll(c.dirName(unit)) | 84 return os.RemoveAll(c.dirName(unit)) |
87 } | 85 } |
LEFT | RIGHT |