|
| 1 | +#!/usr/bin/python3 |
| 2 | +# |
| 3 | +# Copyright 2015 MarkLogic Corporation |
| 4 | +# |
| 5 | +# This script attempts to read all of the resource types on the cluster. |
| 6 | +# The point of this script is to make sure that we catch any new properties |
| 7 | +# that have been added by the server. |
| 8 | + |
| 9 | +__author__ = 'ndw' |
| 10 | + |
| 11 | +import argparse |
| 12 | +import logging |
| 13 | +import json |
| 14 | +import logging |
| 15 | +import sys |
| 16 | +from requests.auth import HTTPDigestAuth |
| 17 | +from marklogic.connection import Connection |
| 18 | +from marklogic.models.cluster import LocalCluster |
| 19 | +from marklogic.models.group import Group |
| 20 | +from marklogic.models.host import Host |
| 21 | +from marklogic.models.database import Database |
| 22 | +from marklogic.models.permission import Permission |
| 23 | +from marklogic.models.privilege import Privilege |
| 24 | +from marklogic.models.role import Role |
| 25 | +from marklogic.models.forest import Forest |
| 26 | +from marklogic.models.server import Server |
| 27 | +from marklogic.models.user import User |
| 28 | + |
| 29 | +class ReadEverything: |
| 30 | + def __init__(self, connection): |
| 31 | + self.databases = {} |
| 32 | + self.forests = {} |
| 33 | + self.servers = {} |
| 34 | + self.users = {} |
| 35 | + self.roles = {} |
| 36 | + self.privileges = {} |
| 37 | + self.connection = connection |
| 38 | + pass |
| 39 | + |
| 40 | + def readClass(self, kind, klass, max_read=sys.maxsize): |
| 41 | + names = klass.list(self.connection) |
| 42 | + for name in names: |
| 43 | + if max_read > 0: |
| 44 | + if name.find("|") > 0: |
| 45 | + parts = name.split("|") |
| 46 | + rsrc = klass.lookup(self.connection, parts[0], parts[1]) |
| 47 | + else: |
| 48 | + rsrc = klass.lookup(self.connection, name) |
| 49 | + max_read = max_read - 1 |
| 50 | + print("{}: {}".format(kind, len(names))) |
| 51 | + |
| 52 | + def readPrivileges(self): |
| 53 | + names = Privilege.list(self.connection) |
| 54 | + max_read = { "execute": 5, "uri": 5 } |
| 55 | + counts = { "execute": 0, "uri": 0 } |
| 56 | + for name in names: |
| 57 | + parts = name.split("|") |
| 58 | + kind = parts[0] |
| 59 | + pname = parts[1] |
| 60 | + |
| 61 | + counts[kind] = counts[kind] + 1 |
| 62 | + |
| 63 | + if max_read[kind] > 0: |
| 64 | + rsrc = Privilege.lookup(self.connection, pname, kind) |
| 65 | + max_read[kind] = max_read[kind] - 1 |
| 66 | + |
| 67 | + print("Execute privileges: {}".format(counts["execute"])) |
| 68 | + print("URI privileges: {}".format(counts["uri"])) |
| 69 | + |
| 70 | + def read(self): |
| 71 | + conn = self.connection |
| 72 | + cluster = LocalCluster(connection=conn).read() |
| 73 | + print("Read local cluster: {}".format(cluster.cluster_name())) |
| 74 | + |
| 75 | + x = cluster.security_version() |
| 76 | + x = cluster.effective_version() |
| 77 | + x = cluster.cluster_id() |
| 78 | + x = cluster.cluster_name() |
| 79 | + x = cluster.ssl_fips_enabled() |
| 80 | + x = cluster.xdqp_ssl_certificate() |
| 81 | + x = cluster.xdqp_ssl_private_key() |
| 82 | + x = cluster.bootstrap_hosts() |
| 83 | + # FIXME: |
| 84 | + #x = cluster.foreign_cluster_id() |
| 85 | + #x = cluster.foreign_cluster_name() |
| 86 | + x = cluster.language_baseline() |
| 87 | + x = cluster.opsdirector_log_level() |
| 88 | + x = cluster.opsdirector_metering() |
| 89 | + x = cluster.opsdirector_session_endpoint() |
| 90 | + |
| 91 | + self.readClass("Groups", Group, max_read=5) |
| 92 | + self.readClass("Hosts", Host, max_read=5) |
| 93 | + self.readClass("Databases", Database, max_read=5) |
| 94 | + self.readClass("Forests", Forest, max_read=5) |
| 95 | + self.readClass("Servers", Server) |
| 96 | + self.readClass("Roles", Role, max_read=5) |
| 97 | + self.readClass("Users", User, max_read=5) |
| 98 | + self.readPrivileges() |
| 99 | + |
| 100 | + return |
| 101 | + |
| 102 | +logging.basicConfig(level=logging.INFO) |
| 103 | + |
| 104 | +parser = argparse.ArgumentParser() |
| 105 | +parser.add_argument("--host", action='store', default="localhost", |
| 106 | + help="Management API host") |
| 107 | +parser.add_argument("--username", action='store', default="admin", |
| 108 | + help="User name") |
| 109 | +parser.add_argument("--password", action='store', default="admin", |
| 110 | + help="Password") |
| 111 | +parser.add_argument('--debug', action='store_true', |
| 112 | + help='Enable debug logging') |
| 113 | +args = parser.parse_args() |
| 114 | + |
| 115 | +if args.debug: |
| 116 | + logging.basicConfig(level=logging.WARNING) |
| 117 | + logging.getLogger("requests").setLevel(logging.WARNING) |
| 118 | + logging.getLogger("marklogic").setLevel(logging.DEBUG) |
| 119 | + |
| 120 | +conn = Connection(args.host, HTTPDigestAuth(args.username, args.password)) |
| 121 | +read_everything = ReadEverything(conn) |
| 122 | + |
| 123 | +print("Reading all resources from {}".format(args.host)) |
| 124 | + |
| 125 | +read_everything.read() |
| 126 | + |
| 127 | +print("Finished") |
0 commit comments