github.com/mitchellh/packer@v1.3.2/builder/vmware/iso/step_remote_upload.go (about)

     1  package iso
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  
     8  	vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  // stepRemoteUpload uploads some thing from the state bag to a remote driver
    14  // (if it can) and stores that new remote path into the state bag.
    15  type stepRemoteUpload struct {
    16  	Key       string
    17  	Message   string
    18  	DoCleanup bool
    19  }
    20  
    21  func (s *stepRemoteUpload) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    22  	driver := state.Get("driver").(vmwcommon.Driver)
    23  	ui := state.Get("ui").(packer.Ui)
    24  
    25  	remote, ok := driver.(RemoteDriver)
    26  	if !ok {
    27  		return multistep.ActionContinue
    28  	}
    29  
    30  	path, ok := state.Get(s.Key).(string)
    31  	if !ok {
    32  		return multistep.ActionContinue
    33  	}
    34  
    35  	config := state.Get("config").(*Config)
    36  	checksum := config.ISOChecksum
    37  	checksumType := config.ISOChecksumType
    38  
    39  	if esx5, ok := remote.(*ESX5Driver); ok {
    40  		remotePath := esx5.cachePath(path)
    41  
    42  		if esx5.verifyChecksum(checksumType, checksum, remotePath) {
    43  			ui.Say("Remote cache was verified skipping remote upload...")
    44  			state.Put(s.Key, remotePath)
    45  			return multistep.ActionContinue
    46  		}
    47  
    48  	}
    49  
    50  	ui.Say(s.Message)
    51  	log.Printf("Remote uploading: %s", path)
    52  	newPath, err := remote.UploadISO(path, checksum, checksumType)
    53  	if err != nil {
    54  		err := fmt.Errorf("Error uploading file: %s", err)
    55  		state.Put("error", err)
    56  		ui.Error(err.Error())
    57  		return multistep.ActionHalt
    58  	}
    59  	state.Put(s.Key, newPath)
    60  
    61  	return multistep.ActionContinue
    62  }
    63  
    64  func (s *stepRemoteUpload) Cleanup(state multistep.StateBag) {
    65  	if !s.DoCleanup {
    66  		return
    67  	}
    68  
    69  	driver := state.Get("driver").(vmwcommon.Driver)
    70  
    71  	remote, ok := driver.(RemoteDriver)
    72  	if !ok {
    73  		return
    74  	}
    75  
    76  	path, ok := state.Get(s.Key).(string)
    77  	if !ok {
    78  		return
    79  	}
    80  
    81  	log.Printf("Cleaning up remote path: %s", path)
    82  	err := remote.RemoveCache(path)
    83  	if err != nil {
    84  		log.Printf("Error cleaning up: %s", err)
    85  	}
    86  }