Example #1
0
def testconn(request):
    need_decode = request.data.get('need_decode', '')
    username = request.data.get('username', '').strip()
    db_name = request.data.get('db_name', '').strip()
    db_type = request.data.get('db_type', '')
    hostname = request.data.get('hostname', '').strip()
    port = request.data.get('port')
    password = request.data.get('password', '').strip()
    if need_decode:
        try:
            password = aes_decode(password)
        except:
            pass

        url = type2jdbcurl(db_type, hostname, port, db_name=db_name)
        jsonobj = {
            'user': username,
            'password': password,
            'jdbc_url': url,
            'driver': Driver[db_type].value
        }
        r = get_java_response('test-conn/', jsonobj)
        result = r.json()
    if r.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR:
        return Response(r.json(), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    elif result.get('message') == False:
        return Response({'message': ''},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    else:
        (Database.objects.filter(db_name=db_name,
                                 db_type=db_type,
                                 hostname=hostname,
                                 port=port)).update(disabled=False)
        return Response(r.json(), status=r.status_code)
Example #2
0
def run_cmd(host, cmd):
    try:
        s = paramiko.SSHClient()
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        s.connect(host.hostname, host.port, host.username,
                  aes_decode(host.password))
        stdin, stdout, stderr = s.exec_command(cmd)
        result = stdout.readlines()
        return result
    except paramiko.ssh_exception.SSHException as e:
        logger.error('is not support telnet now!')
    except Exception as e:
        logger.error(str(e))
Example #3
0
def hosts_summary(host):
    summary_data = cache.get('host-data' + str(host.id))
    if summary_data:
        return summary_data
    else:
        s = paramiko.SSHClient()
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        if host.ssh_key:
            private_key_file = io.StringIO()
            private_key_file.write(host.ssh_key)
            private_key_file.seek(0)
            private_key = paramiko.RSAKey.from_private_key(private_key_file)
            host_config = {
                'pkey': private_key,
                'hostname': host.address,
                'username': host.username,
                'timeout': 60,
                'port': host.port
            }
            s.connect(**host_config)
        else:
            s.connect(host.address, host.port, host.username,
                      aes_decode(host.password))
        cpu_model, cpu_num, core_num = get_cpu_info(s)
        memory_max_size, memory_speed, memory_model, memory_size = get_memory_info(
            s)
        network_info_list = get_network_info(s)
        directory_info_list = get_directory_info(s)
        hostname, lsb = get_host_info(s)
        summary_data = {
            'host_info': {
                'lsb': lsb,
                'hostname': hostname
            },
            'memory': {
                'memory_max_size': memory_max_size,
                'memory_speed': memory_speed,
                'memory_model': memory_model,
                'memory_size': memory_size
            },
            'processor': {
                'ansible_processor': cpu_model,
                'ansible_processor_count': cpu_num,
                'ansible_processor_core_count': core_num
            },
            'disk_mount': directory_info_list,
            'network_info': network_info_list
        }
        cache.set('host-data' + str(host.id), summary_data, timeout=360)
        return summary_data
Example #4
0
def host_test_connection(request):
    address = request.data.get(
        'address', '').strip() if request.data.get('address') else ''
    port = request.data.get('port', None)
    username = request.data.get(
        'username', '').strip() if request.data.get('username') else ''
    password = request.data.get(
        'password', '').strip() if request.data.get('password') else ''
    need_decode = request.data.get('need_decode', False)
    ssh_key = request.data.get('ssh_key')
    if not address:
        return Response({'message': 'hostname is required'},
                        status=status.HTTP_400_BAD_REQUEST)
    if ssh_key:
        private_key_file = io.StringIO()
        private_key_file.write(ssh_key)
        private_key_file.seek(0)
        private_key = paramiko.RSAKey.from_private_key(private_key_file)
        host_config = {
            'pkey': private_key,
            'hostname': address,
            'username': username,
            'timeout': 60
        }
    else:
        if need_decode:
            try:
                password = aes_decode(password)
            except Exception as e:
                print(str(e))

            host_config = {
                'hostname': address,
                'username': username,
                'password': password,
                'timeout': 60
            }
        if port:
            host_config.update({'port': port})
        try:
            s = paramiko.SSHClient()
            s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            s.connect(**host_config)
        except Exception as e:
            return Response({'message': ',:' + str(e)},
                            status=status.HTTP_400_BAD_REQUEST)

        return Response({'message': ''}, status=status.HTTP_200_OK)
Example #5
0
 def update(self, request, *args, **kwargs):
     partial = kwargs.pop('partial', False)
     instance = self.get_object()
     serializer = self.get_serializer(instance,
                                      data=request.data,
                                      partial=partial)
     serializer.is_valid(raise_exception=True)
     original_is_switch_off = instance.is_switch_off
     current_is_switch_off = serializer.context.get('request').data.get(
         'is_switch_off')
     if original_is_switch_off == True:
         if current_is_switch_off == False:
             instance_count = instance.instance_count or 1
             license_checked = current_target_available()
     if license_checked is False:
         return Response({'error_message': '!'},
                         status=status.HTTP_400_BAD_REQUEST)
     else:
         if license_checked is not True:
             if license_checked - instance_count < 0:
                 return Response(
                     {
                         'error_message':
                         ('{},{},!').format(license_checked, instance_count)
                     },
                     status=status.HTTP_400_BAD_REQUEST)
             if current_is_switch_off == True and original_is_switch_off == False:
                 url = type2jdbcurl(instance.db_type,
                                    instance.hostname,
                                    instance.port,
                                    db_name=instance.db_name)
                 jsonobj = {
                     'user': instance.username,
                     'password': aes_decode(instance.password),
                     'jdbc_url': url,
                     'driver': Driver[instance.db_type].value
                 }
                 get_java_response('close-conn/', jsonobj)
             self.perform_update(serializer)
             if getattr(instance, '_prefetched_objects_cache', None):
                 instance._prefetched_objects_cache = {}
         return Response(serializer.data)
Example #6
0
 def get_password(self):
     if not self.password:
         return ''
     else:
         return aes_decode(self.password)
Example #7
0
 def get_password(self):
     return aes_decode(self.password)