Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 16 | |
| 17 | import os |
| 18 | import pickle |
| 19 | import sys |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 20 | import traceback |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 21 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 22 | from xosconfig import Config |
| 23 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 24 | try: |
| 25 | # Python 2: "reload" is built-in |
| 26 | reload # pylint: disable=reload-builtin |
| 27 | except NameError: |
| 28 | from importlib import reload |
| 29 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 30 | sys.path.append("/opt/xos") |
| 31 | |
| 32 | |
| 33 | def run_playbook(ansible_hosts, ansible_config, fqp, opts): |
| 34 | try: |
| 35 | if ansible_config: |
| 36 | os.environ["ANSIBLE_CONFIG"] = ansible_config |
| 37 | else: |
| 38 | try: |
| 39 | del os.environ["ANSIBLE_CONFIG"] |
| 40 | except KeyError: |
| 41 | pass |
| 42 | |
| 43 | if ansible_hosts: |
| 44 | os.environ["ANSIBLE_HOSTS"] = ansible_hosts |
| 45 | else: |
| 46 | try: |
| 47 | del os.environ["ANSIBLE_HOSTS"] |
| 48 | except KeyError: |
| 49 | pass |
| 50 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 51 | from . import ansible_runner |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 52 | |
| 53 | reload(ansible_runner) |
| 54 | |
| 55 | # Dropped support for observer_pretend - to be redone |
| 56 | runner = ansible_runner.Runner( |
| 57 | playbook=fqp, run_data=opts, host_file=ansible_hosts |
| 58 | ) |
| 59 | |
| 60 | stats, aresults = runner.run() |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 61 | except Exception: |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 62 | return {"stats": None, "aresults": None, "exception": traceback.format_exc()} |
| 63 | |
| 64 | return {"stats": stats, "aresults": aresults} |
| 65 | |
| 66 | |
| 67 | def main(): |
| 68 | input_fn = sys.argv[1] |
| 69 | result_fn = sys.argv[2] |
| 70 | |
| 71 | args = pickle.loads(open(input_fn).read()) |
| 72 | |
| 73 | Config.init(args["config_file"], "synchronizer-config-schema.yaml") |
| 74 | |
| 75 | ansible_hosts = args["ansible_hosts"] |
| 76 | ansible_config = args["ansible_config"] |
| 77 | fqp = args["fqp"] |
| 78 | opts = args["opts"] |
| 79 | |
| 80 | result = run_playbook(ansible_hosts, ansible_config, fqp, opts) |
| 81 | |
| 82 | open(result_fn, "w").write(pickle.dumps(result)) |
| 83 | |
| 84 | |
| 85 | if __name__ == "__main__": |
| 86 | main() |