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 | #!/usr/bin/python |
| 28 | import pymysql |
| 29 | from os import listdir |
| 30 | from os.path import isfile, join |
| 31 | |
| 32 | class mySql(): |
| 33 | |
| 34 | def __init__(self, myhost, myuser, mypasswd, mydb): |
| 35 | self.db = pymysql.connect(host=myhost, |
| 36 | user=myuser, |
| 37 | passwd=mypasswd, |
| 38 | db=mydb) |
| 39 | self.cur = self.db.cursor() |
| 40 | |
| 41 | def Query (self, myquery, val = None): |
| 42 | results = None |
| 43 | error = None |
| 44 | try: |
| 45 | if val: |
| 46 | self.cur.execute(myquery, val) |
| 47 | else: |
| 48 | self.cur.execute(myquery) |
| 49 | self.db.commit() |
| 50 | results = self.cur.fetchall() |
| 51 | except Exception, e: |
| 52 | error = str (e) |
| 53 | return results, error |
| 54 | |
| 55 | def Close (self): |
| 56 | self.db.close() |
| 57 | |
| 58 | def loadPlaybook (value, version, ext = '.yml'): |
| 59 | |
| 60 | errorCode = 0 |
| 61 | diag = '' |
| 62 | |
| 63 | # Test if primary key already defined |
| 64 | query = "SELECT name FROM playbook WHERE name='" + value +"'" |
| 65 | results, error = sqlintf.Query (query) |
| 66 | if results: |
| 67 | # print "Primary key already defined: Updating playbook" |
| 68 | pass |
| 69 | else: |
| 70 | # print "Primary key not defined: Insert new playbook" |
| 71 | query = "INSERT INTO playbook (name) VALUES ('" + value + "')" |
| 72 | results, error = sqlintf.Query (query) |
| 73 | if error: |
| 74 | errorCode = 1 |
| 75 | diag = error |
| 76 | |
| 77 | # Load playbook |
| 78 | file = open(playbook_path + value + ext, 'r') |
| 79 | load_file = file.read() |
| 80 | |
| 81 | # Load playbook |
| 82 | |
| 83 | if not errorCode: |
| 84 | sql = "UPDATE playbook SET value=%s, version=%s, type=%s WHERE name=%s" |
| 85 | |
| 86 | results, error = sqlintf.Query(sql, (load_file, version, ext, value)) |
| 87 | |
| 88 | if error: |
| 89 | # Error loading playbook |
| 90 | errorCode = 1 |
| 91 | diag = error |
| 92 | |
| 93 | return errorCode, diag |
| 94 | |
| 95 | def loadCredentials (hostgroup, hostname, cred): |
| 96 | errorCode = 0 |
| 97 | diag = '' |
| 98 | |
| 99 | # Load credentials |
| 100 | |
| 101 | query = "SELECT hostname,hostgroup FROM inventory WHERE hostname='" + hostname +"'" |
| 102 | results = sqlintf.Query (query) |
| 103 | |
| 104 | print '==>', results |
| 105 | |
| 106 | if hostname in str(results): |
| 107 | |
| 108 | results_hostgroups = results[0][0][1] |
| 109 | |
| 110 | # print "Record already defined: Updating inventory" |
| 111 | if hostgroup in results_hostgroups.split(','): |
| 112 | query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\ |
| 113 | cred +\ |
| 114 | "' WHERE hostname='" + hostname + "'" |
| 115 | else: |
| 116 | |
| 117 | results_hostgroups = results_hostgroups + ',' + hostgroup |
| 118 | |
| 119 | query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\ |
| 120 | cred + "',hostgroup='" + results_hostgroups + \ |
| 121 | "' WHERE hostname='" + hostname + "'" |
| 122 | |
| 123 | results, error = sqlintf.Query (query) |
| 124 | |
| 125 | else: |
| 126 | |
| 127 | query = "INSERT INTO inventory (hostgroup, hostname, credentials) VALUES ('" + \ |
| 128 | hostgroup + "','" + hostname + "','" + cred + "')" |
| 129 | results, error = sqlintf.Query (query) |
| 130 | |
| 131 | if error: |
| 132 | # Error loading credentials |
| 133 | errorCode = 1 |
| 134 | diag = results |
| 135 | |
| 136 | return errorCode, diag |
| 137 | |
| 138 | |
| 139 | if __name__ == '__main__': |
| 140 | |
| 141 | ################################################################ |
| 142 | # Change below |
| 143 | ################################################################ |
| 144 | host="localhost" # your host, usually localhost |
| 145 | user="mysql_user_id" # your username |
| 146 | passwd="password_4_mysql_user_id" # your password |
| 147 | db="ansible" # name of the data base |
| 148 | |
| 149 | playbook_path = "/home/ubuntu/RestServerOpenSource/" |
| 150 | inventory = "/home/ubuntu/RestServerOpenSource/Ansible_inventory" |
| 151 | ################################################################ |
| 152 | |
| 153 | onlyfiles = [f for f in listdir(playbook_path) |
| 154 | if isfile(join(playbook_path, f))] |
| 155 | |
| 156 | sqlintf = mySql (host, user, passwd, db) |
| 157 | |
| 158 | # Load playbooks |
| 159 | print "Loading playbooks" |
| 160 | for file in onlyfiles: |
| 161 | if "yml" in file: |
| 162 | name = file.split (".yml")[0] |
| 163 | print " Loading:", name |
| 164 | version = name.split("@")[1] |
| 165 | errorCode, diag = loadPlaybook (name, version) |
| 166 | if errorCode: |
| 167 | print " Results: Failed - ", diag |
| 168 | else: |
| 169 | print " Results: Success" |
| 170 | if "tar.gz" in file: |
| 171 | name = file.split (".tar.gz")[0] |
| 172 | print " Loading:", name |
| 173 | version = name.split("@")[1] |
| 174 | errorCode, diag = loadPlaybook (name, version, ".tar.gz") |
| 175 | |
| 176 | print "\nLoading inventory" |
| 177 | |
| 178 | # Load inventory |
| 179 | hostgroup = None |
| 180 | inv = {} |
| 181 | file = open(inventory, 'r') |
| 182 | |
| 183 | for line in file: |
| 184 | |
| 185 | if '[' in line and ']' in line: |
| 186 | hostgroup = line.strip().replace('[','').replace(']','') |
| 187 | inv[hostgroup] = {} |
| 188 | elif hostgroup and len(line.strip())>0: |
| 189 | host = line.strip().split(" ")[0] |
| 190 | credentials = line.replace(host,"") |
| 191 | inv[hostgroup][host] = credentials |
| 192 | |
| 193 | file.close() |
| 194 | |
| 195 | for hostgroup in inv: |
| 196 | print " Loading:", hostgroup |
| 197 | hostfqdn = '' |
| 198 | cred = '' |
| 199 | for hostname in inv[hostgroup]: |
| 200 | cred = inv[hostgroup][hostname] |
| 201 | errorCode, diag = loadCredentials (hostgroup, hostname, cred) |
| 202 | if errorCode: |
| 203 | print " Results: Failed - ", diag |
| 204 | else: |
| 205 | print " Results: Success" |
| 206 | |
| 207 | sqlintf.Close() |