OLD | NEW |
1 package upstart | 1 package upstart |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | 4 "bytes" |
5 "errors" | 5 "errors" |
6 "fmt" | 6 "fmt" |
7 "io/ioutil" | 7 "io/ioutil" |
8 "os" | 8 "os" |
9 "os/exec" | 9 "os/exec" |
10 "path/filepath" | 10 "path/filepath" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 86 } |
87 | 87 |
88 // BUG: %q quoting does not necessarily match libnih quoting rules | 88 // BUG: %q quoting does not necessarily match libnih quoting rules |
89 // (as used by upstart); this may become an issue in the future. | 89 // (as used by upstart); this may become an issue in the future. |
90 var confT = template.Must(template.New("").Parse(` | 90 var confT = template.Must(template.New("").Parse(` |
91 description "{{.Desc}}" | 91 description "{{.Desc}}" |
92 author "Juju Team <juju@lists.ubuntu.com>" | 92 author "Juju Team <juju@lists.ubuntu.com>" |
93 start on runlevel [2345] | 93 start on runlevel [2345] |
94 stop on runlevel [!2345] | 94 stop on runlevel [!2345] |
95 respawn | 95 respawn |
| 96 normal exit 0 |
96 {{range $k, $v := .Env}}env {{$k}}={{$v|printf "%q"}} | 97 {{range $k, $v := .Env}}env {{$k}}={{$v|printf "%q"}} |
97 {{end}} | 98 {{end}} |
98 exec {{.Cmd}}{{if .Out}} >> {{.Out}} 2>&1{{end}} | 99 exec {{.Cmd}}{{if .Out}} >> {{.Out}} 2>&1{{end}} |
99 `[1:])) | 100 `[1:])) |
100 | 101 |
101 // Conf is responsible for defining and installing upstart services. Its fields | 102 // Conf is responsible for defining and installing upstart services. Its fields |
102 // represent elements of an upstart service configuration file. | 103 // represent elements of an upstart service configuration file. |
103 type Conf struct { | 104 type Conf struct { |
104 Service | 105 Service |
105 // Desc is the upstart service's description. | 106 // Desc is the upstart service's description. |
106 Desc string | 107 Desc string |
107 // Env holds the environment variables that will be set when the command
runs. | 108 // Env holds the environment variables that will be set when the command
runs. |
108 Env map[string]string | 109 Env map[string]string |
109 // Cmd is the command (with arguments) that will be run. | 110 // Cmd is the command (with arguments) that will be run. |
| 111 // The command will be restarted if it exits with a non-zero exit code. |
110 Cmd string | 112 Cmd string |
111 // Out, if set, will redirect output to that path. | 113 // Out, if set, will redirect output to that path. |
112 Out string | 114 Out string |
113 } | 115 } |
114 | 116 |
115 // validate returns an error if the service is not adequately defined. | 117 // validate returns an error if the service is not adequately defined. |
116 func (c *Conf) validate() error { | 118 func (c *Conf) validate() error { |
117 if c.Name == "" { | 119 if c.Name == "" { |
118 return errors.New("missing Name") | 120 return errors.New("missing Name") |
119 } | 121 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 func (c *Conf) InstallCommands() ([]string, error) { | 165 func (c *Conf) InstallCommands() ([]string, error) { |
164 conf, err := c.render() | 166 conf, err := c.render() |
165 if err != nil { | 167 if err != nil { |
166 return nil, err | 168 return nil, err |
167 } | 169 } |
168 return []string{ | 170 return []string{ |
169 fmt.Sprintf("cat >> %s << 'EOF'\n%sEOF\n", c.confPath(), conf), | 171 fmt.Sprintf("cat >> %s << 'EOF'\n%sEOF\n", c.confPath(), conf), |
170 "start " + c.Name, | 172 "start " + c.Name, |
171 }, nil | 173 }, nil |
172 } | 174 } |
OLD | NEW |