Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require confirmation if rolling back more than 9 releases #975

Merged
merged 2 commits into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
* The log level within empire can now be configured when starting the service. [#929](https://github.com/remind101/empire/issues/929)
* The CloudFormation backend now has experimental support for a `Custom::ECSTaskDefinition` resource that greatly reduces the size of generated templates. [#935](https://github.com/remind101/empire/pull/935)
* The Scheduler now has a `Restart` method which will trigger a restart of all the processes within an app. Previously, a "Restart" just re-released the app. Now schedulers like the cloudformation backend can optimize how the restart is handled. [#697](https://github.com/remind101/empire/issues/697)
* `emp run` now publishes an event when it is ran [#954](https://github.com/remind101/empire/pull/954)
* `emp run` now publishes an event when it is ran. [#954](https://github.com/remind101/empire/pull/954)
* `emp rollback` requires confirmation if rolling back more than 9 versions. [#975](https://github.com/remind101/empire/pull/975)

**Bugs**

Expand Down
30 changes: 30 additions & 0 deletions cmd/emp/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"sort"
"strconv"
"strings"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -226,7 +227,36 @@ func runRollback(cmd *Command, args []string) {
os.Exit(2)
}
ver := strings.TrimPrefix(args[0], "v")
rollbackSafetyCheck(ver, appname)

rel, err := client.ReleaseRollback(appname, ver, message)
must(err)
log.Printf("Rolled back %s to v%s as v%d.\n", appname, ver, rel.Version)
}

func rollbackSafetyCheck(verStr, appname string) {
// Grab head release to get the current version
hrels, err := client.ReleaseList(appname, &heroku.ListRange{
Field: "version",
Max: 1,
Descending: true,
})
must(err)

currVer := hrels[0].Version
verNum, err := strconv.Atoi(verStr)
must(err)

diff := currVer - verNum
if diff >= 10 {
var vconfirmation string
fmt.Printf("WARNING. Attempting to rollback %d versions from v%d to v%d.\n", diff, currVer, verNum)
fmt.Printf("To proceed, type v%d: ", verNum)
fmt.Scanln(&vconfirmation)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably re-use the mustConfirm helper here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, perfect

vconfirmation = strings.TrimPrefix(vconfirmation, "v")
if vconfirmation != verStr {
fmt.Println("Rollback confirmation did not match.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use printFatal so it ends up on stderr and colorized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mustConfirm takes care of the error now

$ EMPIRE_API_URL=http://$(docker-machine ip default):8080 ./build/emp rollback v1 -a acme-inc
warning: Attempting to rollback 19 versions from v20 to v1. Type v1 to continue:
> v3838
error: Confirmation did not match "v1".

os.Exit(2)
}
}
}