Exemplo n.º 1
0
async def connect_host():

    data = await request.json or request.args or await request.form
    if not data:
        return abort(400)

    name = data.get('name')
    docker_port = data.get('docker_port')
    ssh_port = data.get('ssh_port')

    try:
        Host.get(name=name)
        return 'name already taken', 400
    except peewee.DoesNotExist:
        pass

    address = request.remote_addr

    Host.create(name=name, protocol='tcp', address=address,
                docker_port=docker_port, ssh_port=ssh_port)

    with open('/etc/madt/hosts', 'a') as hosts_file:
        hosts_file.write(str(ssh_port) + '\n')

    with open('/root/.ssh/id_rsa.pub') as public_key_file:
        public_key = public_key_file.read()

    return public_key, 200
Exemplo n.º 2
0
def r_get(nodes_id):

    args_rules = [Rules.IDS.value]

    try:
        ji.Check.previewing(args_rules, {args_rules[0][1]: nodes_id})

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)
        ret['data'] = list()

        if -1 == nodes_id.find(','):
            node_id = nodes_id
            if db.r.hexists(app.config['hosts_info'], node_id):
                v = json.loads(db.r.hget(app.config['hosts_info'], node_id))
                v = Host.alive_check(v)
                v['node_id'] = node_id
                ret['data'] = v

        else:
            for node_id in nodes_id.split(','):
                if db.r.hexists(app.config['hosts_info'], node_id):
                    v = json.loads(db.r.hget(app.config['hosts_info'],
                                             node_id))
                    v = Host.alive_check(v)
                    v['node_id'] = node_id
                    ret['data'].append(v)

            if ret['data'].__len__() > 1:
                ret['data'].sort(key=lambda _k: _k['boot_time'])

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 3
0
 def test_host_delete(self):
     """Deleting existing host"""
     host = Host(name='Host', description='Description',
                 ipv4='1.2.3.4', ipv6='', user=self.user)
     host.save()
     response = self.client.post(reverse('host_delete', args=[host.pk]))
     self.assertEqual(response.status_code, 302)
Exemplo n.º 4
0
def is_host_active(url):
    host = _parse_url(url)
    try:
        Host.get(url=host)
        return True
    except Host.DoesNotExist:
        return False
Exemplo n.º 5
0
class HostTest(TestCase):
    """Tests for hosts"""
    
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user('user', '*****@*****.**', 'userpassword')
        self.user.save()
        self.client.login(username='******', password='******')
        self.host = Host(name='Host', description='Description', ipv4='1.2.3.4', ipv6='', user=self.user)
        self.host.save()
        
    def test_host_list(self):
        """Get hosts list"""
        for i in xrange(10):
            host = Host(name='Host %i' % i, description='Description',
                        ipv4='1.2.3.%i' % i, ipv6='', user=self.user)
            host.save()
        response = self.client.get(reverse('host_list'))
        self.assertEqual(response.status_code, 200)
        
    def test_host_list_empty(self):
        """Get empty hosts list"""
        response = self.client.get(reverse('host_list'))
        self.assertEqual(response.status_code, 200)

    def test_host_detail(self):
        """Get host details"""
        response = self.client.get(reverse('host_detail', args=[self.host.pk]))
        self.assertEqual(response.status_code, 200)

    def test_host_create(self):
        """Creating new host"""
        host_data = {
            'name': 'New host',
            'description': 'New host description',
            'ipv4': '12.34.56.78',
            'ipv6': '2002:0:0:0:0:0:c22:384e',
            'user': self.user.pk
        }
        response = self.client.post(reverse('host_new'), host_data)
        self.assertEqual(response.status_code, 301)
        
    def test_host_delete(self):
        """Deleting existing host"""
        host = Host(name='Host', description='Description',
                    ipv4='1.2.3.4', ipv6='', user=self.user)
        host.save()
        response = self.client.post(reverse('host_delete', args=[host.pk]))
        self.assertEqual(response.status_code, 302)
        
    def test_host_update(self):
        """Update existing host"""
        host_data = {
            'name': 'New name',
            'description': 'New description'
        }
        url = reverse('host_update', args=[self.host.pk])
        response = self.client.post(url, host_data)
        self.assertEqual(response.status_code, 302)
Exemplo n.º 6
0
 def test_host_list(self):
     """Get hosts list"""
     for i in xrange(10):
         host = Host(name='Host %i' % i, description='Description',
                     ipv4='1.2.3.%i' % i, ipv6='', user=self.user)
         host.save()
     response = self.client.get(reverse('host_list'))
     self.assertEqual(response.status_code, 200)
Exemplo n.º 7
0
async def remove_host():
    try:
        id = await request.json.get('id', int)
    except ValueError:
        abort(404)

    Host.delete_by_id(id)

    return 'OK', 200
Exemplo n.º 8
0
 def done(self):
     logger.info("start to add hosts")
     for h in self.yal['host']:
         name = h['name']
         ip = h['ip']
         mac = h['mac']
         logger.info(mac)
         logger.info(ip)
         add_host = add_one_host(ip,name ,mac)
         add_host.done() 
         new_host = Host(hostname=name,static_ip=ip,status="added",hwaddr=mac)
         new_host.save()          
Exemplo n.º 9
0
 def _createHost(self, resource):
     '''Creates a host record for the mote described in the provided resource.
     
     :param resource: 
     :return: The created host
     :rtype: Host
     '''
     host = Host()
     host.interface_id = resource.value
     host.address      = resource.sourceAddress[0]
     host.name         = getInvariantName(host)    # requires ipAddress
     host.coords       = "100,100"                 # arbitrary values, so shows on map
     
     return host
Exemplo n.º 10
0
 def setUp(self):
     self.client = Client()
     self.user = User.objects.create_user('user', '*****@*****.**', 'userpassword')
     self.user.save()
     self.client.login(username='******', password='******')
     self.host = Host(name='Host', description='Description', ipv4='1.2.3.4', ipv6='', user=self.user)
     self.host.save()
Exemplo n.º 11
0
def r_content_search():
    keyword = request.args.get('keyword', '')

    args_rules = [Rules.KEYWORD.value]

    try:
        ji.Check.previewing(args_rules, {'keyword': keyword})

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)
        ret['data'] = list()

        for k, v in db.r.hgetall(app.config['hosts_info']).items():
            v = json.loads(v)
            if -1 != v['hostname'].find(keyword):
                v = Host.alive_check(v)
                v['node_id'] = k
                ret['data'].append(v)

        if ret['data'].__len__() > 1:
            ret['data'].sort(key=lambda _k: _k['boot_time'])

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 12
0
async def stop_lab_api():

    data = await request.json

    if data is None or 'name' not in data:
        return abort(400)

    name = data.get('name')

    try:
        lab = Lab.get(Lab.name == name)
    except peewee.DoesNotExist:
        abort(404)

    try:
        args = []
        if lab.runtime == 'cluster':
            args.extend((lab.get_host_map(), Host.get_all_configs()))

        stop_lab(lab_path(name),
                 prefix(name),
                 *args,
                 runtime=lab.runtime,
                 remove=True)
        lab.delete_instance()
        msg_cache.delete(name)
        return 'OK', 200

    except FileNotFoundError:
        return abort(404)
