示例#1
0
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
}
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
0
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
0
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
}
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

		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)
	}

	return err
}
示例#6
0
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
0
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

	var stdoutBuff bytes.Buffer
	var stderrBuff bytes.Buffer
	var cmd packer.RemoteCmd
	cmd.Stdout = &stdoutBuff
	cmd.Stderr = &stderrBuff

	cmd.Command = runScript

	ui.Message("Starting provisioning. It may take some time...")
	err = comm.Start(&cmd)
	if err != nil {
		err = fmt.Errorf(errorMsg, "comm.Start", err.Error())
		return err
	}

	ui.Message("Provision is Completed")

	stderrString := stderrBuff.String()
	if len(stderrString) > 0 {
		err = fmt.Errorf(errorMsg, "stderrString", stderrString)
		log.Printf("Provision stderr: %s", stderrString)
	}

	ui.Say("Script output")

	stdoutString := stdoutBuff.String()
	if len(stdoutString) > 0 {
		log.Printf("Provision stdout: %s", stdoutString)
		scriptMessages := strings.Split(stdoutString, "\\n")
		for _, m := range scriptMessages {
			ui.Message(m)
		}
	}

	return err
}