Fixes in more quit monit dependencies

This commit is contained in:
absurdo 2023-09-03 00:47:06 +02:00
parent 65111f67ae
commit e4905c9769
4 changed files with 157 additions and 146 deletions

View file

@ -224,7 +224,7 @@ def pastafari2_add_server_task():
except: except:
pass pass
ssh_port='22'
#make ping to server #make ping to server
@ -232,6 +232,10 @@ def pastafari2_add_server_task():
if server_host=='': if server_host=='':
error=1 error=1
error_form['#server_host_error']=I18n.lang('pastafari2', 'error_hostname', 'Error: you need enter a valid hostname') error_form['#server_host_error']=I18n.lang('pastafari2', 'error_hostname', 'Error: you need enter a valid hostname')
if server_username=='':
error=1
error_form['#server_username_error']=I18n.lang('pastafari2', 'error_username', 'Error: you need enter a valid username for the server')
txt_error='' txt_error=''
@ -252,7 +256,9 @@ def pastafari2_add_server_task():
arr_group=server_group.set_conditions('WHERE id=%s', [group_id]).select_a_row_where() arr_group=server_group.set_conditions('WHERE id=%s', [group_id]).select_a_row_where()
group_name=arr_group['code_group'] group_name=arr_group['code_group']
data={'ssh_user': ssh_user, 'pub_key': public_key, 'url_stats': config.domain_url+url_for('monit_app.monit_get_data', api_key=config.monit_api_key), 'hostname': server_host, 'ip': ip, 'group_id': group_id, 'group_name': group_name} # 'url_stats': config.domain_url+url_for('monit_app.monit_get_data', api_key=config.monit_api_key)
data={'ssh_user': ssh_user, 'pub_key': public_key, 'hostname': server_host, 'ip': ip, 'group_id': group_id, 'group_name': group_name}
with SSHTask(server_host, db, remote_user=server_username, remote_password=server_password, private_key=private_key, remote_path=remote_path, task_id=task_id, data=data, port=ssh_port) as ssh_task: with SSHTask(server_host, db, remote_user=server_username, remote_password=server_password, private_key=private_key, remote_path=remote_path, task_id=task_id, data=data, port=ssh_port) as ssh_task:
if not ssh_task.prepare_connection(): if not ssh_task.prepare_connection():
@ -263,19 +269,19 @@ def pastafari2_add_server_task():
# Prepare task for install monit # Prepare task for install monit
pass pass
task=Task(db) task=Task(db)
task_id=0 task_id=0
path_task='modules.pastafari2.tasks.system.task' path_task='modules.pastafari2.tasks.system.task'
if not task.run_task(ip, path_task, 'Add new server', 'add_new_server', 'Task for add a new server', user=server_username, password=server_password, where_sql_server='', url='', data=data, send_task=True, ssh_port=ssh_port): if not task.run_task(ip, path_task, 'Add new server', 'add_new_server', 'Task for add a new server', user=server_username, password=server_password, where_sql_server='', url='', data=data, send_task=True, ssh_port=ssh_port):
error=1 error=1
error_form['#server_host_error']=I18n.lang('pastafari2', 'error_exec_task', 'Error: cannot execute the task '+task.txt_error) error_form['#server_host_error']=I18n.lang('pastafari2', 'error_exec_task', 'Error: cannot execute the task '+task.txt_error)
txt_error=I18n.lang('pastafari2', 'error_exec_task', 'Error: cannot execute the task '+task.txt_error) txt_error=I18n.lang('pastafari2', 'error_exec_task', 'Error: cannot execute the task '+task.txt_error)
task_id=task.task_id task_id=task.task_id
return {'error': error, 'txt_error': txt_error, 'error_form': error_form, 'task_id': task_id} return {'error': error, 'txt_error': txt_error, 'error_form': error_form, 'task_id': task_id}

View file

