Merge "reduce ccsdk-ansible-server image footprint"
[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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 * ============LICENSE_END=========================================================
24 */
25 '''
26
27 import os, subprocess
28 import sys
29 from collections import namedtuple
30 import json
31
32 import uuid
33 import cherrypy
34 from cherrypy.lib.httputil import parse_query_string
35 from cherrypy.lib import auth_basic
36
37 def buildHostsSysCall(JsonInput, run_path, inventory_type):
38
39   cherrypy.log( "***> in BuildHostFile.buildHostSysCall")
40
41   # Build host file in run dir
42   output_file = open(run_path + "/host_file.txt","w")
43
44   #
45   # host vm will be formated based on the inventory_type value passed
46   #
47   cherrypy.log( "*** buildHostsSysCall -> Inventory_type: "  + inventory_type)
48
49   # print standard header stuff to file
50   output_file.write ("[host]\n")
51   output_file.write ("localhost   ansible_connection=local\n")
52
53   TypeList=[]
54
55   # print vm type then vm & ips
56   for NodeList in JsonInput['NodeList']:
57       #print( "" )
58       #print ("Node: ")
59       #print NodeList
60   
61       #need to add check that vnfc-type is present in request
62       if not ('vnfc-type' in NodeList):
63         cherrypy.log( "*** buildHostsSysCall -> vnfc-type Not in NodeList: ")
64         return(-1)
65
66       Type =  NodeList['vnfc-type']
67       TypeList.append(Type)
68   
69   
70       # Optional Floating Address & VIP Element
71       FloatingIP=""
72       NE_ID_VIP=""
73       if ('floating_ip_address-vip' in NodeList) & ('ne_id_vip' in NodeList): 
74         FloatingIP = NodeList['floating_ip_address-vip']
75         NE_ID_VIP = NodeList['ne_id_vip']
76         #print ("FloatingIP: " + FloatingIP)
77         #print ("ne_id_vip: " + NE_ID_VIP)
78         output_file.write ("\n[%svip]\n" % Type )
79         if inventory_type == "None":
80           output_file.write ("%s\n" % (FloatingIP) )
81         elif inventory_type == "VNFC": 
82           output_file.write ("%s ansible_host=%s\n" % (NE_ID_VIP, FloatingIP) )
83         elif inventory_type == "VM": 
84           output_file.write ("%s ansible_host=%s\n" % (NE_ID_VIP[0:13], FloatingIP) )
85   
86       output_file.write ("\n[%s]\n" % Type )
87       Site =  NodeList['site']
88
89       #print ("Type: " + Type)
90       #print ("Site: " + Site)
91
92       for  vm in NodeList['vm-info']:
93           #print ("VM: " )
94           #print (vm)
95           Name   = vm['ne_id']
96           IpAddr = vm['fixed_ip_address']
97           #print ("vm: " + Name + ": " + IpAddr)
98           if inventory_type == "None":
99             output_file.write ("%s\n" % (IpAddr) )
100           elif inventory_type == "VNFC": 
101             output_file.write ("%s ansible_host=%s\n" % (Name, IpAddr) )
102           elif inventory_type == "VM": 
103             output_file.write ("%s ansible_host=%s\n" % (Name[0:13], IpAddr) )
104
105   # print  site list
106   output_file.write ("\n[%s:children]\n" % Site )
107   for child_type in TypeList:
108       output_file.write ("%s\n" % child_type)
109
110
111   output_file.close()
112   return(0)