-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunlock.go
More file actions
168 lines (140 loc) · 4.46 KB
/
unlock.go
File metadata and controls
168 lines (140 loc) · 4.46 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkg
import (
"context"
"fmt"
"os"
"path/filepath"
"stash.appscode.dev/apimachinery/apis/stash/v1alpha1"
cs "stash.appscode.dev/apimachinery/client/clientset/versioned"
"stash.appscode.dev/apimachinery/pkg/restic"
"stash.appscode.dev/stash/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)
type unlockOptions struct {
kubeClient *kubernetes.Clientset
config *rest.Config
repo *v1alpha1.Repository
}
func NewCmdUnlockRepository(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
opt := unlockOptions{}
cmd := &cobra.Command{
Use: "unlock",
Short: `Unlock restic repository`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "" {
return fmt.Errorf("repository name not found")
}
repositoryName := args[0]
var err error
opt.config, err = clientGetter.ToRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to read kubeconfig")
}
namespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
opt.kubeClient, err = kubernetes.NewForConfig(opt.config)
if err != nil {
return err
}
stashClient, err = cs.NewForConfig(opt.config)
if err != nil {
return err
}
// get source repository
opt.repo, err = stashClient.StashV1alpha1().Repositories(namespace).Get(context.TODO(), repositoryName, metav1.GetOptions{})
if err != nil {
return err
}
if opt.repo.Spec.Backend.Local != nil {
return opt.unlockLocalRepository()
}
return opt.unlockRepository()
},
}
return cmd
}
func (opt *unlockOptions) unlockLocalRepository() error {
// get the pod that mount this repository as volume
pod, err := getBackendMountingPod(opt.kubeClient, opt.repo)
if err != nil {
return err
}
command := []string{"/stash-enterprise", "unlock"}
command = append(command, "--repo-name="+opt.repo.Name)
command = append(command, "--repo-namespace="+opt.repo.Namespace)
_, err = execCommandOnPod(opt.kubeClient, opt.config, pod, command)
if err != nil {
return err
}
klog.Infof("Repository %s/%s has been unlocked successfully", opt.repo.Namespace, opt.repo.Name)
return nil
}
func (opt *unlockOptions) unlockRepository() error {
// get source repository secret
secret, err := opt.kubeClient.CoreV1().Secrets(namespace).Get(context.TODO(), opt.repo.Spec.Backend.StorageSecretName, metav1.GetOptions{})
if err != nil {
return err
}
if err = os.MkdirAll(ScratchDir, 0o755); err != nil {
return err
}
defer removeDirWithLogErr(ScratchDir)
// configure restic wrapper
extraOpt := util.ExtraOptions{
StorageSecret: secret,
ScratchDir: ScratchDir,
}
// configure setupOption
setupOpt, err := util.SetupOptionsForRepository(*opt.repo, extraOpt)
if err != nil {
return fmt.Errorf("setup option for repository failed")
}
// init restic wrapper
resticWrapper, err := restic.NewResticWrapper(setupOpt)
if err != nil {
return err
}
localDirs := &cliLocalDirectories{
configDir: filepath.Join(ScratchDir, configDirName),
}
// dump restic's environments into `restic-env` file.
// we will pass this env file to restic docker container.
err = resticWrapper.DumpEnv(localDirs.configDir, ResticEnvs)
if err != nil {
return err
}
extraArgs := []string{
"--no-cache",
}
// For TLS secured Minio/REST server, specify cert path
if resticWrapper.GetCaPath() != "" {
extraArgs = append(extraArgs, "--cacert", resticWrapper.GetCaPath())
}
// run unlock inside docker
if err = runCmdViaDocker(*localDirs, "unlock", extraArgs); err != nil {
return err
}
klog.Infof("Repository %s/%s has been unlocked successfully", opt.repo.Namespace, opt.repo.Name)
return nil
}