/
spec_manager.go
204 lines (172 loc) · 5.22 KB
/
spec_manager.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package spec
import (
"errors"
"os"
"path/filepath"
"github.com/joomcode/errorx"
"github.com/luyomo/OhMyTiUP/pkg/meta"
"github.com/luyomo/OhMyTiUP/pkg/tui"
"github.com/luyomo/OhMyTiUP/pkg/utils"
"github.com/luyomo/OhMyTiUP/pkg/version"
perrs "github.com/pingcap/errors"
"gopkg.in/yaml.v2"
)
var (
errNS = errorx.NewNamespace("spec")
// ErrCreateDirFailed is ErrCreateDirFailed
ErrCreateDirFailed = errNS.NewType("create_dir_failed")
// ErrSaveMetaFailed is ErrSaveMetaFailed
ErrSaveMetaFailed = errNS.NewType("save_meta_failed")
)
const (
// metaFileName is the file name of the meta file.
metaFileName = "meta.yaml"
// PatchDirName is the directory to store patch file eg. {PatchDirName}/tidb-hotfix.tar.gz
PatchDirName = "patch"
// BackupDirName is the directory to save backup files.
BackupDirName = "backup"
)
//revive:disable
// SpecManager control management of spec meta data.
type SpecManager struct {
base string
newMeta func() Metadata
}
// NewSpec create a spec instance.
func NewSpec(base string, newMeta func() Metadata) *SpecManager {
return &SpecManager{
base: base,
newMeta: newMeta,
}
}
// NewMetadata alloc a Metadata according the type.
func (s *SpecManager) NewMetadata() Metadata {
return s.newMeta()
}
// Path returns the full path to a subpath (file or directory) of a
// cluster, it is a subdir in the profile dir of the user, with the cluster name
// as its name.
func (s *SpecManager) Path(cluster string, subpath ...string) string {
if cluster == "" {
// keep the same behavior with legacy version of TiUP, we could change
// it in the future if needed.
cluster = "default-cluster"
}
return filepath.Join(append([]string{
s.base,
cluster,
}, subpath...)...)
}
// SaveMeta save the meta with specified cluster name.
func (s *SpecManager) SaveMeta(clusterName string, meta Metadata) error {
wrapError := func(err error) *errorx.Error {
return ErrSaveMetaFailed.Wrap(err, "Failed to save cluster metadata")
}
metaFile := s.Path(clusterName, metaFileName)
backupDir := s.Path(clusterName, BackupDirName)
if err := s.ensureDir(clusterName); err != nil {
return wrapError(err)
}
if err := os.MkdirAll(backupDir, 0755); err != nil {
return wrapError(err)
}
data, err := yaml.Marshal(meta)
if err != nil {
return wrapError(err)
}
opsVer := meta.GetBaseMeta().OpsVer
if opsVer != nil {
*opsVer = version.NewTiUPVersion().String()
}
err = utils.SaveFileWithBackup(metaFile, data, backupDir)
if err != nil {
return wrapError(err)
}
return nil
}
// Metadata tries to read the metadata of a cluster from file
func (s *SpecManager) Metadata(clusterName string, meta interface{}) error {
fname := s.Path(clusterName, metaFileName)
yamlFile, err := os.ReadFile(fname)
if err != nil {
return perrs.AddStack(err)
}
err = yaml.Unmarshal(yamlFile, meta)
if err != nil {
return perrs.AddStack(err)
}
return nil
}
// Exist check if the cluster exist by checking the meta file.
func (s *SpecManager) Exist(name string) (exist bool, err error) {
fname := s.Path(name, metaFileName)
_, err = os.Stat(fname)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, perrs.AddStack(err)
}
return true, nil
}
// Remove remove the data with specified cluster name.
func (s *SpecManager) Remove(name string) error {
return os.RemoveAll(s.Path(name))
}
// List return the cluster names.
func (s *SpecManager) List() (names []string, err error) {
fileInfos, err := os.ReadDir(s.base)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, perrs.AddStack(err)
}
for _, info := range fileInfos {
if utils.IsNotExist(s.Path(info.Name(), metaFileName)) {
continue
}
names = append(names, info.Name())
}
return
}
// GetAllClusters get a metadata list of all clusters deployed by current user
func (s *SpecManager) GetAllClusters() (map[string]Metadata, error) {
clusters := make(map[string]Metadata)
names, err := s.List()
if err != nil {
return nil, err
}
for _, name := range names {
metadata := s.NewMetadata()
err = s.Metadata(name, metadata)
// clusters with topology validation errors should also be listed
if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) &&
!errors.Is(perrs.Cause(err), ErrNoTiSparkMaster) {
return nil, perrs.Trace(err)
}
clusters[name] = metadata
}
return clusters, nil
}
// ensureDir ensures that the cluster directory exists.
func (s *SpecManager) ensureDir(clusterName string) error {
if err := utils.CreateDir(s.Path(clusterName)); err != nil {
return ErrCreateDirFailed.
Wrap(err, "Failed to create cluster metadata directory '%s'", s.Path(clusterName)).
WithProperty(tui.SuggestionFromString("Please check file system permissions and try again."))
}
return nil
}