• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang exec.RunCommands函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/juju/utils/exec.RunCommands函数的典型用法代码示例。如果您正苦于以下问题:Golang RunCommands函数的具体用法?Golang RunCommands怎么用?Golang RunCommands使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了RunCommands函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestNewShellSelectCommandPosix

func (s *discoverySuite) TestNewShellSelectCommandPosix(c *gc.C) {
	if runtime.GOOS == "windows" {
		c.Skip("not supported on windows")
	}

	discoveryScript := service.DiscoverInitSystemScript()
	handler := func(initSystem string) (string, bool) {
		return "echo -n " + initSystem, true
	}
	script := "init_system=$(" + discoveryScript + ")\n"
	// The script will fail with exit 1 if it cannot match in init system.
	script += service.NewShellSelectCommand("init_system", "exit 1", handler)
	commands, filename := s.writeScript(c, "test_shell_select.sh", script)
	commands += "sh " + filename
	response, err := exec.RunCommands(exec.RunParams{
		Commands: script,
	})
	c.Assert(err, jc.ErrorIsNil)

	initSystem, err := service.DiscoverInitSystem()
	c.Assert(err, jc.ErrorIsNil)
	c.Check(response.Code, gc.Equals, 0)
	c.Check(string(response.Stdout), gc.Equals, initSystem)
	c.Check(string(response.Stderr), gc.Equals, "")
}
开发者ID:imoapps,项目名称:juju,代码行数:25,代码来源:discovery_test.go


示例2: executeNoContext

func (c *RunCommand) executeNoContext() (*exec.ExecResponse, error) {
	// Acquire the uniter hook execution lock to make sure we don't
	// stomp on each other.
	spec := mutex.Spec{
		Name:  c.MachineLockName,
		Clock: clock.WallClock,
		Delay: 250 * time.Millisecond,
	}
	logger.Debugf("acquire lock %q for juju-run", c.MachineLockName)
	releaser, err := mutex.Acquire(spec)
	if err != nil {
		return nil, errors.Trace(err)
	}
	logger.Debugf("lock %q acquired", c.MachineLockName)

	// Defer the logging first so it is executed after the Release. LIFO.
	defer logger.Debugf("release lock %q for juju-run", c.MachineLockName)
	defer releaser.Release()

	runCmd := c.appendProxyToCommands()

	return exec.RunCommands(
		exec.RunParams{
			Commands: runCmd,
		})
}
开发者ID:bac,项目名称:juju,代码行数:26,代码来源:run.go


示例3: writeEnvironmentFile

func (w *MachineEnvironmentWorker) writeEnvironmentFile() error {
	// Writing the environment file is handled by executing the script for two
	// primary reasons:
	//
	// 1: In order to have the local provider specify the environment settings
	// for the machine agent running on the host, this worker needs to run,
	// but it shouldn't be touching any files on the disk.  If however there is
	// an ubuntu user, it will. This shouldn't be a problem.
	//
	// 2: On cloud-instance ubuntu images, the ubuntu user is uid 1000, but in
	// the situation where the ubuntu user has been created as a part of the
	// manual provisioning process, the user will exist, and will not have the
	// same uid/gid as the default cloud image.
	//
	// It is easier to shell out to check both these things, and is also the
	// same way that the file is written in the cloud-init process, so
	// consistency FTW.
	filePath := path.Join(ProxyDirectory, ProxyFile)
	result, err := exec.RunCommands(exec.RunParams{
		Commands: fmt.Sprintf(
			`[ -e %s ] && (printf '%%s\n' %s > %s && chown ubuntu:ubuntu %s)`,
			ProxyDirectory,
			utils.ShQuote(w.proxy.AsScriptEnvironment()),
			filePath, filePath),
		WorkingDir: ProxyDirectory,
	})
	if err != nil {
		return err
	}
	if result.Code != 0 {
		logger.Errorf("failed writing new proxy values: \n%s\n%s", result.Stdout, result.Stderr)
	}
	return nil
}
开发者ID:rogpeppe,项目名称:juju,代码行数:34,代码来源:machineenvironmentworker.go


示例4: writeEnvironmentToRegistry

