Fixed test for security groups when checking for project/tenant ID
[snaps.git] / snaps / playbook_runner.py
1 # Copyright (c) 2016 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 private key
31     """
32     logging.basicConfig(level=logging.DEBUG)
33     logger.info('Starting Playbook Runner')
34
35     proxy_settings = None
36     if parsed_args.http_proxy:
37         tokens = re.split(':', parsed_args.http_proxy)
38         proxy_settings = ProxySettings(tokens[0], tokens[1], parsed_args.ssh_proxy_cmd)
39
40     # Ensure can get an SSH client
41     ansible_utils.ssh_client(parsed_args.ip_addr, parsed_args.host_user, parsed_args.priv_key, proxy_settings)
42
43     retval = ansible_utils.apply_playbook(parsed_args.playbook, [parsed_args.ip_addr], parsed_args.host_user,
44                                           parsed_args.priv_key, variables={'name': 'Foo'}, proxy_setting=proxy_settings)
45     exit(retval)
46
47
48 if __name__ == '__main__':
49     parser = argparse.ArgumentParser()
50     parser.add_argument('-a', '--ip-addr', dest='ip_addr', required=True, help='The Host IP Address')
51     parser.add_argument('-k', '--priv-key', dest='priv_key', required=True, help='The location of the private key file')
52     parser.add_argument('-u', '--host-user', dest='host_user', required=True, help='Host user account')
53     parser.add_argument('-b', '--playbook', dest='playbook', required=True, help='Playbook Location')
54     parser.add_argument('-p', '--http-proxy', dest='http_proxy', required=False, help='<host>:<port>')
55     parser.add_argument('-s', '--ssh-proxy-cmd', dest='ssh_proxy_cmd', required=False)
56     args = parser.parse_args()
57
58     main(args)