summaryrefslogtreecommitdiff
path: root/src/orchestration/vmb_producer.py
blob: 709b193706b4f63b66757392b15a85d739f6b0e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
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”: <Example 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://<IPv6>: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 <topic> -i <inputfile>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('vmb_producer.py -t [cluster_status|namespace|kubeconfig|image_status> -i <inputfile>')
            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:])