func (w *proxyWorker) writeEnvironmentToRegistry() error {
	// On windows we write the proxy settings to the registry.
	setProxyScript := `$value_path = "%s"
    $new_proxy = "%s"
    $proxy_val = Get-ItemProperty -Path $value_path -Name ProxySettings
    if ($? -eq $false){ New-ItemProperty -Path $value_path -Name ProxySettings -PropertyType String -Value $new_proxy }else{ Set-ItemProperty -Path $value_path -Name ProxySettings -Value $new_proxy }
    `

	if w.config.RegistryPath == "" {
		err := fmt.Errorf("config.RegistryPath is empty")
		logger.Errorf("writeEnvironmentToRegistry couldn't write proxy settings to registry: %s", err)
		return err
	}

	result, err := exec.RunCommands(exec.RunParams{
		Commands: fmt.Sprintf(
			setProxyScript,
			w.config.RegistryPath,
			w.proxy.Http),
	})
	if err != nil {
		return err
	}
	if result.Code != 0 {
		logger.Errorf("failed writing new proxy values: \n%s\n%s", result.Stdout, result.Stderr)
	}
	return nil
}
开发者ID:makyo,项目名称:juju,代码行数:28,代码来源:proxyupdater.go


示例5: writeEnvironmentFile

func (w *proxyWorker) writeEnvironmentFile() error {
	// Writing the environment file is handled by executing the script:
	//
	// On cloud-instance ubuntu images, the ubuntu user is uid 1000, but in
	// the situation where the ubuntu user has been created as a part of the
	// manual provisioning process, the user will exist, and will not have the
	// same uid/gid as the default cloud image.
	//
	// It is easier to shell out to check, and is also the same way that the file
	// is written in the cloud-init process, so consistency FTW.
	filePath := path.Join(w.config.Directory, w.config.Filename)
	result, err := exec.RunCommands(exec.RunParams{
		Commands: fmt.Sprintf(
			`[ -e %s ] && (printf '%%s\n' %s > %s && chown ubuntu:ubuntu %s)`,
			w.config.Directory,
			utils.ShQuote(w.proxy.AsScriptEnvironment()),
			filePath, filePath),
		WorkingDir: w.config.Directory,
	})

	if err != nil {
		return err
	}
	if result.Code != 0 {
		logger.Errorf("failed writing new proxy values: \n%s\n%s", result.Stdout, result.Stderr)
	}
	return nil
}
开发者ID:makyo,项目名称:juju,代码行数:28,代码来源:proxyupdater.go


示例6: RunCommands

// RunCommands executes the commands in an environment which allows it to to
// call back into the hook context to execute jujuc tools.
func (ctx *HookContext) RunCommands(commands, charmDir, toolsDir, socketPath string) (*utilexec.ExecResponse, error) {
	env := ctx.hookVars(charmDir, toolsDir, socketPath)
	result, err := utilexec.RunCommands(
		utilexec.RunParams{
			Commands:    commands,
			WorkingDir:  charmDir,
			Environment: env})
	return result, ctx.finalizeContext("run commands", err)
}
开发者ID:kapilt,项目名称:juju,代码行数:11,代码来源:context.go


示例7: TestExecUnknownCommand

func (*execSuite) TestExecUnknownCommand(c *gc.C) {
	result, err := exec.RunCommands(
		exec.RunParams{
			Commands: "unknown-command",
		},
	)
	c.Assert(err, gc.IsNil)
	c.Assert(result.Stdout, gc.HasLen, 0)
	c.Assert(string(result.Stderr), jc.Contains, "unknown-command: command not found")
	// 127 is a special bash return code meaning command not found.
	c.Assert(result.Code, gc.Equals, 127)
}
开发者ID:natefinch,项目名称:utils,代码行数:12,代码来源:exec_test.go


示例8: TestExecUnknownCommand

func (*execSuite) TestExecUnknownCommand(c *gc.C) {
	result, err := exec.RunCommands(
		exec.RunParams{
			Commands: "unknown-command",
		},
	)
	c.Assert(err, gc.IsNil)
	c.Assert(result.Stdout, gc.HasLen, 0)
	c.Assert(string(result.Stderr), jc.Contains, "is not recognized as the name of a cmdlet")
	// 1 is returned by RunCommands when powershell commands throw exceptions
	c.Assert(result.Code, gc.Equals, 1)
}
开发者ID:wwitzel3,项目名称:utils,代码行数:12,代码来源:exec_windows_test.go


示例9: runPsCommand

