forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcloud.go
248 lines (213 loc) · 5.52 KB
/
gcloud.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package gke
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
)
var (
REQUIRED_SERVICE_ACCOUNT_ROLES = []string{"roles/compute.instanceAdmin.v1",
"roles/iam.serviceAccountActor",
"roles/container.clusterAdmin",
"roles/container.admin",
"roles/container.developer",
"roles/storage.objectAdmin",
"roles/editor"}
)
func BucketExists(projectId string, bucketName string) (bool, error) {
fullBucketName := fmt.Sprintf("gs://%s", bucketName)
args := []string{"ls"}
if projectId != "" {
args = append(args, "-p")
args = append(args, projectId)
}
cmd := util.Command{
Name: "gsutil",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
return false, err
}
return strings.Contains(output, fullBucketName), nil
}
func CreateBucket(projectId string, bucketName string, location string) error {
fullBucketName := fmt.Sprintf("gs://%s", bucketName)
args := []string{"mb", "-l", location}
if projectId != "" {
args = append(args, "-p")
args = append(args, projectId)
}
args = append(args, fullBucketName)
cmd := util.Command{
Name: "gsutil",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
return nil
}
func GetRegionFromZone(zone string) string {
return zone[0 : len(zone)-2]
}
func GetOrCreateServiceAccount(serviceAccount string, projectId string, clusterConfigDir string) (string, error) {
if projectId == "" {
return "", errors.New("cannot get/create a service account without a projectId")
}
args := []string{"iam",
"service-accounts",
"list",
"--filter",
serviceAccount,
"--project",
projectId}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
return "", err
}
if output == "Listed 0 items." {
log.Infof("Unable to find service account %s, checking if we have enough permission to create\n", serviceAccount)
// if it doesn't check to see if we have permissions to create (assign roles) to a service account
hasPerm, err := CheckPermission("resourcemanager.projects.setIamPolicy", projectId)
if err != nil {
return "", err
}
if !hasPerm {
return "", errors.New("User does not have the required role 'resourcemanager.projects.setIamPolicy' to configure a service account")
}
// create service
log.Infof("Creating service account %s\n", serviceAccount)
args = []string{"iam",
"service-accounts",
"create",
serviceAccount,
"--project",
projectId}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err = cmd.RunWithoutRetry()
if err != nil {
return "", err
}
// assign roles to service account
for _, role := range REQUIRED_SERVICE_ACCOUNT_ROLES {
log.Infof("Assigning role %s\n", role)
args = []string{"projects",
"add-iam-policy-binding",
projectId,
"--member",
fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", serviceAccount, projectId),
"--role",
role,
"--project",
projectId}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return "", err
}
}
} else {
log.Info("Service Account exists\n")
}
os.MkdirAll(clusterConfigDir, os.ModePerm)
keyPath := filepath.Join(clusterConfigDir, fmt.Sprintf("%s.key.json", serviceAccount))
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
log.Info("Downloading service account key\n")
args = []string{"iam",
"service-accounts",
"keys",
"create",
keyPath,
"--iam-account",
fmt.Sprintf("%s@%s.iam.gserviceaccount.com", serviceAccount, projectId),
"--project",
projectId}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return "", err
}
}
return keyPath, nil
}
func EnableApis(projectId string, apis ...string) error {
args := []string{"services", "enable"}
args = append(args, apis...)
if projectId != "" {
args = append(args, "--project")
args = append(args, projectId)
}
log.Infof("Lets ensure we have container and compute enabled on your project via: %s\n", util.ColorInfo("gcloud "+strings.Join(args, " ")))
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
return nil
}
func Login(serviceAccountKeyPath string, skipLogin bool) error {
if serviceAccountKeyPath != "" {
if _, err := os.Stat(serviceAccountKeyPath); os.IsNotExist(err) {
return errors.New("Unable to locate service account " + serviceAccountKeyPath)
}
cmd := util.Command{
Name: "gcloud",
Args: []string{"auth", "activate-service-account", "--key-file", serviceAccountKeyPath},
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
} else if !skipLogin {
cmd := util.Command{
Name: "gcloud",
Args: []string{"auth", "login", "--brief"},
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
}
return nil
}
func CheckPermission(perm string, projectId string) (bool, error) {
if projectId == "" {
return false, errors.New("cannot check permission without a projectId")
}
// if it doesn't check to see if we have permissions to create (assign roles) to a service account
args := []string{"iam",
"list-testable-permissions",
fmt.Sprintf("//cloudresourcemanager.googleapis.com/projects/%s", projectId),
"--filter",
perm}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
return false, err
}
return strings.Contains(output, perm), nil
}