]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/ansible/output_wizards.py
update download target update for octopus release
[ceph.git] / ceph / src / pybind / mgr / ansible / output_wizards.py
CommitLineData
81eedcae
TL
1"""
2ceph-mgr Output Wizards module
3
4Output wizards are used to process results in different ways in
5completion objects
6"""
7
8# pylint: disable=bad-continuation
9
10import json
11
12
13from orchestrator import InventoryDevice, InventoryNode
14
15from .ansible_runner_svc import EVENT_DATA_URL
16
17class OutputWizard(object):
18 """Base class for help to process output in completion objects
19 """
20 def __init__(self, ar_client, logger):
21 """Make easy to work in output wizards using this attributes:
22
23 :param ars_client: Ansible Runner Service client
24 :param logger: log object
25 """
26 self.ar_client = ar_client
27 self.log = logger
28
29 def process(self, operation_id, raw_result):
30 """Make the magic here
31
32 :param operation_id: Allows to identify the Ansible Runner Service
33 operation whose result we wnat to process
34 :param raw_result: input for processing
35 """
36 raise NotImplementedError
37
38class ProcessInventory(OutputWizard):
39 """ Adapt the output of the playbook used in 'get_inventory'
40 to the Orchestrator expected output (list of InventoryNode)
41 """
42
43 def process(self, operation_id, raw_result):
44 """
45 :param operation_id: Playbook uuid
46 :param raw_result: events dict with the results
47
48 Example:
49 inventory_events =
50 {'37-100564f1-9fed-48c2-bd62-4ae8636dfcdb': {'host': '192.168.121.254',
51 'task': 'list storage inventory',
52 'event': 'runner_on_ok'},
53 '36-2016b900-e38f-7dcd-a2e7-00000000000e': {'host': '192.168.121.252'
54 'task': 'list storage inventory',
55 'event': 'runner_on_ok'}}
56
57 :return : list of InventoryNode
58 """
59 # Just making more readable the method
60 inventory_events = raw_result
61
62 #Obtain the needed data for each result event
63 inventory_nodes = []
64
65 # Loop over the result events and request the event data
66 for event_key, dummy_data in inventory_events.items():
67
68 event_response = self.ar_client.http_get(EVENT_DATA_URL %
69 (operation_id, event_key))
70
71 # self.pb_execution.play_uuid
72
73 # Process the data for each event
74 if event_response:
75 event_data = json.loads(event_response.text)["data"]["event_data"]
76
77 host = event_data["host"]
78
79 devices = json.loads(event_data["res"]["stdout"])
80 devs = []
81 for storage_device in devices:
82 dev = InventoryDevice.from_ceph_volume_inventory(storage_device)
83 devs.append(dev)
84
85 inventory_nodes.append(InventoryNode(host, devs))
86
87
88 return inventory_nodes
89
90class ProcessPlaybookResult(OutputWizard):
91 """ Provides the result of a playbook execution as plain text
92 """
93 def process(self, operation_id, raw_result):
94 """
95 :param operation_id: Playbook uuid
96 :param raw_result: events dict with the results
97
98 :return : String with the playbook execution event list
99 """
100 # Just making more readable the method
101 inventory_events = raw_result
102
103 result = ""
104
105 # Loop over the result events and request the data
106 for event_key, dummy_data in inventory_events.items():
107 event_response = self.ar_client.http_get(EVENT_DATA_URL %
108 (operation_id, event_key))
109
110 result += event_response.text
111
112 return result
113
114
115class ProcessHostsList(OutputWizard):
116 """ Format the output of host ls call
117 """
118 def process(self, operation_id, raw_result):
119 """ Format the output of host ls call
120
121 :param operation_id: Not used in this output wizard
122 :param raw_result: In this case is like the following json:
123 {
124 "status": "OK",
125 "msg": "",
126 "data": {
127 "hosts": [
128 "host_a",
129 "host_b",
130 ...
131 "host_x",
132 ]
133 }
134 }
135
136 :return: list of InventoryNodes
137 """
138 # Just making more readable the method
139 host_ls_json = raw_result
140
141 inventory_nodes = []
142
143 try:
144 json_resp = json.loads(host_ls_json)
145
146 for host in json_resp["data"]["hosts"]:
147 inventory_nodes.append(InventoryNode(host, []))
148
149 except ValueError:
150 self.log.exception("Malformed json response")
151 except KeyError:
152 self.log.exception("Unexpected content in Ansible Runner Service"
153 " response")
154 except TypeError:
155 self.log.exception("Hosts data must be iterable in Ansible Runner "
156 "Service response")
157
158 return inventory_nodes