当前位置: 首页>>代码示例>>Golang>>正文


Golang Communicator.UploadDir方法代码示例

本文整理汇总了Golang中github.com/hashicorp/terraform/communicator.Communicator.UploadDir方法的典型用法代码示例。如果您正苦于以下问题:Golang Communicator.UploadDir方法的具体用法?Golang Communicator.UploadDir怎么用?Golang Communicator.UploadDir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/hashicorp/terraform/communicator.Communicator的用法示例。


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

示例1: copyFiles

// copyFiles is used to copy the files from a source to a destination
func (p *ResourceProvisioner) copyFiles(comm communicator.Communicator, src, dst string) error {
	// Wait and retry until we establish the connection
	err := retryFunc(comm.Timeout(), func() error {
		err := comm.Connect(nil)
		return err
	})
	if err != nil {
		return err
	}
	defer comm.Disconnect()

	info, err := os.Stat(src)
	if err != nil {
		return err
	}

	// If we're uploading a directory, short circuit and do that
	if info.IsDir() {
		if err := comm.UploadDir(dst, src); err != nil {
			return fmt.Errorf("Upload failed: %v", err)
		}
		return nil
	}

	// We're uploading a file...
	f, err := os.Open(src)
	if err != nil {
		return err
	}
	defer f.Close()

	err = comm.Upload(dst, f)
	if err != nil {
		return fmt.Errorf("Upload failed: %v", err)
	}
	return err
}
开发者ID:Originate,项目名称:terraform,代码行数:38,代码来源:resource_provisioner.go

示例2: Run

func (p *Provisioner) Run(o terraform.UIOutput, comm communicator.Communicator) error {
	// parse the playbook path and ensure that it is valid before doing
	// anything else. This is done in validate but is repeated here, just
	// in case.
	playbookPath, err := p.resolvePath(p.Playbook)
	if err != nil {
		return err
	}

	// commands that are needed to setup a basic environment to run the `ansible-local.py` script
	// TODO pivot based upon different platforms and allow optional python provision steps
	// TODO this should be configurable for folks who want to customize this
	provisionAnsibleCommands := []string{
		// https://github.com/hashicorp/terraform/issues/1025
		// cloud-init runs on fresh sources and can interfere with apt-get update commands causing intermittent failures
		"/bin/bash -c 'until [[ -f /var/lib/cloud/instance/boot-finished ]]; do sleep 1; done'",
		"apt-get update",
		"apt-get install -y build-essential python-dev",
		"curl https://bootstrap.pypa.io/get-pip.py | sudo python",
		"pip install ansible",
	}

	for _, command := range provisionAnsibleCommands {
		o.Output(fmt.Sprintf("running command: %s", command))
		err := p.runCommand(o, comm, command)
		if err != nil {
			return err
		}
	}

	// ansible projects are structured such that the playbook file is in
	// the top level of the module path. As such, we parse the playbook
	// path's directory and upload the entire thing
	playbookDir := filepath.Dir(playbookPath)

	// the host playbook path is the path on the host where the playbook
	// will be uploaded too
	remotePlaybookPath := filepath.Join("/tmp/ansible", filepath.Base(playbookPath))

	// upload ansible source and playbook to the host
	if err := comm.UploadDir("/tmp/ansible", playbookDir); err != nil {
		return err
	}

	extraVars, err := json.Marshal(p.ExtraVars)
	if err != nil {
		return err
	}

	// build a command to run ansible on the host machine
	command := fmt.Sprintf("curl %s | python - --playbook=%s --hosts=%s --plays=%s --groups=%s --extra-vars=%s",
		p.ansibleLocalScript,
		remotePlaybookPath,
		strings.Join(p.Hosts, ","),
		strings.Join(p.Plays, ","),
		strings.Join(p.Groups, ","),
		string(extraVars))

	o.Output(fmt.Sprintf("running command: %s", command))
	if err := p.runCommand(o, comm, command); err != nil {
		return err
	}

	return nil
}
开发者ID:jonmorehouse,项目名称:terraform-provisioner-ansible,代码行数:65,代码来源:provisioner.go


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