remove LCM in ansibleSysCall
[ccsdk/distribution.git] / ansible-server / src / main / ansible-server / BuildHostFile.py
1 '''
2 /*-
3 * ============LICENSE_START=======================================================
4 * ONAP : APPC
5 * ================================================================================
6 * Copyright (C) 2019 AT&T Intellectual Property.  All rights reserved.
7 * ================================================================================
8 * Copyright (C) 2019 Amdocs
9 * =============================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 *      http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21
22 * ============LICENSE_END=========================================================
23 */
24 '''
25
26 import os, subprocess
27 import sys
28 from collections import namedtuple
29 import json
30
31 import uuid
32 import cherrypy
33 from cherrypy.lib.httputil import parse_query_string
34 from cherrypy.lib import auth_basic
35
36 def buildHostsSysCall(JsonInput, run_path, inventory_type):
37
38   cherrypy.log( "***> in BuildHostFile.buildHostSysCall")
39
40   # Build host file in run dir
41   output_file = open(run_path + "/host_file.txt","w")
42
43   #
44   # host vm will be formated based on the inventory_type value passed
45   #
46   cherrypy.log( "*** buildHostsSysCall -> Inventory_type: "  + inventory_type)
47
48   # print standard header stuff to file
49   output_file.write ("[host]\n")
50   output_file.write ("localhost   ansible_connection=local\n")
51
52   TypeList=[]
53
54   # print vm type then vm & ips
55   for NodeList in JsonInput['NodeList']:
56       #print( "" )
57       #print ("Node: ")
58       #print NodeList
59   
60       #need to add check that vnfc-type is present in request
61       if not ('vnfc-type' in NodeList):
62         cherrypy.log( "*** buildHostsSysCall -> vnfc-type Not in NodeList: ")
63         return(-1)
64
65       Type =  NodeList['vnfc-type']
66       TypeList.append(Type)
67   
68   
69       # Optional Floating Address & VIP Element
70       FloatingIP=""
71       NE_ID_VIP=""
72       if ('floating_ip_address-vip' in NodeList) & ('ne_id_vip' in NodeList): 
73         FloatingIP = NodeList['floating_ip_address-vip']
74         NE_ID_VIP = NodeList['ne_id_vip']
75         #print ("FloatingIP: " + FloatingIP)
76         #print ("ne_id_vip: " + NE_ID_VIP)
77         output_file.write ("\n[%svip]\n" % Type )
78         if inventory_type == "None":
79           output_file.write ("%s\n" % (FloatingIP) )
80         elif inventory_type == "VNFC": 
81           output_file.write ("%s ansible_host=%s\n" % (NE_ID_VIP, FloatingIP) )
82         elif inventory_type == "VM": 
83           output_file.write ("%s ansible_host=%s\n" % (NE_ID_VIP[0:13], FloatingIP) )
84   
85       output_file.write ("\n[%s]\n" % Type )
86       Site =  NodeList['site']
87
88       #print ("Type: " + Type)
89       #print ("Site: " + Site)
90
91       for  vm in NodeList['vm-info']:
92           #print ("VM: " )
93           #print (vm)
94           Name   = vm['ne_id']
95           IpAddr = vm['fixed_ip_address']
96           #print ("vm: " + Name + ": " + IpAddr)
97           if inventory_type == "None":
98             output_file.write ("%s\n" % (IpAddr) )
99           elif inventory_type == "VNFC": 
100             output_file.write ("%s ansible_host=%s\n" % (Name, IpAddr) )
101           elif inventory_type == "VM": 
102             output_file.write ("%s ansible_host=%s\n" % (Name[0:13], IpAddr) )
103
104   # print  site list
105   output_file.write ("\n[%s:children]\n" % Site )
106   for child_type in TypeList:
107       output_file.write ("%s\n" % child_type)
108
109
110   output_file.close()
111   return(0)