func (u Up) Execute(subcommandFlags []string, state storage.State) error { config, err := u.parseFlags(subcommandFlags) if err != nil { return err } if u.awsCredentialsPresent(config) { state.AWS.AccessKeyID = config.awsAccessKeyID state.AWS.SecretAccessKey = config.awsSecretAccessKey state.AWS.Region = config.awsRegion if err := u.stateStore.Set(state); err != nil { return err } u.configProvider.SetConfig(aws.Config{ AccessKeyID: config.awsAccessKeyID, SecretAccessKey: config.awsSecretAccessKey, Region: config.awsRegion, }) } else if u.awsCredentialsNotPresent(config) { err := u.awsCredentialValidator.Validate() if err != nil { return err } } else { return u.awsMissingCredentials(config) } err = u.checkForFastFails(state) if err != nil { return err } if state.EnvID == "" { state.EnvID, err = u.envIDGenerator.Generate() if err != nil { return err } } if state.KeyPair.Name == "" { state.KeyPair.Name = fmt.Sprintf("keypair-%s", state.EnvID) } if err := u.stateStore.Set(state); err != nil { return err } keyPair, err := u.keyPairSynchronizer.Sync(ec2.KeyPair{ Name: state.KeyPair.Name, PublicKey: state.KeyPair.PublicKey, PrivateKey: state.KeyPair.PrivateKey, }) if err != nil { return err } state.KeyPair.PublicKey = keyPair.PublicKey state.KeyPair.PrivateKey = keyPair.PrivateKey if err := u.stateStore.Set(state); err != nil { return err } availabilityZones, err := u.availabilityZoneRetriever.Retrieve(state.AWS.Region) if err != nil { return err } if state.Stack.Name == "" { stackEnvID := strings.Replace(state.EnvID, ":", "-", -1) state.Stack.Name = fmt.Sprintf("stack-%s", stackEnvID) if err := u.stateStore.Set(state); err != nil { return err } } var certificateARN string if lbExists(state.Stack.LBType) { certificate, err := u.certificateDescriber.Describe(state.Stack.CertificateName) if err != nil { return err } certificateARN = certificate.ARN } stack, err := u.infrastructureManager.Create(state.KeyPair.Name, len(availabilityZones), state.Stack.Name, state.Stack.LBType, certificateARN, state.EnvID) if err != nil { return err } infrastructureConfiguration := boshinit.InfrastructureConfiguration{ AWSRegion: state.AWS.Region, SubnetID: stack.Outputs["BOSHSubnet"], AvailabilityZone: stack.Outputs["BOSHSubnetAZ"], ElasticIP: stack.Outputs["BOSHEIP"], AccessKeyID: stack.Outputs["BOSHUserAccessKey"], SecretAccessKey: stack.Outputs["BOSHUserSecretAccessKey"], SecurityGroup: stack.Outputs["BOSHSecurityGroup"], } deployInput, err := boshinit.NewDeployInput(state, infrastructureConfiguration, u.stringGenerator, state.EnvID) if err != nil { return err } deployOutput, err := u.boshDeployer.Deploy(deployInput) if err != nil { return err } if state.BOSH.IsEmpty() { state.BOSH = storage.BOSH{ DirectorName: deployInput.DirectorName, DirectorAddress: stack.Outputs["BOSHURL"], DirectorUsername: deployInput.DirectorUsername, DirectorPassword: deployInput.DirectorPassword, DirectorSSLCA: string(deployOutput.DirectorSSLKeyPair.CA), DirectorSSLCertificate: string(deployOutput.DirectorSSLKeyPair.Certificate), DirectorSSLPrivateKey: string(deployOutput.DirectorSSLKeyPair.PrivateKey), Credentials: deployOutput.Credentials, } } state.BOSH.State = deployOutput.BOSHInitState state.BOSH.Manifest = deployOutput.BOSHInitManifest err = u.stateStore.Set(state) if err != nil { return err } boshClient := u.boshClientProvider.Client(state.BOSH.DirectorAddress, state.BOSH.DirectorUsername, state.BOSH.DirectorPassword) cloudConfigInput := u.boshCloudConfigurator.Configure(stack, availabilityZones) err = u.cloudConfigManager.Update(cloudConfigInput, boshClient) if err != nil { return err } err = u.stateStore.Set(state) if err != nil { return err } return nil }
infrastructureConfiguration = boshinit.InfrastructureConfiguration{ AWSRegion: "some-aws-region", SubnetID: "some-subnet-id", AvailabilityZone: "some-az", ElasticIP: "some-eip", AccessKeyID: "some-access-key-id", SecretAccessKey: "some-secret-access-key", SecurityGroup: "some-security-group", } envID = "some-env-id" }) It("constructs a DeployInput given a state", func() { deployInput, err := boshinit.NewDeployInput(state, infrastructureConfiguration, fakeStringGenerator, envID) Expect(err).NotTo(HaveOccurred()) Expect(deployInput).To(Equal(boshinit.DeployInput{ DirectorName: "some-director-name", DirectorUsername: "******", DirectorPassword: "******", State: map[string]interface{}{ "some-state-key": "some-state-value", }, InfrastructureConfiguration: boshinit.InfrastructureConfiguration{ AWSRegion: "some-aws-region", SubnetID: "some-subnet-id", AvailabilityZone: "some-az", ElasticIP: "some-eip", AccessKeyID: "some-access-key-id",