本文整理汇总了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
}
示例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
}