blob: 6a8c7117ed2fb933ac40e0fb1234dd7912d127d9 [file] [log] [blame]
Scott Bakerbba67b62019-01-28 17:38:21 -08001# 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 Williams5c2ea232019-01-30 15:23:01 -070015from __future__ import absolute_import
Scott Bakerbba67b62019-01-28 17:38:21 -080016
17import os
18import pickle
19import sys
Scott Bakerbba67b62019-01-28 17:38:21 -080020import traceback
Zack Williams5c2ea232019-01-30 15:23:01 -070021
Scott Bakerbba67b62019-01-28 17:38:21 -080022from xosconfig import Config
23
Zack Williams5c2ea232019-01-30 15:23:01 -070024try:
25 # Python 2: "reload" is built-in
26 reload # pylint: disable=reload-builtin
27except NameError:
28 from importlib import reload
29
Scott Bakerbba67b62019-01-28 17:38:21 -080030sys.path.append("/opt/xos")
31
32
33def 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 Williams5c2ea232019-01-30 15:23:01 -070051 from . import ansible_runner
Scott Bakerbba67b62019-01-28 17:38:21 -080052
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 Williams5c2ea232019-01-30 15:23:01 -070061 except Exception:
Scott Bakerbba67b62019-01-28 17:38:21 -080062 return {"stats": None, "aresults": None, "exception": traceback.format_exc()}
63
64 return {"stats": stats, "aresults": aresults}
65
66
67def 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
85if __name__ == "__main__":
86 main()