mirror of
https://github.com/rancher/os.git
synced 2025-04-28 03:20:50 +00:00
Support string values for runcmd
This commit is contained in:
parent
ee16cd4311
commit
c1abc67fa8
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/coreos/coreos-cloudinit/config"
|
"github.com/coreos/coreos-cloudinit/config"
|
||||||
"github.com/docker/engine-api/types"
|
"github.com/docker/engine-api/types"
|
||||||
composeConfig "github.com/docker/libcompose/config"
|
composeConfig "github.com/docker/libcompose/config"
|
||||||
|
"github.com/rancher/os/config/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -83,13 +84,13 @@ type Repository struct {
|
|||||||
type Repositories map[string]Repository
|
type Repositories map[string]Repository
|
||||||
|
|
||||||
type CloudConfig struct {
|
type CloudConfig struct {
|
||||||
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"`
|
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"`
|
||||||
WriteFiles []File `yaml:"write_files"`
|
WriteFiles []File `yaml:"write_files"`
|
||||||
Hostname string `yaml:"hostname"`
|
Hostname string `yaml:"hostname"`
|
||||||
Mounts [][]string `yaml:"mounts,omitempty"`
|
Mounts [][]string `yaml:"mounts,omitempty"`
|
||||||
Rancher RancherConfig `yaml:"rancher,omitempty"`
|
Rancher RancherConfig `yaml:"rancher,omitempty"`
|
||||||
Runcmd [][]string `yaml:"runcmd,omitempty"`
|
Runcmd []yaml.StringandSlice `yaml:"runcmd,omitempty"`
|
||||||
Bootcmd [][]string `yaml:"bootcmd,omitempty"`
|
Bootcmd []yaml.StringandSlice `yaml:"bootcmd,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
|
44
config/yaml/command.go
Normal file
44
config/yaml/command.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package yaml
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// StringandSlice stores either a string or slice depending on original type
|
||||||
|
// Differs from libcompose Stringorslice by being able to determine original type
|
||||||
|
type StringandSlice struct {
|
||||||
|
StringValue string
|
||||||
|
SliceValue []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalYAML implements the Unmarshaller interface.
|
||||||
|
// TODO: this needs to be ported to go-yaml
|
||||||
|
func (s *StringandSlice) UnmarshalYAML(tag string, value interface{}) error {
|
||||||
|
switch value := value.(type) {
|
||||||
|
case []interface{}:
|
||||||
|
parts, err := toStrings(value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.SliceValue = parts
|
||||||
|
case string:
|
||||||
|
s.StringValue = value
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Failed to unmarshal StringandSlice: %#v", value)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: use this function from libcompose
|
||||||
|
func toStrings(s []interface{}) ([]string, error) {
|
||||||
|
if len(s) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
r := make([]string, len(s))
|
||||||
|
for k, v := range s {
|
||||||
|
if sv, ok := v.(string); ok {
|
||||||
|
r[k] = sv
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("Cannot unmarshal '%v' of type %T into a string value", v, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
@ -7,12 +7,13 @@ layout: os-default
|
|||||||
## Running Commands
|
## Running Commands
|
||||||
---
|
---
|
||||||
|
|
||||||
You can automate running commands on boot using the `runcmd` cloud-config directive. Commands must be specified in a list syntax as in the following example.
|
You can automate running commands on boot using the `runcmd` cloud-config directive. Commands can be specified as either a list or a string. In the latter case, the command is executed with `sh`.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
#cloud-config
|
#cloud-config
|
||||||
runcmd:
|
runcmd:
|
||||||
- [ touch, /home/rancher/test ]
|
- [ touch, /home/rancher/test1 ]
|
||||||
|
- echo "test" > /home/rancher/test2
|
||||||
```
|
```
|
||||||
|
|
||||||
Commands specified using `runcmd` will be executed within the context of the `console` container. More details on the ordering of commands run in the `console` container can be found [here]({{site.baseurl}}/os/system-services/built-in-system-services/#console).
|
Commands specified using `runcmd` will be executed within the context of the `console` container. More details on the ordering of commands run in the `console` container can be found [here]({{site.baseurl}}/os/system-services/built-in-system-services/#console).
|
||||||
|
@ -27,5 +27,6 @@ runcmd:
|
|||||||
- []
|
- []
|
||||||
- [ test ]
|
- [ test ]
|
||||||
- [ touch, /home/rancher/test2 ]
|
- [ touch, /home/rancher/test2 ]
|
||||||
|
- echo "" > /home/rancher/test5
|
||||||
ssh_authorized_keys:
|
ssh_authorized_keys:
|
||||||
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC85w9stZyiLQp/DkVO6fqwiShYcj1ClKdtCqgHtf+PLpJkFReSFu8y21y+ev09gsSMRRrjF7yt0pUHV6zncQhVeqsZtgc5WbELY2DOYUGmRn/CCvPbXovoBrQjSorqlBmpuPwsStYLr92Xn+VVsMNSUIegHY22DphGbDKG85vrKB8HxUxGIDxFBds/uE8FhSy+xsoyT/jUZDK6pgq2HnGl6D81ViIlKecpOpWlW3B+fea99ADNyZNVvDzbHE5pcI3VRw8u59WmpWOUgT6qacNVACl8GqpBvQk8sw7O/X9DSZHCKafeD9G5k+GYbAUz92fKWrx/lOXfUXPS3+c8dRIF
|
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC85w9stZyiLQp/DkVO6fqwiShYcj1ClKdtCqgHtf+PLpJkFReSFu8y21y+ev09gsSMRRrjF7yt0pUHV6zncQhVeqsZtgc5WbELY2DOYUGmRn/CCvPbXovoBrQjSorqlBmpuPwsStYLr92Xn+VVsMNSUIegHY22DphGbDKG85vrKB8HxUxGIDxFBds/uE8FhSy+xsoyT/jUZDK6pgq2HnGl6D81ViIlKecpOpWlW3B+fea99ADNyZNVvDzbHE5pcI3VRw8u59WmpWOUgT6qacNVACl8GqpBvQk8sw7O/X9DSZHCKafeD9G5k+GYbAUz92fKWrx/lOXfUXPS3+c8dRIF
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
func (s *QemuSuite) TestStartCommands(c *C) {
|
func (s *QemuSuite) TestStartCommands(c *C) {
|
||||||
s.RunQemu(c, "--cloud-config", "./tests/assets/test_26/cloud-config.yml")
|
s.RunQemu(c, "--cloud-config", "./tests/assets/test_26/cloud-config.yml")
|
||||||
|
|
||||||
for i := 1; i < 5; i++ {
|
for i := 1; i < 6; i++ {
|
||||||
s.CheckCall(c, fmt.Sprintf("ls /home/rancher | grep test%d", i))
|
s.CheckCall(c, fmt.Sprintf("ls /home/rancher | grep test%d", i))
|
||||||
}
|
}
|
||||||
s.CheckCall(c, "docker ps | grep nginx")
|
s.CheckCall(c, "docker ps | grep nginx")
|
||||||
|
12
util/util.go
12
util/util.go
@ -12,6 +12,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
yaml "github.com/cloudfoundry-incubator/candiedyaml"
|
yaml "github.com/cloudfoundry-incubator/candiedyaml"
|
||||||
|
osYaml "github.com/rancher/os/config/yaml"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -269,13 +270,16 @@ func RunScript(path string) error {
|
|||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunCommandSequence(commandSequence [][]string) {
|
func RunCommandSequence(commandSequence []osYaml.StringandSlice) {
|
||||||
for _, command := range commandSequence {
|
for _, command := range commandSequence {
|
||||||
if len(command) == 0 {
|
var cmd *exec.Cmd
|
||||||
|
if command.StringValue != "" {
|
||||||
|
cmd = exec.Command("sh", "-c", command.StringValue)
|
||||||
|
} else if len(command.SliceValue) > 0 {
|
||||||
|
cmd = exec.Command(command.SliceValue[0], command.SliceValue[1:]...)
|
||||||
|
} else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(command[0], command[1:]...)
|
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user