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

feat(*): support restore from backup/snapshot #56

Merged
merged 1 commit into from
May 9, 2018
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
4 changes: 4 additions & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (c *CLI) RegisterCommands() {
cmd.Command("detach", "detach ISO from a virtual machine (server will hard reboot)", serversDetachISO)
cmd.Command("status", "show status of ISO attached to a virtual machine", serversStatusISO)
})
cmd.Command("restore", "restore from backup/snapshot", func(cmd *cli.Cmd) {
cmd.Command("backup", "restore from backup (any data already on the server will be lost)", serversRestoreBackup)
cmd.Command("snapshot", "restore from snapshot (any data already on the server will be lost)", serversRestoreSnapshot)
})
cmd.Command("delete", "delete a virtual machine", serversDelete)
cmd.Command("bandwidth", "list bandwidth used by a virtual machine", serversBandwidth)
cmd.Command("list", "list all active or pending virtual machines on current account", serversList)
Expand Down
26 changes: 26 additions & 0 deletions cmd/commands_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,32 @@ func serversStatusISO(cmd *cli.Cmd) {
}
}

func serversRestoreBackup(cmd *cli.Cmd) {
cmd.Spec = "SUBID -b"
id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see <servers>)")
backupID := cmd.StringOpt("b backup", "", "BACKUPID of backups")

cmd.Action = func() {
if err := GetClient().RestoreBackup(*id, *backupID); err != nil {
log.Fatal(err)
}
fmt.Printf("Virtual machine restored, server will be reboot: %v\n", *id)
}
}

func serversRestoreSnapshot(cmd *cli.Cmd) {
cmd.Spec = "SUBID -s"
id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see <servers>)")
snapshotID := cmd.StringOpt("s snapshot", "", "SNAPSHOTID of snapshots")

cmd.Action = func() {
if err := GetClient().RestoreSnapshot(*id, *snapshotID); err != nil {
log.Fatal(err)
}
fmt.Printf("Virtual machine restored, server will be reboot: %v\n", *id)
}
}

func serversListOS(cmd *cli.Cmd) {
id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see <servers>)")
cmd.Action = func() {
Expand Down
26 changes: 26 additions & 0 deletions lib/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,32 @@ func (c *Client) GetISOStatusofServer(id string) (isoStatus ISOStatus, err error
return isoStatus, nil
}

// RestoreBackup restore the specified backup to the virtual machine
func (c *Client) RestoreBackup(id, backupID string) error {
values := url.Values{
"SUBID": {id},
"BACKUPID": {backupID},
}

if err := c.post(`server/restore_backup`, values, nil); err != nil {
return err
}
return nil
}

// RestoreSnapshot restore the specified snapshot to the virtual machine
func (c *Client) RestoreSnapshot(id, snapshotID string) error {
values := url.Values{
"SUBID": {id},
"SNAPSHOTID": {snapshotID},
}

if err := c.post(`server/restore_snapshot`, values, nil); err != nil {
return err
}
return nil
}

// DeleteServer deletes an existing virtual machine
func (c *Client) DeleteServer(id string) error {
values := url.Values{
Expand Down