mirror of
https://github.com/altlinux/gpupdate.git
synced 2025-03-21 18:50:38 +03:00
Added implementation of running scripts
This commit is contained in:
parent
877ce7b2aa
commit
31ba4ad214
@ -21,7 +21,6 @@ import subprocess
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class Scripts_runner:
|
||||
|
||||
def __init__(self, work_mode = None, user_name = None, action = None):
|
||||
@ -29,11 +28,13 @@ class Scripts_runner:
|
||||
self.dir_scripts_users = '/var/cache/gpupdate_scripts_cache/users/'
|
||||
self.user_name = user_name
|
||||
self.sort_path_scripts = dict()
|
||||
self.list_with_all_commands = list()
|
||||
if action:
|
||||
self.action = action.upper()
|
||||
else:
|
||||
print('Action needed')
|
||||
return
|
||||
|
||||
self.stack_dir = list()
|
||||
|
||||
if work_mode and work_mode.upper() == 'MACHINE':
|
||||
@ -44,7 +45,11 @@ class Scripts_runner:
|
||||
self.find_action()
|
||||
self.sort_path_scripts = sorted(self.sort_path_scripts.items(), reverse = True)
|
||||
for key in self.sort_path_scripts:
|
||||
self.search_scripts_in_dir(key[1])
|
||||
self.list_with_all_commands.append(self.search_scripts_in_dir(key[1]))
|
||||
|
||||
for it in range(len(self.list_with_all_commands)):
|
||||
for it_cmd in self.list_with_all_commands.pop(0):
|
||||
print(self.run_cmd_subprocess(it_cmd))
|
||||
|
||||
def user_runner_fill(self):
|
||||
if self.dir_scripts_users and self.user_name:
|
||||
@ -68,7 +73,6 @@ class Scripts_runner:
|
||||
|
||||
def find_action(self):
|
||||
self.stack_dir.sort()
|
||||
dict_scr = dict()
|
||||
while self.stack_dir:
|
||||
path_turn = self.stack_dir.pop()
|
||||
key_dict = str(path_turn).split('/')[-1]
|
||||
@ -76,8 +80,39 @@ class Scripts_runner:
|
||||
for action in list_tmp:
|
||||
if action.find(self.action) != -1:
|
||||
self.sort_path_scripts[key_dict] = action
|
||||
|
||||
def search_scripts_in_dir(self, path_to_action):
|
||||
pass
|
||||
one_gpt_action_scripts = self.get_stack_dir(path_to_action, True)
|
||||
return self.launch_list(one_gpt_action_scripts)
|
||||
|
||||
def launch_list(self, list_gpt):
|
||||
list_gpt = sorted(list_gpt)
|
||||
script_cmd = list()
|
||||
for file_in_task_dir in list_gpt:
|
||||
if file_in_task_dir[-4:].upper() == '.ARG':
|
||||
try:
|
||||
arg = self.read_args(file_in_task_dir)
|
||||
script_cmd[-1] += arg
|
||||
except Exception as exc:
|
||||
print('Argument read for {}: {}'.format(script_cmd.pop(), exc))
|
||||
else:
|
||||
cmd = list()
|
||||
cmd.append(file_in_task_dir)
|
||||
script_cmd.append(cmd)
|
||||
|
||||
return script_cmd
|
||||
|
||||
def read_args(self, path):
|
||||
with open(path + '/arg') as f:
|
||||
args = f.readlines()
|
||||
return args
|
||||
|
||||
def run_cmd_subprocess(self, cmd):
|
||||
try:
|
||||
res = subprocess.check_output(cmd, encoding='utf-8')
|
||||
return 'Script output: {}'.format(res)
|
||||
except Exception as exc:
|
||||
return exc
|
||||
|
||||
|
||||
|
||||
@ -87,10 +122,8 @@ if __name__ == '__main__':
|
||||
parser.add_argument('--user', type = str, help = 'User name ', nargs = '?', default = None)
|
||||
parser.add_argument('--action', type = str, help = 'MACHINE : [STARTUP or SHUTDOWN], USER : [LOGON or LOGOFF]', nargs = '?', default = None)
|
||||
|
||||
|
||||
args = parser.parse_args()
|
||||
try:
|
||||
Scripts_runner(args.mode, args.user, args.action)
|
||||
except Exception as exc:
|
||||
print(exc)
|
||||
|
||||
print(exc)
|
Loading…
x
Reference in New Issue
Block a user