Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate CodePipeline locally #28

Open
3 tasks
ivorscott opened this issue Jun 13, 2022 · 0 comments
Open
3 tasks

Simulate CodePipeline locally #28

ivorscott opened this issue Jun 13, 2022 · 0 comments

Comments

@ivorscott
Copy link
Owner

ivorscott commented Jun 13, 2022

Problem

We need to simulate the CodePipeline to provision resources for premium tenants during local development.

Acceptance Criteria

  • Provision tenant specific resources locally.
  • Create tenant specific database tables: for user and project services.
  • Create custom deployment manifests: for user and project services.
  • Incoming requests for tenants specific resources should navigate to tenant specific namespaces.

Technical Breakdown

  • Create folder for manifest templates:
manifests
  templates
     project.tpl.txt
     user.tpl.txt

Ensure template contents have parameters in all caps. For example:

test: INSERT_NUMBER
  • Create provision.sh script in project root.
#!/bin/bash

# Create namespace.

kubectl create ns ${TenantName}

# Create dedicated user table for tenant.

# Create dedicated project table for tenant.

# Initialize manifests from templates.
cp user.tpl.txt ./manifests/templates/mic-user.yaml
cp project.tpl.txt ./manifests/templates/mic-project.yaml

# Hydrate user deployment yaml file for tenant.
sed -i -e 's,INSERT_NUMBER,'$Num',g' ./manifests/templates/mic-user.yaml

# Hydrate project deployment yaml file for tenant.
sed -i -e 's,INSERT_NUMBER,'$Num',g' ./manifests/templates/mic-project.yaml

# Deploy resources.
kubectl apply -f ./manifests/templates
  • Call provision script from registration service when appropriate.
type MockCodePipeline struct {
	logger *zap.Logger
	cmd    *exec.Cmd
}

func NewMockCodePipeline(logger *zap.Logger) *MockCodePipeline {
	cmd := exec.Command("./provision.sh")
	return &MockCodePipeline{
		logger: logger,
		cmd:    cmd,
	}
}

func (scp *MockCodePipeline) simulateCodePipeline() {
	// Gather requirements. Do not include duplicate environment keys to avoid overriding values.
	scp.setProjectEnvRequirements()
	scp.setUserEnvRequirements()

	// Execute.
	scp.logger.Info(scp.cmd.String())
	//if output, err := cmd.Output(); err != nil {
	//	scp.logger.Error("", zap.Error(err))
	//} else {
	//	scp.logger.Info("", zap.Any("output", output))
	//}
}

func (scp *MockCodePipeline) setProjectEnvRequirements() {
	var env []string

	// gather requirements

	scp.cmd.Env = append(scp.cmd.Env, env...)
}
func (scp *MockCodePipeline) setUserEnvRequirements() {
	var env []string

	// gather requirements

	scp.cmd.Env = append(scp.cmd.Env, env...)
}


func (rs *RegistrationService) provision(ctx context.Context, tenantPath string, plan Plan) error {
	if plan != PlanPremium {
		return nil
	}
	if !rs.isProd {
		scp := NewMockCodePipeline(rs.logger)
		scp.simulateCodePipeline()
		return nil
	}
	client := codepipeline.New(codepipeline.Options{
		Region: rs.region,
	})

	input := codepipeline.StartPipelineExecutionInput{
		Name:               aws.String("tenant-onboarding-pipeline"),
		ClientRequestToken: aws.String(fmt.Sprintf("requestToken-%s", time.Now().UTC())),
	}

	output, err := client.StartPipelineExecution(ctx, &input)
	if err != nil {
		return err
	}
	rs.logger.Info(fmt.Sprintf("successfully started pipeline - response: %+v", output))
	return nil
}

https://github.com/aws-samples/aws-saas-factory-eks-saas-workshop/blob/main/services/application/apps/order/k8s/template.yaml

https://github.com/aws-samples/aws-saas-factory-eks-saas-workshop/blob/c59165babe46073601243876b947029521bd2019/cdk/root/lib/tenant-infra/tenant-infra-stack.ts

https://github.com/aws-samples/aws-saas-factory-eks-saas-workshop/blob/c59165babe46073601243876b947029521bd2019/buildspec.yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant