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 all 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
12 changes: 6 additions & 6 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
54 changes: 54 additions & 0 deletions db/backup_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package db

import (
"time"

"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/io"
scalingo "github.com/Scalingo/go-scalingo"
"github.com/briandowns/spinner"
"github.com/fatih/color"
errgo "gopkg.in/errgo.v1"
)

func CreateBackup(app, addon string) error {
spinner := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
spinner.Suffix = " Schedule a new backup"
spinner.Start()
defer spinner.Stop()

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

for backup.Status != scalingo.BackupStatusDone &&
backup.Status != scalingo.BackupStatusError {
spinner.Lock()
if backup.Status == scalingo.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.BackupShow(app, addon, backup.ID)
if err != nil {
return errgo.Notef(err, "fail to refresh backup state")
}
}
spinner.Stop()

if backup.Status == scalingo.BackupStatusDone {
io.Status(color.New(color.FgGreen).Sprint("Backup successfuly finished"))
} else {
io.Error(color.New(color.FgRed).Sprintf("Backup failed"))
}
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.

Loading