Skip to content

Commit

Permalink
fixup the approach to default buildspec-test
Browse files Browse the repository at this point in the history
  • Loading branch information
cplee committed Oct 31, 2018
1 parent a36d213 commit d481a0e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 69 deletions.
4 changes: 2 additions & 2 deletions templates/assets/cloudformation/artifact-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ Resources:
{{if .TestImage -}}
TestImage: {{ .TestImage }}
{{- end}}
DefaultBuildspec: "{{ .DefaultBuildspec }}"
MuDownloadVersion: "{{ .MuDownloadVersion }}"
MuFile: "mu.yml"
MuFilename: "{{ .MuFilename }}"
MuBasedir: "{{ .MuBasedir }}"
CodePipelineKeyArn: !GetAtt PipelineRoleset.Outputs.CodePipelineKeyArn
CodePipelineRoleArn: !GetAtt PipelineRoleset.Outputs.CodePipelineRoleArn
CodeBuildCIRoleArn: !GetAtt PipelineRoleset.Outputs.CodeBuildCIRoleArn
Expand Down
47 changes: 23 additions & 24 deletions templates/assets/cloudformation/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ Parameters:
Type: String
Default: "mu-linux-amd64"
Description: The name of the mu file to download to install
DefaultBuildspec:
Type: String
Description: The default buildspec to use
MuFile:
MuBasedir:
Type: String
Description: Path to mu.yml, relative to GitHubRepo
Default: "."
MuFilename:
Type: String
Description: Name of mu config file
Default: "mu.yml"
AcptEnv:
Type: String
Expand Down Expand Up @@ -225,6 +226,7 @@ Resources:
Image: !Sub ${BuildImage}
Source:
Type: CODEPIPELINE
BuildSpec: !Sub ${MuBasedir}/buildspec.yml
TimeoutInMinutes: 30
CodeBuildImage:
Type: AWS::CodeBuild::Project
Expand Down Expand Up @@ -257,10 +259,11 @@ Resources:
commands:
- curl -sL ${MuDownloadBaseurl}/v${MuDownloadVersion}/${MuDownloadFile} -o /usr/bin/mu
- chmod +rx /usr/bin/mu
- mu -c ${MuFile} svc push -k ${CodePipelineKeyArn}
- mu -c ${MuBasedir}/${MuFilename} init
- mu -c ${MuBasedir}/${MuFilename} svc push -k ${CodePipelineKeyArn}
artifacts:
files:
- ${MuFile}
- ${MuBasedir}/${MuFilename}
TimeoutInMinutes: !Ref PipelineBuildTimeout
DeployAcceptance:
Type: AWS::CodeBuild::Project
Expand All @@ -286,20 +289,17 @@ Resources:
Type: CODEPIPELINE
BuildSpec: !Sub |
version: 0.2
env:
variables:
DEFAULT_BUILDSPEC: "${DefaultBuildspec}"
phases:
build:
commands:
- curl -sL ${MuDownloadBaseurl}/v${MuDownloadVersion}/${MuDownloadFile} -o /usr/bin/mu
- chmod +rx /usr/bin/mu
- mu -c ${MuFile} --assume-role ${MuAcptRoleArn} --disable-iam env up ${AcptEnv}
- mu -c ${MuFile} --assume-role ${MuAcptRoleArn} --disable-iam db up ${AcptEnv}
- mu -c ${MuFile} --assume-role ${MuAcptRoleArn} --disable-iam svc deploy ${AcptEnv}
- mu -c ${MuFile} --assume-role ${MuAcptRoleArn} env show ${AcptEnv} -f json > env.json
- mu -c ${MuFile} --assume-role ${MuAcptRoleArn} env show ${AcptEnv} -f shell > mu-env.sh
- mv buildspec-test.yml buildspec.yml || echo "$DEFAULT_BUILDSPEC" > buildspec.yml
- mu -c ${MuBasedir}/${MuFilename} init
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuAcptRoleArn} --disable-iam env up ${AcptEnv}
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuAcptRoleArn} --disable-iam db up ${AcptEnv}
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuAcptRoleArn} --disable-iam svc deploy ${AcptEnv}
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuAcptRoleArn} env show ${AcptEnv} -f json > env.json
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuAcptRoleArn} env show ${AcptEnv} -f shell > mu-env.sh
artifacts:
files:
- '**/*'
Expand All @@ -323,6 +323,7 @@ Resources:
Image: !Sub ${TestImage}
Source:
Type: CODEPIPELINE
BuildSpec: !Sub ${MuBasedir}/buildspec-test.yml
TimeoutInMinutes: !Ref PipelineBuildAcceptanceTimeout
DeployProduction:
Type: AWS::CodeBuild::Project
Expand All @@ -348,20 +349,17 @@ Resources:
Type: CODEPIPELINE
BuildSpec: !Sub |
version: 0.2
env:
variables:
DEFAULT_BUILDSPEC: "${DefaultBuildspec}"
phases:
build:
commands:
- curl -sL ${MuDownloadBaseurl}/v${MuDownloadVersion}/${MuDownloadFile} -o /usr/bin/mu
- chmod +rx /usr/bin/mu
- mu -c ${MuFile} --assume-role ${MuProdRoleArn} --disable-iam env up ${ProdEnv}
- mu -c ${MuFile} --assume-role ${MuProdRoleArn} --disable-iam db up ${ProdEnv}
- mu -c ${MuFile} --assume-role ${MuProdRoleArn} --disable-iam svc deploy ${ProdEnv}
- mu -c ${MuFile} --assume-role ${MuProdRoleArn} env show ${ProdEnv} -f json > env.json
- mu -c ${MuFile} --assume-role ${MuProdRoleArn} env show ${ProdEnv} -f shell > mu-env.sh
- mv buildspec-prod.yml buildspec.yml || echo "$DEFAULT_BUILDSPEC" > buildspec.yml
- mu -c ${MuBasedir}/${MuFilename} init
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuProdRoleArn} --disable-iam env up ${ProdEnv}
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuProdRoleArn} --disable-iam db up ${ProdEnv}
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuProdRoleArn} --disable-iam svc deploy ${ProdEnv}
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuProdRoleArn} env show ${ProdEnv} -f json > env.json
- mu -c ${MuBasedir}/${MuFilename} --assume-role ${MuProdRoleArn} env show ${ProdEnv} -f shell > mu-env.sh
artifacts:
files:
- '**/*'
Expand All @@ -382,6 +380,7 @@ Resources:
Image: !Sub ${TestImage}
Source:
Type: CODEPIPELINE
BuildSpec: !Sub ${MuBasedir}/buildspec-prod.yml
TimeoutInMinutes: !Ref PipelineBuildProductionTimeout
Pipeline:
Type: AWS::CodePipeline::Pipeline
Expand Down
47 changes: 22 additions & 25 deletions workflows/config_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,9 @@ func (workflow *configWorkflow) configInitialize(config *common.Config, createEn
return fmt.Errorf("Unable to determine git repo to use for the pipeline. Have you initialized your repo and pushed yet? %s", config.Repo.Slug)
}

