Пример #1
0
func (s *StepSetRemoting) Run(state multistep.StateBag) multistep.StepAction {
	driver := state.Get("driver").(Driver)
	ui := state.Get("ui").(packer.Ui)

	errorMsg := "Error StepRemoteSession: %s"
	vmName := state.Get("vmName").(string)
	ip := state.Get("ip").(string)

	ui.Say("Adding to TrustedHosts (require elevated mode)")

	var blockBuffer bytes.Buffer
	blockBuffer.WriteString("Invoke-Command -scriptblock { Set-Item -path WSMan:\\localhost\\Client\\TrustedHosts '")
	blockBuffer.WriteString(ip)
	blockBuffer.WriteString("' -Force }")

	var err error
	err = driver.HypervManage(blockBuffer.String())

	if err != nil {
		err := fmt.Errorf(errorMsg, err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	comm, err := powershell.New(
		&powershell.Config{
			Username:     "******",
			Password:     "******",
			RemoteHostIP: ip,
			VmName:       vmName,
			Ui:           ui,
		})

	if err != nil {
		err := fmt.Errorf(errorMsg, err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	packerCommunicator := packer.Communicator(comm)

	s.comm = packerCommunicator
	state.Put("communicator", packerCommunicator)

	return multistep.ActionContinue
}
func (s *StepSetProvisionInfrastructure) Run(state multistep.StateBag) multistep.StepAction {
	ui := state.Get("ui").(packer.Ui)
	client := state.Get(constants.RequestManager).(management.Client)
	config := state.Get(constants.Config).(*Config)

	errorMsg := "Error StepSetProvisionInfrastructure: %s"
	ui.Say("Preparing infrastructure for provision...")

	// get key for storage account
	ui.Message("Looking up storage account...")

	//create temporary container
	s.flagTempContainerCreated = false

	ui.Message("Creating Azure temporary container...")
	err := config.storageClient.GetBlobService().CreateContainer(s.TempContainerName, storage.ContainerAccessTypePrivate)
	if err != nil {
		err := fmt.Errorf(errorMsg, err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	s.flagTempContainerCreated = true

	comm := azureVmCustomScriptExtension.New(
		azureVmCustomScriptExtension.Config{
			ServiceName:               s.ServiceName,
			VmName:                    s.VmName,
			StorageAccountName:        s.StorageAccountName,
			StorageAccountKey:         config.storageAccountKey,
			BlobClient:                config.storageClient.GetBlobService(),
			ContainerName:             s.TempContainerName,
			Ui:                        ui,
			ManagementClient:          client,
			ProvisionTimeoutInMinutes: s.ProvisionTimeoutInMinutes,
		})

	packerCommunicator := packer.Communicator(comm)

	state.Put("communicator", packerCommunicator)

	return multistep.ActionContinue
}
func (s *StepSetProvisionInfrastructure) Run(state multistep.StateBag) multistep.StepAction {
	ui := state.Get("ui").(packer.Ui)
	client := state.Get(constants.RequestManager).(management.Client)

	errorMsg := "Error StepRemoteSession: %s"
	ui.Say("Preparing infrastructure for provision...")

	// get key for storage account
	ui.Message("Getting key for storage account...")

	keys, err := storageservice.NewClient(client).GetStorageServiceKeys(s.StorageAccountName)
	if err != nil {
		err := fmt.Errorf(errorMsg, err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	//create storage driver
	s.storageClient, err = storage.NewBasicClient(s.StorageAccountName, keys.PrimaryKey)
	if err != nil {
		err := fmt.Errorf(errorMsg, err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	//create temporary container
	s.flagTempContainerCreated = false

	ui.Message("Creating Azure temporary container...")
	err = s.storageClient.GetBlobService().CreateContainer(s.TempContainerName, storage.ContainerAccessTypePrivate)
	if err != nil {
		err := fmt.Errorf(errorMsg, err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	s.flagTempContainerCreated = true

	isOSImage := state.Get(constants.IsOSImage).(bool)

	comm, err := azureVmCustomScriptExtension.New(
		azureVmCustomScriptExtension.Config{
			ServiceName:        s.ServiceName,
			VmName:             s.VmName,
			StorageAccountName: s.StorageAccountName,
			StorageAccountKey:  keys.PrimaryKey,
			ContainerName:      s.TempContainerName,
			Ui:                 ui,
			IsOSImage:          isOSImage,
			ManagementClient:   client,
		})

	if err != nil {
		err := fmt.Errorf(errorMsg, err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	packerCommunicator := packer.Communicator(comm)

	state.Put("communicator", packerCommunicator)

	return multistep.ActionContinue
}