Commit b5627235 authored by Andy Cobaugh's avatar Andy Cobaugh

ci wait: add option to wait for the rollout to complete

parent 85d4c6f7
......@@ -23,37 +23,7 @@ var ciWaitCmd = &cobra.Command{
Short: "Wait for a deployment to be deployed by watching the live version endpoint",
RunE: runCiWaitCmd,
PreRunE: func(cmd *cobra.Command, args []string) error {
err := viper.BindPFlag("url-max-tries", cmd.PersistentFlags().Lookup("url-max-tries"))
if err != nil {
return err
}
err = viper.BindPFlag("url-delay", cmd.PersistentFlags().Lookup("url-delay"))
if err != nil {
return err
}
err = viper.BindPFlag("update-max-tries", cmd.PersistentFlags().Lookup("update-max-tries"))
if err != nil {
return err
}
err = viper.BindPFlag("update-delay", cmd.PersistentFlags().Lookup("update-delay"))
if err != nil {
return err
}
err = viper.BindPFlag("timeout", cmd.PersistentFlags().Lookup("timeout"))
if err != nil {
return err
}
err = viper.BindPFlag("urls", cmd.PersistentFlags().Lookup("urls"))
if err != nil {
return err
}
return nil
return viper.BindPFlags(cmd.PersistentFlags())
},
}
......@@ -78,6 +48,9 @@ func init() {
ciWaitCmd.PersistentFlags().Int("update-max-tries", 30, "Max tries to check version endpoint for the update")
ciWaitCmd.PersistentFlags().Duration("update-delay", 10*time.Second, "Time to wait between checking version endpoint for update")
ciWaitCmd.PersistentFlags().Duration("timeout", 2*time.Second, "Time to wait for every HTTP request")
ciWaitCmd.PersistentFlags().Bool("wait-for-rollout", true, "Wait for the rollout to finish by repeatedly checking the version endpoint every [rollout-delay]s until we see the new version [rollout-count] times in a row")
ciWaitCmd.PersistentFlags().Duration("rollout-delay", 15*time.Second, "Time to wait between tries when waiting for rollout")
ciWaitCmd.PersistentFlags().Int("rollout-count", 5, "Number of successive requests that must return the new version before the rollout is considered done")
ciCmd.AddCommand(ciWaitCmd)
}
......@@ -185,6 +158,27 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
}
if ciWaitVersionCompare(initialBody, newBody, newCommit, newVersion) {
log.Info("New version is live")
seen := 1
// optionally wait for the rollout
for seen < viper.GetInt("rollout-count") {
log.WithFields(log.Fields{"seen": seen, "delay": viper.GetDuration("rollout-delay"), "count": viper.GetInt("rollout-count")}).Info("Waiting for rollout")
time.Sleep(viper.GetDuration("rollout-delay"))
resp, err := hc.Get(url)
if err != nil {
log.WithError(err).WithField("url", url).Error("Error fetching URL")
continue
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK && ciWaitVersionCompare(initialBody, newBody, newCommit, newVersion) {
seen = +1
log.Info("NEW")
continue
}
// we consider non-200 statuses and a response with the old version to be failures
// reset seen back to 0 and continue
seen = 0
log.Warn("OLD")
}
return nil
} else {
log.Warn("No update to version endpoint")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment