Beispiel #1
0
def get_customers_of_user(user_name=None, conn=None):

    if not user_name:
        return None

    customers = (
        r.table(Collection.UsersPerCustomer)
        .get_all(
            user_name,
            index=UsersPerCustomerKey.UserId
        )
        .pluck(UsersPerCustomerKey.CustomerId)
        .eq_join(
            UsersPerCustomerKey.CustomerId,
            r.table(Collection.Customers),
            index=CustomerKey.CustomerName
        )
        .zip()
        .run(conn)
    )

    c = []
    for customer in customers:
        c.append(customer)

    return c
Beispiel #2
0
def update_file_data(app_id, agent_id, file_data):
    conn = db_connect()
    try:
        if len(file_data) > 0:
            for uri in file_data:
                exists = (r.table(FilesCollection).get(
                    uri[FilesKey.FileName]).run(conn))
                if exists:
                    (r.table(FilesCollection).get(
                        uri[FilesKey.FileName]).update({
                            FilesKey.AppIds:
                            (r.row[FilesKey.AppIds].set_insert(app_id)),
                            FilesKey.AgentIds:
                            (r.row[FilesKey.AgentIds].set_insert(agent_id))
                        }).run(conn))

                else:
                    (r.table(FilesCollection).insert(
                        {
                            FilesKey.AppIds: [app_id],
                            FilesKey.AgentIds: [agent_id],
                            FilesKey.FileName: uri[FilesKey.FileName],
                            FilesKey.FileSize: uri[FilesKey.FileSize],
                            FilesKey.FileUri: uri[FilesKey.FileUri],
                            FilesKey.FileHash: uri[FilesKey.FileHash],
                        }, ).run(conn, no_reply=True))

    except Exception as e:
        logger.exception(e)

    conn.close()
Beispiel #3
0
def update_agent_status(agentid, username, uri=None, method=None):
    try:
        conn = db_connect()
        update_status = {
            AgentKey.LastAgentUpdate:
            r.epoch_time(mktime(datetime.now().timetuple())),
            AgentKey.AgentStatus:
            'up'
        }
        exists = (r.table(AgentsCollection).get(agentid).run(conn))
        if exists:
            (r.table(AgentsCollection).get(agentid).update(update_status).run(
                conn))
            status = (GenericResults(username, uri, method).object_updated(
                agentid, 'agent', update_status))
        else:
            status = (GenericResults(username, uri,
                                     method).does_not_exists(agentid, 'agent'))

        logger.info(status['message'])
        conn.close()

    except Exception as e:
        status = (GenericResults(username, uri,
                                 method).something_broke(agentid, 'agent', e))

        logger.exception(e)
Beispiel #4
0
def get_users_of_customer(customer_name=None, conn=None):

    if not customer_name:
        return None

    users = (
        r.table(Collection.UsersPerCustomer)
        .get_all(
            customer_name,
            index=UsersPerCustomerKey.CustomerId
        )
        .pluck(UsersPerCustomerKey.UserId)
        .eq_join(
            UsersPerCustomerKey.UserId,
            r.table(Collection.Users),
            index=UserKey.UserName
        )
        .zip()
        .run(conn)
    )

    u = []
    for user in users:
        u.append(user)

    return u
Beispiel #5
0
    def get_all_operations_by_type(self, oper_type, conn=None):
        try:
            if oper_type in VALID_OPERATIONS:
                count = (r.table(OperationsCollection).get_all(
                    [oper_type, self.customer_name],
                    index=OperationIndexes.OperationAndCustomer).count().run(
                        conn))

                operations = list(
                    r.table(OperationsCollection).get_all(
                        [oper_type, self.customer_name],
                        index=OperationIndexes.OperationAndCustomer).pluck(
                            self.pluck_list).order_by(self.sort(
                                self.sort_key)).skip(self.offset).limit(
                                    self.count).map(self.map_hash).run(conn))

                results = (GenericResults(self.username, self.uri,
                                          self.method).information_retrieved(
                                              operations, count))
                logger.info(results)

            else:
                results = (OperationResults(
                    self.username, self.uri,
                    self.method).invalid_operation_type(oper_type))
                logger.warn(results)

        except Exception as e:
            results = (GenericResults(self.username, self.uri,
                                      self.method).something_broke(
                                          'Operations Query', 'Operations', e))
            logger.exception(results)

        return (results)
Beispiel #6
0
def get_all_agent_ids(customer_name=None, agent_os=None, conn=None):
    if not customer_name and agent_os:
        agent_ids = list(
            r.table(AgentsCollection).filter({
                AgentKey.OsCode: agent_os
            }).map(lambda x: x[AgentKey.AgentId]).run(conn))

    elif customer_name and agent_os:
        agent_ids = list(
            r.table(AgentsCollection).get_all(
                customer_name, index=AgentIndexes.CustomerName).filter({
                    AgentKey.OsCode:
                    agent_os
                }).map(lambda x: x[AgentKey.AgentId]).run(conn))

    elif customer_name and not agent_os:
        agent_ids = list(
            r.table(AgentsCollection).get_all(
                customer_name, index=AgentIndexes.CustomerName).map(
                    lambda x: x[AgentKey.AgentId]).run(conn))

    else:
        agent_ids = list(
            r.table(AgentsCollection).map(lambda x: x[AgentKey.AgentId]).run(
                conn))

    return (agent_ids)
Beispiel #7
0
def get_file_data(app_id, agent_id=None):
    conn = db_connect()
    try:
        if agent_id:
            file_data = list(
                r.table(FilesCollection).filter(lambda x: (
                    x[FilesKey.AppIds].contains(app_id)
                    & x[FilesKey.AgentIds].contains(agent_id))).without(
                        FilesKey.AppIds,
                        FilesKey.AgentIds,
                    ).run(conn))
        else:
            file_data = list(
                r.table(FilesCollection).filter(
                    lambda x: (x[FilesKey.AppIds].contains(app_id))).without(
                        FilesKey.AppIds,
                        FilesKey.AgentIds,
                    ).run(conn))

    except Exception as e:
        file_data = []
        logger.exception(e)

    conn.close()
    return (file_data)
Beispiel #8
0
    def get_all_operations_by_agentid(self, agent_id, conn=None):
        try:
            count = (r.table(OperationsPerAgentCollection).get_all(
                [agent_id, self.customer_name],
                index=OperationPerAgentIndexes.AgentIdAndCustomer).count().run(
                    conn))

            operations = list(
                r.table(OperationsPerAgentCollection).get_all(
                    [agent_id, self.customer_name],
                    index=OperationPerAgentIndexes.AgentIdAndCustomer).eq_join(
                        OperationKey.OperationId,
                        r.table(OperationsCollection)).zip().pluck(
                            self.pluck_list).order_by(self.sort(
                                self.sort_key)).skip(self.offset).limit(
                                    self.count).map(self.map_hash).run(conn))

            results = (GenericResults(self.username, self.uri,
                                      self.method).information_retrieved(
                                          operations, count))
            logger.info(results)

        except Exception as e:
            results = (GenericResults(self.username, self.uri,
                                      self.method).something_broke(
                                          'Operations Query', 'Operations', e))
            logger.exception(results)

        return (results)
