Пример #1
0
    def post(self):
        form = CreateHostForm()
        hosts = Host.query.order_by(Host.type.asc(), Host.name.asc()).all()
        if form.validate_on_submit():
            try:
                # Create database entry
                h = Host()
                h.name = form.name.data or None
                h.hostname = form.hostname.data or None
                h.region = form.region.data or None
                h.uri = form.uri.data or None
                h.active = False
                h.type = form.type.data or None
                h.username = form.username.data or None
                h.password = form.password.data or None

                db.session.add(h)
                db.session.commit()
                return redirect('/admin/hosts/')

            except:
                import traceback
                db.session.rollback()
                traceback.print_exc()
                return redirect('/admin/hosts/')

        return render_template('admin/hosts.html', form=form, hosts=hosts)
Пример #2
0
def get_executer_data(query, recursive=False, include_unattached=False):
    from app.models import Project, Datacenter, Group, Host

    host_fields = list(Host.FIELDS)
    group_fields = list(Group.FIELDS)

    if recursive:
        host_fields += ["all_tags", "all_custom_fields"]
        group_fields += ["all_tags", "all_custom_fields"]

    projects = Project.find(query)
    projects = cursor_to_list(projects)
    project_ids = [x["_id"] for x in projects]

    groups = Group.find({"project_id": {"$in": project_ids}})
    groups = cursor_to_list(groups, fields=group_fields)
    group_ids = [x["_id"] for x in groups]

    if include_unattached:
        hosts = Host.find({})
    else:
        hosts = Host.find({"group_id": {"$in": group_ids}})
    hosts = cursor_to_list(hosts, fields=host_fields)

    datacenters = Datacenter.find({})
    datacenters = cursor_to_list(datacenters)
    return {
        "datacenters": datacenters,
        "projects": projects,
        "groups": groups,
        "hosts": hosts
    }
Пример #3
0
    def get(self, jwt, host_id, page_id, host_name=None):
        data = []

        # If page_id is None do it the slow way.
        if page_id is None:
            # If no host_id is passed in get all hosts.
            if (host_id is None) and (host_name is None):
                # This is slow so lets just return an error
                # hosts = Host.get_all()
                return jsonify(
                    error=True,
                    msg=
                    "Getting all hosts is slow. Please use the pagination endpoint at ./manage_host/page/"
                )
            elif host_id:
                if Host.get_by_id(host_id):
                    hosts = [Host.get_by_id(host_id)]
                else:
                    hosts = []
            elif host_name:
                hosts = [Host.get_by_hostname(host_name)]

            # Loop over results and get json form of host to return.
            if len(hosts) > 0:
                for host in hosts:
                    temp_data = host.serialize()
                    temp_data_services = Service.get_all_by_host_name(
                        host.host_name)
                    temp_data["all_services"] = []
                    for tds in temp_data_services:
                        temp_data["all_services"].append(tds.serialize())
                    data.append(temp_data)
                    pass
                return jsonify(data=data)
            else:
                return jsonify(error=True, msg="Host does not exist.")
        else:
            per_page = 10
            totalhosts = Host.get_count()
            total_pages = floor(totalhosts / per_page)
            hosts = Host.get_by_page((page_id * per_page), per_page)

            if hosts is not None:
                for host in hosts:
                    temp_data = host.serialize()
                    temp_data_services = Service.get_all_by_host_name(
                        host.host_name)
                    temp_data["all_services"] = []
                    for tds in temp_data_services:
                        temp_data["all_services"].append(tds.serialize())
                    data.append(temp_data)

            return jsonify({
                "data": data,
                "totalhosts": totalhosts,
                "this_page": page_id,
                "more": (page_id < total_pages)
            })
Пример #4
0
 def test_update_host(self):
     self.test_create_host()
     host = Host.find_one({"fqdn": TEST_HOST_1["fqdn"]})
     payload = TEST_HOST_2
     r = self.put_json("/api/v1/hosts/%s" % host._id, payload)
     self.assertEqual(r.status_code, 200)
     host = Host.find_one({"_id": host._id})
     self.assertEqual(host.fqdn, TEST_HOST_2["fqdn"])
     self.assertEqual(host.description, TEST_HOST_2["description"])
     self.assertItemsEqual(host.tags, TEST_HOST_2["tags"])
