Ejemplo n.º 1
0
 def __init__(self, name, size=1):
     
     required=False
     
     self.yes_text=I18n.lang('common', 'yes', 'Yes')
     self.no_text=I18n.lang('common', 'no', 'No')
     
     super(IntegerField, self).__init__(name, size, required)
Ejemplo n.º 2
0
 def show_formatted(self, value):
     
     value=int(value)
     
     if value==0:
         return I18n.lang('admin', 'without_privileges', 'Without privileges')
     elif value==1:
         return I18n.lang('admin', 'selected_privileges', 'Selected privileges')
     elif value==2:
         return I18n.lang('admin', 'administrator', 'Administrator')
Ejemplo n.º 3
0
def tasklist(server_id):
    
    content_index=''
    
    if check_login():

        conn=WebModel.connection()

        server=Server(conn)
        
        arr_server=server.select_a_row(server_id)
        
        if arr_server:

            s=get_session()

            menu=get_menu(config_admin.modules_admin)
        
            lang_selected=get_language(s)           

            task=Task(conn)
            
            arr_task_id=[]
            
            with task.query('select DISTINCT task_id from logtask where server=%s', [arr_server['ip']]) as cursor:
                for arr_task in cursor:
                    arr_task_id.append(arr_task['task_id'])
            
            task.set_conditions('WHERE id IN '+task.check_in_list_str('id', arr_task_id), [])
            
            task_list=SimpleList(task, make_url(pastafari_folder+'/tasklist/'+str(server_id), {}), t)
        
            task_list.fields_showed=['id', 'name_task', 'description_task', 'error', 'status']
        
            task_list.yes_search=False

            task_list.order_field='id'
            task_list.order=1
            
            request.environ['server_id']=arr_server['id']

            task_list.arr_extra_options=[server_task_options]
            
            return_url=make_url('pastafari/servers')
        
            content_index=t.load_template('pastafari/admin/showtasks.phtml', task_list=task_list, server=arr_server, return_url=return_url)
            
        else:
            cont_index=I18n.lang('pastafari', 'sorry_i_cannot_find_the_server', 'Sorry, i cannot find the server')
        
        conn.close()  
        
        return t.load_template('admin/content.html', title=I18n.lang('pastafari', 'task_list_of', 'Task list of')+ ' %s' % arr_server['hostname'], content_index=content_index, menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n)
    
    return ""
Ejemplo n.º 4
0
 def make_error_task(self):
     
     if self.error_task!=None:
         self.logtask.insert({'task_id': self.id, 'progress': 0, 'message': I18n.lang('pastafari', 'error_tasks', 'Error tasks executing...'), 'error': 0, 'status': 1, 'server': self.server})
         
         if self.error_task(self):
             self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': I18n.lang('pastafari', 'error_tasks_executed', 'Error tasks executed successfully...'), 'error': 0, 'status': 1, 'server': self.server})
         else:
             self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': I18n.lang('pastafari', 'error_tasks_executed', 'Error Post task cannot be executed...'), 'error': 0, 'status': 1, 'server': self.server})
             
             return False
Ejemplo n.º 5
0
    def make_error_task(self):

        if self.error_task != None:
            self.logtask.insert({
                'task_id':
                self.id,
                'progress':
                0,
                'message':
                I18n.lang('pastafari', 'error_tasks',
                          'Error tasks executing...'),
                'error':
                0,
                'status':
                1,
                'server':
                self.server
            })

            if self.error_task(self):
                self.logtask.insert({
                    'task_id':
                    self.id,
                    'progress':
                    100,
                    'message':
                    I18n.lang('pastafari', 'error_tasks_executed',
                              'Error tasks executed successfully...'),
                    'error':
                    0,
                    'status':
                    1,
                    'server':
                    self.server
                })
            else:
                self.logtask.insert({
                    'task_id':
                    self.id,
                    'progress':
                    100,
                    'message':
                    I18n.lang('pastafari', 'error_tasks_executed',
                              'Error Post task cannot be executed...'),
                    'error':
                    0,
                    'status':
                    1,
                    'server':
                    self.server
                })

                return False
Ejemplo n.º 6
0
def admin(**args):
    
    
    t=args['t']
    connection=args['connection']
    
    user_admin=UserAdmin(connection)
    
    user_admin.fields['privileges'].name_form=SelectForm
    
    user_admin.create_forms(['username', 'password', 'email', 'privileges', 'lang'])
    
    user_admin.forms['privileges'].arr_select={0: I18n.lang('admin', 'without_privileges', 'Without privileges'), 1: I18n.lang('admin', 'selected_privileges', 'Selected privileges'), 2: I18n.lang('admin', 'administrator', 'Administrator')}
    
    user_admin.fields['password'].protected=False
    
    url=make_url('admin/ausers', {})
    
    admin=GenerateAdminClass(user_admin, url, t)
    
    admin.list.fields_showed=['username', 'privileges']
    
    admin.list.search_fields=['username']
    
    admin.arr_fields_edit=['username', 'password', 'repeat_password', 'email', 'privileges', 'lang']
    
    #admin.list.limit_pages=5
    
    form_admin=admin.show()
    
    return form_admin
Ejemplo n.º 7
0
def server_options(url, id, arr_row):
    options = []
    options.append('<a href="' +
                   add_get_parameters(url, op='view_status', id=id) + '">' +
                   I18n.lang('pastafari', 'view_status', 'View status') +
                   '</a>')
    return options
Ejemplo n.º 8
0
def check_code_token():
    
    t=PTemplate(env)
    
    if yes_recovery_login==True:
    
        getpost=GetPostFiles()
        
        getpost.obtain_post()
        
        connection=WebModel.connection()
    
        user_admin=UserAdmin(connection)
        
        token=getpost.post.get('token',  '')
        
        token=user_admin.fields['token_recovery'].check(token)
    
        if token.strip()!='':
            
            user_admin.set_conditions('WHERE token_recovery=%s', [token])
            
            user_admin.yes_reset_conditions=False
            
            arr_user=user_admin.select_a_row_where(['id', 'email'])
            
            if arr_user:
                
                new_password=create_key()
                           
                user_admin.valid_fields=['password', 'token_recovery', 'num_tries']

                user_admin.reset_require()
                
                user_admin.check_user=False
                
                if user_admin.update({'password': new_password, 'token_recovery': "", 'num_tries': 0}, False):
                    
                    send_mail=SendMail()
                    
                    content_mail=t.load_template('admin/recovery_password.phtml', password=new_password)
                    
                    if not send_mail.send(email_address, [arr_user['email']], I18n.lang('admin', 'send_password_email', 'Your new password'), content_mail):
                        return {'token': 'Error: i cannot send mail', 'error': 1}
                    
                    return {'token': 'Error: cannot send the maild with the new password', 'error': 0} 
    
    s=get_session()
                
    s['csrf_token']=create_key_encrypt()
    
    s.save()
    
    return {'token': 'Error: token is not valid', 'error': 1,  'csrf_token': s['csrf_token']}
Ejemplo n.º 9
0
def send_password():
    
    connection=WebModel.connection()
    
    user_admin=UserAdmin(connection)
    
    t=PTemplate(env)
    
    getpost=GetPostFiles()
    
    getpost.obtain_post()
    
    email=getpost.post.get('email',  '')
    
    email=user_admin.fields['email'].check(email)
    
    if user_admin.fields['email'].error:
        
        s=get_session()
                
        s['csrf_token']=create_key_encrypt()
        
        s.save()
        
        return {'email': user_admin.fields['email'].txt_error, 'error': 1, 'csrf_token': s['csrf_token']}
        
    else:
        
        user_admin.set_conditions('WHERE email=%s', [email])
        
        user_admin.yes_reset_conditions=False
        
        if user_admin.select_count()==1:
            
            user_admin.reset_require()
            
            user_admin.valid_fields=['token_recovery']
            
            user_admin.check_user=False
            
            token=create_key_encrypt_256()
            
            if user_admin.update({'token_recovery': token}):
                
                send_mail=SendMail()
                
                content_mail=t.load_template('admin/recovery_mail.phtml', token=token)
                
                if not send_mail.send(email_address, [email], I18n.lang('admin', 'send_email', 'Email for recovery your password'), content_mail):
                    return {'email': 'Error: i cannot send mail', 'error': 1}
                
            
        return {'email': '', 'error': 0}
Ejemplo n.º 10
0
def home():
    
    connection=WebModel.connection()
    #Fix, make local variable
    
    s=get_session()
    
    if check_login():
        
        getpostfiles=GetPostFiles()
        
        getpostfiles.obtain_get()

        parent_id=getpostfiles.get.get('parent_id', '0')
        
        parent_id=int(parent_id)
        
        #Load menu
        
        menu=get_menu(config_admin.modules_admin)
    
        lang_selected=get_language(s)
        
        groups=servers.ServerGroup(connection)
        
        groups.create_forms()
        
        groups.forms['parent_id'].default_value=parent_id
        
        hierarchy=HierarchyModelLinks(groups, 'All groups', 'name', 'parent_id', make_url('pastafari/groups'))
        
        hierarchy.prepare()
        
        group_list=GenerateAdminClass(groups, make_url(pastafari_folder+'/groups', {'parent_id': str(parent_id)}), t)
        
        groups.set_conditions('WHERE parent_id=%s', [parent_id])
        
        group_list.list.fields_showed=['name']
        
        group_list.list.arr_extra_options=[task_options]
        
        group_list.list.s['order']='0'
        group_list.list.s['order_field']='name'
        
        group_list.list.yes_search=False
        
        content_index=t.load_template('pastafari/groups.phtml', group_list=group_list, hierarchy_links=hierarchy, son_id=parent_id)
        #group_list.show()
        
        return t.load_template('admin/content.html', title=I18n.lang('pastafari', 'servers_groups', 'Server\'s Group'), content_index=content_index, menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n)
        
    else:
        redirect(make_url(config.admin_folder))
Ejemplo n.º 11
0
def set_extra_forms_user(user_admin):
    
    user_admin.fields['password'].required=True
    user_admin.fields['email'].required=True

    user_admin.create_forms(['username', 'email', 'password'])
    
    user_admin.forms['repeat_password']=PasswordForm('repeat_password', '')
    
    user_admin.forms['repeat_password'].required=True
    
    user_admin.forms['repeat_password'].label=I18n.lang('common', 'repeat_password', 'Repeat Password')
Ejemplo n.º 12
0
 def create_forms(self, arr_fields=[]):
     
     # Add password_repeat to forms from the model
     
     arr_fields=super().create_forms(arr_fields)
     
     if self.password_field in arr_fields and self.yes_repeat_password:
         
         repeat_password=PasswordForm('repeat_password', '')
 
         repeat_password.required=1
         
         repeat_password.label=I18n.lang('common', 'repeat_password', 'Repeat Password')
         
         repeat_password.field=self.fields['password']
         
         self.create_form_after(self.password_field, repeat_password)
     
     return arr_fields
Ejemplo n.º 13
0
def admin(t):

    GetPostFiles.obtain_get()

    GetPostFiles.get['op'] = GetPostFiles.get.get('op', '')

    server = servers.Server()

    url = make_url(config.admin_folder + '/pastafari/servers')

    if GetPostFiles.get['op'] == 'add_new_server':

        #Check if the server can be access with god module using ssh, if yes, install idea(aka virus or platon) using protozoo or similar program

        server.create_forms()

        return ""
        pass
    elif GetPostFiles.get['op'] == 'view_status':

        GetPostFiles.get['id'] = GetPostFiles.get.get('id', '0')

        server_id = int(GetPostFiles.get['id'])

        server_view = server.select_a_row(server_id)

        GetPostFiles.get['show_data'] = GetPostFiles.get.get('show_data', '0')

        if server_view != False:

            if GetPostFiles.get['show_data'] == '0':

                return ts.load_template('server_status.phtml',
                                        server_view=server_view)

            elif GetPostFiles.get['show_data'] == '1':

                get_c = 60

                ptemplate.show_basic_template = False

                network_status = servers.ServerInfoNet()

                c = network_status.select_count()

                begin_c = c - get_c

                if begin_c < 0:

                    begin_c = 0

                network_status.set_conditions('where server=%s',
                                              [server_view['ip']])

                network_status.set_order(['date'], ['ASC'])

                network_status.set_limit([begin_c, get_c])

                network_cur = network_status.select()

                #arr_dates=[]

                arr_dates = {}

                for net_info in network_cur:

                    arr_dates[net_info['device']] = arr_dates.get(
                        net_info['device'], [])

                    net_info['date'] = datetime.format_fulldate(
                        net_info['date'])

                    arr_dates[net_info['device']].append(net_info)

                    #arr_dates.append(arr_date)

                arr_final_dates = {}

                for dev in arr_dates:

                    substract_up = arr_dates[dev][0]['network_up']

                    substract_down = arr_dates[dev][0]['network_down']

                    arr_final_dates[dev] = []

                    for x in range(1, len(arr_dates[dev])):

                        up = arr_dates[dev][x]['network_up'] - substract_up

                        down = arr_dates[dev][x][
                            'network_down'] - substract_down

                        arr_final_dates[dev].append({
                            'date':
                            arr_dates[dev][x]['date'],
                            'network_up':
                            up,
                            'network_down':
                            down
                        })

                        substract_up = arr_dates[dev][x]['network_up']

                        substract_down = arr_dates[dev][x]['network_down']

                        pass

                return json.dumps(arr_final_dates)

    else:

        # Obtain uptimes

        server.conditions = ['where status=%s', [0]]

        num_servers_failed = server.select_count()

        server.conditions = ['where status=%s', [1]]

        num_servers_uptime = server.select_count()

        # Obtain profiles

        server.distinct = 'DISTINCT'

        server.order_by = 'order by profile ASC'

        arr_profiles = server.select_to_array(['profile'])

        profiles = SelectForm('profile', '')

        profiles.arr_select[''] = ''

        for profile in arr_profiles.values():

            profiles.arr_select[profile['profile']] = profile['profile']

        server.distinct = ''

        GetPostFiles.get['profile'] = GetPostFiles.get.get('profile', '')

        url = add_get_parameters(url, profile=GetPostFiles.get['profile'])

        if GetPostFiles.get[
                'profile'] in profiles.arr_select and GetPostFiles.get[
                    'profile'] != '':
            server.conditions = [
                'where profile=%s', [GetPostFiles.get['profile']]
            ]

        profiles.default_value = GetPostFiles.get['profile']

        server_list = SimpleList(server, url, t)

        #server_list.fields=['id', 'type']

        server_list.fields_showed = [
            'hostname', 'ip', 'profile', 'type', 'last_updated'
        ]

        server_list.yes_search = False

        server_list.arr_extra_fields = [
            I18n.lang('common', 'options', 'Options')
        ]

        server_list.arr_extra_options = [server_options]

        return ts.load_template('servers.phtml',
                                server_list=server_list,
                                url=url,
                                profiles=profiles,
                                num_servers_failed=num_servers_failed,
                                num_servers_uptime=num_servers_uptime)