func runPsCommand(cmd string) (*exec.ExecResponse, error) {
	com := exec.RunParams{
		Commands: cmd,
	}
	out, err := exec.RunCommands(com)
	if err != nil {
		return nil, err
	}
	if out.Code != 0 {
		return nil, fmt.Errorf("Error running %s: %s", cmd, string(out.Stderr))
	}
	return out, nil
}
开发者ID:kapilt,项目名称:juju,代码行数:13,代码来源:service.go


示例10: windowsListServices

func windowsListServices() ([]string, error) {
	com := exec.RunParams{
		Commands: `(Get-Service).Name`,
	}
	out, err := exec.RunCommands(com)
	if err != nil {
		return nil, err
	}
	if out.Code != 0 {
		return nil, fmt.Errorf("Error running %s: %s", com.Commands, string(out.Stderr))
	}
	return strings.Fields(string(out.Stdout)), nil
}
开发者ID:kapilt,项目名称:juju,代码行数:13,代码来源:service.go


示例11: TestRunCommands

func (*execSuite) TestRunCommands(c *gc.C) {
	newDir, err := longPathAsString(c.MkDir())
	c.Assert(err, gc.IsNil)
	for i, test := range []struct {
		message     string
		commands    string
		workingDir  string
		environment []string
		stdout      string
		stderr      string
		code        int
	}{
		{
			message:  "test stdout capture",
			commands: "echo 'testing stdout'",
			stdout:   "testing stdout\r\n",
		}, {
			message:  "test stderr capture",
			commands: "Write-Error 'testing stderr'",
			stderr:   "testing stderr\r\n",
		}, {
			message:  "test return code",
			commands: "exit 42",
			code:     42,
		}, {
			message:    "test working dir",
			commands:   "(pwd).Path",
			workingDir: newDir,
			stdout:     filepath.FromSlash(newDir) + "\r\n",
		}, {
			message:     "test environment",
			commands:    "echo $env:OMG_IT_WORKS",
			environment: []string{"OMG_IT_WORKS=like magic"},
			stdout:      "like magic\r\n",
		},
	} {
		c.Logf("%v: %s", i, test.message)

		result, err := exec.RunCommands(
			exec.RunParams{
				Commands:    test.commands,
				WorkingDir:  test.workingDir,
				Environment: test.environment,
			})
		c.Assert(err, gc.IsNil)
		c.Assert(string(result.Stdout), gc.Equals, test.stdout)
		c.Assert(string(result.Stderr), jc.Contains, test.stderr)
		c.Assert(result.Code, gc.Equals, test.code)
	}
}
开发者ID:wwitzel3,项目名称:utils,代码行数:50,代码来源:exec_windows_test.go


示例12: TestRunCommands

func (*execSuite) TestRunCommands(c *gc.C) {
	newDir := c.MkDir()

	for i, test := range []struct {
		message     string
		commands    string
		workingDir  string
		environment []string
		stdout      string
		stderr      string
		code        int
	}{
		{
			message:  "test stdout capture",
			commands: "echo testing stdout",
			stdout:   "testing stdout\n",
		}, {
			message:  "test stderr capture",
			commands: "echo testing stderr >&2",
			stderr:   "testing stderr\n",
		}, {
			message:  "test return code",
			commands: "exit 42",
			code:     42,
		}, {
			message:    "test working dir",
			commands:   "pwd",
			workingDir: newDir,
			stdout:     newDir + "\n",
		}, {
			message:     "test environment",
			commands:    "echo $OMG_IT_WORKS",
			environment: []string{"OMG_IT_WORKS=like magic"},
			stdout:      "like magic\n",
		},
	} {
		c.Logf("%v: %s", i, test.message)

		result, err := exec.RunCommands(
			exec.RunParams{
				Commands:    test.commands,
				WorkingDir:  test.workingDir,
				Environment: test.environment,
			})
		c.Assert(err, gc.IsNil)
		c.Assert(string(result.Stdout), gc.Equals, test.stdout)
		c.Assert(string(result.Stderr), gc.Equals, test.stderr)
		c.Assert(result.Code, gc.Equals, test.code)
	}
}
开发者ID:natefinch,项目名称:utils,代码行数:50,代码来源:exec_test.go


示例13: TestDiscoverInitSystemScriptPosix

