|
| 1 | +#!/usr/bin/env python |
| 2 | +# Licensed to Cloudera, Inc. under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. Cloudera, Inc. licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +""" |
| 19 | +Lists all hosts managed by CM. Demonstrates connecting to CM via HTTPS using a |
| 20 | +custom CA certificate in PEM format. Requires Python 2.7.9 or higher. |
| 21 | +
|
| 22 | +Usage: %s hostname username password ca_cert_file [options] |
| 23 | +
|
| 24 | +Options: |
| 25 | +-h Displays usage |
| 26 | +""" |
| 27 | + |
| 28 | +import getopt |
| 29 | +import inspect |
| 30 | +import logging |
| 31 | +import ssl |
| 32 | +import sys |
| 33 | +import textwrap |
| 34 | + |
| 35 | +from cm_api.api_client import ApiResource |
| 36 | + |
| 37 | +def list_hosts(host, username, password, cafile): |
| 38 | + context = ssl.create_default_context(cafile=cafile) |
| 39 | + |
| 40 | + api = ApiResource(host, username=username, password=password, use_tls=True, |
| 41 | + ssl_context=context) |
| 42 | + |
| 43 | + for h in api.get_all_hosts(): |
| 44 | + print h.hostname |
| 45 | + |
| 46 | +def usage(): |
| 47 | + doc = inspect.getmodule(usage).__doc__ |
| 48 | + print >>sys.stderr, textwrap.dedent(doc % (sys.argv[0],)) |
| 49 | + |
| 50 | +def setup_logging(level): |
| 51 | + logging.basicConfig() |
| 52 | + logging.getLogger().setLevel(level) |
| 53 | + |
| 54 | +def main(argv): |
| 55 | + setup_logging(logging.INFO) |
| 56 | + |
| 57 | + # Argument parsing |
| 58 | + try: |
| 59 | + opts, args = getopt.getopt(argv[1:], "h") |
| 60 | + except getopt.GetoptError, err: |
| 61 | + print >>sys.stderr, err |
| 62 | + usage() |
| 63 | + return -1 |
| 64 | + |
| 65 | + for option, val in opts: |
| 66 | + if option == '-h': |
| 67 | + usage() |
| 68 | + return -1 |
| 69 | + else: |
| 70 | + print >>sys.stderr, "Unknown flag:", option |
| 71 | + usage() |
| 72 | + return -1 |
| 73 | + |
| 74 | + if len(args) < 4: |
| 75 | + print >>sys.stderr, "Invalid number of arguments" |
| 76 | + usage() |
| 77 | + return -1 |
| 78 | + |
| 79 | + # Do work |
| 80 | + list_hosts(*args) |
| 81 | + return 0 |
| 82 | + |
| 83 | + |
| 84 | +# |
| 85 | +# The "main" entry |
| 86 | +# |
| 87 | +if __name__ == '__main__': |
| 88 | + sys.exit(main(sys.argv)) |
0 commit comments