Ejemplo n.º 14
0
def admin(t):
  
    GetPostFiles.obtain_get()
    
    GetPostFiles.get['op']=GetPostFiles.get.get('op', '')
    
    server=servers.Server()
    
    url=make_url(config.admin_folder+'/pastafari/servers')
    
    if GetPostFiles.get['op']=='add_new_server':
    
        #Check if the server can be access with god module using ssh, if yes, install idea(aka virus or platon) using protozoo or similar program
    
        server.create_forms()
    
        return ""
        pass
    elif GetPostFiles.get['op']=='view_status':
        
        GetPostFiles.get['id']=GetPostFiles.get.get('id', '0')
    
        server_id=int(GetPostFiles.get['id'])
    
        server_view=server.select_a_row(server_id)
        
        GetPostFiles.get['show_data']=GetPostFiles.get.get('show_data', '0')
        
        if server_view!=False:
        
            if GetPostFiles.get['show_data']=='0':
    
                return ts.load_template('server_status.phtml', server_view=server_view)
    
            elif GetPostFiles.get['show_data']=='1':
                
                get_c=60
            
                ptemplate.show_basic_template=False
            
                network_status=servers.ServerInfoNet()
                
                c=network_status.select_count()
                
                begin_c=c-get_c
                
                if begin_c<0:
                    
                    begin_c=0
                
                network_status.set_conditions('where server=%s', [server_view['ip']])
                
                network_status.set_order(['date'], ['ASC'])
                
                network_status.set_limit([begin_c, get_c])
                
                network_cur=network_status.select()
                
                #arr_dates=[]
                
                arr_dates={}
                
                for net_info in network_cur:
            
                    arr_dates[net_info['device']]=arr_dates.get(net_info['device'], [])
            
                    net_info['date']=datetime.format_fulldate(net_info['date'])
            
                    arr_dates[net_info['device']].append(net_info)
            
                    #arr_dates.append(arr_date)
            
                arr_final_dates={}
            
                for dev in arr_dates:
                    
                    substract_up=arr_dates[dev][0]['network_up']
                    
                    substract_down=arr_dates[dev][0]['network_down']
                    
                    arr_final_dates[dev]=[]
                    
                    for x in range(1, len(arr_dates[dev])):
                        
                        up=arr_dates[dev][x]['network_up']-substract_up
                        
                        down=arr_dates[dev][x]['network_down']-substract_down
                        
                        arr_final_dates[dev].append({'date': arr_dates[dev][x]['date'], 'network_up': up, 'network_down': down})
                        
                        substract_up=arr_dates[dev][x]['network_up']
                    
                        substract_down=arr_dates[dev][x]['network_down']
                        
                        pass
                    
            
                return json.dumps(arr_final_dates)
    
    else:
    
        # Obtain uptimes
        
        server.conditions=['where status=%s', [0]]
        
        num_servers_failed=server.select_count()
        
        server.conditions=['where status=%s', [1]]
        
        num_servers_uptime=server.select_count()
        
        # Obtain profiles
        
        server.distinct='DISTINCT'
        
        server.order_by='order by profile ASC'
        
        arr_profiles=server.select_to_array(['profile'])
        
        profiles=SelectForm('profile', '')
        
        profiles.arr_select['']=''
        
        for profile in arr_profiles.values():
            
            profiles.arr_select[profile['profile']]=profile['profile']
    
        server.distinct=''
        
        GetPostFiles.get['profile']=GetPostFiles.get.get('profile', '')
        
        url=add_get_parameters(url, profile= GetPostFiles.get['profile'])
        
        if GetPostFiles.get['profile'] in profiles.arr_select and GetPostFiles.get['profile']!='':
            server.conditions=['where profile=%s', [GetPostFiles.get['profile']]]
        
        profiles.default_value=GetPostFiles.get['profile']
    
        server_list=SimpleList(server, url, t)
        
        #server_list.fields=['id', 'type']
        
        server_list.fields_showed=['hostname', 'ip', 'profile', 'type', 'last_updated']
        
        server_list.yes_search=False
        
        server_list.arr_extra_fields=[I18n.lang('common', 'options', 'Options')]
        
        server_list.arr_extra_options=[server_options]
    
        return ts.load_template('servers.phtml', server_list=server_list, url=url, profiles=profiles, num_servers_failed=num_servers_failed, num_servers_uptime=num_servers_uptime)
Ejemplo n.º 15
0
 def standard_options(url, id, arr_row):
     options=[]
     options.append('<a href="'+add_get_parameters(url, op_admin=1, id=id)+'">'+I18n.lang('common', 'edit', 'Edit')+'</a>')
     options.append('<a href="'+add_get_parameters(url, op_admin=3, id=id)+'">'+I18n.lang('common', 'delete', 'Delete')+'</a>')
     return options
Ejemplo n.º 16
0
from paramecio.citoplasma.i18n import I18n, load_lang
from settings.config_admin import modules_admin

modules_other=[I18n.lang('pastafari', 'pastafari', 'Pastafari'), [["Dashboard", 'modules.pastafari.dashboard', '/pastafari'], [I18n.lang('pastafari', 'os', 'Operating systems'), 'modules.pastafari.os', '/pastafari/os'], [I18n.lang('pastafari', 'servers_groups', 'Server Groups'), 'modules.pastafari.groups', '/pastafari/groups'], [I18n.lang('pastafari', 'servers', 'Servers'), 'modules.pastafari.servers', '/pastafari/servers']], 'pastafari']

modules_admin.append(modules_other)
Ejemplo n.º 17
0
    def exec(self):

        # Get task

        #self.id=task_id

        self.task.reset_require()

        self.task.valid_fields = [
            'name_task', 'description_task', 'error', 'status', 'server'
        ]

        self.logtask.valid_fields = self.logtask.fields.keys()

        if self.id == 0:

            # Insert the task

            self.task.reset_require()

            self.task.insert({
                'name_task': self.name_task,
                'description_task': self.description_task,
                'server': self.server
            })

            self.id = self.task.insert_id()

        if not self.prepare_connection():
            #self.task.conditions=['WHERE id=%s', [self.id]]
            #self.task.update({'error': 1, 'status': 1})

            self.logtask.insert({
                'task_id': self.id,
                'progress': 100,
                'message': self.txt_error,
                'error': 1,
                'status': 1,
                'server': self.server
            })

            self.make_error_task()

            return False

        # Pre task

        if self.pre_task != None:
            self.logtask.insert({
                'task_id':
                self.id,
                'progress':
                0,
                'message':
                I18n.lang('pastafari', 'pre_tasks', 'Pre tasks executing...'),
                'error':
                0,
                'status':
                1,
                'server':
                self.server
            })

            if self.pre_task(self):
                self.logtask.insert({
                    'task_id':
                    self.id,
                    'progress':
                    100,
                    'message':
                    I18n.lang('pastafari', 'pre_tasks_executed',
                              'Pre tasks executed successfully...'),
                    'error':
                    0,
                    'status':
                    1,
                    'server':
                    self.server
                })
            else:
                self.logtask.set_conditions('where id=%s', [last_log_id])

                self.logtask.update({
                    'progress': 100,
                    'error': 1,
                    'message': "Error executing post task",
                    'status': 1,
                    'server': self.server
                })

                self.make_error_task()

                return False

        #Check if script was executed

        if self.codename != '':

            if self.one_time == True:

                with self.ssh.open_sftp() as sftp:

                    try:

                        with sftp.file(self.config.remote_path + '/tasks/' +
                                       self.codename) as f:
                            version = f.read()
                            version = version.decode('utf-8').strip()

                            if version == self.version:
                                #self.task.conditions=['WHERE id=%s', [self.id]]
                                #self.task.update({'error': 0, 'status': 1})

                                self.logtask.insert({
                                    'task_id': self.id,
                                    'progress': 100,
                                    'message':
                                    'This script was executed correctly in this server',
                                    'error': 0,
                                    'status': 1,
                                    'server': self.server
                                })

                                return True

                    except IOError:
                        # It was not executed
                        pass

        if not self.upload_files():
            #self.task.conditions=['WHERE id=%s', [self.id]]
            #self.task.update({'error': 1, 'status': 1})

            self.logtask.insert({
                'task_id': self.id,
                'progress': 100,
                'message': self.txt_error,
                'error': 1,
                'status': 1,
                'server': self.server
            })

            self.make_error_task()

            return False

        self.logtask.insert({
            'task_id': self.id,
            'progress': 0,
            'message': 'Executing commands...',
            'error': 0,
            'status': 0,
            'server': self.server
        })

        # Execute commands

        json_code = []

        for c in self.commands_to_execute:

            try:
                command = c[0]

                command = command.replace('${os_server}', self.os_server)

                arguments = c[1]

                sudo_str = ''

                if len(c) == 3:
                    sudo_str = 'sudo '

                #, get_pty=True
                stdin, stdout, stderr = self.ssh.exec_command(
                    sudo_str + self.config.remote_path + '/' + command + ' ' +
                    arguments)

                for line in stdout:

                    if line == None:

                        line = "[]"

                    line = line.strip()

                    try:
                        json_code = json.loads(line)

                        if not 'progress' in json_code or not 'message' in json_code or not 'error' in json_code:
                            #self.task.conditions=['WHERE id=%s', [self.id]]
                            #self.task.update({'error': 1, 'status': 1})

                            self.logtask.insert({
                                'task_id':
                                self.id,
                                'progress':
                                100,
                                'message':
                                'Malformed json code: ' + str(line),
                                'error':
                                1,
                                'server':
                                self.server
                            })
                            self.make_error_task()
                            return False

                        else:

                            json_code['task_id'] = self.id

                            json_code['server'] = self.server

                            self.logtask.insert(json_code)

                            if json_code['error'] == 1:
                                self.make_error_task()
                                return False

                    except:

                        #self.task.conditions=['WHERE id=%s', [self.id]]
                        #self.task.update({'error': 0, 'status':0})

                        self.logtask.insert({
                            'task_id': self.id,
                            'progress': 0,
                            'no_progress': 1,
                            'message': str(line),
                            'error': 0,
                            'status': 0,
                            'server': self.server
                        })

                        #return False

                last_log_id = self.logtask.insert_id()

                if stdout.channel.recv_exit_status() > 0:
                    #line=stdout.readlines()
                    #logging.warning(action.codename+" WARNING: "+line)
                    final_text = 'Error executing the command: %s' % command

                    #self.task.conditions=['WHERE id=%s', [self.id]]
                    #self.task.update({'error': 1, 'status': 1})

                    for line in stdout:
                        final_text += ' ' + line

                    for line in stderr:
                        final_text += ' ' + line

                    self.logtask.set_conditions('where id=%s', [last_log_id])

                    self.logtask.update({
                        'progress': 100,
                        'error': 1,
                        'message': final_text,
                        'status': 1,
                        'server': self.server
                    })
                    self.make_error_task()
                    return False

            except:

                #self.task.conditions=['WHERE id=%s', [self.id]]
                #self.task.update({'error': 1, 'status': 1})

                self.logtask.insert({
                    'task_id': self.id,
                    'progress': 100,
                    'message': traceback.format_exc(),
                    'error': 1,
                    'status': 1,
                    'server': self.server
                })
                self.make_error_task()
                return False

        # Clean files

        if not self.delete_files_and_dirs():

            #self.task.conditions=['WHERE id=%s', [self.id]]
            #self.task.update({'error': 1, 'status': 1})

            self.logtask.insert({
                'task_id': self.id,
                'progress': 100,
                'message': self.txt_error,
                'error': 1,
                'status': 1,
                'server': self.server
            })
            self.make_error_task()
            return False

        #Upload files

        #self.ssh.close()

        # FInish task

        #Put this version how executed

        if self.codename != '':

            if self.one_time == True:

                with self.ssh.open_sftp() as sftp:

                    try:
                        path_check = self.config.remote_path + '/tasks/'

                        f_stat = sftp.stat(path_check)

                    except IOError:

                        sftp.mkdir(path_check)

                    with sftp.file(path_check + self.codename, 'w') as f:
                        f.write(self.version)

        if self.post_task != None:
            self.logtask.insert({
                'task_id':
                self.id,
                'progress':
                0,
                'message':
                I18n.lang('pastafari', 'post_tasks',
                          'Post tasks executing...'),
                'error':
                0,
                'status':
                1,
                'server':
                self.server
            })

            if self.post_task(self):
                self.logtask.insert({
                    'task_id':
                    self.id,
                    'progress':
                    100,
                    'message':
                    I18n.lang('pastafari', 'post_tasks_executed',
                              'Post tasks executed successfully...'),
                    'error':
                    0,
                    'status':
                    1,
                    'server':
                    self.server
                })
            else:
                #self.logtask.set_conditions('where id=%s', [last_log_id])

                #self.logtask.update({'progress': 100, 'error': 1, 'message': "Error executing post task", 'status': 1, 'server': self.server})

                self.logtask.insert({
                    'task_id':
                    self.id,
                    'progress':
                    100,
                    'message':
                    I18n.lang(
                        'pastafari', 'error_post_tasks_executed',
                        'Error executing post task -> ' +
                        self.error_post_task),
                    'error':
                    1,
                    'status':
                    1,
                    'server':
                    self.server
                })
                self.make_error_task()
                return False

        if 'progress' in json_code:
            if json_code['progress'] != 100:
                self.logtask.insert({
                    'task_id':
                    self.id,
                    'progress':
                    100,
                    'message':
                    I18n.lang('pastafari', 'finished_successfully',
                              'All tasks done successfully...'),
                    'error':
                    0,
                    'status':
                    1,
                    'server':
                    self.server
                })
        else:
            self.logtask.insert({
                'task_id':
                self.id,
                'progress':
                100,
                'message':
                I18n.lang('pastafari', 'finished_successfully',
                          'All tasks done successfully...'),
                'error':
                0,
                'status':
                1,
                'server':
                self.server
            })

        # Add
        self.taskdone.create_forms()

        self.taskdone.insert({'name_task': self.codename, 'ip': self.server})

        #self.task.conditions=['WHERE id=%s', [self.id]]
        #self.task.update({'error': 0, 'status': 1})

        #connection.close()
        return True