Beispiel #9
0
def get_app_data(app_id,
                 table=AppsCollection,
                 app_key=AppsKey.AppId,
                 filterbykey=None,
                 filterbyval=None,
                 fields_to_pluck=None,
                 conn=None):
    apps = None
    if fields_to_pluck and not filterbykey and not filterbyval:
        apps = (r.table(table).get(app_id).pluck(fields_to_pluck).run(conn))

    elif fields_to_pluck and filterbykey and filterbyval:
        apps = list(
            r.table(table).filter({
                filterbykey: filterbyval,
                app_key: app_id
            }).pluck(fields_to_pluck).run(conn))

    elif not fields_to_pluck and filterbykey and filterbyval:
        apps = list(
            r.table(table).filter({
                filterbykey: filterbyval,
                app_key: app_id
            }).run(conn))

    else:
        apps = (r.table(table).get(app_id).run(conn))

    return (apps)
Beispiel #10
0
def tag_lister(customer_name="default", query=None, conn=None):
    """
        return a list of tags in json
    """
    if query:
        tags = list(
            r.table(TagsCollection)
            .get_all(customer_name, index=TagsIndexes.CustomerName)
            .filter(lambda x: x[TagsKey.TagName].match(query))
            .run(conn)
        )

    else:
        tags = list(
            r.table(TagsCollection)
            .get_all(customer_name, index=TagsIndexes.CustomerName)
            .order_by(TagsKey.TagName)
            .run(conn)
        )

    if tags:
        for tag in xrange(len(tags)):
            agents_in_tag = list(
                r.table(TagsPerAgentCollection)
                .get_all(tags[tag]["id"], index=TagsPerAgentIndexes.TagId)
                .eq_join(TagsPerAgentKey.AgentId, r.table(TagsCollection))
                .zip()
                .pluck(TagsPerAgentKey.AgentId, AgentKey.ComputerName, AgentKey.DisplayName)
                .run(conn)
            )
            tags[tag][AgentsCollection] = agents_in_tag

    return {"pass": True, "message": "", "data": tags}
Beispiel #11
0
    def lower_version_exists_of_app(self,
                                    app,
                                    app_info,
                                    status=AVAILABLE,
                                    conn=None):
        try:
            lower_version_exists = list(
                r.table(AppsCollection).get_all(
                    app_info[AppsKey.Name],
                    index=AppsIndexes.Name).filter(lambda x: x[
                        AppsKey.Version] < app_info[AppsKey.Version]).eq_join(
                            lambda y: [
                                app[AgentKey.AgentId],
                                app[AppsPerAgentKey.AppId],
                            ],
                            r.table(AppsPerAgentCollection),
                            index=AppsPerAgentIndexes.AgentId).zip().filter({
                                AppsPerAgentKey.Status:
                                status
                            }).pluck(AppsKey.AppId, AppsPerAgentKey.Id,
                                     AppsKey.Name,
                                     AppsPerAgentKey.AgentId).run(conn))

        except Exception as e:
            logger.exception(e)

        return (lower_version_exists)
    def get_sending_emails(self, notif_rules, conn=None):
        try:
            email_sender_list = []
            for notif in notif_rules:
                if notif[NotificationKeys.Group]:
                    users_list = (r.table(Collection.Groups).filter({
                        'name':
                        notif['group']
                    }).filter(lambda x: x['customer']['name'] == notif[
                        'customer_name']).map(lambda x: x['users']).run(conn))
                    if users_list:
                        users = map(lambda x: x['name'], user_list[0])
                        email_sender_list += (r.expr(users).map(
                            lambda user: r.table('users').get(user)).map(
                                lambda x: x['email']).run(conn))

                elif notif[NotificationKeys.User]:
                    email = (r.table(Collection.Users).get(
                        notif[NotificationKeys.User]).pluck(
                            UserKey.Email).run(conn))
                    if email:
                        email_sender_list.append(email[UserKey.Email])

        except Exception as e:
            logger.exception(e)

        return (email_sender_list)
Beispiel #13
0
def update_customers_in_app(customer_name, app_id, table=AppsCollection):
    conn = db_connect()
    if table == AppsCollection:
        CurrentAppsKey = AppsKey

    elif table == CustomAppsCollection:
        CurrentAppsKey = CustomAppsKey

    elif table == SupportedAppsCollection:
        CurrentAppsKey = SupportedAppsKey

    elif table == AgentAppsCollection:
        CurrentAppsKey = AgentAppsKey

    try:
        exists = (r.table(table).get(app_id).run(conn))
        if exists:
            (r.table(table).get(app_id).update({
                CurrentAppsKey.Customers:
                (r.row[CurrentAppsKey.Customers].set_insert(customer_name)),
            }).run(conn))

    except Exception as e:
        logger.exception(e)

    conn.close()
Beispiel #14
0
    def rule_exist_on_agent(self, conn=None):
        rules_exist = None
        if self.agentid and self.plugin and self.oper_type and self.threshold:
            email_sender_list = []

            if not self.filesystem and self.plugin == 'monitoring':
                rules_exist = list(
                    r.table('notification_per_agent').get_all(
                        'agent_id', index='agent_id').filter({
                            'monitoring_operation_type':
                            self.oper_type,
                            'plugin_name':
                            self.plugin
                        }).filter(
                            r.row['monitoring_threshold'] >= self.threshold).
                    run(conn))

            elif self.filesystem and self.plugin == 'monitoring':
                rules_exist = list(
                    r.table('notification_per_agent').get_all(
                        'agent_id', index='agent_id').filter({
                            'monitoring_operation_type':
                            self.oper_type,
                            'plugin_name':
                            self.plugin
                        }).filter(lambda x: x['file_system'].match(
                            self.filesystem)).filter(
                                r.row['monitoring_threshold'] >= self.threshold
                            ).run(conn))
Beispiel #15
0
def tag_stats_by_os(username, customer_name, uri, method, tag_id, count=3, conn=None):
    try:
        stats = (
            r.table(TagsPerAgentCollection, use_outdated=True)
            .get_all(tag_id, index=TagsPerAgentIndexes.TagId)
            .pluck(TagsPerAgentKey.AgentId)
            .eq_join(
                lambda x: [AVAILABLE, x[AppsPerAgentKey.AgentId]],
                r.table(AppsPerAgentCollection),
                index=AppsPerAgentIndexes.StatusAndAgentId,
            )
            .zip()
            .eq_join(AgentKey.AgentId, r.table(AgentsCollection))
            .zip()
            .pluck(APP_ID, AgentKey.OsString)
            .distinct()
            .group_by(AgentKey.OsString, r.count)
            .order_by(r.desc("reduction"))
            .limit(count)
            .run(conn)
        )

        data = []
        if stats:
            data = app_stats_by_os(stats)

        results = GenericResults(username, uri, method).information_retrieved(data, count)

    except Exception as e:
        results = GenericResults(username, uri, method).something_broke("tag widget stats", "widget", e)
        logger.exception(results)

    return results
Beispiel #16
0
def tag_lister(customer_name='default', query=None, conn=None):
    """
        return a list of tags in json
    """
    if query:
        tags = list(
            r.table(TagsCollection).get_all(
                customer_name, index=TagsIndexes.CustomerName).filter(
                    lambda x: x[TagsKey.TagName].match(query)).run(conn))

    else:
        tags = list(
            r.table(TagsCollection).get_all(
                customer_name, index=TagsIndexes.CustomerName).order_by(
                    TagsKey.TagName).run(conn))

    if tags:
        for tag in xrange(len(tags)):
            agents_in_tag = list(
                r.table(TagsPerAgentCollection).get_all(
                    tags[tag]['id'], index=TagsPerAgentIndexes.TagId).eq_join(
                        TagsPerAgentKey.AgentId,
                        r.table(TagsCollection)).zip().pluck(
                            TagsPerAgentKey.AgentId, AgentKey.ComputerName,
                            AgentKey.DisplayName).run(conn))
            tags[tag][AgentsCollection] = agents_in_tag

    return ({'pass': True, 'message': '', 'data': tags})