// unless force is set, don't overwrite...make sure files don't exist
if forceOverwrite == false {
log.Debugf("Checking for existing config file at %s/mu.yml", basedir)
if _, err := os.Stat(fmt.Sprintf("%s/mu.yml", basedir)); err == nil {
return fmt.Errorf("Config file already exists - '%s/mu.yml'. Use --force to overwrite", basedir)
}

log.Debugf("Checking for existing buildspec file at %s/buildspec.yml", basedir)
if _, err := os.Stat(fmt.Sprintf("%s/buildspec.yml", basedir)); err == nil {
return fmt.Errorf("buildspec file already exists - '%s/buildspec.yml'. Use --force to overwrite", basedir)
}
}

// write config
config.Service.Port = listenPort
//config.Service.Name = config.Repo.Name
config.Service.PathPatterns = []string{"/*"}
config.Service.Pipeline.Source.Repo = config.Repo.Slug
config.Service.Pipeline.Source.Provider = config.Repo.Provider

if createEnvironment && len(config.Environments) == 0 {
config.Environments = append(config.Environments,
Expand All @@ -69,11 +53,15 @@ func (workflow *configWorkflow) configInitialize(config *common.Config, createEn
return err
}

log.Noticef("Writing config to '%s/mu.yml'", basedir)

err = ioutil.WriteFile(fmt.Sprintf("%s/mu.yml", basedir), configBytes, 0600)
if err != nil {
return err
// unless force is set, don't overwrite...make sure files don't exist
if _, err := os.Stat(fmt.Sprintf("%s/mu.yml", basedir)); !forceOverwrite && err == nil {
log.Infof("Config file already exists - '%s/mu.yml'. Use --force to overwrite", basedir)
} else {
log.Noticef("Writing config to '%s/mu.yml'", basedir)
err = ioutil.WriteFile(fmt.Sprintf("%s/mu.yml", basedir), configBytes, 0600)
if err != nil {
return err
}
}

// write buildspec
Expand All @@ -84,11 +72,20 @@ func (workflow *configWorkflow) configInitialize(config *common.Config, createEn
}
buildspecBytes := []byte(buildspec)

log.Noticef("Writing buildspec to '%s/buildspec.yml'", basedir)
for _, path := range []string{
"buildspec.yml", "buildspec-test.yml", "buildspec-prod.yml",
} {
abspath := fmt.Sprintf("%s/%s", basedir, path)
if _, err := os.Stat(abspath); !forceOverwrite && err == nil {
log.Infof("buildspec file already exists - '%s'. Use --force to overwrite", abspath)
continue
}

err = ioutil.WriteFile(fmt.Sprintf("%s/buildspec.yml", basedir), buildspecBytes, 0600)
if err != nil {
return err
log.Noticef("Writing buildspec to '%s'", abspath)
err = ioutil.WriteFile(fmt.Sprintf("%s", abspath), buildspecBytes, 0600)
if err != nil {
return err
}
}

return nil
Expand Down
9 changes: 5 additions & 4 deletions workflows/config_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/stelligent/mu/common"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"testing"

"github.com/stelligent/mu/common"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

func TestNewConfigInitializer(t *testing.T) {
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestNewConfigInitializer_FileExists(t *testing.T) {
}

err = workflow.configInitialize(config, false, 80, false)()
assert.NotNil(err)
assert.Nil(err)

err = workflow.configInitialize(config, false, 3000, true)()
assert.Nil(err)
Expand Down
17 changes: 3 additions & 14 deletions workflows/pipeline_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package workflows

import (
"fmt"
"regexp"
"path"
"strconv"
"strings"

"github.com/stelligent/mu/common"
"github.com/stelligent/mu/templates"
)

// NewPipelineUpserter create a new workflow for upserting a pipeline
Expand Down Expand Up @@ -270,7 +269,8 @@ func PipelineParams(pipelineConfig *common.Pipeline, namespace string, serviceNa

params["Namespace"] = namespace
params["ServiceName"] = serviceName
params["MuFile"] = muFile
params["MuFilename"] = path.Base(muFile)
params["MuBasedir"] = path.Dir(muFile)
params["SourceProvider"] = pipelineConfig.Source.Provider
params["SourceRepo"] = pipelineConfig.Source.Repo

Expand Down Expand Up @@ -299,17 +299,6 @@ func PipelineParams(pipelineConfig *common.Pipeline, namespace string, serviceNa
params["EnableAcptStage"] = strconv.FormatBool(!pipelineConfig.Acceptance.Disabled)
params["EnableProdStage"] = strconv.FormatBool(!pipelineConfig.Production.Disabled)

// get default buildspec
buildspec, err := templates.GetAsset(common.TemplateBuildspec,
templates.ExecuteTemplate(nil))
if err != nil {
return err
}
newlineRegexp := regexp.MustCompile(`\r?\n`)
buildspecString := newlineRegexp.ReplaceAllString(buildspec, "\\n")

params["DefaultBuildspec"] = buildspecString

version := pipelineConfig.MuVersion
if version == "" {
version = common.GetVersion()
Expand Down

0 comments on commit d481a0e

Please sign in to comment.