Skip to content

Commit

Permalink
rename bootstrap to setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vlasic committed Aug 18, 2021
1 parent 7ca2d22 commit 4d67540
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 50 deletions.
23 changes: 14 additions & 9 deletions cmd/mantil/cmd/bootstrap.go → cmd/mantil/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package cmd

import (
"github.com/atoz-technology/mantil-cli/internal/aws"
"github.com/atoz-technology/mantil-cli/internal/commands/bootstrap"
"github.com/atoz-technology/mantil-cli/internal/commands/setup"
"github.com/atoz-technology/mantil-cli/internal/log"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

// bootstrapCmd represents the bootstrap command
var bootstrapCmd = &cobra.Command{
Use: "bootstrap",
Short: "Bootstraps mantil backend infrastructure in specified AWS account",
// setupCmd represents the setup command
var setupCmd = &cobra.Command{
Use: "setup",
Short: "Setups mantil backend infrastructure in specified AWS account",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
profiles, err := aws.ListProfiles()
Expand All @@ -30,18 +30,23 @@ var bootstrapCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
b := bootstrap.New(awsClient)
b := setup.New(awsClient)
destroy, err := cmd.Flags().GetBool("destroy")
if err != nil {
log.Fatal(err)
}
if err := b.Bootstrap(destroy); err != nil {
if err := b.Setup(destroy); err != nil {
log.Fatal(err)
}
if destroy {
log.Notice("infrastructure successfully destroyed with profile %s", profile)
} else {
log.Notice("setup successfully finished with profile %s", profile)
}
},
}

func init() {
rootCmd.AddCommand(bootstrapCmd)
bootstrapCmd.Flags().BoolP("destroy", "d", false, "Destroy all resources created by Bootstrap")
rootCmd.AddCommand(setupCmd)
setupCmd.Flags().BoolP("destroy", "d", false, "Destroy all resources created by Setup")
}
10 changes: 5 additions & 5 deletions internal/aws/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
iamTypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
)

func (a *AWS) CreateBootstrapRole(name, lambdaName string) (string, error) {
r, err := a.createRole(name, bootstrapAssumeRolePolicy())
func (a *AWS) CreateSetupRole(name, lambdaName string) (string, error) {
r, err := a.createRole(name, setupAssumeRolePolicy())
if err != nil {
return "", err
}
p, err := a.createPolicy(name, bootstrapLambdaPolicy(*r.RoleId, lambdaName))
p, err := a.createPolicy(name, setupLambdaPolicy(*r.RoleId, lambdaName))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (a *AWS) attachRolePolicy(policyArn, roleName string) error {
return nil
}

func bootstrapAssumeRolePolicy() string {
func setupAssumeRolePolicy() string {
return `{
"Version": "2012-10-17",
"Statement": [
Expand All @@ -90,7 +90,7 @@ func bootstrapAssumeRolePolicy() string {
}`
}

func bootstrapLambdaPolicy(roleID, lambdaName string) string {
func setupLambdaPolicy(roleID, lambdaName string) string {
return `{
"Version": "2012-10-17",
"Statement": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bootstrap
package setup

import (
"context"
Expand All @@ -12,90 +12,89 @@ import (
)

const (
bootstrapLambdaName = "mantil-bootstrap"
setupLambdaName = "mantil-setup"
)

type BootstrapCmd struct {
type SetupCmd struct {
awsClient *aws.AWS
}

func New(awsClient *aws.AWS) *BootstrapCmd {
return &BootstrapCmd{
func New(awsClient *aws.AWS) *SetupCmd {
return &SetupCmd{
awsClient: awsClient,
}
}

type BootstrapRequest struct {
type SetupRequest struct {
Destroy bool
}

type BootstrapResponse struct {
type SetupResponse struct {
APIGatewayURL string
}

func (b *BootstrapCmd) Bootstrap(destroy bool) error {
func (b *SetupCmd) Setup(destroy bool) error {
if destroy {
return b.destroy()
}
return b.create()
}

func (b *BootstrapCmd) create() error {
log.Info("Creating bootstrap function...")
roleARN, err := b.awsClient.CreateBootstrapRole(
bootstrapLambdaName,
bootstrapLambdaName,
func (b *SetupCmd) create() error {
log.Info("Creating setup function...")
roleARN, err := b.awsClient.CreateSetupRole(
setupLambdaName,
setupLambdaName,
)
if err != nil {
return fmt.Errorf("could not create bootstrap role - %v", err)
return fmt.Errorf("could not create setup role - %v", err)
}
_, err = b.awsClient.CreateLambdaFunction(
bootstrapLambdaName,
setupLambdaName,
roleARN,
"mantil-downloads",
"functions/bootstrap.zip",
"functions/setup.zip",
[]string{
"arn:aws:lambda:eu-central-1:553035198032:layer:git-lambda2:8",
"arn:aws:lambda:eu-central-1:477361877445:layer:terraform-lambda:1",
},
)
if err != nil {
return fmt.Errorf("could not create bootstrap function - %v", err)
return fmt.Errorf("could not create setup function - %v", err)
}
log.Info("Deploying backend infrastructure...")
req := &BootstrapRequest{
req := &SetupRequest{
Destroy: false,
}
rsp, err := b.invokeBootstrapLambda(req)
rsp, err := b.invokeSetupLambda(req)
if err != nil {
return fmt.Errorf("could not invoke bootstrap function - %v", err)
return fmt.Errorf("could not invoke setup function - %v", err)
}
config := &commands.BackendConfig{
APIGatewayURL: rsp.APIGatewayURL,
}
if err := config.Save(); err != nil {
return fmt.Errorf("could not save backend config - %v", err)
}
log.Notice("setup successfully finished")
return nil
}

func (b *BootstrapCmd) destroy() error {
req := &BootstrapRequest{
func (b *SetupCmd) destroy() error {
req := &SetupRequest{
Destroy: true,
}
log.Info("Destroying backend infrastructure...")
if _, err := b.invokeBootstrapLambda(req); err != nil {
return fmt.Errorf("could not invoke bootstrap function - %v", err)
if _, err := b.invokeSetupLambda(req); err != nil {
return fmt.Errorf("could not invoke setup function - %v", err)
}
log.Info("Deleting bootstrap function...")
if err := b.awsClient.DeleteRole(bootstrapLambdaName); err != nil {
log.Info("Deleting setup function...")
if err := b.awsClient.DeleteRole(setupLambdaName); err != nil {
return err
}
if err := b.awsClient.DeletePolicy(bootstrapLambdaName); err != nil {
if err := b.awsClient.DeletePolicy(setupLambdaName); err != nil {
return err
}
if err := b.awsClient.DeleteLambdaFunction(bootstrapLambdaName); err != nil {
if err := b.awsClient.DeleteLambdaFunction(setupLambdaName); err != nil {
return err
}
configPath, err := commands.BackendConfigPath()
Expand All @@ -108,14 +107,14 @@ func (b *BootstrapCmd) destroy() error {
return nil
}

func (b *BootstrapCmd) invokeBootstrapLambda(req *BootstrapRequest) (*BootstrapResponse, error) {
lambdaARN, err := b.bootstrapLambdaARN()
func (b *SetupCmd) invokeSetupLambda(req *SetupRequest) (*SetupResponse, error) {
lambdaARN, err := b.setupLambdaARN()
if err != nil {
return nil, err
}
l := logs.NewListener()
wait, err := l.Listen(context.Background(), func(msg string) error {
fmt.Print(msg)
log.Backend(msg)
return nil
})
if err != nil {
Expand All @@ -127,14 +126,14 @@ func (b *BootstrapCmd) invokeBootstrapLambda(req *BootstrapRequest) (*BootstrapR
logs.InboxHeaderKey: l.Subject(),
},
}
rsp := &BootstrapResponse{}
rsp := &SetupResponse{}
if err := b.awsClient.InvokeLambdaFunction(lambdaARN, req, rsp, clientCtx); err != nil {
return nil, fmt.Errorf("could not invoke bootstrap function - %v", err)
return nil, fmt.Errorf("could not invoke setup function - %v", err)
}
return rsp, nil
}

func (b *BootstrapCmd) bootstrapLambdaARN() (string, error) {
func (b *SetupCmd) setupLambdaARN() (string, error) {
accountID, err := b.awsClient.AccountID()
if err != nil {
return "", err
Expand All @@ -143,6 +142,6 @@ func (b *BootstrapCmd) bootstrapLambdaARN() (string, error) {
"arn:aws:lambda:%s:%s:function:%s",
b.awsClient.Region(),
accountID,
bootstrapLambdaName,
setupLambdaName,
), nil
}

0 comments on commit 4d67540

Please sign in to comment.