Skip to content

Commit 528273f

Browse files
jmcarpengelke
authored andcommitted
Miscellaneous updates to copy composer environment script. (GoogleCloudPlatform#1919)
* 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 * Remove import platform no longer used
1 parent dc843e2 commit 528273f

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

.travis.yml

+1-1
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

+24-18
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,18 @@
2828
from __future__ import print_function
2929

3030
import argparse
31-
import ast
3231
import base64
3332
import contextlib
3433
import json
3534
import os
36-
import platform
3735
import re
3836
import shutil
3937
import subprocess
4038
import sys
4139
import tempfile
4240
import time
4341
import uuid
42+
from distutils.spawn import find_executable
4443

4544
from cryptography import fernet
4645
import google.auth
@@ -53,6 +52,7 @@
5352
from six.moves import configparser
5453

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

5757

5858
def parse_args():
@@ -295,7 +295,7 @@ def create_service_account_key(iam_client, project, service_account_name):
295295
)
296296
.execute()
297297
)
298-
service_account_key_decoded = ast.literal_eval(
298+
service_account_key_decoded = json.loads(
299299
base64.b64decode(service_account_key.get("privateKeyData", ""))
300300
.decode("utf-8")
301301
)
@@ -335,16 +335,10 @@ def get_sql_instance_service_account(sql_client, project, instance):
335335

336336

337337
def grant_rw_permissions(gcs_bucket, service_account):
338-
if subprocess.call(
339-
[
340-
"gsutil",
341-
"acl",
342-
"ch",
343-
"-u",
344-
service_account + ":O",
345-
"gs://" + gcs_bucket.name,
346-
]
347-
):
338+
try:
339+
gcs_bucket.acl.user(service_account).grant_owner()
340+
gcs_bucket.acl.save()
341+
except Exception:
348342
print(
349343
"Failed to set acls for service account {} on bucket {}.".format(
350344
service_account, gcs_bucket.name
@@ -544,11 +538,10 @@ def import_data(
544538
if proxy_subprocess:
545539
proxy_subprocess.kill()
546540
if fuse_dir:
547-
if platform.system().lower().startswith('darwin'):
548-
# Mac OSX does not have fusermount
549-
subprocess.call(["umount", fuse_dir])
550-
else:
541+
try:
551542
subprocess.call(["fusermount", "-u", fuse_dir])
543+
except OSError:
544+
subprocess.call(["umount", fuse_dir])
552545
if tmp_dir_name:
553546
shutil.rmtree(tmp_dir_name)
554547

@@ -577,7 +570,9 @@ def copy_database(project, existing_env, new_env, running_as_service_account):
577570
try:
578571
# create default creds clients
579572
default_credentials, _ = google.auth.default(scopes=DEFAULT_SCOPES)
580-
storage_client = storage.Client(credentials=default_credentials)
573+
storage_client = storage.Client(
574+
project=project, credentials=default_credentials
575+
)
581576
iam_client = discovery.build(
582577
"iam", "v1", credentials=default_credentials
583578
)
@@ -723,8 +718,19 @@ def clone_environment(
723718
)
724719

725720

721+
def check_executables():
722+
not_found = [
723+
executable for executable in EXECUTABLES
724+
if not find_executable(executable)
725+
]
726+
if not_found:
727+
print('Required executables not found: {}'.format(' '.join(not_found)))
728+
sys.exit(1)
729+
730+
726731
if __name__ == "__main__":
727732
args = parse_args()
733+
check_executables()
728734
clone_environment(
729735
args.project,
730736
args.location,

composer/tools/copy_environment_test.py

+1-1
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)