move and delete agent has been coded

This commit is contained in:
Hasan Kara 2020-08-05 11:37:24 +03:00
parent 2bcc0fa441
commit 1ea4435920
2 changed files with 49 additions and 64 deletions

View file

@ -1,66 +1,44 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Edip YILDIZ
# Author: Tuncay ÇOLAK <tuncay.colak@tubitak.gov.tr>
# Author: Hasan Kara <hasankara@pardus.org.tr>
from base.model.enum.content_type import ContentType
import json, threading
import json
from base.scope import Scope
from base.plugin.abstract_plugin import AbstractPlugin
import threading
from base.registration.registration import Registration
class MoveAgent(AbstractPlugin):
class DeleteAgent(AbstractPlugin):
def __init__(self, data, context):
super(AbstractPlugin, self).__init__()
scope = Scope.get_instance()
self.data = data
self.context = context
self.logger = self.get_logger()
self.message_code = self.get_message_code()
def update_dn(self, jid, newDn):
cols = ['dn'];
values = [newDn]
return self.db_service.update('registration', cols, values, 'jid=\''+jid+'\'')
def getCnFromDn(self,dn):
if dn !=None and str(dn) !="":
dnStrArr = str(dn).split(",")
if len(dnStrArr)>0:
return dnStrArr[0]
def handle_task(self):
try:
dn = self.data['dn']
newParentDn = self.data['newParentDn']
jid= self.db_service.select_one_result('registration','jid','registered = 1')
newDn=str(dn).replace(dn, self.getCnFromDn(dn)+ str(newParentDn))
self.update_dn(jid,newDn)
directory_server = self.data['directory_server']
registration = Scope.get_instance().get_registration()
registration.purge_and_unregister(directory_server)
self.context.create_response(code=self.message_code.TASK_PROCESSED.value,
message='Ahenk başarı ile taşındı.',
data=json.dumps({'Dn': newDn}),
message='Ahenk başarı ile silindi.',
data=json.dumps({'Dn': dn}),
content_type=ContentType.APPLICATION_JSON.value)
except Exception as e:
self.logger.error(" error on handle xmessage task. Error: " + str(e))
self.logger.error(" error on handle deleting agent. Error: " + str(e))
self.context.create_response(code=self.message_code.TASK_ERROR.value,
message='Ahenk taşınırken hata olustu' + str(e),
message='Ahenk silinirken hata olustu' + str(e),
content_type=ContentType.APPLICATION_JSON.value)
def handle_task(task, context):
cls = MoveAgent(task, context)
cls = DeleteAgent(task, context)
cls.handle_task()

View file

@ -1,17 +1,13 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Edip YILDIZ
# Author: Tuncay ÇOLAK <tuncay.colak@tubitak.gov.tr>
# Author: Hasan Kara <hasankara@pardus.org.tr>
from base.model.enum.content_type import ContentType
import json, threading
import json
from base.plugin.abstract_plugin import AbstractPlugin
import threading
class MoveAgent(AbstractPlugin):
def __init__(self, data, context):
@ -21,46 +17,57 @@ class MoveAgent(AbstractPlugin):
self.logger = self.get_logger()
self.message_code = self.get_message_code()
def update_dn(self, jid, newDn):
cols = ['dn'];
values = [newDn]
def update_dn(self, jid, new_dn):
cols = ['dn']
values = [new_dn]
return self.db_service.update('registration', cols, values, 'jid=\''+jid+'\'')
def getCnFromDn(self,dn):
if dn !=None and str(dn) !="":
dnStrArr = str(dn).split(",")
if len(dnStrArr)>0:
return dnStrArr[0]
def get_cn_from_dn(self, dn):
if dn != None and str(dn) != "":
dn_str_arr = str(dn).split(",")
if len(dn_str_arr) > 0:
return dn_str_arr[0]
def handle_task(self):
try:
dn = self.data['dn']
newParentDn = self.data['newParentDn']
new_parent_dn = self.data['new_parent_dn']
directory_server = self.data['directory_server']
jid= self.db_service.select_one_result('registration','jid','registered = 1')
jid = self.db_service.select_one_result('registration', 'jid', 'registered = 1')
new_dn = str(dn).replace(dn, self.get_cn_from_dn(dn) + ',' + str(new_parent_dn))
self.update_dn(jid, new_dn)
newDn=str(dn).replace(dn, self.getCnFromDn(dn)+ str(newParentDn))
if directory_server == "LDAP":
# update SSSD conf agent DN
sssd_config_file_path = "/etc/sssd/sssd.conf"
file_sssd = open(sssd_config_file_path, 'r')
file_data = file_sssd.read()
old_dn_in_sssd = ""
new_dn_in_sssd = "ldap_default_bind_dn = " + new_dn + "\n"
with open(sssd_config_file_path) as fp:
for line in fp:
if line.startswith('ldap_default_bind_dn'):
old_dn_in_sssd = line
file_data = file_data.replace(old_dn_in_sssd, new_dn_in_sssd)
self.update_dn(jid,newDn)
file_sssd.close()
file_sssd = open(sssd_config_file_path, 'w')
file_sssd.write(file_data)
file_sssd.close()
self.context.create_response(code=self.message_code.TASK_PROCESSED.value,
message='Ahenk başarı ile taşındı.',
data=json.dumps({'Dn': newDn}),
data=json.dumps({'Dn': new_dn}),
content_type=ContentType.APPLICATION_JSON.value)
except Exception as e:
self.logger.error(" error on handle xmessage task. Error: " + str(e))
self.logger.error("Error occured while moving agent. Error: " + str(e))
self.context.create_response(code=self.message_code.TASK_ERROR.value,
message='Ahenk taşınırken hata olustu' + str(e),
content_type=ContentType.APPLICATION_JSON.value)
def handle_task(task, context):
cls = MoveAgent(task, context)
cls.handle_task()