Пример #5
0
def add_host():
    if request.json:
        host = Host()
        host.uuid = str(uuid.uuid1())
        host.name = request.json['name']
        host.ip = request.json['ip']
        host.port = request.json['port']
        host.password = request.json['password']
        host.status = request.json['status']
        host.create_time = datetime.utcnow()
        db.session.add(host)

        try:
            db.session.commit()
            data = {'message': '新建数据成功'}

        except SQLAlchemyError as e:
            db.session.rollback()
            data = {'message': ' 新建数据失败'}

    else:
        data = {'message': '请求数据异常'}

    # return jsonify(data)

    # 支持跨域
    response = make_response(jsonify(data))
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
    response.headers[
        'Access-Control-Allow-Headers'] = 'Referer,Accept,Origin,User-Agent'
    return response
Пример #6
0
 def test_delete_host(self):
     self.test_create_host()
     host = Host.find_one({"fqdn": TEST_HOST_1["fqdn"]})
     r = self.delete("/api/v1/hosts/%s" % host._id)
     self.assertEqual(r.status_code, 200)
     data = json.loads(r.data)
     self.assertIn("data", data)
     host_data = data["data"]
     self.assertEqual(host_data["_id"], None)
     host = Host.find_one({"_id": host._id})
     self.assertIsNone(host)
Пример #7
0
def test_update_per_reporter_staleness(db_create_host, models_datetime_mock):
    puptoo_stale_timestamp = models_datetime_mock + timedelta(days=1)
    input_host = Host(
        {"fqdn": "fqdn"}, display_name="display_name", reporter="puptoo", stale_timestamp=puptoo_stale_timestamp
    )
    existing_host = db_create_host(host=input_host)

    assert existing_host.per_reporter_staleness == {
        "puptoo": {
            "last_check_in": models_datetime_mock.isoformat(),
            "stale_timestamp": puptoo_stale_timestamp.isoformat(),
            "check_in_succeeded": True,
        }
    }

    puptoo_stale_timestamp += timedelta(days=1)

    update_host = Host(
        {"fqdn": "fqdn"}, display_name="display_name", reporter="puptoo", stale_timestamp=puptoo_stale_timestamp
    )
    existing_host.update(update_host)

    # datetime will not change because the datetime.now() method is patched
    assert existing_host.per_reporter_staleness == {
        "puptoo": {
            "last_check_in": models_datetime_mock.isoformat(),
            "stale_timestamp": puptoo_stale_timestamp.isoformat(),
            "check_in_succeeded": True,
        }
    }

    yupana_stale_timestamp = puptoo_stale_timestamp + timedelta(days=1)

    update_host = Host(
        {"fqdn": "fqdn"}, display_name="display_name", reporter="yupana", stale_timestamp=yupana_stale_timestamp
    )
    existing_host.update(update_host)

    # datetime will not change because the datetime.now() method is patched
    assert existing_host.per_reporter_staleness == {
        "puptoo": {
            "last_check_in": models_datetime_mock.isoformat(),
            "stale_timestamp": puptoo_stale_timestamp.isoformat(),
            "check_in_succeeded": True,
        },
        "yupana": {
            "last_check_in": models_datetime_mock.isoformat(),
            "stale_timestamp": yupana_stale_timestamp.isoformat(),
            "check_in_succeeded": True,
        },
    }
Пример #8
0
 def test_show_host(self):
     h = Host(**TEST_HOST_1)
     h.save()
     r = self.get("/api/v1/hosts/%s" % h._id)
     self.assertEqual(200, r.status_code)
     data = json.loads(r.data)
     self.assertIn("data", data)
     data = data["data"]
     self.assertIs(list, type(data))
     self.assertEqual(1, len(data))
     host_attrs = data[0]
     self.assertEqual(h.fqdn, host_attrs["fqdn"])
     self.assertItemsEqual(h.tags, host_attrs["tags"])
     self.assertEqual(h.description, host_attrs["description"])
Пример #9
0
    def insert_one(self, host):
        """Insert a New Host"""
        token = self.__crypto.get_token()
        host = Host(
            name=host["name"],
            slug=host["slug"],
            type=host["type"],
            server=self.__crypto.encrypt(host["server"], token),
            auth_data=self.__crypto.encrypt(host["auth_data"], token),
            user=User.objects.get(pk=host["user_id"]),
            token=token
        )

        host.save()
        return False if host.pk is None else host
