Пример #1
0
    def get_file(self, local_path=None, file_name=None):
        self.logger.debug('Getting file ...')
        try:
            tmp_file_name = str(Util.generate_uuid())
            local_full_path = System.Ahenk.received_dir_path() + tmp_file_name
            sftp = paramiko.SFTPClient.from_transport(self.connection)
            sftp.get(self.target_path, local_full_path)

            if local_path is None:
                receive_path = System.Ahenk.received_dir_path()
            else:
                receive_path = local_path
            if file_name is not None:
                f_name = file_name
            else:
                f_name = str(Util.get_md5_file(local_full_path))
            Util.rename_file(local_full_path, receive_path + f_name)
            self.logger.debug('File was downloaded to {0} from {1}'.format(
                receive_path, self.target_path))
        except Exception as e:
            self.logger.warning(
                'A problem occurred while downloading file. Exception message: {0}'
                .format(str(e)))
            raise
        return f_name
Пример #2
0
    def execute_script(self, arg):
        try:
            self.logger.debug('Executing script...')
            messenger = Scope().get_instance().get_messenger()

            json_data = json.loads(arg)
            result_code, p_out, p_err = Util.execute(str(json_data['command']))

            self.logger.debug('Executed script')

            data = dict()
            data['type'] = 'SCRIPT_RESULT'
            data['timestamp'] = str(Util.timestamp())

            if result_code == 0:
                self.logger.debug('Command execution was finished successfully')
                try:
                    temp_name = str(Util.generate_uuid())
                    temp_full_path = System.Ahenk.received_dir_path() + temp_name
                    self.logger.debug('Writing result to file')
                    Util.write_file(temp_full_path, str(p_out))
                    md5 = Util.get_md5_file(temp_full_path)
                    Util.rename_file(temp_full_path, System.Ahenk.received_dir_path() + md5)

                    file_manager = FileTransferManager(json_data['fileServerConf']['protocol'],
                                                       json_data['fileServerConf']['parameterMap'])
                    file_manager.transporter.connect()
                    self.logger.debug('File transfer connection was created')
                    success = file_manager.transporter.send_file(System.Ahenk.received_dir_path() + md5, md5)
                    self.logger.debug('File was transferred')
                    file_manager.transporter.disconnect()
                    self.logger.debug('File transfer connection was closed')

                    if success is False:
                        self.logger.error('A problem occurred while file transferring')
                        data['resultCode'] = '-1'
                        data[
                            'errorMessage'] = 'Command executed successfully but a problem occurred while sending result file'

                    else:
                        data['md5'] = md5

                except Exception as e:
                    self.logger.error(
                        'A problem occurred while file transferring. Error Message :{0}'.format(
                            str(e)))
                    raise
            else:
                self.logger.error(
                    'Command execution was failed. Error Message :{0}'.format(str(result_code)))
                data['resultCode'] = str(result_code)
                data['errorMessage'] = str(p_err)

            messenger.send_direct_message(json.dumps(data))
        except Exception as e:
            self.logger.error(
                'A problem occurred while running execute script action. Error Message :{0}'.format(
                    str(e)))
Пример #3
0
    def execute_script(self, arg):
        try:
            self.logger.debug('Executing script...')
            messenger = Scope().get_instance().get_messenger()

            json_data = json.loads(arg)
            result_code, p_out, p_err = Util.execute(str(json_data['command']))

            self.logger.debug('Executed script')

            data = dict()
            data['type'] = 'SCRIPT_RESULT'
            data['timestamp'] = str(Util.timestamp())

            if result_code == 0:
                self.logger.debug('Command execution was finished successfully')
                try:
                    temp_name = str(Util.generate_uuid())
                    temp_full_path = System.Ahenk.received_dir_path() + temp_name
                    self.logger.debug('Writing result to file')
                    Util.write_file(temp_full_path, str(p_out))
                    md5 = Util.get_md5_file(temp_full_path)
                    Util.rename_file(temp_full_path, System.Ahenk.received_dir_path() + md5)

                    file_manager = FileTransferManager(json_data['fileServerConf']['protocol'],
                                                       json_data['fileServerConf']['parameterMap'])
                    file_manager.transporter.connect()
                    self.logger.debug('File transfer connection was created')
                    success = file_manager.transporter.send_file(System.Ahenk.received_dir_path() + md5, md5)
                    self.logger.debug('File was transferred')
                    file_manager.transporter.disconnect()
                    self.logger.debug('File transfer connection was closed')

                    if success is False:
                        self.logger.error('A problem occurred while file transferring')
                        data['resultCode'] = '-1'
                        data[
                            'errorMessage'] = 'Command executed successfully but a problem occurred while sending result file'

                    else:
                        data['md5'] = md5

                except Exception as e:
                    self.logger.error(
                        'A problem occurred while file transferring. Error Message :{0}'.format(
                            str(e)))
                    raise
            else:
                self.logger.error(
                    'Command execution was failed. Error Message :{0}'.format(str(result_code)))
                data['resultCode'] = str(result_code)
                data['errorMessage'] = str(p_err)

            messenger.send_direct_message(json.dumps(data))
        except Exception as e:
            self.logger.error(
                'A problem occurred while running execute script action. Error Message :{0}'.format(
                    str(e)))