Beispiel #17
0
def customer_stats_by_os(username, customer_name, uri, method, count=3, conn=None):
    try:
        stats = (
            r.table(AppsPerAgentCollection, use_outdated=True)
            .get_all([AVAILABLE, customer_name], index=AppsPerAgentIndexes.StatusAndCustomer)
            .eq_join(AgentKey.AgentId, r.table(AgentsCollection))
            .map({AppsKey.AppId: r.row["left"][AppsKey.AppId], AgentKey.OsString: r.row["right"][AgentKey.OsString]})
            .pluck(AppsKey.AppId, AgentKey.OsString)
            .distinct()
            .group_by(AgentKey.OsString, r.count)
            .order_by(r.desc("reduction"))
            .limit(count)
            .run(conn)
        )

        data = []
        if stats:
            data = app_stats_by_os(stats)

        results = GenericResults(username, uri, method).information_retrieved(data, count)

    except Exception as e:
        results = GenericResults(username, uri, method).something_broke("widget stats", "widget", e)
        logger.exception(results)

    return results
Beispiel #18
0
def get_severity_bar_chart_stats_for_tag(username, customer_name, uri, method, tag_id, conn=None):
    try:
        sevs = (
            r.table(TagsPerAgentCollection, use_outdated=True)
            .get_all(tag_id, index=TagsPerAgentIndexes.TagId)
            .pluck(TagsPerAgentKey.AgentId)
            .eq_join(
                lambda x: [AVAILABLE, x[AppsPerAgentKey.AgentId]],
                r.table(AppsPerAgentCollection),
                index=AppsPerAgentIndexes.StatusAndAgentId,
            )
            .map({AppsKey.AppId: r.row["right"][AppsKey.AppId]})
            .eq_join(AppsKey.AppId, r.table(AppsCollection))
            .map({AppsKey.AppId: r.row["right"][AppsKey.AppId], AppsKey.RvSeverity: r.row["right"][AppsKey.RvSeverity]})
            .group_by(AppsKey.RvSeverity, r.count)
            .order_by(r.desc("reduction"))
            .run(conn)
        )
        data = app_stats_by_severity(sevs)

        results = GenericResults(username, uri, method).information_retrieved(data, len(ValidRvSeverities))

    except Exception as e:
        results = GenericResults(username, uri, method).something_broke("widget severity stats", "widget", e)
        logger.exception(results)

    return results
Beispiel #19
0
    def filter_by_status_and_query_by_name_and_sev(self,
                                                   name,
                                                   pkg_status,
                                                   sev,
                                                   conn=None):
        try:
            if pkg_status in ValidPackageStatuses:
                if sev in ValidRvSeverities:
                    base = (r.table(
                        self.CurrentAppsPerAgentCollection).get_all(
                            [pkg_status, self.customer_name],
                            index=self.CurrentAppsPerAgentIndexes.
                            StatusAndCustomer).eq_join(
                                self.CurrentAppsPerAgentKey.AppId,
                                r.table(self.CurrentAppsCollection)).map(
                                    self.joined_map_hash))

                    if self.show_hidden == NO:
                        base = base.filter({self.CurrentAppsKey.Hidden: NO})

                    packages = list(
                        base.filter(
                            (r.row[self.CurrentAppsKey.RvSeverity] == sev)
                            & (r.row[self.CurrentAppsKey.Name].match(
                                "(?i)" + name))).distinct().order_by(
                                    self.sort(self.sort_key)).skip(
                                        self.offset).limit(
                                            self.count).run(conn))

                    pkg_count = (base.pluck(
                        self.CurrentAppsKey.RvSeverity,
                        self.CurrentAppsKey.Name).distinct().filter(
                            (r.row[self.CurrentAppsKey.RvSeverity] == sev)
                            & (r.row[self.CurrentAppsKey.Name].match(
                                "(?i)" + name))).count().run(conn))

                    return_status = (GenericResults(
                        self.username, self.uri,
                        self.method).information_retrieved(
                            packages, pkg_count))

                else:
                    return_status = (PackageResults(
                        self.username, self.uri,
                        self.method).invalid_severity(sev))

            else:
                return_status = (PackageResults(
                    self.username, self.uri,
                    self.method).invalid_global_status(pkg_status))

        except Exception as e:
            return_status = (GenericResults(
                self.username, self.uri,
                self.method).something_broke("Package Searching went haywire",
                                             'os_updates', e))
            logger.exception(e)

        return (return_status)
Beispiel #20
0
 def insert_app_and_delete_old(self, app, lower_apps, conn=None):
     try:
         (r.table(self.CurrentAppsPerAgentCollection).insert(app).run(conn))
         for lower_app in lower_apps:
             (r.table(AppsPerAgentCollection).get(
                 lower_app[AppsPerAgentKey.Id]).delete().run(conn))
     except Exception as e:
         logger.exception(e)
    def filter_by_status_and_sev(self, pkg_status, sev, conn=None):

        try:
            agent = get_agent_info(self.agent_id)
            if agent:
                if pkg_status in ValidPackageStatuses:
                    if sev in ValidRvSeverities:
                        base = (r.table(
                            self.CurrentAppsPerAgentCollection).get_all(
                                [pkg_status, self.agent_id],
                                index=self.CurrentAppsPerAgentIndexes.
                                StatusAndAgentId).eq_join(
                                    self.CurrentAppsPerAgentKey.AppId,
                                    r.table(self.CurrentAppsCollection)).zip())
                        if self.show_hidden == NO:
                            base = base.filter(
                                {self.CurrentAppsKey.Hidden: NO})

                        packages = list(
                            base.filter(r.row[self.CurrentAppsKey.RvSeverity]
                                        == sev).map(self.map_hash).order_by(
                                            self.sort(self.sort_key)).skip(
                                                self.offset).limit(
                                                    self.count).run(conn))

                        pkg_count = (base.filter(
                            r.row[self.CurrentAppsKey.RvSeverity] ==
                            sev).count().run(conn))

                        return_status = (GenericResults(
                            self.username, self.uri,
                            self.method).information_retrieved(
                                packages, pkg_count))

                    else:
                        return_status = (PackageResults(
                            self.username, self.uri,
                            self.method).invalid_severity(sev))

                else:
                    return_status = (PackageResults(
                        self.username, self.uri,
                        self.method).invalid_status(self.agent_id, pkg_status))

            else:
                return_status = (GenericResults(self.username, self.uri,
                                                self.method).invalid_id(
                                                    self.agent_id, 'agents'))

        except Exception as e:
            return_status = (GenericResults(
                self.username, self.uri,
                self.method).something_broke("Package Searching went haywire",
                                             'os_updates', e))
            logger.exception(e)

        return (return_status)
