3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
26 from os import listdir
27 from os.path import isfile, join
31 def __init__(self, myhost, myuser, mypasswd, mydb):
32 self.db = pymysql.connect(host=myhost,
36 self.cur = self.db.cursor()
38 def Query (self, myquery, val = None):
43 self.cur.execute(myquery, val)
45 self.cur.execute(myquery)
47 results = self.cur.fetchall()
55 def loadPlaybook (value, version, ext = '.yml'):
60 # Test if primary key already defined
61 query = "SELECT name FROM playbook WHERE name='" + value +"'"
62 results, error = sqlintf.Query (query)
64 # print "Primary key already defined: Updating playbook"
67 # print "Primary key not defined: Insert new playbook"
68 query = "INSERT INTO playbook (name) VALUES ('" + value + "')"
69 results, error = sqlintf.Query (query)
75 file = open(playbook_path + value + ext, 'r')
76 load_file = file.read()
81 sql = "UPDATE playbook SET value=%s, version=%s, type=%s WHERE name=%s"
83 results, error = sqlintf.Query(sql, (load_file, version, ext, value))
86 # Error loading playbook
90 return errorCode, diag
92 def loadCredentials (hostgroup, hostname, cred):
98 query = "SELECT hostname,hostgroup FROM inventory WHERE hostname='" + hostname +"'"
99 results = sqlintf.Query (query)
103 if hostname in str(results):
105 results_hostgroups = results[0][0][1]
107 # print "Record already defined: Updating inventory"
108 if hostgroup in results_hostgroups.split(','):
109 query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\
111 "' WHERE hostname='" + hostname + "'"
114 results_hostgroups = results_hostgroups + ',' + hostgroup
116 query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\
117 cred + "',hostgroup='" + results_hostgroups + \
118 "' WHERE hostname='" + hostname + "'"
120 results, error = sqlintf.Query (query)
124 query = "INSERT INTO inventory (hostgroup, hostname, credentials) VALUES ('" + \
125 hostgroup + "','" + hostname + "','" + cred + "')"
126 results, error = sqlintf.Query (query)
129 # Error loading credentials
133 return errorCode, diag
136 if __name__ == '__main__':
138 ################################################################
140 ################################################################
141 host="localhost" # your host, usually localhost
142 user="mysql_user_id" # your username
143 passwd="password_4_mysql_user_id" # your password
144 db="ansible" # name of the data base
146 playbook_path = "/home/ubuntu/RestServerOpenSource/"
147 inventory = "/home/ubuntu/RestServerOpenSource/Ansible_inventory"
148 ################################################################
150 onlyfiles = [f for f in listdir(playbook_path)
151 if isfile(join(playbook_path, f))]
153 sqlintf = mySql (host, user, passwd, db)
156 print "Loading playbooks"
157 for file in onlyfiles:
159 name = file.split (".yml")[0]
160 print " Loading:", name
161 version = name.split("@")[1]
162 errorCode, diag = loadPlaybook (name, version)
164 print " Results: Failed - ", diag
166 print " Results: Success"
168 name = file.split (".tar.gz")[0]
169 print " Loading:", name
170 version = name.split("@")[1]
171 errorCode, diag = loadPlaybook (name, version, ".tar.gz")
173 print "\nLoading inventory"
178 file = open(inventory, 'r')
182 if '[' in line and ']' in line:
183 hostgroup = line.strip().replace('[','').replace(']','')
185 elif hostgroup and len(line.strip())>0:
186 host = line.strip().split(" ")[0]
187 credentials = line.replace(host,"")
188 inv[hostgroup][host] = credentials
192 for hostgroup in inv:
193 print " Loading:", hostgroup
196 for hostname in inv[hostgroup]:
197 cred = inv[hostgroup][hostname]
198 errorCode, diag = loadCredentials (hostgroup, hostname, cred)
200 print " Results: Failed - ", diag
202 print " Results: Success"