Пример #10
0
def test_host_model_updated_timestamp(db_create_host):
    host = Host(account=ACCOUNT, canonical_facts={"fqdn": "fqdn"})

    before_insert_commit = now()
    db_create_host(host)
    after_insert_commit = now()

    host.canonical_facts = {"fqdn": "ndqf"}

    before_update_commit = now()
    db.session.commit()
    after_update_commit = now()

    assert before_insert_commit < host.created_on < after_insert_commit
    assert before_update_commit < host.modified_on < after_update_commit
Пример #11
0
def get_export():
    export_data = dict(hosts=[], groups=[])
    args = merge_args_data(request.args, request.get_json(silent=True))

    o_hosts = Host.objects()
    for o_host in o_hosts:
        export_data["hosts"].append(o_host.to_barn_dict())

    o_groups = Group.objects()
    for o_group in o_groups:
        export_data["groups"].append(o_group.to_barn_dict())

    if args.get("full", "").lower() in ("false", "no", "n", "0"):
        export_data = remove_empty_fields(export_data)

    response = None
    if args.get("format") in ("yaml", "yml"):
        response = make_response(
            yaml.dump(yaml.load(json.dumps(export_data)), indent=2))
        response.headers.set('Content-Type', 'text/plain')
    else:
        response = make_response(json.dumps(export_data, indent=2))
        response.headers.set('Content-Type', 'application/json')

    if "file" in args and args.get("file").lower() in ("true", "yes", "y",
                                                       "1"):
        response.headers.set('Content-Disposition',
                             'attachment',
                             filename='barn-export.%s' % (args.get("format")))

    return response
Пример #12
0
def executer_data():
    from app.models import Project, Datacenter, Group, Host
    if "projects" not in request.values:
        return json_response(
            {
                "errors":
                ["'projects' parameter is required for executer_data handler"]
            }, 400)
    project_names = request.values["projects"].split(",")

    projects = Project.find({"name": {"$in": project_names}})
    projects = cursor_to_list(projects)
    project_ids = [x["_id"] for x in projects]

    groups = Group.find({"project_id": {"$in": project_ids}})
    groups = cursor_to_list(groups)
    group_ids = [x["_id"] for x in groups]

    hosts = Host.find({"group_id": {"$in": group_ids}})
    hosts = cursor_to_list(hosts)

    datacenters = Datacenter.find({})
    datacenters = cursor_to_list(datacenters)

    results = {
        "datacenters": datacenters,
        "projects": projects,
        "groups": groups,
        "hosts": hosts
    }
    return json_response({"data": results})
Пример #13
0
def test_update_existing_host_fix_display_name_using_input_fqdn(
        db_create_host):
    # Create an "existing" host
    fqdn = "host1.domain1.com"

    existing_host = db_create_host(extra_data={
        "canonical_facts": {
            "fqdn": fqdn
        },
        "display_name": None
    })

    # Clear the display_name
    existing_host.display_name = None
    db.session.commit()
    assert existing_host.display_name is None

    # Update the host
    expected_fqdn = "different.domain1.com"
    input_host = Host({"fqdn": expected_fqdn},
                      display_name="",
                      reporter="puptoo",
                      stale_timestamp=now())
    existing_host.update(input_host)

    assert existing_host.display_name == expected_fqdn
Пример #14
0
def test_update_existing_host_fix_display_name_using_existing_fqdn(
        db_create_host):
    expected_fqdn = "host1.domain1.com"
    insights_id = generate_uuid()

    existing_host = db_create_host(
        extra_data={
            "canonical_facts": {
                "fqdn": expected_fqdn,
                "insights_id": insights_id
            },
            "display_name": None
        })

    # Clear the display_name
    existing_host.display_name = None
    db.session.commit()
    assert existing_host.display_name is None

    # Update the host
    input_host = Host({"insights_id": insights_id},
                      display_name="",
                      reporter="puptoo",
                      stale_timestamp=now())
    existing_host.update(input_host)

    assert existing_host.display_name == expected_fqdn
