def setUpModule(): if USE_EMULATOR: from google.auth.credentials import AnonymousCredentials emulator_project = os.getenv("GCLOUD_PROJECT", "emulator-test-project") Config.CLIENT = Client(project=emulator_project, credentials=AnonymousCredentials()) else: Config.CLIENT = Client() retry = RetryErrors(exceptions.ServiceUnavailable) configs = list(retry(Config.CLIENT.list_instance_configs)()) instances = retry(_list_instances)() EXISTING_INSTANCES[:] = instances # Delete test instances that are older than an hour. cutoff = int(time.time()) - 1 * 60 * 60 for instance_pb in Config.CLIENT.list_instances( "labels.python-spanner-dbapi-systests:true"): instance = Instance.from_pb(instance_pb, Config.CLIENT) if "created" not in instance.labels: continue create_time = int(instance.labels["created"]) if create_time > cutoff: continue # Instance cannot be deleted while backups exist. for backup_pb in instance.list_backups(): backup = Backup.from_pb(backup_pb, instance) backup.delete() instance.delete() if CREATE_INSTANCE: if not USE_EMULATOR: # Defend against back-end returning configs for regions we aren't # actually allowed to use. configs = [config for config in configs if "-us-" in config.name] if not configs: raise ValueError("List instance configs failed in module set up.") Config.INSTANCE_CONFIG = configs[0] config_name = configs[0].name create_time = str(int(time.time())) labels = { "python-spanner-dbapi-systests": "true", "created": create_time } Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID, config_name, labels=labels) created_op = Config.INSTANCE.create() created_op.result( SPANNER_OPERATION_TIMEOUT_IN_SECONDS) # block until completion else: Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID) Config.INSTANCE.reload()
def _item_to_instance(self, iterator, instance_pb): """Convert an instance protobuf to the native object. :type iterator: :class:`~google.api_core.page_iterator.Iterator` :param iterator: The iterator that is currently in use. :type instance_pb: :class:`~google.spanner.admin.instance.v1.Instance` :param instance_pb: An instance returned from the API. :rtype: :class:`~google.cloud.spanner_v1.instance.Instance` :returns: The next instance in the page. """ return Instance.from_pb(instance_pb, self)
def cleanup_old_instances(spanner_client): # Delete test instances that are older than an hour. cutoff = int(time.time()) - 1 * 60 * 60 instance_pbs = spanner_client.list_instances( "labels.cloud_spanner_samples:true") for instance_pb in instance_pbs: instance = Instance.from_pb(instance_pb, spanner_client) if "created" not in instance.labels: continue create_time = int(instance.labels["created"]) if create_time > cutoff: continue for backup_pb in instance.list_backups(): backup = Backup.from_pb(backup_pb, instance) backup.delete() instance.delete()
def delete_stale_test_instances(): """Delete test instances that are older than four hours.""" cutoff = int(time.time()) - 4 * 60 * 60 instances_pbs = CLIENT.list_instances( "labels.python-spanner-sqlalchemy-systest:true") for instance_pb in instances_pbs: instance = Instance.from_pb(instance_pb, CLIENT) if "created" not in instance.labels: continue create_time = int(instance.labels["created"]) if create_time > cutoff: continue # Backups are not used in sqlalchemy dialect test, # therefore instance can just be deleted. try: instance.delete() time.sleep(5) # Sleep for 5 seconds to give time for cooldown. except ResourceExhausted: print( "Unable to drop stale instance '{}'. May need manual delete.". format(instance.instance_id))