import sys, getopt import yaml import os from django.conf import settings from .vmb_messages import * from pulsar import Client, AuthenticationTLS class VMBProducer(object): #os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.far_edge_ops_api.settings') TLS_CERT_FILE = os.path.join(settings.VMB["CERTS_PATH"], settings.VMB["TLS_CERT_FILE"]) TLS_KEY_FILE = os.path.join(settings.VMB["CERTS_PATH"], settings.VMB["TLS_KEY_FILE"]) TLS_TRUST_CERTS_FILE_PATH = os.path.join(settings.VMB["CERTS_PATH"], settings.VMB["TLS_TRUST_CERTS_FILE_PATH"]) #TLS_CERT_FILE = os.path.join(os.path.dirname(__file__), settings.VMB["TLS_CERT_FILE"]) #TLS_KEY_FILE = os.path.join(os.path.dirname(__file__), settings.VMB["TLS_KEY_FILE"]) #TLS_TRUST_CERTS_FILE_PATH = os.path.join(os.path.dirname(__file__), settings.VMB["TLS_TRUST_CERTS_FILE_PATH"]) # Each environment(Rocklin, Branchburg, AWS Non Prod/PLE/STG/PROD has its own service url SERVICE_URL = settings.VMB['SERVICE_URL'] #SERVICE_URL = "pulsar://localhost:6650/" auth = AuthenticationTLS(TLS_CERT_FILE,TLS_KEY_FILE) def vmb_client(self, topic, schema_name, message): client = Client(self.SERVICE_URL, tls_trust_certs_file_path=self.TLS_TRUST_CERTS_FILE_PATH, tls_allow_insecure_connection=False, authentication=self.auth) topic = settings.VMB["VMB_TOPICS"][topic] print("topic: " + topic) producer = client.create_producer(topic=topic, schema=JsonSchema(eval(schema_name))) consumer = client.subscribe(topic=topic, subscription_name='sub-fe', schema=JsonSchema(eval(schema_name))) producer.send(message) msg = consumer.receive() try: print("VMB-received message: %s" % (msg.value())) consumer.acknowledge(msg) except: # Message failed to be processed consumer.negative_acknowledge(msg) producer.close() client.close() def namespace_creation(self, message): ''' Payload Format: JSON Example Content: { "reportName": “vcp_fe_namespace”, "reportDescription": null, "reportGeneratedOn": "2020-04-28T09:06:32.1962088-04:00", "rowCount": 1, "reportDataRows": [{ “cluster”: "wsbomagj-d654321-001", “namespace”: “WSBOMAGJ-441352VZWcVDU-Y-SM-x-001” “location”: 654321 “created_at”: 2020-01-07 04:16:15.743617 }] } ''' self.vmb_client("TOPIC_NAMESPACE_CREATION", "NameSpaceMessage", message) def cluster_status(self, message): ''' Format: JSON Example Content: { "reportName": “vcp_fe_cluster_status”, "reportDescription": null, "reportGeneratedOn": "2020-04-28T09:06:32.1962088-04:00", "rowCount": 1, "reportDataRows": [ { “name”: "wsbomagj-d654321-001", “description”: NE CONCORD 8_NH “location”: 654321 “software version”: 19.12 “availability”: online “deploy_status”: complete “created_at”: 2020-01-07 04:16:15.743617 “updated_at”: 2020-01-07 06:03:10.854598 } ] ''' self.vmb_client("TOPIC_CLUSTER_STATUS", "ClusterStatusMessage", message) def kubeconfig(self, message): ''' Topic - VCP Far Edge Kubeconfig Token Payload Format: JSON Example Content: { "reportName": “vcp_fe_kubeconfig”, "reportDescription": null, "reportGeneratedOn": "2020-04-28T09:06:32.1962088-04:00", "rowCount": 1, "reportDataRows": [{ “cluster”: "wsbomagj-d654321-001", “namespace”: “WSBOMAGJ-441352VZWcVDU-Y-SM-x-001” “location”: 654321 “kubeconfig”: “created_at”: 2020-01-07 04:16:15.743617 }] } --- Example kubeconfig: apiVersion: v1 kind: Config users: - name: ldap-user user: token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJub2tpYSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJzYTEtdG9rZW4tbW5mMmoiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoic2ExIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiZTA5YTkwMDItMTg0Zi0xMWVhLWE5NzYtMDgwMDI3YTFjODc3Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Om5va2lhOnNhMSJ9.e9uD5kMmAT0HRboSTAbH5xlkETkltLclVQ2GedvoeUmH76WB6G5kGWQrhJjkjpMtPDKxWp6wTzZdEXXwGYGdB6aXbaxcAmau1qid5NGz725BtaoRbSVS2Uk6XrOSNfycFzqc8Z7GTX81VtKKSPYnjeMo47W6FqHw6qk0NEpLLxbpGfJHz8w2KZQiuvI-JRQXtA3PHW3tEaWq3ME3XnYgHNSRJmoiKA99bWjN-HKoOsBDMjhX7kw_VtycRYJ1gbqXmVsSl7BuAQjoplnLN_stRt7ZgpsV4aqmZueqJqHaglB91XOeqe_JcD5unLMb3B5VXpEXPp3V6tjLIyVD8F8XbA clusters: - cluster: server: https://:8443 name: wsbomagj-d654321-001 contexts: - context: cluster: wsbomagj-d654321-001 user: ldap-user namespace: WSBOMAGJ-441352VZWcVDU-Y-SM-x-001 name: ldap-user current-context: ldab-user ''' self.vmb_client("TOPIC_KUBECONFIG_TOKEN", "KubeconfigMessage", message) def images_status(self, message): ''' Topic - VCP Far Edge Image Status Payload Format: JSON Example Content: Image Upload Example { "reportName": “vcp_fe_imagestatus”, "reportDescription": null, "reportGeneratedOn": "2020-04-28T09:06:32.1962088-04:00", "rowCount": 2, "reportDataRows": [{ “cluster”: "wsbomagj-d654321-001", “image”: “i1” “action”: “UPLOAD” “status”: “SUCCESS” “message”:”Image Successfully Deleted” “created_at”: 2020-01-07 04:16:15.743617 } { “cluster”: "wsbomagj-d654321-001", “image”: “i2” “action”: “UPLOAD” “status”: “FAILED” “message”:”Image Not Present in Artifactory” “created_at”: 2020-01-07 04:16:15.743617 }] } ''' self.vmb_client("TOPIC_IMAGE_STATUS", "ImagesStatusMessage", message) def main(argv): try: opts, args = getopt.getopt(argv,"ht:i:",["topic=","ifile="]) except getopt.GetoptError: print('vmb_producer.py -t -i ') sys.exit(2) for opt, arg in opts: if opt == '-h': print('vmb_producer.py -t [cluster_status|namespace|kubeconfig|image_status> -i ') sys.exit() elif opt in ("-t", "--topic"): topic = arg print('topic is ' + topic) elif opt in ("-i", "--ifile"): inputfile = arg print('Input file is ' + inputfile) vmbProducer = VMBProducer() f = open (inputfile, "r") message_json = json.loads(f.read()) print(message_json) if (topic == 'namespace'): # 1. Namespace Creation print('return namespace to vmb ...') remote_region = RemoteRegion(cluster='wsbomagj', namespace='WSBOMAGcluster', location='CILI654321', created_at='2020-01-07 04:16:15.743617') remote_region1 = RemoteRegion(cluster='wsbomagj1', namespace='WSBOMAGcluster1', location='123456', created_at='2020-06-07 04:16:15.743617') namespace_message = NameSpaceMessage(reportName='vcp_fe_namespace', reportDescription='dRAN namespace', reportGeneratedOn='2020-06-07 04:16:15.743617', rowCount= 2, reportDataRows=[remote_region, remote_region1]) vmbProducer.namespace_creation(namespace_message) elif(topic == 'cluster_status'): # 2. Cluster Status print('return cluster status to vmb ...') cluster_status_message = ClusterStatusMessage() for k in message_json: if k == 'reportName': cluster_status_message.reportName = message_json[k] if k == 'reportDescription': cluster_status_message.reportDescription = message_json[k] if k == 'reportGeneratedOn': cluster_status_message.reportGeneratedOn = message_json[k] if k == 'reportDataRows': regions_list = message_json['reportDataRows'] for region in regions_list: cluster_status = ClusterStatus() for k in region: if k == 'name': cluster_status.name = region[k] if k == 'description': cluster_status.description = region[k] if k == 'location': cluster_status.location = region[k] if k == 'software_version': cluster_status.software_version = region[k] if k == 'availability': cluster_status.availability = region[k] if k == 'deploy_status': cluster_status.deploy_status = region[k] if k == 'created_at': cluster_status.created_at = region[k] if k == 'updated_at': cluster_status.updated_at = region[k] cluster_status_message.reportDataRows = [cluster_status] cluster_status_message.rowCount = 1 #cluster_status = ClusterStatus(name= "wsbomagj-d654321-001", # description='NE CONCORD 8_NH', # location='654321', # software_version='19.12', # availability='online', # deploy_status='complete', # created_at='2020-01-07 04:16:15.743617', # updated_at='2020-01-07 06:03:10.854598') #cluster_status_message = ClusterStatusMessage(reportName='vcp_fe_cluster_status', # reportDescription='cluster status', # reportGeneratedOn='2020-04-28T09:06:32.1962088-04:00', # reportDataRows=cluster_status #) vmbProducer.cluster_status(cluster_status_message) elif(topic == 'kubeconfig'): # 3. Kubeconfig Token print('return kubeconfig to vmb ...') kubeconfig = Kubeconfig( cluster='wsbomagj-d654321-001', namespace='WSBOMAGJ-441352VZWcVDU-Y-SM-x-001', location='654321', kubeconfig='Example kubeconfig', created_at='2020-01-07 04:16:15.743617' ) with open('tests/kubeconfig.yaml') as f: #kubeconfig_value = json.dumps(yaml.load(f,Loader=yaml.FullLoader), indent=2) kubeconfig_value = json.dumps(yaml.load(f,Loader=yaml.FullLoader)) print(kubeconfig_value) kubeconfig.kubeconfig = kubeconfig_value kubeconfig_message = KubeconfigMessage( reportName='vcp_fe_kubeconfig', reportDescription='kubeconfig file', reportGeneratedOn='2020-04-28T09:06:32.1962088-04:00', rowCount = 1, reportDataRows=[kubeconfig] ) vmbProducer.kubeconfig(kubeconfig_message) elif(topic == 'image_status'): # 4. Images Status print('return image status to vmb ...') image_status1 = ImageStatus( cluster='wsbomagj-d654321-001', image='i1', action='UPLOAD', status='SUCCESS', message='Image Successfully uploaded', created_at='2020-01-07 04:16:15.743617', ) image_status2 = ImageStatus( cluster='wsbomagj-d654321-002', image='i1', action='UPLOAD', status='FAILED', message='Image uploading failed', created_at='2020-01-07 04:16:15.743617', ) images_status_message = ImagesStatusMessage(reportName='vcp_fe_imagestatus', transactionId='13e11612a7494cd6a7f69ea46e3c0537', reportDescription='cluster status', reportGeneratedOn='2020-04-28T09:06:32.1962088-04:00', rowCount = 2, reportDataRows=[image_status1,image_status2]) vmbProducer.images_status(images_status_message) else: print('unsupport topic: ' + topic) if __name__ == "__main__": main(sys.argv[1:])