-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform.go
227 lines (189 loc) · 4.46 KB
/
terraform.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
package main
import (
"archive/zip"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime"
"sort"
"strings"
ver "github.com/hashicorp/go-version"
)
type Terraform struct {
location string
verbose bool
versions ver.Collection
}
func NewTerraform(location string, verbose bool) (*Terraform, error) {
location = expandPath(location)
err := createDir(location)
if err != nil {
return nil, err
}
tf := &Terraform{
location: strings.TrimRight(location, "/"),
verbose: verbose,
}
files, err := os.ReadDir(tf.location)
if err != nil {
return tf, err
}
for _, f := range files {
v, err := ver.NewVersion(f.Name())
if err != nil {
return tf, err
}
tf.versions = append(tf.versions, v)
}
return tf, nil
}
func (tf *Terraform) String() string {
o := []string{}
for _, v := range tf.versions {
o = append(o, v.String())
}
return strings.Join(o, "\n")
}
func (tf *Terraform) FindLatest(c ver.Constraints) (*ver.Version, error) {
var latest *ver.Version
if len(tf.versions) == 0 {
return latest, fmt.Errorf("no binaries available in %s", tf.location)
}
for _, v := range tf.versions {
if c.Check(v) && (latest == nil || v.GreaterThan(latest)) {
latest = v
}
}
if latest == nil {
return latest, fmt.Errorf("no matching version found for %s", c.String())
}
return latest, nil
}
func (tf *Terraform) ListInstalled() ver.Collection {
return tf.versions
}
func (tf *Terraform) ListAvailable() (ver.Collection, error) {
out := ver.Collection{}
type releaseInfo struct {
Versions map[string]struct {
Builds []struct {
Os string `json:"os"`
Arch string `json:"arch"`
} `json:"builds"`
} `json:"versions"`
}
url := "https://releases.hashicorp.com/terraform/index.json"
resp, err := http.Get(url)
if err != nil {
return out, fmt.Errorf("could not download %s: %s", url, err.Error())
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return out, fmt.Errorf("could not download %s: %s", url, resp.Status)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return out, err
}
releases := releaseInfo{}
err = json.Unmarshal(body, &releases)
if err != nil {
return out, err
}
for vs, spec := range releases.Versions {
version, err := ver.NewVersion(vs)
if err != nil {
continue
}
for _, build := range spec.Builds {
if build.Arch == runtime.GOARCH && build.Os == runtime.GOOS {
out = append(out, version)
}
}
}
sort.Sort(out)
return out, nil
}
func (tf *Terraform) Run(v *ver.Version, args []string, w wrapper) (*os.ProcessState, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
pa := os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
Dir: wd,
}
bin := fmt.Sprintf("%s/%s", tf.location, v.String())
cmd, args, err := w.Wrap(bin, args, tf.verbose)
if err != nil {
return nil, err
}
proc, err := os.StartProcess(cmd, append([]string{"terraform"}, args...), &pa)
if err != nil {
return nil, err
}
status, err := proc.Wait()
if err != nil {
return status, err
}
return status, w.Cleanup()
}
func (tf *Terraform) DownloadVersion(v *ver.Version) (string, error) {
url := fmt.Sprintf("https://releases.hashicorp.com/terraform/%s/terraform_%s_%s_%s.zip", v.String(), v.String(), runtime.GOOS, runtime.GOARCH)
filename := fmt.Sprintf("%s/%s", tf.location, v.String())
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("could not download %s: %s", url, err.Error())
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return "", fmt.Errorf("could not download %s: %s", url, resp.Status)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
if err != nil {
return "", err
}
unpacked := false
for _, zipped := range zipReader.File {
if zipped.Name != "terraform" {
continue
}
t, err := zipped.Open()
if err != nil {
return "", err
}
defer t.Close()
b, err := io.ReadAll(t)
if err != nil {
return "", err
}
dest, err := os.Create(filename)
if err != nil {
return "", err
}
defer dest.Close()
_, err = dest.Write(b)
if err != nil {
return "", err
}
err = dest.Sync()
if err != nil {
return "", err
}
unpacked = true
}
if !unpacked {
return "", fmt.Errorf("could not find file `terraform` not found in downloaded zip")
}
if err := os.Chmod(filename, 0700); err != nil {
return "", err
}
return filename, nil
}