-
Notifications
You must be signed in to change notification settings - Fork 62
/
setup.go
82 lines (69 loc) · 2.2 KB
/
setup.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package apmintegration
import (
"os"
"github.com/ava-labs/apm/apm"
"github.com/ava-labs/apm/config"
"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/spf13/afero"
"gopkg.in/yaml.v2"
)
// Note, you can only call this method once per run
func SetupApm(app *application.Avalanche, apmBaseDir string) error {
credentials, err := initCredentials(app)
if err != nil {
return err
}
// Need to initialize a afero filesystem object to run apm
fs := afero.NewOsFs()
err = os.MkdirAll(app.GetAPMPluginDir(), constants.DefaultPerms755)
if err != nil {
return err
}
// The New() function has a lot of prints we'd like to hide from the user,
// so going to divert stdout to the log temporarily
stdOutHolder := os.Stdout
apmLog, err := os.OpenFile(app.GetAPMLog(), os.O_APPEND|os.O_CREATE|os.O_WRONLY, constants.DefaultPerms755)
if err != nil {
return err
}
defer apmLog.Close()
os.Stdout = apmLog
apmConfig := apm.Config{
Directory: apmBaseDir,
Auth: credentials,
AdminAPIEndpoint: app.Conf.GetConfigStringValue(constants.ConfigAPMAdminAPIEndpointKey),
PluginDir: app.GetAPMPluginDir(),
Fs: fs,
}
apmInstance, err := apm.New(apmConfig)
if err != nil {
return err
}
os.Stdout = stdOutHolder
app.Apm = apmInstance
app.ApmDir = apmBaseDir
return err
}
// If we need to use custom git credentials (say for private repos).
// the zero value for credentials is safe to use.
// Stolen from APM repo
func initCredentials(app *application.Avalanche) (http.BasicAuth, error) {
result := http.BasicAuth{}
if app.Conf.ConfigValueIsSet(constants.ConfigAPMCredentialsFileKey) {
credentials := &config.Credential{}
bytes, err := os.ReadFile(app.Conf.GetConfigStringValue(constants.ConfigAPMCredentialsFileKey))
if err != nil {
return result, err
}
if err := yaml.Unmarshal(bytes, credentials); err != nil {
return result, err
}
result.Username = credentials.Username
result.Password = credentials.Password
}
return result, nil
}