Пример #15
0
def addHost(host):
    print("addHost()")
    print("host:", host)

    # Required inputs:
    # account
    # canonical_facts

    canonical_facts = host.get("canonical_facts")

    found_host = Host.query.filter(
        Host.canonical_facts.comparator.contains(canonical_facts)
        | Host.canonical_facts.comparator.contained_by(canonical_facts)).first(
        )

    if not found_host:
        print("Creating a new host")
        host = Host.from_json(host)
        host.save()
        return host.to_json(), 201

    else:
        print("Updating host...")

        found_host.update(host)

        print("*** Updated host:", found_host)

        return found_host.to_json(), 200
Пример #16
0
def test_host_model_assigned_values(db_create_host, db_get_host):
    values = {
        "account": USER_IDENTITY["account_number"],
        "display_name": "display_name",
        "ansible_host": "ansible_host",
        "facts": [{
            "namespace": "namespace",
            "facts": {
                "key": "value"
            }
        }],
        "tags": {
            "namespace": {
                "key": ["value"]
            }
        },
        "canonical_facts": {
            "fqdn": "fqdn"
        },
        "system_profile_facts": {
            "number_of_cpus": 1
        },
        "stale_timestamp": now(),
        "reporter": "reporter",
    }

    inserted_host = Host(**values)
    db_create_host(inserted_host)

    selected_host = db_get_host(inserted_host.id)
    for key, value in values.items():
        assert getattr(selected_host, key) == value
Пример #17
0
def db_host(**values):
    data = {
        "account": USER_IDENTITY["account_number"],
        "display_name": "test-display-name",
        "ansible_host": "test-ansible-host",
        "canonical_facts": {
            "insights_id": generate_uuid(),
            "subscription_manager_id": generate_uuid(),
            "bios_uuid": generate_uuid(),
            "fqdn": "test-fqdn",
            "satellite_id": generate_uuid(),
            "rhel_machine_id": generate_uuid(),
            "ip_addresses": ["10.0.0.1"],
            "mac_addresses": ["aa:bb:cc:dd:ee:ff"],
        },
        "facts": {
            "ns1": {
                "key1": "value1"
            }
        },
        "tags": {
            "ns1": {
                "key1": ["val1", "val2"],
                "key2": ["val1"]
            },
            "SPECIAL": {
                "tag": ["ToFind"]
            }
        },
        "stale_timestamp": (now() + timedelta(days=randint(1, 7))).isoformat(),
        "reporter": "test-reporter",
        **values,
    }
    return Host(**data)
Пример #18
0
def host_add():
    form = HostForm()
    if form.validate_on_submit():
        data = form.data
        name_count = Host.query.filter_by(name=data["host_name"]).count()
        if name_count == 1:
            flash("主机名已经存在!", "err")
            return redirect(url_for("admin.host_add"))
        outernetip_num = Host.query.filter_by(
            outernet_ip=data["outernet_ip"]).count()
        if outernetip_num == 1:
            flash("外网IP已经存在!", "err")
            return redirect(url_for("admin.host_add"))
        host = Host(
            name=data["host_name"],
            system=data["system"],
            outernet_ip=data["outernet_ip"],
            intranet_ip=data["intranet_ip"],
            cpu=data["cpu"],
            memory=data["memory"],
            disk=data["disk"],
            username=data["username"],
            password=data["password"],
            port=data["port"],
            ssh_port=data["ssh_port"],
            status=data["status"],
        )
        db.session.add(host)
        db.session.commit()
        flash("添加主机成功", "ok")
        return redirect(url_for("admin.host_list", page=1))
    return render_template("admin/host_add.html", form=form)
Пример #19
0
def test_add_multiple_hosts(session):
    host1 = Host(hostname="test-hostname2",
                 ll_ip="fe80::dead:beef:cafe:babe/64",
                 ctrl_if="eno2",
                 last_seen=datetime.now())

    host2 = Host(hostname="test-hostname2",
                 ll_ip="fe80::cafe:babe:dead:beaf/64",
                 ctrl_if="eno2",
                 last_seen=datetime.now())

    session.add(host1)
    session.add(host2)
    session.commit()

    assert session.query(Host).count() == 2