Beispiel #22
0
def get_agent_info(agentid, keys_to_pluck=None, conn=None):
    if not keys_to_pluck:
        agent_info = (r.table(AgentsCollection).get(agentid).run(conn))

    else:
        agent_info = (r.table(AgentsCollection).get(agentid).pluck(
            keys_to_pluck).run(conn))

    return (agent_info)
    def filter_by_sev_and_query_by_name(self, name, sev, conn=None):

        try:
            tag = tag_exists(self.tag_id)
            if tag:
                if sev in ValidRvSeverities:
                    base = (r.table(
                        TagsPerAgentCollection, use_outdated=True).get_all(
                            self.tag_id, index=TagsPerAgentIndexes.TagId
                        ).pluck(TagsPerAgentKey.AgentId).eq_join(
                            self.CurrentAppsPerAgentIndexes.AgentId,
                            r.table(self.CurrentAppsPerAgentCollection),
                            index=self.CurrentAppsPerAgentIndexes.AgentId
                        ).eq_join(
                            lambda y: y['right'][self.CurrentAppsKey.AppId],
                            r.table(self.CurrentAppsCollection)).map(
                                self.map_hash))
                    if self.show_hidden == NO:
                        base = base.filter({self.CurrentAppsKey.Hidden: NO})

                    packages = list(
                        base.filter(
                            (r.row[self.CurrentAppsKey.RvSeverity] == sev)
                            & (r.row[self.CurrentAppsKey.Name].match(
                                "(?i)" + name))).distinct().order_by(
                                    self.sort(self.sort_key)).skip(
                                        self.offset).limit(
                                            self.count).run(conn))

                    pkg_count = (base.filter(
                        (r.row[self.CurrentAppsKey.RvSeverity] == sev)
                        & (r.row[self.CurrentAppsKey.Name].match("(?i)" + name)
                           )).distinct().count().run(conn))

                    return_status = (GenericResults(
                        self.username, self.uri,
                        self.method).information_retrieved(
                            packages, pkg_count))

                else:
                    return_status = (PackageResults(
                        self.username, self.uri,
                        self.method).invalid_severity(sev))

            else:
                return_status = (GenericResults(self.username, self.uri,
                                                self.method).invalid_id(
                                                    self.tag_id, 'agents'))

        except Exception as e:
            return_status = (GenericResults(
                self.username, self.uri,
                self.method).something_broke("Package Searching went haywire",
                                             'os_updates', e))
            logger.exception(e)

        return (return_status)
Beispiel #24
0
    def filter_by(self, fkey, fval, conn=None):
        try:
            if fkey in self.list_of_valid_keys:
                count = (r.table(TagsCollection).filter({
                    fkey:
                    fval,
                    TagsKey.CustomerName:
                    self.customer_name
                }).count().run(conn))
                data = list(
                    r.table(TagsCollection).filter({
                        fkey:
                        fval,
                        TagsKey.CustomerName:
                        self.customer_name
                    }).order_by(self.sort(self.sort_key)).skip(
                        self.qoffset).limit(self.qcount).run(conn))
                if data:
                    for tag in xrange(len(data)):
                        data[tag][BASIC_RV_STATS] = (
                            get_all_avail_stats_by_tagid(
                                self.username, self.customer_name, self.uri,
                                self.method, tag[TagsKey.TagId])['data'])

                        agents_in_tag = list(
                            r.table(TagsPerAgentCollection).get_all(
                                data[tag][TagsPerAgentKey.Id],
                                index=TagsPerAgentIndexes.TagId).eq_join(
                                    TagsPerAgentKey.AgentId,
                                    r.table(AgentsCollection)).zip().pluck(
                                        TagsPerAgentKey.AgentId,
                                        AgentKey.ComputerName,
                                        AgentKey.DisplayName).run(conn))
                        data[tag]['agents'] = agents_in_tag

                status = (GenericResults(self.username, self.uri,
                                         self.method).information_retrieved(
                                             data, count))

                logger.info(status['message'])

            else:
                status = (GenericResults(self.username, self.uri,
                                         self.method).incorrect_arguments(
                                             data, count))

                logger.info(status['message'])

        except Exception as e:
            status = (GenericResults(self.username, self.uri,
                                     self.method).something_broke(
                                         'search_tags_by_filter', 'tags', e))

            logger.exception(status['message'])

        return (status)
Beispiel #25
0
    def add_agent_to_install_operation(self,
                                       agent_id,
                                       operation_id,
                                       applications,
                                       conn=None):
        try:
            (r.table(OperationsPerAgentCollection).insert({
                OperationPerAgentKey.AgentId:
                agent_id,
                OperationPerAgentKey.OperationId:
                operation_id,
                OperationPerAgentKey.CustomerName:
                self.customer_name,
                OperationPerAgentKey.Status:
                PENDINGPICKUP,
                OperationPerAgentKey.PickedUpTime:
                r.epoch_time(0.0),
                OperationPerAgentKey.CompletedTime:
                r.epoch_time(0.0),
                OperationPerAgentKey.AppsTotalCount:
                len(applications),
                OperationPerAgentKey.AppsPendingCount:
                len(applications),
                OperationPerAgentKey.AppsFailedCount:
                self.INIT_COUNT,
                OperationPerAgentKey.AppsCompletedCount:
                self.INIT_COUNT,
                OperationPerAgentKey.Errors:
                None
            }).run(conn))
            for app in applications:
                (r.table(OperationsPerAppCollection).insert({
                    OperationPerAppKey.AgentId:
                    agent_id,
                    OperationPerAppKey.OperationId:
                    operation_id,
                    OperationPerAppKey.CustomerName:
                    self.customer_name,
                    OperationPerAppKey.Results:
                    OperationCodes.ResultsPending,
                    OperationPerAppKey.ResultsReceivedTime:
                    r.epoch_time(0.0),
                    OperationPerAppKey.AppId:
                    app[OperationPerAppKey.AppId],
                    OperationPerAppKey.AppName:
                    app[OperationPerAppKey.AppName],
                    OperationPerAppKey.Errors:
                    None
                }).run(conn))

        except Exception as e:
            results = (GenericResults(self.username, self.uri,
                                      self.method).something_broke(
                                          operation_id,
                                          'add agent to install operation', e))
            logger.exception(results)
Beispiel #26
0
def insert_into_agent_apps(customer_name, app, conn=None):

    table = AgentAppsCollection

    exists = []
    try:
        exists = (r.table(table).get(app[AgentAppsKey.AppId]).run(conn))

    except Exception as e:
        msg = ('Failed to get unique app_id %s, error: %s' %
               (app[AppsKey.AppId], e))
        logger.error(e)

    status = app.pop(AppsPerAgentKey.Status)
    if exists:

        if len(app[AppsKey.FileData]) > 0 and status == AVAILABLE:
            app[AppsKey.FileData] = (unique_uris(
                uris=app[AppsKey.FileData],
                orig_uris=exists[AppsKey.FileData],
            ))

        try:
            (r.table(table).get(app[AppsKey.AppId]).update({
                AppsKey.FileData:
                app[AppsKey.FileData]
            }).run(conn, no_reply=True))

        except Exception as e:
            msg = ('Failed to update unique_applications with %s, error: %s' %
                   (app[AppsKey.AppId], e))
            logger.error(msg)

    else:

        if (len(app[AppsKey.FileData]) > 0 and status == AVAILABLE
                or len(app[AppsKey.FileData]) > 0 and status == INSTALLED):
            app[AppsKey.FilesDownloadStatus] = PackageCodes.FilePendingDownload

            app[AppsKey.FileData] = (unique_uris(
                uris=app[AppsKey.FileData], orig_uris=app[AppsKey.FileData]))
        elif len(app[AppsKey.FileData]) == 0 and status == AVAILABLE:
            app[AppsKey.FilesDownloadStatus] = PackageCodes.MissingUri

        elif len(app[AppsKey.FileData]) == 0 and status == INSTALLED:
            app[AppsKey.FilesDownloadStatus] = PackageCodes.FileNotRequired

        try:
            (r.table(AppsCollection).insert(app).run(conn, no_reply=True))

        except Exception as e:
            msg = ('Failed to insert %s into unique_applications, error: %s' %
                   (app[AppsKey.AppId], e))
            logger.exception(msg)

    return (app)
