github.com/hashicorp/packer@v1.14.3/command/execute.go (about)

     1  //
     2  // This file is automatically generated by scripts/generate-plugins.go -- Do not edit!
     3  //
     4  
     5  package command
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"regexp"
    11  	"strings"
    12  
    13  	packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
    14  	"github.com/hashicorp/packer-plugin-sdk/plugin"
    15  
    16  	filebuilder "github.com/hashicorp/packer/builder/file"
    17  	nullbuilder "github.com/hashicorp/packer/builder/null"
    18  	hcppackerartifactdatasource "github.com/hashicorp/packer/datasource/hcp-packer-artifact"
    19  	hcppackerimagedatasource "github.com/hashicorp/packer/datasource/hcp-packer-image"
    20  	hcppackeriterationdatasource "github.com/hashicorp/packer/datasource/hcp-packer-iteration"
    21  	hcppackerversiondatasource "github.com/hashicorp/packer/datasource/hcp-packer-version"
    22  	httpdatasource "github.com/hashicorp/packer/datasource/http"
    23  	nulldatasource "github.com/hashicorp/packer/datasource/null"
    24  	artificepostprocessor "github.com/hashicorp/packer/post-processor/artifice"
    25  	checksumpostprocessor "github.com/hashicorp/packer/post-processor/checksum"
    26  	compresspostprocessor "github.com/hashicorp/packer/post-processor/compress"
    27  	manifestpostprocessor "github.com/hashicorp/packer/post-processor/manifest"
    28  	shelllocalpostprocessor "github.com/hashicorp/packer/post-processor/shell-local"
    29  	breakpointprovisioner "github.com/hashicorp/packer/provisioner/breakpoint"
    30  	fileprovisioner "github.com/hashicorp/packer/provisioner/file"
    31  	hcpsbomprovisioner "github.com/hashicorp/packer/provisioner/hcp-sbom"
    32  	powershellprovisioner "github.com/hashicorp/packer/provisioner/powershell"
    33  	shellprovisioner "github.com/hashicorp/packer/provisioner/shell"
    34  	shelllocalprovisioner "github.com/hashicorp/packer/provisioner/shell-local"
    35  	sleepprovisioner "github.com/hashicorp/packer/provisioner/sleep"
    36  	windowsrestartprovisioner "github.com/hashicorp/packer/provisioner/windows-restart"
    37  	windowsshellprovisioner "github.com/hashicorp/packer/provisioner/windows-shell"
    38  )
    39  
    40  type ExecuteCommand struct {
    41  	Meta
    42  }
    43  
    44  var Builders = map[string]packersdk.Builder{
    45  	"file": new(filebuilder.Builder),
    46  	"null": new(nullbuilder.Builder),
    47  }
    48  
    49  var Provisioners = map[string]packersdk.Provisioner{
    50  	"breakpoint":      new(breakpointprovisioner.Provisioner),
    51  	"file":            new(fileprovisioner.Provisioner),
    52  	"hcp-sbom":        new(hcpsbomprovisioner.Provisioner),
    53  	"powershell":      new(powershellprovisioner.Provisioner),
    54  	"shell":           new(shellprovisioner.Provisioner),
    55  	"shell-local":     new(shelllocalprovisioner.Provisioner),
    56  	"sleep":           new(sleepprovisioner.Provisioner),
    57  	"windows-restart": new(windowsrestartprovisioner.Provisioner),
    58  	"windows-shell":   new(windowsshellprovisioner.Provisioner),
    59  }
    60  
    61  var PostProcessors = map[string]packersdk.PostProcessor{
    62  	"artifice":    new(artificepostprocessor.PostProcessor),
    63  	"checksum":    new(checksumpostprocessor.PostProcessor),
    64  	"compress":    new(compresspostprocessor.PostProcessor),
    65  	"manifest":    new(manifestpostprocessor.PostProcessor),
    66  	"shell-local": new(shelllocalpostprocessor.PostProcessor),
    67  }
    68  
    69  var Datasources = map[string]packersdk.Datasource{
    70  	"hcp-packer-artifact":  new(hcppackerartifactdatasource.Datasource),
    71  	"hcp-packer-image":     new(hcppackerimagedatasource.Datasource),
    72  	"hcp-packer-iteration": new(hcppackeriterationdatasource.Datasource),
    73  	"hcp-packer-version":   new(hcppackerversiondatasource.Datasource),
    74  	"http":                 new(httpdatasource.Datasource),
    75  	"null":                 new(nulldatasource.Datasource),
    76  }
    77  
    78  var pluginRegexp = regexp.MustCompile("packer-(builder|post-processor|provisioner|datasource)-(.+)")
    79  
    80  type ExecuteArgs struct {
    81  	UseProtobuf bool
    82  	CommandType string
    83  }
    84  
    85  func (ea *ExecuteArgs) AddFlagSets(flags *flag.FlagSet) {
    86  	flags.BoolVar(&ea.UseProtobuf, "protobuf", false, "Use protobuf for serialising data over the wire instead of gob")
    87  }
    88  
    89  func (c *ExecuteCommand) ParseArgs(args []string) (*ExecuteArgs, int) {
    90  	var cfg ExecuteArgs
    91  	flags := c.Meta.FlagSet("")
    92  	flags.Usage = func() { c.Ui.Say(c.Help()) }
    93  	cfg.AddFlagSets(flags)
    94  	if err := flags.Parse(args); err != nil {
    95  		return &cfg, 1
    96  	}
    97  
    98  	args = flags.Args()
    99  	if len(args) != 1 {
   100  		flags.Usage()
   101  		return &cfg, 1
   102  	}
   103  	cfg.CommandType = args[0]
   104  	return &cfg, 0
   105  }
   106  
   107  func (c *ExecuteCommand) Run(args []string) int {
   108  	cfg, ret := c.ParseArgs(args)
   109  	if ret != 0 {
   110  		return ret
   111  	}
   112  
   113  	return c.RunContext(cfg)
   114  }
   115  
   116  func (c *ExecuteCommand) RunContext(args *ExecuteArgs) int {
   117  	// Plugin will match something like "packer-builder-amazon-ebs"
   118  	parts := pluginRegexp.FindStringSubmatch(args.CommandType)
   119  	if len(parts) != 3 {
   120  		c.Ui.Error(c.Help())
   121  		return 1
   122  	}
   123  	pluginType := parts[1] // capture group 1 (builder|post-processor|provisioner)
   124  	pluginName := parts[2] // capture group 2 (.+)
   125  
   126  	server, err := plugin.Server()
   127  	if err != nil {
   128  		c.Ui.Error(fmt.Sprintf("Error starting plugin server: %s", err))
   129  		return 1
   130  	}
   131  
   132  	if args.UseProtobuf {
   133  		server.UseProto = true
   134  	}
   135  
   136  	switch pluginType {
   137  	case "builder":
   138  		builder, found := Builders[pluginName]
   139  		if !found {
   140  			c.Ui.Error(fmt.Sprintf("Could not load builder: %s", pluginName))
   141  			return 1
   142  		}
   143  		server.RegisterBuilder(builder)
   144  	case "provisioner":
   145  		provisioner, found := Provisioners[pluginName]
   146  		if !found {
   147  			c.Ui.Error(fmt.Sprintf("Could not load provisioner: %s", pluginName))
   148  			return 1
   149  		}
   150  		server.RegisterProvisioner(provisioner)
   151  	case "post-processor":
   152  		postProcessor, found := PostProcessors[pluginName]
   153  		if !found {
   154  			c.Ui.Error(fmt.Sprintf("Could not load post-processor: %s", pluginName))
   155  			return 1
   156  		}
   157  		server.RegisterPostProcessor(postProcessor)
   158  	case "datasource":
   159  		datasource, found := Datasources[pluginName]
   160  		if !found {
   161  			c.Ui.Error(fmt.Sprintf("Could not load datasource: %s", pluginName))
   162  			return 1
   163  		}
   164  		server.RegisterDatasource(datasource)
   165  	}
   166  
   167  	server.Serve()
   168  
   169  	return 0
   170  }
   171  
   172  func (*ExecuteCommand) Help() string {
   173  	helpText := `
   174  Usage: packer execute [options] PLUGIN
   175  
   176    Runs an internally-compiled version of a plugin from the packer binary.
   177  
   178    NOTE: this is an internal command and you should not call it yourself.
   179  
   180  Options:
   181  
   182    --protobuf: use protobuf for serialising data over-the-wire instead of gob.
   183  `
   184  
   185  	return strings.TrimSpace(helpText)
   186  }
   187  
   188  func (c *ExecuteCommand) Synopsis() string {
   189  	return "internal plugin command"
   190  }