|
| 1 | +""" Module for storing snapshots in shared local disk """ |
| 2 | +import logging |
| 3 | +import os |
| 4 | +from shutil import copyfile |
| 5 | + |
| 6 | +from manager.env import env, to_flag |
| 7 | +from manager.utils import debug |
| 8 | +from minio import Minio as pyminio, error as minioerror |
| 9 | + |
| 10 | +logging.getLogger('manta').setLevel(logging.INFO) |
| 11 | + |
| 12 | +class Minio(object): |
| 13 | + """ |
| 14 | +
|
| 15 | + The Minio class wraps access to the Minio object store, where we'll put |
| 16 | + our MySQL backups. |
| 17 | + """ |
| 18 | + def __init__(self, envs=os.environ): |
| 19 | + self.access_key = env('MINIO_ACCESS_KEY', None, envs) |
| 20 | + self.secret_key = env('MINIO_SECRET_KEY', None, envs) |
| 21 | + self.bucket = env('MINIO_BUCKET', 'backups', envs) |
| 22 | + self.location = env('MINIO_LOCATION', 'us-east-1', envs) |
| 23 | + self.url = env('MINIO_URL', 'minio:9000') |
| 24 | + is_tls = env('MINIO_TLS_SECURE', False, envs, fn=to_flag) |
| 25 | + |
| 26 | + self.client = pyminio( |
| 27 | + self.url, |
| 28 | + access_key=self.access_key, |
| 29 | + secret_key=self.secret_key, |
| 30 | + secure=is_tls) |
| 31 | + try: |
| 32 | + self.client.make_bucket(self.bucket, location=self.location) |
| 33 | + except minioerror.BucketAlreadyOwnedByYou: |
| 34 | + pass |
| 35 | + |
| 36 | + @debug |
| 37 | + def get_backup(self, backup_id): |
| 38 | + """ |
| 39 | + Download file from Minio, allowing exceptions to bubble up. |
| 40 | + """ |
| 41 | + try: |
| 42 | + os.mkdir('/tmp/backup', 0770) |
| 43 | + except OSError: |
| 44 | + pass |
| 45 | + outfile = '/tmp/backup/{}'.format(backup_id) |
| 46 | + self.client.fget_object(self.bucket, backup_id, outfile) |
| 47 | + |
| 48 | + def put_backup(self, backup_id, infile): |
| 49 | + """ |
| 50 | + Upload the backup file to the expected path. |
| 51 | + """ |
| 52 | + self.client.fput_object(self.bucket, backup_id, infile) |
| 53 | + return backup_id |
0 commit comments