Exemplo n.º 13
0
    def exec_cmd(self, host_list, cmd):
        """
        批量命令
        :param host_list: 主机列表
        :param cmd: 执行命令
        :return: 命令执行结果
        """
        results_list = []  # 结果列表
        for host in host_list:
            host_obj = Host.filter(host)[0]  # 获取主机对象
            password = decrypt_oralce(host_obj.password)  # 解密
            conn = Lazyconnection(host_obj.addr, host_obj.name, password,
                                  host_obj.port)
            with conn as s:
                stdin, stdout, stderr = s.exec_command(cmd)
                stdout_result, stderr_result = stdout.read(), stderr.read()

                print(str(stdout_result, encoding='utf-8'),
                      str(stderr.read(), encoding='utf-8'))
                if stderr_result:
                    results_list.append(
                        {host_obj.addr: str(stderr_result, encoding='utf-8')})
                else:
                    results_list.append(
                        {host_obj.addr: str(stdout_result, encoding='utf-8')})

        return results_list
Exemplo n.º 14
0
def bootstrap_host(hostname):
    if hostname not in _host_db:
        logging.info("Host %s could not be bootstrapped" % hostname)
        return False

    cdn_id, uses_ssl = _host_db[hostname]

    try:
        cdn = CDN.get(id=cdn_id)
    except CDN.DoesNotExist:
        bootstrap_cdn(cdn_id)
        cdn = CDN.get(id=cdn_id)

    Host.create(url=hostname, cdn=cdn, ssl=uses_ssl)

    return True
Exemplo n.º 15
0
async def refresh_status():
    hosts = Host.select()

    if not hosts:
        return jsonify([])

    for host in hosts:
        try:
            host.get_docker_client().ping()
            host.is_alive = True
        except:
            host.is_alive = False

    Host.bulk_update(hosts, fields=[Host.is_alive])

    return jsonify({h.name: h.is_alive for h in hosts})
Exemplo n.º 16
0
def r_get_by_filter():

    try:
        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)
        ret['data'] = list()

        alive = None

        if 'alive' in request.args:
            alive = request.args['alive']

            if str(alive).lower() in ['false', '0']:
                alive = False

            else:
                alive = True

        for host in Host.get_all():
            if alive is not None and alive is not host['alive']:
                continue

            ret['data'].append(host)

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 17
0
def r_migrate(uuids, destination_host):

    args_rules = [
        Rules.UUIDS.value,
        Rules.DESTINATION_HOST.value
    ]

    try:
        ji.Check.previewing(args_rules, {'uuids': uuids, 'destination_host': destination_host})

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)

        # 取全部活着的 hosts
        available_hosts = Host.get_available_hosts(nonrandom=None)

        if available_hosts.__len__() == 0:
            ret['state'] = ji.Common.exchange_state(50351)
            return ret

        available_hosts_mapping_by_node_id = dict()

        for host in available_hosts:
            if host['node_id'] not in available_hosts_mapping_by_node_id:
                available_hosts_mapping_by_node_id[host['node_id']] = host

        guest = Guest()
        for uuid in uuids.split(','):
            guest.uuid = uuid
            guest.get_by('uuid')

        config = Config()
        config.id = 1
        config.get()

        for uuid in uuids.split(','):
            guest.uuid = uuid
            guest.get_by('uuid')

            # 忽略宕机计算节点 上面的 虚拟机 迁移请求
            # 忽略目标计算节点 等于 当前所在 计算节点 的虚拟机 迁移请求
            if guest.node_id not in available_hosts_mapping_by_node_id or \
                    available_hosts_mapping_by_node_id[guest.node_id]['hostname'] == destination_host:
                continue

            message = {
                '_object': 'guest',
                'action': 'migrate',
                'uuid': uuid,
                'node_id': guest.node_id,
                'storage_mode': config.storage_mode,
                'duri': 'qemu+ssh://' + destination_host + '/system'
            }

            Utils.emit_instruction(message=json.dumps(message))

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 18
0
 def domain_list(self):
     """
     List the active hosts
     """
     hosts = Host.select()
     for host in hosts:
         self.send_line("%*s: %s" % (20, host.url, host.cdn.id))
Exemplo n.º 19
0
def create_or_update_host(db_session, hostname, region_id, location, roles, software_profile_id, connection_type,
                          host_or_ip, username, password, enable_password, port_number, jump_host_id, created_by,
                          host=None):

    hostname = check_acceptable_string(hostname)
    """ Create a new host in the Database """
    if host is None:
        host = Host(created_by=created_by)
        host.inventory_job.append(InventoryJob())
        host.context.append(HostContext())
        db_session.add(host)

    host.hostname = hostname
    host.region_id = region_id if region_id > 0 else None
    host.software_profile_id = software_profile_id if software_profile_id > 0 else None
    host.location = '' if location is None else remove_extra_spaces(location)
    host.roles = '' if roles is None else remove_extra_spaces(roles)
    host.connection_param = [ConnectionParam(
        # could have multiple IPs, separated by comma
        host_or_ip='' if host_or_ip is None else remove_extra_spaces(host_or_ip),
        username='' if username is None else remove_extra_spaces(username),
        password='' if password is None else remove_extra_spaces(password),
        enable_password='' if enable_password is None else remove_extra_spaces(enable_password),
        jump_host_id=jump_host_id if jump_host_id > 0 else None,
        connection_type=connection_type,
        # could have multiple ports, separated by comma
        port_number='' if port_number is None else remove_extra_spaces(port_number))]

    db_session.commit()

    return host
Exemplo n.º 20
0
def r_delete(ids):
    ret = dict()
    ret['state'] = ji.Common.exchange_state(20000)

    config = Config()
    config.id = 1
    config.get()

    # 取全部活着的 hosts
    available_hosts = Host.get_available_hosts(nonrandom=None)

    if available_hosts.__len__() == 0:
        ret['state'] = ji.Common.exchange_state(50351)
        return ret

    chosen_host = available_hosts[0]
    node_id = chosen_host['node_id']

    os_template_image = OSTemplateImage()

    # TODO: 加入对,是否有被 Guest 引用的判断
    for _id in ids.split(','):
        os_template_image.id = _id
        os_template_image.get()

    for _id in ids.split(','):
        os_template_image.id = _id
        os_template_image.get()

        # 暂时不支持从计算节点上,删除公共镜像
        if os_template_image.kind == OSTemplateImageKind.public.value:
            os_template_image.delete()
            continue

        elif os_template_image.kind == OSTemplateImageKind.custom.value:
            os_template_image.progress = 254

            message = {
                '_object': 'os_template_image',
                'action': 'delete',
                'storage_mode': config.storage_mode,
                'dfs_volume': config.dfs_volume,
                'template_path': os_template_image.path,
                # uuid 这里没有实际意义,仅仅是为了迁就 JimV-C 的个命令格式
                'uuid': None,
                'node_id': node_id,
                'os_template_image_id': os_template_image.id,
                'passback_parameters': {
                    'id': os_template_image.id
                }
            }

            Utils.emit_instruction(message=json.dumps(message))

            os_template_image.update()

    return ret