@ -16,7 +16,7 @@ import distro
parser = argparse.ArgumentParser(description='A script for install leviathan user') parser = argparse.ArgumentParser(description='A script for install leviathan user')
#parser.add_argument('--url', help='The url where notify updates', required=True) #parser.add_argument('--url', help='The url where notify updates', required=True)
parser.add_argument('--url_stats', help='The url where pastafaristats notify the stats', required=True) #parser.add_argument('--url_stats', help='The url where pastafaristats notify the stats', required=True)
parser.add_argument('--user', help='The user for pastafari', required=True) parser.add_argument('--user', help='The user for pastafari', required=True)
parser.add_argument('--pub_key', help='The pub key used in pastafari user', required=True) parser.add_argument('--pub_key', help='The pub key used in pastafari user', required=True)
parser.add_argument('--group', help='Server group', required=False) parser.add_argument('--group', help='Server group', required=False)
@ -38,142 +38,144 @@ print('{"error": 0, "status": 0, "progress": 0, "no_progress":0, "message": "Ins
#check_url.match(args.url) and #check_url.match(args.url) and
if check_url.match(args.url_stats): #if check_url.match(args.url_stats):
try:
u=pwd.getpwnam(args.user)
try: if call("sudo userdel -r %s" % args.user, shell=True) > 0:
u=pwd.getpwnam(args.user) print('Error, user with same username exists and cannot delete of the server')
exit(1)
if call("sudo userdel -r %s" % args.user, shell=True) > 0: else:
print('Error, user with same username exists and cannot delete of the server') print('Cleaning user with the same name')
exit(1)
else: except:
print('Cleaning user with the same name') pass
except:
pass
# Create users # Create users
if call("sudo useradd -m -s /bin/bash %s" % args.user, shell=True) > 0: if call("sudo useradd -m -s /bin/bash %s" % args.user, shell=True) > 0:
# Delete user with this name, you should not install it in a old server. # Delete user with this name, you should not install it in a old server.
if call("sudo userdel -r %s" % args.user, shell=True) > 0: if call("sudo userdel -r %s" % args.user, shell=True) > 0:
print('Error, remove the old user') print('Error, remove the old user')
exit(1)
else:
if call("sudo useradd -m -s /bin/sh %s" % args.user, shell=True) > 0:
print('Error, cannot add a new user')
exit(1) exit(1)
else:
else:
print('Added user')
if call("sudo mkdir -p /home/"+args.user+"/.ssh && sudo chown "+args.user+":"+args.user+" /home/"+args.user+"/.ssh && sudo chmod 700 /home/"+args.user+"/.ssh", shell=True) > 0:
print('Error, cannot add ssh directory')
exit(1)
else:
print('Added ssh directory')
if call("sudo cp "+args.pub_key+" /home/"+args.user+"/.ssh/authorized_keys && sudo chown "+args.user+":"+args.user+" /home/"+args.user+"/.ssh/authorized_keys && sudo chmod 600 /home/"+args.user+"/.ssh/authorized_keys", shell=True) > 0:
print('Error, cannot pub key to user')
exit(1)
else:
print('Added pub key to user')
# Edit sudo file
if call("sudo useradd -m -s /bin/sh %s" % args.user, shell=True) > 0: with open('modules/pastafari2/scripts/system/sudoers.d/spanel') as f:
print('Error, cannot add a new user') sudoers=f.read()
exit(1)
with open('/etc/sudoers.d/spanel', 'w') as f:
else: sudoers=sudoers.replace("spanel", args.user)
print('Added user') f.write(sudoers)
if call("sudo mkdir -p /home/"+args.user+"/.ssh && sudo chown "+args.user+":"+args.user+" /home/"+args.user+"/.ssh && sudo chmod 700 /home/"+args.user+"/.ssh", shell=True) > 0: # Installing composer things for php alerts...
print('Error, cannot add ssh directory')
#mkdir $HOME/pzoo/scripts
#chown $USER:$USER $HOME/pzoo/scripts
#su - $USER -s /bin/bash -c "composer --working-dir=$HOME/scripts require guzzlehttp/guzzle:~6.0"
args.path=os.path.basename(args.path)
pt=Path('/home/'+args.user+'/'+args.path+'/tasks')
pt.mkdir(mode=0o755, parents=True, exist_ok=True)
shutil.chown('/home/'+args.user+'/'+args.path, args.user, args.user)
shutil.chown('/home/'+args.user+'/'+args.path+'/tasks', args.user, args.user)
# Create scripts pzoo
"""
if call("sudo /opt/pythonenv/bin/pip3 install --upgrade git+https://bitbucket.org/paramecio/pastafaristats", shell=True)>0:
print('Error, cannot install pastafari stats')
exit(1)
else:
print('Added pastafari stats')
# Add configuration to pastafari stats
if not os.path.isdir('/etc/pastafari'):
# Create pastafari dir
p=Path('/etc/pastafari')
p.mkdir(mode=0o755, parents=False, exist_ok=True)
with open('/etc/pastafari/stats.cfg', 'w') as f:
f.write("[DEFAULT]\n\nurl_server="+args.url_stats+"\ngroup="+args.group)
with open('/etc/systemd/system/pastafaristats.service', 'w') as f:
#f.write(systemd_unit)
f.write('# Save it in /etc/systemd/system/pastafaristats.service\n')
f.write('[Unit]\n')
f.write('Description=Pastafari Stats\n')
f.write('After=syslog.target\n')
f.write('After=network.target\n\n')
f.write('[Service]\n')
f.write('Type=simple\n')
f.write('User=pzoo\n'.replace('pzoo', args.user))
f.write('Group=pzoo\n'.replace('pzoo', args.user))
f.write('ExecStart=/opt/pythonenv/bin/pastafaristats\n')
f.write('Restart=always\n')
f.write('Environment=PYTHONUNBUFFERED=1\n\n')
f.write('[Install]\n')
f.write('WantedBy=multi-user.target\n')
if call("sudo systemctl enable pastafaristats.service && sudo systemctl start pastafaristats.service", shell=True)>0:
print('Error, cannot start pastafari stats')
exit(1)
else:
print('Pastafari stats ready')
print('{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Tools installed..."}')
"""
sleep(1)
linux_distro=distro.id()
# sudo su - %s -s /bin/bash -c 'mkdir -p %s'
if linux_distro=='arch':
print('Add utilities for archlinux distro...')
if call("sudo su - %s -s /bin/bash -c 'sudo pacman -S --noconfirm --needed git base-devel && git clone https://aur.archlinux.org/yay.git && cd yay && makepkg -si --noconfirm --needed'" % args.user, shell=True) > 0:
print('Error, cannot add utilities for archlinux')
exit(1) exit(1)
else:
print('Added ssh directory')
if call("sudo cp "+args.pub_key+" /home/"+args.user+"/.ssh/authorized_keys && sudo chown "+args.user+":"+args.user+" /home/"+args.user+"/.ssh/authorized_keys && sudo chmod 600 /home/"+args.user+"/.ssh/authorized_keys", shell=True) > 0:
print('Error, cannot pub key to user')
exit(1)
else:
print('Added pub key to user')
# Edit sudo file
with open('modules/pastafari2/scripts/system/sudoers.d/spanel') as f:
sudoers=f.read()
with open('/etc/sudoers.d/spanel', 'w') as f:
sudoers=sudoers.replace("spanel", args.user)
f.write(sudoers)
# Installing composer things for php alerts...
#mkdir $HOME/pzoo/scripts
#chown $USER:$USER $HOME/pzoo/scripts
#su - $USER -s /bin/bash -c "composer --working-dir=$HOME/scripts require guzzlehttp/guzzle:~6.0"
args.path=os.path.basename(args.path)
pt=Path('/home/'+args.user+'/'+args.path+'/tasks')
pt.mkdir(mode=0o755, parents=True, exist_ok=True)
shutil.chown('/home/'+args.user+'/'+args.path, args.user, args.user)
shutil.chown('/home/'+args.user+'/'+args.path+'/tasks', args.user, args.user)
# Create scripts pzoo
"""
if call("sudo /opt/pythonenv/bin/pip3 install --upgrade git+https://bitbucket.org/paramecio/pastafaristats", shell=True)>0:
print('Error, cannot install pastafari stats')
exit(1)
else:
print('Added pastafari stats')
# Add configuration to pastafari stats
if not os.path.isdir('/etc/pastafari'):
# Create pastafari dir
p=Path('/etc/pastafari')
p.mkdir(mode=0o755, parents=False, exist_ok=True)
with open('/etc/pastafari/stats.cfg', 'w') as f:
f.write("[DEFAULT]\n\nurl_server="+args.url_stats+"\ngroup="+args.group)
with open('/etc/systemd/system/pastafaristats.service', 'w') as f:
#f.write(systemd_unit)
f.write('# Save it in /etc/systemd/system/pastafaristats.service\n')
f.write('[Unit]\n')
f.write('Description=Pastafari Stats\n')
f.write('After=syslog.target\n')
f.write('After=network.target\n\n')
f.write('[Service]\n')
f.write('Type=simple\n')
f.write('User=pzoo\n'.replace('pzoo', args.user))
f.write('Group=pzoo\n'.replace('pzoo', args.user))
f.write('ExecStart=/opt/pythonenv/bin/pastafaristats\n')
f.write('Restart=always\n')
f.write('Environment=PYTHONUNBUFFERED=1\n\n')
f.write('[Install]\n')
f.write('WantedBy=multi-user.target\n')
if call("sudo systemctl enable pastafaristats.service && sudo systemctl start pastafaristats.service", shell=True)>0:
print('Error, cannot start pastafari stats')
exit(1)
else:
print('Pastafari stats ready')
print('{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Tools installed..."}')
"""
sleep(1)
linux_distro=distro.id()
# sudo su - %s -s /bin/bash -c 'mkdir -p %s'
if linux_distro=='arch':
print('Add utilities for archlinux distro...')
if call("sudo su - %s -s /bin/bash -c 'sudo pacman -S --noconfirm --needed git base-devel && git clone https://aur.archlinux.org/yay.git && cd yay && makepkg -si --noconfirm --needed'" % args.user, shell=True) > 0:
print('Error, cannot add utilities for archlinux')
exit(1)
"""
else: else:
print('Error installing the module, not valid url') print('Error installing the module, not valid url')
exit(1) exit(1)
"""

View file

@ -27,7 +27,10 @@ class ServerTask(Task):
self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_php.py', '']) self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_php.py', ''])
self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_unixutils.sh', '']) self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_unixutils.sh', ''])
self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_psutil.sh', '']) self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_psutil.sh', ''])
self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_pzoo_stats.py', '--user='+self.data['ssh_user']+' --pub_key='+self.data['pub_key']+' --url_stats='+self.data['url_stats']+' --group="'+self.data['group_name']+'" --path='+remote_path])
#' --url_stats='+self.data['url_stats']+
self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_pzoo_stats.py', '--user='+self.data['ssh_user']+' --pub_key='+self.data['pub_key']+' --group="'+self.data['group_name']+'" --path='+remote_path])
def post_task(self): def post_task(self):

