~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to backup-to-s3.py

  • Committer: Curtis Hovey
  • Date: 2014-08-12 20:35:41 UTC
  • mto: This revision was merged to the branch mainline in revision 623.
  • Revision ID: curtis@canonical.com-20140812203541-ebdge5usoltynmk1
Strip the /tools dir from from the tools-metadata-url because s3 will return a 404
for a dir.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
"""Backup Jenkins data to S3 and remove old backups."""
3
3
 
4
 
from __future__ import print_function
5
 
 
6
4
from datetime import datetime
 
5
import os
7
6
import re
8
 
 
9
 
from utility import s3_cmd
 
7
import subprocess
10
8
 
11
9
 
12
10
MAX_BACKUPS = 10
28
26
    ]
29
27
 
30
28
 
 
29
def s3_cmd(params, drop_output=False):
 
30
    s3cfg_path = os.path.join(
 
31
        os.environ['HOME'], 'cloud-city/juju-qa.s3cfg')
 
32
    if drop_output:
 
33
        return subprocess.check_call(
 
34
            ['s3cmd', '-c', s3cfg_path] + params, stdout=open('/dev/null', 'w'))
 
35
    else:
 
36
        return subprocess.check_output(
 
37
            ['s3cmd', '-c', s3cfg_path] + params)
 
38
 
 
39
 
31
40
def current_backups():
32
41
    """Return a list of S3 URLs of existing backups."""
33
42
    # We expect lines like
34
43
    # "         DIR   s3://juju-qa-data/juju-ci/backups/2014-07-25/"
35
44
    result = []
36
45
    for line in s3_cmd(['ls', BACKUP_URL]).split('\n'):
37
 
        mo = re.search(r'^\s+DIR\s+(%s\d\d\d\d-\d\d-\d\d/)$' % BACKUP_URL,
38
 
                       line)
 
46
        mo = re.search(r'^\s+DIR\s+(%s\d\d\d\d-\d\d-\d\d/)$' % BACKUP_URL, line)
39
47
        if mo is None:
40
48
            continue
41
49
        url = mo.group(1)
57
65
    today = datetime.now().strftime('%Y-%m-%d')
58
66
    todays_url = '%s%s/' % (BACKUP_URL, today)
59
67
    if todays_url in all_backups:
60
 
        print("backup for %s already exists." % today)
 
68
        print "backup for %s already exists." % today
61
69
    else:
62
70
        run_backup(todays_url)
63
71
        all_backups.append(todays_url)