pastafari2/tasks/system/add_user.py

64 lines
2.3 KiB
Python

#/usr/bin/env python3
from collections import OrderedDict
import json
from modules.pastafari2.libraries.task import Task
from modules.pastafari2.models.pastafari2 import ServerDbTask, SystemUser
from modules.pastafari2.libraries.configtask import config_task
#import distro
class ServerTask(Task):
def __init__(self, server, conn, remote_user='root', remote_password='', private_key='./ssh/id_rsa', password_key='', remote_path='pastafari2', task_id=0, data={}, port=22):
super().__init__(server, conn, remote_user, remote_password, private_key, password_key, remote_path, task_id, data, port)
self.name_task='Add user to server'
self.description_task='Add user to the selected server'
self.codename_task='add_user'
self.files=[]
self.files=[['modules/pastafari2/scripts/system/add_user.py', 0o755]]
# Format first array element is command with the interpreter, the task is agnostic, the files in os directory. The commands are setted with 750 permission.
# First element is the file, next elements are the arguments
self.commands_to_execute=[['modules/pastafari2/scripts/system/add_user.py', '']]
#THe files to delete
self.delete_files=[]
self.delete_directories=['modules/pastafari2/scripts']
#self.task=Task(conn)
self.one_time=False
self.version='1.0'
self.path_module='admin_app.pastafari2_dashboard'
def pre_task(self):
self.commands_to_execute=[['/home/{}/pythonenv/bin/python3 -u modules/pastafari2/scripts/system/add_user.py'.format(config_task.remote_user), '--user={} --home={} --shell={}'.format(self.data['username'], self.data['home'], '/bin/bash')]]
return True
def post_task(self):
serverdb=ServerDbTask(self.connection)
system_user=SystemUser(self.connection)
arr_server=serverdb.set_conditions('WHERE ip=%s', [self.server]).select_a_row_where()
system_user.create_forms()
system_user.insert({'username': self.data['username'], 'home': self.data['home'], 'server_id': arr_server['id']})
return True