@@ -6,25 +6,23 @@ import (
6
6
"os"
7
7
"os/exec"
8
8
"os/user"
9
- "path/filepath"
10
9
11
10
"github.com/appscode/go/flags"
12
11
"github.com/appscode/go/log"
13
12
"github.com/appscode/stash/pkg/cmds/docker"
14
13
"github.com/appscode/stash/pkg/restic"
15
14
"github.com/appscode/stash/pkg/util"
16
15
"github.com/spf13/cobra"
17
- core "k8s.io/api/core/v1"
18
16
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
17
)
20
18
21
19
func NewDownloadCmd () * cobra.Command {
22
20
var (
23
- kubeConfig string
24
- repositoryName string
25
- namespace string
26
- localDestination string
27
- restoreOpt = restic.RestoreOptions {
21
+ kubeConfig string
22
+ repositoryName string
23
+ namespace string
24
+ localDirs = & cliLocalDirectories {}
25
+ restoreOpt = restic.RestoreOptions {
28
26
SourceHost : restic .DefaultHost ,
29
27
Destination : docker .DestinationDir ,
30
28
}
@@ -69,16 +67,27 @@ func NewDownloadCmd() *cobra.Command {
69
67
return fmt .Errorf ("setup option for repository failed" )
70
68
}
71
69
72
- // write secret and config
73
- // cleanup whole config/secret dir at the end
74
- defer os .RemoveAll (cliSecretDir )
75
- defer os .RemoveAll (cliConfigDir )
76
- if err = prepareDockerVolumeForRestore (* secret , setupOpt , restoreOpt ); err != nil {
70
+ // write secret and config in a temp dir
71
+ // cleanup whole tempDir dir at the end
72
+ tempDir , err := ioutil .TempDir ("" , "stash-cli" )
73
+ if err != nil {
74
+ return err
75
+ }
76
+ defer os .RemoveAll (tempDir )
77
+
78
+ // prepare local dirs
79
+ if err = localDirs .prepareSecretDir (tempDir , secret ); err != nil {
80
+ return err
81
+ }
82
+ if err = localDirs .prepareConfigDir (tempDir , & setupOpt , & restoreOpt ); err != nil {
83
+ return err
84
+ }
85
+ if err = localDirs .prepareDownloadDir (); err != nil {
77
86
return err
78
87
}
79
88
80
89
// run restore inside docker
81
- if err = runRestoreViaDocker (localDestination ); err != nil {
90
+ if err = runRestoreViaDocker (* localDirs ); err != nil {
82
91
return err
83
92
}
84
93
log .Infof ("Repository %s/%s restored in path %s" , namespace , repositoryName , restoreOpt .Destination )
@@ -89,59 +98,31 @@ func NewDownloadCmd() *cobra.Command {
89
98
cmd .Flags ().StringVar (& kubeConfig , "kubeconfig" , kubeConfig , "Path of the Kube config file." )
90
99
cmd .Flags ().StringVar (& repositoryName , "repository" , repositoryName , "Name of the Repository." )
91
100
cmd .Flags ().StringVar (& namespace , "namespace" , "default" , "Namespace of the Repository." )
92
- cmd .Flags ().StringVar (& localDestination , "destination" , localDestination , "Destination path where snapshot will be restored." )
101
+ cmd .Flags ().StringVar (& localDirs . downloadDir , "destination" , localDirs . downloadDir , "Destination path where snapshot will be restored." )
93
102
94
103
cmd .Flags ().StringVar (& restoreOpt .SourceHost , "host" , restoreOpt .SourceHost , "Name of the source host machine" )
95
104
cmd .Flags ().StringSliceVar (& restoreOpt .RestoreDirs , "directories" , restoreOpt .RestoreDirs , "List of directories to be restored" )
96
105
cmd .Flags ().StringSliceVar (& restoreOpt .Snapshots , "snapshots" , restoreOpt .Snapshots , "List of snapshots to be restored" )
97
106
98
- cmd .Flags ().StringVar (& image .Registry , "docker-registry" , image .Registry , "Docker image registry for unlock job " )
99
- cmd .Flags ().StringVar (& image .Tag , "image-tag" , image .Tag , "Stash image tag for unlock job " )
107
+ cmd .Flags ().StringVar (& image .Registry , "docker-registry" , image .Registry , "Docker image registry" )
108
+ cmd .Flags ().StringVar (& image .Tag , "image-tag" , image .Tag , "Stash image tag" )
100
109
101
110
return cmd
102
111
}
103
112
104
- func prepareDockerVolumeForRestore (secret core.Secret , setupOpt restic.SetupOptions , restoreOpt restic.RestoreOptions ) error {
105
- // write repository secrets
106
- if err := os .MkdirAll (cliSecretDir , 0755 ); err != nil {
107
- return err
108
- }
109
- for key , value := range secret .Data {
110
- if err := ioutil .WriteFile (filepath .Join (cliSecretDir , key ), value , 0755 ); err != nil {
111
- return err
112
- }
113
- }
114
- // write restic options
115
- err := docker .WriteSetupOptionToFile (& setupOpt , filepath .Join (cliConfigDir , docker .SetupOptionsFile ))
116
- if err != nil {
117
- return err
118
- }
119
- return docker .WriteRestoreOptionToFile (& restoreOpt , filepath .Join (cliConfigDir , docker .RestoreOptionsFile ))
120
- }
121
-
122
- func runRestoreViaDocker (localDestination string ) error {
113
+ func runRestoreViaDocker (localDirs cliLocalDirectories ) error {
123
114
// get current user
124
115
currentUser , err := user .Current ()
125
116
if err != nil {
126
117
return err
127
118
}
128
- // if destination flag is not specified, restore in current directory
129
- if localDestination == "" {
130
- if localDestination , err = os .Getwd (); err != nil {
131
- return err
132
- }
133
- }
134
- // create local destination dir
135
- if err := os .MkdirAll (localDestination , 0755 ); err != nil {
136
- return err
137
- }
138
119
args := []string {
139
120
"run" ,
140
121
"--rm" ,
141
122
"-u" , currentUser .Uid ,
142
- "-v" , cliConfigDir + ":" + docker .ConfigDir ,
143
- "-v" , cliSecretDir + ":" + docker .SecretDir ,
144
- "-v" , localDestination + ":" + docker .DestinationDir ,
123
+ "-v" , localDirs . configDir + ":" + docker .ConfigDir ,
124
+ "-v" , localDirs . secretDir + ":" + docker .SecretDir ,
125
+ "-v" , localDirs . downloadDir + ":" + docker .DestinationDir ,
145
126
image .ToContainerImage (),
146
127
"docker" ,
147
128
"download-snapshots" ,
0 commit comments