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

Add the backups-create command #452

Merged
merged 8 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
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: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### To be Released

* Bugfix if notifier send all events [#453](https://github.com/Scalingo/cli/pull/453)
* Only fill regions cache for regional commands [#449](https://github.com/Scalingo/cli/pull/449)
* Add `backups-create` command [#452](https://github.com/Scalingo/cli/pull/452)
* Deprecate `backup-download` in favor of `backups-download` [#452](https://github.com/Scalingo/cli/pull/452)

### 1.14.1

Expand Down
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 39 additions & 6 deletions cmd/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (

"github.com/Scalingo/cli/appdetect"
"github.com/Scalingo/cli/db"
"github.com/Scalingo/cli/io"
"github.com/urfave/cli"
)

var (
BackupListCommand = cli.Command{
backupsListCommand = cli.Command{
Name: "backups",
Category: "Addons",
Usage: "List backups for an addon",
Flags: []cli.Flag{appFlag, addonFlag},
Description: ` List all available backups for an addon:
$ scalingo --app myapp --addon addon_uuid backups
$ scalingo --app my-app --addon addon_uuid backups

# See also 'addons' and 'backup-download'
# See also 'addons' and 'backups-download'
`,
Action: func(c *cli.Context) {
currentApp := appdetect.CurrentApp(c)
Expand All @@ -29,8 +30,28 @@ var (
},
}

BackupDownloadCommand = cli.Command{
Name: "backup-download",
backupsCreateCommand = cli.Command{
Name: "backups-create",
Category: "Addons",
Usage: "Ask for a new backup",
Flags: []cli.Flag{appFlag, addonFlag},
Description: ` Ask for a new backup of your addon:
$ scalingo --app my-app --addon addon_uuid backups-download --backup my_backup

# See also 'backups' and 'addons'`,
Action: func(c *cli.Context) {
currentApp := appdetect.CurrentApp(c)
addon := addonName(c)

err := db.CreateBackup(currentApp, addon)
if err != nil {
errorQuit(err)
}
},
}

backupsDownloadCommand = cli.Command{
Name: "backups-download",
Category: "Addons",
Usage: "Download a backup",
Flags: []cli.Flag{appFlag, addonFlag, cli.StringFlag{
Expand All @@ -44,7 +65,7 @@ var (
Usage: "Do not show progress bar and loading messages",
}},
Description: ` Download a specific backup:
$ scalingo -a myapp --addon addon_uuid backup-download --backup my_backup
$ scalingo --app my-app --addon addon_uuid backups-download --backup my_backup

# See also 'backups' and 'addons'`,
Action: func(c *cli.Context) {
Expand All @@ -67,4 +88,16 @@ var (
}
},
}

backupDownloadCommand = cli.Command{
Name: "backup-download",
Category: backupsDownloadCommand.Category,
Usage: backupsDownloadCommand.Usage,
Description: backupsDownloadCommand.Description,
Before: func(*cli.Context) error {
io.Warningf("DEPRECATED: please use backups-download instead of this command\n\n")
return nil
},
Action: backupsDownloadCommand.Action,
}
)
6 changes: 4 additions & 2 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ var (
InfluxDBConsoleCommand,

// Backups
BackupListCommand,
BackupDownloadCommand,
backupsListCommand,
backupsCreateCommand,
backupsDownloadCommand,
backupDownloadCommand,

// Alerts
alertsListCommand,
Expand Down
21 changes: 21 additions & 0 deletions db/backup_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package db

import (
"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/io"
errgo "gopkg.in/errgo.v1"
)

func CreateBackup(app, addon string) error {
client, err := config.ScalingoClient()
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
}
err = client.BackupCreate(app, addon)
if err != nil {
return err
}

io.Status("Successfully ordered to make a backup")
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if we could follow the backup progression.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well actually it was not really well designed. The backend should have returned 202 and an operation URL in the Location header. Then we could have follow the process... ^^

That being said I can add a backups-show command and update the status message to state:

Successfully scheduled a new backup. Type "scalingo backups-show backup-id" to follow the progress

If it's what you want, I would prefer to do it in a separate PR. OK?

Copy link
Contributor

Choose a reason for hiding this comment

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

	for !backup.IsFinished() {
		spinner.Lock()
		if backup.Status == types.BackupStatusScheduled {
			spinner.Suffix = " Waiting for the backup to start"
		} else {
			spinner.Suffix = " Waiting for the backup to finish"
		}
		spinner.Unlock()

		time.Sleep(1 * time.Second)

		backup, err = client.BackupGet(dbId, backup.ID.Hex())
		if err != nil {
			return errors.Wrap(err, "Fail to refresh backup state")
		}
	}

This is what i did for the db-cli. I think that this might be enough no ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean BackupGet or GetBackup? 😆

Copy link
Member Author

Choose a reason for hiding this comment

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

But yes, good idea!

return nil
}
2 changes: 1 addition & 1 deletion db/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ListBackups(app, addon string) error {
}
backups, err := client.BackupList(app, addon)
if err != nil {
return errgo.Notef(err, "fail to least backups")
return errgo.Notef(err, "fail to list backups")
}

t := tablewriter.NewWriter(os.Stdout)
Expand Down
4 changes: 4 additions & 0 deletions io/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func Warning(args ...interface{}) {
fmt.Println(args...)
}

func Warningf(format string, args ...interface{}) {
fmt.Printf(" /!\\ "+format, args...)
}

func Status(args ...interface{}) {
fmt.Print("-----> ")
fmt.Println(args...)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.