forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
742 lines (615 loc) · 19.6 KB
/
main.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
import logging
import sys
import warnings
from fnmatch import fnmatch
from os import chdir as os_chdir, getcwd, path
from typing import Iterable, List, Tuple, Union
import click
from pyinfra import __version__, logger, state
from pyinfra.api import Config, State
from pyinfra.api.connect import connect_all, disconnect_all
from pyinfra.api.exceptions import NoGroupError, PyinfraError
from pyinfra.api.facts import get_facts
from pyinfra.api.operations import run_ops
from pyinfra.api.state import StateStage
from pyinfra.api.util import get_kwargs_str
from pyinfra.context import ctx_config, ctx_inventory, ctx_state
from pyinfra.operations import server
from .commands import get_facts_and_args, get_func_and_args
from .exceptions import CliError, UnexpectedExternalError, UnexpectedInternalError, WrappedError
from .inventory import make_inventory
from .log import setup_logging
from .prints import (
print_facts,
print_inventory,
print_meta,
print_results,
print_state_operations,
print_support_info,
)
from .util import exec_file, load_deploy_file, load_func, parse_cli_arg
from .virtualenv import init_virtualenv
def _exit() -> None:
if ctx_state.isset() and state.failed_hosts:
sys.exit(1)
sys.exit(0)
def _print_support(ctx, param, value):
if not value:
return
logger.info("--> Support information:")
print_support_info()
ctx.exit()
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument("inventory", nargs=1, type=click.Path(exists=False))
@click.argument("operations", nargs=-1, required=True, type=click.Path(exists=False))
@click.option(
"verbosity",
"-v",
count=True,
help="Print meta (-v), input (-vv) and output (-vvv).",
)
@click.option(
"--dry",
is_flag=True,
default=False,
help="Don't execute operations on the target hosts.",
)
@click.option(
"-y",
"--yes",
is_flag=True,
default=False,
help="Execute operations immediately on hosts without prompt or checking for changes.",
envvar="PYINFRA_YES",
show_envvar=True,
)
@click.option(
"--limit",
help="Restrict the target hosts by name and group name.",
multiple=True,
)
@click.option("--fail-percent", type=int, help="% of hosts that need to fail before exiting early.")
@click.option(
"--data",
multiple=True,
help="Override data values, format key=value.",
)
@click.option(
"--group-data",
multiple=True,
help="Paths to load additional group data from (overrides matching keys).",
)
@click.option(
"--config",
"config_filename",
help="Specify config file to use (default: config.py).",
default="config.py",
)
@click.option(
"--chdir",
help="Set the working directory before executing.",
)
# Auth args
@click.option(
"--sudo",
is_flag=True,
default=False,
help="Whether to execute operations with sudo.",
)
@click.option("--sudo-user", help="Which user to sudo when sudoing.")
@click.option(
"--use-sudo-password",
is_flag=True,
default=False,
help="Whether to use a password with sudo.",
)
@click.option("--su-user", help="Which user to su to.")
@click.option("--shell-executable", help='Shell to use (ex: "sh", "cmd", "ps").')
# Operation flow args
@click.option("--parallel", type=int, help="Number of operations to run in parallel.")
@click.option(
"--no-wait",
is_flag=True,
default=False,
help="Don't wait between operations for hosts.",
)
@click.option(
"--serial",
is_flag=True,
default=False,
help="Run operations in serial, host by host.",
)
# SSH connector args
# TODO: remove the non-ssh-prefixed variants
@click.option("--ssh-user", "--user", "ssh_user", help="SSH user to connect as.")
@click.option("--ssh-port", "--port", "ssh_port", type=int, help="SSH port to connect to.")
@click.option("--ssh-key", "--key", "ssh_key", type=click.Path(), help="SSH Private key filename.")
@click.option(
"--ssh-key-password",
"--key-password",
"ssh_key_password",
help="SSH Private key password.",
)
@click.option("--ssh-password", "--password", "ssh_password", help="SSH password.")
# Eager commands (pyinfra --support)
@click.option(
"--support",
is_flag=True,
is_eager=True,
callback=_print_support,
help="Print useful information for support and exit.",
)
# Debug args
@click.option(
"--debug",
is_flag=True,
default=False,
help="Print debug logs from pyinfra.",
)
@click.option(
"--debug-all",
is_flag=True,
default=False,
help="Print debug logs from all packages including pyinfra.",
)
@click.option(
"--debug-facts",
is_flag=True,
default=False,
help="Print facts after generating operations and exit.",
)
@click.option(
"--debug-operations",
is_flag=True,
default=False,
help="Print operations after generating and exit.",
)
@click.version_option(
version=__version__,
prog_name="pyinfra",
message="%(prog)s: v%(version)s",
)
def cli(*args, **kwargs):
"""
pyinfra manages the state of one or more servers. It can be used for
app/service deployment, config management and ad-hoc command execution.
Documentation: docs.pyinfra.com
# INVENTORY
\b
+ a file (inventory.py)
+ hostname (host.net)
+ Comma separated hostnames:
host-1.net,host-2.net,@local
# OPERATIONS
\b
# Run one or more deploys against the inventory
pyinfra INVENTORY deploy_web.py [deploy_db.py]...
\b
# Run a single operation against the inventory
pyinfra INVENTORY server.user pyinfra home=/home/pyinfra
\b
# Execute an arbitrary command against the inventory
pyinfra INVENTORY exec -- echo "hello world"
\b
# Run one or more facts against the inventory
pyinfra INVENTORY fact server.LinuxName [server.Users]...
pyinfra INVENTORY fact files.File path=/path/to/file...
\b
# Debug the inventory hosts and data
pyinfra INVENTORY debug-inventory
"""
try:
_main(*args, **kwargs)
except (CliError, UnexpectedExternalError):
raise
except PyinfraError as e:
# Re-raise "expected" pyinfra exceptions with our click exception wrapper
raise WrappedError(e)
except Exception as e:
# Re-raise any unexpected internal exceptions as UnexpectedInternalError
raise UnexpectedInternalError(e)
finally:
if ctx_state.isset() and state.initialised:
logger.info("--> Disconnecting from hosts...")
# Triggers any executor disconnect requirements
disconnect_all(state)
class CliCommands:
DEBUG_INVENTORY = "DEBUG_INVENTORY"
FACT = "FACT"
SHELL = "SHELL"
DEPLOY_FILES = "DEPLOY_FILES"
FUNC = "FUNC"
def _main(
inventory,
operations: Union[List, Tuple],
verbosity: int,
chdir: str,
ssh_user,
ssh_port: int,
ssh_key,
ssh_key_password: str,
ssh_password: str,
shell_executable,
sudo: bool,
sudo_user: str,
use_sudo_password: bool,
su_user: str,
parallel: int,
fail_percent: int,
data,
group_data,
config_filename: str,
dry: bool,
yes: bool,
limit: Iterable,
no_wait: bool,
serial: bool,
debug: bool,
debug_all: bool,
debug_facts: bool,
debug_operations: bool,
support: bool = False,
):
# Setup working directory
#
if chdir:
os_chdir(chdir)
# Setup logging & Bootstrap/Venv
#
_setup_log_level(debug, debug_all)
init_virtualenv()
# Check operations are valid and setup commands
#
original_operations, operations, command, chdir = _validate_operations(operations, chdir)
# Setup state, config & inventory
#
state = _setup_state(verbosity, yes)
config = Config()
ctx_config.set(config)
# Update Config & Override Data
#
config = _set_config(
config,
config_filename,
sudo,
sudo_user,
use_sudo_password,
su_user,
parallel,
shell_executable,
fail_percent,
yes,
)
override_data = _set_override_data(
data,
ssh_user,
ssh_key,
ssh_key_password,
ssh_port,
ssh_password,
)
if yes is False:
_set_fail_prompts(state, config)
# Load up the inventory from the filesystem
#
logger.info("--> Loading inventory...")
inventory = make_inventory(
inventory,
cwd=state.cwd,
override_data=override_data,
group_data_directories=group_data,
)
ctx_inventory.set(inventory)
# Now that we have inventory, apply --limit config override
initial_limit = _apply_inventory_limit(inventory, limit)
# Initialise the state
state.init(inventory, config, initial_limit=initial_limit)
if command == CliCommands.DEBUG_INVENTORY:
print_inventory(state)
_exit()
# Connect to the hosts & start handling the user commands
#
logger.info("--> Connecting to hosts...")
state.set_stage(StateStage.Connect)
connect_all(state)
logger.info("--> Preparing operations...")
state.set_stage(StateStage.Prepare)
can_diff, state, config = _handle_commands(
state, config, command, original_operations, operations
)
# Print proposed changes, execute unless --dry, and exit
#
if can_diff:
if yes:
logger.info("--> Skipping change detection")
else:
logger.info("--> Detected changes:")
print_meta(state)
click.echo(
"""
Detected changes may not include every change pyinfra will execute.
Hidden side effects of operations may alter behaviour of future operations,
this will be shown in the results. The remote state will always be updated
to reflect the state defined by the input operations.""",
err=True,
)
# If --debug-facts or --debug-operations, print and exit
if debug_facts or debug_operations:
if debug_operations:
print_state_operations(state)
_exit()
if dry:
_exit()
if (
can_diff
and not yes
and not _do_confirm("Detected changes displayed above, skip this step with -y")
):
_exit()
logger.info("--> Beginning operation run...")
state.set_stage(StateStage.Execute)
run_ops(state, serial=serial, no_wait=no_wait)
logger.info("--> Results:")
state.set_stage(StateStage.Disconnect)
print_results(state)
_exit()
def _do_confirm(msg: str) -> bool:
click.echo(err=True)
click.echo(f" {msg}", err=True)
warning_count = state.get_warning_counter()
if warning_count > 0:
click.secho(
f" {warning_count} warnings shown during change detection, see above",
fg="yellow",
err=True,
)
confirm_msg = " Press enter to execute..."
click.echo(confirm_msg, err=True, nl=False)
v = input()
if v:
click.echo(f" Unexpected user input: {v}", err=True)
return False
# Go up, clear the line, go up again - as if the confirmation statement was never here!
click.echo(
"\033[1A{0}\033[1A".format("".join(" " for _ in range(len(confirm_msg)))),
err=True,
nl=False,
)
click.echo(err=True)
return True
# Setup
#
def _setup_log_level(debug, debug_all):
if not debug and not sys.warnoptions:
warnings.simplefilter("ignore")
log_level = logging.INFO
if debug or debug_all:
log_level = logging.DEBUG
other_log_level = None
if debug_all:
other_log_level = logging.DEBUG
setup_logging(log_level, other_log_level)
def _validate_operations(operations, chdir):
# Make a copy before we overwrite
original_operations = operations
# Debug (print) inventory + group data
if operations[0] == "debug-inventory":
command = CliCommands.DEBUG_INVENTORY
# Get one or more facts
elif operations[0] == "fact":
command = CliCommands.FACT
operations = get_facts_and_args(operations[1:])
# Execute a raw command with server.shell
elif operations[0] == "exec":
command = CliCommands.SHELL
operations = operations[1:]
# Execute one or more deploy files
elif all(cmd.endswith(".py") for cmd in operations):
command = CliCommands.DEPLOY_FILES
filenames = []
for filename in operations[0:]:
if path.exists(filename):
filenames.append(filename)
continue
if chdir and filename.startswith(chdir):
correct_filename = path.relpath(filename, chdir)
logger.warning(
(
"Fixing deploy filename under `--chdir` argument: "
f"{filename} -> {correct_filename}"
),
)
filenames.append(correct_filename)
continue
raise CliError(
"No deploy file: {0}".format(
path.join(chdir, filename) if chdir else filename,
),
)
operations = filenames
# Load a function (op or deploy) directly with arguments
elif "." in operations[0]:
command = CliCommands.FUNC
operations = get_func_and_args(operations)
else:
raise CliError(
"""Invalid operations: {0}
Operation usage:
pyinfra INVENTORY deploy_web.py [deploy_db.py]...
pyinfra INVENTORY server.user pyinfra home=/home/pyinfra
pyinfra INVENTORY exec -- echo "hello world"
pyinfra INVENTORY fact os [users]...""".format(
operations,
),
)
return original_operations, operations, command, chdir
def _set_verbosity(state, verbosity):
if verbosity > 0:
state.print_fact_info = True
state.print_noop_info = True
if verbosity > 1:
state.print_input = state.print_fact_input = True
if verbosity > 2:
state.print_output = state.print_fact_output = True
return state
def _setup_state(verbosity, yes):
cwd = getcwd()
if cwd not in sys.path: # ensure cwd is present in sys.path
sys.path.append(cwd)
state = State(check_for_changes=not yes)
state.cwd = cwd
ctx_state.set(state)
state = _set_verbosity(state, verbosity)
return state
def _set_config(
config,
config_filename,
sudo,
sudo_user,
use_sudo_password,
su_user,
parallel,
shell_executable,
fail_percent,
yes,
):
logger.info("--> Loading config...")
# Load up any config.py from the filesystem
if state.cwd:
config_filename = path.join(state.cwd, config_filename)
if path.exists(config_filename):
exec_file(config_filename)
# Lock the current config, this allows us to restore this version after
# executing deploy files that may alter them.
config.lock_current_state()
# Arg based config overrides
if sudo:
config.SUDO = True
if sudo_user:
config.SUDO_USER = sudo_user
if use_sudo_password:
config.USE_SUDO_PASSWORD = use_sudo_password
if su_user:
config.SU_USER = su_user
if parallel:
config.PARALLEL = parallel
if shell_executable:
config.SHELL = None if shell_executable in ("None", "null") else shell_executable
if fail_percent is not None:
config.FAIL_PERCENT = fail_percent
return config
def _set_override_data(
data,
ssh_user,
ssh_key,
ssh_key_password,
ssh_port,
ssh_password,
):
override_data = {}
for arg in data:
key, value = arg.split("=", 1)
override_data[key] = value
override_data = {key: parse_cli_arg(value) for key, value in override_data.items()}
for key, value in (
("ssh_user", ssh_user),
("ssh_key", ssh_key),
("ssh_key_password", ssh_key_password),
("ssh_port", ssh_port),
("ssh_password", ssh_password),
):
if value:
override_data[key] = value
return override_data
def _set_fail_prompts(state: State, config: Config) -> None:
# Set fail percent to zero, meaning we'll raise an exception for any fail,
# and we can capture + prompt the user to continue/exit.
config.FAIL_PERCENT = 0
def should_raise_failed_hosts(state: State) -> bool:
if state.current_stage == StateStage.Connect:
return not _do_confirm("One of more hosts failed to connect, continue?")
return not _do_confirm("One of more hosts failed, continue?")
state.should_raise_failed_hosts = should_raise_failed_hosts
def _apply_inventory_limit(inventory, limit):
initial_limit = None
if limit:
all_limit_hosts = []
for limiter in limit:
try:
limit_hosts = inventory.get_group(limiter)
except NoGroupError:
limit_hosts = [host for host in inventory if fnmatch(host.name, limiter)]
if not limit_hosts:
logger.warning("No host matches found for --limit pattern: {0}".format(limiter))
all_limit_hosts.extend(limit_hosts)
initial_limit = list(set(all_limit_hosts))
return initial_limit
# Operations Execution
#
def _handle_commands(state, config, command, original_operations, operations):
if command is CliCommands.FACT:
state, fact_data = _run_fact_operations(state, config, operations)
print_facts(fact_data)
_exit()
can_diff = True
if command == CliCommands.SHELL:
state = _prepare_exec_operations(state, config, operations)
can_diff = False
elif command == CliCommands.DEPLOY_FILES:
state, config, operations = _prepare_deploy_operations(state, config, operations)
elif command == CliCommands.FUNC:
state, kwargs = _prepare_func_operations(
state,
config,
operations,
original_operations,
)
return can_diff, state, config
def _run_fact_operations(state, config, operations):
logger.info("--> Gathering facts...")
state.print_fact_info = True
fact_data = {}
for i, command in enumerate(operations):
fact_cls, args, kwargs = command
fact_key = fact_cls.name
if args or kwargs:
_fact_args = args or ""
_fact_details = " ({0})".format(get_kwargs_str(kwargs)) if kwargs else ""
fact_key = "{0}{1}{2}".format(fact_cls.name, _fact_args, _fact_details)
try:
fact_data[fact_key] = get_facts(
state,
fact_cls,
args=args,
kwargs=kwargs,
apply_failed_hosts=False,
)
except PyinfraError:
pass
return state, fact_data
def _prepare_exec_operations(state, config, operations):
state.print_output = True
load_func(
state,
server.shell,
" ".join(operations),
)
return state
def _prepare_deploy_operations(state, config, operations):
logger.info("--> Preparing Operations...")
# Number of "steps" to make = number of files * number of hosts
for i, filename in enumerate(operations):
_log_styled_msg = click.style(filename, bold=True)
logger.info("Loading: {0}".format(_log_styled_msg))
state.current_op_file_number = i
load_deploy_file(state, filename)
# Remove any config changes introduced by the deploy file & any includes
config.reset_locked_state()
return state, config, operations
def _prepare_func_operations(state, config, operations, original_operations):
logger.info("--> Preparing operation...")
op, args = operations
args, kwargs = args
load_func(state, op, *args, **kwargs)
return state, kwargs