Timoney, Dan (dt5972) | 99fe1a5 | 2018-03-29 12:31:45 -0400 | [diff] [blame] | 1 | ''' |
| 2 | /*- |
| 3 | * ============LICENSE_START======================================================= |
| 4 | * ONAP : APPC |
| 5 | * ================================================================================ |
| 6 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 7 | * ================================================================================ |
| 8 | * Copyright (C) 2017 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 | |
| 34 | def ansibleSysCall (inventory_path, playbook_path, nodelist, mandatory, |
| 35 | envparameters, localparameters, lcm, timeout): |
| 36 | |
| 37 | print "***> in AnsibleModule.ansibleSysCall" |
| 38 | print " EnvParameters: ", envparameters |
| 39 | print " LocalParameters:", localparameters |
| 40 | print " Inventory: ", inventory_path |
| 41 | print " Playbook: ", playbook_path |
| 42 | print " NodeList: ", nodelist |
| 43 | print " Mandatory: ", mandatory |
| 44 | print " Timeout: ", timeout |
| 45 | log = [] |
| 46 | |
| 47 | str_parameters = '' |
| 48 | |
| 49 | if not envparameters == {}: |
| 50 | for key in envparameters: |
| 51 | if str_parameters == '': |
| 52 | str_parameters = '"' + str(key) + '=\'' + str(envparameters[key]) + '\'' |
| 53 | else: |
| 54 | str_parameters += ' ' + str(key) + '=\'' + str(envparameters[key]) + '\'' |
| 55 | str_parameters += '"' |
| 56 | |
| 57 | if len(str_parameters) > 0: |
| 58 | cmd = 'timeout --signal=KILL ' + str(timeout) + \ |
| 59 | ' ansible-playbook -v --extra-vars ' + str_parameters + ' -i ' + \ |
| 60 | inventory_path + ' ' + playbook_path |
| 61 | else: |
| 62 | cmd = 'timeout --signal=KILL ' + str(timeout) + \ |
| 63 | ' ansible-playbook -v -i ' + inventory_path + ' ' + playbook_path |
| 64 | |
| 65 | print " CMD: ", cmd |
| 66 | |
| 67 | print "\n =================ANSIBLE STDOUT BEGIN============================================\n" |
| 68 | p = subprocess.Popen(cmd, shell=True, |
| 69 | stdout=subprocess.PIPE, |
| 70 | stderr=subprocess.STDOUT) |
| 71 | # p.wait() |
| 72 | (stdout_value, err) = p.communicate() |
| 73 | |
| 74 | stdout_value_cleanup = '' |
| 75 | for line in stdout_value: |
| 76 | stdout_value_cleanup += line.replace(' ', ' ') |
| 77 | stdout_value = stdout_value_cleanup.splitlines() |
| 78 | |
| 79 | ParseFlag = False |
| 80 | retval = {} |
| 81 | returncode = p.returncode |
| 82 | |
| 83 | if returncode == 137: |
| 84 | |
| 85 | print " ansible-playbook system call timed out" |
| 86 | # ansible-playbook system call timed out |
| 87 | for line in stdout_value: # p.stdout.readlines(): |
| 88 | log.append (line) |
| 89 | |
| 90 | |
| 91 | elif 'ping' in lcm: |
| 92 | |
| 93 | targetnode = envparameters['TargetNode'].split(' ') |
| 94 | str_json = None |
| 95 | for line in stdout_value: # p.stdout.readlines(): |
| 96 | print line # line, |
| 97 | if "PLAY RECAP" in line: |
| 98 | ParseFlag = False |
| 99 | if ParseFlag and len(line.strip())>0: |
| 100 | str_json += line.strip() |
| 101 | if "TASK [debug]" in line: |
| 102 | ParseFlag = True |
| 103 | str_json = '' |
| 104 | log.append (line) |
| 105 | |
| 106 | if str_json: |
| 107 | if '=>' in str_json: |
| 108 | out_json =eval(str_json.split('=>')[1].replace('true','True').replace('false','False')) |
| 109 | |
| 110 | if 'ping.stdout_lines' in out_json: |
| 111 | for node in targetnode: |
| 112 | ip_address = node |
| 113 | ok_flag = '0' |
| 114 | changed_flag = '0' |
| 115 | unreachable_flag = '0' |
| 116 | failed_flag = '1' |
| 117 | for rec in out_json['ping.stdout_lines']: |
| 118 | if node in rec and "is alive" in rec: |
| 119 | ok_flag = '1' |
| 120 | changed_flag = '1' |
| 121 | unreachable_flag = '0' |
| 122 | failed_flag = '0' |
| 123 | for rec in out_json['ping.stdout_lines']: |
| 124 | if node in rec and "address not found" in rec: |
| 125 | ok_flag = '0' |
| 126 | changed_flag = '0' |
| 127 | unreachable_flag = '1' |
| 128 | failed_flag = '0' |
| 129 | retval[ip_address]=[ok_flag, changed_flag, unreachable_flag, |
| 130 | failed_flag] |
| 131 | else: |
| 132 | |
| 133 | for line in stdout_value: # p.stdout.readlines(): |
| 134 | print line # line, |
| 135 | if ParseFlag and len(line.strip())>0: |
| 136 | ip_address = line.split(':')[0].strip() |
| 137 | ok_flag = line.split(':')[1].strip().split('=')[1].split('changed')[0].strip() |
| 138 | changed_flag = line.split(':')[1].strip().split('=')[2].split('unreachable')[0].strip() |
| 139 | unreachable_flag = line.split(':')[1].strip().split('=')[3].split('failed')[0].strip() |
| 140 | failed_flag = line.split(':')[1].strip().split('=')[4].strip() |
| 141 | retval[ip_address]=[ok_flag, changed_flag, unreachable_flag, failed_flag] |
| 142 | if "PLAY RECAP" in line: |
| 143 | ParseFlag = True |
| 144 | log.append (line) |
| 145 | |
| 146 | # retval['p'] = p.wait() |
| 147 | |
| 148 | print " =================ANSIBLE STDOUT END==============================================\n" |
| 149 | |
| 150 | return retval, log, returncode |
| 151 | |
| 152 | if __name__ == '__main__': |
| 153 | |
| 154 | from multiprocessing import Process, Value, Array, Manager |
| 155 | import time |
| 156 | |
| 157 | nodelist = 'host' |
| 158 | |
| 159 | playbook_file = 'ansible_sleep@0.00.yml' |
| 160 | |
| 161 | |
| 162 | d = Manager().dict() |
| 163 | |
| 164 | p = Process(nodelist=ansible_call, args=('ansible_module_config', playbook_file, nodelist,d, )) |
| 165 | p.start() |
| 166 | |
| 167 | print "Process running" |
| 168 | print d |
| 169 | p.join() |
| 170 | print d |