Browse Source

добавил апдейты базы

master
Mikhail Grebenkin 6 years ago
parent
commit
c43723d503
2 changed files with 26 additions and 8 deletions
  1. +12
    -2
      importer.py
  2. +14
    -6
      zayavka_reader.py

+ 12
- 2
importer.py View File

@ -22,6 +22,8 @@ dbconn = {'host': '10.1.11.26',
'base': 'sitemanager0'}
sql = 'select * from LDAP'
sql_update_changes = "update LDAP set Changes = 'N' where ID = '%s'"
sql_delete = "delete from LDAP where Delete = 'Y' and ID = '%s'"
ad_groups = ['vpn-users', 'gogs']
@ -48,7 +50,10 @@ def main():
lp = samba.param.LoadParm()
lp.load(samba.param.default_path()) # или lp.load("/etc/samba/smb.conf")
sam = SamDB(lp=lp, session_info=system_session())
z_users = z_reader.users_make_zayavka(z_reader.mysql_reader(dbconn, sql), ad_groups)
z_users, err = z_reader.users_make_zayavka(z_reader.mysql_reader(dbconn, sql), ad_groups)
if err:
print err
exit(0)
print len(z_users)
for z_user in z_users:
print z_user
@ -56,12 +61,17 @@ def main():
if z_users[z_user]['toRemove']: # удаляем, если такой есть
if ad_user.exists:
ad_user.remove()
dat, err = z_reader.mysql_reader(dbconn, sql_delete % z_user['ID'])
if err:
print err
continue
if not ad_user.exists:
ad_user.add()
if z_users[z_user]['pwChange']: # обновляем пароль
ad_user.set_passwd()
dat, err = z_reader.mysql_reader(dbconn, sql_update_changes % z_user['ID'])
if err:
print err
if z_users[z_user]['enabled']: # инаблим или дисаблим
ad_user.enable()
else:


+ 14
- 6
zayavka_reader.py View File

@ -14,6 +14,8 @@ ad_groups = ['vpn-users', 'gogs']
def mysql_reader(dbconn, sql):
data = tuple
err = False
try:
conn = MySQLdb.connect(host=dbconn['host'], user=dbconn['user'],
passwd=dbconn['pass'], db=dbconn['base'], charset="utf8")
@ -21,12 +23,13 @@ def mysql_reader(dbconn, sql):
cur.execute(sql)
data = cur.fetchall()
cur.close()
except MySQLdb.Error as err:
except MySQLdb.Error as exc:
print("Connection error: {}".format(err))
conn.close()
err = exc
else:
conn.close()
return data
return data, err
def users_make_zayavka(users, memberOf):
@ -42,7 +45,8 @@ def users_make_zayavka(users, memberOf):
pwChange = True
if i['Delete'] == 'Y':
toRemove = True
res[usrName] = {'samAccountName': usrName,
res[usrName] = {'id': str(i['ID']),
'samAccountName': usrName,
'memberOf': memberOf,
'usrPass': str(i['Pass']),
'groupSet': str(i['Otdel']),
@ -54,9 +58,13 @@ def users_make_zayavka(users, memberOf):
def main():
users = users_make_zayavka(mysql_reader(dbconn, sql), ad_groups)
for user in users:
print user, users[user]
data, err = mysql_reader(dbconn, sql)
if not err:
users = users_make_zayavka(data, ad_groups)
for user in users:
print user, users[user]
else:
print err
if __name__ == '__main__':


Loading…
Cancel
Save

Powered by TurnKey Linux.