func (s *discoverySuite) TestDiscoverInitSystemScriptPosix(c *gc.C) {
	if runtime.GOOS == "windows" {
		c.Skip("not supported on windows")
	}

	script, filename := s.newDiscoverInitSystemScript(c)
	script += "sh " + filename
	response, err := exec.RunCommands(exec.RunParams{
		Commands: script,
	})
	c.Assert(err, jc.ErrorIsNil)

	initSystem, err := service.DiscoverInitSystem()
	c.Assert(err, jc.ErrorIsNil)
	c.Check(response.Code, gc.Equals, 0)
	c.Check(string(response.Stdout), gc.Equals, initSystem)
	c.Check(string(response.Stderr), gc.Equals, "")
}
开发者ID:imoapps,项目名称:juju,代码行数:18,代码来源:discovery_test.go


示例14: ensureLockDirExistsAndUbuntuWritable

// Previously the lock directory was created when the uniter started. This
// allows serialization of all of the hook execution across units running on a
// single machine.  This lock directory is now also used but the juju-run
// command on the host machine.  juju-run also gets a lock on the hook
// execution fslock prior to execution.  However, the lock directory was owned
// by root, and the juju-run process was being executed by the ubuntu user, so
// we need to change the ownership of the lock directory to ubuntu:ubuntu.
// Also we need to make sure that this directory exists on machines with no
// units.
func ensureLockDirExistsAndUbuntuWritable(context Context) error {
	lockDir := path.Join(context.AgentConfig().DataDir(), "locks")
	// We only try to change ownership if there is an ubuntu user
	// defined, and we determine this by the existance of the home dir.
	command := fmt.Sprintf(""+
		"mkdir -p %s\n"+
		"[ -e %s ] && chown ubuntu:ubuntu %s\n",
		lockDir, ubuntuHome, lockDir)
	logger.Tracef("command: %s", command)
	result, err := exec.RunCommands(exec.RunParams{
		Commands: command,
	})
	if err != nil {
		return err
	}
	logger.Tracef("stdout: %s", result.Stdout)
	return nil
}
开发者ID:jiasir,项目名称:juju,代码行数:27,代码来源:lockdirectory.go


示例15: ensureUbuntuDotProfileSourcesProxyFile

// As of the middle of the 1.17 cycle, the proxy settings are written out to
// /home/ubuntu/.juju-proxy both by cloud-init and the machine environ worker.
// An older version of juju that has been upgraded will get the proxy settings
// written out to the .juju-proxy file, but the .profile for the ubuntu user
// wouldn't have been updated to source this file.
//
// This upgrade step is to add the line to source the file if it is missing
// from the file.
func ensureUbuntuDotProfileSourcesProxyFile(context Context) error {
	// We look to see if the proxy line is there already as the manual
	// provider may have had it aleady. The ubuntu user may not exist
	// (local provider only).
	command := fmt.Sprintf(""+
		`([ ! -e %s/.profile ] || grep -q '.juju-proxy' %s/.profile) || `+
		`printf '\n# Added by juju\n[ -f "$HOME/.juju-proxy" ] && . "$HOME/.juju-proxy"\n' >> %s/.profile`,
		ubuntuHome, ubuntuHome, ubuntuHome)
	logger.Tracef("command: %s", command)
	result, err := exec.RunCommands(exec.RunParams{
		Commands: command,
	})
	if err != nil {
		return err
	}
	logger.Tracef("stdout: %s", result.Stdout)
	return nil
}
开发者ID:kapilt,项目名称:juju,代码行数:26,代码来源:dotprofile.go


示例16: executeCommands

// executeCommands execute a batch of commands one by one.
func executeCommands(commands []string) error {
	for _, command := range commands {
		result, err := exec.RunCommands(exec.RunParams{
			Commands:   command,
			WorkingDir: "/",
		})
		if err != nil {
			return fmt.Errorf("failed to execute %q: %v", command, err)
		}
		if result.Code != 0 {
			return fmt.Errorf(
				"command %q failed (code: %d, stdout: %s, stderr: %s)",
				command, result.Code, result.Stdout, result.Stderr)
		}
		logger.Debugf("command %q (code: %d, stdout: %s, stderr: %s)",
			command, result.Code, result.Stdout, result.Stderr)
	}
	return nil
}
开发者ID:imoapps,项目名称:juju,代码行数:20,代码来源:utils.go


示例17: executeNoContext