Пример #4
0
    def ask(self, username, display):

        result = self.db_service.select('contract', ['content', 'title', 'id'],
                                        'id =(select MAX(id) from contract)')

        if result is None or len(result) < 1:
            content = 'Ahenk kurulu bu bilgisayarda ilk defa oturum açıyorsunuz. ' \
                      'Devam ederseniz Lider-Ahenk in bilgisayar üzeride yapacağı ' \
                      'tüm işlemlere onay vermiş sayılacaksınız. Kabul ediyor musunuz?' \
                      ' \n(Tanımlanmış zaman aralığında olumlu cevaplandırmadığınız takdirde oturumunuz ' \
                      'sonlandırılacaktır.)'
            title = 'Ahenk Kurulu Bilgisayar Kullanım Anlaşması'
            contract_id = '-1'
        else:
            content = str(result[0][0])
            title = result[0][1]
            contract_id = result[0][2]
        try:
            agreement_path = System.Ahenk.received_dir_path(
            ) + Util.generate_uuid()
            Util.write_file(agreement_path, content)
            Util.set_permission(agreement_path, 777)
            command = 'export DISPLAY={0};su - {1} -c \'python3 {2} \"$(cat {3})\" \"{4}\"\''.format(
                display, username, self.ask_path, agreement_path, title)
            result_code, p_out, p_err = Util.execute(command)

            pout = str(p_out).replace('\n', '')
            if pout != 'Error':
                if pout == 'Y':
                    self.logger.debug(
                        'Agreement was accepted by {0}.'.format(username))
                    self.db_service.update(
                        'agreement', self.db_service.get_cols('agreement'),
                        [contract_id, username,
                         Util.timestamp(), 'Y'])
                elif pout == 'N':
                    self.db_service.update(
                        'agreement', self.db_service.get_cols('agreement'),
                        [contract_id, username,
                         Util.timestamp(), 'N'])
                    self.logger.debug(
                        'Agreement was ignored by {0}. Session will be closed'.
                        format(username))
                else:
                    self.logger.error(
                        'A problem occurred while executing ask.py. Error Message: {0}'
                        .format(str(pout)))
                Util.delete_file(agreement_path)
            else:
                self.logger.error(
                    'A problem occurred while executing ask.py (Probably argument fault). Error Message: {0}'
                    .format(str(pout)))

        except Exception as e:
            self.logger.error(
                'A Problem occurred while displaying agreement. Error Message: {0}'
                .format(str(e)))
