|
| 1 | +importos |
| 2 | +importio |
| 3 | +importsys |
| 4 | + |
| 5 | +importminio |
| 6 | +fromminioimportMinio |
| 7 | +fromminio.deleteobjectsimportDeleteObject |
| 8 | +importurllib3 |
| 9 | +frompg_probackup2.storage.fs_backupimportTestBackupDir |
| 10 | +frompg_probackup2.init_helpersimportinit_params |
| 11 | +froms3.test_utilsimportconfig_provider |
| 12 | + |
| 13 | +root=os.path.realpath(os.path.join(os.path.dirname(__file__),'../..')) |
| 14 | +ifrootnotinsys.path: |
| 15 | +sys.path.append(root) |
| 16 | + |
| 17 | +status_forcelist= [413,# RequestBodyTooLarge |
| 18 | +429,# TooManyRequests |
| 19 | +500,# InternalError |
| 20 | +503,# ServerBusy |
| 21 | + ] |
| 22 | + |
| 23 | +DEFAULT_CONF_FILE='s3/tests/s3.conf' |
| 24 | + |
| 25 | + |
| 26 | +classS3TestBackupDir(TestBackupDir): |
| 27 | +is_file_based=False |
| 28 | + |
| 29 | +def__init__(self,*,rel_path,backup): |
| 30 | +self.access_key=None |
| 31 | +self.secret_key=None |
| 32 | +self.s3_type=None |
| 33 | +self.tmp_path=None |
| 34 | +self.host=None |
| 35 | +self.port=None |
| 36 | +self.bucket_name=None |
| 37 | +self.region=None |
| 38 | +self.bucket=None |
| 39 | +self.path_suffix=None |
| 40 | +self.https=None |
| 41 | +self.s3_config_file=None |
| 42 | +self.ca_certificate=None |
| 43 | + |
| 44 | +self.set_s3_config_file() |
| 45 | +self.setup_s3_env() |
| 46 | + |
| 47 | +path="pg_probackup" |
| 48 | +ifself.path_suffix: |
| 49 | +path+="_"+self.path_suffix |
| 50 | +ifself.tmp_path==''oros.path.isabs(self.tmp_path): |
| 51 | +self.path=f"{path}{self.tmp_path}/{rel_path}/{backup}" |
| 52 | +else: |
| 53 | +self.path=f"{path}/{self.tmp_path}/{rel_path}/{backup}" |
| 54 | + |
| 55 | +secure:bool=False |
| 56 | +self.versioning:bool=False |
| 57 | +ifself.httpsin ['ON','HTTPS']: |
| 58 | +secure=True |
| 59 | +ifself.httpsandself.ca_certificate: |
| 60 | +http_client=urllib3.PoolManager(cert_reqs='CERT_REQUIRED', |
| 61 | +ca_certs=self.ca_certificate, |
| 62 | +retries=urllib3.Retry(total=5, |
| 63 | +backoff_factor=1, |
| 64 | +status_forcelist=status_forcelist)) |
| 65 | +else: |
| 66 | +http_client=urllib3.PoolManager(retries=urllib3.Retry(total=5, |
| 67 | +backoff_factor=1, |
| 68 | +status_forcelist=status_forcelist)) |
| 69 | + |
| 70 | +self.conn=Minio(self.host+":"+self.port,secure=secure,access_key=self.access_key, |
| 71 | +secret_key=self.secret_key,http_client=http_client) |
| 72 | +ifnotself.conn.bucket_exists(self.bucket): |
| 73 | +raiseException(f"Test bucket{self.bucket} does not exist.") |
| 74 | + |
| 75 | +try: |
| 76 | +config=self.conn.get_bucket_versioning(self.bucket) |
| 77 | +ifconfig.status.lower()=="enabled"orconfig.status.lower()=="suspended": |
| 78 | +self.versioning=True |
| 79 | +else: |
| 80 | +self.versioning=False |
| 81 | +exceptExceptionase: |
| 82 | +if"NotImplemented"inrepr(e): |
| 83 | +self.versioning=False |
| 84 | +else: |
| 85 | +raisee |
| 86 | +self.pb_args= ('-B','/'+self.path,f'--s3={init_params.s3_type}') |
| 87 | +ifself.s3_config_file: |
| 88 | +self.pb_args+= (f'--s3-config-file={self.s3_config_file}',) |
| 89 | +return |
| 90 | + |
| 91 | +defsetup_s3_env(self,s3_config=None): |
| 92 | +self.tmp_path=os.environ.get('PGPROBACKUP_TMP_DIR',default='') |
| 93 | +self.host=os.environ.get('PG_PROBACKUP_S3_HOST',default='') |
| 94 | + |
| 95 | +# If environment variables are not setup, use from config |
| 96 | +ifself.s3_config_fileors3_config: |
| 97 | +minio_config=config_provider.read_config(self.s3_config_fileors3_config) |
| 98 | +self.access_key=minio_config['access-key'] |
| 99 | +self.secret_key=minio_config['secret-key'] |
| 100 | +self.host=minio_config['s3-host'] |
| 101 | +self.port=minio_config['s3-port'] |
| 102 | +self.bucket=minio_config['s3-bucket'] |
| 103 | +self.region=minio_config['s3-region'] |
| 104 | +self.https=minio_config['s3-secure'] |
| 105 | +init_params.s3_type='minio' |
| 106 | +else: |
| 107 | +self.access_key=os.environ.get('PG_PROBACKUP_S3_ACCESS_KEY') |
| 108 | +self.secret_key=os.environ.get('PG_PROBACKUP_S3_SECRET_ACCESS_KEY') |
| 109 | +self.host=os.environ.get('PG_PROBACKUP_S3_HOST') |
| 110 | +self.port=os.environ.get('PG_PROBACKUP_S3_PORT') |
| 111 | +self.bucket=os.environ.get('PG_PROBACKUP_S3_BUCKET_NAME') |
| 112 | +self.region=os.environ.get('PG_PROBACKUP_S3_REGION') |
| 113 | +self.https=os.environ.get('PG_PROBACKUP_S3_HTTPS') |
| 114 | +self.ca_certificate=os.environ.get('PG_PROBACKUP_S3_CA_CERTIFICATE') |
| 115 | +init_params.s3_type=os.environ.get('PG_PROBACKUP_S3_TEST') |
| 116 | + |
| 117 | +# multi-url case |
| 118 | +# remove all urls from string except the first one |
| 119 | +if';'inself.host: |
| 120 | +self.host=self.host[:self.host.find(';')] |
| 121 | +if':'inself.host:# also change port if it was overridden in multihost string |
| 122 | +self.port=self.host[self.host.find(':')+1:] |
| 123 | +self.host=self.host[:self.host.find(':')] |
| 124 | + |
| 125 | +defset_s3_config_file(self): |
| 126 | +s3_config=os.environ.get('PG_PROBACKUP_S3_CONFIG_FILE') |
| 127 | +ifs3_configisnotNoneands3_config.strip().lower()=="true": |
| 128 | +self.s3_config_file=DEFAULT_CONF_FILE |
| 129 | +else: |
| 130 | +self.s3_config_file=s3_config |
| 131 | + |
| 132 | +deflist_instance_backups(self,instance): |
| 133 | +full_path=os.path.join(self.path,'backups',instance) |
| 134 | +candidates=self.conn.list_objects(self.bucket,prefix=full_path,recursive=True) |
| 135 | +return [os.path.basename(os.path.dirname(x.object_name)) |
| 136 | +forxincandidatesifx.object_name.endswith('backup.control')] |
| 137 | + |
| 138 | +deflist_files(self,sub_dir,recursive=False): |
| 139 | +full_path=os.path.join(self.path,sub_dir) |
| 140 | +# Need '/' in the end to find inside the folder |
| 141 | +full_path_dir=full_pathiffull_path[-1]=='/'elsefull_path+'/' |
| 142 | +object_list=self.conn.list_objects(self.bucket,prefix=full_path_dir,recursive=recursive) |
| 143 | +return [obj.object_name.replace(full_path_dir,'',1) |
| 144 | +forobjinobject_list |
| 145 | +ifnotobj.is_dir] |
| 146 | + |
| 147 | +deflist_dirs(self,sub_dir): |
| 148 | +full_path=os.path.join(self.path,sub_dir) |
| 149 | +# Need '/' in the end to find inside the folder |
| 150 | +full_path_dir=full_pathiffull_path[-1]=='/'elsefull_path+'/' |
| 151 | +object_list=self.conn.list_objects(self.bucket,prefix=full_path_dir,recursive=False) |
| 152 | +return [obj.object_name.replace(full_path_dir,'',1).rstrip('\\/') |
| 153 | +forobjinobject_list |
| 154 | +ifobj.is_dir] |
| 155 | + |
| 156 | +defread_file(self,sub_path,*,text=True): |
| 157 | +full_path=os.path.join(self.path,sub_path) |
| 158 | +bytes=self.conn.get_object(self.bucket,full_path).read() |
| 159 | +ifnottext: |
| 160 | +returnbytes |
| 161 | +returnbytes.decode('utf-8') |
| 162 | + |
| 163 | +defwrite_file(self,sub_path,data,*,text=True): |
| 164 | +full_path=os.path.join(self.path,sub_path) |
| 165 | +iftext: |
| 166 | +data=data.encode('utf-8') |
| 167 | +self.conn.put_object(self.bucket,full_path,io.BytesIO(data),length=len(data)) |
| 168 | + |
| 169 | +defcleanup(self,dir=''): |
| 170 | +self.remove_dir(dir) |
| 171 | + |
| 172 | +defremove_file(self,sub_path): |
| 173 | +full_path=os.path.join(self.path,sub_path) |
| 174 | +self.conn.remove_object(self.bucket,full_path) |
| 175 | + |
| 176 | +defremove_dir(self,sub_path): |
| 177 | +ifsub_path: |
| 178 | +full_path=os.path.join(self.path,sub_path) |
| 179 | +else: |
| 180 | +full_path=self.path |
| 181 | +objs=self.conn.list_objects(self.bucket,prefix=full_path,recursive=True, |
| 182 | +include_version=self.versioning) |
| 183 | +delobjs= (DeleteObject(o.object_name,o.version_id)foroinobjs) |
| 184 | +errs=list(self.conn.remove_objects(self.bucket,delobjs)) |
| 185 | +iferrs: |
| 186 | +strerrs="; ".join(str(err)forerrinerrs) |
| 187 | +raiseException("There were errors: {0}".format(strerrs)) |
| 188 | + |
| 189 | +defexists(self,sub_path): |
| 190 | +full_path=os.path.join(self.path,sub_path) |
| 191 | +try: |
| 192 | +self.conn.stat_object(self.bucket,full_path) |
| 193 | +returnTrue |
| 194 | +exceptminio.error.S3Errorass3err: |
| 195 | +ifs3err.code=='NoSuchKey': |
| 196 | +returnFalse |
| 197 | +raises3err |
| 198 | +exceptExceptionaserr: |
| 199 | +raiseerr |
| 200 | + |
| 201 | +def__str__(self): |
| 202 | +return'/'+self.path |
| 203 | + |
| 204 | +def__repr__(self): |
| 205 | +return"S3TestBackupDir"+str(self.path) |
| 206 | + |
| 207 | +def__fspath__(self): |
| 208 | +returnself.path |