summaryrefslogtreecommitdiffstats
path: root/ansible-adapter/ansible-example-server/AnsibleSql.py
blob: ab58a96c7ace4bab7239b47a25e58728b67b7bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'''
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* 
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
* ============LICENSE_END=========================================================
*/
'''

import pymysql, sys
from os import listdir
from os.path import isfile, join

class mySql():
    
    def __init__(self, myhost, myuser, mypasswd, mydb):
        self.con = True
        self.error = ''
        self.db = None
        try:
            self.db = pymysql.connect(host=myhost,
                                      user=myuser,
                                      passwd=mypasswd,
                                      db=mydb)
            self.cur = self.db.cursor()
        except Exception as e:
            self.error = e[1]
            self.con = False

    def Query (self, myquery, val = None):
        results = None
        try:
            if val:
                self.cur.execute(myquery, val)
            else:
                self.cur.execute(myquery)
            self.db.commit()
            results = self.cur.fetchall()
        except Exception, e:
            results = repr(e)
        return results
    
    def Close (self):
        if self.db:
            self.db.close()

def loadPlaybook (sqlintf, value, version, ext = '.yml'):

    errorCode = 0
    diag = ''
    
    # Test if primary key already defined
    query = "SELECT name FROM playbook WHERE name='" + value +"'"
    results = sqlintf.Query (query)
    if len(results) > 0:
        pass
    else:
        query = "INSERT INTO playbook (name) VALUES ('" + value + "')"
        results = sqlintf.Query (query)
        if len(results) > 0:
            errorCode = 1
            diag = results

    # Load playbook
    file = open(playbook_path + value + ext, 'r')
    load_file = file.read()
        
    if not errorCode:
        sql = "UPDATE playbook SET value=%s, version=%s, type=%s WHERE name=%s"

        results = sqlintf.Query(sql, (load_file, version, ext, value))

        if len (results) > 0:
            # Error loading playbook
            errorCode = 1
            diag = results

    return errorCode, diag

def loadCredentials (sqlintf, hostgroup, hostname, cred):
    errorCode = 0
    diag = ''
    
    # Load credentials

    query = "SELECT hostname,hostgroup FROM inventory WHERE hostname='" + hostname +"'"
    results = sqlintf.Query (query)

    if hostname in str (results):

        results_hostgroups = results[0][1]
        
        if hostgroup in results_hostgroups.split(','):
            query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\
                    cred +\
                    "' WHERE hostname='" + hostname + "'"
        else:

            results_hostgroups = results_hostgroups + ',' + hostgroup
            
            query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\
                    cred + "',hostgroup='" + results_hostgroups + \
                    "' WHERE hostname='" + hostname + "'"
                        
        results = sqlintf.Query (query)
        
    else:
        
        query = "INSERT INTO inventory (hostgroup, hostname, credentials) VALUES ('" + \
                hostgroup + "','" + hostname + "','" + cred + "')"
        results = sqlintf.Query (query)

    if len (results) > 0:
        # Error loading playbook
        errorCode = 1
        diag = results

    return errorCode, diag
    

def readPlaybook (sqlintf, value, version=None):

    errorCode = 0
    diag = ''

    print "***> in AnsibleSql.readPlaybook"
    
    if not version:
        query = "SELECT MAX(version) FROM playbook WHERE name like'" + value + "%'"
        print "   Query:", query
        results = sqlintf.Query (query)
        version = results[0][0]

    print "   Provided playbook name:", value 
    print "   Used version:", version

    results = []
    if version:
        query = "SELECT value,type FROM playbook WHERE name='" + value + "@" + version + "'"
        results = sqlintf.Query (query)

        print "Query:", query
        print "Results:", results
    
    if len(results) == 0:
        errorCode = 1
    else:
        if len(results[0]) == 0:
            errorCode = 1
            diag = results[0]
        else:
            diag = results[0]

    return value, version, errorCode, diag