Beispiel #27
0
def get_tags_info_from_tag_ids(tag_ids, keys_to_pluck=None, conn=None):

    if not keys_to_pluck:
        tags = list(r.expr(tag_ids).map(lambda tag_id: r.table(TagsCollection).get(tag_id)).run(conn))

    else:
        tags = list(
            r.expr(tag_ids).map(lambda tag_id: r.table(TagsCollection).get(tag_id).pluck(keys_to_pluck)).run(conn)
        )

    return tags
Beispiel #28
0
def update_os_app(app_id, data, table=AppsCollection, conn=None):
    app_updated = None
    try:
        exists = (r.table(table).get(app_id).run(conn))
        if exists:
            app_updated = (r.table(table).get(app_id).update(data).run(conn))

    except Exception as e:
        logger.exception(e)

    return (app_updated)
Beispiel #29
0
def get_all_tag_ids(customer_name=None, conn=None):
    if customer_name:
        tag_ids = list(
            r.table(TagsCollection).get_all(
                customer_name, index=TagsIndexes.CustomerName).map(
                    lambda x: x[TagsKey.TagId]).run(conn))

    else:
        tag_ids = list(
            r.table(TagsCollection).map(lambda x: x[TagKey.AgentId]).run(conn))

    return (tag_ids)
Beispiel #30
0
def get_all_agents_per_appid(username,
                             customer_name,
                             uri,
                             method,
                             app_id,
                             conn=None):
    data = []
    try:
        agents = (r.table(CustomAppsPerAgentCollection).get_all(
            app_id, index=CustomAppsPerAgentKey.AppId).eq_join(
                CustomAppsPerAgentKey.AgentId,
                r.table(AgentsCollection)).zip().grouped_map_reduce(
                    lambda x: x[CustomAppsPerAgentKey.Status], lambda x: {
                        AGENTS: [{
                            AgentKey.ComputerName:
                            x[AgentKey.ComputerName],
                            AgentKey.DisplayName:
                            x[AgentKey.DisplayName],
                            CustomAppsPerAgentKey.AgentId:
                            x[CustomAppsPerAgentKey.AgentId]
                        }],
                        COUNT:
                        1
                    }, lambda x, y: {
                        AGENTS: x[AGENTS] + y[AGENTS],
                        COUNT: x[COUNT] + y[COUNT]
                    }).run(conn))
        if agents:
            for i in agents:
                new_data = i['reduction']
                new_data[CustomAppsPerAgentKey.Status] = i['group']
                data.append(new_data)

        statuses = map(lambda x: x['status'], data)
        difference = set(ValidPackageStatuses).difference(statuses)
        if len(difference) > 0:
            for status in difference:
                status = {COUNT: 0, AGENTS: [], STATUS: status}
                data.append(status)

        results = (GenericResults(username, uri, method).information_retrieved(
            data, len(data)))

        logger.info(results)

    except Exception as e:
        results = (GenericResults(username, uri, method).something_broke(
            'getting_pkg_stats', 'updates', e))

        logger.info(results)

    return (results)
Beispiel #31
0
def update_supported_and_agent_apps(json_data, table=SupportedAppsCollection):

    if table == SupportedAppsCollection:
        CurrentAppsKey = SupportedAppsKey
        LatestDownloadedCollection = LatestDownloadedSupportedCollection
        AppType = 'supported_apps'

    elif table == AgentAppsCollection:
        CurrentAppsKey = AgentAppsKey
        LatestDownloadedCollection = LatestDownloadedAgentCollection
        AppType = 'agent_apps'
    try:
        rv_q = Queue('downloader', connection=rq_pkg_pool)
        conn = db_connect()
        inserted_count = 0
        all_customers = (list(
            r.table(Collection.Customers).map(
                lambda x: x[CustomerKey.CustomerName]).run(conn)))
        for i in range(len(json_data)):
            json_data[i][CurrentAppsKey.Customers] = all_customers
            json_data[i][CurrentAppsKey.ReleaseDate] = r.epoch_time(
                json_data[i][CurrentAppsKey.ReleaseDate])
            json_data[i][
                CurrentAppsKey.
                FilesDownloadStatus] = PackageCodes.FilePendingDownload
            json_data[i][CurrentAppsKey.Hidden] = 'no'
            insert_data_into_table(json_data[i], LatestDownloadedCollection)
            file_data = json_data[i].get(CurrentAppsKey.FileData)
            insert_file_data(json_data[i][CurrentAppsKey.AppId], file_data)
            data_to_update = ({CurrentAppsKey.Customers: all_customers})
            exists = (r.table(table).get(
                json_data[i][CurrentAppsKey.AppId]).run(conn))
            if exists:
                updated = (r.table(table).get(json_data[i][
                    CurrentAppsKey.AppId]).update(data_to_update).run(conn))

            else:
                updated = (r.table(table).insert(json_data[i]).run(conn))

            rv_q.enqueue_call(func=download_all_files_in_app,
                              args=(json_data[i][CurrentAppsKey.AppId],
                                    json_data[i][CurrentAppsKey.OsCode], None,
                                    file_data, 0, AppType),
                              timeout=86400)

            inserted_count += updated['inserted']
        conn.close()
        update_apps = IncomingSupportedOrAgentApps(table=table)
        update_apps.sync_supported_updates_to_all_agents(json_data)

    except Exception as e:
        logger.exception(e)
Beispiel #32
0
def get_all_tag_ids(customer_name=None, conn=None):
    if customer_name:
        tag_ids = list(
            r.table(TagsCollection)
            .get_all(customer_name, index=TagsIndexes.CustomerName)
            .map(lambda x: x[TagsKey.TagId])
            .run(conn)
        )

    else:
        tag_ids = list(r.table(TagsCollection).map(lambda x: x[TagKey.AgentId]).run(conn))

    return tag_ids
Beispiel #33
0
def get_tags_info_from_tag_ids(tag_ids, keys_to_pluck=None, conn=None):

    if not keys_to_pluck:
        tags = list(
            r.expr(tag_ids).map(
                lambda tag_id: r.table(TagsCollection).get(tag_id)).run(conn))

    else:
        tags = list(
            r.expr(tag_ids).map(lambda tag_id: r.table(TagsCollection).get(
                tag_id).pluck(keys_to_pluck)).run(conn))

    return (tags)
Beispiel #34
0
def get_tags_by_agent_id(agent_id=None, conn=None):
    tag_info = []
    if agent_id:
        tag_info = list(
            r.table(TagsPerAgentCollection).get_all(
                agent_id, index=TagsPerAgentKey.AgentId).eq_join(
                    TagsPerAgentKey.TagId, r.table(TagsCollection)).zip().map({
                        TagsPerAgentKey.TagId:
                        r.row[TagsPerAgentKey.TagId],
                        TagsPerAgentKey.TagName:
                        r.row[TagsKey.TagName]
                    }).distinct().run(conn))

    return (tag_info)
Beispiel #35
0
def get_tags_by_agent_id(agent_id=None, conn=None):
    tag_info = []
    if agent_id:
        tag_info = list(
            r.table(TagsPerAgentCollection)
            .get_all(agent_id, index=TagsPerAgentKey.AgentId)
            .eq_join(TagsPerAgentKey.TagId, r.table(TagsCollection))
            .zip()
            .map({TagsPerAgentKey.TagId: r.row[TagsPerAgentKey.TagId], TagsPerAgentKey.TagName: r.row[TagsKey.TagName]})
            .distinct()
            .run(conn)
        )

    return tag_info