Ejemplo n.º 18
0
def admin(**args):

    t = args['t']

    conn = args['connection']

    server = servers.Server(conn)

    os_model = servers.OsServer(conn)

    task = tasks.Task(conn)

    logtask = tasks.LogTask(conn)

    group_server = servers.ServerGroup(conn)

    group_server_item = servers.ServerGroupItem(conn)

    getpostfiles = GetPostFiles()

    getpostfiles.obtain_get()

    getpostfiles.get['op'] = getpostfiles.get.get('op', '')

    getpostfiles.get['group_id'] = getpostfiles.get.get('group_id', '0')

    request_type = formsutils.request_type()

    server.fields['os_codename'].name_form = SelectForm

    server.create_forms(['hostname', 'ip', 'os_codename'])

    arr_os = {}

    with os_model.select() as cur:
        arr_os = {v['codename']: v['name'] for v in cur}

    server.forms['os_codename'].arr_select = arr_os

    server.forms['password'] = PasswordForm('password', '')

    server.forms['password'].required = True

    server.forms['password'].label = I18n.lang('pastafari', 'password',
                                               'Password')

    server.forms['delete_root_password'] = SelectForm('delete_root_password',
                                                      '', {
                                                          '0': 'No',
                                                          '1': 'Yes'
                                                      })

    server.forms['delete_root_password'].label = I18n.lang(
        'pastafari', 'delete_root_password', 'Disable root password')

    server.forms['group_id'] = SelectModelForm('group_id',
                                               getpostfiles.get['group_id'],
                                               group_server, 'name', 'id',
                                               'parent_id')

    server.forms['group_id'].field = group_server_item.fields['server_id']

    server.forms['group_id'].required = True

    server.forms['group_id'].label = label = I18n.lang('pastafari',
                                                       'principal_group',
                                                       'Principal group')

    if getpostfiles.get['op'] == '1':

        if request_type != "POST":

            forms = formsutils.show_form({}, server.forms, t, False, False)

            return t.load_template('pastafari/admin/add_servers.phtml',
                                   group_id=getpostfiles.get['group_id'],
                                   form_server=forms,
                                   url=url + '?op=1&group_id=' +
                                   getpostfiles.get['group_id'])
        else:

            #if insert then send task to servertask

            getpostfiles.obtain_post()

            post = {}

            post['password'] = getpostfiles.post.get('password', '')
            post['delete_root_password'] = getpostfiles.post.get(
                'delete_root_password', '0')
            post['ip'] = getpostfiles.post.get('ip', '')

            try:

                post['group_id'] = getpostfiles.post.get('group_id', '0')

            except:

                post['group_id'] = '0'

            check_form = formsutils.CheckForm()

            (post, pass_form) = check_form.check(
                post, {
                    'password': server.forms['password'],
                    'delete_root_password':
                    server.forms['delete_root_password'],
                    'ip': server.forms['ip'],
                    'group_id': server.forms['group_id']
                })

            server.set_conditions('WHERE ip=%s', [post['ip']])

            c_ip = server.select_count()

            if c_ip == 0:

                if check_form.error == 0:

                    #try connect to the server

                    task_ssh = Task(post['ip'])

                    testing_task = copy.copy(config_task)

                    testing_task.remote_user = '******'
                    testing_task.remote_password = post['password']

                    task_ssh.config = testing_task

                    if task_ssh.prepare_connection():

                        task_ssh.ssh.close()

                        if not server.insert(getpostfiles.post):

                            forms = formsutils.show_form(
                                getpostfiles.post, server.forms, t, True)

                            return t.load_template(
                                'pastafari/admin/add_servers.phtml',
                                group_id=getpostfiles.get['group_id'],
                                form_server=forms,
                                url=url + '?op=1')

                        else:

                            server_id = server.insert_id()

                            # Insert in server group

                            group_server_item.valid_fields = [
                                'group_id', 'server_id'
                            ]

                            if group_server_item.insert({
                                    'group_id':
                                    post['group_id'],
                                    'server_id':
                                    server_id
                            }):

                                task.create_forms()

                                os_server = getpostfiles.post[
                                    'os_codename'].replace('/', '').replace(
                                        '.', '')

                                ip = server.fields['ip'].check(
                                    getpostfiles.post['ip'])

                                files = []

                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_python.sh', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_curl.sh', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_psutil.sh', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/upgrade.sh', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/alive.py', 0o750
                                ])
                                #files.append(['monit/'+os_server+'/files/alive.sh', 0o750];
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/files/get_info.py', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/files/get_updates.py', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/files/crontab/alive', 0o640
                                ])
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/files/sudoers.d/spanel',
                                    0o640
                                ])
                                files.append([config_task.public_key, 0o600])

                                commands_to_execute = []

                                commands_to_execute.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_python.sh', ''
                                ])
                                commands_to_execute.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_curl.sh', ''
                                ])
                                commands_to_execute.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_psutil.sh', ''
                                ])
                                commands_to_execute.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/alive.py',
                                    '--url=' + config_task.url_monit + '/' +
                                    ip + '/' + config_task.api_key +
                                    ' --user='******' --pub_key=' + config_task.public_key
                                ])

                                delete_files = []

                                delete_files.append(
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_python.sh')
                                delete_files.append(
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_curl.sh')
                                delete_files.append(
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_psutil.sh')
                                delete_files.append(config_task.public_key)

                                delete_directories = ['modules/pastafari']

                                if post['delete_root_password'] == '1':
                                    #delete_root_passwd.sh
                                    files.append([
                                        'modules/pastafari/scripts/standard/' +
                                        os_server + '/delete_root_passwd.sh',
                                        0o750
                                    ])
                                    commands_to_execute.append([
                                        'modules/pastafari/scripts/standard/' +
                                        os_server + '/delete_root_passwd.sh',
                                        ''
                                    ])

                                if task.insert({
                                        'name_task':
                                        'monit_server',
                                        'description_task':
                                        I18n.lang(
                                            'pastafari', 'add_monit',
                                            'Adding monitoritation to the server...'
                                        ),
                                        'url_return':
                                        url,
                                        'files':
                                        files,
                                        'commands_to_execute':
                                        commands_to_execute,
                                        'delete_files':
                                        delete_files,
                                        'delete_directories':
                                        delete_directories,
                                        'server':
                                        ip,
                                        'user':
                                        '******',
                                        'password':
                                        post['password'],
                                        'path':
                                        '/root'
                                }):

                                    task_id = task.insert_id()

                                    try:

                                        r = requests.get(server_task +
                                                         str(task_id))

                                        arr_data = r.json()

                                        arr_data['task_id'] = task_id

                                        logtask.create_forms()

                                        if not logtask.insert(arr_data):

                                            return "Error:Wrong format of json data..."

                                            #return t_admin.load_template('pastafari/ajax_progress.phtml', title='Adding monitoritation to the server...') #"Load template with ajax..."

                                    except:

                                        logtask.conditions = [
                                            'WHERE id=%s', [task_id]
                                        ]

                                        task.update({'status': 1, 'error': 1})

                                        server.conditions = [
                                            'WHERE id=%s', [server_id]
                                        ]

                                        group_server_item.conditions = [
                                            'WHERE server_id=%s', [server_id]
                                        ]

                                        group_server_item.delete()

                                        server.delete()

                                        return "Error:cannot connect to task server, check the url for it..."

                                    return t.load_template(
                                        'pastafari/progress.phtml',
                                        name_task=I18n.lang(
                                            'pastafari', 'add_monit',
                                            'Adding monitoritation to the server...'
                                        ),
                                        description_task=I18n.lang(
                                            'pastafari', 'add_monit_explain',
                                            'Installing the basic scripts for send info from server to monit module'
                                        ),
                                        task_id=task_id,
                                        server=ip,
                                        position=0)
                                    #return "Server is building..."
                                    #redirect('servers?op=2&task_id='+str(task_id))

                                else:

                                    server.conditions = [
                                        'WHERE id=%s', [server_id]
                                    ]

                                    server.delete()

                                    group_server_item.conditions = [
                                        'WHERE server_id=%s', [server_id]
                                    ]

                                    group_server_item.delete()

                                    return "Error: cannot create the new task"
                            else:

                                server.conditions = [
                                    'WHERE id=%s', [server_id]
                                ]

                                server.delete()

                                return "Error: you need a initial group for your server"

                    else:
                        task_ssh.ssh.close()
                        return "Cannot connect to the new server " + task_ssh.txt_error

                else:

                    forms = formsutils.show_form(getpostfiles.post,
                                                 server.forms, t, True)

                    return t.load_template(
                        'pastafari/admin/add_servers.phtml',
                        group_id=getpostfiles.get['group_id'],
                        form_server=forms,
                        url=url + '?op=1&group_id=' +
                        getpostfiles.get['group_id'])

            else:

                server.fields['ip'].duplicated_ip = True

                forms = formsutils.show_form(getpostfiles.post, server.forms,
                                             t, True)

                return t.load_template('pastafari/admin/add_servers.phtml',
                                       group_id=getpostfiles.get['group_id'],
                                       form_server=forms,
                                       url=url + '?op=1&group_id=' +
                                       getpostfiles.get['group_id'])

    #elif getpostfiles.get['op']=='2':

    #return ""

    elif getpostfiles.get['op'] == '2':

        getpostfiles = GetPostFiles()

        getpostfiles.obtain_get()

        server_id = int(getpostfiles.get.get('id', '0'))

        arr_server = server.select_a_row(server_id)

        if arr_server:

            return t.load_template('pastafari/admin/graphs.phtml',
                                   server=arr_server,
                                   api_key=config_task.api_key)

        else:

            return ""

    elif getpostfiles.get['op'] == '3':

        t.show_basic_template = False

        getpost = GetPostFiles()

        getpost.obtain_get()

        server_id = int(getpostfiles.get.get('id', '0'))

        arr_server = server.select_a_row(server_id)

        if 'ip' in arr_server:

            ip = arr_server['ip']

            now = datetime.obtain_timestamp(datetime.now())

            hours12 = now - 21600

            date_now = datetime.timestamp_to_datetime(now)

            date_hours12 = datetime.timestamp_to_datetime(hours12)

            status_cpu = servers.StatusCpu(conn)

            status_cpu.set_conditions('where ip=%s and date>=%s and date<=%s',
                                      [ip, date_hours12, date_now])

            #arr_cpu=status_cpu.select_to_array(['idle', 'date'])
            cur = status_cpu.select(['idle', 'date'])

            x = 0

            arr_cpu = []

            cur.fetchone()

            for cpu_info in cur:

                arr_cpu.append(cpu_info['idle'])

            cur.close()

            status_mem = servers.StatusMemory(conn)

            status_mem.set_conditions('where ip=%s and date>=%s and date<=%s',
                                      [ip, date_hours12, date_now])

            #status_mem.set_order(['id', 'ASC'])

            #arr_mem=status_mem.select_to_array(['used', 'free', 'date'])
            arr_mem = []
            with status_mem.select(['used', 'free', 'cached', 'date']) as cur:
                #cur.fetchone()

                for mem_info in cur:
                    mem_info['used'] = (
                        (mem_info['used'] / 1024) / 1024) / 1024
                    mem_info['free'] = (
                        (mem_info['free'] / 1024) / 1024) / 1024
                    mem_info['cached'] = (
                        (mem_info['cached'] / 1024) / 1024) / 1024
                    arr_mem.append(mem_info)

            if len(arr_mem) > 2:
                arr_mem.pop(0)

            #arr_cpu=status_cpu.select_to_array(['idle', 'date'])
            cur = status_cpu.select(['idle', 'date'])

            arr_net = {}

            status_net = servers.StatusNet(conn)

            status_net.set_conditions('where ip=%s and date>=%s and date<=%s',
                                      [ip, date_hours12, date_now])

            arr_net = []

            cur = status_net.select(['bytes_sent', 'bytes_recv', 'date'])

            substract_time = 0  #datetime.obtain_timestamp(datetime.now())

            c_hours12 = now

            c_elements = 0

            if cur.rowcount > 0:

                data_net = cur.fetchone()

                first_recv = data_net['bytes_recv']
                first_sent = data_net['bytes_sent']

                for data_net in cur:

                    timestamp = datetime.obtain_timestamp(
                        data_net['date'], True)

                    diff_time = timestamp - substract_time

                    if substract_time != 0 and diff_time > 300:

                        count_time = timestamp

                        while substract_time <= count_time:

                            form_time = datetime.timestamp_to_datetime(
                                substract_time)

                            arr_net.append(
                                {'date': datetime.format_time(form_time)})

                            substract_time += 60

                    bytes_sent = round(
                        (data_net['bytes_sent'] - first_sent) / 1024)
                    bytes_recv = round(
                        (data_net['bytes_recv'] - first_recv) / 1024)
                    cpu = arr_cpu[x]

                    memory_used = arr_mem[x]['used']
                    memory_free = arr_mem[x]['free']
                    memory_cached = arr_mem[x]['cached']

                    arr_net.append({
                        'bytes_sent':
                        bytes_sent,
                        'bytes_recv':
                        bytes_recv,
                        'date':
                        datetime.format_time(data_net['date']),
                        'cpu':
                        cpu,
                        'memory_used':
                        memory_used,
                        'memory_free':
                        memory_free,
                        'memory_cached':
                        memory_cached
                    })

                    first_sent = data_net['bytes_sent']
                    first_recv = data_net['bytes_recv']

                    c_hours12 = timestamp

                    substract_time = int(timestamp)

                    c_elements += 1

                    x += 1

                # If the last time is more little that now make a loop

                while c_hours12 <= now:

                    form_time = datetime.timestamp_to_datetime(c_hours12)

                    seconds = form_time[-2:]

                    #print(form_time)

                    if seconds == '00':

                        arr_net.append(
                            {'date': datetime.format_time(form_time)})

                        # if secons is 00 and z=1 put value
                        #arr_net.append({'date': datetime.format_time(form_time)})

                        pass

                    c_hours12 += 1

                cur.close()

                if c_elements > 2:

                    return json.dumps(arr_net)
                else:

                    return {}

                return {}

        return {}

    elif getpostfiles.get['op'] == '4':

        t.show_basic_template = False

        getpost = GetPostFiles()

        getpost.obtain_get()

        server_id = int(getpostfiles.get.get('id', '0'))

        arr_server = server.select_a_row(server_id)

        if 'ip' in arr_server:

            ip = arr_server['ip']

            status_disk = servers.StatusDisk(conn)

            status_disk.set_conditions('where ip=%s', [ip])

            arr_disk = status_disk.select_to_array(
                ['disk', 'used', 'free', 'date'])

            return json.dumps(arr_disk)

    elif getpostfiles.get['op'] == '5':

        getpostfiles.get['id'] = getpostfiles.get.get('id', '0')

        getpostfiles.get['delete'] = getpostfiles.get.get('delete', '0')

        try:
            getpostfiles.get['id'] = int(getpostfiles.get['id'])
        except:
            getpostfiles.get['id'] = 0

        if getpostfiles.get['delete'] != '0':

            group_server_item.set_conditions('WHERE server_id=%s',
                                             [getpostfiles.get['id']])

            group_server_item.delete()

            server.set_conditions('WHERE id=%s', [getpostfiles.get['id']])

            server.delete()

            set_flash_message('Deleted the server sucessfully')

            redirect(make_admin_url('pastafari/servers'))

            pass
        else:

            return "It deleted the server in database <strong>only</strong><p>You need delete the server with the tool of your election: <input type=\"button\" value=\"Do you are sure?\" onclick=\"javascript:location.href='" + make_admin_url(
                'pastafari/servers', {
                    'op': '5',
                    'id': str(getpostfiles.get['id']),
                    'delete': '1'
                }) + "';\"></p>"

        pass

    else:
        """
        $actual_timestamp=time();
        
        $past_timestamp=time()-300;

        $actual_time=PhangoApp\PhaTime\DateTime::format_timestamp($actual_timestamp, $localtime=false);

        $past_time=PhangoApp\PhaTime\DateTime::format_timestamp($past_timestamp, $localtime=false);

        //$m->server->set_order(['date' => 1]);

        //$m->server->set_conditions(['where date<?', [$past_time]]);
        
        $admin->where_sql=['where date <?', [$past_time]];
        """

        getpost = GetPostFiles()

        getpost.obtain_get()

        try:

            group_id = int(getpost.get.get('group_id', '0'))

        except:

            group_id = 0

        select_form_group = SelectModelForm('group_id', group_id,
                                            servers.ServerGroup(conn), 'name',
                                            'id', 'parent_id')

        select_form_group.name_field_id = 'change_group_id_form'

        servers_list = SimpleList(server, url, t)

        yes_form = 0

        type_op = ''

        #servers_list.arr_extra_fields=[I18n.lang('common', 'options', 'Options')]

        servers_list.arr_extra_options = [server_options]

        select_task = None

        if 'type' in getpost.get:

            if getpost.get['type'] == 'down':

                actual_timestamp = datetime.obtain_timestamp(datetime.now())

                past_timestamp = actual_timestamp - 300

                actual_time = datetime.timestamp_to_datetime(actual_timestamp)

                past_time = datetime.timestamp_to_datetime(past_timestamp)

                servers_list.model.set_conditions('WHERE date<%s', [past_time])

            elif getpost.get['type'] == 'heavy':
                servers_list.model.set_conditions("where actual_idle>%s", [80])

            elif getpost.get['type'] == 'disks':
                servers_list.model.set_conditions(
                    "where ip IN (select ip from statusdisk where percent>90)",
                    [])

            elif getpost.get['type'] == 'update_servers':

                servers_list.model.set_conditions("where num_updates>0", [])

                servers_list.arr_extra_fields = [
                    I18n.lang('common', 'update_server', 'Update server')
                ]

                servers_list.arr_extra_options = [server_update_options]

                servers_list.yes_search = False

                yes_form = 1

            elif getpost.get['type'] == 'task_servers':

                servers_list.arr_extra_fields = [
                    I18n.lang('pastafari', 'choose_server', 'Choose server')
                ]

                servers_list.arr_extra_options = [server_update_options]

                servers_list.yes_search = False

                yes_form = 2

                # Get tasks and put in select_task

                # Folders are tasks/ and modules/pastafari/tasks

                base_path = 'modules/pastafari/tasks'

                config_parser = configparser.ConfigParser()

                select_task = scandir(base_path, config_parser, OrderedDict(),
                                      'tasks')

                #OrderedDict([('tasks', {'servers': [['Servers', 'tasks', 0]]}), ('servers', {'databases': [['Database servers', 'servers', 0]], 'mail': [['Mail', 'servers', 0], ['Standalone postfix server,  Install on your servers a simple and secure postfix mail server', 'mail', 'modules/pastafari/tasks/servers/mail/postfix/standalone_postfix.py', 1]]})])

            type_op = getpost.get['type']

        if group_id > 0:
            servers_list.model.conditions[
                0] += ' AND id IN (select server_id from servergroupitem where group_id=%s)'
            servers_list.model.conditions[1].append(group_id)

        servers_list.fields_showed = ['hostname', 'ip', 'num_updates', 'date']

        servers_list.limit_pages = 100

        show_servers = servers_list.show()

        return t.load_template('pastafari/admin/servers.phtml',
                               show_servers=show_servers,
                               type_op=type_op,
                               yes_form=yes_form,
                               csrf_token=csrf_token(),
                               select_form_group=select_form_group,
                               group_id=group_id,
                               select_task=select_task)

    return ""
Ejemplo n.º 19
0
#!/usr/bin/env python3

from paramecio.citoplasma.i18n import I18n

modules_admin=[[I18n.lang('admin', 'users_admin', 'User\'s Admin'), 'paramecio.modules.admin.admin.ausers', 'ausers']]

modules_other=[I18n.lang('pastafari', 'pastafari', 'Pastafari'), [["Dashboard", 'modules.pastafari.dashboard', '/pastafari'], [I18n.lang('pastafari', 'os', 'Operating systems'), 'modules.pastafari.os', '/pastafari/os'], [I18n.lang('pastafari', 'servers_groups', 'Server Groups'), 'modules.pastafari.groups', '/pastafari/groups'], [I18n.lang('pastafari', 'servers', 'Servers'), 'modules.pastafari.servers', '/pastafari/servers']], 'pastafari']

modules_admin.append(modules_other)
Ejemplo n.º 20
0
    def upload_files(self):

        try:

            sftp = self.ssh.open_sftp()

        except:

            self.txt_error = 'Sorry, error connecting to sftp: ' + traceback.format_exc(
            )
            return False

        c = len(self.files)

        if c > 0:

            percent = 100 / c

            progress = 0

            for f in self.files:

                source_file = f[0]

                source_file = source_file.replace('${os_server}',
                                                  self.os_server)

                permissions = f[1]
                dest_file = self.config.remote_path + '/' + source_file

                try:

                    if not os.path.isfile(source_file):
                        self.txt_error = "Sorry, you don't have source file to upload " + source_file
                        return False

                    dir_file = os.path.dirname(source_file)

                    parts_dir_file = dir_file.split('/')

                    # Create remote directory

                    try:

                        f_stat = sftp.stat(dir_file)

                    except IOError:

                        try:

                            final_path = ''

                            for d in parts_dir_file:

                                final_path += d + '/'

                                #print(self.config.remote_path+'/'+final_path)

                                try:

                                    f_stat = sftp.stat(final_path)

                                except IOError:

                                    sftp.mkdir(final_path)

                        except:

                            self.txt_error = 'Sorry, error creating the directories for the files: ' + traceback.format_exc(
                            )
                            return False

                    # Upload file

                    try:

                        sftp.put(source_file,
                                 dest_file,
                                 callback=None,
                                 confirm=True)

                        sftp.chmod(dest_file, permissions)

                        progress += percent

                        self.logtask.insert({
                            'task_id':
                            self.id,
                            'progress':
                            progress,
                            'message':
                            I18n.lang('pastafari', 'uploading_files',
                                      'Uploading file: ') + source_file,
                            'error':
                            0,
                            'server':
                            self.server
                        })

                    except:

                        self.txt_error = 'Sorry, cannot upload file ' + source_file + ': ' + traceback.format_exc(
                        )
                        return False

                    # Create directory recursively if not exists

                except:
                    self.txt_error = 'Error: ' + traceback.format_exc()
                    return False

            self.logtask.insert({
                'task_id':
                self.id,
                'progress':
                100,
                'message':
                I18n.lang('pastafari', 'upload_successful',
                          'Files uploaded successfully...'),
                'error':
                0,
                'server':
                self.server
            })

        return True
Ejemplo n.º 21
0
    def exec(self):
        
        # Get task
        
        #self.id=task_id
        
        self.task.reset_require()
        
        self.task.valid_fields=['name_task', 'description_task', 'error', 'status', 'server']
        
        self.logtask.valid_fields=self.logtask.fields.keys()
        
        if self.id==0:
            
            # Insert the task
            
            self.task.reset_require()
            
            self.task.insert({'name_task': self.name_task, 'description_task': self.description_task, 'server': self.server})
            
            self.id=self.task.insert_id()
        
        if not self.prepare_connection():
            #self.task.conditions=['WHERE id=%s', [self.id]]
            #self.task.update({'error': 1, 'status': 1})
            
            self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': self.txt_error, 'error': 1, 'status': 1, 'server': self.server})
            
            self.make_error_task()
            
            return False
        
        # Pre task
        
        if self.pre_task!=None:
            self.logtask.insert({'task_id': self.id, 'progress': 0, 'message': I18n.lang('pastafari', 'pre_tasks', 'Pre tasks executing...'), 'error': 0, 'status': 1, 'server': self.server})
                
            if self.pre_task(self):
                self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': I18n.lang('pastafari', 'pre_tasks_executed', 'Pre tasks executed successfully...'), 'error': 0, 'status': 1, 'server': self.server})
            else:
                self.logtask.set_conditions('where id=%s', [last_log_id])
                    
                self.logtask.update({'progress': 100, 'error': 1, 'message': "Error executing post task", 'status': 1, 'server': self.server})
                
                self.make_error_task()
                
                return False
        
        #Check if script was executed
        
        if self.codename!='':
        
            if self.one_time==True:
            
                with self.ssh.open_sftp() as sftp:
                    
                    try:
                    
                        with sftp.file(self.config.remote_path+'/tasks/'+self.codename) as f:
                            version=f.read()
                            version=version.decode('utf-8').strip()
                            
                            if version==self.version:
                                #self.task.conditions=['WHERE id=%s', [self.id]]
                                #self.task.update({'error': 0, 'status': 1})
                                
                                self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': 'This script was executed correctly in this server', 'error': 0, 'status': 1, 'server': self.server})
                                
                                return True
                                
                    except IOError:
                        # It was not executed
                        pass
        
        if not self.upload_files():
            #self.task.conditions=['WHERE id=%s', [self.id]]
            #self.task.update({'error': 1, 'status': 1})
            
            self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': self.txt_error, 'error': 1, 'status': 1, 'server': self.server})
            
            self.make_error_task()
            
            return False

        self.logtask.insert({'task_id': self.id, 'progress': 0, 'message': 'Executing commands...', 'error': 0, 'status': 0, 'server': self.server})

        # Execute commands
        
        json_code=[]
        
        for c in self.commands_to_execute:
        
            try:
                command=c[0]
                
                command=command.replace('${os_server}', self.os_server)
                
                arguments=c[1]
                
                sudo_str=''
                
                if len(c)==3:
                    sudo_str='sudo '
                
                #, get_pty=True
                stdin, stdout, stderr = self.ssh.exec_command(sudo_str+self.config.remote_path+'/'+command+' '+arguments)
                
                for line in stdout:
                    
                    if line==None:
                        
                        line="[]"
                    
                    line=line.strip()

                    try:
                        json_code=json.loads(line)
                        
                        if not 'progress' in json_code or not 'message' in json_code or not 'error' in json_code:
                            #self.task.conditions=['WHERE id=%s', [self.id]]
                            #self.task.update({'error': 1, 'status': 1})
                            
                            self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': 'Malformed json code: '+str(line), 'error': 1, 'server': self.server})
                            self.make_error_task()
                            return False
                            
                        else:
                            
                            json_code['task_id']=self.id
                            
                            json_code['server']=self.server
                    
                            self.logtask.insert(json_code)
                            
                            if json_code['error']==1:
                                self.make_error_task()
                                return False
                                                
                    
                    
                    except:
                        
                        #self.task.conditions=['WHERE id=%s', [self.id]]
                        #self.task.update({'error': 0, 'status':0})
                        
                        self.logtask.insert({'task_id': self.id, 'progress': 0, 'no_progress': 1, 'message': str(line), 'error': 0, 'status': 0, 'server': self.server})
                        
                        #return False
                
                last_log_id=self.logtask.insert_id()
                
                if stdout.channel.recv_exit_status()>0:
                    #line=stdout.readlines()
                    #logging.warning(action.codename+" WARNING: "+line)
                    final_text='Error executing the command: %s' % command
                    
                    #self.task.conditions=['WHERE id=%s', [self.id]]
                    #self.task.update({'error': 1, 'status': 1})
                    
                    for line in stdout:
                        final_text+=' '+line
                    
                    for line in stderr:
                        final_text+=' '+line
                    
                    self.logtask.set_conditions('where id=%s', [last_log_id])
                    
                    self.logtask.update({'progress': 100, 'error': 1, 'message': final_text, 'status': 1, 'server': self.server})
                    self.make_error_task()
                    return False
                
            except:
                
                #self.task.conditions=['WHERE id=%s', [self.id]]
                #self.task.update({'error': 1, 'status': 1})
                
                self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': traceback.format_exc(), 'error': 1, 'status': 1, 'server': self.server})
                self.make_error_task()
                return False
        
        # Clean files
        
        if not self.delete_files_and_dirs():
            
            #self.task.conditions=['WHERE id=%s', [self.id]]
            #self.task.update({'error': 1, 'status': 1})
                
            self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': self.txt_error, 'error': 1, 'status': 1, 'server': self.server})
            self.make_error_task()
            return False
        
        #Upload files

        #self.ssh.close()

        # FInish task
        
        #Put this version how executed
        
        if self.codename!='':
        
            if self.one_time==True:
                
                with self.ssh.open_sftp() as sftp:
                
                    try:
                        path_check=self.config.remote_path+'/tasks/'
                                            
                        f_stat=sftp.stat(path_check)
                    
                    except IOError:
                    
                        sftp.mkdir(path_check)
                
                    with sftp.file(path_check+self.codename, 'w') as f:
                        f.write(self.version)
                        
        if self.post_task!=None:
            self.logtask.insert({'task_id': self.id, 'progress': 0, 'message': I18n.lang('pastafari', 'post_tasks', 'Post tasks executing...'), 'error': 0, 'status': 1, 'server': self.server})
            
            if self.post_task(self):
                self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': I18n.lang('pastafari', 'post_tasks_executed', 'Post tasks executed successfully...'), 'error': 0, 'status': 1, 'server': self.server})
            else:
                #self.logtask.set_conditions('where id=%s', [last_log_id])
                    
                #self.logtask.update({'progress': 100, 'error': 1, 'message': "Error executing post task", 'status': 1, 'server': self.server})
                
                self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': I18n.lang('pastafari', 'error_post_tasks_executed', 'Error executing post task -> '+self.error_post_task), 'error': 1, 'status': 1, 'server': self.server})
                self.make_error_task()
                return False
                

        if 'progress' in json_code:
            if json_code['progress']!=100:
                self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': I18n.lang('pastafari', 'finished_successfully', 'All tasks done successfully...'), 'error': 0, 'status': 1, 'server': self.server})
        else:
            self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': I18n.lang('pastafari', 'finished_successfully', 'All tasks done successfully...'), 'error': 0, 'status': 1, 'server': self.server})
        
        # Add 
        self.taskdone.create_forms()
        
        self.taskdone.insert({'name_task': self.codename, 'ip': self.server})
        
        #self.task.conditions=['WHERE id=%s', [self.id]]
        #self.task.update({'error': 0, 'status': 1})
        
        #connection.close()
        return True
Ejemplo n.º 22
0
def home():

    connection = WebModel.connection()
    #Fix, make local variable

    s = get_session()

    if check_login():

        getpostfiles = GetPostFiles()

        getpostfiles.obtain_get()

        parent_id = getpostfiles.get.get('parent_id', '0')

        parent_id = int(parent_id)

        #Load menu

        menu = get_menu(config_admin.modules_admin)

        lang_selected = get_language(s)

        groups = servers.ServerGroup(connection)

        groups.create_forms()

        groups.forms['parent_id'].default_value = parent_id

        hierarchy = HierarchyModelLinks(groups, 'All groups', 'name',
                                        'parent_id',
                                        make_url('pastafari/groups'))

        hierarchy.prepare()

        group_list = GenerateAdminClass(
            groups,
            make_url(pastafari_folder + '/groups',
                     {'parent_id': str(parent_id)}), t)

        groups.set_conditions('WHERE parent_id=%s', [parent_id])

        group_list.list.fields_showed = ['name']

        group_list.list.arr_extra_options = [task_options]

        group_list.list.s['order'] = '0'
        group_list.list.s['order_field'] = 'name'

        group_list.list.yes_search = False

        content_index = t.load_template('pastafari/groups.phtml',
                                        group_list=group_list,
                                        hierarchy_links=hierarchy,
                                        son_id=parent_id)
        #group_list.show()

        return t.load_template('admin/content.html',
                               title=I18n.lang('pastafari', 'servers_groups',
                                               'Server\'s Group'),
                               content_index=content_index,
                               menu=menu,
                               lang_selected=lang_selected,
                               arr_i18n=I18n.dict_i18n)

    else:
        redirect(make_url(config.admin_folder))
Ejemplo n.º 23
0
    def delete_files_and_dirs(self):
        
        sftp=self.ssh.open_sftp()
        
        c=len(self.delete_files)
        
        if c>0:
        
            percent=100/c
            
            progress=0
            
            for filepath in self.delete_files:
                
                filepath=filepath.replace('${os_server}', self.os_server)
                
                try:
                    sftp.remove(self.config.remote_path+'/'+filepath)
                    
                    progress+=percent
                    
                    self.logtask.insert({'task_id': self.id, 'progress': progress, 'message': I18n.lang('pastafari', 'cleaning_files', 'Cleaning file: ')+filepath, 'error': 0, 'server': self.server})
                    
                except IOError:
                    
                    self.txt_error="Sorry, cannot remove file "+filepath+" from server."
                    
                    return False


        c=len(self.delete_directories)
        
        if c>0:
        
            percent=100/c
            
            progress=0

            for path in self.delete_directories:

                path=path.replace('${os_server}', self.os_server)

                if self.delete_dir(path, sftp):
                
                    progress+=percent
                    
                    self.logtask.insert({'task_id': self.id, 'progress': progress, 'message': I18n.lang('pastafari', 'cleaning_directories', 'Cleaning directory: ')+path, 'error': 0, 'server': self.server})

                else:
                    self.txt_error+="Sorry, cannot remove directory "+path+" from server."
                    
                    return False

        return True
Ejemplo n.º 24
0
    def upload_files(self):
        
        try:
    
            sftp=self.ssh.open_sftp()
        
        except:

            self.txt_error='Sorry, error connecting to sftp: '+traceback.format_exc()
            return False
        
        c=len(self.files)
        
        if c>0:
        
            percent=100/c
            
            progress=0
            
            for f in self.files:
                
                source_file=f[0]
                
                source_file=source_file.replace('${os_server}', self.os_server)
                
                permissions=f[1]
                dest_file=self.config.remote_path+'/'+source_file
                
                try:
                    
                    if not os.path.isfile(source_file):
                        self.txt_error="Sorry, you don't have source file to upload "+source_file
                        return False
                    
                    dir_file=os.path.dirname(source_file)
                    
                    parts_dir_file=dir_file.split('/')
                        
                    # Create remote directory
                    
                    try:
                        
                        f_stat=sftp.stat(dir_file)

                    except IOError:

                        try:
                            
                            final_path=''
                            
                            for d in parts_dir_file:
                                
                                final_path+=d+'/'
                                
                                #print(self.config.remote_path+'/'+final_path)
                                
                                try:
                                    
                                    f_stat=sftp.stat(final_path)
                                
                                except IOError:
                                
                                    sftp.mkdir(final_path)
                            
                        except:
                            
                            self.txt_error='Sorry, error creating the directories for the files: '+traceback.format_exc()
                            return False
                    
                    # Upload file
                    
                    try:
                    
                        sftp.put(source_file, dest_file, callback=None, confirm=True)
                        
                        sftp.chmod(dest_file, permissions)
                        
                        progress+=percent
                        
                        self.logtask.insert({'task_id': self.id, 'progress': progress, 'message': I18n.lang('pastafari', 'uploading_files', 'Uploading file: ')+source_file, 'error': 0, 'server': self.server})
                        
                    except:
                        
                        self.txt_error='Sorry, cannot upload file '+source_file+': '+traceback.format_exc()
                        return False
                    
                    # Create directory recursively if not exists
                
                except:
                    self.txt_error='Error: '+traceback.format_exc()
                    return False
         
            self.logtask.insert({'task_id': self.id, 'progress': 100, 'message': I18n.lang('pastafari', 'upload_successful', 'Files uploaded successfully...'), 'error': 0, 'server': self.server})
        
        return True
Ejemplo n.º 25
0
def server_options(url, id, arr_row):
        options=[]
        options.append('<a href="'+add_get_parameters(url, op='view_status', id=id)+'">'+I18n.lang('pastafari', 'view_status', 'View status')+'</a>')
        return options
Ejemplo n.º 26
0
#!/usr/bin/env python3

from paramecio.citoplasma.i18n import I18n

modules_admin = [[
    I18n.lang('admin', 'users_admin', 'User\'s Admin'),
    'paramecio.modules.admin.admin.ausers', 'ausers'
]]

modules_other = [
    I18n.lang('pastafari', 'pastafari', 'Pastafari'),
    [["Dashboard", 'modules.pastafari.dashboard', '/pastafari'],
     [
         I18n.lang('pastafari', 'os', 'Operating systems'),
         'modules.pastafari.os', '/pastafari/os'
     ],
     [
         I18n.lang('pastafari', 'servers_groups', 'Server Groups'),
         'modules.pastafari.groups', '/pastafari/groups'
     ],
     [
         I18n.lang('pastafari', 'servers', 'Servers'),
         'modules.pastafari.servers', '/pastafari/servers'
     ]], 'pastafari'
]

modules_admin.append(modules_other)
Ejemplo n.º 27
0
 def __init__(self, model, url, t):
     
     self.raw_query=True
     
     self.getpostfiles=GetPostFiles()
         
     self.getpostfiles.obtain_get()
     
     self.t=t
     
     self.model=model
     
     #if len(self.model.forms)==0:
     
         #self.model.create_forms()
     
     self.fields=model.fields.keys()
     
     self.fields_showed=self.fields
     
     self.url=url
     
     self.limit_pages=20
     
     self.order_defaults=['ASC', 'DESC']
     
     self.order_class=['down', 'up']
     
     self.s=get_session()
     
     #clean session
     
     self.s['order']='0'
     
     self.s['order_field']=self.model.name_field_id
     
     self.order_by=self.order_defaults[0]
     
     self.change_order={}
     
     self.yes_search=True
     
     self.search_text=''
     
     self.initial_num_pages=20
     
     request.query.get('begin_page', '0')
     
     try: 
     
         self.begin_page=int(request.query.begin_page)
         
     except ValueError:
         self.begin_page=0
     
     if self.begin_page<0:
         self.begin_page=0
     
     self.search_fields=self.fields
     
     #self.yes_options=True
     
     self.arr_extra_fields=[I18n.lang('common', 'options', 'Options')]
     
     self.arr_extra_options=[SimpleList.standard_options]
     
     self.jln='<br />'
Ejemplo n.º 28
0
#!/usr/bin/python3

from paramecio.citoplasma.i18n import I18n

modules_admin = [[I18n.lang("admin", "users_admin", "User's Admin"), "paramecio.modules.admin.admin.ausers", "ausers"]]

modules_other = [
    I18n.lang("pastafari", "pastafari", "Pastafari"),
    [[I18n.lang("pastafari", "servers", "Servers"), "modules.pastafari.admin.servers", "pastafari/servers"]],
    "pastafari",
]

modules_admin.append(modules_other)

# modules_admin={'ausers': [I18n.lang('admin', 'users_admin', 'User\'s Admin'), 'paramecio.modules.admin.admin.ausers'], 'module_father': [ I18n.lang('common', 'submodule', 'ModuleFather') , {'submodule': ['This is a module', 'paramecio.modules.admin.admin.ausers'] } ] }
Ejemplo n.º 29
0
    def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
        
        error=0
        
        try:
            
            fields, values, update_values=super().check_all_fields(dict_values, external_agent, yes_update, errors_set)
        except: 
            
            error+=1
        
        if self.check_user==True:
        
            # Check if passwords matches
            
            if self.password_field in dict_values:
                
                dict_values['repeat_password']=dict_values.get('repeat_password', '')
                
                if dict_values[self.password_field].strip()!="":
                
                    if dict_values['repeat_password']!=dict_values[self.password_field]:
                        
                        self.fields[self.password_field].error=True
                        self.fields[self.password_field].txt_error=I18n.lang('common', 'error_passwords_no_match', 'Error: passwords doesn\'t match')
                    
                        error+=1

            # Check if exists user with same email or password
            
            get_id=0
            
            if self.updated:
                # Need the id
                #GetPostFiles.obtain_get()
                #GetPostFiles.obtain_post()
                
                getpostfiles=GetPostFiles()
                
                getpostfiles.obtain_get()
                
                get_id=getpostfiles.get.get(self.name_field_id, '0')
                
                post_id=getpostfiles.post.get(self.name_field_id, '0')
                
                if get_id!='0':
                    get_id=int(get_id)
                
                if post_id!='0':
                    get_id=int(post_id)
                
                pass
            
            get_id=int(get_id)
            
            sql_id=''
            
            original_conditions=self.conditions
            
            self.reset_conditions()
            
            if self.username_field in dict_values:
            
                self.conditions=['WHERE (username=%s', [dict_values[self.username_field]]]

            
            if self.email_field in dict_values:
            
                if len(self.conditions[1])>0:
            
                    self.conditions[0]+=' OR email=%s)'
                else:
                    self.conditions[0]='WHERE (email=%s)'
                    self.conditions[1]=[]
            
                self.conditions[1].append([dict_values[self.email_field]])
            
            if get_id>0:
                
                self.conditions[0]+=' AND '+self.name_field_id+'!=%s'
                self.conditions[1].append(get_id)
            
            
            if self.select_count()>0:
                
                self.fields[self.username_field].error=True
                self.fields[self.username_field].txt_error=I18n.lang('common', 'error_username_or_password_exists', 'Error: username or email exists in database')
                
                error+=1
            
            self.conditions=original_conditions

        if error>0:
            self.query_error+='Error:if is not expected, please, check that you disabled the special checkings of this model'
            return False

        return fields, values, update_values
Ejemplo n.º 30
0
    def delete_files_and_dirs(self):

        sftp = self.ssh.open_sftp()

        c = len(self.delete_files)

        if c > 0:

            percent = 100 / c

            progress = 0

            for filepath in self.delete_files:

                filepath = filepath.replace('${os_server}', self.os_server)

                try:
                    sftp.remove(self.config.remote_path + '/' + filepath)

                    progress += percent

                    self.logtask.insert({
                        'task_id':
                        self.id,
                        'progress':
                        progress,
                        'message':
                        I18n.lang('pastafari', 'cleaning_files',
                                  'Cleaning file: ') + filepath,
                        'error':
                        0,
                        'server':
                        self.server
                    })

                except IOError:

                    self.txt_error = "Sorry, cannot remove file " + filepath + " from server."

                    return False

        c = len(self.delete_directories)

        if c > 0:

            percent = 100 / c

            progress = 0

            for path in self.delete_directories:

                path = path.replace('${os_server}', self.os_server)

                if self.delete_dir(path, sftp):

                    progress += percent

                    self.logtask.insert({
                        'task_id':
                        self.id,
                        'progress':
                        progress,
                        'message':
                        I18n.lang('pastafari', 'cleaning_directories',
                                  'Cleaning directory: ') + path,
                        'error':
                        0,
                        'server':
                        self.server
                    })

                else:
                    self.txt_error += "Sorry, cannot remove directory " + path + " from server."

                    return False

        return True
Ejemplo n.º 31
0
def tasklist(server_id):

    content_index = ''

    if check_login():

        conn = WebModel.connection()

        server = Server(conn)

        arr_server = server.select_a_row(server_id)

        if arr_server:

            s = get_session()

            menu = get_menu(config_admin.modules_admin)

            lang_selected = get_language(s)

            task = Task(conn)

            arr_task_id = []

            with task.query(
                    'select DISTINCT task_id from logtask where server=%s',
                [arr_server['ip']]) as cursor:
                for arr_task in cursor:
                    arr_task_id.append(arr_task['task_id'])

            task.set_conditions(
                'WHERE id IN ' + task.check_in_list_str('id', arr_task_id), [])

            task_list = SimpleList(
                task,
                make_url(pastafari_folder + '/tasklist/' + str(server_id), {}),
                t)

            task_list.fields_showed = [
                'id', 'name_task', 'description_task', 'error', 'status'
            ]

            task_list.yes_search = False

            task_list.order_field = 'id'
            task_list.order = 1

            request.environ['server_id'] = arr_server['id']

            task_list.arr_extra_options = [server_task_options]

            return_url = make_url('pastafari/servers')

            content_index = t.load_template('pastafari/admin/showtasks.phtml',
                                            task_list=task_list,
                                            server=arr_server,
                                            return_url=return_url)

        else:
            cont_index = I18n.lang('pastafari',
                                   'sorry_i_cannot_find_the_server',
                                   'Sorry, i cannot find the server')

        conn.close()

        return t.load_template(
            'admin/content.html',
            title=I18n.lang('pastafari', 'task_list_of', 'Task list of') +
            ' %s' % arr_server['hostname'],
            content_index=content_index,
            menu=menu,
            lang_selected=lang_selected,
            arr_i18n=I18n.dict_i18n)

    return ""
Ejemplo n.º 32
0
timestamp_now = datetime.obtain_timestamp(now)

five_minutes = int(timestamp_now) - 300

five_minutes_date = datetime.timestamp_to_datetime(five_minutes)

server.set_conditions('WHERE date<%s', [five_minutes_date])

arr_server = []

with server.select(['hostname']) as cur:
    for s in cur:
        arr_server.append(s['hostname'])

if len(arr_server) > 0:

    send_mail = SendMail()

    content_mail = "THE NEXT SERVERS ARE DOWN: " + ", ".join(
        arr_server) + "\n\n"

    content_mail = 'Please, click in this link for view the servers down; ' + config.domain_url + make_url(
        'pastafari/servers', {'type': 'down'})

    if not send_mail.send(
            email_address, config.email_notification,
            I18n.lang('pastafari', 'servers_down',
                      'WARNING:  SERVERS ARE DOWN!'), content_mail):
        print('Sended email with notification')
Ejemplo n.º 33
0
def admin(**args):
    
    t=args['t']
    
    conn=args['connection']
    
    server=servers.Server(conn)
    
    os_model=servers.OsServer(conn)
    
    task=tasks.Task(conn)
    
    logtask=tasks.LogTask(conn)
    
    group_server=servers.ServerGroup(conn)
    
    group_server_item=servers.ServerGroupItem(conn)
    
    getpostfiles=GetPostFiles()
    
    getpostfiles.obtain_get()
    
    getpostfiles.get['op']=getpostfiles.get.get('op', '')
    
    getpostfiles.get['group_id']=getpostfiles.get.get('group_id', '0')

    request_type=formsutils.request_type()
    
    server.fields['os_codename'].name_form=SelectForm

    server.create_forms(['hostname', 'ip', 'os_codename'])
    
    arr_os={}
    
    with os_model.select() as cur:
        arr_os = { v['codename'] : v['name'] for v in cur }
    
    server.fields['date'].label=I18n.lang('pastafari', 'server_status', 'Server status')
    
    server.forms['os_codename'].arr_select=arr_os
    
    server.forms['password']=PasswordForm('password', '')
    
    server.forms['password'].required=True
    
    server.forms['password'].label=I18n.lang('pastafari', 'password', 'Password')
    
    server.forms['delete_root_password']=SelectForm('delete_root_password', '1', {'0': 'No', '1': 'Yes'})
    
    server.forms['delete_root_password'].label=I18n.lang('pastafari', 'delete_root_password', 'Disable root password')
    
    server.forms['clean_gcc']=SelectForm('clean_gcc', '1', {'0': 'No', '1': 'Yes'})
    
    server.forms['clean_gcc'].label=I18n.lang('pastafari', 'clean_gcc', 'Clean build dependencies for soft monitoring install?,if you want gcc in your server, answer NO')
    
    server.forms['group_id']=SelectModelForm('group_id', getpostfiles.get['group_id'], group_server, 'name', 'id', 'parent_id')
    
    server.forms['group_id'].field=group_server_item.fields['server_id']
    
    server.forms['group_id'].required=True
    
    server.forms['group_id'].label=label=I18n.lang('pastafari', 'principal_group', 'Principal group')

    if getpostfiles.get['op']=='1':
        
        if request_type!="POST":
            
            forms=formsutils.show_form({}, server.forms, t, False, False)
            
            return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url+'?op=1&group_id='+getpostfiles.get['group_id'])
        else:
            
            #if insert then send task to servertask
            
            getpostfiles.obtain_post()
            
            post={}
            
            post['password']=getpostfiles.post.get('password', '')
            post['delete_root_password']=getpostfiles.post.get('delete_root_password', '0')
            post['ip']=getpostfiles.post.get('ip', '')
            post['clean_gcc']=getpostfiles.post.get('clean_gcc', '1')
            
            try:
            
                post['group_id']=getpostfiles.post.get('group_id', '0')
                
            except:
                
                post['group_id']='0'
            
            check_form=formsutils.CheckForm()
            
            (post, pass_form)=check_form.check(post, {'password': server.forms['password'], 'delete_root_password': server.forms['delete_root_password'], 'ip': server.forms['ip'], 'group_id': server.forms['group_id']})
            
            server.set_conditions('WHERE ip=%s', [post['ip']])
            
            c_ip=server.select_count()
            
            if c_ip==0:
            
                if check_form.error==0:
                    
                    #try connect to the server
                    
                    task_ssh=Task(post['ip'])
                    
                    testing_task=copy.copy(config_task)
                    
                    testing_task.remote_user='******'
                    testing_task.remote_password=post['password']
                    
                    task_ssh.config=testing_task
                    
                    if task_ssh.prepare_connection():
                    
                        task_ssh.ssh.close()
                
                        if not server.insert(getpostfiles.post):
                            
                            forms=formsutils.show_form(getpostfiles.post, server.forms, t, True)
                            
                            return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url+'?op=1')
                
                        else:
                            
                            server_id=server.insert_id()
                            
                            # Insert in server group
                            
                            group_server_item.valid_fields=['group_id', 'server_id']
                            
                            if group_server_item.insert({'group_id': post['group_id'], 'server_id': server_id}):
                            
                                task.create_forms()
                                
                                os_server=getpostfiles.post['os_codename'].replace('/', '').replace('.', '')
                                
                                ip=server.fields['ip'].check(getpostfiles.post['ip'])
                                
                                files=[]
                                
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/install_python.sh', 0o750])
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/install_curl.sh', 0o750])
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/install_psutil.sh', 0o750])
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/upgrade.sh', 0o750])
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/clean_gcc.sh', 0o750])
                                
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/alive.py', 0o750])
                                #files.append(['monit/'+os_server+'/files/alive.sh', 0o750];
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/files/get_info.py', 0o750])
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/files/get_updates.py', 0o750])
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/files/crontab/alive', 0o640])
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/files/sudoers.d/spanel', 0o640])
                                files.append([config_task.public_key, 0o600])

                                commands_to_execute=[]
                                
                                commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/install_curl.sh', ''])
                                commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/install_python.sh', ''])
                                commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/install_psutil.sh', ''])
                                commands_to_execute.append(['modules/pastafari/scripts/monit/'+os_server+'/alive.py', '--url='+config_task.url_monit+'/'+ip+'/'+config_task.api_key+' --user='******' --pub_key='+config_task.public_key])
                                
                                delete_files=[]
                                
                                delete_files.append('modules/pastafari/scripts/standard/'+os_server+'/install_python.sh')
                                delete_files.append('modules/pastafari/scripts/standard/'+os_server+'/install_curl.sh')
                                delete_files.append('modules/pastafari/scripts/standard/'+os_server+'/install_psutil.sh')
                                delete_files.append(config_task.public_key)
                                
                                delete_directories=['modules/pastafari']
                                delete_directories=[os.path.dirname(config_task.public_key)]
                                
                                if post['delete_root_password']=='1':
                                    #delete_root_passwd.sh
                                    files.append(['modules/pastafari/scripts/standard/'+os_server+'/delete_root_passwd.sh', 0o750])
                                    commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/delete_root_passwd.sh', ''])
                                    
                                if post['clean_gcc']=='1':
                                    #delete_root_passwd.sh
                                    files.append(['modules/pastafari/scripts/standard/'+os_server+'/clean_gcc.sh', 0o750])
                                    commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/clean_gcc.sh', ''])
                                    
                                #'modules.pastafari.tasks.system.install.task_functions'
                                if task.insert({'name_task': 'monit_server','description_task': I18n.lang('pastafari', 'add_monit', 'Adding monitoritation to the server...'), 'url_return': url, 'files': files, 'commands_to_execute': commands_to_execute, 'delete_files': delete_files, 'delete_directories': delete_directories, 'server': ip, 'user': '******', 'password': post['password'], 'path': '/root', 'error_func': '', 'extra_data': {'server_id': server_id}}):
                                                    
                                    task_id=task.insert_id()
                                                    
                                    try:
                                    
                                        r=requests.get(server_task+str(task_id))
                                        
                                        arr_data=r.json()
                                        
                                        arr_data['task_id']=task_id
                                        
                                        logtask.create_forms()
                                        
                                        if not logtask.insert(arr_data):
                                            
                                            return "Error:Wrong format of json data..."
                                            
                                            #return t_admin.load_template('pastafari/ajax_progress.phtml', title='Adding monitoritation to the server...') #"Load template with ajax..."
                                    
                                    except:
                                        
                                        logtask.conditions=['WHERE id=%s', [task_id]]
                                        
                                        task.update({'status': 1, 'error': 1})
                                        
                                        server.conditions=['WHERE id=%s', [server_id]]
                                        
                                        group_server_item.conditions=['WHERE server_id=%s', [server_id]]
                                    
                                        group_server_item.delete()
                                    
                                        server.delete()
                                        
                                        return "Error:cannot connect to task server, check the url for it..."
                                    
                                    #return t.load_template('pastafari/progress.phtml', name_task=I18n.lang('pastafari', 'add_monit', 'Adding monitoritation to the server...'), description_task=I18n.lang('pastafari', 'add_monit_explain', 'Installing the basic scripts for send info from server to monit module'), task_id=task_id, server=ip, position=0)
                                    #return "Server is building..."
                                    #redirect('servers?op=2&task_id='+str(task_id))
                                    #@get('/'+pastafari_folder+'/showprogress/<task_id:int>/<server>')
                                    redirect(make_url(pastafari_folder+'/showprogress/'+str(task_id)+'/'+ip))
                    
                                else:
                                    
                                    server.conditions=['WHERE id=%s', [server_id]]
                                    
                                    server.delete()
                                    
                                    group_server_item.conditions=['WHERE server_id=%s', [server_id]]
                                    
                                    group_server_item.delete()
                                    
                                    return "Error: cannot create the new task"
                            else:
                                
                                server.conditions=['WHERE id=%s', [server_id]]
                                    
                                server.delete()
                                
                                return "Error: you need a initial group for your server"
                                
                    else:
                        task_ssh.ssh.close()
                        return "Cannot connect to the new server "+task_ssh.txt_error
                    
                else:
                    
                    forms=formsutils.show_form(getpostfiles.post, server.forms, t, True)
                        
                    return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url+'?op=1&group_id='+getpostfiles.get['group_id'])
                    
            else:
                
                server.fields['ip'].duplicated_ip=True
                
                forms=formsutils.show_form(getpostfiles.post, server.forms, t, True)
                        
                return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url+'?op=1&group_id='+getpostfiles.get['group_id'])
                
    #elif getpostfiles.get['op']=='2':
        
        #return ""
    
    elif getpostfiles.get['op']=='2':
        
        getpostfiles=GetPostFiles()
        
        getpostfiles.obtain_get()
        
        server_id=int(getpostfiles.get.get('id', '0'))
        
        arr_server=server.select_a_row(server_id)
        
        if arr_server:
        
            return t.load_template('pastafari/admin/graphs.phtml', server=arr_server, api_key=config_task.api_key)
            
        else:
            
            return ""
            
    
    elif getpostfiles.get['op']=='5':
        
        getpostfiles.get['id']=getpostfiles.get.get('id', '0')
        
        getpostfiles.get['delete']=getpostfiles.get.get('delete', '0')
        
        try:
            getpostfiles.get['id']=int(getpostfiles.get['id'])
        except:
            getpostfiles.get['id']=0
            
        if getpostfiles.get['delete']!='0':
            
            group_server_item.set_conditions('WHERE server_id=%s', [ getpostfiles.get['id']])
            
            group_server_item.delete()
            
            server.set_conditions('WHERE id=%s', [ getpostfiles.get['id']])
            
            server.delete()
            
            set_flash_message('Deleted the server sucessfully')
            
            redirect(make_url(pastafari_folder+'/servers'))
            
            pass
        else:
            
            return "It deleted the server in database <strong>only</strong><p>You need delete the server with the tool of your election: <input type=\"button\" value=\"Do you are sure?\" onclick=\"javascript:location.href='"+make_url(pastafari_folder+'/servers', {'op': '5', 'id': str(getpostfiles.get['id']), 'delete': '1'})+"';\"></p>"
        
        
        pass
    
    else:
        
        getpost=GetPostFiles()
        
        getpost.obtain_get()
        
        try: 
        
            group_id=int(getpost.get.get('group_id', '0'))
            
        except:
            
            group_id=0
        
        select_form_group=SelectModelForm('group_id', group_id, servers.ServerGroup(conn), 'name', 'id', 'parent_id')
        
        select_form_group.name_field_id='change_group_id_form'
                
        servers_list=SimpleList(server, url, t)
        
        yes_form=0
        
        type_op=''
        
        #servers_list.arr_extra_fields=[I18n.lang('common', 'options', 'Options')]
        
        servers_list.arr_extra_options=[server_options]
        
        servers_list.yes_search=False
        
        select_task=None
        
        if 'type' in getpost.get:
            
            if getpost.get['type']=='down':
                
                actual_timestamp=datetime.obtain_timestamp(datetime.now())
            
                past_timestamp=actual_timestamp-300
                
                actual_time=datetime.timestamp_to_datetime(actual_timestamp)
            
                past_time=datetime.timestamp_to_datetime(past_timestamp)
                
                servers_list.model.set_conditions('WHERE date<%s', [past_time])
                
            elif getpost.get['type']=='heavy':
                servers_list.model.set_conditions("where actual_idle>%s", [80])
            
            elif getpost.get['type']=='disks':
                servers_list.model.set_conditions("where ip IN (select ip from statusdisk where percent>90)", [])
                
            elif getpost.get['type']=='update_servers':
                
                servers_list.model.set_conditions("where num_updates>0", [])
                
                servers_list.arr_extra_fields=[I18n.lang('common', 'update_server', 'Update server')]
        
                servers_list.arr_extra_options=[server_update_options]
                
                servers_list.yes_search=False
                
                yes_form=1
                
            elif getpost.get['type']=='task_servers': 
                
                servers_list.arr_extra_fields=[I18n.lang('pastafari', 'choose_server', 'Choose server')]
        
                servers_list.arr_extra_options=[server_update_options]
                
                servers_list.yes_search=False
                
                yes_form=2
                
                # Get tasks and put in select_task
                
                # Folders are tasks/ and modules/pastafari/tasks
                
                base_path='modules/pastafari/tasks'
                
                config_parser = configparser.ConfigParser()
                
                select_task=scandir(base_path, config_parser, OrderedDict(),  'tasks')
                
                #OrderedDict([('tasks', {'servers': [['Servers', 'tasks', 0]]}), ('servers', {'databases': [['Database servers', 'servers', 0]], 'mail': [['Mail', 'servers', 0], ['Standalone postfix server,  Install on your servers a simple and secure postfix mail server', 'mail', 'modules/pastafari/tasks/servers/mail/postfix/standalone_postfix.py', 1]]})])

                
            type_op=getpost.get['type']
        
        if group_id>0:
            servers_list.model.conditions[0]+=' AND id IN (select server_id from servergroupitem where group_id=%s)'
            servers_list.model.conditions[1].append(group_id)
        
        servers_list.fields_showed=['hostname', 'ip', 'num_updates', 'date']
        
        servers_list.limit_pages=100
        
        servers_list.s['order']='0'
        servers_list.s['order_field']='hostname'

        show_servers=servers_list.show()
        
        return t.load_template('pastafari/admin/servers.phtml', show_servers=show_servers, type_op=type_op, yes_form=yes_form, csrf_token=csrf_token(), select_form_group=select_form_group, group_id=group_id, select_task=select_task)
    
    
    return ""
