forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_deploy.py
More file actions
112 lines (86 loc) · 2.5 KB
/
api_deploy.py
File metadata and controls
112 lines (86 loc) · 2.5 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# piynfra
# File: example/api_deploy.py
# Desc: example of how to deploy via the pyinfra API
from gevent import monkey # noqa
monkey.patch_all() # noqa async things (speed++, optional)
import json # noqa
import logging
from collections import defaultdict
from pyinfra.api import Config, Inventory, State
from pyinfra.api.connect import connect_all
from pyinfra.api.connectors.vagrant import make_names_data
from pyinfra.api.facts import get_facts
from pyinfra.api.operation import add_op
from pyinfra.api.operations import run_ops
from pyinfra.modules import files, server
from pyinfra_cli.prints import jsonify
# Enable pyinfra logging
logging.basicConfig(level=logging.WARNING)
logging.getLogger('pyinfra').setLevel(logging.INFO)
# Make our hosts and groups data (using the Vagrant connector in this case)
hosts = []
groups = defaultdict(lambda: ([], {}))
for name, data, group_names in make_names_data():
hosts.append((name, data))
for group_name in group_names:
if name not in groups[group_name][0]:
groups[group_name][0].append(name)
# First we setup some inventory we want to target
# the first argument is a tuple of (list all all hosts, global/ALL data)
inventory = Inventory((hosts, {}), **groups)
# Now we create a new config (w/optional args)
config = Config(
FAIL_PERCENT=81,
CONNECT_TIMEOUT=5,
)
# Setup the pyinfra state for this deploy
state = State(inventory, config)
# Connect to all the hosts
print('Connecting...')
connect_all(state)
# Start adding operations
print('Generating operations...')
add_op(
state, server.user,
'pyinfra',
home='/home/pyinfra',
shell='/bin/bash',
sudo=True,
)
add_op(
state, server.group,
{'Ensure pyinfra2 group exists'}, # set as the first arg names the operation
'pyinfra2',
sudo=True,
# Add an op only to a subset of hosts
# (in this case, the inventory.centos group)
hosts=inventory.get_group('centos'),
)
# Ensure the state of files
add_op(
state, files.file,
'/var/log/pyinfra.log',
user='pyinfra',
group='pyinfra',
mode='644',
sudo=True,
)
# Ensure the state of directories
add_op(
state, files.directory,
'/tmp/email',
user='pyinfra',
group='pyinfra',
mode='755',
sudo=True,
)
# Copy local files to remote host
add_op(
state, files.put,
'files/file.txt', '/home/vagrant/file.txt',
)
# And finally we run the ops
run_ops(state)
# We can also get facts for all the hosts
facts = get_facts(state, 'os')
print(jsonify(facts, indent=4))