Beispiel #36
0
    def _update_app_stats(self,
                          operation_id,
                          agent_id,
                          app_id,
                          results,
                          conn=None):
        completed = True
        try:
            pending_count = 0
            completed_count = 0
            failed_count = 0
            app_stats_count = (r.table(OperationsPerAppCollection).get_all(
                [operation_id, agent_id],
                index=OperationPerAppIndexes.OperationIdAndAgentId).group_by(
                    OperationPerAppKey.Results, r.count).run(conn))

            for i in app_stats_count:
                if i['group']['results'] == OperationCodes.ResultsPending:
                    pending_count = i['reduction']

                elif i['group']['results'] == OperationCodes.ResultsReceived:
                    completed_count = i['reduction']

                elif i['group'][
                        'results'] == OperationCodes.ResultsReceivedWithErrors:
                    failed_count = i['reduction']

            (r.table(OperationsPerAgentCollection).get_all(
                [operation_id, agent_id],
                index=OperationPerAgentIndexes.OperationIdAndAgentId).update({
                    OperationPerAgentKey.AppsCompletedCount:
                    completed_count,
                    OperationPerAgentKey.AppsFailedCount:
                    failed_count,
                    OperationPerAgentKey.AppsPendingCount:
                    pending_count,
                }).run(conn))
            self._update_agent_stats_by_app_stats(operation_id, agent_id,
                                                  completed_count,
                                                  failed_count, pending_count)

        except Exception as e:
            results = (GenericResults(self.username, self.uri,
                                      self.method).something_broke(
                                          operation_id, agent_id, e))
            logger.exception(results)
            completed = False

        return (completed)
Beispiel #37
0
def update_agent(username,
                 customer_name,
                 uri,
                 method,
                 agent_id,
                 system_info,
                 hardware,
                 rebooted,
                 conn=None):
    """Add a node to the database"""
    agent_data = {}

    try:
        agent_orig_info = (r.table(AgentsCollection).get(agent_id).run(conn))
        if agent_orig_info:
            agent_data[AgentKey.Hardware] = hardware

            for key, value in system_info.items():
                agent_data[key] = value

            agent_data[AgentKey.LastAgentUpdate] = (r.epoch_time(
                mktime(datetime.now().timetuple())))
            agent_data[AgentKey.HostName] = (agent_orig_info.get(
                AgentKey.HostName, None))
            agent_data[AgentKey.DisplayName] = (agent_orig_info.get(
                AgentKey.DisplayName, None))

            if rebooted == 'yes':
                agent_data[AgentKey.NeedsReboot] = 'no'

            (r.table(AgentsCollection).get(agent_id).update(agent_data).run(
                conn))
            Hardware().add(agent_id, hardware)
            status = (AgentResults(username, uri,
                                   method).startup(agent_id, agent_data))

            logger.info(status)

        else:
            status = (AgentResults(username, uri, method).startup_failed())
            logger.warn(status)

    except Exception as e:
        status = (GenericResults(username, uri, method).something_broke(
            agent_id, 'startup', e))

        logger.exception(status)

    return (status)
Beispiel #38
0
def get_severity_bar_chart_stats_for_tag(username, customer_name,
                                         uri, method, tag_id, conn=None):
    try:
        sevs = (
            r
            .table(TagsPerAgentCollection, use_outdated=True)
            .get_all(tag_id, index=TagsPerAgentIndexes.TagId)
            .pluck(TagsPerAgentKey.AgentId)
            .eq_join(
                lambda x: [
                    AVAILABLE,
                    x[AppsPerAgentKey.AgentId]
                ],
                r.table(AppsPerAgentCollection),
                index=AppsPerAgentIndexes.StatusAndAgentId
            )
            .map(
                {
                    AppsKey.AppId: r.row['right'][AppsKey.AppId],
                }
            )
            .eq_join(AppsKey.AppId, r.table(AppsCollection))
            .map(
                {
                    AppsKey.AppId: r.row['right'][AppsKey.AppId],
                    AppsKey.RvSeverity: r.row['right'][AppsKey.RvSeverity]
                }
            )
            .group_by(AppsKey.RvSeverity, r.count)
            .order_by(r.desc('reduction'))
            .run(conn)
        )
        data = app_stats_by_severity(sevs)

        results = (
            GenericResults(
                username, uri, method
            ).information_retrieved(data, len(ValidRvSeverities))
        )

    except Exception as e:
        results = (
            GenericResults(
                username, uri, method
            ).something_broke('widget severity stats', 'widget', e)
        )
        logger.exception(results)

    return(results)
Beispiel #39
0
def get_tags_info(customer_name=None, keys_to_pluck=None, conn=None):

    if keys_to_pluck and not customer_name:
        tags = list(r.table(TagsCollection).pluck(keys_to_pluck).run(conn))

    elif keys_to_pluck and customer_name:
        tags = list(
            r.table(TagsCollection).get_all(
                customer_name,
                index=TagsIndexes.CustomerName).pluck(keys_to_pluck).run(conn))

    else:
        tags = list(r.table(TagsCollection).run(conn))

    return (tags)
Beispiel #40
0
    def get_all(self, conn=None):
        try:
            count = (
                r
                .table(TagsCollection)
                .get_all(self.customer_name, index=TagsIndexes.CustomerName)
                .count()
                .run(conn)
            )
            data = list(
                r
                .table(TagsCollection)
                .get_all(self.customer_name, index=TagsIndexes.CustomerName)
                .order_by(self.sort(self.sort_key))
                .skip(self.qoffset)
                .limit(self.qcount)
                .run(conn)
            )

            if data:
                for tag in xrange(len(data)):
                    data[tag][BASIC_RV_STATS] = (
                        get_all_avail_stats_by_tagid(
                            self.username, self.customer_name,
                            self.uri, self.method, data[tag][TagsKey.TagId]
                        )['data']
                    )

                    agents_in_tag = list(
                        r
                        .table(TagsPerAgentCollection)
                        .get_all(data[tag][TagsPerAgentKey.TagId],
                                 index=TagsPerAgentIndexes.TagId)
                        .eq_join(TagsPerAgentKey.AgentId, r.table(AgentsCollection))
                        .zip()
                        .pluck(
                            TagsPerAgentKey.AgentId, AgentKey.ComputerName,
                            AgentKey.DisplayName)
                        .run(conn)
                    )
                    data[tag]['agents'] = agents_in_tag

            status = (
                GenericResults(
                    self.username, self.uri, self.method
                ).information_retrieved(data, count)
            )

            logger.info(status['message'])

        except Exception as e:
            status = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke('get_all_tags', 'tags', e)
            )

            logger.exception(status['message'])

        return(status)
Beispiel #41
0
def delete_app_from_agent(
        app_name, app_version, agent_id,
        table=AppsCollection,
        per_agent_table=AppsPerAgentCollection,
        index_to_use=AppsIndexes.NameAndVersion,
        per_agent_index=AppsPerAgentIndexes.AgentIdAndAppId,
        conn=None
        ):
    try:
        app_agent_id = (
            r
            .table(table)
            .get_all([app_name, app_version], index=index_to_use)
            .eq_join(
                lambda x: [agent_id, x[AppsKey.AppId]],
                r.table(per_agent_table),
                index=per_agent_index
            )
            .zip()
            .map(lambda x: x[Id])
            .run(conn)
        )
        if app_agent_id:
            (
                r
                .table(per_agent_table)
                .get(app_agent_id[0])
                .delete()
                .run(conn)
            )

    except Exception as e:
        logger.exception(e)
