From 387c86a6bcb1c9c5c7b5f5a2864536af251a84c4 Mon Sep 17 00:00:00 2001 From: Andy Cobaugh <andrew.cobaugh@gmail.com> Date: Tue, 16 Jun 2020 22:38:59 -0400 Subject: [PATCH] ci wait: multiple logical fixes, add flags for project-name, tag, sha, add more informational output --- cmd/ci_wait.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/ci_wait.go b/cmd/ci_wait.go index 58f98d2..5c0eeb9 100644 --- a/cmd/ci_wait.go +++ b/cmd/ci_wait.go @@ -42,6 +42,9 @@ type ciWaitBaseURLData struct { } func init() { + ciWaitCmd.PersistentFlags().String("project-name", os.Getenv("CI_PROJECT_NAME"), "Name of project (CI_PROJECT_NAME)") + ciWaitCmd.PersistentFlags().String("sha", os.Getenv("CI_COMMIT_SHA"), "Commit SHA (CI_COMMIT_SHA)") + ciWaitCmd.PersistentFlags().String("tag", os.Getenv("CI_COMMIT_TAG"), "Commit tag (CI_COMMIT_TAG)") ciWaitCmd.PersistentFlags().StringSliceP("urls", "u", ciWaitURLsDefault, "List of URL templates to check in order to find version endpoint") ciWaitCmd.PersistentFlags().Int("url-max-tries", 10, "Max tries to locate the version endpoint") ciWaitCmd.PersistentFlags().Duration("url-delay", 10*time.Second, "Time to wait between attempts to locate version endpoint") @@ -57,15 +60,14 @@ func init() { func runCiWaitCmd(cmd *cobra.Command, args []string) error { start := time.Now() data := ciWaitBaseURLData{ - Project: os.Getenv("CI_PROJECT_NAME"), + Project: viper.GetString("project-name"), Env: environmentSuffix, } - newCommit := os.Getenv("CI_COMMIT_SHA") - newVersion := os.Getenv("CI_COMMIT_TAG") + newCommit := viper.GetString("sha") + newVersion := viper.GetString("tag") var url string var initialBody []byte - var newBody []byte hc := &http.Client{Timeout: viper.GetDuration("timeout")} log.WithFields(log.Fields{ @@ -138,10 +140,12 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error { "delay": viper.GetDuration("update-delay").String(), }).Infof("Waiting for version update") - cli.Default.Padding = 2 * InitialPadding // wait for version to update for i := 1; i <= viper.GetInt("update-max-tries"); i++ { + cli.Default.Padding = InitialPadding log.WithField("try", i).Infof("Checking URL: %s", url) + cli.Default.Padding = 2 * InitialPadding + // check version url and compare resp, err := hc.Get(url) if err != nil { @@ -190,8 +194,13 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error { } else { log.Warn("No update to version endpoint") } + + log.Infof("Initial: %s", initialBody) + log.Infof("Current: %s", newBody) + + } else { + log.WithFields(log.Fields{"code": resp.StatusCode}).Warn("No OK response") } - log.WithFields(log.Fields{"code": resp.StatusCode}).Warn("No OK response") log.Warnf("Sleeping for %s ...", viper.GetDuration("update-delay").String()) time.Sleep(viper.GetDuration("update-delay")) @@ -200,8 +209,6 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error { log.WithFields(log.Fields{ "url": url, - "old": initialBody, - "new": newBody, "commit": newCommit, "version": newVersion, }).Errorf("Did not observe an update to the version endpoint after %s", time.Since(start).Truncate(time.Millisecond)) @@ -215,7 +222,6 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error { // or 3) if all else fails, newBody is different from oldBody func ciWaitVersionCompare(initialBody []byte, newBody []byte, commit string, version string) bool { var nb map[string]interface{} - err := json.Unmarshal(newBody, &nb) if err != nil { // unable to unmarshal newBody, do a straight string comparison -- GitLab