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 pymysql, sys |
| 28 | from os import listdir |
| 29 | from os.path import isfile, join |
| 30 | |
| 31 | class mySql(): |
| 32 | |
| 33 | def __init__(self, myhost, myuser, mypasswd, mydb): |
| 34 | self.con = True |
| 35 | self.error = '' |
| 36 | self.db = None |
| 37 | try: |
| 38 | self.db = pymysql.connect(host=myhost, |
| 39 | user=myuser, |
| 40 | passwd=mypasswd, |
| 41 | db=mydb) |
| 42 | self.cur = self.db.cursor() |
| 43 | except Exception as e: |
| 44 | self.error = e[1] |
| 45 | self.con = False |
| 46 | |
| 47 | def Query (self, myquery, val = None): |
| 48 | results = None |
| 49 | try: |
| 50 | if val: |
| 51 | self.cur.execute(myquery, val) |
| 52 | else: |
| 53 | self.cur.execute(myquery) |
| 54 | self.db.commit() |
| 55 | results = self.cur.fetchall() |
| 56 | except Exception, e: |
| 57 | results = repr(e) |
| 58 | return results |
| 59 | |
| 60 | def Close (self): |
| 61 | if self.db: |
| 62 | self.db.close() |
| 63 | |
| 64 | def loadPlaybook (sqlintf, value, version, ext = '.yml'): |
| 65 | |
| 66 | errorCode = 0 |
| 67 | diag = '' |
| 68 | |
| 69 | # Test if primary key already defined |
| 70 | query = "SELECT name FROM playbook WHERE name='" + value +"'" |
| 71 | results = sqlintf.Query (query) |
| 72 | if len(results) > 0: |
| 73 | pass |
| 74 | else: |
| 75 | query = "INSERT INTO playbook (name) VALUES ('" + value + "')" |
| 76 | results = sqlintf.Query (query) |
| 77 | if len(results) > 0: |
| 78 | errorCode = 1 |
| 79 | diag = results |
| 80 | |
| 81 | # Load playbook |
| 82 | file = open(playbook_path + value + ext, 'r') |
| 83 | load_file = file.read() |
| 84 | |
| 85 | if not errorCode: |
| 86 | sql = "UPDATE playbook SET value=%s, version=%s, type=%s WHERE name=%s" |
| 87 | |
| 88 | results = sqlintf.Query(sql, (load_file, version, ext, value)) |
| 89 | |
| 90 | if len (results) > 0: |
| 91 | # Error loading playbook |
| 92 | errorCode = 1 |
| 93 | diag = results |
| 94 | |
| 95 | return errorCode, diag |
| 96 | |
| 97 | def loadCredentials (sqlintf, hostgroup, hostname, cred): |
| 98 | errorCode = 0 |
| 99 | diag = '' |
| 100 | |
| 101 | # Load credentials |
| 102 | |
| 103 | query = "SELECT hostname,hostgroup FROM inventory WHERE hostname='" + hostname +"'" |
| 104 | results = sqlintf.Query (query) |
| 105 | |
| 106 | if hostname in str (results): |
| 107 | |
| 108 | results_hostgroups = results[0][1] |
| 109 | |
| 110 | if hostgroup in results_hostgroups.split(','): |
| 111 | query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\ |
| 112 | cred +\ |
| 113 | "' WHERE hostname='" + hostname + "'" |
| 114 | else: |
| 115 | |
| 116 | results_hostgroups = results_hostgroups + ',' + hostgroup |
| 117 | |
| 118 | query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\ |
| 119 | cred + "',hostgroup='" + results_hostgroups + \ |
| 120 | "' WHERE hostname='" + hostname + "'" |
| 121 | |
| 122 | results = sqlintf.Query (query) |
| 123 | |
| 124 | else: |
| 125 | |
| 126 | query = "INSERT INTO inventory (hostgroup, hostname, credentials) VALUES ('" + \ |
| 127 | hostgroup + "','" + hostname + "','" + cred + "')" |
| 128 | results = sqlintf.Query (query) |
| 129 | |
| 130 | if len (results) > 0: |
| 131 | # Error loading playbook |
| 132 | errorCode = 1 |
| 133 | diag = results |
| 134 | |
| 135 | return errorCode, diag |
| 136 | |
| 137 | |
| 138 | def readPlaybook (sqlintf, value, version=None): |
| 139 | |
| 140 | errorCode = 0 |
| 141 | diag = '' |
| 142 | |
| 143 | print "***> in AnsibleSql.readPlaybook" |
| 144 | |
| 145 | if not version: |
| 146 | query = "SELECT MAX(version) FROM playbook WHERE name like'" + value + "%'" |
| 147 | print " Query:", query |
| 148 | results = sqlintf.Query (query) |
| 149 | version = results[0][0] |
| 150 | |
| 151 | print " Provided playbook name:", value |
| 152 | print " Used version:", version |
| 153 | |
| 154 | results = [] |
| 155 | if version: |
| 156 | query = "SELECT value,type FROM playbook WHERE name='" + value + "@" + version + "'" |
| 157 | results = sqlintf.Query (query) |
| 158 | |
| 159 | print "Query:", query |
| 160 | print "Results:", results |
| 161 | |
| 162 | if len(results) == 0: |
| 163 | errorCode = 1 |
| 164 | else: |
| 165 | if len(results[0]) == 0: |
| 166 | errorCode = 1 |
| 167 | diag = results[0] |
| 168 | else: |
| 169 | diag = results[0] |
| 170 | |
| 171 | return value, version, errorCode, diag |
| 172 | |
| 173 | def readCredentials (sqlintf, tag): |
| 174 | errorCode = [] |
| 175 | diag = [] |
| 176 | |
| 177 | print "***> in AnsibleSql.readCredential" |
| 178 | |
| 179 | # Load credentials |
| 180 | |
| 181 | for rec in tag: |
| 182 | |
| 183 | # Try hostgroup |
| 184 | query = "SELECT hostgroup, hostname, credentials FROM inventory WHERE hostgroup LIKE '%" + \ |
| 185 | rec +"%'" |
| 186 | query_results = sqlintf.Query (query) |
| 187 | |
| 188 | results = () |
| 189 | for q in query_results: |
| 190 | if rec in q[0].split(','): |
| 191 | l = list(q) |
| 192 | l[0] = rec |
| 193 | q = tuple(l) |
| 194 | results = (q,) + results |
| 195 | |
| 196 | if len(results) == 0: |
| 197 | # Try hostname |
| 198 | query = "SELECT hostgroup, hostname, credentials FROM inventory WHERE hostname='" + \ |
| 199 | rec +"'" |
| 200 | results = sqlintf.Query (query) |
| 201 | |
| 202 | print " Query:", query |
| 203 | print " Results:", len(results), results |
| 204 | |
| 205 | if len(results) == 0: |
| 206 | errorCode = 1 |
| 207 | hostgroup = rec |
| 208 | hostname = rec |
| 209 | credentials = 'ansible_connection=ssh ansible_ssh_user=na ansible_ssh_private_key_file=na\n' |
| 210 | diag.append([hostgroup, hostname, credentials]) |
| 211 | else: |
| 212 | errorCode = 0 |
| 213 | for i in range(len (results)): |
| 214 | for h in results[i][0].split(','): |
| 215 | hostgroup = h |
| 216 | hostname = results[i][1] |
| 217 | credentials = results[i][2] |
| 218 | diag.append([hostgroup, hostname, credentials]) |
| 219 | |
| 220 | return errorCode, diag |
| 221 | |
| 222 | |
| 223 | if __name__ == '__main__': |
| 224 | |
| 225 | ################################################################ |
| 226 | # Change below |
| 227 | ################################################################ |
| 228 | host="localhost" # your host, usually localhost |
| 229 | user="mysql_user_id" # your username |
| 230 | passwd="password_4_mysql_user_id" # your password |
| 231 | db="ansible" # name of the data base |
| 232 | |
| 233 | playbook_path = "/home/ubuntu/RestServerOpenSource/" |
| 234 | inventory = "/home/ubuntu/RestServerOpenSource/Ansible_inventory" |
| 235 | ################################################################ |
| 236 | |
| 237 | onlyfiles = [f for f in listdir(playbook_path) |
| 238 | if isfile(join(playbook_path, f))] |
| 239 | |
| 240 | sqlintf = mySql (host, user, passwd, db) |
| 241 | |
| 242 | # Load playbooks |
| 243 | |
| 244 | print "Loading playbooks" |
| 245 | for file in onlyfiles: |
| 246 | if "yml" in file: |
| 247 | |
| 248 | name = file.split (".yml")[0] |
| 249 | print " Loading:", name |
| 250 | version = name.split("@")[1] |
| 251 | errorCode, diag = loadPlaybook (sqlintf, name, version, '.yml') |
| 252 | if errorCode: |
| 253 | print " Results: Failed - ", diag |
| 254 | else: |
| 255 | print " Results: Success" |
| 256 | |
| 257 | print "\nLoading inventory" |
| 258 | |
| 259 | # Load inventory |
| 260 | |
| 261 | hostgroup = None |
| 262 | inv = {} |
| 263 | file = open(inventory, 'r') |
| 264 | |
| 265 | for line in file: |
| 266 | |
| 267 | if '[' in line and ']' in line: |
| 268 | hostgroup = line.strip().replace('[','').replace(']','') |
| 269 | inv[hostgroup] = {} |
| 270 | elif hostgroup and len(line.strip())>0: |
| 271 | host = line.strip().split(" ")[0] |
| 272 | credentials = line.replace(host,"") |
| 273 | inv[hostgroup][host] = credentials |
| 274 | |
| 275 | file.close() |
| 276 | |
| 277 | for hostgroup in inv: |
| 278 | print " Loading:", hostgroup |
| 279 | hostfqdn = '' |
| 280 | cred = '' |
| 281 | for hostname in inv[hostgroup]: |
| 282 | cred = inv[hostgroup][hostname] |
| 283 | errorCode, diag = loadCredentials (sqlintf, hostgroup, hostname, cred) |
| 284 | if errorCode: |
| 285 | print " Results: Failed - ", diag |
| 286 | else: |
| 287 | print " Results: Success" |
| 288 | |
| 289 | print "\nReading playbook" |
| 290 | |
| 291 | # Read playbook |
| 292 | |
| 293 | if not sqlintf.con: |
| 294 | print "Cannot connect to MySql:", sqlintf.error |
| 295 | sys.exit() |
| 296 | |
| 297 | name = "ansible_sleep" |
| 298 | print "Reading playbook:", name |
| 299 | value, version, errorCode, diag = readPlaybook (sqlintf, name) |
| 300 | if errorCode: |
| 301 | print "Results: Failed - ", diag |
| 302 | else: |
| 303 | print "Results: Success" |
| 304 | print value |
| 305 | print version |
| 306 | print diag |
| 307 | |
| 308 | print "\nReading inventory" |
| 309 | |
| 310 | # Read inventory |
| 311 | |
| 312 | tag = ["your_inventory_test_group_name"] |
| 313 | print "Reading inventory tag:", tag |
| 314 | errorCode, diag = readCredentials (sqlintf, tag) |
| 315 | if errorCode: |
| 316 | print "Results: Failed - ", diag |
| 317 | else: |
| 318 | print "Results: Success" |
| 319 | print diag |
| 320 | |
| 321 | sqlintf.Close() |
| 322 | |