func proxyCommand(host *config.Host, command string, dryRun bool) error { command = host.ExpandString(command) Logger.Debugf("ProxyCommand: %s", command) args, err := shlex.Split(command) if err != nil { return err } if dryRun { return fmt.Errorf("dry-run: Execute %s", args) } spawn := exec.Command(args[0], args[1:]...) spawn.Stdout = os.Stdout spawn.Stdin = os.Stdin spawn.Stderr = os.Stderr return spawn.Run() }
func hostPrepare(host *config.Host) error { if host.HostName == "" { host.HostName = host.Name() } if len(host.ResolveNameservers) > 0 { Logger.Debugf("Resolving host: '%s' using nameservers %s", host.HostName, host.ResolveNameservers) // FIXME: resolve using custom dns server results, err := net.LookupAddr(host.HostName) if err != nil { return err } if len(results) > 0 { host.HostName = results[0] } Logger.Debugf("Resolved host is: %s", host.HostName) } if host.ResolveCommand != "" { command := host.ExpandString(host.ResolveCommand) Logger.Debugf("Resolving host: %q using command: %q", host.HostName, command) args, err := shlex.Split(command) if err != nil { return err } cmd := exec.Command(args[0], args[1:]...) var stdout bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr if err := cmd.Run(); err != nil { Logger.Errorf("ResolveCommand failed: %s", stderr.String()) return err } host.HostName = strings.TrimSpace(fmt.Sprintf("%s", stdout.String())) Logger.Debugf("Resolved host is: %s", host.HostName) } return nil }
func (c *Container) parseCmd() { flags := flag.NewFlagSet("run", flag.ExitOnError) flRemove := flags.Bool([]string{"#rm", "-rm"}, false, "") flDetach := flags.Bool([]string{"d", "-detach"}, false, "") flName := flags.String([]string{"#name", "-name"}, "", "") args, err := shlex.Split(c.ContainerCfg.Cmd) if err != nil { c.Err = err return } log.Debugf("Parsing [%s]", strings.Join(args, ",")) c.Config, c.HostConfig, _, c.Err = runconfig.Parse(flags, args) c.Name = *flName c.detach = *flDetach c.remove = *flRemove }
func hostPrepare(host *config.Host) error { if host.HostName == "" { host.HostName = host.Name() } if len(host.ResolveNameservers) > 0 { Logger.Debugf("Resolving host: '%s' using nameservers %s", host.HostName, host.ResolveNameservers) // FIXME: resolve using custom dns server results, err := net.LookupAddr(host.HostName) if err != nil { return err } if len(results) > 0 { host.HostName = results[0] } Logger.Debugf("Resolved host is: %s", host.HostName) } if host.ResolveCommand != "" { command := host.ExpandString(host.ResolveCommand) Logger.Debugf("Resolving host: %q using command: %q", host.HostName, command) args, err := shlex.Split(command) if err != nil { return err } out, err := exec.Command(args[0], args[1:]...).Output() if err != nil { return err } host.HostName = strings.TrimSpace(fmt.Sprintf("%s", out)) Logger.Debugf("Resolved host is: %s", host.HostName) } return nil }
func TestParseCommand(t *testing.T) { exp := []string{"sh", "-c", "exec /opt/bin/flanneld -logtostderr=true -iface=${NODE_IP}"} cmd, err := shlex.Split("sh -c 'exec /opt/bin/flanneld -logtostderr=true -iface=${NODE_IP}'") assert.Nil(t, err) assert.Equal(t, exp, cmd) }
func Convert(c *project.ServiceConfig) (*runconfig.Config, *runconfig.HostConfig, error) { vs := Filter(c.Volumes, isVolume) volumes := make(map[string]struct{}, len(vs)) for _, v := range vs { volumes[v] = struct{}{} } cmd, _ := shlex.Split(c.Command) entrypoint, _ := shlex.Split(c.Entrypoint) ports, binding, err := nat.ParsePortSpecs(c.Ports) if err != nil { return nil, nil, err } restart, err := runconfig.ParseRestartPolicy(c.Restart) if err != nil { return nil, nil, err } dns := c.Dns.Slice() dnssearch := c.DnsSearch.Slice() labels := c.Labels.MapParts() if len(c.Expose) > 0 { exposedPorts, _, err := nat.ParsePortSpecs(c.Expose) ports = exposedPorts if err != nil { return nil, nil, err } } config := &runconfig.Config{ Entrypoint: runconfig.NewEntrypoint(entrypoint...), Hostname: c.Hostname, Domainname: c.DomainName, User: c.User, Env: c.Environment.Slice(), Cmd: runconfig.NewCommand(cmd...), Image: c.Image, Labels: labels, ExposedPorts: ports, Tty: c.Tty, OpenStdin: c.StdinOpen, WorkingDir: c.WorkingDir, Volumes: volumes, } host_config := &runconfig.HostConfig{ VolumesFrom: c.VolumesFrom, CapAdd: c.CapAdd, CapDrop: c.CapDrop, CpuShares: c.CpuShares, Privileged: c.Privileged, Binds: Filter(c.Volumes, isBind), Dns: dns, DnsSearch: dnssearch, LogConfig: runconfig.LogConfig{ Type: c.LogDriver, }, Memory: c.MemLimit, NetworkMode: runconfig.NetworkMode(c.Net), ReadonlyRootfs: c.ReadOnly, PidMode: runconfig.PidMode(c.Pid), IpcMode: runconfig.IpcMode(c.Ipc), PortBindings: binding, RestartPolicy: restart, } return config, host_config, nil }