saltext.noderings 0.1.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- saltext/noderings/__init__.py +28 -0
- saltext/noderings/clouds/__init__.py +0 -0
- saltext/noderings/clouds/noderings_mod.py +457 -0
- saltext/noderings/modules/__init__.py +0 -0
- saltext/noderings/modules/noderings_mod.py +996 -0
- saltext/noderings/states/__init__.py +0 -0
- saltext/noderings/states/noderings_mod.py +562 -0
- saltext/noderings/utils/__init__.py +0 -0
- saltext/noderings/utils/client.py +593 -0
- saltext/noderings/utils/compute.py +392 -0
- saltext/noderings/utils/dns.py +162 -0
- saltext/noderings/utils/firewall.py +323 -0
- saltext/noderings/utils/marketplace.py +182 -0
- saltext/noderings/version.py +1 -0
- saltext_noderings-0.1.0.dist-info/METADATA +198 -0
- saltext_noderings-0.1.0.dist-info/RECORD +21 -0
- saltext_noderings-0.1.0.dist-info/WHEEL +6 -0
- saltext_noderings-0.1.0.dist-info/entry_points.txt +2 -0
- saltext_noderings-0.1.0.dist-info/licenses/LICENSE +201 -0
- saltext_noderings-0.1.0.dist-info/licenses/NOTICE +10 -0
- saltext_noderings-0.1.0.dist-info/top_level.txt +1 -0
|
File without changes
|
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Declare NodeRings resources in Salt states: SSH keys, DNS zones and records,
|
|
3
|
+
firewall rulesets and virtual machines.
|
|
4
|
+
|
|
5
|
+
Every state supports ``test=True``. Credentials come from ``noderings:token``
|
|
6
|
+
in the minion configuration or pillar, or the ``NODERINGS_TOKEN`` environment
|
|
7
|
+
variable; see :py:mod:`the execution module <saltext.noderings.modules.noderings_mod>`.
|
|
8
|
+
|
|
9
|
+
.. code-block:: yaml
|
|
10
|
+
|
|
11
|
+
laptop-key:
|
|
12
|
+
noderings.ssh_key_present:
|
|
13
|
+
- name: laptop
|
|
14
|
+
- public_key: ssh-ed25519 AAAA... me@laptop
|
|
15
|
+
|
|
16
|
+
example.com:
|
|
17
|
+
noderings.dns_zone_present
|
|
18
|
+
|
|
19
|
+
web-1:
|
|
20
|
+
noderings.vm_present:
|
|
21
|
+
- plan_code: SC-GC-1C-1G-20
|
|
22
|
+
- country: United States
|
|
23
|
+
- city: Ashburn
|
|
24
|
+
- distro: ubuntu_24_04
|
|
25
|
+
- ssh_keys: [laptop]
|
|
26
|
+
- firewall_ruleset: web
|
|
27
|
+
- require:
|
|
28
|
+
- noderings: laptop-key
|
|
29
|
+
- noderings: web-firewall
|
|
30
|
+
|
|
31
|
+
www:
|
|
32
|
+
noderings.dns_record_present:
|
|
33
|
+
- zone: example.com
|
|
34
|
+
- rtype: A
|
|
35
|
+
- values:
|
|
36
|
+
- {{ salt['noderings.get_vm']('web-1')['ipv4'] }}
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
import logging
|
|
40
|
+
|
|
41
|
+
from salt.exceptions import CommandExecutionError
|
|
42
|
+
from salt.exceptions import SaltInvocationError
|
|
43
|
+
|
|
44
|
+
from saltext.noderings.utils import dns as dnsutil
|
|
45
|
+
from saltext.noderings.utils import firewall as fwutil
|
|
46
|
+
from saltext.noderings.utils.client import client_from_config
|
|
47
|
+
from saltext.noderings.utils.client import find_by_name
|
|
48
|
+
|
|
49
|
+
log = logging.getLogger(__name__)
|
|
50
|
+
|
|
51
|
+
__virtualname__ = "noderings"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def __virtual__():
|
|
55
|
+
if "noderings.get_vm" not in __salt__:
|
|
56
|
+
return False, "The noderings execution module is not available"
|
|
57
|
+
return __virtualname__
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _ret(name):
|
|
61
|
+
return {"name": name, "result": True, "changes": {}, "comment": ""}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _fail(ret, exc):
|
|
65
|
+
ret["result"] = False
|
|
66
|
+
ret["comment"] = str(exc)
|
|
67
|
+
return ret
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _test():
|
|
71
|
+
return bool(__opts__.get("test"))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# -- SSH keys -------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def ssh_key_present(name, public_key, profile="noderings"):
|
|
78
|
+
"""
|
|
79
|
+
Ensure an SSH key with this name holds ``public_key``. A key with the same
|
|
80
|
+
name but another public key is replaced.
|
|
81
|
+
|
|
82
|
+
name
|
|
83
|
+
Key name.
|
|
84
|
+
|
|
85
|
+
public_key
|
|
86
|
+
The public key, for example ``ssh-ed25519 AAAA... me@laptop``.
|
|
87
|
+
"""
|
|
88
|
+
ret = _ret(name)
|
|
89
|
+
want = " ".join(public_key.split()[:2])
|
|
90
|
+
try:
|
|
91
|
+
have = __salt__["noderings.get_ssh_key"](name, profile=profile)
|
|
92
|
+
if have and " ".join((have.get("public_key") or "").split()[:2]) == want:
|
|
93
|
+
ret["comment"] = f"SSH key {name} is up to date"
|
|
94
|
+
return ret
|
|
95
|
+
if _test():
|
|
96
|
+
ret["result"] = None
|
|
97
|
+
ret["comment"] = f"SSH key {name} would be {'replaced' if have else 'created'}"
|
|
98
|
+
ret["changes"] = {"old": have, "new": {"name": name, "public_key": public_key}}
|
|
99
|
+
return ret
|
|
100
|
+
if have:
|
|
101
|
+
__salt__["noderings.delete_ssh_key"](have["id"], profile=profile)
|
|
102
|
+
new = __salt__["noderings.create_ssh_key"](name, public_key, profile=profile)
|
|
103
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
104
|
+
return _fail(ret, exc)
|
|
105
|
+
ret["changes"] = {"old": have, "new": new}
|
|
106
|
+
ret["comment"] = f"SSH key {name} {'replaced' if have else 'created'}"
|
|
107
|
+
return ret
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def ssh_key_absent(name, profile="noderings"):
|
|
111
|
+
"""
|
|
112
|
+
Ensure no SSH key has this name.
|
|
113
|
+
"""
|
|
114
|
+
ret = _ret(name)
|
|
115
|
+
try:
|
|
116
|
+
have = __salt__["noderings.get_ssh_key"](name, profile=profile)
|
|
117
|
+
if not have:
|
|
118
|
+
ret["comment"] = f"SSH key {name} is already absent"
|
|
119
|
+
return ret
|
|
120
|
+
if _test():
|
|
121
|
+
ret["result"] = None
|
|
122
|
+
ret["comment"] = f"SSH key {name} would be deleted"
|
|
123
|
+
ret["changes"] = {"old": have, "new": None}
|
|
124
|
+
return ret
|
|
125
|
+
__salt__["noderings.delete_ssh_key"](have["id"], profile=profile)
|
|
126
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
127
|
+
return _fail(ret, exc)
|
|
128
|
+
ret["changes"] = {"old": have, "new": None}
|
|
129
|
+
ret["comment"] = f"SSH key {name} deleted"
|
|
130
|
+
return ret
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# -- DNS ------------------------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def dns_zone_present(name, dnssec=False, profile="noderings"):
|
|
137
|
+
"""
|
|
138
|
+
Ensure a DNS zone exists, and optionally that it is signed with DNSSEC.
|
|
139
|
+
Set the zone's nameservers (in the ``nameservers`` change) at the registrar.
|
|
140
|
+
|
|
141
|
+
name
|
|
142
|
+
Domain, for example ``example.com``.
|
|
143
|
+
|
|
144
|
+
dnssec
|
|
145
|
+
Sign the zone. Add the DS record from ``noderings.get_dnssec`` at the
|
|
146
|
+
registrar afterwards. Turning signing off is left to
|
|
147
|
+
``noderings.delete_zone`` or the console, because doing it while the
|
|
148
|
+
registrar has the DS record breaks the domain.
|
|
149
|
+
"""
|
|
150
|
+
ret = _ret(name)
|
|
151
|
+
try:
|
|
152
|
+
zone = __salt__["noderings.get_zone"](name, profile=profile)
|
|
153
|
+
changes = {}
|
|
154
|
+
if not zone:
|
|
155
|
+
if _test():
|
|
156
|
+
ret["result"] = None
|
|
157
|
+
ret["comment"] = f"DNS zone {name} would be created"
|
|
158
|
+
ret["changes"] = {"old": None, "new": {"name": name, "dnssec": dnssec}}
|
|
159
|
+
return ret
|
|
160
|
+
zone = __salt__["noderings.create_zone"](name, profile=profile)
|
|
161
|
+
changes["created"] = {"id": zone.get("id"), "nameservers": zone.get("nameservers")}
|
|
162
|
+
if dnssec and zone.get("dnssec_mode") in ("", "unsigned", None):
|
|
163
|
+
if _test():
|
|
164
|
+
ret["result"] = None
|
|
165
|
+
ret["comment"] = f"DNSSEC would be enabled on {name}"
|
|
166
|
+
ret["changes"] = {"dnssec": {"old": "unsigned", "new": "signed"}}
|
|
167
|
+
return ret
|
|
168
|
+
sec = __salt__["noderings.enable_dnssec"](name, profile=profile)
|
|
169
|
+
changes["dnssec"] = {"old": "unsigned", "new": sec.get("ds") or sec}
|
|
170
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
171
|
+
return _fail(ret, exc)
|
|
172
|
+
ret["changes"] = changes
|
|
173
|
+
ret["comment"] = f"DNS zone {name} {'updated' if changes else 'is up to date'}"
|
|
174
|
+
return ret
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def dns_zone_absent(name, force=False, profile="noderings"):
|
|
178
|
+
"""
|
|
179
|
+
Ensure a DNS zone does not exist. Deleting it deletes all its records.
|
|
180
|
+
"""
|
|
181
|
+
ret = _ret(name)
|
|
182
|
+
try:
|
|
183
|
+
zone = __salt__["noderings.get_zone"](name, profile=profile)
|
|
184
|
+
if not zone:
|
|
185
|
+
ret["comment"] = f"DNS zone {name} is already absent"
|
|
186
|
+
return ret
|
|
187
|
+
if _test():
|
|
188
|
+
ret["result"] = None
|
|
189
|
+
ret["comment"] = f"DNS zone {name} would be deleted"
|
|
190
|
+
ret["changes"] = {"old": zone.get("name"), "new": None}
|
|
191
|
+
return ret
|
|
192
|
+
__salt__["noderings.delete_zone"](name, force=force, profile=profile)
|
|
193
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
194
|
+
return _fail(ret, exc)
|
|
195
|
+
ret["changes"] = {"old": zone.get("name"), "new": None}
|
|
196
|
+
ret["comment"] = f"DNS zone {name} deleted"
|
|
197
|
+
return ret
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def dns_record_present(name, zone, rtype, values, ttl=3600, profile="noderings"):
|
|
201
|
+
"""
|
|
202
|
+
Ensure the records of one name and type hold exactly ``values``.
|
|
203
|
+
|
|
204
|
+
name
|
|
205
|
+
Record name relative to the zone (``www``, ``@`` for the apex), or
|
|
206
|
+
fully qualified.
|
|
207
|
+
|
|
208
|
+
zone
|
|
209
|
+
Zone name or ID.
|
|
210
|
+
|
|
211
|
+
rtype
|
|
212
|
+
Record type, for example ``A``, ``AAAA``, ``CNAME``, ``MX`` or ``TXT``.
|
|
213
|
+
|
|
214
|
+
values
|
|
215
|
+
List of values. Hostnames should end with a dot. TXT values are plain
|
|
216
|
+
text without quotes.
|
|
217
|
+
|
|
218
|
+
ttl
|
|
219
|
+
Time to live in seconds, 30 to 86400.
|
|
220
|
+
"""
|
|
221
|
+
ret = _ret(f"{name} {rtype.upper()}")
|
|
222
|
+
rrtype = rtype.upper()
|
|
223
|
+
if isinstance(values, str):
|
|
224
|
+
values = [values]
|
|
225
|
+
try:
|
|
226
|
+
have = __salt__["noderings.get_record"](zone, name, rrtype, profile=profile)
|
|
227
|
+
if (
|
|
228
|
+
have
|
|
229
|
+
and have.get("ttl") == int(ttl)
|
|
230
|
+
and dnsutil.same_values(rrtype, have.get("values"), values)
|
|
231
|
+
):
|
|
232
|
+
ret["comment"] = f"DNS record {name} {rrtype} is up to date"
|
|
233
|
+
return ret
|
|
234
|
+
new = {"ttl": int(ttl), "values": list(values)}
|
|
235
|
+
old = {"ttl": have.get("ttl"), "values": have.get("values")} if have else None
|
|
236
|
+
if _test():
|
|
237
|
+
ret["result"] = None
|
|
238
|
+
ret["comment"] = (
|
|
239
|
+
f"DNS record {name} {rrtype} would be {'updated' if have else 'created'}"
|
|
240
|
+
)
|
|
241
|
+
ret["changes"] = {"old": old, "new": new}
|
|
242
|
+
return ret
|
|
243
|
+
__salt__["noderings.set_record"](zone, name, rrtype, values, ttl=ttl, profile=profile)
|
|
244
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
245
|
+
return _fail(ret, exc)
|
|
246
|
+
ret["changes"] = {"old": old, "new": new}
|
|
247
|
+
ret["comment"] = f"DNS record {name} {rrtype} {'updated' if have else 'created'}"
|
|
248
|
+
return ret
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def dns_record_absent(name, zone, rtype, profile="noderings"):
|
|
252
|
+
"""
|
|
253
|
+
Ensure there are no records of this name and type. A missing zone counts
|
|
254
|
+
as absent.
|
|
255
|
+
"""
|
|
256
|
+
ret = _ret(f"{name} {rtype.upper()}")
|
|
257
|
+
rrtype = rtype.upper()
|
|
258
|
+
try:
|
|
259
|
+
if not __salt__["noderings.get_zone"](zone, profile=profile):
|
|
260
|
+
ret["comment"] = f"DNS zone {zone} does not exist, so neither does the record"
|
|
261
|
+
return ret
|
|
262
|
+
have = __salt__["noderings.get_record"](zone, name, rrtype, profile=profile)
|
|
263
|
+
if not have:
|
|
264
|
+
ret["comment"] = f"DNS record {name} {rrtype} is already absent"
|
|
265
|
+
return ret
|
|
266
|
+
if _test():
|
|
267
|
+
ret["result"] = None
|
|
268
|
+
ret["comment"] = f"DNS record {name} {rrtype} would be deleted"
|
|
269
|
+
ret["changes"] = {"old": have.get("values"), "new": None}
|
|
270
|
+
return ret
|
|
271
|
+
__salt__["noderings.delete_record"](zone, name, rrtype, profile=profile)
|
|
272
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
273
|
+
return _fail(ret, exc)
|
|
274
|
+
ret["changes"] = {"old": have.get("values"), "new": None}
|
|
275
|
+
ret["comment"] = f"DNS record {name} {rrtype} deleted"
|
|
276
|
+
return ret
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
# -- firewall -------------------------------------------------------------------
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _desired_rules(client, rules):
|
|
283
|
+
names = set()
|
|
284
|
+
for i, rule in enumerate(rules):
|
|
285
|
+
rule = dict(rule)
|
|
286
|
+
rule.setdefault("protocol", "tcp")
|
|
287
|
+
err = fwutil.validate_rule(rule, i)
|
|
288
|
+
if err:
|
|
289
|
+
raise SaltInvocationError(err)
|
|
290
|
+
for side in ("source", "destination"):
|
|
291
|
+
names.update((rule.get(side) or {}).get("address_groups") or [])
|
|
292
|
+
groups = {}
|
|
293
|
+
if names:
|
|
294
|
+
existing = client.list_firewall_address_groups()
|
|
295
|
+
for gname in names:
|
|
296
|
+
match = find_by_name(existing, gname)
|
|
297
|
+
if not match:
|
|
298
|
+
raise SaltInvocationError(f"no firewall address group is named {gname}")
|
|
299
|
+
groups[gname] = match["id"]
|
|
300
|
+
return [fwutil.rule_to_api(dict(r, protocol=r.get("protocol") or "tcp"), groups) for r in rules]
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def firewall_ruleset_present(name, rules=None, profile="noderings"):
|
|
304
|
+
"""
|
|
305
|
+
Ensure a firewall ruleset exists and, when ``rules`` is given, holds
|
|
306
|
+
exactly these rules in this order.
|
|
307
|
+
|
|
308
|
+
Rules run top to bottom, and traffic that matches no rule is accepted, so
|
|
309
|
+
end the list with a drop rule. Each rule is a mapping with ``direction``
|
|
310
|
+
(``ingress`` or ``egress``), ``action`` (``accept`` or ``drop``),
|
|
311
|
+
``protocol`` (default ``tcp``), ``source`` and ``destination`` (each with
|
|
312
|
+
``cidrs``, ``address_groups``, ``address_group_ids`` or ``vm_tags``), and
|
|
313
|
+
``destination_ports`` / ``source_ports`` (one port such as ``443`` or one
|
|
314
|
+
range such as ``8000-8100``).
|
|
315
|
+
|
|
316
|
+
.. code-block:: yaml
|
|
317
|
+
|
|
318
|
+
web-firewall:
|
|
319
|
+
noderings.firewall_ruleset_present:
|
|
320
|
+
- name: web
|
|
321
|
+
- rules:
|
|
322
|
+
- {direction: ingress, action: accept, protocol: tcp, destination_ports: "22", source: {cidrs: [198.51.100.0/24]}}
|
|
323
|
+
- {direction: ingress, action: accept, protocol: tcp, destination_ports: "443", source: {cidrs: [0.0.0.0/0]}}
|
|
324
|
+
- {direction: ingress, action: drop, protocol: any, source: {cidrs: [0.0.0.0/0]}}
|
|
325
|
+
"""
|
|
326
|
+
ret = _ret(name)
|
|
327
|
+
try:
|
|
328
|
+
client = client_from_config(__salt__["config.get"](profile, {}) or {})
|
|
329
|
+
desired = _desired_rules(client, rules) if rules is not None else None
|
|
330
|
+
ruleset = find_by_name(client.list_firewall_rulesets(), name)
|
|
331
|
+
current = client.list_firewall_rules(ruleset["id"]) if ruleset else []
|
|
332
|
+
create = not ruleset
|
|
333
|
+
rewrite = desired is not None and fwutil.plan_rules(current, desired)
|
|
334
|
+
if not create and not rewrite:
|
|
335
|
+
ret["comment"] = f"Firewall ruleset {name} is up to date"
|
|
336
|
+
return ret
|
|
337
|
+
old = [fwutil.rule_view(r) for r in current] if ruleset else None
|
|
338
|
+
new = [fwutil.rule_view(d) for d in desired] if desired is not None else []
|
|
339
|
+
if _test():
|
|
340
|
+
ret["result"] = None
|
|
341
|
+
ret["comment"] = (
|
|
342
|
+
f"Firewall ruleset {name} would be {'created' if create else 'updated'}"
|
|
343
|
+
)
|
|
344
|
+
ret["changes"] = {"old": old, "new": new}
|
|
345
|
+
return ret
|
|
346
|
+
if create:
|
|
347
|
+
ruleset = client.create_firewall_ruleset(name)
|
|
348
|
+
if desired is not None:
|
|
349
|
+
final = fwutil.reconcile(client, ruleset["id"], desired)
|
|
350
|
+
new = [fwutil.rule_view(r) for r in final]
|
|
351
|
+
except (ValueError, SaltInvocationError) as exc:
|
|
352
|
+
return _fail(ret, exc)
|
|
353
|
+
except Exception as exc: # pylint: disable=broad-except
|
|
354
|
+
return _fail(ret, f"writing firewall ruleset {name}: {exc}")
|
|
355
|
+
ret["changes"] = {"old": old, "new": new}
|
|
356
|
+
ret["comment"] = f"Firewall ruleset {name} {'created' if create else 'updated'}"
|
|
357
|
+
return ret
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def firewall_ruleset_absent(name, force=False, profile="noderings"):
|
|
361
|
+
"""
|
|
362
|
+
Ensure a firewall ruleset does not exist. ``force=True`` deletes it even
|
|
363
|
+
while it is attached to virtual machines.
|
|
364
|
+
"""
|
|
365
|
+
ret = _ret(name)
|
|
366
|
+
try:
|
|
367
|
+
have = __salt__["noderings.get_ruleset"](name, profile=profile)
|
|
368
|
+
if not have:
|
|
369
|
+
ret["comment"] = f"Firewall ruleset {name} is already absent"
|
|
370
|
+
return ret
|
|
371
|
+
if _test():
|
|
372
|
+
ret["result"] = None
|
|
373
|
+
ret["comment"] = f"Firewall ruleset {name} would be deleted"
|
|
374
|
+
ret["changes"] = {"old": have.get("name"), "new": None}
|
|
375
|
+
return ret
|
|
376
|
+
__salt__["noderings.delete_ruleset"](name, force=force, profile=profile)
|
|
377
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
378
|
+
return _fail(ret, exc)
|
|
379
|
+
ret["changes"] = {"old": have.get("name"), "new": None}
|
|
380
|
+
ret["comment"] = f"Firewall ruleset {name} deleted"
|
|
381
|
+
return ret
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
# -- virtual machines -----------------------------------------------------------
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def vm_present(
|
|
388
|
+
name,
|
|
389
|
+
plan_code=None,
|
|
390
|
+
country=None,
|
|
391
|
+
city=None,
|
|
392
|
+
distro="ubuntu_24_04",
|
|
393
|
+
billing_type="usage",
|
|
394
|
+
ssh_keys=None,
|
|
395
|
+
tags=None,
|
|
396
|
+
firewall_ruleset=None,
|
|
397
|
+
power_state="running",
|
|
398
|
+
allow_reinstall=False,
|
|
399
|
+
plan_id=None,
|
|
400
|
+
region_id=None,
|
|
401
|
+
provider_id=None,
|
|
402
|
+
password=None,
|
|
403
|
+
wait_timeout=900,
|
|
404
|
+
profile="noderings",
|
|
405
|
+
):
|
|
406
|
+
"""
|
|
407
|
+
Ensure a virtual machine with this hostname exists, in the given state.
|
|
408
|
+
|
|
409
|
+
A new VM is ordered from the cheapest offer for ``plan_code`` in
|
|
410
|
+
``country`` and ``city`` (or exactly ``plan_id`` and ``region_id``).
|
|
411
|
+
Ordering costs money: ``billing_type=usage`` bills by the hour from prepaid
|
|
412
|
+
credit. For an existing VM, ``tags``, ``firewall_ruleset`` and
|
|
413
|
+
``power_state`` are updated in place. A different ``distro`` reinstalls the
|
|
414
|
+
VM only with ``allow_reinstall=True``, because reinstalling erases the
|
|
415
|
+
disk; otherwise the state reports the difference and fails. The plan and
|
|
416
|
+
region of an existing VM are never changed.
|
|
417
|
+
|
|
418
|
+
name
|
|
419
|
+
Hostname. Must be unique in the organization.
|
|
420
|
+
|
|
421
|
+
power_state
|
|
422
|
+
``running`` or ``stopped``.
|
|
423
|
+
|
|
424
|
+
firewall_ruleset
|
|
425
|
+
Name or ID of a ruleset to attach. ``""`` detaches the current one;
|
|
426
|
+
``None`` (the default) leaves it alone.
|
|
427
|
+
"""
|
|
428
|
+
ret = _ret(name)
|
|
429
|
+
if power_state not in ("running", "stopped"):
|
|
430
|
+
return _fail(ret, "power_state must be running or stopped")
|
|
431
|
+
try:
|
|
432
|
+
vm = __salt__["noderings.get_vm"](name, profile=profile)
|
|
433
|
+
if not vm:
|
|
434
|
+
if _test():
|
|
435
|
+
ret["result"] = None
|
|
436
|
+
ret["comment"] = f"Virtual machine {name} would be ordered"
|
|
437
|
+
ret["changes"] = {
|
|
438
|
+
"old": None,
|
|
439
|
+
"new": {
|
|
440
|
+
"hostname": name,
|
|
441
|
+
"plan_code": plan_code,
|
|
442
|
+
"plan_id": plan_id,
|
|
443
|
+
"distro": distro,
|
|
444
|
+
},
|
|
445
|
+
}
|
|
446
|
+
return ret
|
|
447
|
+
new = __salt__["noderings.create_vm"](
|
|
448
|
+
name,
|
|
449
|
+
plan_code=plan_code,
|
|
450
|
+
country=country,
|
|
451
|
+
city=city,
|
|
452
|
+
distro=distro,
|
|
453
|
+
billing_type=billing_type,
|
|
454
|
+
ssh_keys=ssh_keys,
|
|
455
|
+
tags=tags,
|
|
456
|
+
firewall_ruleset=firewall_ruleset or None,
|
|
457
|
+
password=password,
|
|
458
|
+
plan_id=plan_id,
|
|
459
|
+
region_id=region_id,
|
|
460
|
+
provider_id=provider_id,
|
|
461
|
+
wait_timeout=wait_timeout,
|
|
462
|
+
profile=profile,
|
|
463
|
+
)
|
|
464
|
+
if power_state == "stopped":
|
|
465
|
+
new = __salt__["noderings.stop_vm"](
|
|
466
|
+
name, wait_timeout=wait_timeout, profile=profile
|
|
467
|
+
)
|
|
468
|
+
ret["changes"] = {
|
|
469
|
+
"old": None,
|
|
470
|
+
"new": {"id": new.get("id"), "ipv4": new.get("ipv4"), "ipv6": new.get("ipv6")},
|
|
471
|
+
}
|
|
472
|
+
ret["comment"] = f"Virtual machine {name} ordered"
|
|
473
|
+
return ret
|
|
474
|
+
|
|
475
|
+
changes = {}
|
|
476
|
+
todo = []
|
|
477
|
+
if distro and vm.get("distro") and vm["distro"] != distro:
|
|
478
|
+
if not allow_reinstall:
|
|
479
|
+
return _fail(
|
|
480
|
+
ret,
|
|
481
|
+
f"Virtual machine {name} runs {vm['distro']}, not {distro}. Set "
|
|
482
|
+
"allow_reinstall: True to reinstall it, which erases the disk.",
|
|
483
|
+
)
|
|
484
|
+
changes["distro"] = {"old": vm["distro"], "new": distro}
|
|
485
|
+
todo.append(
|
|
486
|
+
lambda: __salt__["noderings.reinstall_vm"](
|
|
487
|
+
name,
|
|
488
|
+
distro,
|
|
489
|
+
ssh_keys=ssh_keys,
|
|
490
|
+
password=password,
|
|
491
|
+
wait_timeout=wait_timeout,
|
|
492
|
+
profile=profile,
|
|
493
|
+
)
|
|
494
|
+
)
|
|
495
|
+
if tags is not None and sorted(set(tags)) != sorted(vm.get("tags") or []):
|
|
496
|
+
changes["tags"] = {"old": vm.get("tags"), "new": sorted(set(tags))}
|
|
497
|
+
todo.append(lambda: __salt__["noderings.set_tags"](name, tags, profile=profile))
|
|
498
|
+
if firewall_ruleset is not None:
|
|
499
|
+
have_rs = vm.get("firewall_ruleset_id") or ""
|
|
500
|
+
want_rs = ""
|
|
501
|
+
if firewall_ruleset:
|
|
502
|
+
rs = __salt__["noderings.get_ruleset"](firewall_ruleset, profile=profile)
|
|
503
|
+
if not rs:
|
|
504
|
+
return _fail(ret, f"no firewall ruleset is named {firewall_ruleset}")
|
|
505
|
+
want_rs = rs["id"]
|
|
506
|
+
if have_rs != want_rs:
|
|
507
|
+
changes["firewall_ruleset_id"] = {"old": have_rs or None, "new": want_rs or None}
|
|
508
|
+
if want_rs:
|
|
509
|
+
todo.append(
|
|
510
|
+
lambda: __salt__["noderings.attach_ruleset"](
|
|
511
|
+
name, want_rs, wait_timeout=wait_timeout, profile=profile
|
|
512
|
+
)
|
|
513
|
+
)
|
|
514
|
+
else:
|
|
515
|
+
todo.append(
|
|
516
|
+
lambda: __salt__["noderings.detach_ruleset"](
|
|
517
|
+
name, wait_timeout=wait_timeout, profile=profile
|
|
518
|
+
)
|
|
519
|
+
)
|
|
520
|
+
if vm.get("power_state") != power_state:
|
|
521
|
+
changes["power_state"] = {"old": vm.get("power_state"), "new": power_state}
|
|
522
|
+
func = "noderings.start_vm" if power_state == "running" else "noderings.stop_vm"
|
|
523
|
+
todo.append(lambda: __salt__[func](name, wait_timeout=wait_timeout, profile=profile))
|
|
524
|
+
if not changes:
|
|
525
|
+
ret["comment"] = f"Virtual machine {name} is up to date"
|
|
526
|
+
return ret
|
|
527
|
+
if _test():
|
|
528
|
+
ret["result"] = None
|
|
529
|
+
ret["comment"] = f"Virtual machine {name} would be updated"
|
|
530
|
+
ret["changes"] = changes
|
|
531
|
+
return ret
|
|
532
|
+
for step in todo:
|
|
533
|
+
step()
|
|
534
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
535
|
+
return _fail(ret, exc)
|
|
536
|
+
ret["changes"] = changes
|
|
537
|
+
ret["comment"] = f"Virtual machine {name} updated"
|
|
538
|
+
return ret
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
def vm_absent(name, profile="noderings"):
|
|
542
|
+
"""
|
|
543
|
+
Ensure no virtual machine has this hostname. An hourly VM stops billing; a
|
|
544
|
+
monthly subscription is cancelled without a refund.
|
|
545
|
+
"""
|
|
546
|
+
ret = _ret(name)
|
|
547
|
+
try:
|
|
548
|
+
vm = __salt__["noderings.get_vm"](name, profile=profile)
|
|
549
|
+
if not vm:
|
|
550
|
+
ret["comment"] = f"Virtual machine {name} is already absent"
|
|
551
|
+
return ret
|
|
552
|
+
if _test():
|
|
553
|
+
ret["result"] = None
|
|
554
|
+
ret["comment"] = f"Virtual machine {name} would be deleted"
|
|
555
|
+
ret["changes"] = {"old": vm.get("id"), "new": None}
|
|
556
|
+
return ret
|
|
557
|
+
__salt__["noderings.delete_vm"](name, profile=profile)
|
|
558
|
+
except (CommandExecutionError, SaltInvocationError) as exc:
|
|
559
|
+
return _fail(ret, exc)
|
|
560
|
+
ret["changes"] = {"old": vm.get("id"), "new": None}
|
|
561
|
+
ret["comment"] = f"Virtual machine {name} deleted"
|
|
562
|
+
return ret
|
|
File without changes
|