This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
store.go
181 lines (151 loc) · 4 KB
/
store.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
package storage
import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"github.com/Microsoft/kunlun/common/fileio"
uuid "github.com/nu7hatch/gouuid"
)
const (
STATE_SCHEMA = 1
STATE_FILE = "kl-state.json"
)
type Store struct {
dir string
fs fs
stateSchema int
}
type fs interface {
fileio.FileWriter
fileio.Remover
fileio.AllRemover
fileio.Stater
fileio.AllMkdirer
fileio.DirReader
}
func NewStore(dir string, fs fs) Store {
return Store{
dir: dir,
fs: fs,
stateSchema: STATE_SCHEMA,
}
}
func (s Store) Set(state State) error {
_, err := s.fs.Stat(s.dir)
if err != nil {
return fmt.Errorf("Stat state dir: %s", err)
}
if reflect.DeepEqual(state, State{}) {
return nil
}
state.Version = s.stateSchema
if state.ID == "" {
uuid, err := uuid.NewV4()
if err != nil {
return fmt.Errorf("Create state ID: %s", err)
}
state.ID = uuid.String()
}
jsonData, err := json.MarshalIndent(state, "", "\t")
if err != nil {
return err
}
stateFile := filepath.Join(s.dir, STATE_FILE)
err = s.fs.WriteFile(stateFile, jsonData, os.FileMode(0644))
if err != nil {
return err
}
return nil
}
func (s Store) GetStateDir() string {
return s.dir
}
func (s Store) GetVarsDir() (string, error) {
return s.getDir("vars", StateMode)
}
// GetArtifactsDir get artifacts folder
func (s Store) GetArtifactsDir() (string, error) {
return s.getDir("artifacts", os.ModePerm)
}
// GetMainArtifactFilePath get artifacts main file path
func (s Store) GetMainArtifactFilePath() (string, error) {
artifactsDir, err := s.GetArtifactsDir()
if err != nil {
return "", err
}
return path.Join(artifactsDir, "main.yml"), nil
}
func (s Store) GetAdminSSHPrivateKeyPath() (string, error) {
varsFolder, err := s.GetVarsDir()
if err != nil {
return "", err
}
sshPrivateKeyPath := path.Join(varsFolder, "admin_ssh_private_key")
return sshPrivateKeyPath, nil
}
// GetMainArtifactVarsFilePath get the variables file path
func (s Store) GetMainArtifactVarsFilePath() (string, error) {
artifactsDir, err := s.GetVarsDir()
if err != nil {
return "", err
}
return path.Join(artifactsDir, "main-vars-file.yml"), nil
}
// GetMainArtifactVarsStoreFilePath get the vars store, to store the vars generated.
func (s Store) GetMainArtifactVarsStoreFilePath() (string, error) {
artifactsDir, err := s.GetVarsDir()
if err != nil {
return "", err
}
return path.Join(artifactsDir, "main-vars-store.yml"), nil
}
// GetArtifactsPatchDir get the patches folder
func (s Store) GetArtifactsPatchDir() (string, error) {
return s.getDir("artifacts/patches", os.ModePerm)
}
// GetInfraDir get the infrastructure folder.
func (s Store) GetInfraDir() (string, error) {
return s.getDir("infra", os.ModePerm)
}
// GetTerraformDir get the terraform folder, this should be the sub folder of infra.
func (s Store) GetTerraformDir() (string, error) {
return s.getDir("infra/terraform", os.ModePerm)
}
func (s Store) GetDeploymentsDir() (string, error) {
return s.getDir("deployments", os.ModePerm)
}
func (s Store) GetDeploymentScriptFile() (string, error) {
deploymentsDir, err := s.GetDeploymentsDir()
if err != nil {
return "", err
}
return path.Join(deploymentsDir, "deploy.sh"), nil
}
func (s Store) GetAnsibleDir() (string, error) {
return s.getDir("deployments/ansible", os.ModePerm)
}
func (s Store) GetAnsibleMainFile() (string, error) {
ansibleDir, err := s.GetAnsibleDir()
if err != nil {
return "", err
}
return path.Join(ansibleDir, "main.yml"), nil
}
// TODO think about merge the vars dir with the global vars dir.
func (s Store) GetAnsibleVarsDir() (string, error) {
return s.getDir("deployments/ansible/vars", os.ModePerm)
}
func (s Store) GetAnsibleInventoriesDir() (string, error) {
return s.getDir("deployments/ansible/inventories", os.ModePerm)
}
func (s Store) getDir(name string, perm os.FileMode) (string, error) {
dir := filepath.Join(s.dir, name)
err := s.fs.MkdirAll(dir, perm)
if err != nil {
return "", fmt.Errorf("Get %s dir: %s", name, err)
}
return dir, nil
}