def send_mail(subject, message, receiver_email=None): if PCWConfig.has('notify'): smtp_server = PCWConfig.get_feature_property('notify', 'smtp') port = PCWConfig.get_feature_property('notify', 'smtp-port') sender_email = PCWConfig.get_feature_property('notify', 'from') if receiver_email is None: receiver_email = PCWConfig.get_feature_property('notify', 'to') mimetext = MIMEText(message) mimetext['Subject'] = '[Openqa-Cloud-Watch] {}'.format(subject) mimetext['From'] = sender_email mimetext['To'] = receiver_email logger.info("Send Email To:'%s' Subject:'[Openqa-Cloud-Watch] %s'", receiver_email, subject) server = smtplib.SMTP(smtp_server, port) server.ehlo() server.sendmail(sender_email, receiver_email.split(','), mimetext.as_string())
def send_cluster_notification(namespace, clusters): if len(clusters) and PCWConfig.has('notify'): clusters_str = ' '.join([str(cluster) for cluster in clusters]) logger.debug("Full clusters list - %s", clusters_str) send_mail("EC2 clusters found", clusters_str, receiver_email=PCWConfig.get_feature_property( 'cluster.notify', 'to', namespace))
def test_get_feature_property_from_pcw_ini_feature(pcw_file): set_pcw_ini( pcw_file, """ [cleanup] max-images-per-flavor = 666 azure-storage-resourcegroup = bla-blub """) assert PCWConfig.get_feature_property('cleanup', 'max-images-per-flavor', 'fake') == 666 assert type( PCWConfig.get_feature_property('cleanup', 'max-images-per-flavor', 'fake')) is int assert PCWConfig.get_feature_property('cleanup', 'azure-storage-resourcegroup', 'fake') == 'bla-blub' assert type( PCWConfig.get_feature_property( 'cleanup', 'azure-storage-resourcegroup', 'fake')) is str
def get_openqa_job_link(self): tags = self.tags() if tags.get('openqa_created_by', '') == 'openqa-suse-de' and 'openqa_var_JOB_ID' in tags: url = '{}/t{}'.format( PCWConfig.get_feature_property('webui', 'openqa_url'), tags['openqa_var_JOB_ID']) title = tags.get('openqa_var_NAME', '') return {'url': url, 'title': title} return None
def bs_client(self): if (self.__blob_service_client is None): storage_account = PCWConfig.get_feature_property( 'cleanup', 'azure-storage-account-name', self._namespace) storage_key = self.get_storage_key(storage_account) connection_string = "{};AccountName={};AccountKey={};EndpointSuffix=core.windows.net".format( "DefaultEndpointsProtocol=https", storage_account, storage_key) self.__blob_service_client = BlobServiceClient.from_connection_string( connection_string) return self.__blob_service_client
def test_get_feature_property_from_pcw_ini_with_namespace(pcw_file): set_pcw_ini( pcw_file, """ [cleanup] max-images-per-flavor = 666 azure-storage-resourcegroup = bla-blub [cleanup.namespace.testns] max-images-per-flavor = 42 azure-storage-resourcegroup = bla-blub-ns """) cleanup_max_images_per_flavor = PCWConfig.get_feature_property( 'cleanup', 'max-images-per-flavor', 'testns') cleanup_azure_storage_resourcegroup = PCWConfig.get_feature_property( 'cleanup', 'azure-storage-resourcegroup', 'testns') assert cleanup_max_images_per_flavor == 42 assert type(cleanup_max_images_per_flavor) is int assert cleanup_azure_storage_resourcegroup == 'bla-blub-ns' assert type(cleanup_azure_storage_resourcegroup) is str
def send_leftover_notification(): if PCWConfig.has('notify'): o = Instance.objects o = o.filter( active=True, csp_info__icontains='openqa_created_by', age__gt=timedelta( hours=PCWConfig.get_feature_property('notify', 'age-hours'))) body_prefix = "Message from {url}\n\n".format(url=build_absolute_uri()) # Handle namespaces for namespace in PCWConfig.get_namespaces_for('notify'): receiver_email = PCWConfig.get_feature_property( 'notify', 'to', namespace) namespace_objects = o.filter(vault_namespace=namespace) if namespace_objects.filter( notified=False).count() > 0 and receiver_email: send_mail('CSP left overs - {}'.format(namespace), body_prefix + draw_instance_table(namespace_objects), receiver_email=receiver_email) o.update(notified=True)
def test_get_feature_property_with_defaults(pcw_file): assert PCWConfig.get_feature_property('cleanup', 'max-images-per-flavor', 'fake') == 1 assert type( PCWConfig.get_feature_property('cleanup', 'max-images-per-flavor', 'fake')) is int assert type( PCWConfig.get_feature_property('cleanup', 'min-image-age-hours', 'fake')) is int assert type( PCWConfig.get_feature_property('cleanup', 'max-image-age-hours', 'fake')) is int assert PCWConfig.get_feature_property('cleanup', 'azure-storage-resourcegroup', 'fake') == 'openqa-upload' assert type( PCWConfig.get_feature_property( 'cleanup', 'azure-storage-resourcegroup', 'fake')) is str
def __init__(self, namespace: str): super().__init__(namespace) self.__resource_group = PCWConfig.get_feature_property( 'cleanup', 'azure-storage-resourcegroup', namespace) self.check_credentials()
def older_than_min_age(self, age): return datetime.now(timezone.utc) > age + timedelta( hours=PCWConfig.get_feature_property( 'cleanup', 'min-image-age-hours', self._namespace))
def test_get_feature_property_lookup_error(pcw_file): with pytest.raises(LookupError): PCWConfig.get_feature_property('notexisting', 'notexisting', 'fake')