-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic.py
More file actions
executable file
·70 lines (52 loc) · 2.34 KB
/
basic.py
File metadata and controls
executable file
·70 lines (52 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Basic example demonstrating deploykit usage for remote and local command execution."""
import argparse
import subprocess
from deploykit import DeployHost, parse_hosts, run
def deploy(host: DeployHost) -> None:
# Our function will receive a DeployHost object. This object behaves
# similar to DeployGroup, except that it is just for one host instead of a
# group of hosts.
# This is running locally
host.run_local("hostname")
# This is running on the remote machine
host.run("hostname")
# We can also use our `DeployHost` object to get connection info for other ssh hosts
# host.run_local(
# f"rsync {' --exclude -vaF --delete -e ssh . {host.user}@{host.host}:/etc/nixos"
# )
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("hosts")
args = parser.parse_args()
# This command runs on the local machine
run("hostname")
# parse_host accepts hosts in comma seperated for form and returns a DeployGroup
# Hosts can contain username and port numbers
# i.e. host1,host2,admin@host3:2222
g = parse_hosts(args.hosts)
# g will now contain a group of hosts. This is a shorthand for writing
# g = deploykit.DeployGroup([
# DeployHost(host="myhostname"),
# DeployHost(host="myhostname2"),
# ])
# Let's see what we can do with a `DeployGroup`
# This command runs locally in parallel for all hosts
g.run_local("hostname")
# This commands runs remotely in parallel for all hosts
g.run("hostname")
# This function runs in parallel for all hosts. This is useful if you want
# to run a series of commands per host.
g.run_function(deploy)
# By default all functions will throw a subprocess.CalledProcess exception if a command fails.
# When check=False is passed, instead a subprocess.CompletedProcess value is returned and the user
# can check the result of command by inspecting the `returncode` attribute
g.run_local("false", check=False)
# To capture the output of a command, set stdout/stderr parameter
g.run_local("hostname", stdout=subprocess.PIPE)
# To select a subset of the hosts, you can use the filter function
g2 = g.filter(lambda h: h.host == "host2")
# This should then only output "host2"
g2.run("hostname")
if __name__ == "__main__":
main()