Пример #5
0
    def ask(self, username, display):

        result = self.db_service.select('contract', ['content', 'title', 'id'], 'id =(select MAX(id) from contract)')

        if result is None or len(result) < 1:
            content = 'Ahenk kurulu bu bilgisayarda ilk defa oturum açıyorsunuz. ' \
                      'Devam ederseniz Lider-Ahenk in bilgisayar üzeride yapacağı ' \
                      'tüm işlemlere onay vermiş sayılacaksınız. Kabul ediyor musunuz?' \
                      ' \n(Tanımlanmış zaman aralığında olumlu cevaplandırmadığınız takdirde oturumunuz ' \
                      'sonlandırılacaktır.)'
            title = 'Ahenk Kurulu Bilgisayar Kullanım Anlaşması'
            contract_id = '-1'
        else:
            content = str(result[0][0])
            title = result[0][1]
            contract_id = result[0][2]
        try:
            agreement_path = System.Ahenk.received_dir_path() + Util.generate_uuid()
            Util.write_file(agreement_path, content)
            Util.set_permission(agreement_path, 777)
            command = 'export DISPLAY={0};su - {1} -c \'python3 {2} \"$(cat {3})\" \"{4}\"\''.format(display, username,
                                                                                                     self.ask_path,
                                                                                                     agreement_path,
                                                                                                     title)
            result_code, p_out, p_err = Util.execute(command)
            pout = str(p_out).replace('\n', '')
            if pout != 'Error':
                if pout == 'Y':
                    self.logger.debug('Agreement was accepted by {0}.'.format(username))
                    self.db_service.update('agreement', self.db_service.get_cols('agreement'),
                                           [contract_id, username, Util.timestamp(), 'Y'])
                elif pout == 'N':
                    self.db_service.update('agreement', self.db_service.get_cols('agreement'),
                                           [contract_id, username, Util.timestamp(), 'N'])
                    self.logger.debug(
                        'Agreement was ignored by {0}. Session will be closed'.format(username))
                else:
                    self.logger.error(
                        'A problem occurred while executing ask.py. Error Message: {0}'.format(str(pout)))
                Util.delete_file(agreement_path)
            else:
                self.logger.error(
                    'A problem occurred while executing ask.py (Probably argument fault). Error Message: {0}'.format(
                        str(pout)))

        except Exception as e:
            self.logger.error(
                'A Problem occurred while displaying agreement. Error Message: {0}'.format(str(e)))
Пример #6
0
    def get_file(self):

        self.logger.debug('[FileTransfer] Getting file ...')
        file_md5 = None
        try:
            tmp_file_name = str(Util.generate_uuid())
            local_full_path = System.Ahenk.received_dir_path() + tmp_file_name
            urllib.request.urlretrieve(self.url, local_full_path)
            file_md5 = str(Util.get_md5_file(local_full_path))
            Util.rename_file(local_full_path, System.Ahenk.received_dir_path() + file_md5)
            self.logger.debug('File was downloaded to {0} from {1}'.format(local_full_path, self.url))
        except Exception as e:
            self.logger.error(
                'A problem occurred while downloading file. Exception message: {0}'.format(str(e)))
            raise
        return file_md5
Пример #7
0
    def get_file(self):

        self.logger.debug('[FileTransfer] Getting file ...')
        file_md5 = None
        try:
            tmp_file_name = str(Util.generate_uuid())
            local_full_path = System.Ahenk.received_dir_path() + tmp_file_name
            urllib.request.urlretrieve(self.url, local_full_path)
            file_md5 = str(Util.get_md5_file(local_full_path))
            Util.rename_file(local_full_path,
                             System.Ahenk.received_dir_path() + file_md5)
            self.logger.debug('File was downloaded to {0} from {1}'.format(
                local_full_path, self.url))
        except Exception as e:
            self.logger.error(
                'A problem occurred while downloading file. Exception message: {0}'
                .format(str(e)))
            raise
        return file_md5
Пример #8
0
    def get_file(self, local_path=None, file_name=None):
        self.logger.debug('Getting file ...')
        try:
            tmp_file_name = str(Util.generate_uuid())
            local_full_path = System.Ahenk.received_dir_path() + tmp_file_name
            sftp = paramiko.SFTPClient.from_transport(self.connection)
            sftp.get(self.target_path, local_full_path)

            if local_path is None:
                receive_path = System.Ahenk.received_dir_path()
            else:
                receive_path = local_path
            if file_name is not None:
                f_name = file_name
            else:
                f_name = str(Util.get_md5_file(local_full_path))
            Util.rename_file(local_full_path, receive_path + f_name)
            self.logger.debug('File was downloaded to {0} from {1}'.format(receive_path, self.target_path))
        except Exception as e:
            self.logger.warning('A problem occurred while downloading file. Exception message: {0}'.format(str(e)))
            raise
        return f_name