Ejemplo n.º 34
0
def admin(**args):
    
    t=args['t']
    
    conn=args['connection']
    
    server=servers.Server(conn)
    
    os_model=servers.OsServer(conn)
    
    task=tasks.Task(conn)
    
    logtask=tasks.LogTask(conn)
    
    group_server=servers.ServerGroup(conn)
    
    group_server_item=servers.ServerGroupItem(conn)
    
    getpostfiles=GetPostFiles()
    
    getpostfiles.obtain_get()
    
    getpostfiles.get['op']=getpostfiles.get.get('op', '')
    
    getpostfiles.get['group_id']=getpostfiles.get.get('group_id', '0')

    request_type=formsutils.request_type()
    
    server.fields['os_codename'].name_form=SelectForm

    server.create_forms(['hostname', 'ip', 'os_codename'])
    
    arr_os={}
    
    with os_model.select() as cur:
        arr_os = { v['codename'] : v['name'] for v in cur }
    
    server.forms['os_codename'].arr_select=arr_os
    
    server.forms['password']=PasswordForm('password', '')
    
    server.forms['password'].required=True
    
    server.forms['password'].label=I18n.lang('pastafari', 'password', 'Password')
    
    server.forms['delete_root_password']=SelectForm('delete_root_password', '', {'0': 'No', '1': 'Yes'})
    
    server.forms['delete_root_password'].label=I18n.lang('pastafari', 'delete_root_password', 'Disable root password')
    
    server.forms['group_id']=SelectModelForm('group_id', getpostfiles.get['group_id'], group_server, 'name', 'id', 'parent_id')
    
    server.forms['group_id'].field=group_server_item.fields['server_id']
    
    server.forms['group_id'].required=True
    
    server.forms['group_id'].label=label=I18n.lang('pastafari', 'principal_group', 'Principal group')

    if getpostfiles.get['op']=='1':
        
        if request_type!="POST":
            
            forms=formsutils.show_form({}, server.forms, t, False, False)
            
            return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url+'?op=1&group_id='+getpostfiles.get['group_id'])
        else:
            
            #if insert then send task to servertask
            
            getpostfiles.obtain_post()
            
            post={}
            
            post['password']=getpostfiles.post.get('password', '')
            post['delete_root_password']=getpostfiles.post.get('delete_root_password', '0')
            post['ip']=getpostfiles.post.get('ip', '')
            
            try:
            
                post['group_id']=getpostfiles.post.get('group_id', '0')
                
            except:
                
                post['group_id']='0'
            
            check_form=formsutils.CheckForm()
            
            (post, pass_form)=check_form.check(post, {'password': server.forms['password'], 'delete_root_password': server.forms['delete_root_password'], 'ip': server.forms['ip'], 'group_id': server.forms['group_id']})
            
            server.set_conditions('WHERE ip=%s', [post['ip']])
            
            c_ip=server.select_count()
            
            if c_ip==0:
            
                if check_form.error==0:
                    
                    #try connect to the server
                    
                    task_ssh=Task(post['ip'])
                    
                    testing_task=copy.copy(config_task)
                    
                    testing_task.remote_user='******'
                    testing_task.remote_password=post['password']
                    
                    task_ssh.config=testing_task
                    
                    if task_ssh.prepare_connection():
                    
                        task_ssh.ssh.close()
                
                        if not server.insert(getpostfiles.post):
                            
                            forms=formsutils.show_form(getpostfiles.post, server.forms, t, True)
                            
                            return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url+'?op=1')
                
                        else:
                            
                            server_id=server.insert_id()
                            
                            # Insert in server group
                            
                            group_server_item.valid_fields=['group_id', 'server_id']
                            
                            if group_server_item.insert({'group_id': post['group_id'], 'server_id': server_id}):
                            
                                task.create_forms()
                                
                                os_server=getpostfiles.post['os_codename'].replace('/', '').replace('.', '')
                                
                                ip=server.fields['ip'].check(getpostfiles.post['ip'])
                                
                                files=[]
                                
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/install_python.sh', 0o750])
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/install_curl.sh', 0o750])
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/install_psutil.sh', 0o750])
                                files.append(['modules/pastafari/scripts/standard/'+os_server+'/upgrade.sh', 0o750])
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/alive.py', 0o750])
                                #files.append(['monit/'+os_server+'/files/alive.sh', 0o750];
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/files/get_info.py', 0o750])
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/files/get_updates.py', 0o750])
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/files/crontab/alive', 0o640])
                                files.append(['modules/pastafari/scripts/monit/'+os_server+'/files/sudoers.d/spanel', 0o640])
                                files.append([config_task.public_key, 0o600])

                                commands_to_execute=[]
                                
                                commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/install_python.sh', ''])
                                commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/install_curl.sh', ''])
                                commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/install_psutil.sh', ''])
                                commands_to_execute.append(['modules/pastafari/scripts/monit/'+os_server+'/alive.py', '--url='+config_task.url_monit+'/'+ip+'/'+config_task.api_key+' --user='******' --pub_key='+config_task.public_key])
                                
                                delete_files=[]
                                
                                delete_files.append('modules/pastafari/scripts/standard/'+os_server+'/install_python.sh')
                                delete_files.append('modules/pastafari/scripts/standard/'+os_server+'/install_curl.sh')
                                delete_files.append('modules/pastafari/scripts/standard/'+os_server+'/install_psutil.sh')
                                delete_files.append(config_task.public_key)
                                
                                delete_directories=['modules/pastafari']
                                
                                if post['delete_root_password']=='1':
                                    #delete_root_passwd.sh
                                    files.append(['modules/pastafari/scripts/standard/'+os_server+'/delete_root_passwd.sh', 0o750])
                                    commands_to_execute.append(['modules/pastafari/scripts/standard/'+os_server+'/delete_root_passwd.sh', ''])
                                    
                                
                                if task.insert({'name_task': 'monit_server','description_task': I18n.lang('pastafari', 'add_monit', 'Adding monitoritation to the server...'), 'url_return': url, 'files': files, 'commands_to_execute': commands_to_execute, 'delete_files': delete_files, 'delete_directories': delete_directories, 'server': ip, 'user': '******', 'password': post['password'], 'path': '/root'}):
                                                    
                                    task_id=task.insert_id()
                                                    
                                    try:
                                    
                                        r=requests.get(server_task+str(task_id))
                                        
                                        arr_data=r.json()
                                        
                                        arr_data['task_id']=task_id
                                        
                                        logtask.create_forms()
                                        
                                        if not logtask.insert(arr_data):
                                            
                                            return "Error:Wrong format of json data..."
                                            
                                            #return t_admin.load_template('pastafari/ajax_progress.phtml', title='Adding monitoritation to the server...') #"Load template with ajax..."
                                    
                                    except:
                                        
                                        logtask.conditions=['WHERE id=%s', [task_id]]
                                        
                                        task.update({'status': 1, 'error': 1})
                                        
                                        server.conditions=['WHERE id=%s', [server_id]]
                                        
                                        group_server_item.conditions=['WHERE server_id=%s', [server_id]]
                                    
                                        group_server_item.delete()
                                    
                                        server.delete()
                                        
                                        return "Error:cannot connect to task server, check the url for it..."
                                    
                                    return t.load_template('pastafari/progress.phtml', name_task=I18n.lang('pastafari', 'add_monit', 'Adding monitoritation to the server...'), description_task=I18n.lang('pastafari', 'add_monit_explain', 'Installing the basic scripts for send info from server to monit module'), task_id=task_id, server=ip, position=0)
                                    #return "Server is building..."
                                    #redirect('servers?op=2&task_id='+str(task_id))
                    
                                else:
                                    
                                    server.conditions=['WHERE id=%s', [server_id]]
                                    
                                    server.delete()
                                    
                                    group_server_item.conditions=['WHERE server_id=%s', [server_id]]
                                    
                                    group_server_item.delete()
                                    
                                    return "Error: cannot create the new task"
                            else:
                                
                                server.conditions=['WHERE id=%s', [server_id]]
                                    
                                server.delete()
                                
                                return "Error: you need a initial group for your server"
                                
                    else:
                        task_ssh.ssh.close()
                        return "Cannot connect to the new server "+task_ssh.txt_error
                    
                else:
                    
                    forms=formsutils.show_form(getpostfiles.post, server.forms, t, True)
                        
                    return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url+'?op=1&group_id='+getpostfiles.get['group_id'])
                    
            else:
                
                server.fields['ip'].duplicated_ip=True
                
                forms=formsutils.show_form(getpostfiles.post, server.forms, t, True)
                        
                return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url+'?op=1&group_id='+getpostfiles.get['group_id'])
                
    #elif getpostfiles.get['op']=='2':
        
        #return ""
    
    elif getpostfiles.get['op']=='2':
        
        getpostfiles=GetPostFiles()
        
        getpostfiles.obtain_get()
        
        server_id=int(getpostfiles.get.get('id', '0'))
        
        arr_server=server.select_a_row(server_id)
        
        if arr_server:
        
            return t.load_template('pastafari/admin/graphs.phtml', server=arr_server, api_key=config_task.api_key)
            
        else:
            
            return ""
    
    elif getpostfiles.get['op']=='3':
        
        t.show_basic_template=False
        
        getpost=GetPostFiles()
        
        getpost.obtain_get()
        
        server_id=int(getpostfiles.get.get('id', '0'))
        
        arr_server=server.select_a_row(server_id)
        
        if 'ip' in arr_server:
        
            ip=arr_server['ip']
            
            now=datetime.obtain_timestamp(datetime.now())
            
            hours12=now-21600
            
            date_now=datetime.timestamp_to_datetime(now)
            
            date_hours12=datetime.timestamp_to_datetime(hours12)
            
            status_cpu=servers.StatusCpu(conn)
            
            status_cpu.set_conditions('where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now])
            
            #arr_cpu=status_cpu.select_to_array(['idle', 'date'])
            cur=status_cpu.select(['idle', 'date'])
            
            x=0
            
            arr_cpu=[]
            
            cur.fetchone()
            
            for cpu_info in cur:
                
                arr_cpu.append(cpu_info['idle'])
                
            cur.close()
            
            status_mem=servers.StatusMemory(conn)
            
            status_mem.set_conditions('where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now]) 
            
            #status_mem.set_order(['id', 'ASC'])
            
            #arr_mem=status_mem.select_to_array(['used', 'free', 'date'])
            arr_mem=[]
            with status_mem.select(['used', 'free', 'cached', 'date'])  as cur:
                #cur.fetchone()
                
                for mem_info in cur:
                    mem_info['used']=((mem_info['used']/1024)/1024)/1024
                    mem_info['free']=((mem_info['free']/1024)/1024)/1024
                    mem_info['cached']=((mem_info['cached']/1024)/1024)/1024
                    arr_mem.append(mem_info)
            
            if len(arr_mem)>2:
                arr_mem.pop(0)
            
            #arr_cpu=status_cpu.select_to_array(['idle', 'date'])
            cur=status_cpu.select(['idle', 'date'])
            
            arr_net={}
            
            status_net=servers.StatusNet(conn)
            
            status_net.set_conditions('where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now])
            
            arr_net=[]
            
            cur=status_net.select(['bytes_sent', 'bytes_recv', 'date'])
            
            substract_time=0 #datetime.obtain_timestamp(datetime.now())
            
            c_hours12=now
            
            c_elements=0
            
            if cur.rowcount>0:
            
                data_net=cur.fetchone()
                
                first_recv=data_net['bytes_recv']
                first_sent=data_net['bytes_sent']
                
                for data_net in cur:
                    
                    timestamp=datetime.obtain_timestamp(data_net['date'], True)
                    
                    diff_time=timestamp-substract_time
                    
                    if substract_time!=0 and diff_time>300:
                        
                        count_time=timestamp
                        
                        while substract_time<=count_time:
                
                            form_time=datetime.timestamp_to_datetime(substract_time)
                            
                            arr_net.append({'date': datetime.format_time(form_time)})
                                    
                            substract_time+=60
                    
                    bytes_sent=round((data_net['bytes_sent']-first_sent)/1024)
                    bytes_recv=round((data_net['bytes_recv']-first_recv)/1024)
                    cpu=arr_cpu[x]
                    
                    memory_used=arr_mem[x]['used']
                    memory_free=arr_mem[x]['free']
                    memory_cached=arr_mem[x]['cached']

                    arr_net.append({'bytes_sent': bytes_sent, 'bytes_recv': bytes_recv, 'date': datetime.format_time(data_net['date']), 'cpu': cpu, 'memory_used': memory_used, 'memory_free': memory_free, 'memory_cached': memory_cached})
                    
                    first_sent=data_net['bytes_sent']
                    first_recv=data_net['bytes_recv']
                    
                    c_hours12=timestamp
                    
                    substract_time=int(timestamp)
                    
                    c_elements+=1
                    
                    x+=1
                    
                # If the last time is more little that now make a loop 
                
                while c_hours12<=now:
                
                    form_time=datetime.timestamp_to_datetime(c_hours12)
                    
                    seconds=form_time[-2:]
                        
                    #print(form_time)
                    
                    if seconds=='00':
                        
                        arr_net.append({'date': datetime.format_time(form_time)})
                            
                        # if secons is 00 and z=1 put value
                        #arr_net.append({'date': datetime.format_time(form_time)})
                            
                        pass
                    
                    c_hours12+=1
                
                cur.close()
                
                if c_elements>2:
                    
                    return json.dumps(arr_net)
                else:
                    
                    return {}
                    
                return {}
            
        return  {}
    
    elif getpostfiles.get['op']=='4':
        
        t.show_basic_template=False
        
        getpost=GetPostFiles()
        
        getpost.obtain_get()
        
        server_id=int(getpostfiles.get.get('id', '0'))
        
        arr_server=server.select_a_row(server_id)
        
        if 'ip' in arr_server:
        
            ip=arr_server['ip']
            
            status_disk=servers.StatusDisk(conn)
            
            status_disk.set_conditions('where ip=%s', [ip])
            
            arr_disk=status_disk.select_to_array(['disk', 'used', 'free', 'date'])
            
            return json.dumps(arr_disk)
    
    elif getpostfiles.get['op']=='5':
        
        getpostfiles.get['id']=getpostfiles.get.get('id', '0')
        
        getpostfiles.get['delete']=getpostfiles.get.get('delete', '0')
        
        try:
            getpostfiles.get['id']=int(getpostfiles.get['id'])
        except:
            getpostfiles.get['id']=0
            
        if getpostfiles.get['delete']!='0':
            
            group_server_item.set_conditions('WHERE server_id=%s', [ getpostfiles.get['id']])
            
            group_server_item.delete()
            
            server.set_conditions('WHERE id=%s', [ getpostfiles.get['id']])
            
            server.delete()
            
            set_flash_message('Deleted the server sucessfully')
            
            redirect(make_admin_url('pastafari/servers'))
            
            pass
        else:
            
            return "It deleted the server in database <strong>only</strong><p>You need delete the server with the tool of your election: <input type=\"button\" value=\"Do you are sure?\" onclick=\"javascript:location.href='"+make_admin_url('pastafari/servers', {'op': '5', 'id': str(getpostfiles.get['id']), 'delete': '1'})+"';\"></p>"
        
        
        pass
    
    else:
        
        """
        $actual_timestamp=time();
        
        $past_timestamp=time()-300;

        $actual_time=PhangoApp\PhaTime\DateTime::format_timestamp($actual_timestamp, $localtime=false);

        $past_time=PhangoApp\PhaTime\DateTime::format_timestamp($past_timestamp, $localtime=false);

        //$m->server->set_order(['date' => 1]);

        //$m->server->set_conditions(['where date<?', [$past_time]]);
        
        $admin->where_sql=['where date <?', [$past_time]];
        """
        
        getpost=GetPostFiles()
        
        getpost.obtain_get()
        
        try: 
        
            group_id=int(getpost.get.get('group_id', '0'))
            
        except:
            
            group_id=0
        
        select_form_group=SelectModelForm('group_id', group_id, servers.ServerGroup(conn), 'name', 'id', 'parent_id')
        
        select_form_group.name_field_id='change_group_id_form'
                
        servers_list=SimpleList(server, url, t)
        
        yes_form=0
        
        type_op=''
        
        #servers_list.arr_extra_fields=[I18n.lang('common', 'options', 'Options')]
        
        servers_list.arr_extra_options=[server_options]
        
        select_task=None
        
        if 'type' in getpost.get:
            
            if getpost.get['type']=='down':
                
                actual_timestamp=datetime.obtain_timestamp(datetime.now())
            
                past_timestamp=actual_timestamp-300
                
                actual_time=datetime.timestamp_to_datetime(actual_timestamp)
            
                past_time=datetime.timestamp_to_datetime(past_timestamp)
                
                servers_list.model.set_conditions('WHERE date<%s', [past_time])
                
            elif getpost.get['type']=='heavy':
                servers_list.model.set_conditions("where actual_idle>%s", [80])
            
            elif getpost.get['type']=='disks':
                servers_list.model.set_conditions("where ip IN (select ip from statusdisk where percent>90)", [])
                
            elif getpost.get['type']=='update_servers':
                
                servers_list.model.set_conditions("where num_updates>0", [])
                
                servers_list.arr_extra_fields=[I18n.lang('common', 'update_server', 'Update server')]
        
                servers_list.arr_extra_options=[server_update_options]
                
                servers_list.yes_search=False
                
                yes_form=1
                
            elif getpost.get['type']=='task_servers': 
                
                servers_list.arr_extra_fields=[I18n.lang('pastafari', 'choose_server', 'Choose server')]
        
                servers_list.arr_extra_options=[server_update_options]
                
                servers_list.yes_search=False
                
                yes_form=2
                
                # Get tasks and put in select_task
                
                # Folders are tasks/ and modules/pastafari/tasks
                
                base_path='modules/pastafari/tasks'
                
                config_parser = configparser.ConfigParser()
                
                select_task=scandir(base_path, config_parser, OrderedDict(),  'tasks')
                
                #OrderedDict([('tasks', {'servers': [['Servers', 'tasks', 0]]}), ('servers', {'databases': [['Database servers', 'servers', 0]], 'mail': [['Mail', 'servers', 0], ['Standalone postfix server,  Install on your servers a simple and secure postfix mail server', 'mail', 'modules/pastafari/tasks/servers/mail/postfix/standalone_postfix.py', 1]]})])

                
            type_op=getpost.get['type']
        
        if group_id>0:
            servers_list.model.conditions[0]+=' AND id IN (select server_id from servergroupitem where group_id=%s)'
            servers_list.model.conditions[1].append(group_id)
        
        servers_list.fields_showed=['hostname', 'ip', 'num_updates', 'date']
        
        servers_list.limit_pages=100

        show_servers=servers_list.show()
        
        return t.load_template('pastafari/admin/servers.phtml', show_servers=show_servers, type_op=type_op, yes_form=yes_form, csrf_token=csrf_token(), select_form_group=select_form_group, group_id=group_id, select_task=select_task)
    
    
    return ""
Ejemplo n.º 35
0
    def show( begin_page, total_elements, num_elements, link ,initial_num_pages=20, variable='begin_page', label='', func_jscript=''):
        
        pages='';

        if begin_page>total_elements:
            begin_page=0

        # Calculamos el total de todas las páginas

        total_page=ceil(total_elements/num_elements)
        
        # Calculamos en que página nos encontramos

        actual_page=ceil(begin_page/num_elements)

        # Calculamos el total de intervalos

        total_interval=ceil(total_page/initial_num_pages)

        # Calculamos el intervalo en el que estamos

        actual_interval=floor(actual_page/initial_num_pages)

        # Calculamos el elemento de inicio del intervalo

        initial_page=ceil(actual_interval*initial_num_pages*num_elements)

        last_page=ceil(actual_interval*initial_num_pages*num_elements)+ceil(initial_num_pages*num_elements)

        if last_page>total_elements:
            last_page=total_elements

        if initial_page>0:
            initial_link=add_get_parameters(link, **{variable: '0'});
            middle_link=add_get_parameters(link, **{variable: str((initial_page-num_elements)) } );
            pages += "<a class=\""+Pages.css_class+"\" href=\""+initial_link+"\" onclick=\"func_jscript\">1</a> <a class=\""+Pages.css_class+"\" href=\""+middle_link+"\">&lt;&lt;</a> "

        arr_pages={}

        #for(x=initial_page;x<last_page;x+=num_elements)
        for x in range(initial_page, last_page, num_elements):
            
            middle_link=add_get_parameters(link, **{variable: str(x)} )

            num_page=ceil(x/num_elements)+1;
            arr_pages[x]="<a class=\""+Pages.css_class+"\" href=\""+middle_link+"\" onclick=\"func_jscript\">"+str(num_page)+"</a> "
            arr_pages[begin_page]='<span class="selected_page">'+str(num_page)+'</span> ';
            pages += arr_pages[x]

        
        if last_page<total_elements:

            middle_link=add_get_parameters(link, **{variable: str(x+num_elements)} );
            last_link=add_get_parameters(link, **{variable: str( ( ( total_page*num_elements ) - num_elements) ) } )

            pages += "<a class=\""+Pages.css_class+"\" href=\""+middle_link+"\" onclick=\"func_jscript\">&gt;&gt;</a> <a class=\"link_pages\" href=\""+last_link+"\" onclick=\"func_jscript\">"+I18n.lang('common', 'last', 'Last')+"</a>"

        
        return pages
Ejemplo n.º 36
0
def home():
    
    connection=WebModel.connection()
    #Fix, make local variable
    
    t=PTemplate(env)
    
    getpost=GetPostFiles()            
    
    getpost.obtain_get()
    
    try:
        group_id=int(getpost.get.get('group_id', '0'))
    except:
        group_id=0

    if request_type()=='POST':
        getpost.obtain_post()
    
    s=get_session()
    
    if 'login' in s:
        
        if s['privileges']==2:
            
            task=Task(connection)
            logtask=LogTask(connection)
            server=Server(connection)
            
            #Load menu
            
            menu=get_menu(config_admin.modules_admin)
        
            lang_selected=get_language(s)
            
            content_index=''
            
            # Send request to server
            
            arr_servers=[]
            
            where_sql='WHERE num_updates>0'
            
            for server_id in getpost.post.values():
                
                try:
                    server_id=int(server_id)
                    
                    if server_id>0:
                        arr_servers.append(str(server_id))
                except:
                    pass
            
            if len(arr_servers)>0:
                where_sql='WHERE id IN (%s)' % ",".join(arr_servers)
            
            if group_id>0:
                where_sql+=' AND id IN (select server_id from servergroupitem where group_id='+str(group_id)+')'
            
            commands_to_execute=[['bin/upgrade.sh', '']]
            
            task.create_forms()
            
            if task.insert({'name_task': 'update_server','description_task': I18n.lang('pastafari', 'update_servers', 'Updating servers...'), 'url_return': '', 'commands_to_execute': commands_to_execute, 'server': '', 'where_sql_server': where_sql}):
                                                
                task_id=task.insert_id()
                                
                try:
                
                    r=requests.get(server_task+str(task_id))
                    
                    arr_data=r.json()
                    
                    arr_data['task_id']=task_id
                    
                    logtask.create_forms()
                    
                    if not logtask.insert(arr_data):
                        
                        content_index="Error:Wrong format of json data..."
                        
                        #return t_admin.load_template('pastafari/ajax_progress.phtml', title='Adding monitoritation to the server...') #"Load template with ajax..."
                    else:
                        
                        server.set_conditions(where_sql, [])
                        
                        num_servers=server.select_count()
                        
                        #content_index=t.load_template('pastafari/updates.phtml', task_id=task_id, title_task=I18n.lang('pastafari', 'servers_updating', 'Servers updating'), num_servers=num_servers)
                        content_index=t.load_template('pastafari/updates.phtml', task_id=task_id, title_task=I18n.lang('pastafari', 'servers_updating', 'Servers updating'), description_task=I18n.lang('pastafari', 'servers_updating_txt', 'All servers selected will be updated...'), num_servers=num_servers)
                
                except:
                    
                    #logtask.conditions=['WHERE id=%s', [task_id]]
                    
                    task.update({'status': 1, 'error': 1})
                    
                    content_index="Error:cannot connect to task server, check the url for it..."+traceback.format_exc()
                    
            else:
                content_index="Error: cannot insert the task: "+task.show_errors()

            return t.load_template('admin/content.html', title='Updating servers', content_index=content_index, menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n)
            
        else:
            redirect(make_url(config.admin_folder))
    
    else:
    
        redirect(make_url(config.admin_folder))
Ejemplo n.º 37
0
def admin(**args):

    t = args['t']

    conn = args['connection']

    server = servers.Server(conn)

    os_model = servers.OsServer(conn)

    task = tasks.Task(conn)

    logtask = tasks.LogTask(conn)

    group_server = servers.ServerGroup(conn)

    group_server_item = servers.ServerGroupItem(conn)

    getpostfiles = GetPostFiles()

    getpostfiles.obtain_get()

    getpostfiles.get['op'] = getpostfiles.get.get('op', '')

    getpostfiles.get['group_id'] = getpostfiles.get.get('group_id', '0')

    request_type = formsutils.request_type()

    server.fields['os_codename'].name_form = SelectForm

    server.create_forms(['hostname', 'ip', 'os_codename'])

    arr_os = {}

    with os_model.select() as cur:
        arr_os = {v['codename']: v['name'] for v in cur}

    server.fields['date'].label = I18n.lang('pastafari', 'server_status',
                                            'Server status')

    server.forms['os_codename'].arr_select = arr_os

    server.forms['password'] = PasswordForm('password', '')

    server.forms['password'].required = True

    server.forms['password'].label = I18n.lang('pastafari', 'password',
                                               'Password')

    server.forms['delete_root_password'] = SelectForm('delete_root_password',
                                                      '1', {
                                                          '0': 'No',
                                                          '1': 'Yes'
                                                      })

    server.forms['delete_root_password'].label = I18n.lang(
        'pastafari', 'delete_root_password', 'Disable root password')

    server.forms['clean_gcc'] = SelectForm('clean_gcc', '1', {
        '0': 'No',
        '1': 'Yes'
    })

    server.forms['clean_gcc'].label = I18n.lang(
        'pastafari', 'clean_gcc',
        'Clean build dependencies for soft monitoring install?,if you want gcc in your server, answer NO'
    )

    server.forms['group_id'] = SelectModelForm('group_id',
                                               getpostfiles.get['group_id'],
                                               group_server, 'name', 'id',
                                               'parent_id')

    server.forms['group_id'].field = group_server_item.fields['server_id']

    server.forms['group_id'].required = True

    server.forms['group_id'].label = label = I18n.lang('pastafari',
                                                       'principal_group',
                                                       'Principal group')

    if getpostfiles.get['op'] == '1':

        if request_type != "POST":

            forms = formsutils.show_form({}, server.forms, t, False, False)

            return t.load_template('pastafari/admin/add_servers.phtml',
                                   group_id=getpostfiles.get['group_id'],
                                   form_server=forms,
                                   url=url + '?op=1&group_id=' +
                                   getpostfiles.get['group_id'])
        else:

            #if insert then send task to servertask

            getpostfiles.obtain_post()

            post = {}

            post['password'] = getpostfiles.post.get('password', '')
            post['delete_root_password'] = getpostfiles.post.get(
                'delete_root_password', '0')
            post['ip'] = getpostfiles.post.get('ip', '')
            post['clean_gcc'] = getpostfiles.post.get('clean_gcc', '1')

            try:

                post['group_id'] = getpostfiles.post.get('group_id', '0')

            except:

                post['group_id'] = '0'

            check_form = formsutils.CheckForm()

            (post, pass_form) = check_form.check(
                post, {
                    'password': server.forms['password'],
                    'delete_root_password':
                    server.forms['delete_root_password'],
                    'ip': server.forms['ip'],
                    'group_id': server.forms['group_id']
                })

            server.set_conditions('WHERE ip=%s', [post['ip']])

            c_ip = server.select_count()

            if c_ip == 0:

                if check_form.error == 0:

                    #try connect to the server

                    task_ssh = Task(post['ip'])

                    testing_task = copy.copy(config_task)

                    testing_task.remote_user = '******'
                    testing_task.remote_password = post['password']

                    task_ssh.config = testing_task

                    if task_ssh.prepare_connection():

                        task_ssh.ssh.close()

                        if not server.insert(getpostfiles.post):

                            forms = formsutils.show_form(
                                getpostfiles.post, server.forms, t, True)

                            return t.load_template(
                                'pastafari/admin/add_servers.phtml',
                                group_id=getpostfiles.get['group_id'],
                                form_server=forms,
                                url=url + '?op=1')

                        else:

                            server_id = server.insert_id()

                            # Insert in server group

                            group_server_item.valid_fields = [
                                'group_id', 'server_id'
                            ]

                            if group_server_item.insert({
                                    'group_id':
                                    post['group_id'],
                                    'server_id':
                                    server_id
                            }):

                                task.create_forms()

                                os_server = getpostfiles.post[
                                    'os_codename'].replace('/', '').replace(
                                        '.', '')

                                ip = server.fields['ip'].check(
                                    getpostfiles.post['ip'])

                                files = []

                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_python.sh', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_curl.sh', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_psutil.sh', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/upgrade.sh', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/clean_gcc.sh', 0o750
                                ])

                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/alive.py', 0o750
                                ])
                                #files.append(['monit/'+os_server+'/files/alive.sh', 0o750];
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/files/get_info.py', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/files/get_updates.py', 0o750
                                ])
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/files/crontab/alive', 0o640
                                ])
                                files.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/files/sudoers.d/spanel',
                                    0o640
                                ])
                                files.append([config_task.public_key, 0o600])

                                commands_to_execute = []

                                commands_to_execute.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_curl.sh', ''
                                ])
                                commands_to_execute.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_python.sh', ''
                                ])
                                commands_to_execute.append([
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_psutil.sh', ''
                                ])
                                commands_to_execute.append([
                                    'modules/pastafari/scripts/monit/' +
                                    os_server + '/alive.py',
                                    '--url=' + config_task.url_monit + '/' +
                                    ip + '/' + config_task.api_key +
                                    ' --user='******' --pub_key=' + config_task.public_key
                                ])

                                delete_files = []

                                delete_files.append(
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_python.sh')
                                delete_files.append(
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_curl.sh')
                                delete_files.append(
                                    'modules/pastafari/scripts/standard/' +
                                    os_server + '/install_psutil.sh')
                                delete_files.append(config_task.public_key)

                                delete_directories = ['modules/pastafari']
                                delete_directories = [
                                    os.path.dirname(config_task.public_key)
                                ]

                                if post['delete_root_password'] == '1':
                                    #delete_root_passwd.sh
                                    files.append([
                                        'modules/pastafari/scripts/standard/' +
                                        os_server + '/delete_root_passwd.sh',
                                        0o750
                                    ])
                                    commands_to_execute.append([
                                        'modules/pastafari/scripts/standard/' +
                                        os_server + '/delete_root_passwd.sh',
                                        ''
                                    ])

                                if post['clean_gcc'] == '1':
                                    #delete_root_passwd.sh
                                    files.append([
                                        'modules/pastafari/scripts/standard/' +
                                        os_server + '/clean_gcc.sh', 0o750
                                    ])
                                    commands_to_execute.append([
                                        'modules/pastafari/scripts/standard/' +
                                        os_server + '/clean_gcc.sh', ''
                                    ])

                                #'modules.pastafari.tasks.system.install.task_functions'
                                if task.insert({
                                        'name_task':
                                        'monit_server',
                                        'description_task':
                                        I18n.lang(
                                            'pastafari', 'add_monit',
                                            'Adding monitoritation to the server...'
                                        ),
                                        'url_return':
                                        url,
                                        'files':
                                        files,
                                        'commands_to_execute':
                                        commands_to_execute,
                                        'delete_files':
                                        delete_files,
                                        'delete_directories':
                                        delete_directories,
                                        'server':
                                        ip,
                                        'user':
                                        '******',
                                        'password':
                                        post['password'],
                                        'path':
                                        '/root',
                                        'error_func':
                                        '',
                                        'extra_data': {
                                            'server_id': server_id
                                        }
                                }):

                                    task_id = task.insert_id()

                                    try:

                                        r = requests.get(server_task +
                                                         str(task_id))

                                        arr_data = r.json()

                                        arr_data['task_id'] = task_id

                                        logtask.create_forms()

                                        if not logtask.insert(arr_data):

                                            return "Error:Wrong format of json data..."

                                            #return t_admin.load_template('pastafari/ajax_progress.phtml', title='Adding monitoritation to the server...') #"Load template with ajax..."

                                    except:

                                        logtask.conditions = [
                                            'WHERE id=%s', [task_id]
                                        ]

                                        task.update({'status': 1, 'error': 1})

                                        server.conditions = [
                                            'WHERE id=%s', [server_id]
                                        ]

                                        group_server_item.conditions = [
                                            'WHERE server_id=%s', [server_id]
                                        ]

                                        group_server_item.delete()

                                        server.delete()

                                        return "Error:cannot connect to task server, check the url for it..."

                                    #return t.load_template('pastafari/progress.phtml', name_task=I18n.lang('pastafari', 'add_monit', 'Adding monitoritation to the server...'), description_task=I18n.lang('pastafari', 'add_monit_explain', 'Installing the basic scripts for send info from server to monit module'), task_id=task_id, server=ip, position=0)
                                    #return "Server is building..."
                                    #redirect('servers?op=2&task_id='+str(task_id))
                                    #@get('/'+pastafari_folder+'/showprogress/<task_id:int>/<server>')
                                    redirect(
                                        make_url(pastafari_folder +
                                                 '/showprogress/' +
                                                 str(task_id) + '/' + ip))

                                else:

                                    server.conditions = [
                                        'WHERE id=%s', [server_id]
                                    ]

                                    server.delete()

                                    group_server_item.conditions = [
                                        'WHERE server_id=%s', [server_id]
                                    ]

                                    group_server_item.delete()

                                    return "Error: cannot create the new task"
                            else:

                                server.conditions = [
                                    'WHERE id=%s', [server_id]
                                ]

                                server.delete()

                                return "Error: you need a initial group for your server"

                    else:
                        task_ssh.ssh.close()
                        return "Cannot connect to the new server " + task_ssh.txt_error

                else:

                    forms = formsutils.show_form(getpostfiles.post,
                                                 server.forms, t, True)

                    return t.load_template(
                        'pastafari/admin/add_servers.phtml',
                        group_id=getpostfiles.get['group_id'],
                        form_server=forms,
                        url=url + '?op=1&group_id=' +
                        getpostfiles.get['group_id'])

            else:

                server.fields['ip'].duplicated_ip = True

                forms = formsutils.show_form(getpostfiles.post, server.forms,
                                             t, True)

                return t.load_template('pastafari/admin/add_servers.phtml',
                                       group_id=getpostfiles.get['group_id'],
                                       form_server=forms,
                                       url=url + '?op=1&group_id=' +
                                       getpostfiles.get['group_id'])

    #elif getpostfiles.get['op']=='2':

    #return ""

    elif getpostfiles.get['op'] == '2':

        getpostfiles = GetPostFiles()

        getpostfiles.obtain_get()

        server_id = int(getpostfiles.get.get('id', '0'))

        arr_server = server.select_a_row(server_id)

        if arr_server:

            return t.load_template('pastafari/admin/graphs.phtml',
                                   server=arr_server,
                                   api_key=config_task.api_key)

        else:

            return ""

    elif getpostfiles.get['op'] == '5':

        getpostfiles.get['id'] = getpostfiles.get.get('id', '0')

        getpostfiles.get['delete'] = getpostfiles.get.get('delete', '0')

        try:
            getpostfiles.get['id'] = int(getpostfiles.get['id'])
        except:
            getpostfiles.get['id'] = 0

        if getpostfiles.get['delete'] != '0':

            group_server_item.set_conditions('WHERE server_id=%s',
                                             [getpostfiles.get['id']])

            group_server_item.delete()

            server.set_conditions('WHERE id=%s', [getpostfiles.get['id']])

            server.delete()

            set_flash_message('Deleted the server sucessfully')

            redirect(make_url(pastafari_folder + '/servers'))

            pass
        else:

            return "It deleted the server in database <strong>only</strong><p>You need delete the server with the tool of your election: <input type=\"button\" value=\"Do you are sure?\" onclick=\"javascript:location.href='" + make_url(
                pastafari_folder + '/servers', {
                    'op': '5',
                    'id': str(getpostfiles.get['id']),
                    'delete': '1'
                }) + "';\"></p>"

        pass

    else:

        getpost = GetPostFiles()

        getpost.obtain_get()

        try:

            group_id = int(getpost.get.get('group_id', '0'))

        except:

            group_id = 0

        select_form_group = SelectModelForm('group_id', group_id,
                                            servers.ServerGroup(conn), 'name',
                                            'id', 'parent_id')

        select_form_group.name_field_id = 'change_group_id_form'

        servers_list = SimpleList(server, url, t)

        yes_form = 0

        type_op = ''

        #servers_list.arr_extra_fields=[I18n.lang('common', 'options', 'Options')]

        servers_list.arr_extra_options = [server_options]

        servers_list.yes_search = False

        select_task = None

        if 'type' in getpost.get:

            if getpost.get['type'] == 'down':

                actual_timestamp = datetime.obtain_timestamp(datetime.now())

                past_timestamp = actual_timestamp - 300

                actual_time = datetime.timestamp_to_datetime(actual_timestamp)

                past_time = datetime.timestamp_to_datetime(past_timestamp)

                servers_list.model.set_conditions('WHERE date<%s', [past_time])

            elif getpost.get['type'] == 'heavy':
                servers_list.model.set_conditions("where actual_idle>%s", [80])

            elif getpost.get['type'] == 'disks':
                servers_list.model.set_conditions(
                    "where ip IN (select ip from statusdisk where percent>90)",
                    [])

            elif getpost.get['type'] == 'update_servers':

                servers_list.model.set_conditions("where num_updates>0", [])

                servers_list.arr_extra_fields = [
                    I18n.lang('common', 'update_server', 'Update server')
                ]

                servers_list.arr_extra_options = [server_update_options]

                servers_list.yes_search = False

                yes_form = 1

            elif getpost.get['type'] == 'task_servers':

                servers_list.arr_extra_fields = [
                    I18n.lang('pastafari', 'choose_server', 'Choose server')
                ]

                servers_list.arr_extra_options = [server_update_options]

                servers_list.yes_search = False

                yes_form = 2

                # Get tasks and put in select_task

                # Folders are tasks/ and modules/pastafari/tasks

                base_path = 'modules/pastafari/tasks'

                config_parser = configparser.ConfigParser()

                select_task = scandir(base_path, config_parser, OrderedDict(),
                                      'tasks')

                #OrderedDict([('tasks', {'servers': [['Servers', 'tasks', 0]]}), ('servers', {'databases': [['Database servers', 'servers', 0]], 'mail': [['Mail', 'servers', 0], ['Standalone postfix server,  Install on your servers a simple and secure postfix mail server', 'mail', 'modules/pastafari/tasks/servers/mail/postfix/standalone_postfix.py', 1]]})])

            type_op = getpost.get['type']

        if group_id > 0:
            servers_list.model.conditions[
                0] += ' AND id IN (select server_id from servergroupitem where group_id=%s)'
            servers_list.model.conditions[1].append(group_id)

        servers_list.fields_showed = ['hostname', 'ip', 'num_updates', 'date']

        servers_list.limit_pages = 100

        servers_list.s['order'] = '0'
        servers_list.s['order_field'] = 'hostname'

        show_servers = servers_list.show()

        return t.load_template('pastafari/admin/servers.phtml',
                               show_servers=show_servers,
                               type_op=type_op,
                               yes_form=yes_form,
                               csrf_token=csrf_token(),
                               select_form_group=select_form_group,
                               group_id=group_id,
                               select_task=select_task)

    return ""
Ejemplo n.º 38
0
from paramecio.citoplasma.i18n import I18n, load_lang
from settings.config_admin import modules_admin

#from modules.pokermind.i18n import runchained

modules_other = [
    I18n.lang('pages', 'pages', 'Pages'), 'modules.pages.admin.pages', 'pages'
]

modules_admin.append(modules_other)
Ejemplo n.º 39
0
    def show(self):
        
        getpostfiles=GetPostFiles()
        
        getpostfiles.obtain_get()
        
        getpostfiles.get['op_admin']=getpostfiles.get.get('op_admin', '0')
        
        getpostfiles.get['id']=getpostfiles.get.get('id', '0')
        
        if len(self.model.forms)==0:

            self.model.create_forms()
        
        edit_forms=OrderedDict()
            
        for key_form in self.arr_fields_edit:
            edit_forms[key_form]=self.model.forms[key_form]
        
        if getpostfiles.get['op_admin']=='1':
            
            post=None
            
            title_edit=I18n.lang('common', 'add_new_item', 'Add new item')
            
            if getpostfiles.get['id']!='0':
                post=self.model.select_a_row(getpostfiles.get['id'], [], True)
                title_edit=I18n.lang('common', 'edit_new_item', 'Edit item')
            
            if post==None or post==False:
                
                if getpostfiles.get['id']=='0':
                    post={}
                else:
                    return ""
            
            form=show_form(post, edit_forms, self.t, False)
                
            return self.t.load_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=getpostfiles.get['id'])
        
        elif getpostfiles.get['op_admin']=='2':
            
            getpostfiles.obtain_post()
            
            #post=getpostfiles.post
            
            self.model.reset_conditions()
            
            insert_row=self.model.insert
            
            try:
                
                getpostfiles.get['id']=str(int(getpostfiles.get['id']))
            
            except:
                
                getpostfiles.get['id']='0'
            
            title_edit=I18n.lang('common', 'add_new_item', 'Add new item')
                
            
            if getpostfiles.get['id']!='0':
                insert_row=self.model.update
                title_edit=I18n.lang('common', 'edit_new_item', 'Edit item')
                self.model.conditions=['WHERE `'+self.model.name+'`.`'+self.model.name_field_id+'`=%s', [getpostfiles.get['id']]]
            
            if insert_row(getpostfiles.post):
                set_flash_message(I18n.lang('common', 'task_successful', 'Task successful'))
                redirect(self.url)
            else:
                form=show_form(getpostfiles.post, edit_forms, self.t, True)
                return self.t.load_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=getpostfiles.get['id'])

            
            pass
            
        elif getpostfiles.get['op_admin']=='3':
            
            verified=getpostfiles.get.get('verified', '0')
            
            if verified=='1':
    
                if getpostfiles.get['id']!='0':
                    self.model.conditions=['WHERE `'+self.model.name+'`.`'+self.model.name_field_id+'`=%s', [getpostfiles.get['id']]]
                    self.model.delete()
                    set_flash_message(I18n.lang('common', 'task_successful', 'Task successful'))
                    redirect(self.url)
    
            else:
                
                return self.t.load_template(self.template_verify_delete, url=self.url, item_id=getpostfiles.get['id'], op_admin=3, verified=1)
    
        else:
            return self.t.load_template(self.template_admin, admin=self)