Exemplo n.º 21
0
def query_module_config(request):
    '''
    |参数名|必选|类型|说明|样例|
    |:--------    |:---|:------|:-----  |-------   |
    |length |是  |int |分页长度   |‘10’|
    |app_id |是  |int |业务ID   |‘10’|
    |offset |是  |int |分页起始位置 |‘10’|
    |value |否  |string |搜索关键词 |‘data’|
    '''
    form = QueryForm(request.GET)
    if not form.is_valid():
        return render_json({"data": [], "total_count": 0, "result": False})
    q = Q()
    value = form.cleaned_data['value']
    if value:
        q = (Q(ip__contains=value) | Q(paths__contains=value)
             | Q(module__contains=value))

    sort = form.cleaned_data['sort']
    if not sort:
        sort = "-id"

    start = form.cleaned_data['offset']
    end = start + form.cleaned_data['length']

    host_list = Host.objects.select_related().filter(
        q, app_id=form.cleaned_data["app_id"]).order_by(sort, '-id')

    for host_check in host_list:
        if host_check.status in [
                int(HostStatus.COLLECTING),
                int(HostStatus.SUCCESS),
                int(HostStatus.STATUS_EXCEPTION)
        ]:
            if host_check.is_get_status:
                continue
            host_check.is_get_status = True
            host_check.save()
            query_host_result(form.cleaned_data['app_id'], host_check,
                              request.COOKIES.get('bk_token', ''))
    bk_token = request.COOKIES['bk_token']
    hosts = get_ip_by_app(form.cleaned_data["app_id"], bk_token)

    module_ip_count_list = {}
    modules = list(
        set(get_modules_by_app(form.cleaned_data["app_id"], bk_token)))
    for module in modules:
        ip_data = get_ip_by_module(hosts, module)
        module_ip_count_list[module] = len(ip_data)
    module_list = Host.to_module_json(host_list, module_ip_count_list)
    total = len(module_list)
    data = {
        "result": True,
        "total_count": total,
        "data": module_list[start:end]
    }
    return render_json(data)
def main():
    nessus = authenticate()
    session = Session()

    for report in nessus.reports:
        if report.status != 'completed':
            continue

        if session.query(exists().where(Report.uuid==report.uuid)).scalar():
            continue

        r = Report(
            name=report.name,
            uuid=report.uuid,
            time=datetime.fromtimestamp(report.timestamp)
        )
        session.add(r)

        for host in report.hosts:
            h = Host(
                hostname=host.hostname,
                info=host.info['count'],
                low=host.low['count'],
                med=host.med['count'],
                high=host.high['count'],
                crit=host.critical['count'],
                cpe=host.cpe.replace('The remote operating system matched the following CPE\'s : \n\n  ', '')
            )

            h.report = r
            session.add(h)

        for vuln in report.vulns:
            v = Vuln(
                name=vuln.name,
                family=vuln.family,
                severity=vuln.severity,
                plugin=vuln.plugin_id,
                hosts_affected=len(report.hosts_affected_by(vuln)),
            )
            v.report = r
            session.add(v)

    session.commit()
Exemplo n.º 23
0
def r_resize(uuid, size):

    args_rules = [
        Rules.UUID.value,
        Rules.DISK_SIZE_STR.value
    ]

    try:
        ji.Check.previewing(args_rules, {'uuid': uuid, 'size': size})

        disk = Disk()
        disk.uuid = uuid
        disk.get_by('uuid')

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)

        if disk.size >= int(size):
            ret['state'] = ji.Common.exchange_state(41257)
            return ret

        config = Config()
        config.id = 1
        config.get()

        disk.size = int(size)
        disk.quota(config=config)
        # 将在事件返回层(models/event_processor.py:224 附近),更新数据库中 disk 对象

        message = {
            '_object': 'disk',
            'action': 'resize',
            'uuid': disk.uuid,
            'guest_uuid': disk.guest_uuid,
            'storage_mode': config.storage_mode,
            'size': disk.size,
            'dfs_volume': config.dfs_volume,
            'node_id': disk.node_id,
            'image_path': disk.path,
            'disks': [disk.__dict__],
            'passback_parameters': {'size': disk.size}
        }

        if config.storage_mode in [StorageMode.shared_mount.value, StorageMode.ceph.value,
                                   StorageMode.glusterfs.value]:
            message['node_id'] = Host.get_lightest_host()['node_id']

        if disk.guest_uuid.__len__() == 36:
            message['device_node'] = dev_table[disk.sequence]

        Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 24
0
def host_add(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        ip = request.POST.get('ip')
        netmask = request.POST.get('netmask')
        gateway = request.POST.get('gateway')
        disk = request.POST.get('disk')
        ip_range = request.POST.get('ip_range')
        print name, ip, netmask, gateway, disk

        host = Host(name=name,
                    ip=ip,
                    netmask=netmask,
                    gateway=gateway,
                    disk_used=disk,
                    ip_range=ip_range)
        host.save()
        return HttpResponse('添加成功')
    return render_to_response('host_add.html', locals())
Exemplo n.º 25
0
def r_delete(uuids):

    args_rules = [
        Rules.UUIDS.value
    ]

    try:
        ji.Check.previewing(args_rules, {'uuids': uuids})

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)

        disk = Disk()

        # 检测所指定的 UUDIs 磁盘都存在
        for uuid in uuids.split(','):
            disk.uuid = uuid
            disk.get_by('uuid')

            # 判断磁盘是否与虚拟机处于离状态
            if disk.state not in [DiskState.idle.value, DiskState.dirty.value]:
                ret['state'] = ji.Common.exchange_state(41256)
                return ret

        config = Config()
        config.id = 1
        config.get()

        # 执行删除操作
        for uuid in uuids.split(','):
            disk.uuid = uuid
            disk.get_by('uuid')

            message = {
                '_object': 'disk',
                'action': 'delete',
                'uuid': disk.uuid,
                'storage_mode': config.storage_mode,
                'dfs_volume': config.dfs_volume,
                'node_id': disk.node_id,
                'image_path': disk.path
            }

            if config.storage_mode in [StorageMode.shared_mount.value, StorageMode.ceph.value,
                                       StorageMode.glusterfs.value]:
                message['node_id'] = Host.get_lightest_host()['node_id']

            Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 26
0
def new_host():
    """
    Add a new album
    """
    form = HostForm(request.form)
    if request.method == 'POST' and form.validate():
        # save the album
        host = Host()
        save_host(host, form, new=True)
        flash('Host created successfully!')
        return redirect('/')
    return render_template('new_host.html', form=form)
Exemplo n.º 27
0
def add_domain(url):
    """
    Add a domain to cachebrowser

    :param url:
    :return:
    """

    host = _parse_url(url)

    # If already exists then skip
    try:
        Host.get(url=host)
        logging.info("Domain %s is already active, skipping add request" % host)
        return host
    except Host.DoesNotExist:
        pass

    if not bootstrapper.bootstrap_host(host):
        return

    return host
Exemplo n.º 28
0
def check_add():
    host_type = request.form.get('type')
    cluster = request.form.get('cluster')
    hosts_temp = request.form.get('hostname')
    add_hosts = hosts_temp.split('|')
    for hostname in add_hosts:
        add_host = Host(type=host_type, cluster=cluster, hostname=hostname)
        db.session.add(add_host)
    try:
        db.session.commit()
        return "success"
    except:
        return "fail"
Exemplo n.º 29
0
    def post(self):
        form = HostForm(request.form)
        if form.validate_on_submit():
            host, user = request.form['host'], request.form['user']
            pwd, port = request.form['password'], request.form['port']
            # 检查主机是否已存在
            if Host.filter(host):
                flash('主机已存在!')
            else:
                # 测试密码是否正确,是否能连接成功
                result = Host.test_connect(host, user, pwd, port)
                if result['code'] == 0:
                    password = encrypt_oracle(pwd)
                    host_obj = Host(addr=host, name=user, password=password, port=port)
                    db.session.add(host_obj)
                    db.session.commit()

                    return redirect(url_for('index'))
                else:
                    flash('添加失败,请检查参数是否正确!')

        return render_template('order.html', form=form)
