-
Notifications
You must be signed in to change notification settings - Fork 8
/
create_projects.py
executable file
·762 lines (556 loc) · 18.2 KB
/
create_projects.py
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#!/usr/bin/env python
"""Create Google Cloud Platform projects."""
# import standard libraries
import glob
import re
import sys
import time
import yaml
# update path
# sys.path.insert(0, 'lib')
from lib.args import create_arg_parser
from lib.google import Google
google = Google()
try:
from config import templates_bucket
except:
templates_bucket = None
def get_parent(settings):
"""
Function: get_parent.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
organization = settings['organization']
folder = settings['folder']
parent = {
'id': None,
'type': None
}
if organization:
parent = {
'id': str(organization).replace('organizations/', ''),
'type': 'organization',
}
elif folder:
parent = {
'id': str(folder).replace('folders/', ''),
'type': 'folder',
}
return parent
def create_project(project_id, settings):
"""
Function: create_project.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
organization = settings['organization']
folder = settings['folder']
# labels = settings['labels']
project = {
'projectId': project_id,
'name': project_id,
}
if organization or folder:
project['parent'] = get_parent(settings)
# if labels:
# project['labels'] = labels
sys.stdout.write(' * creating project: %s...' % project_id)
sys.stdout.flush()
result = google.create_project(project)
if result:
created = False
while not created:
try:
project = google.get_project(project_id)
created = True
except:
created = False
print 'successful.'
return result
def create_service_accounts(project_id, settings):
"""
Function: create_service_accounts.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
service_accounts = settings['service_accounts']
print ' * creating service accounts:'
for account_id in service_accounts:
sys.stdout.write(' - %s...' % account_id)
sys.stdout.flush()
result = google.create_service_account(project_id, account_id)
if result:
print 'successful.'
def create_usage_bucket(project_id, settings):
"""
Function: create_usage_bucket.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
usage_bucket = settings['usage_bucket']
try:
google.get_bucket(usage_bucket)
except:
sys.stdout.write(' * creating usage bucket: %s...' % usage_bucket)
sys.stdout.flush()
result = google.create_bucket(project_id, usage_bucket)
if result:
print 'successful.'
def display_settings(settings):
"""
Function: display_settings.
description
Args:
settings - [type/description]
Returns:
return description
"""
print '\nSettings:\n'
for key in sorted(settings):
if key in ['organization'] and settings[key]:
print ' Organization: %s...' % settings[key]
elif key in ['folder'] and settings[key]:
print ' Folder: %s...' % settings[key]
elif key in ['apis'] and settings[key]:
print ' APIs: '
print ' - ' + '\n - '.join(settings[key])
elif key in ['billing_account'] and settings[key]:
print ' Billing Account: %s...' % settings[key]
elif key in ['iam_policy'] and settings[key]:
print ' IAM Policy:'
for role in sorted(settings[key]):
print ' %s:' % role
for i in sorted(settings[key][role]):
print ' - %s' % i
elif key in ['labels'] and settings[key]:
print ' Labels:'
for label in sorted(settings[key]):
print ' %s: %s' % (label, settings[key][label])
elif key in ['service_accounts'] and settings[key]:
print ' Service Accounts:'
print ' - '+'\n - '.join(settings[key])
elif key in ['default_service_account'] and settings[key]:
print ' Default Service Account: %s' % settings[key]
elif key in ['usage_bucket'] and settings[key]:
print ' Usage Bucket: %s' % settings[key]
elif key in ['region'] and settings[key]:
print ' Region: %s' % settings[key]
elif key in ['zone'] and settings[key]:
print ' Zone: %s' % settings[key]
def enable_billing(project_id, settings):
"""
Function: enable_billing.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
billing_account = 'billingAccounts/%s' % settings['billing_account']
billing_string = settings['billing_account']
sys.stdout.write(' * enabling billing: %s...' % billing_string)
sys.stdout.flush()
result = google.enable_project_billing(project_id, '%s' % billing_account)
if result:
print 'successful.'
def enable_services(project_id, settings):
"""
Function: enable_services.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
apis = settings['apis']
print ' * enabling APIs:'
for service_name in apis:
sys.stdout.write(' - %s...' % service_name)
sys.stdout.flush()
response = google.enable_project_service(project_id, service_name)
if response:
while True:
operation = google.get_service_operation(response['name'])
if 'done' in operation and operation['done']:
break
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1)
print 'successful.'
else:
print
return sorted(apis)
def enable_usage_bucket(project_id, settings):
"""
Function: enable_usage_bucket.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
usage_bucket = settings['usage_bucket']
message = 'enabling compute usage export'
sys.stdout.write(' * %s: gs://%s/...' % (message, usage_bucket))
sys.stdout.flush()
result = google.set_project_usage_export_bucket(project_id, usage_bucket)
if result:
print 'successful.'
else:
print
def get_bindings(iam_policy_args):
"""
Function: get_bindings.
description
Args:
iam_policy_args - [type/description]
Returns:
return description
"""
if not iam_policy_args:
return None
bindings = {}
for i in iam_policy_args.split(','):
if re.search('=', i):
(role, identity) = i.split('=')
if role not in bindings:
bindings[role] = [identity]
else:
bindings[role].append(identity)
return bindings
def get_labels(labels_args):
"""
Function: get_labels.
description
Args:
labelsArgs - [type/description]
Returns:
return description
"""
if not labels_args:
return None
labels = {}
for label in labels_args.split(','):
if re.search('=', label):
(name, value) = label.split('=')
labels[name] = value
return labels
def get_effective_settings(args):
"""
Function: get_effective_settings.
description
Args:
args - [type/description]
Returns:
return description
"""
# set defaults
settings = {
'apis': None,
'billing_account': None,
'default_service_account': None,
'folder': None,
'iam_policy': None,
'labels': None,
'organization': None,
'region': None,
'service_accounts': None,
'template': None,
'usage_bucket': None,
'zone': None,
}
# print json.dumps(settings, indent=2, sort_keys=True)
# check for template
if args.template:
print '\nLoading template: %s...' % args.template
remote = False
#
# # check if file is a gs:// path
# if re.match('gs:', args.template):
# path = args.template.replace('gs://', '')
# bucket_name = path.split('/')[0]
# object_name = '/'.join(path.split('/')[1:])
# remote = True
#
# # otherwise check if templates_bucket is set
# elif templates_bucket:
# path = templates_bucket.replace('gs://', '')
# bucket_name = path.split('/')[0]
# object_name = '/'.join(path.split('/')[1:])+args.template+'.yml'
# remote = True
#
# if remote:
# # get the file from google
# stream = google.get_bucket_object(bucket_name, object_name)
# text = stream.getvalue()
# docs = yaml.load_all(text)
# # print docs
#
# else:
# look for template in templates directory
template_files = glob.glob('templates/%s.yml' % args.template)
if len(template_files) < 1:
print 'ERROR: Template not found: %s' % args.template
sys.exit(1)
settings['template'] = args.template
# open the template file as a stream and process the yaml
stream = open(template_files[0], 'r')
docs = yaml.load_all(stream)
# apply template values to settings
for doc in docs:
for key, value in doc.items():
# print key+' --> '+str(value)
settings[key] = value
# print
# print json.dumps(settings, indent=2, sort_keys=True)
# command line arguments override template default values
if args.apis:
settings['apis'] = sorted(args.apis.split(','))
if args.billing_account:
settings['billing_account'] = args.billing_account
if args.default_service_account:
settings['default_service_account'] = args.default_service_account
if args.folder:
settings['folder'] = args.folder
if args.iam_policy:
settings['iam_policy'] = get_bindings(args.iam_policy)
if args.labels:
settings['labels'] = get_labels(args.labels)
if args.organization:
settings['organization'] = args.organization
if args.region:
settings['region'] = args.region
if args.service_accounts:
settings['service_accounts'] = sorted(args.service_accounts.split(','))
if args.usage_bucket:
settings['usage_bucket'] = args.usage_bucket
if args.zone:
settings['zone'] = args.region
return settings
def set_default_service_account(project_id, settings):
"""
Function: set_default_service_account.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
service_account = settings['default_service_account']
if not re.search('@', service_account):
domain = 'iam.gserviceaccount.com'
service_account = '%s@%s.%s' % (service_account, project_id, domain)
sys.stdout.write(' * setting default service account: %s...' %
service_account)
sys.stdout.flush()
result = google.set_default_service_account(project_id, service_account)
if result:
print 'successful.'
def set_iam_policy(project_id, settings):
"""
Function: set_iam_policy.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
iam_policy = settings['iam_policy']
policy = {
'bindings': []
}
for role in iam_policy:
binding = {
'role': 'roles/%s' % (role),
'members': [],
}
for member in iam_policy[role]:
binding['members'].append(member)
policy['bindings'].append(binding)
sys.stdout.write(' * updating IAM policy...')
sys.stdout.flush()
result = google.set_iam_policy(project_id, policy)
if result:
print 'successful.'
def update_labels(project_id, settings):
"""
Function: update_labels.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
labels = settings['labels']
labels_list = []
for label in sorted(labels):
labels_list.append('%s=%s' % (label, labels[label]))
sys.stdout.write(' * updating labels: %s...' % ','.join(labels_list))
sys.stdout.flush()
project = google.get_project(project_id)
if 'labels' not in project:
project['labels'] = {}
for label in labels:
if re.search('.=.', label):
(key, value) = label.split('=')
project['labels'][key.lower()] = value
result = google.update_project(project_id, project)
if result:
print 'successful.'
def update_project_metadata(project_id, settings):
"""
Function: update_project_metadata.
description
Args:
project_id - [type/description]
settings - [type/description]
Returns:
return description
"""
region = settings['region']
zone = settings['zone']
project = google.get_compute_project(project_id)
metadata = None
update = False
if 'commonInstanceMetadata' in project:
metadata = project['commonInstanceMetadata']
metadata_dict = {}
if 'items' in metadata:
for item in metadata['items']:
key = item['key']
value = item['value']
metadata_dict[key] = value
if region and (
('google-compute-default-region' in metadata_dict and
metadata_dict['google-compute-default-region'] != region)
or 'google-compute-default-region' not in metadata_dict
):
update = True
if zone and (
('google-compute-default-zone' in metadata_dict and
metadata_dict['google-compute-default-zone'] != zone)
or 'google-compute-default-zone' not in metadata_dict
):
update = True
else:
update = True
if update:
metadata_items = {
'items': []
}
if region:
metadata_items['items'].append({
'key': 'google-compute-default-region',
'value': region,
})
if zone:
metadata_items['items'].append({
'key': 'google-compute-default-zone',
'value': zone,
})
if metadata:
metadata_items['fingerprint'] = metadata['fingerprint']
sys.stdout.write(' * updating common instance metadata...')
sys.stdout.flush()
result = google.set_common_instance_metadata(
project_id,
metadata_items
)
if result:
print 'successful.'
def main():
"""Main function."""
# create arg parser
parser = create_arg_parser()
# parse the arguments
args = parser.parse_args()
# check args
folder_error = 'Provide either an organization or a folder, but not both.'
if args.organization and args.folder:
print 'ERROR: %s' % folder_error
sys.exit(1)
# connect to google
sys.stdout.write('Connecting to Google with default credentials...')
sys.stdout.flush()
# authenticate via the gcloud application-default credentials
google.auth()
print 'successful.'
# get effective settings
settings = get_effective_settings(args)
# display effective settings
display_settings(settings)
print '\nCreating Projects:'
for project_id in args.project_id:
print '\n %s:' % project_id
# create the project
project = create_project(project_id, settings)
# check if project aready exists
if not project and (settings['organization'] or settings['folder']):
project = google.get_project(project_id)
# check parent
current_parent = None
if 'parent' in project:
current_parent = project['parent']
parent = get_parent(settings)
if current_parent != parent:
parent_string = '%ss/%s' % (parent['type'], parent['id'])
sys.stdout.write(' * updating parent: %s...' % parent_string)
sys.stdout.flush()
update = google.update_project(project_id, {'parent': parent})
if update:
print 'successful.'
# create service accounts
if settings['service_accounts']:
create_service_accounts(project_id, settings)
# set IAM policies
if settings['iam_policy']:
set_iam_policy(project_id, settings)
# create project labels
if settings['labels']:
update_labels(project_id, settings)
# enable billing
if settings['billing_account']:
enable_billing(project_id, settings)
# enable apis
apis = []
if settings['apis']:
apis = enable_services(project_id, settings)
# check if compute api is enabled
if 'compute_component' in apis:
# default service account
if settings['default_service_account']:
set_default_service_account(project_id, settings)
# compute usage bucket
if settings['usage_bucket']:
create_usage_bucket(project_id, settings)
enable_usage_bucket(project_id, settings)
# update project metadata
if settings['region'] or settings['zone']:
update_project_metadata(project_id, settings)
base = 'https://console.cloud.google.com/home/dashboard?project='
print ' * console: %s%s' % (base, project_id)
if __name__ == "__main__":
main()