forked from cfregly/Nut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.go
71 lines (59 loc) · 1.5 KB
/
restore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package commands
import (
"errors"
"flag"
"fmt"
"github.com/PagerDuty/nut/container"
"github.com/mitchellh/cli"
log "github.com/sirupsen/logrus"
"strings"
)
type RestoreCommand struct{}
func Restore() (cli.Command, error) {
command := &RestoreCommand{}
return command, nil
}
func (command *RestoreCommand) Help() string {
helpText := `
Usage: nut restore [options] <container> <image>
nut restore is used to create container from archived images
-sudo Use sudo while invoking tar
`
return strings.TrimSpace(helpText)
}
func (command *RestoreCommand) Synopsis() string {
return "Create container from tarball image"
}
func (command *RestoreCommand) Run(args []string) int {
flagSet := flag.NewFlagSet("restore", flag.ExitOnError)
flagSet.Usage = func() { fmt.Println(command.Help()) }
sudo := flagSet.Bool("sudo", false, "Use sudo while invoking tar")
if err := flagSet.Parse(args); err != nil {
log.Errorln(err)
return -1
}
args = flagSet.Args()
if len(args) != 2 {
log.Errorln(errors.New("Insufficient argument. Please pass container name and image file name"))
return -1
}
i, err := container.NewImage(args[0], args[1])
if err != nil {
log.Errorln(err)
return -1
}
if err := i.Decompress(*sudo); err != nil {
log.Errorf("Failed to restore container. Error: %s\n", err)
return -1
}
ct, err := container.NewContainer(args[0])
if err != nil {
log.Errorln(err)
return -1
}
if err := ct.UpdateUTS(args[0]); err != nil {
log.Errorln(err)
return -1
}
return 0
}