github.phpd.cn/hashicorp/packer@v1.3.2/command/plugin.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  	"fmt"
     9  	"log"
    10  	"regexp"
    11  	"strings"
    12  
    13  	"github.com/hashicorp/packer/packer"
    14  	"github.com/hashicorp/packer/packer/plugin"
    15  
    16  	alicloudecsbuilder "github.com/hashicorp/packer/builder/alicloud/ecs"
    17  	amazonchrootbuilder "github.com/hashicorp/packer/builder/amazon/chroot"
    18  	amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs"
    19  	amazonebssurrogatebuilder "github.com/hashicorp/packer/builder/amazon/ebssurrogate"
    20  	amazonebsvolumebuilder "github.com/hashicorp/packer/builder/amazon/ebsvolume"
    21  	amazoninstancebuilder "github.com/hashicorp/packer/builder/amazon/instance"
    22  	azurearmbuilder "github.com/hashicorp/packer/builder/azure/arm"
    23  	cloudstackbuilder "github.com/hashicorp/packer/builder/cloudstack"
    24  	digitaloceanbuilder "github.com/hashicorp/packer/builder/digitalocean"
    25  	dockerbuilder "github.com/hashicorp/packer/builder/docker"
    26  	filebuilder "github.com/hashicorp/packer/builder/file"
    27  	googlecomputebuilder "github.com/hashicorp/packer/builder/googlecompute"
    28  	hcloudbuilder "github.com/hashicorp/packer/builder/hcloud"
    29  	hypervisobuilder "github.com/hashicorp/packer/builder/hyperv/iso"
    30  	hypervvmcxbuilder "github.com/hashicorp/packer/builder/hyperv/vmcx"
    31  	lxcbuilder "github.com/hashicorp/packer/builder/lxc"
    32  	lxdbuilder "github.com/hashicorp/packer/builder/lxd"
    33  	ncloudbuilder "github.com/hashicorp/packer/builder/ncloud"
    34  	nullbuilder "github.com/hashicorp/packer/builder/null"
    35  	oneandonebuilder "github.com/hashicorp/packer/builder/oneandone"
    36  	openstackbuilder "github.com/hashicorp/packer/builder/openstack"
    37  	oracleclassicbuilder "github.com/hashicorp/packer/builder/oracle/classic"
    38  	oracleocibuilder "github.com/hashicorp/packer/builder/oracle/oci"
    39  	parallelsisobuilder "github.com/hashicorp/packer/builder/parallels/iso"
    40  	parallelspvmbuilder "github.com/hashicorp/packer/builder/parallels/pvm"
    41  	profitbricksbuilder "github.com/hashicorp/packer/builder/profitbricks"
    42  	qemubuilder "github.com/hashicorp/packer/builder/qemu"
    43  	scalewaybuilder "github.com/hashicorp/packer/builder/scaleway"
    44  	tritonbuilder "github.com/hashicorp/packer/builder/triton"
    45  	virtualboxisobuilder "github.com/hashicorp/packer/builder/virtualbox/iso"
    46  	virtualboxovfbuilder "github.com/hashicorp/packer/builder/virtualbox/ovf"
    47  	vmwareisobuilder "github.com/hashicorp/packer/builder/vmware/iso"
    48  	vmwarevmxbuilder "github.com/hashicorp/packer/builder/vmware/vmx"
    49  	alicloudimportpostprocessor "github.com/hashicorp/packer/post-processor/alicloud-import"
    50  	amazonimportpostprocessor "github.com/hashicorp/packer/post-processor/amazon-import"
    51  	artificepostprocessor "github.com/hashicorp/packer/post-processor/artifice"
    52  	checksumpostprocessor "github.com/hashicorp/packer/post-processor/checksum"
    53  	compresspostprocessor "github.com/hashicorp/packer/post-processor/compress"
    54  	dockerimportpostprocessor "github.com/hashicorp/packer/post-processor/docker-import"
    55  	dockerpushpostprocessor "github.com/hashicorp/packer/post-processor/docker-push"
    56  	dockersavepostprocessor "github.com/hashicorp/packer/post-processor/docker-save"
    57  	dockertagpostprocessor "github.com/hashicorp/packer/post-processor/docker-tag"
    58  	googlecomputeexportpostprocessor "github.com/hashicorp/packer/post-processor/googlecompute-export"
    59  	googlecomputeimportpostprocessor "github.com/hashicorp/packer/post-processor/googlecompute-import"
    60  	manifestpostprocessor "github.com/hashicorp/packer/post-processor/manifest"
    61  	shelllocalpostprocessor "github.com/hashicorp/packer/post-processor/shell-local"
    62  	vagrantpostprocessor "github.com/hashicorp/packer/post-processor/vagrant"
    63  	vagrantcloudpostprocessor "github.com/hashicorp/packer/post-processor/vagrant-cloud"
    64  	vspherepostprocessor "github.com/hashicorp/packer/post-processor/vsphere"
    65  	vspheretemplatepostprocessor "github.com/hashicorp/packer/post-processor/vsphere-template"
    66  	ansibleprovisioner "github.com/hashicorp/packer/provisioner/ansible"
    67  	ansiblelocalprovisioner "github.com/hashicorp/packer/provisioner/ansible-local"
    68  	chefclientprovisioner "github.com/hashicorp/packer/provisioner/chef-client"
    69  	chefsoloprovisioner "github.com/hashicorp/packer/provisioner/chef-solo"
    70  	convergeprovisioner "github.com/hashicorp/packer/provisioner/converge"
    71  	fileprovisioner "github.com/hashicorp/packer/provisioner/file"
    72  	powershellprovisioner "github.com/hashicorp/packer/provisioner/powershell"
    73  	puppetmasterlessprovisioner "github.com/hashicorp/packer/provisioner/puppet-masterless"
    74  	puppetserverprovisioner "github.com/hashicorp/packer/provisioner/puppet-server"
    75  	saltmasterlessprovisioner "github.com/hashicorp/packer/provisioner/salt-masterless"
    76  	shellprovisioner "github.com/hashicorp/packer/provisioner/shell"
    77  	shelllocalprovisioner "github.com/hashicorp/packer/provisioner/shell-local"
    78  	windowsrestartprovisioner "github.com/hashicorp/packer/provisioner/windows-restart"
    79  	windowsshellprovisioner "github.com/hashicorp/packer/provisioner/windows-shell"
    80  )
    81  
    82  type PluginCommand struct {
    83  	Meta
    84  }
    85  
    86  var Builders = map[string]packer.Builder{
    87  	"alicloud-ecs":        new(alicloudecsbuilder.Builder),
    88  	"amazon-chroot":       new(amazonchrootbuilder.Builder),
    89  	"amazon-ebs":          new(amazonebsbuilder.Builder),
    90  	"amazon-ebssurrogate": new(amazonebssurrogatebuilder.Builder),
    91  	"amazon-ebsvolume":    new(amazonebsvolumebuilder.Builder),
    92  	"amazon-instance":     new(amazoninstancebuilder.Builder),
    93  	"azure-arm":           new(azurearmbuilder.Builder),
    94  	"cloudstack":          new(cloudstackbuilder.Builder),
    95  	"digitalocean":        new(digitaloceanbuilder.Builder),
    96  	"docker":              new(dockerbuilder.Builder),
    97  	"file":                new(filebuilder.Builder),
    98  	"googlecompute":       new(googlecomputebuilder.Builder),
    99  	"hcloud":              new(hcloudbuilder.Builder),
   100  	"hyperv-iso":          new(hypervisobuilder.Builder),
   101  	"hyperv-vmcx":         new(hypervvmcxbuilder.Builder),
   102  	"lxc":                 new(lxcbuilder.Builder),
   103  	"lxd":                 new(lxdbuilder.Builder),
   104  	"ncloud":              new(ncloudbuilder.Builder),
   105  	"null":                new(nullbuilder.Builder),
   106  	"oneandone":           new(oneandonebuilder.Builder),
   107  	"openstack":           new(openstackbuilder.Builder),
   108  	"oracle-classic":      new(oracleclassicbuilder.Builder),
   109  	"oracle-oci":          new(oracleocibuilder.Builder),
   110  	"parallels-iso":       new(parallelsisobuilder.Builder),
   111  	"parallels-pvm":       new(parallelspvmbuilder.Builder),
   112  	"profitbricks":        new(profitbricksbuilder.Builder),
   113  	"qemu":                new(qemubuilder.Builder),
   114  	"scaleway":            new(scalewaybuilder.Builder),
   115  	"triton":              new(tritonbuilder.Builder),
   116  	"virtualbox-iso":      new(virtualboxisobuilder.Builder),
   117  	"virtualbox-ovf":      new(virtualboxovfbuilder.Builder),
   118  	"vmware-iso":          new(vmwareisobuilder.Builder),
   119  	"vmware-vmx":          new(vmwarevmxbuilder.Builder),
   120  }
   121  
   122  var Provisioners = map[string]packer.Provisioner{
   123  	"ansible":           new(ansibleprovisioner.Provisioner),
   124  	"ansible-local":     new(ansiblelocalprovisioner.Provisioner),
   125  	"chef-client":       new(chefclientprovisioner.Provisioner),
   126  	"chef-solo":         new(chefsoloprovisioner.Provisioner),
   127  	"converge":          new(convergeprovisioner.Provisioner),
   128  	"file":              new(fileprovisioner.Provisioner),
   129  	"powershell":        new(powershellprovisioner.Provisioner),
   130  	"puppet-masterless": new(puppetmasterlessprovisioner.Provisioner),
   131  	"puppet-server":     new(puppetserverprovisioner.Provisioner),
   132  	"salt-masterless":   new(saltmasterlessprovisioner.Provisioner),
   133  	"shell":             new(shellprovisioner.Provisioner),
   134  	"shell-local":       new(shelllocalprovisioner.Provisioner),
   135  	"windows-restart":   new(windowsrestartprovisioner.Provisioner),
   136  	"windows-shell":     new(windowsshellprovisioner.Provisioner),
   137  }
   138  
   139  var PostProcessors = map[string]packer.PostProcessor{
   140  	"alicloud-import":      new(alicloudimportpostprocessor.PostProcessor),
   141  	"amazon-import":        new(amazonimportpostprocessor.PostProcessor),
   142  	"artifice":             new(artificepostprocessor.PostProcessor),
   143  	"checksum":             new(checksumpostprocessor.PostProcessor),
   144  	"compress":             new(compresspostprocessor.PostProcessor),
   145  	"docker-import":        new(dockerimportpostprocessor.PostProcessor),
   146  	"docker-push":          new(dockerpushpostprocessor.PostProcessor),
   147  	"docker-save":          new(dockersavepostprocessor.PostProcessor),
   148  	"docker-tag":           new(dockertagpostprocessor.PostProcessor),
   149  	"googlecompute-export": new(googlecomputeexportpostprocessor.PostProcessor),
   150  	"googlecompute-import": new(googlecomputeimportpostprocessor.PostProcessor),
   151  	"manifest":             new(manifestpostprocessor.PostProcessor),
   152  	"shell-local":          new(shelllocalpostprocessor.PostProcessor),
   153  	"vagrant":              new(vagrantpostprocessor.PostProcessor),
   154  	"vagrant-cloud":        new(vagrantcloudpostprocessor.PostProcessor),
   155  	"vsphere":              new(vspherepostprocessor.PostProcessor),
   156  	"vsphere-template":     new(vspheretemplatepostprocessor.PostProcessor),
   157  }
   158  
   159  var pluginRegexp = regexp.MustCompile("packer-(builder|post-processor|provisioner)-(.+)")
   160  
   161  func (c *PluginCommand) Run(args []string) int {
   162  	// This is an internal call (users should not call this directly) so we're
   163  	// not going to do much input validation. If there's a problem we'll often
   164  	// just crash. Error handling should be added to facilitate debugging.
   165  	log.Printf("args: %#v", args)
   166  	if len(args) != 1 {
   167  		c.Ui.Error("Wrong number of args")
   168  		return 1
   169  	}
   170  
   171  	// Plugin will match something like "packer-builder-amazon-ebs"
   172  	parts := pluginRegexp.FindStringSubmatch(args[0])
   173  	if len(parts) != 3 {
   174  		c.Ui.Error(fmt.Sprintf("Error parsing plugin argument [DEBUG]: %#v", parts))
   175  		return 1
   176  	}
   177  	pluginType := parts[1] // capture group 1 (builder|post-processor|provisioner)
   178  	pluginName := parts[2] // capture group 2 (.+)
   179  
   180  	server, err := plugin.Server()
   181  	if err != nil {
   182  		c.Ui.Error(fmt.Sprintf("Error starting plugin server: %s", err))
   183  		return 1
   184  	}
   185  
   186  	switch pluginType {
   187  	case "builder":
   188  		builder, found := Builders[pluginName]
   189  		if !found {
   190  			c.Ui.Error(fmt.Sprintf("Could not load builder: %s", pluginName))
   191  			return 1
   192  		}
   193  		server.RegisterBuilder(builder)
   194  	case "provisioner":
   195  		provisioner, found := Provisioners[pluginName]
   196  		if !found {
   197  			c.Ui.Error(fmt.Sprintf("Could not load provisioner: %s", pluginName))
   198  			return 1
   199  		}
   200  		server.RegisterProvisioner(provisioner)
   201  	case "post-processor":
   202  		postProcessor, found := PostProcessors[pluginName]
   203  		if !found {
   204  			c.Ui.Error(fmt.Sprintf("Could not load post-processor: %s", pluginName))
   205  			return 1
   206  		}
   207  		server.RegisterPostProcessor(postProcessor)
   208  	}
   209  
   210  	server.Serve()
   211  
   212  	return 0
   213  }
   214  
   215  func (*PluginCommand) Help() string {
   216  	helpText := `
   217  Usage: packer plugin PLUGIN
   218  
   219    Runs an internally-compiled version of a plugin from the packer binary.
   220  
   221    NOTE: this is an internal command and you should not call it yourself.
   222  `
   223  
   224  	return strings.TrimSpace(helpText)
   225  }
   226  
   227  func (c *PluginCommand) Synopsis() string {
   228  	return "internal plugin command"
   229  }