Exemplo n.º 30
0
def do_run_command(host, command, uid):
    try:
        host_obj = Host.filter(host)[0]
        password = decrypt_oralce(host_obj.password)  # 解密

        # print(host_obj.addr, host_obj.port, host_obj.name, password)
        ssh.connect(hostname=host_obj.addr,
                    port=host_obj.port,
                    username=host_obj.name,
                    password=password)  # 连接服务器
        stdin, stdout, stderr = ssh.exec_command(command)

        channel = stdout.channel
        pending = err_pending = None

        while not channel.closed or channel.recv_ready(
        ) or channel.recv_stderr_ready():
            readq, _, _ = select.select([channel], [], [], 1)
            for c in readq:
                # 有标准输出
                if c.recv_ready():
                    chunk = c.recv(len(c.in_buffer))
                    if pending is not None:
                        chunk = pending + chunk
                    lines = chunk.splitlines()
                    if lines and lines[-1] and lines[-1][-1] == chunk[-1]:
                        pending = lines.pop()
                    else:
                        pending = None

                    [push_log(line.decode(), uid) for line in lines]

                # 有标准错误输出
                if c.recv_stderr_ready():
                    chunk = c.recv_stderr(len(c.in_stderr_buffer))
                    if err_pending is not None:
                        chunk = err_pending + chunk
                    lines = chunk.splitlines()
                    if lines and lines[-1] and lines[-1][-1] == chunk[-1]:
                        err_pending = lines.pop()
                    else:
                        err_pending = None

                    [push_log(line.decode(), uid) for line in lines]

    except Exception as e:
        logger.error("远程连接发生错误:%s" % e)
        print("远程连接发生错误:%s" % e)
    finally:
        logger.info("远程连接关闭:%s" % ssh)
        ssh.close()
Exemplo n.º 31
0
def create_or_update_host(db_session,
                          hostname,
                          region_id,
                          location,
                          roles,
                          software_profile_id,
                          connection_type,
                          host_or_ip,
                          username,
                          password,
                          enable_password,
                          port_number,
                          jump_host_id,
                          created_by,
                          host=None):

    hostname = check_acceptable_string(hostname)
    """ Create a new host in the Database """
    if host is None:
        host = Host(created_by=created_by)
        host.inventory_job.append(InventoryJob())
        host.context.append(HostContext())
        db_session.add(host)

    host.hostname = hostname
    host.region_id = region_id if region_id > 0 else None
    host.software_profile_id = software_profile_id if software_profile_id > 0 else None
    host.location = '' if location is None else remove_extra_spaces(location)
    host.roles = '' if roles is None else remove_extra_spaces(roles)
    host.connection_param = [
        ConnectionParam(
            # could have multiple IPs, separated by comma
            host_or_ip=''
            if host_or_ip is None else remove_extra_spaces(host_or_ip),
            username='' if username is None else remove_extra_spaces(username),
            password='' if password is None else remove_extra_spaces(password),
            enable_password='' if enable_password is None else
            remove_extra_spaces(enable_password),
            jump_host_id=jump_host_id if jump_host_id > 0 else None,
            connection_type=connection_type,
            # could have multiple ports, separated by comma
            port_number=''
            if port_number is None else remove_extra_spaces(port_number))
    ]

    db_session.commit()

    return host
Exemplo n.º 32
0
    def _start_upstream(self):
        self._buffer.seek(0)
        firstline = self._buffer.readline()
        match = re.match("(?:CONNECT) ([^:]+)(?:[:](\d+))? \w+", firstline)
        if match is None:
            return

        host = match.group(1)
        port = int(match.group(2) or 443)

        cachebrowsed = False
        try:
            Host.get(url=host)
            cachebrowsed = True
        except Host.DoesNotExist:
            pass

        if cachebrowsed:
            logging.info("[HTTPS] %s:%s  <REJECTED>" % (host, port))
            self.connection.close_local()
        else:
            logging.info("[HTTPS] %s:%s  <PROXYING>" % (host, port))
            return self._connect_upstream(host, port)
async def get_new_host_for_file(file):
    for hoster in Host.get_queryset_for_uploading_file(file):
        with contextlib.suppress(aiohttp.ClientConnectorError,
                                 asyncio.TimeoutError):
            async with aiohttp.ClientSession() as session:
                async with session.get(f'http://{hoster.ip}/free-space/',
                                       timeout=1) as resp:
                    if resp.status == 200:
                        resp_data = await resp.json()
                        space = resp_data.get('data').get('result')
                        if space >= file.size:
                            return hoster
    else:
        return None
Exemplo n.º 34
0
 def send(self, local_path, remote_path, host_list):
     """
     上传文件到远程主机
     :param local_path: 本地文件路径
     :param remote_path: 远程主机路径
     :param host_list: 主机列表
     :return:
     """
     for host in host_list:
         host_obj = Host.filter(host)[0]  # 获取主机对象
         password = decrypt_oralce(host_obj.password)  # 解密
         conn = LazyFileconnection(host_obj.addr, host_obj.name, password,
                                   host_obj.port)
         with conn as s:
             s.put(local_path, remote_path)
Exemplo n.º 35
0
def r_nonrandom(hosts_name, random):

    args_rules = [Rules.HOSTS_NAME.value]

    try:
        ji.Check.previewing(args_rules, {args_rules[0][1]: hosts_name})

        if str(random).lower() in ['false', '0']:
            random = False

        else:
            random = True

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)

        Host.set_allocation_mode(hosts_name=hosts_name.split(','),
                                 random=random)

        ret['data'] = Host.get_all()
        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 36
0
def create_host():
    if request.methods == 'POST':
        ip = (request.json['ip'])
    
        try:
            host = Host.query.filter(Host.ip == ip).first()
            if host is not None:
                return jsonify({'success': False, "error": "this host already exists with id " + host.id})
            host = Host(ip=ip)
            db.session.add(host)
            db.session.commit()
            return jsonify({'success': True, "error": null})
        except:
            error ='ip upload error'
            return jsonify({'success': False, "error": error})
Exemplo n.º 37
0
 def setUp(self):
     self.client = Client()
     
     self.user = User.objects.create_user('user', '*****@*****.**', 'userpassword')
     self.client.login(username='******', password='******')
     
     self.other_user = User.objects.create_user('other', '*****@*****.**', 'otherpassword')
     
     self.host = Host(name="Host", description="Description",
                      ipv4='1.2.3.4', ipv6='2002:0:0:0:0:0:c22:384e',
                      user=self.user)
     self.host.save()
     
     self.net = Network(name="Net", description="Description", user=self.user)
     self.net.save()
Exemplo n.º 38
0
 def get(self, local_path, remote_path, host_list):
     """
     从远程主机下载文件
     :param local_path: 本地文件路径
     :param remote_path: 远程主机路径
     :param host_list: 主机列表
     :return:
     """
     for host in host_list:
         host_obj = Host.filter(host)[0]  # 获取主机对象
         password = decrypt_oralce(host_obj.password)  # 解密
         conn = LazyFileconnection(host_obj.addr, host_obj.name, password,
                                   host_obj.port)
         with conn as s:
             s.get(remote_path, local_path)  # 将remove_path 下载到本地 local_path
