github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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  	ui.Say(s.Message)
    40  	log.Printf("Remote uploading: %s", path)
    41  	newPath, err := remote.UploadISO(path, checksum, checksumType)
    42  	if err != nil {
    43  		err := fmt.Errorf("Error uploading file: %s", err)
    44  		state.Put("error", err)
    45  		ui.Error(err.Error())
    46  		return multistep.ActionHalt
    47  	}
    48  
    49  	state.Put(s.Key, newPath)
    50  	return multistep.ActionContinue
    51  }
    52  
    53  func (s *stepRemoteUpload) Cleanup(state multistep.StateBag) {
    54  	if !s.DoCleanup {
    55  		return
    56  	}
    57  
    58  	driver := state.Get("driver").(vmwcommon.Driver)
    59  
    60  	remote, ok := driver.(RemoteDriver)
    61  	if !ok {
    62  		return
    63  	}
    64  
    65  	path, ok := state.Get(s.Key).(string)
    66  	if !ok {
    67  		return
    68  	}
    69  
    70  	log.Printf("Cleaning up remote path: %s", path)
    71  	err := remote.RemoveCache(path)
    72  	if err != nil {
    73  		log.Printf("Error cleaning up: %s", err)
    74  	}
    75  }