- hostgrouplist = []
- hostnamelist = []
-
- NodeList = []
- if 'NodeList' in input_json:
- NodeList = input_json['NodeList']
-
- print " NodeList: ", NodeList
-
- if NodeList == []:
- # By default set to local host
- AnsibleInvFail = False
-
- LocalNodeList = "host"
- LocalCredentials = "localhost ansible_connection=local"
- f = open(PlaybookDir + "/" + AnsibleInv, "w")
- f.write("[" + LocalNodeList + "]\n")
- f.write(LocalCredentials)
- f.close()
-
- else:
-
- if from_files:
-
- # Get credentials from file
-
- data_inventory_orig = {}
- data_inventory_target = {}
- curr_group = None
-
- print "***>", ansible_path + "/" + ansible_inv
- f = open(ansible_path + "/" + ansible_inv, "r")
- for line in f:
- line = line.rstrip()
-
- if len(line)> 0:
- if '#' not in line:
- if "[" in line and "]" in line:
- data_inventory_orig[line] = []
- curr_group = line
- else:
- data_inventory_orig[curr_group].append(line)
- f.close()
-
- for node in NodeList:
- Fail = True
- if "[" + node + "]" in data_inventory_orig:
- if not "[" + node + "]" in data_inventory_target:
-
- print "RESET", "[" + node + "]"
- data_inventory_target["[" + node + "]"] = []
- else:
- print "OK", "[" + node + "]"
- Fail = False
- for cred in data_inventory_orig["[" + node + "]"]:
- data_inventory_target["[" + node + "]"].append(cred)
-
- else:
- for key in data_inventory_orig:
- if node in " ".join(data_inventory_orig[key]):
- if not key in data_inventory_target:
- data_inventory_target[key] = []
- for cred in data_inventory_orig[key]:
- if node in cred:
- data_inventory_target[key].append(cred)
- Fail = False
-
- if Fail:
- data_inventory_target["["+node+"]"] = \
- [node + " ansible_connection=ssh ansible_ssh_user=na ansible_ssh_private_key_file=na"]
-
- AnsibleInvFail = False
-
- f = open(PlaybookDir + "/" + AnsibleInv, "w")
- for key in data_inventory_target:
- f.write(key + "\n")
- for rec in data_inventory_target[key]:
- hostgrouplist.append(key.replace("[", '').replace("]", ''))
- hostnamelist.append(rec.split(' ')[0])
- f.write(rec + "\n")
- f.close()
-
- else:
-
- # Get credentials from mySQL
-
- sqlintf = AnsibleSql.mySql (host, user, passwd,
- db)
- if sqlintf.con:
- MySqlConFail = False
- errorCode, diag = readCredentials (sqlintf,
- NodeList)
-
- print errorCode, diag
- if len (diag) > 0:
- f = open(PlaybookDir + "/" + AnsibleInv,
- "w")
- AnsibleInvFail = False
- # [hostgroup, hostname, credentials]
- for i in range(len(diag)):
- f.write('[' + diag[i][0] + ']' + "\n")
- f.write(diag[i][1]+ " " + diag[i][2] + "\n\n")
- hostgrouplist.append(diag[i][0])
- hostnamelist.append(diag[i][1])
- f.close()
- else:
- MySqlConFailCause = sqlintf.error
- sqlintf.Close()
-
- timeout = timeout_seconds
- if 'Timeout' in input_json:
- timeout = int (input_json['Timeout'])
-
- EnvParam = {}
- if 'EnvParameters' in input_json:
- EnvParam = input_json['EnvParameters']
-
- LocalParam = {}
- if 'LocalParameters' in input_json:
- LocalParam = input_json['LocalParameters']
-
- FileParam = {}
- if 'FileParameters' in input_json:
- FileParam = input_json['FileParameters']
-
- callback_flag = None
- if 'CallBack' in input_json:
- callback_flag = input_json['CallBack']
-
- TestRecord[Id] = {'PlaybookName': PlaybookName,
- 'LCM': LCM,
- 'Version': version,
- 'NodeList': NodeList,
- 'HostGroupList': hostgrouplist,
- 'HostNameList': hostnamelist,
- 'Time': time_now,
- 'Duration': timeout,
- 'Timeout': timeout,
- 'EnvParameters': EnvParam,
- 'LocalParameters': LocalParam,
- 'FileParameters': FileParam,
- 'CallBack': callback_flag,
- 'Result': {"StatusCode": 100,
- "StatusMessage": 'PENDING',
- "ExpectedDuration": str(timeout) + "sec"},
- 'Log': '',
- 'Output': {},
- 'Path': PlaybookDir,
- 'Mandatory': None}
-
- # Write files
-
- if not TestRecord[Id]['FileParameters'] == {}:
- for key in TestRecord[Id]['FileParameters']:
- filename = key
- filecontent = TestRecord[Id]['FileParameters'][key]
- f = open(PlaybookDir + "/" + filename, "w")
- f.write(filecontent)
- f.close()
-
-
- # Process playbook
-
- if from_files:
-
- # Get playbooks from files
-
- MySqlConFail = False
-
- version = None
- target_PlaybookName = None
-
- if '@' in PlaybookName:
- version = PlaybookName.split("@")[1]
- version = version.replace('.yml','')
- version = version.replace('.tar.gz','')
-
- onlyfiles = [f for f in listdir(ansible_path)
- if isfile(join(ansible_path, f))]
-
- version_max = '0.00'
- version_target = ''
-
- for file in onlyfiles:
- if LCM in file:
- temp_version = file.split("@")[1]
- temp_version = temp_version.replace('.yml','')
- temp_version = temp_version.replace('.tar.gz','')
- if version_max < temp_version:
- version_max = temp_version
-
- if not version == None:
- if version in PlaybookName:
- version_target = version
- target_PlaybookName = file
-
- if target_PlaybookName == None:
- for file in onlyfiles:
- if LCM in file and version_max in file:
- target_PlaybookName = file
- version_target = version_max
-
- if target_PlaybookName:
- AnsiblePlaybookFail = False
- readversion = version_target
- src = ansible_path + "/" + target_PlaybookName
- if ".tar.gz" in target_PlaybookName:
- dest = PlaybookDir + "/" + LCM + ".tar.gz"
- shutil.copy2(src, dest)
- retcode = subprocess.call(['tar', '-xvzf',
- dest, "-C", PlaybookDir])
- ArchiveFlag = True
- else:
- dest = PlaybookDir + "/" + LCM + ".yml"
- shutil.copy2(src, dest)
-
- else:
- # Get playbooks from mySQL
-
- sqlintf = AnsibleSql.mySql (host, user, passwd, db)
- if sqlintf.con:
- MySqlConFail = False
-
- name, readversion, AnsiblePlaybookFail, diag = \
- readPlaybook (sqlintf, PlaybookName.split(".")[0],
- version)
-
- if not AnsiblePlaybookFail:
-
- f = open(PlaybookDir + "/" + LCM + diag[1], "w")
- f.write(diag[0])
- f.close()
-
- if ".tar.gz" in diag[1]:
- retcode = subprocess.call(['tar', '-xvzf',
- PlaybookDir + "/" + LCM + diag[1], "-C", PlaybookDir])
- f.close()
- ArchiveFlag = True
- else:
- MySqlConFailCause = sqlintf.error
- sqlintf.Close()
-
- if MySqlConFail:
- if os.path.exists(PlaybookDir):
- shutil.rmtree (PlaybookDir)
- del TestRecord[Id]
- return {"StatusCode": 101,
- "StatusMessage": "CANNOT CONNECT TO MYSQL: " \
- + MySqlConFailCause}
- elif AnsiblePlaybookFail:
- if os.path.exists(PlaybookDir):
- shutil.rmtree (PlaybookDir)
- del TestRecord[Id]
- return {"StatusCode": 101,
- "StatusMessage": "PLAYBOOK NOT FOUND"}
- elif AnsibleInvFail:
- if os.path.exists(PlaybookDir):
- shutil.rmtree (PlaybookDir)
- del TestRecord[Id]
- return {"StatusCode": 101,
- "StatusMessage": "NODE LIST CREDENTIALS NOT FOUND"}
- else:
-
- # Test EnvParameters
- playbook_path = None
- if ArchiveFlag:
- for dName, sdName, fList in os.walk(PlaybookDir):
- if LCM+".yml" in fList:
- playbook_path = dName
- else:
- playbook_path = PlaybookDir
-
- # Store local vars
- if not os.path.exists(playbook_path + "/vars"):
- os.mkdir(playbook_path + "/vars")
- if not os.path.isfile(playbook_path + "/vars/defaults.yml"):
- os.mknod(playbook_path + "/vars/defaults.yml")
-
- for key in TestRecord[Id]['LocalParameters']:
- host_index = []
- for i in range(len(TestRecord[Id]['HostNameList'])):
- if key in TestRecord[Id]['HostNameList'][i]:
- host_index.append(i)
- if len(host_index) == 0:
- for i in range(len(TestRecord[Id]['HostGroupList'])):
- if key in TestRecord[Id]['HostGroupList'][i]:
- host_index.append(i)
- if len(host_index) > 0:
- for i in range(len(host_index)):
- f = open(playbook_path + "/vars/" +
- TestRecord[Id]['HostNameList'][host_index[i]] +
- ".yml", "a")
- for param in TestRecord[Id]['LocalParameters'][key]:
- f.write(param + ": " +
- str (TestRecord[Id]['LocalParameters'][key][param]) +
- "\n")
- f.close()
-
- # Get mandatory parameters from playbook
- Mandatory = []
- with open(playbook_path + "/" + LCM + ".yml") as origin_file:
- for line in origin_file:
- if "Mandatory" in line:
- temp = line.split(":")[1].strip().replace(' ', '')
- if len(temp) > 0:
- Mandatory = temp.split(",")
-
- TestRecord[Id] = {'PlaybookName': TestRecord[Id]['PlaybookName'],
- 'LCM': TestRecord[Id]['LCM'],
- 'Version': readversion,
- 'NodeList': TestRecord[Id]['NodeList'],
- 'HostGroupList': TestRecord[Id]['HostGroupList'],
- 'HostNameList': TestRecord[Id]['HostNameList'],
- 'Time': TestRecord[Id]['Time'],
- 'Timeout': TestRecord[Id]['Timeout'],
- 'Duration': TestRecord[Id]['Duration'],
- 'EnvParameters': TestRecord[Id]['EnvParameters'],
- 'LocalParameters': TestRecord[Id]['LocalParameters'],
- 'FileParameters': TestRecord[Id]['FileParameters'],
- 'CallBack': TestRecord[Id]['CallBack'],
- 'Result': TestRecord[Id]['Result'],
- 'Log': TestRecord[Id]['Log'],
- 'Output': TestRecord[Id]['Output'],
- 'Path': TestRecord[Id]['Path'],
- 'Mandatory': Mandatory}
-
- TestKey = False
-
- if Mandatory:
- for val in Mandatory:
- if EnvParam:
- if val in EnvParam:
- TestKey = True
- else:
- if LocalParam:
- for key in TestRecord[Id]['NodeList']:
- if key in LocalParam:
- if val in LocalParam[key]:
- TestKey = True
- else:
- if LocalParam:
- for key in TestRecord[Id]['NodeList']:
- if key in LocalParam:
- if val in LocalParam[key]:
- TestKey = True
-
- if not TestKey:
- if os.path.exists(PlaybookDir):
- shutil.rmtree (PlaybookDir)
- del TestRecord[Id]
- return {"StatusCode": 101,
- "StatusMessage": "MISSING MANDATORY PARAMETER: " + \
- " ".join(str(x) for x in Mandatory)}
-
-
- # Cannot use thread because ansible module uses
- # signals which are only supported in main thread.
- # So use multiprocess with shared object
-
- p = Process(target = RunAnsible_Playbook,
- args = (callback, Id, PlaybookDir + "/" + AnsibleInv,
- playbook_path + "/" + LCM + ".yml",
- NodeList, TestRecord, PlaybookDir,
- ArchiveFlag))
- p.start()
- ActiveProcess[Id] = p
- return TestRecord[Id]['Result']
- else:
- return {"StatusCode": 101, "StatusMessage": "TEST ID ALREADY DEFINED"}