Пример #20
0
def _add_host(host):
    """
    Add or update a host

    Required parameters:
     - at least one of the canonical facts fields is required
     - account number
    """
    validated_input_host_dict = HostSchema(strict=True).load(host)

    input_host = Host.from_json(validated_input_host_dict.data)

    if not current_identity.is_trusted_system and current_identity.account_number != input_host.account:
        raise InventoryException(
            title="Invalid request",
            detail=
            "The account number associated with the user does not match the account number associated with the "
            "host",
        )

    existing_host = find_existing_host(input_host.account,
                                       input_host.canonical_facts)

    if existing_host:
        return update_existing_host(existing_host, input_host)
    else:
        return create_new_host(input_host)
Пример #21
0
def add_host(host):
    """
    Add or update a host

    Required parameters:
     - at least one of the canonical facts fields is required
     - account number
    """
    logger.debug("add_host(%s)" % host)

    account_number = host.get("account", None)

    if current_identity.account_number != account_number:
        raise InventoryException(
            title="Invalid request",
            detail="The account number associated with the user does not "
            "match the account number associated with the host")

    input_host = Host.from_json(host)

    canonical_facts = input_host.canonical_facts

    if not canonical_facts:
        raise InventoryException(title="Invalid request",
                                 detail="At least one of the canonical fact "
                                 "fields must be present.")

    existing_host = find_existing_host(account_number, canonical_facts)

    if existing_host:
        return update_existing_host(existing_host, input_host)
    else:
        return create_new_host(input_host)
Пример #22
0
def enc(host_id):
    from app.models import Host
    host_id = resolve_id(host_id)
    host = Host.find_one({
        "$or": [{
            "_id": host_id
        }, {
            "fqdn": host_id
        }, {
            "short_name": host_id
        }]
    })
    if host is None:
        return json_response({"errors": ["Host not found"]}, 404)

    puppet_data = {"classes": [], "parameters": {}}

    for tag in host.all_tags:
        class_match = CLASS_EXPR.match(tag)
        if class_match is not None:
            puppet_data["classes"].append(class_match.groups()[0])
            continue
        param_match = PARAM_EXPR.match(tag)
        if param_match is not None:
            k, v = param_match.groups()
            puppet_data["parameters"][k] = v
            continue
        env_match = ENV_EXPR.match(tag)
        if env_match is not None:
            puppet_data["environment"] = env_match.groups()[0]

    return yaml_response(puppet_data)
def deserialize_host_xjoin(data):
    host = Host(
        canonical_facts=data["canonical_facts"],
        display_name=data["display_name"],
        ansible_host=data["ansible_host"],
        account=data["account"],
        facts=data["facts"] or {},
        tags={},  # Not a part of host list output
        system_profile_facts={},  # Not a part of host list output
        stale_timestamp=_deserialize_datetime(data["stale_timestamp"]),
        reporter=data["reporter"],
    )
    for field in ("created_on", "modified_on"):
        setattr(host, field, _deserialize_datetime(data[field]))
    host.id = data["id"]
    return host
Пример #24
0
def get_all_hosts():
    hosts = Host.get_all_hosts()

    hosts_list = []
    for host in hosts:
        hosts_list.append((host.region, host.region))
    return hosts_list
Пример #25
0
    def post(self, jwt):
        data = []
        if request.is_json and request.get_json(silent=True) is not None:
            try:
                post_data = request.get_json()

                host_name = post_data.get('host_name')

                community_str = ""

                if host_name is None:
                    return jsonify(error=True,
                                   msg="Missing host_name required field.")

                host = Host.get_by_hostname(host_name)
                if host is None:
                    return jsonify(error=True)
                else:
                    community_str = host._SNMPCOMMUNITY
                    if community_str is None or len(community_str) == 0:
                        return jsonify(error=True,
                                       msg="Missing SNMP community.")

                    data.append({
                        "id":
                        "1",
                        "type_name":
                        "snmp_cpuload",
                        "type_alias":
                        "SNMP CPU Load",
                        "type_info":
                        "Monitors the load of a CPU via SNMP"
                    })

                    data.append({
                        "id":
                        "2",
                        "type_name":
                        "snmp_traffic",
                        "type_alias":
                        "SNMP Traffic",
                        "type_info":
                        "Monitors bandwidth and traffic on servers, PCs, switches, etc. using SNMP"
                    })

                    data.append({
                        "id":
                        "3",
                        "type_name":
                        "snmp_memory",
                        "type_alias":
                        "SNMP Memory",
                        "type_info":
                        "Monitors the memory usage via SNMP"
                    })
            except Exception as e:
                return jsonify(error=True, msg=str(e))

        return jsonify(data=data)