Exemplo n.º 39
0
def query_host(request):
    """
    @summary:主机管理分页
    """
    form = QueryForm(request.GET)
    if not form.is_valid():
        return render_json({"data": [], "total_count": 0, "status": False})
    q = Q()
    value = form.cleaned_data['value']
    if value:
        q = (Q(ip__contains=value) | Q(paths__contains=value))

    sort = form.cleaned_data['sort']
    if not sort:
        sort = "-id"

    start = form.cleaned_data['offset']
    end = start + form.cleaned_data['length']

    host_list_check = (Host.objects.filter(
        q, app_id=form.cleaned_data["app_id"]).order_by(sort,
                                                        '-id')[start:end])
    host_list = Host.objects.filter(
        q, app_id=form.cleaned_data["app_id"]).order_by(sort, '-id')

    for host_check in host_list_check:
        if host_check.status in [
                int(HostStatus.COLLECTING),
                int(HostStatus.SUCCESS),
                int(HostStatus.STATUS_EXCEPTION)
        ]:
            if host_check.is_get_status:
                continue
            host_check.is_get_status = True
            host_check.save()
            query_host_result(form.cleaned_data['app_id'], host_check,
                              request.COOKIES.get('bk_token', ''))

    total = host_list.count()

    host_json = Host.to_json(host_list[start:end])

    return render_json({
        "status": True,
        'data': host_json,
        'total_count': total,
    })
Exemplo n.º 40
0
 def host_add(self, name, groups=[]):
     # does this host exist?
     try:
         host = self.session.query(Host).filter(Host.name == name).one()
     except:
         host = Host(name=name)
         # if we have to make a host, add it to [all]
         host.groups.append(
             self.session.query(Group).filter(Group.name == 'all').one())
     if groups:
         for group in groups:
             # does the group exist? this throws an exception if the group wasnt found
             group = self.session.query(Group).filter(
                 Group.name == group).one()
             if not group in host.groups: host.groups.append(group)
     self.session.add(host)
     self.session.commit()
Exemplo n.º 41
0
def resolve_host(hostname, use_cachebrowser_db=True):
    # Check if host exists in database
    if use_cachebrowser_db:
        try:
            host = Host.get(hostname)
            cdn = host.cdn

            if cdn is None:
                _bootstrap_host(host)

            addresses = cdn.addresses
            if addresses is None or len(addresses) == 0:
                _bootstrap_cdn(cdn)

            return cdn.addresses[0], True  # make it random?
        except Host.DoesNotExist:
            pass
    return _dns_request(hostname), False
Exemplo n.º 42
0
    def _start_remote(self):
        http_request = self.request_builder.http_request

        url = http_request.path
        parsed_url = urlparse.urlparse(url)
        try:
            host = Host.get(url=parsed_url.hostname)
            if host.ssl:
                url = url.replace('http', 'https')
            self.cachebrowsed = True
        except Host.DoesNotExist:
            pass

        logging.info("[%s] %s %s" % (http_request.method, url, '<CACHEBROWSED>' if self.cachebrowsed else ''))
        request = http_request.get_raw()
        # request = re.sub(r'^(GET|POST|PUT|DELETE|HEAD) http[s]?://[^/]+/(.+) (\w+)', r'\1 /\2 \3', request)
        response = http.request(url, raw_request=request)

        self._connection.start_remote(response)
Exemplo n.º 43
0
def r_get_by_filter():

    try:
        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)
        ret['data'] = list()
        for k, v in db.r.hgetall(app.config['hosts_info']).items():
            v = json.loads(v)
            v = Host.alive_check(v)
            v['node_id'] = k
            ret['data'].append(v)

        if ret['data'].__len__() > 1:
            ret['data'].sort(key=lambda _k: _k['boot_time'])

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 44
0
def r_content_search():
    keyword = request.args.get('keyword', '')

    args_rules = [Rules.KEYWORD.value]

    try:
        ji.Check.previewing(args_rules, {'keyword': keyword})

        ret = dict()
        ret['state'] = ji.Common.exchange_state(20000)
        ret['data'] = list()

        for host in Host.get_all():
            if -1 != host['hostname'].find(keyword):
                ret['data'].append(host)

        return ret

    except ji.PreviewingError, e:
        return json.loads(e.message)
Exemplo n.º 45
0
    def get_available_hosts():

        from models import Host

        hosts = list()

        for k, v in db.r.hgetall(app.config['hosts_info']).items():
            v = json.loads(v)

            v = Host.alive_check(v)

            if not v['alive']:
                continue

            v['system_load_per_cpu'] = float(v['system_load'][0]) / v['cpu']
            hosts.append(v)

        hosts.sort(key=lambda _k: _k['system_load_per_cpu'])

        return hosts
Exemplo n.º 46
0
def add():
    if request.method == "POST":
        try:
            if request.form['a_servername']:
                a_servername = request.form.get('a_servername').strip()
                if servername_check(a_servername) is not None:
                    return jsonify("Failed, Server Name exists in database.")
            if request.form['a_ip']:
                a_ip = request.form.get('a_ip').strip()
            if request.form['a_port']:
                a_port = request.form.get('a_port').strip()
            if request.form['a_username']:
                a_username = request.form.get('a_username').strip()
            if request.form['a_password']:
                a_password = encrypt(request.form.get('a_password').strip())
            if request.form['a_servername']:
                a_servername = request.form.get('a_servername').strip()
            if request.form['a_comment']:
                a_comment = request.form.get('a_comment').strip()
            if request.form['a_servergroup']:
                a_servergroup = request.form.get('a_servergroup').strip()
            server = Host(servername=a_servername,
                          ip=a_ip,
                          port=a_port,
                          group=a_servergroup)
            session.add(server)
            session.commit()
            passwd = Passwd(username=a_username,
                            password=a_password,
                            host=server,
                            comment=a_comment)
            session.add(passwd)
            session.commit()

        except Exception, e:
            print e
            return jsonify(
                "Added failed, make sure you include the right values.")
        return jsonify("Added successfully")
Exemplo n.º 47
0
def host_list(request, page=None):
    search_phrase = request.GET.get('s')
    if search_phrase and search != None:
        hosts = search(Host, search_phrase)
    else:
        hosts = Host.shared_objects(request.user)
        
    paginator = Paginator(list(hosts), 10)
    
    page = page or request.GET.get('page', 1)
    try:
        hosts = paginator.page(page)
    except PageNotAnInteger:
        hosts = paginator.page(1)
    except EmptyPage:
        hosts = paginator.page(paginator.num_pages)

    extra_context = {
        'hosts': hosts,
        'url': reverse('host_list')
    }
    return direct_to_template(request, 'networks/host_list.html',
                              extra_context=extra_context)
