Skip to content

Commit 6274006

Browse files
committed
Miscellaneous updates to copy composer environment script.
* Fail fast if required executables missing * Pass project to storage client * Set bucket permissions without subprocess * Handle fuse umount on mac os
1 parent cc820e6 commit 6274006

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ install:
3232
- pip install --upgrade nox
3333
- pip install --upgrade git+https://github.com/dhermes/ci-diff-helper.git
3434
script:
35-
- "./scripts/travis.sh"
35+
- "./scripts/travis.sh"

composer/tools/copy_environment.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from __future__ import print_function
2929

3030
import argparse
31-
import ast
3231
import base64
3332
import contextlib
3433
import json
@@ -40,6 +39,7 @@
4039
import tempfile
4140
import time
4241
import uuid
42+
from distutils.spawn import find_executable
4343

4444
from cryptography import fernet
4545
import google.auth
@@ -52,6 +52,7 @@
5252
from six.moves import configparser
5353

5454
DEFAULT_SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
55+
EXECUTABLES = ['gcsfuse', 'cloud_sql_proxy', 'mysql', 'gcloud', 'gsutil']
5556

5657

5758
def parse_args():
@@ -294,7 +295,7 @@ def create_service_account_key(iam_client, project, service_account_name):
294295
)
295296
.execute()
296297
)
297-
service_account_key_decoded = ast.literal_eval(
298+
service_account_key_decoded = json.loads(
298299
base64.b64decode(service_account_key.get("privateKeyData", ""))
299300
)
300301
time.sleep(5)
@@ -333,16 +334,10 @@ def get_sql_instance_service_account(sql_client, project, instance):
333334

334335

335336
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:
346341
print(
347342
"Failed to set acls for service account {} on bucket {}.".format(
348343
service_account, gcs_bucket.name
@@ -542,7 +537,10 @@ def import_data(
542537
if proxy_subprocess:
543538
proxy_subprocess.kill()
544539
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])
546544
if tmp_dir_name:
547545
shutil.rmtree(tmp_dir_name)
548546

@@ -571,7 +569,9 @@ def copy_database(project, existing_env, new_env, running_as_service_account):
571569
try:
572570
# create default creds clients
573571
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+
)
575575
iam_client = discovery.build(
576576
"iam", "v1", credentials=default_credentials
577577
)
@@ -717,8 +717,19 @@ def clone_environment(
717717
)
718718

719719

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+
720730
if __name__ == "__main__":
721731
args = parse_args()
732+
check_executables()
722733
clone_environment(
723734
args.project,
724735
args.location,

composer/tools/copy_environment_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
def test_grant_rw_permissions_fails_gracefully(monkeypatch, capsys):
2424
mock_call = mock.Mock()
25-
mock_call.return_value = 1
25+
mock_call.side_effect = RuntimeError()
2626
monkeypatch.setattr(subprocess, 'call', mock_call)
2727
monkeypatch.setattr(time, 'sleep', lambda sec: None)
2828
from . import copy_environment

0 commit comments

Comments
 (0)