def readCredentials (sqlintf, tag):
    errorCode = []
    diag = []

    print "***> in AnsibleSql.readCredential"
    
    # Load credentials

    for rec in tag:

        # Try hostgroup
        query = "SELECT hostgroup, hostname, credentials FROM inventory WHERE hostgroup LIKE '%" + \
                rec +"%'"
        query_results = sqlintf.Query (query)

        results = ()
        for q in query_results:
            if rec in q[0].split(','):
                l = list(q)
                l[0] = rec
                q = tuple(l)
                results = (q,) + results

        if len(results) == 0:
            # Try hostname
            query = "SELECT hostgroup, hostname, credentials FROM inventory WHERE hostname='" + \
                    rec +"'"
            results = sqlintf.Query (query)

        print "   Query:", query
        print "   Results:", len(results), results

        if len(results) == 0:
            errorCode = 1
            hostgroup = rec
            hostname = rec
            credentials = 'ansible_connection=ssh ansible_ssh_user=na ansible_ssh_private_key_file=na\n'
            diag.append([hostgroup, hostname, credentials])
        else:
            errorCode = 0
            for i in range(len (results)):
                for h in results[i][0].split(','):
                    hostgroup = h
                    hostname = results[i][1]
                    credentials = results[i][2]
                    diag.append([hostgroup, hostname, credentials])

    return errorCode, diag
    

if __name__ == '__main__':

    ################################################################
    # Change below
    ################################################################
    host="localhost"                    # your host, usually localhost
    user="mysql_user_id"                # your username
    passwd="password_4_mysql_user_id"   # your password
    db="ansible"                        # name of the data base

    playbook_path = "/home/ubuntu/RestServerOpenSource/"
    inventory = "/home/ubuntu/RestServerOpenSource/Ansible_inventory"
    ################################################################

    onlyfiles = [f for f in listdir(playbook_path)
                 if isfile(join(playbook_path, f))]

    sqlintf = mySql (host, user, passwd, db)

    # Load playbooks

    print "Loading playbooks"
    for file in onlyfiles:
        if "yml" in file:

            name = file.split (".yml")[0]
            print "  Loading:", name
            version = name.split("@")[1]
            errorCode, diag = loadPlaybook (sqlintf, name, version, '.yml')
            if errorCode:
                print "  Results: Failed - ", diag
            else:
                print "  Results: Success"

    print "\nLoading inventory"
    
    # Load inventory

    hostgroup = None
    inv = {}
    file = open(inventory, 'r')

    for line in file:

        if '[' in line and ']' in line:
            hostgroup = line.strip().replace('[','').replace(']','')
            inv[hostgroup] = {}
        elif hostgroup and len(line.strip())>0:
            host = line.strip().split(" ")[0]
            credentials = line.replace(host,"")
            inv[hostgroup][host] = credentials
                               
    file.close()

    for hostgroup in inv:
        print "  Loading:", hostgroup
        hostfqdn = ''
        cred = ''
        for hostname in inv[hostgroup]:
            cred = inv[hostgroup][hostname]
            errorCode, diag = loadCredentials (sqlintf, hostgroup, hostname, cred)
            if errorCode:
                print "  Results: Failed - ", diag
            else:
                print "  Results: Success"
                
    print "\nReading playbook"
    
    # Read playbook

    if not sqlintf.con:
        print "Cannot connect to MySql:", sqlintf.error
        sys.exit()
        
    name = "ansible_sleep"
    print "Reading playbook:", name
    value, version, errorCode, diag = readPlaybook (sqlintf, name)
    if errorCode:
        print "Results: Failed - ", diag
    else:
        print "Results: Success"
        print value
        print version
        print diag

    print "\nReading inventory"

    # Read inventory

    tag = ["your_inventory_test_group_name"]
    print "Reading inventory tag:", tag
    errorCode, diag = readCredentials (sqlintf, tag)
    if errorCode:
        print "Results: Failed - ", diag
    else:
        print "Results: Success"
        print diag
                
    sqlintf.Close()