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 | |
| 15 | |
| 16 | import os |
| 17 | import pickle |
| 18 | import sys |
| 19 | |
| 20 | # import json |
| 21 | import traceback |
| 22 | from xosconfig import Config |
| 23 | |
| 24 | sys.path.append("/opt/xos") |
| 25 | |
| 26 | |
| 27 | def run_playbook(ansible_hosts, ansible_config, fqp, opts): |
| 28 | try: |
| 29 | if ansible_config: |
| 30 | os.environ["ANSIBLE_CONFIG"] = ansible_config |
| 31 | else: |
| 32 | try: |
| 33 | del os.environ["ANSIBLE_CONFIG"] |
| 34 | except KeyError: |
| 35 | pass |
| 36 | |
| 37 | if ansible_hosts: |
| 38 | os.environ["ANSIBLE_HOSTS"] = ansible_hosts |
| 39 | else: |
| 40 | try: |
| 41 | del os.environ["ANSIBLE_HOSTS"] |
| 42 | except KeyError: |
| 43 | pass |
| 44 | |
| 45 | import ansible_runner |
| 46 | |
| 47 | reload(ansible_runner) |
| 48 | |
| 49 | # Dropped support for observer_pretend - to be redone |
| 50 | runner = ansible_runner.Runner( |
| 51 | playbook=fqp, run_data=opts, host_file=ansible_hosts |
| 52 | ) |
| 53 | |
| 54 | stats, aresults = runner.run() |
| 55 | except Exception as e: |
| 56 | return {"stats": None, "aresults": None, "exception": traceback.format_exc()} |
| 57 | |
| 58 | return {"stats": stats, "aresults": aresults} |
| 59 | |
| 60 | |
| 61 | def main(): |
| 62 | input_fn = sys.argv[1] |
| 63 | result_fn = sys.argv[2] |
| 64 | |
| 65 | args = pickle.loads(open(input_fn).read()) |
| 66 | |
| 67 | Config.init(args["config_file"], "synchronizer-config-schema.yaml") |
| 68 | |
| 69 | ansible_hosts = args["ansible_hosts"] |
| 70 | ansible_config = args["ansible_config"] |
| 71 | fqp = args["fqp"] |
| 72 | opts = args["opts"] |
| 73 | |
| 74 | result = run_playbook(ansible_hosts, ansible_config, fqp, opts) |
| 75 | |
| 76 | open(result_fn, "w").write(pickle.dumps(result)) |
| 77 | |
| 78 | |
| 79 | if __name__ == "__main__": |
| 80 | main() |