Skip to content

Commit

Permalink
* Default buildspecs
Browse files Browse the repository at this point in the history
* Environment term also does service term
* Quickstart demo
  • Loading branch information
cplee committed Mar 8, 2017
1 parent 0bf728d commit f73fe21
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 28 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ IS_MASTER := $(filter master, $(BRANCH))
VERSION := $(shell cat VERSION)$(if $(IS_MASTER),,-$(BRANCH))
SRC_FILES = $(shell glide nv)
ARCH := $(shell go env GOARCH)
OS := $(shell go env GOOS)
BUILD_DIR = $(if $(CIRCLE_ARTIFACTS),$(CIRCLE_ARTIFACTS),.release)
BUILD_FILES = $(foreach os, $(TARGET_OS), $(BUILD_DIR)/$(PACKAGE)-$(os)-$(ARCH))
UPLOAD_FILES = $(foreach os, $(TARGET_OS), $(PACKAGE)-$(os)-$(ARCH))
Expand Down Expand Up @@ -43,13 +44,18 @@ else
endif


build: $(BUILD_FILES) gen
build: gen $(BUILD_FILES)

$(BUILD_FILES):
@echo "=== building $(VERSION) - $@ ==="
mkdir -p $(BUILD_DIR)
GOOS=$(word 2,$(subst -, ,$(notdir $@))) GOARCH=$(word 3,$(subst -, ,$(notdir $@))) go build -ldflags=$(GOLDFLAGS) -o '$@'

install: build
@echo "=== building $(VERSION) - $(PACKAGE)-$(OS)-$(ARCH) ==="
cp $(BUILD_DIR)/$(PACKAGE)-$(OS)-$(ARCH) /usr/local/bin/mu
chmod 755 /usr/local/bin/mu

release-clean:
ifeq ($(IS_MASTER),)
@echo "=== clearing old release $(VERSION) ==="
Expand Down Expand Up @@ -91,4 +97,4 @@ fmt:
go fmt $(SRC_FILES)


.PHONY: default all lint test build deps gen clean release-clean release-create dev-release release $(UPLOAD_FILES) $(TARGET_OS)
.PHONY: default all lint test build deps gen clean release-clean release-create dev-release release install $(UPLOAD_FILES) $(TARGET_OS)
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ The `mu` tool uses CloudFormation stacks to manage all resources it creates. Ad