Beispiel #42
0
def db_get(
    collection=None,
    primary_id=None,
    pluck=None,
    conn=None
):
    if (
        not collection
        or not primary_id
    ):
        return None

    query = r.table(collection).get(primary_id)

    if pluck:

        if isinstance(pluck, list):
            doc = query.pluck(*pluck).run(conn)
        else:
            doc = query.pluck(pluck).run(conn)

    else:
        doc = query.run(conn)

    return doc
Beispiel #43
0
def db_delete(collection=None, _id=None, conn=None):
    """Attempts to delete data from the DB.

    Tries to delete a document based on the id or filter provided. If filter is
    used, the first document returned is deleted.

    Args:

        collection_name: Name of the collection to be used.

        _id: Id (primary key) representing a document

    Returns:

        True if document was deleted, False otherwise.

    """

    success = None

    if _id:

        result = r.table(collection).get(_id).delete().run(conn)

        if 'deleted' in result and result['deleted'] > 0:

            success = True

    return success
Beispiel #44
0
    def filter_by_status_and_query_by_name(self, name, pkg_status, conn=None):
        try:
            if pkg_status in ValidPackageStatuses:
                base = (
                    r
                    .table(self.CurrentAppsPerAgentCollection)
                    .get_all(
                        pkg_status, index=self.CurrentAppsPerAgentIndexes.Status
                    )
                    .eq_join(self.CurrentAppsPerAgentKey.AppId, r.table(self.CurrentAppsCollection))
                    .map(self.joined_map_hash)
                )

                if self.show_hidden == NO:
                    base = base.filter({self.CurrentAppsKey.Hidden: NO})

                packages = list(
                    base
                    .filter(lambda x: x[self.CurrentAppsKey.Name].match("(?i)"+name))
                    .distinct()
                    .order_by(self.sort(self.sort_key))
                    .skip(self.offset)
                    .limit(self.count)
                    .run(conn)
                )

                pkg_count = (
                    base
                    .filter(lambda x: x[self.CurrentAppsKey.Name].match("(?i)"+name))
                    .pluck(self.CurrentAppsKey.AppId)
                    .count()
                    .run(conn)
                )

                return_status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).information_retrieved(packages, pkg_count)
                )

            else:
                return_status = (
                    PackageResults(
                        self.username, self.uri, self.method
                    ).invalid_global_status(pkg_status)
                )

        except Exception as e:
            return_status = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke(
                    "Package Searching went haywire",
                    'os_updates', e
                    )
            )
            logger.exception(e)

        return(return_status)
Beispiel #45
0
def get_tags_info(customer_name=None, keys_to_pluck=None, conn=None):

    if keys_to_pluck and not customer_name:
        tags = list(r.table(TagsCollection).pluck(keys_to_pluck).run(conn))

    elif keys_to_pluck and customer_name:
        tags = list(
            r.table(TagsCollection)
            .get_all(customer_name, index=TagsIndexes.CustomerName)
            .pluck(keys_to_pluck)
            .run(conn)
        )

    else:
        tags = list(r.table(TagsCollection).run(conn))

    return tags
Beispiel #46
0
    def get_tag(self, tag_id, conn=None):
        try:
            tag_info = (
                r
                .table(TagsCollection)
                .get(tag_id)
                .run(conn)
            )

            if tag_info:
                agents_in_tag = list(
                    r
                    .table(TagsPerAgentCollection)
                    .get_all(tag_id, index=TagsPerAgentIndexes.TagId)
                    .eq_join(TagsPerAgentKey.AgentId, r.table(AgentsCollection))
                    .zip()
                    .pluck(
                        TagsPerAgentKey.AgentId, AgentKey.ComputerName,
                        AgentKey.DisplayName)
                    .run(conn)
                )

                tag_info[BASIC_RV_STATS] = (
                    get_all_app_stats_by_tagid(
                        self.username, self.customer_name,
                        self.uri, self.method, tag_id
                    )['data']
                )

                tag_info['agents'] = agents_in_tag

                status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).information_retrieved(tag_info, 1)
                )

                logger.info(status['message'])

            else:
                status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).invalid_id(tag_id, 'tag_id')
                )

                logger.info(status['message'])

        except Exception as e:
            status = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke(tag_id, 'tags', e)
            )

            logger.exception(status['message'])

        return(status)
Beispiel #47
0
def top_packages_needed(username, customer_name, uri, method, count=5, conn=None):

    apps_needed = []

    try:
        appid_needed = (
            r.table(AppsPerAgentCollection)
            .get_all([AVAILABLE, customer_name], index=AppsPerAgentIndexes.StatusAndCustomer)
            .group_by(AppsPerAgentKey.AppId, r.count)
            .order_by(r.desc("reduction"))
            .run(conn)
        )

        for i in xrange(len(appid_needed)):
            app_info = (
                r.table(AppsCollection)
                .get_all(appid_needed[i]["group"][AppsPerAgentKey.AppId], index=AppsIndexes.AppId)
                .pluck(AppsKey.Name, AppsKey.AppId, AppsKey.RvSeverity, AppsKey.ReleaseDate, AppsKey.Hidden)
                .map(
                    {
                        AppsKey.Name: r.row[AppsKey.Name],
                        AppsKey.AppId: r.row[AppsKey.AppId],
                        AppsKey.Hidden: r.row[AppsKey.Hidden],
                        AppsKey.RvSeverity: r.row[AppsKey.RvSeverity],
                        AppsKey.ReleaseDate: r.row[AppsKey.ReleaseDate].to_epoch_time(),
                        "count": appid_needed[i]["reduction"],
                    }
                )
                .run(conn)[0]
            )

            if app_info[AppsKey.Hidden] == "no":
                apps_needed.append(app_info)

            if len(apps_needed) == count:
                break

        results = GenericResults(username, uri, method).information_retrieved(apps_needed, count)

    except Exception as e:
        results = GenericResults(username, uri, method).something_broke("top os apps needed", "widget", e)
        logger.exception(results)

    return results
Beispiel #48
0
    def query_by_name(self, name, conn=None):
        try:
            agent = get_agent_info(self.agent_id)
            if agent:
                base = (
                    r
                    .table(self.CurrentAppsPerAgentCollection)
                    .get_all(self.agent_id, index=self.CurrentAppsPerAgentIndexes.AgentId)
                    .eq_join(self.CurrentAppsPerAgentKey.AppId, r.table(self.CurrentAppsCollection))
                    .zip()
                )
                if self.show_hidden == NO:
                    base = base.filter({self.CurrentAppsKey.Hidden: NO})

                packages = list(
                    base
                    .filter(lambda x: x[self.CurrentAppsKey.Name].match("(?i)"+name))
                    .map(self.map_hash)
                    .order_by(self.sort(self.sort_key))
                    .skip(self.offset)
                    .limit(self.count)
                    .run(conn)
                )

                pkg_count = (
                    base
                    .filter(lambda x: x[self.CurrentAppsKey.Name].match("(?i)"+name))
                    .count()
                    .run(conn)
                )

                return_status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).information_retrieved(packages, pkg_count)
                )

            else:
                return_status = (
                    GenericResults(
                        self.username, self.uri, self.method
                    ).invalid_id(self.agent_id, 'agents')
                )

        except Exception as e:
            return_status = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke(
                    "Package Searching went haywire",
                    'os_updates', e
                    )
            )
            logger.exception(e)

        return(return_status)
Beispiel #49
0
def delete_agent_from_all_tags(agent_id, conn=None):
    deleted = True
    try:
        (r.table(TagsPerAgentCollection).get_all(agent_id, index=TagsPerAgentIndexes.AgentId).delete().run(conn))

    except Exception as e:
        logger.exception(e)
        deleted = False

    return deleted
