func DeleteTestStateBagStepDeleteResourceGroup() multistep.StateBag { stateBag := new(multistep.BasicStateBag) stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") stateBag.Put(constants.ArmIsResourceGroupCreated, "Unit Test: IsResourceGroupCreated") return stateBag }
func TestStepInjectConfiguration(t *testing.T) { env := new(multistep.BasicStateBag) os.Mkdir("tmp", 0777) env.Put("config_path", "tmp/") conf := config.NewDefault() // Create the configuration file sections and items conf.AddSection("gethub") conf.AddSection("github") conf.AddSection("ignores") conf.AddOption("gethub", "path", "tmp") conf.AddOption("github", "username", "foo") conf.AddOption("github", "token", "bar") conf.AddOption("ignores", "repo", "facebook") conf.AddOption("ignores", "owner", "pearkes") conf.WriteFile("tmp/.gethubconfig", 0644, "") step := &StepInjectConfiguration{} results := step.Run(env) if results != multistep.ActionContinue { t.Fatal("step did not return ActionContinue") } os.RemoveAll("tmp") }
func createTestStateBagStepValidateTemplate() multistep.StateBag { stateBag := new(multistep.BasicStateBag) stateBag.Put(constants.ArmDeploymentName, "Unit Test: DeploymentName") stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") return stateBag }
func createTestStateBagStepCreateResourceGroup() multistep.StateBag { stateBag := new(multistep.BasicStateBag) stateBag.Put(constants.ArmLocation, "Unit Test: Location") stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") return stateBag }
func createTestStateBagStepGetOSDisk() multistep.StateBag { stateBag := new(multistep.BasicStateBag) stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName") stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") return stateBag }
func testStepCreateFloppyState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("ui", &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) return state }
func createTestStateBagStepGetIPAddress() multistep.StateBag { stateBag := new(multistep.BasicStateBag) stateBag.Put(constants.ArmPublicIPAddressName, "Unit Test: PublicIPAddressName") stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") return stateBag }
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { // Only accepts input from the vagrant post-processor if artifact.BuilderId() != "mitchellh.post-processor.vagrant" { return nil, false, fmt.Errorf( "Unknown artifact type, requires box from vagrant post-processor: %s", artifact.BuilderId()) } // We assume that there is only one .box file to upload if !strings.HasSuffix(artifact.Files()[0], ".box") { return nil, false, fmt.Errorf( "Unknown files in artifact from vagrant post-processor: %s", artifact.Files()) } // create the HTTP client p.client = VagrantCloudClient{}.New(p.config.VagrantCloudUrl, p.config.AccessToken) // The name of the provider for vagrant cloud, and vagrant providerName := providerFromBuilderName(artifact.Id()) // Set up the state state := new(multistep.BasicStateBag) state.Put("config", p.config) state.Put("client", p.client) state.Put("artifact", artifact) state.Put("artifactFilePath", artifact.Files()[0]) state.Put("ui", ui) state.Put("providerName", providerName) // Build the steps steps := []multistep.Step{ new(stepVerifyBox), new(stepCreateVersion), new(stepCreateProvider), new(stepPrepareUpload), new(stepUpload), new(stepVerifyUpload), new(stepReleaseVersion), } // Run the steps if p.config.PackerDebug { p.runner = &multistep.DebugRunner{ Steps: steps, PauseFn: common.MultistepDebugFn(ui), } } else { p.runner = &multistep.BasicRunner{Steps: steps} } p.runner.Run(state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { return nil, false, rawErr.(error) } return NewArtifact(providerName, p.config.Tag), true, nil }
// getTestState is a utility function that sets up a BasicStateBag with a // BasicUi func getTestState() *multistep.BasicStateBag { state := new(multistep.BasicStateBag) state.Put("ui", &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) return state }
func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("hook", &packer.MockHook{}) state.Put("ui", &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) return state }
func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("driver", new(vmwcommon.DriverMock)) state.Put("ui", &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) return state }
func TestStepCheckPath_Not_Exists(t *testing.T) { env := new(multistep.BasicStateBag) env.Put("path", "foobar/") step := &StepCheckPath{} results := step.Run(env) if results != multistep.ActionHalt { t.Fatal("step did not return ActionContinue") } }
func TestStepCheckRepo_Ignore_Repo_Name(t *testing.T) { env := new(multistep.BasicStateBag) os.MkdirAll("tmp", 0777) env.Put("path", "tmp") env.Put("ignored_owners", []string{}) env.Put("ignored_repos", []string{"bootstrap"}) repo := Repo{FullName: "pearkes/bootstrap"} env.Put("repo", repo) step := &StepCheckRepo{} results := step.Run(env) state := env.Get("repo_state").(string) if state != "ignore" { t.Fatal("repo state does not match ignore") } if results != multistep.ActionContinue { t.Fatal("step did not return ActionContinue") } os.RemoveAll("tmp") }
func TestStepCheckPath_Exists(t *testing.T) { env := new(multistep.BasicStateBag) env.Put("path", "tmp/") os.Mkdir("tmp", 0777) step := &StepCheckPath{} results := step.Run(env) if results != multistep.ActionContinue { t.Fatal("step did not return ActionContinue") } os.RemoveAll("tmp") }
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { driver := &DockerDriver{Tpl: b.config.tpl, Ui: ui} if err := driver.Verify(); err != nil { return nil, err } steps := []multistep.Step{ &StepTempDir{}, &StepPull{}, &StepRun{}, &StepProvision{}, } if b.config.Commit { steps = append(steps, new(StepCommit)) } else { steps = append(steps, new(StepExport)) } // Setup the state bag and initial state for the steps state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("hook", hook) state.Put("ui", ui) // Setup the driver that will talk to Docker state.Put("driver", driver) // Run! if b.config.PackerDebug { b.runner = &multistep.DebugRunner{ Steps: steps, PauseFn: common.MultistepDebugFn(ui), } } else { b.runner = &multistep.BasicRunner{Steps: steps} } b.runner.Run(state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } var artifact packer.Artifact // No errors, must've worked if b.config.Commit { artifact = &ImportArtifact{ IdValue: state.Get("image_id").(string), BuilderIdValue: BuilderIdImport, Driver: driver, } } else { artifact = &ExportArtifact{path: b.config.ExportPath} } return artifact, nil }
// Run executes a googlecompute Packer build and returns a packer.Artifact // representing a GCE machine image. func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { driver, err := NewDriverGCE( ui, b.config.ProjectId, &b.config.Account) if err != nil { return nil, err } // Set up the state. state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("driver", driver) state.Put("hook", hook) state.Put("ui", ui) // Build the steps. steps := []multistep.Step{ new(StepCheckExistingImage), &StepCreateSSHKey{ Debug: b.config.PackerDebug, DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName), }, &StepCreateInstance{ Debug: b.config.PackerDebug, }, &StepInstanceInfo{ Debug: b.config.PackerDebug, }, &communicator.StepConnect{ Config: &b.config.Comm, Host: commHost, SSHConfig: sshConfig, }, new(common.StepProvision), new(StepWaitInstanceStartup), new(StepTeardownInstance), new(StepCreateImage), } // Run the steps. b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) b.runner.Run(state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } if _, ok := state.GetOk("image"); !ok { log.Println("Failed to find image in state. Bug?") return nil, nil } artifact := &Artifact{ image: state.Get("image").(*Image), driver: driver, config: b.config, } return artifact, nil }
func TestStepCheckConfigurationFile_No_Config(t *testing.T) { env := new(multistep.BasicStateBag) os.Mkdir("tmp", 0777) env.Put("config_path", "tmp/") step := &StepCheckConfigurationFile{} results := step.Run(env) // Output: It seems as though you haven't set-up gethub. Please run `gethub authorize` if results != multistep.ActionHalt { t.Fatal("step did not return ActionHalt") } os.RemoveAll("tmp") }
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { // Initialize the DO API client client := DigitalOceanClient{}.New(b.config.ClientID, b.config.APIKey) // Set up the state state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("client", client) state.Put("hook", hook) state.Put("ui", ui) // Build the steps steps := []multistep.Step{ new(stepCreateSSHKey), new(stepCreateDroplet), new(stepDropletInfo), &common.StepConnectSSH{ SSHAddress: sshAddress, SSHConfig: sshConfig, SSHWaitTimeout: 5 * time.Minute, }, new(common.StepProvision), new(stepShutdown), new(stepPowerOff), new(stepSnapshot), } // Run the steps if b.config.PackerDebug { b.runner = &multistep.DebugRunner{ Steps: steps, PauseFn: common.MultistepDebugFn(ui), } } else { b.runner = &multistep.BasicRunner{Steps: steps} } b.runner.Run(state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } if _, ok := state.GetOk("snapshot_name"); !ok { log.Println("Failed to find snapshot_name in state. Bug?") return nil, nil } artifact := &Artifact{ snapshotName: state.Get("snapshot_name").(string), snapshotId: state.Get("snapshot_image_id").(uint), client: client, } return artifact, nil }
// Run executes a googlecompute Packer build and returns a packer.Artifact // representing a GCE machine image. func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { driver, err := NewDriverGCE( ui, b.config.ProjectId, b.config.clientSecrets, b.config.privateKeyBytes) if err != nil { return nil, err } // Set up the state. state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("driver", driver) state.Put("hook", hook) state.Put("ui", ui) // Build the steps. steps := []multistep.Step{ new(StepCreateSSHKey), new(StepCreateInstance), new(StepInstanceInfo), &common.StepConnectSSH{ SSHAddress: sshAddress, SSHConfig: sshConfig, SSHWaitTimeout: 5 * time.Minute, }, new(common.StepProvision), new(StepUpdateGsutil), new(StepCreateImage), new(StepUploadImage), new(StepRegisterImage), } // Run the steps. if b.config.PackerDebug { b.runner = &multistep.DebugRunner{ Steps: steps, PauseFn: common.MultistepDebugFn(ui), } } else { b.runner = &multistep.BasicRunner{Steps: steps} } b.runner.Run(state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } if _, ok := state.GetOk("image_name"); !ok { log.Println("Failed to find image_name in state. Bug?") return nil, nil } artifact := &Artifact{ imageName: state.Get("image_name").(string), driver: driver, } return artifact, nil }
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { config := b.config driver, err := NewDriverTriton(ui, config) if err != nil { return nil, err } state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("driver", driver) state.Put("hook", hook) state.Put("ui", ui) steps := []multistep.Step{ &StepCreateSourceMachine{}, &communicator.StepConnect{ Config: &config.Comm, Host: commHost, SSHConfig: sshConfig, }, &common.StepProvision{}, &StepStopMachine{}, &StepCreateImageFromMachine{}, &StepDeleteMachine{}, } if b.config.PackerDebug { b.runner = &multistep.DebugRunner{ Steps: steps, PauseFn: common.MultistepDebugFn(ui), } } else { b.runner = &multistep.BasicRunner{Steps: steps} } b.runner.Run(state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } // If there is no image, just return if _, ok := state.GetOk("image"); !ok { return nil, nil } artifact := &Artifact{ ImageID: state.Get("image").(string), BuilderIDValue: BuilderId, Driver: driver, } return artifact, nil }
func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) state.Put("config", testConfigStruct(t)) state.Put("driver", &MockDriver{}) state.Put("hook", &packer.MockHook{}) state.Put("ui", &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), }) return state }
// Run executes a SoftLayer Packer build and returns a packer.Artifact // representing a SoftLayer machine image (flex). func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { // Create the client client := (SoftlayerClient{}).New(self.config.Username, self.config.APIKey) // Set up the state which is used to share state between the steps state := new(multistep.BasicStateBag) state.Put("config", &self.config) state.Put("client", client) state.Put("hook", hook) state.Put("ui", ui) // Build the steps steps := []multistep.Step{ &stepCreateSshKey{ PrivateKeyFile: self.config.SSHPrivateKey, }, new(stepCreateInstance), new(stepWaitforInstance), &communicator.StepConnect{ Config: &self.config.Config, Host: sshHost, SSHPort: sshPort, SSHConfig: sshConfig, }, new(common.StepProvision), new(stepCaptureImage), } // Create the runner which will run the steps we just build self.runner = &multistep.BasicRunner{Steps: steps} self.runner.Run(state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } if _, ok := state.GetOk("image_id"); !ok { log.Println("Failed to find image_id in state. Bug?") return nil, nil } // Create an artifact and return it artifact := &Artifact{ imageName: self.config.ImageName, imageId: state.Get("image_id").(string), datacenterName: self.config.DatacenterName, client: client, } return artifact, nil }
func TestWinRMAddressFunc_UsesPortForwarding(t *testing.T) { config := wincommon.WinRMConfig{ WinRMHost: "localhost", WinRMPort: 456, } state := new(multistep.BasicStateBag) state.Put("winrmHostPort", uint(123)) f := WinRMAddressFunc(config) address, err := f(state) if err != nil { t.Fatalf("unexpected err %v", err) } if address != "localhost:123" { t.Errorf("should have forwarded to port 123, but was %s", address) } }
func TestStepCheckConfigurationFile_Good_Config(t *testing.T) { env := new(multistep.BasicStateBag) os.Mkdir("tmp", 0777) env.Put("config_path", "tmp/") file, _ := os.Create("tmp/.gethubconfig") file.WriteString("\n[gethub]\npath: tmp/\n") step := &StepCheckConfigurationFile{} results := step.Run(env) if results != multistep.ActionContinue { t.Fatal("step did not return ActionContinue") } os.RemoveAll("tmp") }
func TestESX5Driver_CommHost(t *testing.T) { const expected_host = "127.0.0.1" config := testConfig() config["communicator"] = "winrm" config["winrm_username"] = "******" config["winrm_password"] = "******" config["winrm_host"] = expected_host var b Builder warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } if err != nil { t.Fatalf("should not have error: %s", err) } if host := b.config.CommConfig.Host(); host != expected_host { t.Fatalf("setup failed, bad host name: %s", host) } state := new(multistep.BasicStateBag) state.Put("config", &b.config) var driver ESX5Driver host, err := driver.CommHost(state) if err != nil { t.Fatalf("should not have error: %s", err) } if host != expected_host { t.Errorf("bad host name: %s", host) } address, ok := state.GetOk("vm_address") if !ok { t.Error("state not updated with vm_address") } if address.(string) != expected_host { t.Errorf("bad vm_address: %s", address.(string)) } }
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { steps := []multistep.Step{ &communicator.StepConnect{ Config: &b.config.CommConfig, Host: CommHost(b.config.CommConfig.Host()), SSHConfig: SSHConfig( b.config.CommConfig.SSHUsername, b.config.CommConfig.SSHPassword, b.config.CommConfig.SSHPrivateKey), }, &common.StepProvision{}, } // Setup the state bag and initial state for the steps state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("hook", hook) state.Put("ui", ui) // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) b.runner.Run(state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } // No errors, must've worked artifact := &NullArtifact{} return artifact, nil }
func main() { // Debugging and version flags debug := flag.Bool("debug", false, "Logs debugging information to STDOUT.") version := flag.Bool("version", false, "Prints the version and exits.") flag.BoolVar(debug, "d", false, "Logs debugging information to STDOUT.") flag.BoolVar(version, "v", false, "Prints the version and exits.") // Override the flag.Usage function to print custom usage info. flag.Usage = usage flag.Parse() arg := flag.Arg(0) // Discard logging if debug is turned off. if *debug == false { log.SetOutput(ioutil.Discard) } // Print the version and exit if *version { fmt.Println(versionString) os.Exit(1) } // Log enabled debugging log.Println("Debugging enabled for", versionString) state := new(multistep.BasicStateBag) state.Put("debug", *debug) state.Put("config_path", os.Getenv("GETCONFIG_PATH")) if arg == "authorize" { authorizeRunner(state) } else if arg == "" { updateRunner(state) } else { fmt.Println("Invalid argument: " + arg) // Prints the usage and exits usage() } }
func TestStepCheckConfigurationFile_Corrupt_Config(t *testing.T) { env := new(multistep.BasicStateBag) os.Mkdir("tmp", 0777) env.Put("config_path", "tmp/") file, _ := os.Create("tmp/.gethubconfig") // Some messy string file.WriteString("foobar!baz:bar\n\nfoob:ar") step := &StepCheckConfigurationFile{} results := step.Run(env) // Output: Something seems to be wrong with your ~/.gethubconfig file. Please run `gethub authorize` if results != multistep.ActionHalt { t.Fatal("step did not return ActionHalt") } os.RemoveAll("tmp") }
func createTestStateBagStepCaptureImage() multistep.StateBag { stateBag := new(multistep.BasicStateBag) stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName") stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") stateBag.Put(constants.ArmVirtualMachineCaptureParameters, &compute.VirtualMachineCaptureParameters{}) return stateBag }
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("hook", hook) state.Put("ui", ui) steps := []multistep.Step{ &StepCreateSSHKey{ Debug: b.config.PackerDebug, DebugKeyPath: fmt.Sprintf("oneandone_%s", b.config.SnapshotName), }, new(stepCreateServer), &communicator.StepConnect{ Config: &b.config.Comm, Host: commHost, SSHConfig: sshConfig, }, &common.StepProvision{}, new(stepTakeSnapshot), } if b.config.PackerDebug { b.runner = &multistep.DebugRunner{ Steps: steps, PauseFn: common.MultistepDebugFn(ui), } } else { b.runner = &multistep.BasicRunner{Steps: steps} } b.runner.Run(state) if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } if temp, ok := state.GetOk("snapshot_name"); ok { b.config.SnapshotName = temp.(string) } artifact := &Artifact{ snapshotName: b.config.SnapshotName, } if id, ok := state.GetOk("snapshot_id"); ok { artifact.snapshotId = id.(string) } else { return nil, errors.New("Image creation has failed.") } return artifact, nil }