]>
Commit | Line | Data |
---|---|---|
05a536ef TL |
1 | #!/usr/bin/env python3 |
2 | ||
3 | import errno | |
4 | import subprocess | |
5 | import logging as log | |
6 | import boto3 | |
7 | import botocore.exceptions | |
8 | ||
9 | log.basicConfig(format = '%(message)s', level=log.DEBUG) | |
10 | log.getLogger('botocore').setLevel(log.CRITICAL) | |
11 | log.getLogger('boto3').setLevel(log.CRITICAL) | |
12 | log.getLogger('urllib3').setLevel(log.CRITICAL) | |
13 | ||
14 | def exec_cmd(cmd, wait = True, **kwargs): | |
15 | check_retcode = kwargs.pop('check_retcode', True) | |
16 | kwargs['shell'] = True | |
17 | kwargs['stdout'] = subprocess.PIPE | |
18 | proc = subprocess.Popen(cmd, **kwargs) | |
19 | log.info(proc.args) | |
20 | if wait: | |
21 | out, _ = proc.communicate() | |
22 | if check_retcode: | |
23 | assert(proc.returncode == 0) | |
24 | return out | |
25 | return (out, proc.returncode) | |
26 | return '' | |
27 | ||
28 | def create_user(uid, display_name, access_key, secret_key): | |
29 | _, ret = exec_cmd(f'radosgw-admin user create --uid {uid} --display-name "{display_name}" --access-key {access_key} --secret {secret_key}', check_retcode=False) | |
30 | assert(ret == 0 or errno.EEXIST) | |
31 | ||
32 | def boto_connect(access_key, secret_key, config=None): | |
33 | def try_connect(portnum, ssl, proto): | |
34 | endpoint = proto + '://localhost:' + portnum | |
35 | conn = boto3.resource('s3', | |
36 | aws_access_key_id=access_key, | |
37 | aws_secret_access_key=secret_key, | |
38 | use_ssl=ssl, | |
39 | endpoint_url=endpoint, | |
40 | verify=False, | |
41 | config=config, | |
42 | ) | |
43 | try: | |
44 | list(conn.buckets.limit(1)) # just verify we can list buckets | |
45 | except botocore.exceptions.ConnectionError as e: | |
46 | print(e) | |
47 | raise | |
48 | print('connected to', endpoint) | |
49 | return conn | |
50 | try: | |
51 | return try_connect('80', False, 'http') | |
52 | except botocore.exceptions.ConnectionError: | |
53 | try: # retry on non-privileged http port | |
54 | return try_connect('8000', False, 'http') | |
55 | except botocore.exceptions.ConnectionError: | |
56 | # retry with ssl | |
57 | return try_connect('443', True, 'https') |