Add external task for install monit
This commit is contained in:
parent
0b57bcab9b
commit
2ebe22f79e
6 changed files with 206 additions and 65 deletions
|
|
@ -261,6 +261,7 @@ def pastafari2_add_server_task():
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
task_id=task.task_id
|
task_id=task.task_id
|
||||||
|
|
||||||
|
|
@ -609,6 +610,7 @@ def pastafari2_make_task():
|
||||||
|
|
||||||
error=1
|
error=1
|
||||||
error_form['#server_host_error']=I18n.lang('pastafari2', 'error_exec_task', 'Error: cannot execute the task')
|
error_form['#server_host_error']=I18n.lang('pastafari2', 'error_exec_task', 'Error: cannot execute the task')
|
||||||
|
txt_error=task.txt_error
|
||||||
|
|
||||||
task_id=task.task_id
|
task_id=task.task_id
|
||||||
|
|
||||||
|
|
|
||||||
102
models/tasks.py
102
models/tasks.py
|
|
@ -55,6 +55,7 @@ class Task(WebModel):
|
||||||
self.fields['where_sql_server'].escape=True
|
self.fields['where_sql_server'].escape=True
|
||||||
|
|
||||||
self.txt_error=''
|
self.txt_error=''
|
||||||
|
self.error=False
|
||||||
|
|
||||||
def run_task(self, server, path, name_task, codename_task, description_task, data={}, user='', password='', where_sql_server='', url='', ssh_key_priv='', ssh_key_password='', send_task=True):
|
def run_task(self, server, path, name_task, codename_task, description_task, data={}, user='', password='', where_sql_server='', url='', ssh_key_priv='', ssh_key_password='', send_task=True):
|
||||||
|
|
||||||
|
|
@ -206,81 +207,54 @@ class Task(WebModel):
|
||||||
result = q.enqueue(task, row['id'], job_timeout=3600)
|
result = q.enqueue(task, row['id'], job_timeout=3600)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return send_task_to_server(task_id)
|
return self.send_task_to_server()
|
||||||
|
|
||||||
else:
|
#result = q.enqueue(task, task_id, job_timeout=3600)
|
||||||
|
|
||||||
#Enqueue task function.
|
|
||||||
|
|
||||||
#result = q.enqueue(task, task_id, job_timeout=3600)
|
|
||||||
|
|
||||||
http = urllib3.PoolManager()
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
#@app.route('/exec/<api_key>/<int:task_id>')
|
|
||||||
|
|
||||||
r = http.request('GET', 'http://127.0.0.1:1337/exec/{}/{}'.format(config_task.api_key, task_id))
|
|
||||||
|
|
||||||
#print('http://127.0.0.1:1337/exec/{}/{}'.format(task_id, pastafari2_api_key))
|
|
||||||
|
|
||||||
if r.status!=200:
|
|
||||||
self.error=True
|
|
||||||
self.txt_error="Cannot access to task server: Error "+str(r.status)
|
|
||||||
return False
|
|
||||||
|
|
||||||
else:
|
|
||||||
resp=json.loads(r.data.decode('utf-8'))
|
|
||||||
|
|
||||||
if resp['error']:
|
|
||||||
self.error=True
|
|
||||||
self.txt_error=resp['message']
|
|
||||||
logtask=LogTask(self.connection)
|
|
||||||
logtask.insert(resp)
|
|
||||||
return False
|
|
||||||
|
|
||||||
except urllib3.exceptions.MaxRetryError:
|
|
||||||
self.error=True
|
|
||||||
self.txt_error='Cannot connect to the task server, check if is up'
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Function for send task to task server
|
# Function for send task to task server
|
||||||
|
|
||||||
def send_task_to_server(task_id):
|
def send_task_to_server(self):
|
||||||
|
|
||||||
http = urllib3.PoolManager()
|
http = urllib3.PoolManager()
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
#@app.route('/exec/<api_key>/<int:task_id>')
|
||||||
|
|
||||||
|
r = http.request('GET', 'http://127.0.0.1:1337/exec/{}/{}'.format(config_task.api_key, self.task_id))
|
||||||
|
|
||||||
|
#print('http://127.0.0.1:1337/exec/{}/{}'.format(task_id, pastafari2_api_key))
|
||||||
|
|
||||||
|
if r.status!=200:
|
||||||
|
self.error=True
|
||||||
|
self.txt_error="Cannot access to task server: Error "+str(r.status)
|
||||||
|
#logtask=LogTask(conn)
|
||||||
|
#logtask.insert({'task_id': task_id, 'progress': 100, 'message': I18n.lang('pastafari', 'error_in_task', 'Error: error in task server, http error: '+r.status), 'error': 1, 'status': 1})
|
||||||
|
|
||||||
try:
|
return False
|
||||||
|
|
||||||
#@app.route('/exec/<api_key>/<int:task_id>')
|
else:
|
||||||
|
resp=json.loads(r.data.decode('utf-8'))
|
||||||
r = http.request('GET', 'http://127.0.0.1:1337/exec/{}/{}'.format(config_task.api_key, task_id))
|
|
||||||
|
if resp['error']:
|
||||||
#print('http://127.0.0.1:1337/exec/{}/{}'.format(task_id, pastafari2_api_key))
|
self.error=True
|
||||||
|
self.txt_error=resp['message']
|
||||||
if r.status!=200:
|
#logtask=LogTask(conn)
|
||||||
|
#logtask.insert(resp)
|
||||||
|
return False
|
||||||
|
|
||||||
|
except urllib3.exceptions.MaxRetryError:
|
||||||
|
|
||||||
self.error=True
|
self.error=True
|
||||||
self.txt_error="Cannot access to task server: Error "+str(r.status)
|
self.txt_error='Cannot connect to the task server, check if is up'
|
||||||
|
print('Cannot connect to task server...')
|
||||||
|
#logtask=LogTask(conn)
|
||||||
|
#logtask.insert({'task_id': task_id, 'progress': 100, 'message': I18n.lang('pastafari', 'error_cannot_connect_to_task', 'Cannot connect to the task server, check if is up'), 'error': 1, 'status': 1})
|
||||||
return False
|
return False
|
||||||
|
|
||||||
else:
|
return True
|
||||||
resp=json.loads(r.data.decode('utf-8'))
|
|
||||||
|
|
||||||
if resp['error']:
|
|
||||||
self.error=True
|
|
||||||
self.txt_error=resp['message']
|
|
||||||
logtask=LogTask(self.connection)
|
|
||||||
logtask.insert(resp)
|
|
||||||
return False
|
|
||||||
|
|
||||||
except urllib3.exceptions.MaxRetryError:
|
|
||||||
self.error=True
|
|
||||||
self.txt_error='Cannot connect to the task server, check if is up'
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
# Function used in rq worker for exec the ssh task in rq.
|
# Function used in rq worker for exec the ssh task in rq.
|
||||||
|
|
||||||
|
|
|
||||||
97
scripts/system/install_monit_stats.py
Normal file
97
scripts/system/install_monit_stats.py
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
#!/opt/pythonenv/bin/python3 -u
|
||||||
|
|
||||||
|
# A script for install pzoo user
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import argparse
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import pwd
|
||||||
|
from subprocess import call
|
||||||
|
from pathlib import Path
|
||||||
|
from time import sleep
|
||||||
|
import distro
|
||||||
|
|
||||||
|
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_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('--group', help='Server group', required=False)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
#url=args.url
|
||||||
|
|
||||||
|
check_url = re.compile(
|
||||||
|
r'^(?:http|ftp)s?://' # http:// or https://
|
||||||
|
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
|
||||||
|
r'localhost|' #localhost...
|
||||||
|
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
|
||||||
|
r'(?::\d+)?' # optional port
|
||||||
|
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
||||||
|
|
||||||
|
print('{"error": 0, "status": 0, "progress": 0, "no_progress":0, "message": "Installing tools for monitoring the server..."}')
|
||||||
|
|
||||||
|
#check_url.match(args.url) and
|
||||||
|
|
||||||
|
if check_url.match(args.url_stats):
|
||||||
|
|
||||||
|
|
||||||
|
# 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": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error, cannot start pastafari stats"}')
|
||||||
|
exit(1)
|
||||||
|
else:
|
||||||
|
print('{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Pastafari stats ready"}')
|
||||||
|
|
||||||
|
print('{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Pastafaristats Tools installed..."}')
|
||||||
|
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
print('{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error installing the module, not valid url"}')
|
||||||
|
|
||||||
|
|
||||||
|
exit(1)
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
[info]
|
[info]
|
||||||
name=Servers
|
name=Servers
|
||||||
description=Install servers infraestructure (http, mail, etc...)
|
description=Install servers infraestructure (http, mail, monitoritation, etc...)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
7
tasks/servers/monit/info.cfg
Normal file
7
tasks/servers/monit/info.cfg
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
[info]
|
||||||
|
name=Monitoritation servers
|
||||||
|
description=Install servers for monitoritation of others servers
|
||||||
|
|
||||||
|
[modules]
|
||||||
|
monit/monit=Simple monitoritation and alerts for your servers
|
||||||
|
|
||||||
61
tasks/servers/monit/monit/monit.py
Normal file
61
tasks/servers/monit/monit/monit.py
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
#!/opt/pythonenv/python3
|
||||||
|
|
||||||
|
from modules.pastafari2.libraries.task import Task
|
||||||
|
from modules.pastafari2.models.pastafari2 import ServerDbTask, ServerGroup
|
||||||
|
from modules.pastafari2.libraries.configtask import config_task
|
||||||
|
from settings import config
|
||||||
|
from paramecio2.libraries.urls import make_url
|
||||||
|
|
||||||
|
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={}):
|
||||||
|
|
||||||
|
super().__init__(server, conn, remote_user, remote_password, private_key, password_key, remote_path, task_id, data)
|
||||||
|
|
||||||
|
#self.files=[['modules/pastafari2/scripts/system/alive.sh', 0o755]]
|
||||||
|
|
||||||
|
self.files=[['modules/pastafari2/scripts/system/install_monit_stats.py', 0o755]]
|
||||||
|
|
||||||
|
self.commands_to_execute=[]
|
||||||
|
|
||||||
|
self.one_time=True
|
||||||
|
|
||||||
|
self.version='1.0'
|
||||||
|
|
||||||
|
|
||||||
|
def pre_task(self):
|
||||||
|
|
||||||
|
ssh_user=config_task.remote_user
|
||||||
|
|
||||||
|
url_stats=config.domain_url+make_url('get_data/'+config.monit_api_key)
|
||||||
|
#url_for('monit_app.monit_get_data', api_key=config.monit_api_key)
|
||||||
|
|
||||||
|
with self.connection.query('select * from serverdbtask WHERE ip=%s', [self.server]) as cursor:
|
||||||
|
arr_server=cursor.fetchone()
|
||||||
|
|
||||||
|
if arr_server:
|
||||||
|
self.data['group_id']=arr_server['group_id']
|
||||||
|
|
||||||
|
if self.data['group_id']!='':
|
||||||
|
server_group=ServerGroup(self.connection)
|
||||||
|
arr_group=server_group.set_conditions('WHERE id=%s', [self.data['group_id']]).select_a_row_where()
|
||||||
|
self.data['group_name']=arr_group['code_group']
|
||||||
|
|
||||||
|
self.commands_to_execute.append(['modules/pastafari2/scripts/system/install_monit_stats.py', '--user='+ssh_user+' --url_stats='+url_stats+' --group="'+self.data['group_name']+'"', 'sudo'])
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
def post_task(self):
|
||||||
|
|
||||||
|
server=ServerDbTask(self.connection)
|
||||||
|
|
||||||
|
server.safe_query()
|
||||||
|
|
||||||
|
#'subdomain_id': self.data['subdomain_id']
|
||||||
|
|
||||||
|
server.insert({'hostname': self.data['hostname'], 'ip': self.data['ip'], 'group_id': self.data['group_id']})
|
||||||
|
|
||||||
|
return True
|
||||||
|
"""
|
||||||
Loading…
Add table
Add a link
Reference in a new issue