本文整理匯總了Golang中github.com/mitchellh/packer/packer.Communicator.UploadDir方法的典型用法代碼示例。如果您正苦於以下問題:Golang Communicator.UploadDir方法的具體用法?Golang Communicator.UploadDir怎麽用?Golang Communicator.UploadDir使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/mitchellh/packer/packer.Communicator
的用法示例。
在下文中一共展示了Communicator.UploadDir方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ProvisionUpload
func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) error {
ui.Say(fmt.Sprintf("Uploading %s => %s", p.config.Source, p.config.Destination))
info, err := os.Stat(p.config.Source)
if err != nil {
return err
}
// If we're uploading a directory, short circuit and do that
if info.IsDir() {
return comm.UploadDir(p.config.Destination, p.config.Source, nil)
}
// We're uploading a file...
f, err := os.Open(p.config.Source)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
err = comm.Upload(p.config.Destination, f, &fi)
if err != nil {
ui.Error(fmt.Sprintf("Upload failed: %s", err))
}
return err
}
示例2: ProvisionUpload
func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) error {
ui.Say(fmt.Sprintf("Uploading %s => %s", p.config.Source, p.config.Destination))
info, _ := os.Stat(p.config.Source)
if info != nil {
// If we're uploading a directory, short circuit and do that
if info.IsDir() {
return comm.UploadDir(p.config.Destination, p.config.Source, nil)
}
}
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("Couldn't get the current working directory")
}
det, err := gg.Detect(p.config.Source, pwd, gg.Detectors)
if err != nil {
return fmt.Errorf("Couldn't detect file source type: %v", err)
}
if len(det) == 0 {
return errors.New("Don't recognise the source type")
}
dir, err := ioutil.TempDir("", "packer")
if err != nil {
return errors.New("Unable to create temp dir")
}
defer os.RemoveAll(dir)
source := filepath.Join(dir, filepath.Base(p.config.Source))
if err := gg.GetFile(source, p.config.Source); err != nil {
return fmt.Errorf("There was a problem getting the file: %v", err)
}
// We're uploading a file...
f, err := os.Open(source)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
err = comm.Upload(p.config.Destination, f, &fi)
if err != nil {
ui.Error(fmt.Sprintf("Upload failed: %s", err))
}
return err
}
示例3: uploadDir
func (p *Provisioner) uploadDir(ui packer.Ui, comm packer.Communicator, dst, src string, ignore []string) error {
if err := p.createDir(ui, comm, dst); err != nil {
return err
}
// Make sure there is a trailing "/" so that the directory isn't
// created on the other side.
if src[len(src)-1] != '/' {
src = src + "/"
}
return comm.UploadDir(dst, src, ignore)
}
示例4: Provision
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
var err error
errorMsg := "Provision error: %s"
ui.Say("Provision...")
if len(p.config.DistrSrcPath) != 0 {
err = comm.UploadDir(p.config.DistrDstPath, p.config.DistrSrcPath, nil)
if err != nil {
return err
}
}
if p.config.Inline != nil {
var blockBuffer bytes.Buffer
blockBuffer.WriteString("{")
for _, command := range p.config.Inline {
blockBuffer.WriteString(command + ";")
}
blockBuffer.WriteString("}")
var stdoutBuff bytes.Buffer
var stderrBuff bytes.Buffer
var cmd packer.RemoteCmd
cmd.Stdout = &stdoutBuff
cmd.Stderr = &stderrBuff
cmd.Command = "-ScriptBlock " + blockBuffer.String()
err = comm.Start(&cmd)
if err != nil {
err = fmt.Errorf(errorMsg, err)
}
stderrString := stderrBuff.String()
if len(stderrString) > 0 {
err = fmt.Errorf(errorMsg, stderrString)
log.Printf("Provision Inline stderr: %s", stderrString)
}
stdoutString := stdoutBuff.String()
if len(stdoutString) > 0 {
log.Printf("Provision Inline stdout: %s", stdoutString)
ui.Message(stdoutString)
}
}
if len(p.config.ScriptPath) != 0 {
var stdoutBuff bytes.Buffer
var stderrBuff bytes.Buffer
var cmd packer.RemoteCmd
cmd.Stdout = &stdoutBuff
cmd.Stderr = &stderrBuff
cmd.Command = "-filepath " + filepath.FromSlash(p.config.ScriptPath)
err = comm.Start(&cmd)
if err != nil {
err = fmt.Errorf(errorMsg, err)
}
stderrString := stderrBuff.String()
if len(stderrString) > 0 {
err = fmt.Errorf(errorMsg, stderrString)
log.Printf("Provision from file stderr: %s", stderrString)
}
stdoutString := stdoutBuff.String()
if len(stdoutString) > 0 {
log.Printf("Provision from file stdout: %s", stdoutString)
ui.Message(stdoutString)
}
}
return err
}
示例5: Provision
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
var err error
errorMsg := "Provision error: %s"
if len(p.config.DistrSrcPath) != 0 {
err = comm.UploadDir(p.config.DistrDstPath, p.config.DistrSrcPath, nil)
if err != nil {
return err
}
}
// check the remote connection is ready
{
var cmd packer.RemoteCmd
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
magicWord := "ready"
var blockBuffer bytes.Buffer
blockBuffer.WriteString("{ Write-Host '" + magicWord + "' }")
cmd.Command = "-ScriptBlock " + blockBuffer.String()
cmd.Stdout = stdout
cmd.Stderr = stderr
count := 5
var duration time.Duration = 1
sleepTime := time.Minute * duration
ui.Say("Checking PS remouting is ready...")
for count > 0 {
err = comm.Start(&cmd)
if err != nil {
return err
}
stderrString := strings.TrimSpace(stderr.String())
stdoutString := strings.TrimSpace(stdout.String())
log.Printf("stdout: %s", stdoutString)
log.Printf("stderr: %s", stderrString)
if stdoutString == magicWord {
break
}
log.Println(fmt.Sprintf("Waiting %v minutes for the remote connection to get ready...", uint(duration)))
time.Sleep(sleepTime)
count--
}
if count == 0 {
err := fmt.Errorf(errorMsg, "Remote connection failed")
return err
}
}
if p.config.Inline != nil {
var cmd packer.RemoteCmd
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
var blockBuffer bytes.Buffer
blockBuffer.WriteString("{")
for _, command := range p.config.Inline {
blockBuffer.WriteString(command + ";")
}
blockBuffer.WriteString("}")
cmd.Command = "-ScriptBlock " + blockBuffer.String()
cmd.Stdout = stdout
cmd.Stderr = stderr
err = comm.Start(&cmd)
stderrString := strings.TrimSpace(stderr.String())
stdoutString := strings.TrimSpace(stdout.String())
log.Printf("stdout: %s", stdoutString)
log.Printf("stderr: %s", stderrString)
if len(stderrString) > 0 {
err = fmt.Errorf("Provision error: %s", stderrString)
}
ui.Say(stdoutString)
}
if len(p.config.ScriptPath) != 0 {
var cmd packer.RemoteCmd
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd.Command = "-filepath " + filepath.FromSlash(p.config.ScriptPath)
cmd.Stdout = stdout
cmd.Stderr = stderr
//.........這裏部分代碼省略.........
示例6: Provision
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
var err error
ui.Say("Provisioning with Salt...")
if !p.config.SkipBootstrap {
cmd := &packer.RemoteCmd{
Command: fmt.Sprintf("wget -O - http://bootstrap.saltstack.org | sudo sh -s %s", p.config.BootstrapArgs),
}
ui.Message(fmt.Sprintf("Installing Salt with command %s", cmd))
if err = cmd.StartWithUi(comm, ui); err != nil {
return fmt.Errorf("Unable to install Salt: %d", err)
}
}
ui.Message(fmt.Sprintf("Creating remote directory: %s", p.config.TempConfigDir))
cmd := &packer.RemoteCmd{Command: fmt.Sprintf("mkdir -p %s", p.config.TempConfigDir)}
if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 {
if err == nil {
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus)
}
return fmt.Errorf("Error creating remote salt state directory: %s", err)
}
if p.config.MinionConfig != "" {
ui.Message(fmt.Sprintf("Uploading minion config: %s", p.config.MinionConfig))
if err = uploadMinionConfig(comm, fmt.Sprintf("%s/minion", p.config.TempConfigDir), p.config.MinionConfig); err != nil {
return fmt.Errorf("Error uploading local minion config file to remote: %s", err)
}
ui.Message(fmt.Sprintf("Moving %s/minion to /etc/salt/minion", p.config.TempConfigDir))
cmd = &packer.RemoteCmd{Command: fmt.Sprintf("sudo mv %s/minion /etc/salt/minion", p.config.TempConfigDir)}
if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 {
if err == nil {
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus)
}
return fmt.Errorf("Unable to move %s/minion to /etc/salt/minion: %d", p.config.TempConfigDir, err)
}
}
ui.Message(fmt.Sprintf("Uploading local state tree: %s", p.config.LocalStateTree))
if err = comm.UploadDir(fmt.Sprintf("%s/states", p.config.TempConfigDir),
p.config.LocalStateTree, []string{".git"}); err != nil {
return fmt.Errorf("Error uploading local state tree to remote: %s", err)
}
ui.Message(fmt.Sprintf("Moving %s/states to /srv/salt", p.config.TempConfigDir))
cmd = &packer.RemoteCmd{Command: fmt.Sprintf("sudo mv %s/states /srv/salt", p.config.TempConfigDir)}
if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 {
if err == nil {
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus)
}
return fmt.Errorf("Unable to move %s/states to /srv/salt: %d", p.config.TempConfigDir, err)
}
if p.config.LocalPillarRoots != "" {
ui.Message(fmt.Sprintf("Uploading local pillar roots: %s", p.config.LocalPillarRoots))
if err = comm.UploadDir(fmt.Sprintf("%s/pillar", p.config.TempConfigDir),
p.config.LocalPillarRoots, []string{".git"}); err != nil {
return fmt.Errorf("Error uploading local pillar roots to remote: %s", err)
}
ui.Message(fmt.Sprintf("Moving %s/pillar to /srv/pillar", p.config.TempConfigDir))
cmd = &packer.RemoteCmd{Command: fmt.Sprintf("sudo mv %s/pillar /srv/pillar", p.config.TempConfigDir)}
if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 {
if err == nil {
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus)
}
return fmt.Errorf("Unable to move %s/pillar to /srv/pillar: %d", p.config.TempConfigDir, err)
}
}
ui.Message("Running highstate")
cmd = &packer.RemoteCmd{Command: "sudo salt-call --local state.highstate -l info"}
if err = cmd.StartWithUi(comm, ui); err != nil || cmd.ExitStatus != 0 {
if err == nil {
err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus)
}
return fmt.Errorf("Error executing highstate: %s", err)
}
return nil
}
示例7: Provision
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
var err error
errorMsg := "Error preparing shell script, %s error: %s"
ui.Say("Provisioning...")
if len(p.config.DistrSrcPath) != 0 {
err = comm.UploadDir("skiped param", p.config.DistrSrcPath, nil)
if err != nil {
return err
}
}
tempDir := os.TempDir()
packerTempDir, err := ioutil.TempDir(tempDir, "packer_script")
if err != nil {
err := fmt.Errorf("Error creating temporary directory: %s", err.Error())
return err
}
// create a temporary script file and upload it to Azure storage
ui.Message("Preparing execution script...")
provisionFileName := fmt.Sprintf("provision-%s.ps1", uuid.New())
scriptPath := filepath.Join(packerTempDir, provisionFileName)
tf, err := os.Create(scriptPath)
if err != nil {
return fmt.Errorf(errorMsg, "os.Create", err.Error())
}
defer os.RemoveAll(packerTempDir)
writer := bufio.NewWriter(tf)
if p.config.Inline != nil {
log.Println("Writing inline commands to execution script...")
// Write our contents to it
for _, command := range p.config.Inline {
log.Println(command)
if _, err := writer.WriteString(command + ";\n"); err != nil {
return fmt.Errorf(errorMsg, "writer.WriteString", err.Error())
}
}
}
// add content to the temp script file
if len(p.config.ScriptPath) != 0 {
log.Println("Writing file commands to execution script...")
f, err := os.Open(p.config.ScriptPath)
if err != nil {
return fmt.Errorf(errorMsg, "os.Open", err.Error())
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
log.Println(scanner.Text())
if _, err := writer.WriteString(scanner.Text() + "\n"); err != nil {
return fmt.Errorf(errorMsg, "writer.WriteString", err.Error())
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf(errorMsg, "scanner.Scan", err.Error())
}
}
log.Println("Writing SysPrep to execution script...")
sysprepPs := []string{
"Write-Host 'Executing Sysprep from File...'",
"Start-Process $env:windir\\System32\\Sysprep\\sysprep.exe -NoNewWindow -Wait -Argument '/quiet /generalize /oobe /quit'",
"Write-Host 'Sysprep is done!'",
}
for _, command := range sysprepPs {
log.Println(command)
if _, err := writer.WriteString(command + ";\n"); err != nil {
return fmt.Errorf(errorMsg, "writer.WriteString", err.Error())
}
}
if err := writer.Flush(); err != nil {
return fmt.Errorf("Error preparing shell script: %s", err.Error())
}
tf.Close()
// upload to Azure storage
ui.Message("Uploading execution script...")
err = comm.UploadDir("skiped param", scriptPath, nil)
if err != nil {
return err
}
// execute script with Custom script extension
runScript := provisionFileName
//.........這裏部分代碼省略.........