func (c *RunCommand) executeNoContext() (*exec.ExecResponse, error) {
	// Acquire the uniter hook execution lock to make sure we don't
	// stomp on each other.
	lock, err := cmdutil.HookExecutionLock(cmdutil.DataDir)
	if err != nil {
		return nil, errors.Trace(err)
	}
	err = lock.Lock("juju-run")
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer lock.Unlock()

	runCmd := c.appendProxyToCommands()

	return exec.RunCommands(
		exec.RunParams{
			Commands: runCmd,
		})
}
开发者ID:imoapps,项目名称:juju,代码行数:20,代码来源:run.go


示例18: runScript

func (s *bridgeConfigSuite) runScript(c *gc.C, configFile, bridgePrefix, bridgeName, interfaceToBridge string) (output string, exitCode int) {
	if bridgePrefix != "" {
		bridgePrefix = fmt.Sprintf("--bridge-prefix=%q", bridgePrefix)
	}

	if bridgeName != "" {
		bridgeName = fmt.Sprintf("--bridge-name=%q", bridgeName)
	}

	if interfaceToBridge != "" {
		interfaceToBridge = fmt.Sprintf("--interface-to-bridge=%q", interfaceToBridge)
	}

	script := fmt.Sprintf("%q %s %s %s %q\n", s.testPythonScript, bridgePrefix, bridgeName, interfaceToBridge, configFile)
	result, err := exec.RunCommands(exec.RunParams{Commands: script})
	c.Assert(err, jc.ErrorIsNil, gc.Commentf("script failed unexpectedly"))
	stdout := string(result.Stdout)
	stderr := string(result.Stderr)
	if stderr != "" {
		return stdout + "\n" + stderr, result.Code
	}
	return stdout, result.Code
}
开发者ID:OSBI,项目名称:juju,代码行数:23,代码来源:bridgescript_test.go


示例19: runTemplateCommand

// runTemplateCommand executes the given template with the given data,
// which generates a command to execute. If exitNonZeroOK is true, no
// error is returned if the exit code is not 0, otherwise an error is
// returned.
func runTemplateCommand(t *template.Template, exitNonZeroOK bool, data interface{}) (
	exitCode int, err error,
) {
	// Clone the template to ensure the original won't be changed.
	cloned, err := t.Clone()
	if err != nil {
		return -1, errors.Annotatef(err, "cannot clone command template %q", t.Name())
	}
	var buf bytes.Buffer
	if err := cloned.Execute(&buf, data); err != nil {
		return -1, errors.Annotatef(err, "cannot execute command template %q", t.Name())
	}
	command := buf.String()
	logger.Debugf("running command %q", command)
	result, err := exec.RunCommands(exec.RunParams{Commands: command})
	if err != nil {
		return -1, errors.Annotatef(err, "cannot run command %q", command)
	}
	exitCode = result.Code
	stdout := string(result.Stdout)
	stderr := string(result.Stderr)
	logger.Debugf(
		"command %q returned code=%d, stdout=%q, stderr=%q",
		command, exitCode, stdout, stderr,
	)
	if exitCode != 0 {
		if exitNonZeroOK {
			return exitCode, nil
		}
		return exitCode, errors.Errorf(
			"command %q failed with exit code %d",
			command, exitCode,
		)
	}
	return 0, nil
}
开发者ID:makyo,项目名称:juju,代码行数:40,代码来源:lxc-broker.go


示例20: runScript

func (s *bridgeConfigSuite) runScript(c *gc.C, configFile string, nic string, bridge string, isBond bool) (output string, exitCode int) {
	var primaryNicIsBonded = ""

	if isBond {
		primaryNicIsBonded = "--primary-nic-is-bonded"
	}

	script := fmt.Sprintf("%s\npython -c %q --render-only --filename=%q --primary-nic=%q --bridge-name=%q %s\n",
		bridgeScriptPythonBashDef,
		"$python_script",
		configFile,
		nic,
		bridge,
		primaryNicIsBonded)

	result, err := exec.RunCommands(exec.RunParams{Commands: script})
	c.Assert(err, jc.ErrorIsNil, gc.Commentf("script failed unexpectedly"))
	stdout := string(result.Stdout)
	stderr := string(result.Stderr)
	if stderr != "" {
		return stdout + "\n" + stderr, result.Code
	}
	return stdout, result.Code
}
开发者ID:imoapps,项目名称:juju,代码行数:24,代码来源:bridgescript_test.go



注:本文中的github.com/juju/utils/exec.RunCommands函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang exec.ExecResponse类代码示例发布时间:2022-05-23
下一篇:
Golang clock.Clock类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap