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