policy for gnome desktop env and safe mode has been added

This commit is contained in:
Tuncay ÇOLAK 2020-08-20 16:34:16 +03:00
parent f208a83d37
commit 68cef4e7f2
4 changed files with 53 additions and 60 deletions

View file

@ -6,7 +6,6 @@
import sys import sys
import easygui import easygui
def ask(content, title): def ask(content, title):
choice = easygui.buttonbox(msg=title, title=content, choices=['TAMAM']) choice = easygui.buttonbox(msg=title, title=content, choices=['TAMAM'])
#choice = easygui.textbox(msg=title, text=content) #choice = easygui.textbox(msg=title, text=content)
@ -15,7 +14,6 @@ def ask(content, title):
else: else:
print('N') print('N')
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) == 3: if len(sys.argv) == 3:

View file

@ -2,13 +2,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Author: Edip YILDIZ # Author: Edip YILDIZ
from base.model.enum.content_type import ContentType from base.model.enum.content_type import ContentType
import json import json
from base.plugin.abstract_plugin import AbstractPlugin from base.plugin.abstract_plugin import AbstractPlugin
class RunConkyCommand(AbstractPlugin): class RunConkyCommand(AbstractPlugin):
def __init__(self, data, context): def __init__(self, data, context):
super(AbstractPlugin, self).__init__() super(AbstractPlugin, self).__init__()
@ -51,12 +48,10 @@ class RunConkyCommand(AbstractPlugin):
self.execute("killall conky") self.execute("killall conky")
if self.is_exist(self.conky_config_global_autorun_file) == True: if self.is_exist(self.conky_config_global_autorun_file) == True:
self.delete_file(self.conky_config_global_autorun_file) self.delete_file(self.conky_config_global_autorun_file)
self.context.create_response(code=self.message_code.TASK_PROCESSED.value, self.context.create_response(code=self.message_code.TASK_PROCESSED.value,
message='Conky measajları kaldırıldı', message='Conky measajları kaldırıldı',
content_type=ContentType.APPLICATION_JSON.value) content_type=ContentType.APPLICATION_JSON.value)
def execute_conky(self, conky_message): def execute_conky(self, conky_message):
self.logger.debug("[CONKY] Executing conky.") self.logger.debug("[CONKY] Executing conky.")
try: try:
@ -68,11 +63,9 @@ class RunConkyCommand(AbstractPlugin):
self.logger.debug('[Conky] Some processes found which names are conky. They will be killed.') self.logger.debug('[Conky] Some processes found which names are conky. They will be killed.')
self.execute('killall conky') self.execute('killall conky')
except: except:
self.logger.error('[Conky] Conky install-kill problem.') self.logger.error('[Conky] Conky install-kill problem.')
raise raise
if self.is_exist(self.conky_config_file_dir) == False: if self.is_exist(self.conky_config_file_dir) == False:
self.logger.debug('[Conky] Creating directory for conky config at ' + self.conky_config_file_dir) self.logger.debug('[Conky] Creating directory for conky config at ' + self.conky_config_file_dir)
self.create_directory(self.conky_config_file_dir) self.create_directory(self.conky_config_file_dir)
@ -86,7 +79,6 @@ class RunConkyCommand(AbstractPlugin):
self.write_file(self.conky_config_file_path, conky_message) self.write_file(self.conky_config_file_path, conky_message)
self.logger.debug('[Conky] Config file was filled by context.') self.logger.debug('[Conky] Config file was filled by context.')
# creating wrapper file if is not exist. wrapper for using conky command..its need for ETA # creating wrapper file if is not exist. wrapper for using conky command..its need for ETA
if self.is_exist(self.conky_wrapper_file) == False: if self.is_exist(self.conky_wrapper_file) == False:
self.logger.debug('[Conky] Creating directory for conky wrapper file at ' + self.conky_wrapper_file) self.logger.debug('[Conky] Creating directory for conky wrapper file at ' + self.conky_wrapper_file)
@ -101,16 +93,13 @@ class RunConkyCommand(AbstractPlugin):
self.logger.debug('[Conky] Creating directory for conky autorun file at ' + self.conky_config_global_autorun_file) self.logger.debug('[Conky] Creating directory for conky autorun file at ' + self.conky_config_global_autorun_file)
self.create_file(self.conky_config_global_autorun_file) self.create_file(self.conky_config_global_autorun_file)
self.write_file(self.conky_config_global_autorun_file, self.conky_autorun_content) self.write_file(self.conky_config_global_autorun_file, self.conky_autorun_content)
users = self.Sessions.user_name() users = self.Sessions.user_name()
desktop_env = self.get_desktop_env() desktop_env = self.get_desktop_env()
self.logger.info("Get desktop environment is {0}".format(desktop_env)) self.logger.info("Get desktop environment is {0}".format(desktop_env))
for user in users: for user in users:
user_display = self.Sessions.display(user) user_display = self.Sessions.display(user)
if desktop_env == "gnome": if desktop_env == "gnome":
user_display = self.get_username_display_gnome(user) user_display = self.get_username_display_gnome(user)
if user_display is None: if user_display is None:
self.logger.debug('[Conky] executing for display none for user '+ str(user)) self.logger.debug('[Conky] executing for display none for user '+ str(user))
self.execute('conky -q', result=False) self.execute('conky -q', result=False)
@ -119,10 +108,7 @@ class RunConkyCommand(AbstractPlugin):
conky_cmd = 'su ' + str(user) + ' -c ' + ' "conky --display=' + str(user_display) + ' " ' conky_cmd = 'su ' + str(user) + ' -c ' + ' "conky --display=' + str(user_display) + ' " '
self.logger.debug('[Conky] executing command: ' + str(conky_cmd)) self.logger.debug('[Conky] executing command: ' + str(conky_cmd))
self.execute(conky_cmd, result=False) self.execute(conky_cmd, result=False)
#self.execute('conky ', result=False) #self.execute('conky ', result=False)
self.context.create_response(code=self.message_code.TASK_PROCESSED.value, self.context.create_response(code=self.message_code.TASK_PROCESSED.value,
message='Conky başarıyla oluşturuldu.', message='Conky başarıyla oluşturuldu.',
data=json.dumps({'Result': conky_message}), data=json.dumps({'Result': conky_message}),
@ -132,20 +118,16 @@ class RunConkyCommand(AbstractPlugin):
try: try:
conky_message = self.data['conkyMessage'] conky_message = self.data['conkyMessage']
remove_conky_message = self.data['removeConkyMessage'] remove_conky_message = self.data['removeConkyMessage']
if remove_conky_message: if remove_conky_message:
self.remove_conky_message() self.remove_conky_message()
else: else:
self.execute_conky(conky_message) self.execute_conky(conky_message)
except Exception as e: except Exception as e:
self.logger.error(" error on handle conky task. Error: " + str(e)) self.logger.error(" error on handle conky task. Error: " + str(e))
self.context.create_response(code=self.message_code.TASK_ERROR.value, self.context.create_response(code=self.message_code.TASK_ERROR.value,
message='Conky mesajı olusturulurken hata oluştu:' + str(e), message='Conky mesajı olusturulurken hata oluştu:' + str(e),
content_type=ContentType.APPLICATION_JSON.value) content_type=ContentType.APPLICATION_JSON.value)
def handle_task(task, context): def handle_task(task, context):
cls = RunConkyCommand(task, context) cls = RunConkyCommand(task, context)
cls.handle_task() cls.handle_task()

View file

@ -3,10 +3,8 @@
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com> # Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import json import json
from base.plugin.abstract_plugin import AbstractPlugin from base.plugin.abstract_plugin import AbstractPlugin
class Conky(AbstractPlugin): class Conky(AbstractPlugin):
def __init__(self, data, context): def __init__(self, data, context):
super(Conky, self).__init__() super(Conky, self).__init__()
@ -24,13 +22,11 @@ class Conky(AbstractPlugin):
def handle_policy(self): def handle_policy(self):
try: try:
# Checking dependecies # Checking dependecies
if self.check_dependencies(['conky', 'conky-all']) is True: if self.check_dependencies(['conky', 'conky-all']) is True:
self.logger.debug('Dependencies checked.') self.logger.debug('Dependencies checked.')
else: else:
return return
# Killing conky processes # Killing conky processes
self.logger.debug('Conky named processes will be killed.') self.logger.debug('Conky named processes will be killed.')
self.execute('killall -9 conky') self.execute('killall -9 conky')
@ -49,8 +45,7 @@ class Conky(AbstractPlugin):
self.logger.debug('Old config file will be deleted.') self.logger.debug('Old config file will be deleted.')
self.delete_file(self.conky_config_file_path) self.delete_file(self.conky_config_file_path)
else: else:
self.logger.debug( self.logger.debug('Creating directory for conky config at {0}'.format(self.conky_config_file_dir))
'Creating directory for conky config at {0}'.format(self.conky_config_file_dir))
self.create_directory(self.conky_config_file_dir) self.create_directory(self.conky_config_file_dir)
if self.create_file(self.conky_config_file_path): if self.create_file(self.conky_config_file_path):
@ -66,28 +61,26 @@ class Conky(AbstractPlugin):
self.initialize_auto_run() self.initialize_auto_run()
if self.machine_profile is False: if self.machine_profile is False:
self.execute( user_display = self.Sessions.display(self.username)
self.command_autorun_conky.format('--display=' + self.Sessions.display(self.username), desktop_env = self.get_desktop_env()
self.conky_config_file_path), if desktop_env == "gnome":
as_user=self.username, result=False) user_display = self.get_username_display_gnome(self.username)
self.logger.info("Get desktop environment is {0}".format(desktop_env))
self.execute(self.command_autorun_conky.format('--display=' + user_display, self.conky_config_file_path), as_user=self.username, result=False)
self.execute('chown -hR ' + self.username + ':' + self.username + ' ' + self.conky_config_file_dir) self.execute('chown -hR ' + self.username + ':' + self.username + ' ' + self.conky_config_file_dir)
self.logger.debug('Owner of Conky config file was changed.') self.logger.debug('Owner of Conky config file was changed.')
else: else:
self.execute(self.command_autorun_conky.format('', self.conky_config_file_path), result=False) self.execute(self.command_autorun_conky.format('', self.conky_config_file_path), result=False)
self.logger.debug('Autorun command executed successfully') self.logger.debug('Autorun command executed successfully')
self.context.create_response(code=self.get_message_code().POLICY_PROCESSED.value, self.context.create_response(code=self.get_message_code().POLICY_PROCESSED.value,
message='Conky politikası başarıyla çalıştırıldı.') message='Conky politikası başarıyla çalıştırıldı.')
except Exception as e: except Exception as e:
self.logger.error( self.logger.error('A problem occurred while handling Conky policy. Error Message: {}'.format(str(e)))
'A problem occurred while handling Conky policy. Error Message: {}'.format(str(e)))
self.context.create_response(code=self.get_message_code().POLICY_ERROR.value, self.context.create_response(code=self.get_message_code().POLICY_ERROR.value,
message='Conky politikası uygulanırken bir hata oluştu.') message='Conky politikası uygulanırken bir hata oluştu.')
def check_dependencies(self, packages): def check_dependencies(self, packages):
self.logger.debug('Checking dependencies') self.logger.debug('Checking dependencies')
for package in packages: for package in packages:
if self.is_installed(package) is False: if self.is_installed(package) is False:
@ -96,47 +89,33 @@ class Conky(AbstractPlugin):
if result_code == 0: if result_code == 0:
self.logger.debug('{0} installed successfully'.format(package)) self.logger.debug('{0} installed successfully'.format(package))
else: else:
self.logger.error( self.logger.error('A problem occurred while installing {0} package. Error Message: {1}'.format(package, str(p_err)))
'A problem occurred while installing {0} package. Error Message: {1}'.format(package,
str(
p_err)))
self.context.create_response(code=self.get_message_code().POLICY_ERROR.value, self.context.create_response(code=self.get_message_code().POLICY_ERROR.value,
message='Bağımlılıklardan {0} paketi kurulurken hata oluştu.') message='Bağımlılıklardan {0} paketi kurulurken hata oluştu.')
return False return False
return True return True
def initialize_auto_run(self): def initialize_auto_run(self):
if self.machine_profile is True: if self.machine_profile is True:
self.logger.debug('All users conky configuration files will be removed because of machine profile') self.logger.debug('All users conky configuration files will be removed because of machine profile')
if self.Sessions.user_name() is not None and len(self.Sessions.user_name()) > 0: if self.Sessions.user_name() is not None and len(self.Sessions.user_name()) > 0:
for username in self.Sessions.user_name(): for username in self.Sessions.user_name():
self.logger.debug( self.logger.debug('Removing conf file of user {0}'.format(username))
'Removing conf file of user {0}'.format(username)) self.delete_file(self.autorun_file_path.format( self.autostart_dir_path.format(self.Sessions.user_home_path(username))))
self.delete_file(
self.autorun_file_path.format(
self.autostart_dir_path.format(self.Sessions.user_home_path(username))))
else: else:
self.logger.debug( self.logger.debug('There are no user')
'There are no user')
else: else:
home_path = self.Sessions.user_home_path(self.username) home_path = self.Sessions.user_home_path(self.username)
self.logger.debug( self.logger.debug('Creating autorun file for user {0}'.format(self.username))
'Creating autorun file for user {0}'.format(self.username))
self.create_autorun_file(self.autostart_dir_path.format(home_path), self.create_autorun_file(self.autostart_dir_path.format(home_path),
self.conky_config_file_path, self.conky_config_file_path,
self.autorun_file_path.format(self.autostart_dir_path.format(home_path))) self.autorun_file_path.format(self.autostart_dir_path.format(home_path)))
self.logger.debug( self.logger.debug('Autorun created')
'Autorun created')
def create_autorun_file(self, autostart_path, conky_config_file_path, autorun_file_path): def create_autorun_file(self, autostart_path, conky_config_file_path, autorun_file_path):
if not self.is_exist(autostart_path): if not self.is_exist(autostart_path):
self.logger.debug( self.logger.debug('Creating file: {0}'.format(autostart_path))
'Creating file: {0}'.format(autostart_path))
self.create_directory(autostart_path) self.create_directory(autostart_path)
file_content = '[Desktop Entry]\n' \ file_content = '[Desktop Entry]\n' \
'Encoding=UTF-8 \n' \ 'Encoding=UTF-8 \n' \
'Type=Application \n' \ 'Type=Application \n' \
@ -145,11 +124,9 @@ class Conky(AbstractPlugin):
'Exec=conky -d -c ' + conky_config_file_path + '\n' \ 'Exec=conky -d -c ' + conky_config_file_path + '\n' \
'StartupNotify=false \n' \ 'StartupNotify=false \n' \
'Terminal=false \n' 'Terminal=false \n'
self.logger.debug( self.logger.debug('Writing content to autorun file.')
'Writing content to autorun file.')
self.write_file(autorun_file_path, file_content, 'w') self.write_file(autorun_file_path, file_content, 'w')
def handle_policy(profile_data, context): def handle_policy(profile_data, context):
plugin = Conky(profile_data, context) plugin = Conky(profile_data, context)
plugin.handle_policy() plugin.handle_policy()

36
src/plugins/conky/safe.py Normal file
View file

@ -0,0 +1,36 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author:Tuncay ÇOLAK <tuncay.colak@tubitak.gov.tr>
from base.plugin.abstract_plugin import AbstractPlugin
class Safe(AbstractPlugin):
def __init__(self, context):
super(Safe, self).__init__()
self.context = context
self.username = str(context.get_username())
self.logger = self.get_logger()
self.logger.debug('Parameters were initialized.')
def handle_safe_mode(self):
# Killing conky processes
self.logger.debug('Conky named processes will be killed.')
self.execute('killall -9 conky')
# delete autostart and conky config file of logout username
self.conky_config_file_dir = '{0}.conky/'.format(self.Sessions.user_home_path(self.username))
self.conky_config_file_path = '{0}conky.conf'.format(self.conky_config_file_dir)
if self.is_exist(self.conky_config_file_dir):
self.logger.debug('Conky config file will be deleted of {0}.'.format(self.username))
self.delete_file(self.conky_config_file_path)
self.autostart_dir_path = '{0}.config/autostart/'
self.autorun_file_path = '{0}conky.desktop'
home_path = self.Sessions.user_home_path(self.username)
auto_start_file = self.autorun_file_path.format(self.autostart_dir_path.format(home_path))
if self.is_exist(auto_start_file):
self.delete_file(auto_start_file)
self.logger.debug('Removing autostart file of user {0}'.format(self.username))
def handle_mode(context):
safe = Safe(context)
safe.handle_safe_mode()