![Architecture Diagram](https://github.com/stelligent/mu/wiki/img/ms-architecture-3.png)

# Demo
Watch the 90 second demo below to see mu in action!

![Demo](https://github.com/stelligent/mu/wiki/quickstart/mu-quickstart.gif)

# Get Started!
Install latest version to /usr/local/bin (or for additional options, see [wiki](https://github.com/stelligent/mu/wiki/Installation)):

Expand All @@ -19,7 +24,6 @@ curl -s https://raw.githubusercontent.com/stelligent/mu/master/install.sh | sh

Assuming your project already has a Dockerfile, you can initialize your mu.yml file with: `mu init`. More details available in the [quickstart](https://github.com/stelligent/mu/wiki/Quickstart).


# What's next?
Check out the [examples](examples) to see common `mu.yml` configuration use cases:

Expand All @@ -36,7 +40,7 @@ Refer to the wiki for complete details on the configuration of `mu.yml` and the
* **[Environments](https://github.com/stelligent/mu/wiki/Environments)** - managing VPCs, ECS clusters, container instances and ALBs
* **[Services](https://github.com/stelligent/mu/wiki/Services)** - managing ECS service configuration
* **[Pipelines](https://github.com/stelligent/mu/wiki/Pipelines)** - managing continuous delivery pipelines
* **[CLI](https://github.com/stelligent/mu/wiki/CLI)** - details about using the CLI
* **[CLI](https://github.com/stelligent/mu/wiki/CLI-Usage)** - details about using the CLI
* **[Custom CloudFormation](https://github.com/stelligent/mu/wiki/Custom-CloudFormation)** - details about customizing the CloudFormation that is generated by mu.

# Contributing
Expand Down
1 change: 1 addition & 0 deletions cli/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func newPipelinesUpsertCommand(ctx *common.Context) *cli.Command {
byteToken, err := terminal.ReadPassword(int(syscall.Stdin))
if err == nil {
token = strings.TrimSpace(string(byteToken))
fmt.Println("")
}
}

Expand Down
35 changes: 22 additions & 13 deletions common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,40 @@ func NewContext() *Context {
// InitializeConfigFromFile loads config from file
func (ctx *Context) InitializeConfigFromFile(muFile string) error {
absMuFile, err := filepath.Abs(muFile)
if err != nil {
return err
}

// load yaml config
yamlFile, err := os.Open(absMuFile)
if err != nil {
return err
}
defer func() {
yamlFile.Close()
}()

// set the basedir
ctx.Config.Basedir = path.Dir(absMuFile)
log.Debugf("Setting basedir=%s", ctx.Config.Basedir)

ctx.Config.Repo.Name = path.Base(ctx.Config.Basedir)
log.Debugf("Setting repo name=%s", ctx.Config.Repo.Name)

ctx.Config.Repo.Revision = time.Now().Format("20060102150405")
gitRevision, err := findGitRevision(absMuFile)

gitRevision, err := findGitRevision(ctx.Config.Basedir)
if err == nil {
ctx.Config.Repo.Revision = gitRevision
} else {
log.Warningf("Unable to determine git revision: %s", err.Error())
}
log.Debugf("Setting repo revision=%s", ctx.Config.Repo.Revision)

gitSlug, err := findGitSlug()
if err == nil {
ctx.Config.Repo.Slug = gitSlug
} else {
log.Warningf("Unable to determine git slug: %s", err.Error())
}
log.Debugf("Setting repo slug=%s", ctx.Config.Repo.Slug)

// load yaml config
yamlFile, err := os.Open(absMuFile)
if err != nil {
return err
}
defer func() {
yamlFile.Close()
}()

return ctx.InitializeConfig(bufio.NewReader(yamlFile))
}
Expand Down
10 changes: 10 additions & 0 deletions common/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ func buildStackTags(tags map[string]string) []*cloudformation.Tag {
func (cfnMgr *cloudformationStackManager) UpsertStack(stackName string, templateBodyReader io.Reader, parameters map[string]string, tags map[string]string) error {
stack := cfnMgr.AwaitFinalStatus(stackName)

// delete stack if in rollback status
if stack != nil && stack.Status == cloudformation.StackStatusRollbackComplete {
log.Warningf(" Stack '%s' was in '%s' status, deleting...", stackName, stack.Status)
err := cfnMgr.DeleteStack(stackName)
if err != nil {
return err
}
stack = cfnMgr.AwaitFinalStatus(stackName)
}

// load the template
templateBodyBytes := new(bytes.Buffer)
templateBodyBytes.ReadFrom(templateBodyReader)
Expand Down
8 changes: 4 additions & 4 deletions templates/assets.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion templates/assets/bucket.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${BucketPrefix}-${AWS::Region}-${AWS::AccountId}
BucketName: !Sub mu-${BucketPrefix}-${AWS::Region}-${AWS::AccountId}
Outputs:
Bucket:
Description: Name of the pipeline bucket
Expand Down
10 changes: 8 additions & 2 deletions templates/assets/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ Resources:
Type: CODEPIPELINE
BuildSpec: !Sub |
version: 0.1
environment_variables:
plaintext:
DEFAULT_BUILDSPEC: "${DefaultBuildspec}"
phases:
build:
commands:
Expand All @@ -209,7 +212,7 @@ Resources:
- mu -c ${MuFile} env up ${TestEnv} || echo "Skipping update of environment"
- mu -c ${MuFile} svc deploy ${TestEnv} -t latest
- mu env show ${TestEnv} -f json > env.json
- mv buildspec-test.yml buildspec.yml || echo "${DefaultBuildspec}" > buildspec.yml
- mv buildspec-test.yml buildspec.yml || echo "$DEFAULT_BUILDSPEC" > buildspec.yml
artifacts:
files:
- '**/*'
Expand Down Expand Up @@ -247,6 +250,9 @@ Resources:
Type: CODEPIPELINE
BuildSpec: !Sub |
version: 0.1
environment_variables:
plaintext:
DEFAULT_BUILDSPEC: "${DefaultBuildspec}"
phases:
build:
commands:
Expand All @@ -255,7 +261,7 @@ Resources:
- mu -c ${MuFile} env up ${ProdEnv} || echo "Skipping update of environment"
- mu -c ${MuFile} svc deploy ${ProdEnv} -t latest
- mu env show ${ProdEnv} -f json > env.json
- mv buildspec-prod.yml buildspec.yml || echo "${DefaultBuildspec}" > buildspec.yml
- mv buildspec-prod.yml buildspec.yml || echo "$DEFAULT_BUILDSPEC" > buildspec.yml
artifacts:
files:
- '**/*'
Expand Down
2 changes: 1 addition & 1 deletion wiki
Submodule wiki updated from 488fcf to f615a3
28 changes: 28 additions & 0 deletions workflows/environment_terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,39 @@ func NewEnvironmentTerminator(ctx *common.Context, environmentName string) Execu
workflow := new(environmentWorkflow)

return newWorkflow(
workflow.environmentServiceTerminator(environmentName, ctx.StackManager, ctx.StackManager, ctx.StackManager),
workflow.environmentEcsTerminator(environmentName, ctx.StackManager, ctx.StackManager),
workflow.environmentVpcTerminator(environmentName, ctx.StackManager, ctx.StackManager),
)
}

func (workflow *environmentWorkflow) environmentServiceTerminator(environmentName string, stackLister common.StackLister, stackDeleter common.StackDeleter, stackWaiter common.StackWaiter) Executor {
return func() error {
log.Noticef("Terminating Services for environment '%s' ...", environmentName)
stacks, err := stackLister.ListStacks(common.StackTypeService)
if err != nil {
return err
}
for _, stack := range stacks {
if stack.Tags["environment"] != environmentName {
continue
}
err := stackDeleter.DeleteStack(stack.Name)
if err != nil {
return err
}
}
for _, stack := range stacks {
if stack.Tags["environment"] != environmentName {
continue
}
log.Infof(" Undeploying service '%s' from environment '%s'", stack.Tags["service"], environmentName )
stackWaiter.AwaitFinalStatus(stack.Name)
}

return nil
}
}
func (workflow *environmentWorkflow) environmentEcsTerminator(environmentName string, stackDeleter common.StackDeleter, stackWaiter common.StackWaiter) Executor {
return func() error {
log.Noticef("Terminating ECS environment '%s' ...", environmentName)
Expand Down
7 changes: 5 additions & 2 deletions workflows/pipeline_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stelligent/mu/common"
"github.com/stelligent/mu/templates"
"strings"
"regexp"
)

// NewPipelineUpserter create a new workflow for upserting a pipeline
Expand All @@ -32,7 +33,7 @@ func (workflow *pipelineWorkflow) pipelineBucket(stackUpserter common.StackUpser
}
log.Noticef("Upserting Bucket for CodePipeline")
bucketParams := make(map[string]string)
bucketParams["BucketPrefix"] = "mu-codepipeline"
bucketParams["BucketPrefix"] = "codepipeline"
err = stackUpserter.UpsertStack(bucketStackName, template, bucketParams, buildPipelineTags(workflow.serviceName, common.StackTypeBucket))
if err != nil {
return err
Expand Down Expand Up @@ -111,7 +112,9 @@ func (workflow *pipelineWorkflow) pipelineUpserter(tokenProvider func(bool) stri
}
buildspecBytes := new(bytes.Buffer)
buildspecBytes.ReadFrom(buildspec)
pipelineParams["DefaultBuildspec"] = buildspecBytes.String()
newlineRegexp := regexp.MustCompile(`\r?\n`)
buildspecString := newlineRegexp.ReplaceAllString(buildspecBytes.String(), "\\n")
pipelineParams["DefaultBuildspec"] = buildspecString

version := workflow.pipelineConfig.MuVersion
if version == "" {
Expand Down
2 changes: 1 addition & 1 deletion workflows/pipeline_upsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestPipelineBucket(t *testing.T) {

stackParams := stackManager.Calls[0].Arguments.Get(1).(map[string]string)
assert.NotNil(stackParams)
assert.Equal("mu-codepipeline", stackParams["BucketPrefix"])
assert.Equal("codepipeline", stackParams["BucketPrefix"])
}

func TestPipelineUpserter(t *testing.T) {
Expand Down

0 comments on commit f73fe21

Please sign in to comment.