|
| 1 | +importos |
| 2 | +importio |
| 3 | +importsys |
| 4 | + |
| 5 | +importminio |
| 6 | +fromminioimportMinio |
| 7 | +fromminio.deleteobjectsimportDeleteObject |
| 8 | +importurllib3 |
| 9 | + |
| 10 | +root=os.path.realpath(os.path.join(os.path.dirname(__file__),'../..')) |
| 11 | +ifrootnotinsys.path: |
| 12 | +sys.path.append(root) |
| 13 | + |
| 14 | +from .fs_backupimportTestBackupDir |
| 15 | + |
| 16 | +# Should fail if either of env vars does not exist |
| 17 | +host=os.environ['PG_PROBACKUP_S3_HOST'] |
| 18 | +port=os.environ['PG_PROBACKUP_S3_PORT'] |
| 19 | +access=os.environ['PG_PROBACKUP_S3_ACCESS_KEY'] |
| 20 | +secret=os.environ['PG_PROBACKUP_S3_SECRET_ACCESS_KEY'] |
| 21 | +bucket=os.environ['PG_PROBACKUP_S3_BUCKET_NAME'] |
| 22 | +path_suffix=os.environ.get("PG_PROBACKUP_TEST_TMP_SUFFIX") |
| 23 | +https=os.environ.get("PG_PROBACKUP_S3_HTTPS") |
| 24 | + |
| 25 | +s3_type=os.environ.get('PG_PROBACKUP_S3_TEST',os.environ.get('PROBACKUP_S3_TYPE_FULL_TEST')) |
| 26 | +tmp_path=os.environ.get('PGPROBACKUP_TMP_DIR',default='') |
| 27 | + |
| 28 | +status_forcelist= [413,# RequestBodyTooLarge |
| 29 | +429,# TooManyRequests |
| 30 | +500,# InternalError |
| 31 | +503,# ServerBusy |
| 32 | + ] |
| 33 | + |
| 34 | + |
| 35 | +classS3TestBackupDir(TestBackupDir): |
| 36 | +is_file_based=False |
| 37 | + |
| 38 | +def__init__(self,*,rel_path,backup): |
| 39 | +path="pg_probackup" |
| 40 | +ifpath_suffix: |
| 41 | +path+="_"+path_suffix |
| 42 | +iftmp_path==''oros.path.isabs(tmp_path): |
| 43 | +self.path=f"{path}{tmp_path}/{rel_path}/{backup}" |
| 44 | +else: |
| 45 | +self.path=f"{path}/{tmp_path}/{rel_path}/{backup}" |
| 46 | + |
| 47 | +secure:bool=False |
| 48 | +ifhttpsin ['ON','HTTPS']: |
| 49 | +secure=True |
| 50 | +self.conn=Minio(host+":"+port,secure=secure,access_key=access, |
| 51 | +secret_key=secret,http_client=urllib3.PoolManager(retries=urllib3.Retry(total=5, |
| 52 | +backoff_factor=1, |
| 53 | +status_forcelist=status_forcelist))) |
| 54 | +ifnotself.conn.bucket_exists(bucket): |
| 55 | +raiseException(f"Test bucket{bucket} does not exist.") |
| 56 | +self.pb_args= ('-B','/'+self.path,f'--s3={s3_type}') |
| 57 | +return |
| 58 | + |
| 59 | +deflist_instance_backups(self,instance): |
| 60 | +full_path=os.path.join(self.path,'backups',instance) |
| 61 | +candidates=self.conn.list_objects(bucket,prefix=full_path,recursive=True) |
| 62 | +return [os.path.basename(os.path.dirname(x.object_name)) |
| 63 | +forxincandidatesifx.object_name.endswith('backup.control')] |
| 64 | + |
| 65 | +deflist_files(self,sub_dir,recursive=False): |
| 66 | +full_path=os.path.join(self.path,sub_dir) |
| 67 | +# Need '/' in the end to find inside the folder |
| 68 | +full_path_dir=full_pathiffull_path[-1]=='/'elsefull_path+'/' |
| 69 | +object_list=self.conn.list_objects(bucket,prefix=full_path_dir,recursive=recursive) |
| 70 | +return [obj.object_name.replace(full_path_dir,'',1) |
| 71 | +forobjinobject_list |
| 72 | +ifnotobj.is_dir] |
| 73 | + |
| 74 | +deflist_dirs(self,sub_dir): |
| 75 | +full_path=os.path.join(self.path,sub_dir) |
| 76 | +# Need '/' in the end to find inside the folder |
| 77 | +full_path_dir=full_pathiffull_path[-1]=='/'elsefull_path+'/' |
| 78 | +object_list=self.conn.list_objects(bucket,prefix=full_path_dir,recursive=False) |
| 79 | +return [obj.object_name.replace(full_path_dir,'',1).rstrip('\\/') |
| 80 | +forobjinobject_list |
| 81 | +ifobj.is_dir] |
| 82 | + |
| 83 | +defread_file(self,sub_path,*,text=True): |
| 84 | +full_path=os.path.join(self.path,sub_path) |
| 85 | +bytes=self.conn.get_object(bucket,full_path).read() |
| 86 | +ifnottext: |
| 87 | +returnbytes |
| 88 | +returnbytes.decode('utf-8') |
| 89 | + |
| 90 | +defwrite_file(self,sub_path,data,*,text=True): |
| 91 | +full_path=os.path.join(self.path,sub_path) |
| 92 | +iftext: |
| 93 | +data=data.encode('utf-8') |
| 94 | +self.conn.put_object(bucket,full_path,io.BytesIO(data),length=len(data)) |
| 95 | + |
| 96 | +defcleanup(self): |
| 97 | +self.remove_dir('') |
| 98 | + |
| 99 | +defremove_file(self,sub_path): |
| 100 | +full_path=os.path.join(self.path,sub_path) |
| 101 | +self.conn.remove_object(bucket,full_path) |
| 102 | + |
| 103 | +defremove_dir(self,sub_path): |
| 104 | +ifsub_path: |
| 105 | +full_path=os.path.join(self.path,sub_path) |
| 106 | +else: |
| 107 | +full_path=self.path |
| 108 | +objs=self.conn.list_objects(bucket,prefix=full_path,recursive=True, |
| 109 | +include_version=True) |
| 110 | +delobjs= (DeleteObject(o.object_name,o.version_id)foroinobjs) |
| 111 | +errs=list(self.conn.remove_objects(bucket,delobjs)) |
| 112 | +iferrs: |
| 113 | +strerrs="; ".join(str(err)forerrinerrs) |
| 114 | +raiseException("There were errors: {0}".format(strerrs)) |
| 115 | + |
| 116 | +defexists(self,sub_path): |
| 117 | +full_path=os.path.join(self.path,sub_path) |
| 118 | +try: |
| 119 | +self.conn.stat_object(bucket,full_path) |
| 120 | +returnTrue |
| 121 | +exceptminio.error.S3Errorass3err: |
| 122 | +ifs3err.code=='NoSuchKey': |
| 123 | +returnFalse |
| 124 | +raises3err |
| 125 | +exceptExceptionaserr: |
| 126 | +raiseerr |
| 127 | + |
| 128 | +def__str__(self): |
| 129 | +return'/'+self.path |
| 130 | + |
| 131 | +def__repr__(self): |
| 132 | +return"S3TestBackupDir"+str(self.path) |
| 133 | + |
| 134 | +def__fspath__(self): |
| 135 | +returnself.path |