-
Notifications
You must be signed in to change notification settings - Fork 312
/
profile.go
303 lines (271 loc) · 8.43 KB
/
profile.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// 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 localdata
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"sort"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/utils"
"golang.org/x/mod/semver"
)
// Profile represents the `tiup` profile
type Profile struct {
root string
Config *TiUPConfig
}
// NewProfile returns a new profile instance
func NewProfile(root string, config *TiUPConfig) *Profile {
return &Profile{root: root, Config: config}
}
// InitProfile creates a new profile using environment variables and defaults.
func InitProfile() *Profile {
var profileDir string
switch {
case os.Getenv(EnvNameHome) != "":
profileDir = os.Getenv(EnvNameHome)
case DefaultTiUPHome != "":
profileDir = DefaultTiUPHome
default:
u, err := user.Current()
if err != nil {
panic("cannot get current user information: " + err.Error())
}
profileDir = filepath.Join(u.HomeDir, ProfileDirName)
}
cfg, err := InitConfig(profileDir)
if err != nil {
panic("cannot read config: " + err.Error())
}
return NewProfile(profileDir, cfg)
}
// Path returns a full path which is related to profile root directory
func (p *Profile) Path(relpath ...string) string {
return filepath.Join(append([]string{p.root}, relpath...)...)
}
// Root returns the root path of the `tiup`
func (p *Profile) Root() string {
return p.root
}
// GetComponentInstalledVersion return the installed version of component.
func (p *Profile) GetComponentInstalledVersion(component string, ver utils.Version) (utils.Version, error) {
if !ver.IsEmpty() && ver.String() != utils.NightlyVersionAlias {
return ver, nil
}
versions, err := p.InstalledVersions(component)
if err != nil {
return "", err
}
// Use the latest version if user doesn't specify a specific version
// report an error if the specific component doesn't be installed
// Check whether the specific version exist in local
if len(versions) == 0 {
return "", errors.Errorf("component not installed, please try `tiup install %s` to install it", component)
}
sort.Slice(versions, func(i, j int) bool {
return semver.Compare(versions[i], versions[j]) < 0
})
if ver.String() != utils.NightlyVersionAlias {
for i := len(versions); i > 0; i-- {
if utils.Version(versions[i-1]).IsNightly() {
return utils.Version(versions[i-1]), nil
}
}
return "", errors.Errorf("component(nightly) not installed, please try `tiup install %s:nightly` to install it", component)
}
return utils.Version(versions[len(versions)-1]), nil
}
// ComponentInstalledPath returns the path where the component installed
func (p *Profile) ComponentInstalledPath(component string, version utils.Version) (string, error) {
installedVersion, err := p.GetComponentInstalledVersion(component, version)
if err != nil {
return "", err
}
return filepath.Join(p.Path(ComponentParentDir), component, installedVersion.String()), nil
}
// SaveTo saves file to the profile directory, path is relative to the
// profile directory of current user
func (p *Profile) SaveTo(path string, data []byte, perm os.FileMode) error {
fullPath := filepath.Join(p.root, path)
// create sub directory if needed
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
return errors.Trace(err)
}
return os.WriteFile(fullPath, data, perm)
}
// WriteJSON writes struct to a file (in the profile directory) in JSON format
func (p *Profile) WriteJSON(path string, data interface{}) error {
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return errors.Trace(err)
}
return p.SaveTo(path, jsonData, 0644)
}
// readJSON read file and unmarshal to target `data`
func (p *Profile) readJSON(path string, data interface{}) error {
fullPath := filepath.Join(p.root, path)
file, err := os.Open(fullPath)
if err != nil {
return errors.Trace(err)
}
defer file.Close()
return json.NewDecoder(file).Decode(data)
}
// ReadMetaFile reads a Process object from dirName/MetaFilename. Returns (nil, nil) if a metafile does not exist.
func (p *Profile) ReadMetaFile(dirName string) (*Process, error) {
metaFile := filepath.Join(DataParentDir, dirName, MetaFilename)
// If the path doesn't contain the meta file, which means startup interrupted
if utils.IsNotExist(p.Path(metaFile)) {
return nil, nil
}
var process Process
err := p.readJSON(metaFile, &process)
return &process, err
}
// InstalledComponents returns the installed components
func (p *Profile) InstalledComponents() ([]string, error) {
compDir := filepath.Join(p.root, ComponentParentDir)
fileInfos, err := os.ReadDir(compDir)
if err != nil && os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, errors.Trace(err)
}
var components []string
for _, fi := range fileInfos {
if !fi.IsDir() {
continue
}
components = append(components, fi.Name())
}
sort.Strings(components)
return components, nil
}
// InstalledVersions returns the installed versions of specific component
func (p *Profile) InstalledVersions(component string) ([]string, error) {
path := filepath.Join(p.root, ComponentParentDir, component)
if utils.IsNotExist(path) {
return nil, nil
}
fileInfos, err := os.ReadDir(path)
if err != nil {
return nil, errors.Trace(err)
}
var versions []string
for _, fi := range fileInfos {
if !fi.IsDir() {
continue
}
sub, err := os.ReadDir(filepath.Join(path, fi.Name()))
if err != nil || len(sub) < 1 {
continue
}
versions = append(versions, fi.Name())
}
return versions, nil
}
// VersionIsInstalled returns true if exactly version of component is installed.
func (p *Profile) VersionIsInstalled(component, version string) (bool, error) {
installed, err := p.InstalledVersions(component)
if err != nil {
return false, err
}
for _, v := range installed {
if v == version {
return true, nil
}
}
return false, nil
}
// ResetMirror reset root.json and cleanup manifests directory
func (p *Profile) ResetMirror(addr, root string) error {
// Calculating root.json path
shaWriter := sha256.New()
if _, err := io.Copy(shaWriter, strings.NewReader(addr)); err != nil {
return err
}
localRoot := p.Path("bin", fmt.Sprintf("%s.root.json", hex.EncodeToString(shaWriter.Sum(nil))[:16]))
if root == "" {
switch {
case utils.IsExist(localRoot):
root = localRoot
case strings.HasSuffix(addr, "/"):
root = addr + "root.json"
default:
root = addr + "/root.json"
}
}
// Fetch root.json
var wc io.ReadCloser
if strings.HasPrefix(root, "http") {
resp, err := http.Get(root)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return errors.Errorf("Fetch remote root.json returns http code %d", resp.StatusCode)
}
wc = resp.Body
} else {
file, err := os.Open(root)
if err != nil {
return err
}
wc = file
}
defer wc.Close()
f, err := os.OpenFile(p.Path("bin", "root.json"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0664)
if err != nil {
return err
}
if _, err := io.Copy(f, wc); err != nil {
f.Close()
return err
}
f.Close()
// Only cache remote mirror
if strings.HasPrefix(addr, "http") && root != localRoot {
if strings.HasPrefix(root, "http") {
fmt.Printf("WARN: adding root certificate via internet: %s\n", root)
fmt.Printf("You can revoke this by remove %s\n", localRoot)
}
_ = utils.Copy(p.Path("bin", "root.json"), localRoot)
}
if err := os.RemoveAll(p.Path(ManifestParentDir)); err != nil {
return err
}
p.Config.Mirror = addr
return p.Config.Flush()
}
// Process represents a process as written to a meta file.
type Process struct {
Component string `json:"component"`
CreatedTime string `json:"created_time"`
Pid int `json:"pid"` // PID of the process
Exec string `json:"exec"` // Path to the binary
Args []string `json:"args,omitempty"` // Command line arguments
Env []string `json:"env,omitempty"` // Environment variables
Dir string `json:"dir,omitempty"` // Data directory
Cmd *exec.Cmd `json:"-"`
}