This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stemcells.go
228 lines (170 loc) · 4.76 KB
/
stemcells.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
package director
import (
"encoding/json"
"fmt"
"net/http"
gourl "net/url"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
semver "github.com/cppforlife/go-semi-semantic/version"
)
type StemcellImpl struct {
client Client
name string
version semver.Version
currentlyDeployed bool
osName string
cid string
}
func (s StemcellImpl) Name() string { return s.name }
func (s StemcellImpl) Version() semver.Version { return s.version }
func (s StemcellImpl) OSName() string { return s.osName }
func (s StemcellImpl) CID() string { return s.cid }
func (s StemcellImpl) VersionMark(suffix string) string {
if s.currentlyDeployed {
return suffix
}
return ""
}
func (s StemcellImpl) Delete(force bool) error {
err := s.client.DeleteStemcell(s.name, s.version.String(), force)
if err != nil {
found, listErr := s.client.HasStemcell(s.name, s.version.String())
if found || listErr != nil {
return err
}
}
return nil
}
type StemcellResp struct {
Name string
Version string
OperatingSystem string `json:"operating_system"`
CID string `json:"cid"`
// Only used for determining if stemcell is deployed
Deployments []interface{}
}
func (d DirectorImpl) Stemcells() ([]Stemcell, error) {
resps, err := d.client.Stemcells()
if err != nil {
return nil, err
}
var stems []Stemcell
for _, resp := range resps {
parsedVersion, err := semver.NewVersionFromString(resp.Version)
if err != nil {
return nil, bosherr.WrapErrorf(
err, "Parsing version for stemcell '%s/%s'", resp.Name, resp.Version)
}
stem := StemcellImpl{
client: d.client,
name: resp.Name,
version: parsedVersion,
currentlyDeployed: len(resp.Deployments) > 0,
osName: resp.OperatingSystem,
cid: resp.CID,
}
stems = append(stems, stem)
}
return stems, nil
}
func (d DirectorImpl) FindStemcell(slug StemcellSlug) (Stemcell, error) {
parsedVersion, err := semver.NewVersionFromString(slug.Version())
if err != nil {
return nil, bosherr.WrapErrorf(
err, "Parsing version for stemcell '%s/%s'", slug.Name(), slug.Version())
}
stem := StemcellImpl{
client: d.client,
name: slug.Name(),
version: parsedVersion,
}
return stem, nil
}
func (d DirectorImpl) HasStemcell(name, version string) (bool, error) {
return d.client.HasStemcell(name, version)
}
func (d DirectorImpl) UploadStemcellURL(url, sha1 string, fix bool) error {
return d.client.UploadStemcellURL(url, sha1, fix)
}
func (d DirectorImpl) UploadStemcellFile(file UploadFile, fix bool) error {
return d.client.UploadStemcellFile(file, fix)
}
func (c Client) Stemcells() ([]StemcellResp, error) {
var resps []StemcellResp
err := c.clientRequest.Get("/stemcells", &resps)
if err != nil {
return resps, bosherr.WrapErrorf(err, "Finding stemcells")
}
return resps, nil
}
func (c Client) HasStemcell(name, version string) (bool, error) {
resps, err := c.Stemcells()
if err != nil {
return false, err
}
for _, r := range resps {
if r.Name == name && r.Version == version {
return true, nil
}
}
return false, nil
}
func (c Client) UploadStemcellURL(url, sha1 string, fix bool) error {
if len(url) == 0 {
return bosherr.Error("Expected non-empty URL")
}
query := gourl.Values{}
if fix {
query.Add("fix", "true")
}
path := "/stemcells?" + query.Encode()
body := map[string]string{"location": url}
if len(sha1) > 0 {
body["sha1"] = sha1
}
reqBody, err := json.Marshal(body)
if err != nil {
return bosherr.WrapErrorf(err, "Marshaling request body")
}
setHeaders := func(req *http.Request) {
req.Header.Add("Content-Type", "application/json")
}
_, err = c.taskClientRequest.PostResult(path, reqBody, setHeaders)
if err != nil {
return bosherr.WrapErrorf(err, "Uploading remote stemcell '%s'", url)
}
return nil
}
func (c Client) UploadStemcellFile(file UploadFile, fix bool) error {
fileInfo, err := file.Stat()
if err != nil {
return bosherr.WrapErrorf(err, "Determining stemcell file size")
}
query := gourl.Values{}
if fix {
query.Add("fix", "true")
}
path := "/stemcells?" + query.Encode()
setHeadersAndBody := func(req *http.Request) {
req.Header.Add("Content-Type", "application/x-compressed")
req.ContentLength = fileInfo.Size()
req.Body = file
}
_, err = c.taskClientRequest.PostResult(path, nil, setHeadersAndBody)
if err != nil {
return bosherr.WrapErrorf(err, "Uploading stemcell file")
}
return nil
}
func (c Client) DeleteStemcell(name, version string, force bool) error {
query := gourl.Values{}
if force {
query.Add("force", "true")
}
path := fmt.Sprintf("/stemcells/%s/%s?%s", name, version, query.Encode())
_, err := c.taskClientRequest.DeleteResult(path)
if err != nil {
return bosherr.WrapErrorf(err, "Deleting stemcell '%s/%s'", name, version)
}
return nil
}