Exemplo n.º 48
0
def api_import_hosts():
    importable_header = [HEADER_FIELD_HOSTNAME, HEADER_FIELD_REGION, HEADER_FIELD_ROLES, HEADER_FIELD_IP,
                         HEADER_FIELD_USERNAME, HEADER_FIELD_PASSWORD, HEADER_FIELD_CONNECTION, HEADER_FIELD_PORT]
    region_id = request.form['region']
    data_list = request.form['data_list']

    db_session = DBSession()
    selected_region = get_region_by_id(db_session, region_id)
    if selected_region is None:
        return jsonify({'status': 'Region is no longer exists in the database.'})

    # Check mandatory data fields
    error = []
    reader = csv.reader(data_list.splitlines(), delimiter=',')
    header_row = next(reader)

    if HEADER_FIELD_HOSTNAME not in header_row:
        error.append('"hostname" is missing in the header.')

    if HEADER_FIELD_IP not in header_row:
        error.append('"ip" is missing in the header.')

    if HEADER_FIELD_CONNECTION not in header_row:
        error.append('"connection" is missing in the header.')

    for header_field in header_row:
        if header_field not in importable_header:
            error.append('"' + header_field + '" is not a correct header field.')

    if error:
        return jsonify({'status': ','.join(error)})

    # Check if each row has the same number of data fields as the header
    error = []
    data_list = list(reader)

    row = 2
    COLUMN_CONNECTION = get_column_number(header_row, HEADER_FIELD_CONNECTION)
    COLUMN_REGION = get_column_number(header_row, HEADER_FIELD_REGION)

    for row_data in data_list:
        if len(row_data) > 0:
            if len(row_data) != len(header_row):
                error.append('line %d has wrong number of data fields.' % row)
            else:
                if COLUMN_CONNECTION >= 0:
                    # Validate the connection type
                    data_field = row_data[COLUMN_CONNECTION]
                    if data_field != ConnectionType.TELNET and data_field != ConnectionType.SSH:
                        error.append('line %d has a wrong connection type (should either be "telnet" or "ssh").' % row)
                if COLUMN_REGION >= 0:
                    # Create a region if necessary
                    data_field = get_acceptable_string(row_data[COLUMN_REGION])
                    region = get_region(db_session, data_field)
                    if region is None and data_field:
                        try:
                            db_session.add(Region(name=data_field,
                                                  created_by=current_user.username))
                            db_session.commit()
                        except Exception:
                            db_session.rollback()
                            error.append('Unable to create region %s.' % data_field)

        row += 1

    if error:
        return jsonify({'status': ','.join(error)})

    # Import the data
    error = []
    im_regions = {}

    for data in data_list:
        if len(data) == 0:
            continue

        db_host = None
        im_host = Host()
        im_host.region_id = selected_region.id
        im_host.created_by = current_user.username
        im_host.inventory_job.append(InventoryJob())
        im_host.context.append(HostContext())
        im_host.connection_param.append(ConnectionParam())
        im_host.connection_param[0].username = ''
        im_host.connection_param[0].password = ''
        im_host.connection_param[0].port_number = ''

        for column in range(len(header_row)):

            header_field = header_row[column]
            data_field = data[column].strip()

            if header_field == HEADER_FIELD_HOSTNAME:
                hostname = get_acceptable_string(data_field)
                db_host = get_host(db_session, hostname)
                im_host.hostname = hostname
            elif header_field == HEADER_FIELD_REGION:
                region_name = get_acceptable_string(data_field)
                if region_name in im_regions:
                    im_host.region_id = im_regions[region_name]
                else:
                    region = get_region(db_session, region_name)
                    if region is not None:
                        im_host.region_id = region.id
                        # Saved for later lookup
                        im_regions[region_name] = region.id
            elif header_field == HEADER_FIELD_ROLES:
                im_host.roles = remove_extra_spaces(data_field)
            elif header_field == HEADER_FIELD_IP:
                im_host.connection_param[0].host_or_ip = remove_extra_spaces(data_field)
            elif header_field == HEADER_FIELD_USERNAME:
                username = get_acceptable_string(data_field)
                im_host.connection_param[0].username = username
            elif header_field == HEADER_FIELD_PASSWORD:
                im_host.connection_param[0].password = data_field
            elif header_field == HEADER_FIELD_CONNECTION:
                im_host.connection_param[0].connection_type = data_field
            elif header_field == HEADER_FIELD_PORT:
                im_host.connection_param[0].port_number = remove_extra_spaces(data_field)

        # Import host already exists in the database, just update it
        if db_host is not None:
            db_host.created_by = im_host.created_by
            db_host.region_id = im_host.region_id

            if HEADER_FIELD_ROLES in header_row:
                db_host.roles = im_host.roles

            if HEADER_FIELD_IP in header_row:
                db_host.connection_param[0].host_or_ip = im_host.connection_param[0].host_or_ip

            if HEADER_FIELD_USERNAME in header_row:
                db_host.connection_param[0].username = im_host.connection_param[0].username

            if HEADER_FIELD_PASSWORD in header_row:
                db_host.connection_param[0].password = im_host.connection_param[0].password

            if HEADER_FIELD_CONNECTION in header_row:
                db_host.connection_param[0].connection_type = im_host.connection_param[0].connection_type

            if HEADER_FIELD_PORT in header_row:
                db_host.connection_param[0].port_number = im_host.connection_param[0].port_number
        else:
            # Add the import host
            db_session.add(im_host)

    if error:
        return jsonify({'status': error})
    else:
        db_session.commit()
        return jsonify({'status': 'OK'})
Exemplo n.º 49
0
def collectjson(request):
    req = request
    if req.method == "POST":
        jsonobj = json.loads(req.body)
        try:
            host = Host.objects.get(identity=jsonobj['identity'])
        except:
            host = Host()
        try:
            host.identity = jsonobj['identity']
            host.hostname = jsonobj['hostname']
            host.product = jsonobj['product']
            host.cpu_num = jsonobj['cpu_num']
            host.cpu_model = jsonobj['cpu_model']
            host.memory = jsonobj['memory']
            host.sn = jsonobj['sn']
            host.osver = jsonobj['osver']
            host.vendor = jsonobj['vendor']
            host.ipaddr = jsonobj['ip']
            host.save()
            return HttpResponse(json.dumps({'status':0,'message':"ok"}))
        except Exception, e:
            return HttpResponse(json.dumps({'status':-1,'message':str(e)}))
Exemplo n.º 50
0
class UserAccessTest(TestCase):
    """Tests for user access and sharing objects"""
    
    def setUp(self):
        self.client = Client()
        
        self.user = User.objects.create_user('user', '*****@*****.**', 'userpassword')
        self.client.login(username='******', password='******')
        
        self.other_user = User.objects.create_user('other', '*****@*****.**', 'otherpassword')
        
        self.host = Host(name="Host", description="Description",
                         ipv4='1.2.3.4', ipv6='2002:0:0:0:0:0:c22:384e',
                         user=self.user)
        self.host.save()
        
        self.net = Network(name="Net", description="Description", user=self.user)
        self.net.save()
        
    def test_user_host_access(self):
        access = user_has_access(self.host, self.user)
        self.assertEqual(access, True)
        
        access = user_has_access(self.host, self.other_user)
        self.assertEqual(access, False)
        
    def test_user_host_share(self):
        grant_access(self.host, self.other_user)
        access = user_has_access(self.host, self.other_user)
        self.assertEqual(access, True)
        
        access = user_can_edit(self.host, self.other_user)
        self.assertEqual(access, True)
        
        revoke_edit(self.host, self.other_user)
        access = user_can_edit(self.host, self.other_user)
        self.assertEqual(access, False)
        
        revoke_access(self.host, self.other_user)
        access = user_has_access(self.host, self.other_user)
        self.assertEqual(access, False)
        
    def test_user_network_access(self):
        access = user_has_access(self.net, self.user)
        self.assertEqual(access, True)
        
        access = user_has_access(self.net, self.other_user)
        self.assertEqual(access, False)
        
    def test_user_network_share(self):
        grant_access(self.net, self.other_user)
        access = user_has_access(self.net, self.other_user)
        self.assertEqual(access, True)
        
        access = user_can_edit(self.net, self.other_user)
        self.assertEqual(access, True)
        
        revoke_edit(self.net, self.other_user)
        access = user_can_edit(self.net, self.other_user)
        self.assertEqual(access, False)
        
        revoke_access(self.net, self.other_user)
        access = user_has_access(self.net, self.other_user)
        self.assertEqual(access, False)
Exemplo n.º 51
0
def collect(request):
    req = request
    if req.POST:
        vendor = req.POST.get('vendor')
        product = req.POST.get('product')
        cpu_model = req.POST.get('cpu_model')
        cpu_num = req.POST.get('cpu_num')
        memory = req.POST.get('memory')
        sn = req.POST.get('sn')
        osver = req.POST.get('osver')
        hostname = req.POST.get('hostname')
        try:
            host = Host.objects.get(hostname=hostname)
        except:
            host = Host()
        host.hostname=hostname
        host.product=product
        host.cpu_num=int(cpu_num)
        host.cpu_model=cpu_model
        host.memory = int(memory)
        host.sn = sn
        host.osver = osver
        host.vendor = vendor
        host.ipaddr = req.POST.get('ip')
        host.save()
        return HttpResponse("ok")
    else:
        return HttpResponse("no post data")
