Jump to content

User:Razzi/Spicerack python api repl

From Wikitech

Create the following on cumin1001:

razzi@cumin1001:~$ cat spicerack_repl.py
#!/usr/bin/env python3
from spicerack import Spicerack

spicerack = Spicerack(dry_run=False)

remote = spicerack.remote()

section = 's3'

query = f"A:db-wikireplica and A:db-section-{section} and not A:wikireplicas-dedicated"

wikireplicas = remote.query(query)

Run it like so:

sudo python3 -i spicerack_repl.py

Then you can, for example, find how to access a command's stdout interactively:

razzi@cumin1001:~$ sudo python3 -i spicerack_repl.py
>>> remote_host.run_sync('echo 3')
----- OUTPUT of 'echo 3' -----
3
================
PASS |██████████████████████████████████████████████| 100% (1/1) [00:00<00:00,  1.74hosts/s]
FAIL |                                                      |   0% (0/1) [00:00<?, ?hosts/s]
100.0% (1/1) success ratio (>= 100.0% threshold) for command: 'echo 3'.
100.0% (1/1) success ratio (>= 100.0% threshold) of nodes successfully executed all commands.
<generator object ClusterShellWorker.get_results at 0x7fc8732a8ed0>
>>> output=_
>>> result=list(output)
>>> result
[(<ClusterShell.NodeSet.NodeSet object at 0x7fc87328b780>, <ClusterShell.MsgTree.MsgTreeElem object at 0x7fc87328b908>)]
>>> result[0][1]
<ClusterShell.MsgTree.MsgTreeElem object at 0x7fc87328b908>
>>> elem=result[0][1]
>>> elem.
elem.append(      elem.keys         elem.message(     elem.parent
elem.children     elem.lines(       elem.msgline      elem.splitlines(
>>> elem.msgline
b'3'
>>> list(elem.lines())
[b'3']