View file

@ -7,8 +7,8 @@
<form method="post" name="add_server" id="add_server_form"> <form method="post" name="add_server" id="add_server_form">
<p>Please fill the form for add the new server to the system.</p> <p>Please fill the form for add the new server to the system.</p>
<p><label>${lang('pastafari2', 'server_host', 'Server host')}* <span class="pastafari2_error error" id="server_host_error"></span></label> <input type="text" name="server_host" value=""></p> <p><label>${lang('pastafari2', 'server_host', 'Server host')}* <span class="pastafari2_error error" id="server_host_error"></span></label> <input type="text" name="server_host" value=""></p>
<p><label>${lang('pastafari2', 'server_group', 'Server group')} <span class="pastafari2_error error" id="group_id_error"></span></label> ${group_form.form()|n}</p> <p><label>${lang('pastafari2', 'server_group', 'Server group')}* <span class="pastafari2_error error" id="group_id_error"></span></label> ${group_form.form()|n}</p>
<p><label>${lang('pastafari2', 'server_username', 'Server username')} <span class="pastafari2_error error" id="server_username_error"></span></label> <input type="text" name="server_username" value=""></p> <p><label>${lang('pastafari2', 'server_username', 'Server username')}* <span class="pastafari2_error error" id="server_username_error"></span></label> <input type="text" name="server_username" value=""></p>
<p><label>${lang('pastafari2', 'server_password', 'Server password.')} <span class="pastafari2_error error" id="server_password_error"></span></label> <input type="password" name="server_password" value=""></p> <p><label>${lang('pastafari2', 'server_password', 'Server password.')} <span class="pastafari2_error error" id="server_password_error"></span></label> <input type="password" name="server_password" value=""></p>
<p><label>${lang('pastafari2', 'repeat_server_password', 'Repeat server password')} <span class="pastafari2_error error" id="repeat_server_password_error"></span></label> <input type="password" name="repeat_server_password" value=""></p> <p><label>${lang('pastafari2', 'repeat_server_password', 'Repeat server password')} <span class="pastafari2_error error" id="repeat_server_password_error"></span></label> <input type="password" name="repeat_server_password" value=""></p>
<p><label>${lang('pastafari2', 'ssh_port', 'SSH port')}* <span class="pastafari2_error error" id="ssh_port_error"></span></label> <input type="number" name="ssh_port" value="22"></p> <p><label>${lang('pastafari2', 'ssh_port', 'SSH port')}* <span class="pastafari2_error error" id="ssh_port_error"></span></label> <input type="number" name="ssh_port" value="22"></p>