Ensure library and tests close all necessary resources.
[snaps.git] / snaps / playbook_runner.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import argparse
16 import logging
17
18 import re
19
20 from snaps.openstack.os_credentials import ProxySettings
21 from snaps.provisioning import ansible_utils
22
23 __author__ = 'spisarski'
24
25 logger = logging.getLogger('playbook_runner')
26
27
28 def main(parsed_args):
29     """
30     Uses ansible_utils for applying Ansible Playbooks to machines with a
31     private key
32     """
33     logging.basicConfig(level=logging.DEBUG)
34     logger.info('Starting Playbook Runner')
35
36     proxy_settings = None
37     if parsed_args.http_proxy:
38         tokens = re.split(':', parsed_args.http_proxy)
39         proxy_settings = ProxySettings(host=tokens[0], port=tokens[1],
40                                        ssh_proxy_cmd=parsed_args.ssh_proxy_cmd)
41
42     # Ensure can get an SSH client
43     ssh = ansible_utils.ssh_client(parsed_args.ip_addr, parsed_args.host_user,
44                                    parsed_args.priv_key, proxy_settings)
45     if ssh:
46         ssh.close()
47
48     retval = ansible_utils.apply_playbook(
49         parsed_args.playbook, [parsed_args.ip_addr], parsed_args.host_user,
50         parsed_args.priv_key, variables={'name': 'Foo'},
51         proxy_setting=proxy_settings)
52     exit(retval)
53
54
55 if __name__ == '__main__':
56     parser = argparse.ArgumentParser()
57     parser.add_argument('-a', '--ip-addr', dest='ip_addr', required=True,
58                         help='The Host IP Address')
59     parser.add_argument('-k', '--priv-key', dest='priv_key', required=True,
60                         help='The location of the private key file')
61     parser.add_argument('-u', '--host-user', dest='host_user', required=True,
62                         help='Host user account')
63     parser.add_argument('-b', '--playbook', dest='playbook', required=True,
64                         help='Playbook Location')
65     parser.add_argument('-p', '--http-proxy', dest='http_proxy',
66                         required=False, help='<host>:<port>')
67     parser.add_argument('-s', '--ssh-proxy-cmd', dest='ssh_proxy_cmd',
68                         required=False)
69     args = parser.parse_args()
70
71     main(args)