Exemplo n.º 52
0
def collect(request):
    req = request
    if req.POST:
        vendor = req.POST.get('vendor')
        product = req.POST.get('product')
        cpu_model = req.POST.get('cpu_model')
        cpu_num = req.POST.get('cpu_num')
        memory = req.POST.get('memory')
        sn = req.POST.get('sn')
        osver = req.POST.get('osver')
        hostname = req.POST.get('hostname')
        try:
            host = Host.objects.get(hostname=hostname)
        except:
            host = Host()
        ipaddrs = req.POST.get('ipaddrs')
        identity = req.POST.get('identity')
        try:
            host = Host.objects.get(identity=identity)
        except:
            host = Host()
        host.hostname = hostname
        host.product = product
        host.cpu_num = int(cpu_num)
        host.cpu_model = cpu_model
        host.memory = int(memory)
        host.sn = sn
        host.osver = osver
        host.vendor = vendor
        host.save()
        for ip in ipaddrs.split(';'):
            o_ip = Ipaddr()
            o_ip.ipaddr = ip
            o_ip.host = host
            o_ip.save()
        return HttpResponse("post data successfully!")
    else:
        return HttpResponse("no any post data!")
Exemplo n.º 53
0
def collect(request):
    asset_info = json.loads(request.body)
    if request.method == 'POST':
        vendor = asset_info['vendor']
        # group = asset_info['group']
        disk = asset_info['disk']
        cpu_model = asset_info['cpu_model']
        cpu_num = asset_info['cpu_num']
        memory = asset_info['memory']
        sn = asset_info['sn']
        osver = asset_info['osver']
        hostname = asset_info['hostname']
        ip = asset_info['ip']
        # asset_type = ""
        # status = ""
        try:
            host = Host.objects.get(hostname=hostname)
        except Exception as msg:
            print(msg)
            host = Host()
            level = get_dir("log_level")
            ssh_pwd = get_dir("ssh_pwd")
            log_path = get_dir("log_path")
            log("cmdb.log", level, log_path)
            logging.info("==========sshkey deploy start==========")
            data = deploy_key(ip, ssh_pwd)
            logging.info(data)
            logging.info("==========sshkey deploy end==========")

        # if req.POST.get('identity'):
        #     identity = req.POST.get('identity')
        #     try:
        #         host = Host.objects.get(identity=identity)
        #     except:
        #         host = Host()
        host.hostname = hostname
        # host.group = group
        host.cpu_num = int(cpu_num)
        host.cpu_model = cpu_model
        host.memory = int(memory)
        host.sn = sn
        host.disk = disk
        host.os = osver
        host.vendor = vendor
        host.ip = ip
        # host.asset_type = asset_type
        # host.status = status
        host.save()
        return HttpResponse("Post asset data to server successfully!")
    else:
        return HttpResponse("No any post data!")
Exemplo n.º 54
0
def collect(request):
    req = request
    if req.POST:
        vendor = req.POST.get('vendor')
        product = req.POST.get('product')
        sn = req.POST.get('sn')
        hostname = req.POST.get('hostname')
        osbit = req.POST.get('osbit')
        osver = req.POST.get('osver')
        cpu_model = req.POST.get('cpu_model') 
        cpucores = req.POST.get('cpucores')
        cpu_num = req.POST.get('cpu_num')
        memory = req.POST.get('memory')
        ipaddr = req.POST.get('ip')
        #ipaddrs = req.POST.get('ipaddr')
        identity = req.POST.get('identity')
        try:
            #host = Host.objects.get(hostname=hostname)
            host = Host.objects.get(identity=identity)
        except:
            host = Host()
        host.identity = identity
        host.vendor = vendor
        host.product = product
        host.sn = sn
        host.hostname = hostname 
        host.osbit = osbit
        host.osver = osver
        host.cpumodel = cpu_model
        host.cpucores = cpucores
        host.cpunum = cpu_num
        host.memory = memory
        host.ipaddr = ipaddr
        host.save()
        
        return HttpResponse("ok")
    else:
        return HttpResponse("no post data")
Exemplo n.º 55
0
def collect(request):
    req = request
    if req.method == 'POST':
        vender = req.POST.get('vender')
        product = req.POST.get('product')
        cpu_model = req.POST.get('cpu_model')
        cpu_num = req.POST.get('cpu_num')
        memory = req.POST.get('memory')
        sn = req.POST.get('sn')
        osver = req.POST.get('osver')
        hostname = req.POST.get('hostname')
        ipaddrs = req.POST.get('ipaddrs')

        host = Host()
        host.vender = vender
        host.product = product
        host.cpu_model = cpu_model
        host.cpu_num = int(cpu_num)
        host.memory = int(memory)
        host.sn = sn
        host.osver = osver
        host.hostname = hostname

        host.save()

        for ip in ipaddrs.split(':'):
            o_ip = IpAddr()
            o_ip.ipaddr = ip
            o_ip.host = host
            o_ip.save()

        return HttpResponse('OK')
    else:
        return HttpResponse('No post data')
Exemplo n.º 56
0
def gethosts(request):
    if request.method == 'POST':
        data = json.loads(request)
        host = Host()
        host.vender = data['verder']
        host.product = data['product']
        host.cpu_model = data['cpu_model']
        host.cpu_num = data['cpu_num']
        host.memory = data['memory']
        host.sn = data['sn']
        host.osver = data['osver']
        host.hostname = data['hostname']

        host.save()

        ipaddrs = data['ipaddrs']
        for ip in ipaddrs.split(':'):
            o_ip = IpAddr()
            o_ip.ipaddr = ip
            o_ip.host = host
            o_ip.save()
    else:
        return HttpResponse('No post data')