Пример #26
0
def test_host_model_timestamp_timezones(db_create_host):
    host = Host(account=ACCOUNT, canonical_facts={"fqdn": "fqdn"}, stale_timestamp=now(), reporter="ingress")

    db_create_host(host)

    assert host.created_on.tzinfo
    assert host.modified_on.tzinfo
    assert host.stale_timestamp.tzinfo
Пример #27
0
def _create_host(fqdn=None, display_name=None):
    canonical_facts = {}
    if fqdn:
        canonical_facts = {'fqdn': fqdn}
    host = Host(canonical_facts, display_name=display_name, account="00102")
    db.session.add(host)
    db.session.commit()
    return host
Пример #28
0
def get_active_hosts_by_type(type):
    hosts = Host.get_hosts_by_type(type)

    hosts_list = []
    for host in hosts:
        if host.active:
            hosts_list.append((host.region, host.name))
    return hosts_list
Пример #29
0
def create():
    from app.models import Host
    hosts_attrs = dict(
        [x for x in request.json.items() if x[0] in Host.FIELDS])

    if "fqdn_pattern" in request.json:
        if "fqdn" in hosts_attrs:
            return json_response({
                "errors": [
                    "fqdn field is not allowed due to fqdn_pattern param presence"
                ]
            })
        try:
            hostnames = list(expand_pattern(request.json["fqdn_pattern"]))
        except Exception as e:
            return json_exception(e)
    else:
        hostnames = [hosts_attrs["fqdn"]]
        del (hosts_attrs["fqdn"])

    try:
        hosts_attrs["group_id"] = ObjectId(hosts_attrs["group_id"])
    except (KeyError, InvalidId):
        hosts_attrs["group_id"] = None

    try:
        hosts_attrs["datacenter_id"] = ObjectId(hosts_attrs["datacenter_id"])
    except (KeyError, InvalidId):
        hosts_attrs["datacenter_id"] = None

    for fqdn in hostnames:
        attrs = hosts_attrs
        attrs["fqdn"] = fqdn
        host = Host(**attrs)
        try:
            host.save()
        except Exception as e:
            return json_exception(e, 500)

    hosts = Host.find({"fqdn": {"$in": list(hostnames)}})
    try:
        data = paginated_data(hosts.sort("fqdn"))
    except AttributeError as e:
        return json_exception(e, 500)
    return json_response(data, 201)
Пример #30
0
def platform_info(name='liudong-ubuntu', force_update=False):
    host = Host.objects.filter(name=name).first()
    if not host or force_update:
        if not host:
            host = Host(name=name)
        host.core_num = psutil.cpu_count()
        host.cpu = platform.machine()
        uname_result = platform.uname()
        host.system = uname_result[0]
        host.system_version = uname_result[2]
        host.system_arch = uname_result[4]

        sdiskusage = psutil.disk_usage('/')
        host.hard_disk = sdiskusage.total * 1.0 / (1024**3)

        mem = psutil.virtual_memory()
        host.memory = mem.total * 1.0 / (1024**3)
        host.save()
Пример #31
0
def test_host_model_constraints(field, value, db_create_host):
    values = {"account": ACCOUNT, "canonical_facts": {"fqdn": "fqdn"}, **{field: value}}
    if field == "reporter":
        values["stale_timestamp"] = now()

    host = Host(**values)

    with pytest.raises(DataError):
        db_create_host(host)
Пример #32
0
def add_host(request):
    request_dict = request.GET
    name = request_dict.get('name')
    force_update = request_dict.get('force_update')
    print request_dict
    data = {k: v for k, v in request_dict.items()}

    host = Host.objects.filter(name=name).first()
    if not host or force_update:
        if not host:
            host = Host(**data)

        host.save(force_insert=True)

    data = {
        'msg': 'ok',
        'status': 'ok'
    }

    return JsonResponse(data)