Ejemplo n.º 40
0
#!/usr/bin/python3

from paramecio.citoplasma.i18n import I18n

modules_admin = [[
    I18n.lang('admin', 'users_admin', 'User\'s Admin'),
    'paramecio.modules.admin.admin.ausers', 'ausers'
]]

modules_other = [
    I18n.lang('pastafari', 'pastafari', 'Pastafari'),
    [[
        I18n.lang('pastafari', 'servers', 'Servers'),
        'modules.pastafari.admin.servers', 'pastafari/servers'
    ]], 'pastafari'
]

modules_admin.append(modules_other)

#modules_admin={'ausers': [I18n.lang('admin', 'users_admin', 'User\'s Admin'), 'paramecio.modules.admin.admin.ausers'], 'module_father': [ I18n.lang('common', 'submodule', 'ModuleFather') , {'submodule': ['This is a module', 'paramecio.modules.admin.admin.ausers'] } ] }
Ejemplo n.º 41
0
def home(module='', submodule=''):
    
    # A simple boolean used for show or not the code of admin module in standard template
    connection=WebModel.connection()
    #Fix, make local variable
    
    t=PTemplate(env)
    
    t.add_filter(make_admin_url)
    
    t.show_basic_template=True
    
    if submodule!='':
        module+='/'+submodule
    
    #t.clean_header_cache()
    
    #check if login
    
    user_admin=UserAdmin(connection)
    
    s=get_session()
    
    if check_login():
        
        s['id']=s.get('id', 0)
        
        lang_selected=get_language(s)
        
        user_admin.set_conditions('WHERE id=%s', [s['id']])
        
        # Check if user id exists in session
        
        c=user_admin.select_count()
        
        if c>0:
        
            if s['privileges']==2:
                            #pass
                        
                if module in menu:
                    
                    #Load module
                    
                    
                    try:
                        #new_module=import_module(menu[module][1])
                        
                        #t.inject_folder=path.dirname(new_module.__file__).replace('/admin', '')
                        
                        #t.env=t.env_theme(path.dirname(__file__))
                        
                        t.env.directories.insert(1, path.dirname(module_imported[module].__file__).replace('/admin', '')+'/templates')
                        #print(t.env.directories)
                        #if config.reloader:
                            #reload(new_module)
                    
                    except ImportError:
                        
                        print("Exception in user code:")
                        print("-"*60)
                        traceback.print_exc(file=sys.stdout)
                        print("-"*60)
                        
                        return "No exists admin module"
                    
                    #args={'t': t, 'connection': connection}

                    content_index=module_imported[module].admin(t=t, connection=connection)

                    if t.show_basic_template==True:   
                    
                        return t.load_template('admin/content.html', title=menu[module][0], content_index=content_index, menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n)
                    else:
                        
                        return content_index
                        
                else:
                    return t.load_template('admin/index.html', title=I18n.lang('admin', 'welcome_to_paramecio', 'Welcome to Paramecio Admin!!!'), menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n)
                
        else:
            
            logout()
            
    else:
        
        user_admin.conditions=['WHERE privileges=%s', [2]]
        
        c=user_admin.select_count()
        
        if c>0:
            
            if request.get_cookie("remember_login", secret=key_encrypt):
            
                 #check login
            
                 token_login=request.get_cookie("remember_login", secret=key_encrypt)
            
                 user_admin.conditions=['WHERE token_login=%s', [token_login]]
    
                 arr_user=user_admin.select_a_row_where(['id', 'privileges'])
                 
                 if arr_user==False:
                     # delete cookioe
                     response.delete_cookie("remember_login")
                 else:
                     s=get_session()
            
                     s['id']=arr_user['id']
                     s['login']=1
                     s['privileges']=arr_user['privileges']
                     
                     s.save()
                     
                     redirect(make_url(config.admin_folder))
            
            else:
                
                post={}
                
                user_admin.yes_repeat_password=False

                user_admin.fields['password'].required=True
                
                user_admin.create_forms(['username', 'password'])
                
                forms=show_form(post, user_admin.forms, t, yes_error=False)
                
                #connection.close()
                
                return t.load_template('admin/login.phtml', forms=forms, yes_recovery_login=yes_recovery_login)
                
        else:
        
            post={}
            
            set_extra_forms_user(user_admin)
            
            forms=show_form(post, user_admin.forms, t, yes_error=False)

            return t.load_template('admin/register.phtml', forms=forms)