Exemplo n.º 57
0
    def consume(self, commandstr):
        """
        Read and act upon a line of lobby protocol.

        Only the following commands are implemented:
          ADDUSER
          BATTLECLOSED
          BATTLEOPENED
          CLIENTSTATUS
          JOINEDBATTLE
          LEFTBATTLE
          REMOVEUSER
          UPDATEBATTLEINFO
        """
        if commandstr.startswith("ADDUSER"):
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#ADDUSER:server
            # ADDUSER userName country cpu [accountID]
            try:
                self.users[commandstr.split()[1]] = User(*commandstr.split()[1:])
            except ValueError:
                logger.exception("Bad format, commandstr: '%s'", repr(commandstr))
                return
            except Exception:
                logger.exception("Exception, commandstr: '%s'", repr(commandstr))
                return
        elif commandstr.startswith("BATTLECLOSED"):
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#BATTLECLOSED:server
            # BATTLECLOSED battleID
            try:
                battleID = commandstr.split()[1]
            except Exception:
                logger.exception("Commandstr: '%s'", repr(commandstr))
                return
            # founder is founder no more
            try:
                self.hosts[battleID].user.host = None
            except:
                logger.exception("error removing host-user-founder association")
            # remove battle from all host lists
            for hosts in [self.hosts, self.hosts_ingame, self.hosts_open]:
                try:
                    del hosts[battleID]
                except:
                    pass
        elif commandstr.startswith("BATTLEOPENED"):
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#BATTLEOPENED:server
            # BATTLEOPENED battleID type natType founder ip port maxPlayers passworded rank mapHash {engineName} \
            # {engineVersion} {map} {title} {gameName}
            try:
                cmd, engineVersion, _map, title, gameName = commandstr.split("\t")
                _, battleID, _type, natType, founder, ip, port, maxPlayers, passworded, rank, mapHash, engineName = \
                    cmd.split()
            except ValueError:
                logger.exception("Bad format, commandstr: '%s'", repr(commandstr))
                return
            except Exception:
                logger.exception("Commandstr: '%s'", repr(commandstr))
                return
            loc = locals()
            del loc["_"]
            del loc["cmd"]
            del loc["commandstr"]
            del loc["self"]
            loc["map"] = _map
            del loc["_map"]
            loc["type"] = _type
            del loc["_type"]
            for k,v in loc.items():
                loc[k] = unicode(v, errors='ignore')
            host = Host(loc)
            self.hosts[battleID] = host
            self.hosts_open[battleID] = host
            host.user = self.users[founder]
            host.user.host = host
        elif commandstr.startswith("CLIENTSTATUS"):
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#CLIENTSTATUS:server
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#MYSTATUS:client
            # CLIENTSTATUS userName status
            # status bits: is_bot|has_access|3*rank|is_away|is_ingame
            try:
                _, userName, status = commandstr.split()
            except ValueError:
                logger.exception("Bad format, commandstr: '%s'", repr(commandstr))
                return
            except:
                logger.exception("Commandstr: '%s'", repr(commandstr))
                return
            try:
                user = self.users[userName]
            except:
                logger.exception("Exception in CLIENTSTATUS: userName '%s' in self.users?: %s, commandstr: '%s'",
                                 userName, userName in self.users, repr(commandstr))
                return
            try:
                status_bin = bin(int(status))[2:].zfill(7)
                user.is_ingame = bool(int(status_bin[6]))
                user.is_away = bool(int(status_bin[5]))
                user.rank = int(status_bin[2:5], base=2)
                user.is_moderator = bool(int(status_bin[1]))
                user.is_bot = bool(int(status_bin[0]))
            except:
                logger.exception("Exception in CLIENTSTATUS: status: '%s', status_bin: '%s', commandstr: '%s'",
                                 repr(commandstr), status, status_bin)
                return
            if user.host:
                user.host.is_ingame = user.is_ingame
                if user.is_ingame:
                    # add host to hosts_ingame
                    self.hosts_ingame[user.host.battleID] = user.host
                    # remove host from hosts_open
                    try:
                        del self.hosts_open[user.host.battleID]
                    except:
                        # CLIENTSTATUS is sent twice in case of self-hosted battles
                        pass
                else:
                    # add host to hosts_open
                    self.hosts_open[user.host.battleID] = user.host
                    # remove host from hosts_ingame
                    try:
                        del self.hosts_ingame[user.host.battleID]
                    except Exception:
                        if self.login_info_consumed:
                            logger.exception(
                                "Exception in CLIENTSTATUS: trying to remove host from hosts_ingame, commandstr: '%s'",
                                repr(commandstr))
                        else:
                            # msg flood in in wrong order during initial setup
                            pass
        elif commandstr.startswith("JOINEDBATTLE"):
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#JOINEDBATTLE:server
            # JOINEDBATTLE battleID userName [scriptPassword]
            try:
                _, battleID, userName = commandstr.split()
            except ValueError:
                logger.exception("Bad format, commandstr: '%s'", repr(commandstr))
                return
            self.hosts[battleID].user_list.append(self.users[userName])
            self.hosts[battleID].set_player_count()
        elif commandstr.startswith("LEFTBATTLE"):
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#LEFTBATTLE:server
            # LEFTBATTLE battleID userName
            try:
                _, battleID, userName = commandstr.split()
            except ValueError:
                logger.exception("Bad format, commandstr: '%s'", repr(commandstr))
                return
            try:
                self.hosts[battleID].user_list.remove(self.users[userName])
                self.hosts[battleID].set_player_count()
            except:
                logger.exception("Exception in LEFTBATTLE: userName '%s' not in self.hosts[%s].user_list?", userName,
                                 battleID)
                return
        elif commandstr.startswith("REMOVEUSER"):
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#REMOVEUSER:server
            # REMOVEUSER userName
            userName = commandstr.split()[1]
            user = self.users[userName]
            if user.host:
                try:
                    del self.hosts[user.host.battleID]
                    del self.hosts_open[user.host.battleID]
                except:
                    pass
            try:
                del self.users[userName]
            except ValueError:
                logger.exception("Bad format, commandstr: '%s'", repr(commandstr))
                return
            except KeyError:
                logger.exception("Exception in REMOVEUSER: userName '%s' not in self.users?", userName)
                return
        elif commandstr.startswith("UPDATEBATTLEINFO"):
            # http://springrts.com/dl/LobbyProtocol/ProtocolDescription.html#UPDATEBATTLEINFO:server
            # UPDATEBATTLEINFO battleID spectatorCount locked mapHash {mapName}
            try:
                battleID, spectatorCount, locked, mapHash = commandstr.split()[1:5]
                mapName = " ".join(commandstr.split()[5:])
            except ValueError:
                logger.exception("Bad format, commandstr: '%s'", repr(commandstr))
                return
            try:
                host = self.hosts[battleID]
            except KeyError:
                logger.exception("Exception in UPDATEBATTLEINFO: battleID '%s' not in self.hosts?", battleID)
                return
            host.spec_count = int(spectatorCount)
            self.hosts[battleID].set_player_count()
            host.locked = bool(locked)
            host.mapHash = mapHash
            self.map = mapName
        else:
            # ignore all other commands
            pass
Exemplo n.º 58
0
def collect(request):
    req = request
    if req.POST:
        vendor = req.POST.get('Product_Name')
        sn = req.POST.get('Serial_Number')
        product = req.POST.get('Manufacturer')
        cpu_model = req.POST.get('Model_Name')
        cpu_num = req.POST.get('Cpu_Cores')
        cpu_vendor = req.POST.get('Vendor_Id')
        memory_part_number = req.POST.get('Part_Number')
        memory_manufacturer = req.POST.get('Manufacturer')
        memory_size = req.POST.get('Size')
        device_model = req.POST.get('Device_Model')
        device_version = req.POST.get('Firmware_Version')
        device_sn = req.POST.get('Serial_Number')
        device_size = req.POST.get('User_Capacity')
        osver = req.POST.get('os_version')
        hostname = req.POST.get('os_name')
        os_release = req.POST.get('os_release')
        ipaddrs = req.POST.get('Ipaddr')
        mac = req.POST.get('Device')
        link = req.POST.get('Link')
        mask = req.POST.get('Mask')
        device = req.POST.get('Device')
 
        host = Host()
        host.hostname = hostname
        host.product = product
        host.cpu_num = cpu_num
        host.cpu_model = cpu_model
        host.cpu_vendor = cpu_vendor
        host.memory_part_number = memory_part_number
        host.memory_manufacturer = memory_manufacturer
        host.memory_size = memory_size
        host.device_model = device_model
        host.device_version = device_version
        host.device_sn = device_sn
        host.device_size = device_size
        host.osver = osver
        host.os_release = os_release
        host.vendor = vendor
        host.sn = sn
        host.ipaddr = ipaddrs 
        host.save()
       
        #for ip in ipaddrs.split(';'):
        #    o_ip = IPaddr()
        #    o_ip.ipaddr = ip
        #    o_ip.host = host
        #    o_ip.save()
          
        return HttpResponse('OK')
    else:
        return HttpResponse('no post data')