Beispiel #50
0
def tag_exists(tag_id, conn=None):
    tag_info = None

    try:
        tag_info = r.table(TagsCollection).get(tag_id).pluck(TagsKey.TagId, TagsKey.TagName).run(conn)

    except Exception as e:
        logger.exception(e)

    return tag_info
Beispiel #51
0
def db_get_all(
    collection=None,
    conn=None
):

    result = list(
        r.table(collection)
        .run(conn)
    )

    return result
Beispiel #52
0
def recently_released_packages(username, customer_name, uri, method, count=5, conn=None):

    app_needed = []

    try:
        data = list(
            r.table(AppsPerAgentCollection, use_outdated=True)
            .get_all([AVAILABLE, customer_name], index=AppsPerAgentIndexes.StatusAndCustomer)
            .eq_join(AppsKey.AppId, r.table(AppsCollection))
            .map(
                {
                    AppsKey.Name: r.row["right"][AppsKey.Name],
                    AppsKey.AppId: r.row["right"][AppsKey.AppId],
                    AppsKey.RvSeverity: r.row["right"][AppsKey.RvSeverity],
                    AppsKey.Hidden: r.row["right"][AppsKey.Hidden],
                    AppsKey.ReleaseDate: r.row["right"][AppsKey.ReleaseDate].to_epoch_time(),
                    "count": (
                        r.table(AppsPerAgentCollection)
                        .get_all([r.row["right"][AppsKey.AppId], AVAILABLE], index=AppsPerAgentIndexes.AppIdAndStatus)
                        .count()
                    ),
                }
            )
            .pluck(AppsKey.Name, AppsKey.AppId, AppsKey.Hidden, AppsKey.RvSeverity, AppsKey.ReleaseDate, "count")
            .order_by(r.desc(AppsKey.ReleaseDate))
            .run(conn)
        )

        for i in xrange(len(data)):
            if data[i][AppsKey.Hidden] == "no":
                app_needed.append(data[i])
            if len(app_needed) == count:
                break

        results = GenericResults(username, uri, method).information_retrieved(app_needed, count)

    except Exception as e:
        results = GenericResults(username, uri, method).something_broke("recently released os apps", "widget", e)
        logger.exception(results)

    return results
Beispiel #53
0
def get_agent_ids_from_tag(tag_id=None, conn=None):
    agent_ids = []
    if tag_id:
        agent_ids = (
            r.table(TagsPerAgentCollection)
            .get_all(tag_id, index=TagsPerAgentIndexes.TagId)
            .map(lambda x: x[TagsPerAgentKey.AgentId])
            .distinct()
            .run(conn)
        )

    return agent_ids
Beispiel #54
0
def get_customer(customer_name=None, conn=None):

    if not customer_name:
        return None

    customer = (
        r.table(Collection.Customers)
        .get(customer_name)
        .run(conn)
    )

    return customer
Beispiel #55
0
def get_user(user_name=None, conn=None):

    if not user_name:
        return None

    user = (
        r.table(Collection.Users)
        .get(user_name)
        .run(conn)
    )

    return user
Beispiel #56
0
    def add_tags_to_agent(self, tag_id, agent_id, conn=None):

        try:
            tag_for_agent_exists = list(
                r.table(TagsPerAgentCollection)
                .get_all([agent_id, tag_id], index=TagsPerAgentIndexes.AgentIdAndTagId)
                .run(conn)
            )

            tag_info = r.table(TagsCollection).get(tag_id).run(conn)
            tag_name = tag_info[TagsKey.TagName]

            if not tag_for_agent_exists:
                agent_info = r.table(AgentsCollection).get(agent_id).run(conn)

                if agent_info:
                    tag_agent_info = {TagsPerAgentKey.AgentId: agent_id, TagsPerAgentKey.TagId: tag_id}

                    add_agent_to_tag = r.table(TagsPerAgentCollection).insert(tag_agent_info).run(conn)

                    if "inserted" in add_agent_to_tag:
                        status = GenericResults(self.username, self.uri, self.method).object_updated(
                            tag_id, "tag_id", tag_agent_info
                        )

                        logger.info(status["message"])

            else:
                status = GenericResults(self.username, self.uri, self.method).object_exists(tag_id, "tag")

                logger.warn(status["message"])

        except Exception as e:
            status = GenericResults(self.username, self.uri, self.method).something_broke(
                tag_id, "adding tag to agent", e
            )

            logger.exception(e)

        return status
Beispiel #57
0
def get_tag_ids_by_agent_id(agent_id=None, conn=None):
    tag_ids = []
    if agent_id:
        tag_ids = (
            r.table(TagsPerAgentCollection)
            .get_all(agent_id, index=TagsPerAgentIndexes.AgentId)
            .pluck(TagsPerAgentKey.TagId)
            .distinct()
            .map(lambda x: x[TagsPerAgentKey.TagId])
            .run(conn)
        )

    return tag_ids
Beispiel #58
0
    def create_tag(self, tag_name, prod_level="Production", conn=None):
        tag_id = None
        ninsert = {
            TagsKey.TagName: tag_name,
            TagsKey.CustomerName: self.customer_name,
            TagsKey.ProductionLevel: prod_level,
        }
        try:
            tag_exists = list(
                r.table(TagsCollection)
                .get_all([self.customer_name, tag_name], index=TagsIndexes.TagNameAndCustomer)
                .pluck(TagsKey.TagId)
                .run(conn)
            )
            if len(tag_exists) == 0:
                inserted = r.table(TagsCollection).insert(ninsert).run(conn)
                if "inserted" in inserted:
                    tag_id = inserted["generated_keys"][0]
                    data = {TagsKey.TagId: tag_id, TagsKey.TagName: tag_name}

                    status = GenericResults(self.username, self.uri, self.method).object_created(tag_id, tag_name, data)

                    logger.info(status["message"])

            else:
                status = GenericResults(self.username, self.uri, self.method).object_exists(tag_id, tag_name)

                logger.warn(status["message"])

        except Exception as e:
            status = GenericResults(self.username, self.uri, self.method).something_broke(
                tag_name, "while creating a tag", e
            )

            logger.exception(e)

        return status
Beispiel #59
0
    def get_all_operations_by_agentid(self, agent_id, conn=None):
        try:
            count = (
                r
                .table(OperationsPerAgentCollection)
                .get_all(
                    [agent_id, self.customer_name],
                    index=OperationPerAgentIndexes.AgentIdAndCustomer
                )
                .count()
                .run(conn)
            )

            operations = list(
                r
                .table(OperationsPerAgentCollection)
                .get_all(
                    [agent_id, self.customer_name],
                    index=OperationPerAgentIndexes.AgentIdAndCustomer
                )
                .eq_join(
                    OperationKey.OperationId,
                    r.table(OperationsCollection)
                )
                .zip()
                .pluck(self.pluck_list)
                .order_by(self.sort(self.sort_key))
                .skip(self.offset)
                .limit(self.count)
                .map(self.map_hash)
                .run(conn)
            )

            results = (
                GenericResults(
                    self.username, self.uri, self.method
                ).information_retrieved(operations, count)
            )
            logger.info(results)

        except Exception as e:
            results = (
                GenericResults(
                    self.username, self.uri, self.method
                ).something_broke('Operations Query', 'Operations', e)
            )
            logger.exception(results)

        return(results)
Beispiel #60
0
def filter(collection=None, filter_value=None, conn=None):

    if(
        not collection
        or not filter_value
    ):
        return False

    docs = list(
        r.table(collection)
        .filter(filter_value)
        .run(conn)
    )

    return docs