-
Notifications
You must be signed in to change notification settings - Fork 217
/
apply.go
183 lines (163 loc) · 5.54 KB
/
apply.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
package operations
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/spaceuptech/space-cloud/space-cli/cmd/model"
"github.com/spaceuptech/space-cloud/space-cli/cmd/utils"
)
// Apply reads the config file(s) from the provided file / directory and applies it to the server
func Apply(applyName string, isForceApply bool, delay time.Duration, retry int) error {
if !strings.HasSuffix(applyName, ".yaml") {
dirName := applyName
if err := os.Chdir(dirName); err != nil {
return utils.LogError(fmt.Sprintf("Unable to switch to directory %s", dirName), err)
}
// list file of current directory. Note : we have changed the directory with the help of above function
files, err := ioutil.ReadDir(".")
if err != nil {
return utils.LogError(fmt.Sprintf("Unable to fetch config files from %s", dirName), err)
}
account, token, err := utils.LoginWithSelectedAccount()
if err != nil {
return utils.LogError("Couldn't get account details or login token", err)
}
var fileNames []string
// filter directories
for _, fileInfo := range files {
if !fileInfo.IsDir() {
fileNames = append(fileNames, fileInfo.Name())
}
}
// sort file names alphanumerically
for _, fileName := range fileNames {
if strings.HasSuffix(fileName, ".yaml") {
specs, err := utils.ReadSpecObjectsFromFile(fileName)
if err != nil {
return utils.LogError("Unable to read spec objects from file", err)
}
// Apply all spec
for _, spec := range specs {
if spec.Type == "db-schema" && !isForceApply {
prompt := &survey.Confirm{
Message: "Changing the schema can cause data loss (this option will be applied to all resources of type db-schema).\n Are you sure you want to continue?",
}
_ = survey.AskOne(prompt, &isForceApply)
if !isForceApply {
utils.LogInfo(fmt.Sprintf("Skipping the resource (db-schema) having meta %v", spec.Meta))
continue
}
}
currentRetryCount := 0
for {
err = ApplySpec(token, account, spec)
if err == nil {
break
}
err = utils.LogError(fmt.Sprintf("Unable to apply file (%s) spec object with id (%v) type (%v)", fileName, spec.Meta["id"], spec.Type), err)
if currentRetryCount == retry {
return err
}
currentRetryCount++
utils.LogInfo(fmt.Sprintf("Retrying spec object with id (%v) and type (%v)", spec.Meta["id"], spec.Type))
}
time.Sleep(delay)
}
}
}
return nil
}
account, token, err := utils.LoginWithSelectedAccount()
if err != nil {
return utils.LogError("Couldn't get account details or login token", err)
}
specs, err := utils.ReadSpecObjectsFromFile(applyName)
if err != nil {
return utils.LogError("Unable to read spec objects from file", err)
}
// Apply all spec
for _, spec := range specs {
if spec.Type == "db-schema" && !isForceApply {
prompt := &survey.Confirm{
Message: "Changing the schema can cause data loss (this option will be applied to all resources of type db-schema).\n Are you sure you want to continue?",
}
_ = survey.AskOne(prompt, &isForceApply)
if !isForceApply {
utils.LogInfo(fmt.Sprintf("Skipping the resource (db-schema) having meta %v", spec.Meta))
continue
}
}
currentRetryCount := 0
for {
err = ApplySpec(token, account, spec)
if err == nil {
break
}
err = utils.LogError(fmt.Sprintf("Unable to apply spec object with id (%v) type (%v)", spec.Meta["id"], spec.Type), err)
if currentRetryCount == retry {
return err
}
currentRetryCount++
utils.LogInfo(fmt.Sprintf("Retrying spec object with id (%v) and type (%v)", spec.Meta["id"], spec.Type))
}
time.Sleep(delay)
}
return nil
}
// ApplySpec takes a spec object and applies it
func ApplySpec(token string, account *model.Account, specObj *model.SpecObject) error {
requestBody, err := json.Marshal(specObj.Spec)
if err != nil {
_ = utils.LogError(fmt.Sprintf("error while applying service unable to marshal spec - %s", err.Error()), nil)
return err
}
url, err := adjustPath(fmt.Sprintf("%s%s", account.ServerURL, specObj.API), specObj.Meta)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(requestBody))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
_ = utils.LogError(fmt.Sprintf("error while applying service unable to send http request - %s", err.Error()), nil)
return err
}
v := map[string]interface{}{}
_ = json.NewDecoder(resp.Body).Decode(&v)
utils.CloseTheCloser(req.Body)
if resp.StatusCode == http.StatusAccepted {
// Make checker send this status
utils.LogInfo(fmt.Sprintf("Successfully queued %s", specObj.Type))
} else if resp.StatusCode == http.StatusOK {
utils.LogInfo(fmt.Sprintf("Successfully applied %s", specObj.Type))
} else {
_ = utils.LogError(fmt.Sprintf("error while applying service got http status code %s - %s", resp.Status, v["error"]), nil)
return fmt.Errorf("%v", v["error"])
}
return nil
}
func adjustPath(path string, meta map[string]string) (string, error) {
newPath := path
for {
pre := strings.IndexRune(newPath, '{')
if pre < 0 {
return newPath, nil
}
post := strings.IndexRune(newPath, '}')
key := strings.TrimSuffix(strings.TrimPrefix(newPath[pre:post], "{"), "}")
value, p := meta[key]
if !p {
return "", fmt.Errorf("provided key (%s) does not exist in metadata", key)
}
newPath = newPath[:pre] + value + newPath[post+1:]
}
}