|
28 | 28 | from __future__ import print_function
|
29 | 29 |
|
30 | 30 | import argparse
|
31 |
| -import ast |
32 | 31 | import base64
|
33 | 32 | import contextlib
|
34 | 33 | import json
|
|
40 | 39 | import tempfile
|
41 | 40 | import time
|
42 | 41 | import uuid
|
| 42 | +from distutils.spawn import find_executable |
43 | 43 |
|
44 | 44 | from cryptography import fernet
|
45 | 45 | import google.auth
|
|
52 | 52 | from six.moves import configparser
|
53 | 53 |
|
54 | 54 | DEFAULT_SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
|
| 55 | +EXECUTABLES = ['gcsfuse', 'cloud_sql_proxy', 'mysql', 'gcloud', 'gsutil'] |
55 | 56 |
|
56 | 57 |
|
57 | 58 | def parse_args():
|
@@ -294,7 +295,7 @@ def create_service_account_key(iam_client, project, service_account_name):
|
294 | 295 | )
|
295 | 296 | .execute()
|
296 | 297 | )
|
297 |
| - service_account_key_decoded = ast.literal_eval( |
| 298 | + service_account_key_decoded = json.loads( |
298 | 299 | base64.b64decode(service_account_key.get("privateKeyData", ""))
|
299 | 300 | )
|
300 | 301 | time.sleep(5)
|
@@ -333,16 +334,10 @@ def get_sql_instance_service_account(sql_client, project, instance):
|
333 | 334 |
|
334 | 335 |
|
335 | 336 | def grant_rw_permissions(gcs_bucket, service_account):
|
336 |
| - if subprocess.call( |
337 |
| - [ |
338 |
| - "gsutil", |
339 |
| - "acl", |
340 |
| - "ch", |
341 |
| - "-u", |
342 |
| - service_account + ":O", |
343 |
| - "gs://" + gcs_bucket.name, |
344 |
| - ] |
345 |
| - ): |
| 337 | + try: |
| 338 | + gcs_bucket.acl.user(service_account).grant_owner() |
| 339 | + gcs_bucket.acl.save() |
| 340 | + except Exception: |
346 | 341 | print(
|
347 | 342 | "Failed to set acls for service account {} on bucket {}.".format(
|
348 | 343 | service_account, gcs_bucket.name
|
@@ -542,7 +537,10 @@ def import_data(
|
542 | 537 | if proxy_subprocess:
|
543 | 538 | proxy_subprocess.kill()
|
544 | 539 | if fuse_dir:
|
545 |
| - subprocess.call(["fusermount", "-u", fuse_dir]) |
| 540 | + try: |
| 541 | + subprocess.call(["fusermount", "-u", fuse_dir]) |
| 542 | + except OSError: |
| 543 | + subprocess.call(["umount", fuse_dir]) |
546 | 544 | if tmp_dir_name:
|
547 | 545 | shutil.rmtree(tmp_dir_name)
|
548 | 546 |
|
@@ -571,7 +569,9 @@ def copy_database(project, existing_env, new_env, running_as_service_account):
|
571 | 569 | try:
|
572 | 570 | # create default creds clients
|
573 | 571 | default_credentials, _ = google.auth.default(scopes=DEFAULT_SCOPES)
|
574 |
| - storage_client = storage.Client(credentials=default_credentials) |
| 572 | + storage_client = storage.Client( |
| 573 | + project=project, credentials=default_credentials |
| 574 | + ) |
575 | 575 | iam_client = discovery.build(
|
576 | 576 | "iam", "v1", credentials=default_credentials
|
577 | 577 | )
|
@@ -717,8 +717,19 @@ def clone_environment(
|
717 | 717 | )
|
718 | 718 |
|
719 | 719 |
|
| 720 | +def check_executables(): |
| 721 | + not_found = [ |
| 722 | + executable for executable in EXECUTABLES |
| 723 | + if not find_executable(executable) |
| 724 | + ] |
| 725 | + if not_found: |
| 726 | + print('Required executables not found: {}'.format(' '.join(not_found))) |
| 727 | + sys.exit(1) |
| 728 | + |
| 729 | + |
720 | 730 | if __name__ == "__main__":
|
721 | 731 | args = parse_args()
|
| 732 | + check_executables() |
722 | 733 | clone_environment(
|
723 | 734 | args.project,
|
724 | 735 | args.location,
|
|
0 commit comments