~juju-qa/juju-ci-tools/trunk

1708 by Curtis Hovey
Added s3check.py used on s390x and vsphere to verify connectivity.
1
#!/usr/bin/env python
2
from __future__ import print_function
3
4
from argparse import ArgumentParser
5
from ConfigParser import ConfigParser
6
import logging
7
import os
8
import re
9
import sys
10
11
from boto.s3.connection import S3Connection
12
13
14
JUJU_QA_DATA = 'juju-qa-data'
15
JUJU_PIP_ARCHIVES = 'juju-pip-archives'
16
CLOUD_CITY = os.path.join(os.environ['HOME'], 'cloud-city')
17
jujuqa_config = os.path.join(CLOUD_CITY, 'juju-qa.s3cfg')
18
19
20
log = logging.getLogger("s3-check")
21
handler = logging.StreamHandler(sys.stderr)
22
handler.setFormatter(logging.Formatter(
23
    fmt='%(asctime)s %(levelname)s %(message)s',
24
    datefmt='%Y-%m-%d %H:%M:%S'))
25
log.addHandler(handler)
26
log.setLevel(logging.INFO)
27
28
29
def parse_args(args=None):
30
    parser = ArgumentParser()
31
    parser.add_argument(
32
        '--configs', nargs='*', default=[jujuqa_config],
33
        help='The job to get files from')
34
    parser.add_argument(
35
        'buckets', nargs='*', default=[JUJU_QA_DATA, JUJU_PIP_ARCHIVES],
36
        help='The job to get files from')
37
38
    return parser.parse_args(args)
39
40
41
def get_s3_credentials(s3cfg_path):
42
    config = ConfigParser()
43
    with open(s3cfg_path) as fp:
44
        config.readfp(fp)
45
    access_key = config.get('default', 'access_key')
46
    secret_key = config.get('default', 'secret_key')
47
    return access_key, secret_key
48
49
50
def get_qa_data_bucket(config, bucket):
51
    credentials = get_s3_credentials(config)
52
    conn = S3Connection(*credentials)
53
    return conn.get_bucket(bucket)
54
55
56
def list_file_keys(bucket, prefix, file_regex):
57
    pattern = re.compile(file_regex)
58
    keys = bucket.list(prefix)
59
    filtered = []
60
    for number, key in enumerate(keys):
61
        if pattern.match(key.name):
62
            filtered.append(key.name)
63
        if number >= 5:
64
            break
65
    return filtered
66
67
68
def main():
69
    args = parse_args()
70
    for config in args.configs:
71
        for bucket in args.buckets:
72
            log.info('trying {} with {}'.format(config, bucket))
73
            bucket = get_qa_data_bucket(config, bucket)
74
            log.info(bucket.generate_url(999999999))
75
            keys = list_file_keys(bucket, '', '.*')
76
            for key in keys:
77
                log.info('    Found {}'.format(key))
78
79
80
if __name__ == '__main__':
81
    sys.exit(main())