Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
[FIX] import droped env argument
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyTobi committed Feb 21, 2019
1 parent 4379983 commit 2e2bb2e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 24 deletions.
62 changes: 49 additions & 13 deletions puppeteer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -37,7 +38,7 @@ func venerableAppName(appName string) string {
return fmt.Sprintf("%s-venerable", appName)
}

func getActionsForApp(appRepo *ApplicationRepo, appName, manifestPath, appPath, stackName string, timeout int, vars []string, varsFiles []string, showLogs bool) []rewind.Action {
func getActionsForApp(appRepo *ApplicationRepo, appName, manifestPath, appPath, stackName string, timeout int, vars []string, varsFiles []string, envs []string, showLogs bool) []rewind.Action {
venName := venerableAppName(appName)
var err error
var curApp, venApp *AppEntity
Expand Down Expand Up @@ -99,7 +100,7 @@ func getActionsForApp(appRepo *ApplicationRepo, appName, manifestPath, appPath,
// push
{
Forward: func() error {
return appRepo.PushApplication(appName, manifestPath, appPath, stackName, timeout, vars, varsFiles, showLogs)
return appRepo.PushApplication(appName, manifestPath, appPath, stackName, timeout, vars, varsFiles, envs, showLogs)
},
ReversePrevious: func() error {
if !haveVenToCleanup {
Expand Down Expand Up @@ -132,11 +133,11 @@ func (plugin CfPuppeteerPlugin) Run(cliConnection plugin.CliConnection, args []s
}

appRepo := NewApplicationRepo(cliConnection)
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, showLogs, err := ParseArgs(args)
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, envs, showLogs, err := ParseArgs(args)
fatalIf(err)

fatalIf((&rewind.Actions{
Actions: getActionsForApp(appRepo, appName, manifestPath, appPath, stackName, timeout, vars, varsFiles, showLogs),
Actions: getActionsForApp(appRepo, appName, manifestPath, appPath, stackName, timeout, vars, varsFiles, envs, showLogs),
RewindFailureMessage: "Oh no. Something's gone wrong. I've tried to roll back but you should check to see if everything is OK.",
}).Execute())

Expand Down Expand Up @@ -179,9 +180,10 @@ func (s *StringSlice) Set(value string) error {
}

//ParseArgs parse all cmd arguments
func ParseArgs(args []string) (string, string, string, int, string, []string, []string, bool, error) {
func ParseArgs(args []string) (string, string, string, int, string, []string, []string, []string, bool, error) {
flags := flag.NewFlagSet("zero-downtime-push", flag.ContinueOnError)

var envs StringSlice
var vars StringSlice
var varsFiles StringSlice

Expand All @@ -190,30 +192,32 @@ func ParseArgs(args []string) (string, string, string, int, string, []string, []
stackName := flags.String("s", "", "name of the stack to use")
timeout := flags.Int("t", 0, "push timout in secounds defualt 60s")
showLogs := flags.Bool("show-app-log", false, "tail and show application log during application start")
flags.Var(&envs, "env", "Variable key value pair for adding dynamic environment variables; can specity multiple times")
flags.Var(&vars, "var", "Variable key value pair for variable substitution, (e.g., name=app1); can specify multiple times")
flags.Var(&varsFiles, "vars-file", "Path to a variable substitution file for manifest; can specify multiple times")

//default start index of parameters is 2 because 1 is the appName
argumentStartIndex := 2
//if first argument is not the appName we have to read the appName out of the manifest
noAppNameProvided := strings.Contains(args[1], "-")
noAppNameProvided, _ := regexp.MatchString("^-[a-z]{0,3}", args[1])
//noAppNameProvided := strings.Contains(args[1], "-")
if noAppNameProvided {
argumentStartIndex = 1
}

err := flags.Parse(args[argumentStartIndex:])
if err != nil {
return "", "", "", *timeout, "", []string{}, []string{}, false, err
return "", "", "", *timeout, "", []string{}, []string{}, []string{}, false, err
}

if *manifestPath == "" {
return "", "", "", *timeout, "", []string{}, []string{}, false, ErrNoManifest
return "", "", "", *timeout, "", []string{}, []string{}, []string{}, false, ErrNoManifest
}

//parse manifest
manifest, err := manifest.ParseManifest(*manifestPath)
if err != nil {
return "", "", "", *timeout, "", []string{}, []string{}, false, ErrManifest
return "", "", "", *timeout, "", []string{}, []string{}, []string{}, false, ErrManifest
}

//set timeout
Expand All @@ -231,12 +235,22 @@ func ParseArgs(args []string) (string, string, string, int, string, []string, []

}

return appName, *manifestPath, *appPath, *timeout, *stackName, vars, varsFiles, *showLogs, nil
//validate envs format
if len(envs) > 0 {
for _, envPair := range envs {
if strings.Contains(envPair, "=") == false {
return "", "", "", *timeout, "", []string{}, []string{}, []string{}, false, ErrWrongEnvFormat
}
}
}

return appName, *manifestPath, *appPath, *timeout, *stackName, vars, varsFiles, envs, *showLogs, nil
}

var (
ErrNoManifest = errors.New("a manifest is required to push this application")
ErrManifest = errors.New("could not parse manifest")
ErrNoManifest = errors.New("a manifest is required to push this application")
ErrManifest = errors.New("could not parse manifest")
ErrWrongEnvFormat = errors.New("--var would be in wrong format, use the vars like key=value")
)

type ApplicationRepo struct {
Expand All @@ -254,7 +268,7 @@ func (repo *ApplicationRepo) RenameApplication(oldName, newName string) error {
return err
}

func (repo *ApplicationRepo) PushApplication(appName, manifestPath, appPath, stackName string, timeout int, vars []string, varsFiles []string, showLogs bool) error {
func (repo *ApplicationRepo) PushApplication(appName, manifestPath, appPath, stackName string, timeout int, vars []string, varsFiles []string, envs []string, showLogs bool) error {
args := []string{"push", appName, "-f", manifestPath, "--no-start"}

if appPath != "" {
Expand Down Expand Up @@ -282,6 +296,11 @@ func (repo *ApplicationRepo) PushApplication(appName, manifestPath, appPath, sta
return err
}

envErr := repo.SetEnvironmentVariables(appName, envs)
if envErr != nil {
return envErr
}

if showLogs {
app, err := repo.conn.GetApp(appName)
if err != nil {
Expand Down Expand Up @@ -327,6 +346,23 @@ func (repo *ApplicationRepo) PushApplication(appName, manifestPath, appPath, sta
return nil
}

//SetEnvironmentVariable set passed envs with set-env to set variables dynamically
func (repo *ApplicationRepo) SetEnvironmentVariables(appName string, envs []string) error {
varArgs := []string{"set-env", appName}
//set all variables passed by --var
for _, envPair := range envs {
tmpArgs := make([]string, len(varArgs))
copy(tmpArgs, varArgs)
newArgs := strings.Split(envPair, "=")
tmpArgs = append(tmpArgs, newArgs...)
_, err := repo.conn.CliCommand(tmpArgs...)
if err != nil {
return err
}
}
return nil
}

func (repo *ApplicationRepo) DeleteApplication(appName string) error {
_, err := repo.conn.CliCommand("delete", appName, "-f")
return err
Expand Down
52 changes: 41 additions & 11 deletions puppeteer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestPuppeteer(t *testing.T) {

var _ = Describe("Flag Parsing", func() {
It("parses args without appName", func() {
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, showLogs, err := ParseArgs(
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, envs, showLogs, err := ParseArgs(
[]string{
"zero-downtime-push",
"-f", "./fixtures/manifest.yml",
Expand All @@ -31,6 +31,8 @@ var _ = Describe("Flag Parsing", func() {
"-var", "foo=bar",
"-var", "baz=bob",
"-vars-file", "vars.yml",
"-env", "foo=bar",
"-env", "baz=bob",
},
)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -41,12 +43,13 @@ var _ = Describe("Flag Parsing", func() {
Expect(stackName).To(Equal("stack-name"))
Expect(vars).To(Equal([]string{"foo=bar", "baz=bob"}))
Expect(varsFiles).To(Equal([]string{"vars.yml"}))
Expect(envs).To(Equal([]string{"foo=bar", "baz=bob"}))
Expect(showLogs).To(Equal(false))
Expect(timeout).To(Equal(120))
})

It("parses a all args without timeout", func() {
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, showLogs, err := ParseArgs(
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, envs, showLogs, err := ParseArgs(
[]string{
"zero-downtime-push",
"appname",
Expand All @@ -56,6 +59,8 @@ var _ = Describe("Flag Parsing", func() {
"-var", "foo=bar",
"-var", "baz=bob",
"-vars-file", "vars.yml",
"-env", "foo=bar",
"-env", "baz=bob",
},
)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -66,12 +71,13 @@ var _ = Describe("Flag Parsing", func() {
Expect(stackName).To(Equal("stack-name"))
Expect(vars).To(Equal([]string{"foo=bar", "baz=bob"}))
Expect(varsFiles).To(Equal([]string{"vars.yml"}))
Expect(envs).To(Equal([]string{"foo=bar", "baz=bob"}))
Expect(showLogs).To(Equal(false))
Expect(timeout).To(Equal(2))
})

It("parses a all args without timeout and no manifest timeout", func() {
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, showLogs, err := ParseArgs(
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, envs, showLogs, err := ParseArgs(
[]string{
"zero-downtime-push",
"appname",
Expand All @@ -81,6 +87,8 @@ var _ = Describe("Flag Parsing", func() {
"-var", "foo=bar",
"-var", "baz=bob",
"-vars-file", "vars.yml",
"-env", "foo=bar",
"-env", "baz=bob",
},
)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -91,12 +99,13 @@ var _ = Describe("Flag Parsing", func() {
Expect(stackName).To(Equal("stack-name"))
Expect(vars).To(Equal([]string{"foo=bar", "baz=bob"}))
Expect(varsFiles).To(Equal([]string{"vars.yml"}))
Expect(envs).To(Equal([]string{"foo=bar", "baz=bob"}))
Expect(showLogs).To(Equal(false))
Expect(timeout).To(Equal(60))
})

It("parses a complete set of args", func() {
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, showLogs, err := ParseArgs(
appName, manifestPath, appPath, timeout, stackName, vars, varsFiles, envs, showLogs, err := ParseArgs(
[]string{
"zero-downtime-push",
"appname",
Expand All @@ -107,6 +116,8 @@ var _ = Describe("Flag Parsing", func() {
"-var", "foo=bar",
"-var", "baz=bob",
"-vars-file", "vars.yml",
"-env", "foo=bar",
"-env", "baz=bob",
},
)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -117,12 +128,31 @@ var _ = Describe("Flag Parsing", func() {
Expect(stackName).To(Equal("stack-name"))
Expect(vars).To(Equal([]string{"foo=bar", "baz=bob"}))
Expect(varsFiles).To(Equal([]string{"vars.yml"}))
Expect(envs).To(Equal([]string{"foo=bar", "baz=bob"}))
Expect(showLogs).To(Equal(false))
Expect(timeout).To(Equal(120))
})

It("parses args without appName and wrong envs format", func() {
_, _, _, _, _, _, _, _, _, err := ParseArgs(
[]string{
"zero-downtime-push",
"-f", "./fixtures/manifest.yml",
"-p", "app-path",
"-s", "stack-name",
"-t", "120",
"-var", "foo=bar",
"-var", "baz bob",
"-vars-file", "vars.yml",
"-env", "foo=bar",
"-env", "baz bob",
},
)
Expect(err).To(MatchError(ErrWrongEnvFormat))
})

It("requires a manifest", func() {
_, _, _, _, _, _, _, _, err := ParseArgs(
_, _, _, _, _, _, _, _, _, err := ParseArgs(
[]string{
"zero-downtime-push",
"appname",
Expand Down Expand Up @@ -250,7 +280,7 @@ var _ = Describe("ApplicationRepo", func() {

Describe("PushApplication", func() {
It("pushes an application with both a manifest and a path", func() {
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "/path/to/the/app", "", 60, []string{}, []string{}, false)
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "/path/to/the/app", "", 60, []string{}, []string{}, []string{}, false)
Expect(err).ToNot(HaveOccurred())

Expect(cliConn.CliCommandCallCount()).To(Equal(2))
Expand All @@ -266,7 +296,7 @@ var _ = Describe("ApplicationRepo", func() {
})

It("pushes an application with only a manifest", func() {
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "", "", 60, []string{}, []string{}, false)
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "", "", 60, []string{}, []string{}, []string{}, false)
Expect(err).ToNot(HaveOccurred())

Expect(cliConn.CliCommandCallCount()).To(Equal(2))
Expand All @@ -281,7 +311,7 @@ var _ = Describe("ApplicationRepo", func() {
})

It("pushes an application with a stack", func() {
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "/path/to/the/app", "stackName", 60, []string{}, []string{}, false)
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "/path/to/the/app", "stackName", 60, []string{}, []string{}, []string{}, false)
Expect(err).ToNot(HaveOccurred())

Expect(cliConn.CliCommandCallCount()).To(Equal(2))
Expand All @@ -298,10 +328,10 @@ var _ = Describe("ApplicationRepo", func() {
})

It("pushes an application with variables", func() {
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "", "", 60, []string{"foo=bar", "baz=bob"}, []string{"vars.yml"}, false)
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "", "", 60, []string{"foo=bar", "baz=bob"}, []string{"vars.yml"}, []string{"foo=bar"}, false)
Expect(err).ToNot(HaveOccurred())

Expect(cliConn.CliCommandCallCount()).To(Equal(2))
Expect(cliConn.CliCommandCallCount()).To(Equal(3))
args := cliConn.CliCommandArgsForCall(0)
Expect(args).To(Equal([]string{
"push",
Expand All @@ -318,7 +348,7 @@ var _ = Describe("ApplicationRepo", func() {
It("returns errors from the push", func() {
cliConn.CliCommandReturns([]string{}, errors.New("bad app"))

err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "/path/to/the/app", "", 60, []string{}, []string{}, false)
err := repo.PushApplication("appName", "/path/to/a/manifest.yml", "/path/to/the/app", "", 60, []string{}, []string{}, []string{}, false)
Expect(err).To(MatchError("bad app"))
})
})
Expand Down

0 comments on commit 2e2bb2e

Please sign in to comment.