"Fossies" - the Fresh Open Source Software Archive 
Member "salt-3002.2/salt/proxy/dummy.py" (18 Nov 2020, 6265 Bytes) of package /linux/misc/salt-3002.2.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "dummy.py" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
3002.1_vs_3002.2.
1 """
2 This is the a dummy proxy-minion designed for testing the proxy minion subsystem.
3 """
4
5 import copy
6 import logging
7 import os
8 import pprint
9 from contextlib import contextmanager
10
11 import salt.utils.files
12 import salt.utils.msgpack
13
14 # This must be present or the Salt loader won't load this module
15 __proxyenabled__ = ["dummy"]
16
17 log = logging.getLogger(__file__)
18
19
20 # This does nothing, it's here just as an example and to provide a log
21 # entry when the module is loaded.
22 def __virtual__():
23 """
24 Only return if all the modules are available
25 """
26 log.debug("dummy proxy __virtual__() called...")
27 return True
28
29
30 def _save_state(opts, details):
31 cachefile = os.path.join(opts["cachedir"], "dummy-proxy.cache")
32 with salt.utils.files.fopen(cachefile, "wb") as pck:
33 pck.write(salt.utils.msgpack.packb(details, use_bin_type=True))
34 log.warning("Dummy Proxy Saved State(%s):\n%s", cachefile, pprint.pformat(details))
35
36
37 def _load_state(opts):
38 cachefile = os.path.join(opts["cachedir"], "dummy-proxy.cache")
39 try:
40 with salt.utils.files.fopen(cachefile, "rb") as pck:
41 state = salt.utils.msgpack.unpackb(pck.read(), raw=False)
42 except FileNotFoundError:
43 state = _initial_state()
44 _save_state(opts, state)
45 except Exception as exc: # pylint: disable=broad-except
46 log.exception("Failed to load state: %s", exc, exc_info=True)
47 state = _initial_state()
48 _save_state(opts, state)
49 log.warning("Dummy Proxy Loaded State(%s):\n%s", cachefile, pprint.pformat(state))
50 return state
51
52
53 @contextmanager
54 def _loaded_state(opts):
55 state = _load_state(opts)
56 original = copy.deepcopy(state)
57 try:
58 yield state
59 finally:
60 if state != original:
61 _save_state(opts, state)
62
63
64 def _initial_state():
65 return {
66 "services": {"apache": "running", "ntp": "running", "samba": "stopped"},
67 "packages": {
68 "coreutils": "1.0",
69 "apache": "2.4",
70 "tinc": "1.4",
71 "redbull": "999.99",
72 },
73 }
74
75
76 # Every proxy module needs an 'init', though you can
77 # just put DETAILS['initialized'] = True here if nothing
78 # else needs to be done.
79
80
81 def init(opts):
82 log.debug("dummy proxy init() called...")
83 with _loaded_state(opts) as state:
84 state["initialized"] = True
85
86
87 def initialized():
88 """
89 Since grains are loaded in many different places and some of those
90 places occur before the proxy can be initialized, return whether
91 our init() function has been called
92 """
93 with _loaded_state(__opts__) as state:
94 return state.get("initialized", False)
95
96
97 def grains():
98 """
99 Make up some grains
100 """
101 with _loaded_state(__opts__) as state:
102 if "grains_cache" not in state:
103 state["grains_cache"] = {
104 "dummy_grain_1": "one",
105 "dummy_grain_2": "two",
106 "dummy_grain_3": "three",
107 }
108 return state["grains_cache"]
109
110
111 def grains_refresh():
112 """
113 Refresh the grains
114 """
115 with _loaded_state(__opts__) as state:
116 if "grains_cache" in state:
117 state.pop("grains_cache")
118 return grains()
119
120
121 def fns():
122 return {
123 "details": "This key is here because a function in "
124 "grains/rest_sample.py called fns() here in the proxymodule."
125 }
126
127
128 def service_start(name):
129 """
130 Start a "service" on the dummy server
131 """
132 with _loaded_state(__opts__) as state:
133 state["services"][name] = "running"
134 return "running"
135
136
137 def service_stop(name):
138 """
139 Stop a "service" on the dummy server
140 """
141 with _loaded_state(__opts__) as state:
142 state["services"][name] = "stopped"
143 return "stopped"
144
145
146 def service_restart(name):
147 """
148 Restart a "service" on the REST server
149 """
150 return True
151
152
153 def service_list():
154 """
155 List "services" on the REST server
156 """
157 with _loaded_state(__opts__) as state:
158 return list(state["services"])
159
160
161 def service_status(name):
162 """
163 Check if a service is running on the REST server
164 """
165 with _loaded_state(__opts__) as state:
166 if state["services"][name] == "running":
167 return {"comment": "running"}
168 else:
169 return {"comment": "stopped"}
170
171
172 def package_list():
173 """
174 List "packages" installed on the REST server
175 """
176 with _loaded_state(__opts__) as state:
177 return state["packages"]
178
179
180 def package_install(name, **kwargs):
181 """
182 Install a "package" on the REST server
183 """
184 if kwargs.get("version", False):
185 version = kwargs["version"]
186 else:
187 version = "1.0"
188 with _loaded_state(__opts__) as state:
189 state["packages"][name] = version
190 return {name: version}
191
192
193 def upgrade():
194 """
195 "Upgrade" packages
196 """
197 with _loaded_state(__opts__) as state:
198 for p in state["packages"]:
199 version_float = float(state["packages"][p])
200 version_float = version_float + 1.0
201 state["packages"][p] = str(version_float)
202 return state["packages"]
203
204
205 def uptodate():
206 """
207 Call the REST endpoint to see if the packages on the "server" are up to date.
208 """
209 with _loaded_state(__opts__) as state:
210 return state["packages"]
211
212
213 def package_remove(name):
214 """
215 Remove a "package" on the REST server
216 """
217 with _loaded_state(__opts__) as state:
218 state["packages"].pop(name)
219 return state["packages"]
220
221
222 def package_status(name):
223 """
224 Check the installation status of a package on the REST server
225 """
226 with _loaded_state(__opts__) as state:
227 if name in state["packages"]:
228 return {name: state["packages"][name]}
229
230
231 def ping():
232 """
233 Degenerate ping
234 """
235 log.debug("dummy proxy returning ping")
236 return True
237
238
239 def shutdown(opts):
240 """
241 For this proxy shutdown is a no-op
242 """
243 log.debug("dummy proxy shutdown() called...")
244 with _loaded_state(__opts__) as state:
245 if "filename" in state:
246 os.unlink(state["filename"])
247
248
249 def test_from_state():
250 """
251 Test function so we have something to call from a state
252 :return:
253 """
254 log.debug("test_from_state called")
255 return "testvalue"