pyinfra-cli-api 0.0.1__tar.gz
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.
- pyinfra_cli_api-0.0.1/LICENSE.md +5 -0
- pyinfra_cli_api-0.0.1/PKG-INFO +103 -0
- pyinfra_cli_api-0.0.1/README.md +90 -0
- pyinfra_cli_api-0.0.1/pyproject.toml +53 -0
- pyinfra_cli_api-0.0.1/setup.cfg +4 -0
- pyinfra_cli_api-0.0.1/src/pyinfra_cli_api/__init__.py +41 -0
- pyinfra_cli_api-0.0.1/src/pyinfra_cli_api/api.py +194 -0
- pyinfra_cli_api-0.0.1/src/pyinfra_cli_api.egg-info/PKG-INFO +103 -0
- pyinfra_cli_api-0.0.1/src/pyinfra_cli_api.egg-info/SOURCES.txt +10 -0
- pyinfra_cli_api-0.0.1/src/pyinfra_cli_api.egg-info/dependency_links.txt +1 -0
- pyinfra_cli_api-0.0.1/src/pyinfra_cli_api.egg-info/requires.txt +1 -0
- pyinfra_cli_api-0.0.1/src/pyinfra_cli_api.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
2
|
+
|
|
3
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
4
|
+
|
|
5
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyinfra-cli-api
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: pyinfra cli api helper
|
|
5
|
+
Author-email: Timshel <timshel@github.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: repository, https://github.com/timshel/pyinfra-cli-api
|
|
8
|
+
Requires-Python: <4.0,>=3.11
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.md
|
|
11
|
+
Requires-Dist: pyinfra==3.11.2
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
A library helper to use [pyinfra-cli](https://docs.pyinfra.com/en/3.x/cli.html) but with additional features.
|
|
15
|
+
|
|
16
|
+
Coming from `Ansible` I missed :
|
|
17
|
+
|
|
18
|
+
- Restricting hosts at the `playbook`/`deploy` level.
|
|
19
|
+
- Running an operation on a specific/different host (`delegate_to`)
|
|
20
|
+
- Tags support to filter operations
|
|
21
|
+
|
|
22
|
+
The idea is to initialize the `cli` in the `deploy` allowwing to directly pass some configurations.
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from pyinfra.operations import files
|
|
26
|
+
from pyinfra_cli_api import cli
|
|
27
|
+
|
|
28
|
+
state = cli(
|
|
29
|
+
inventory="inventory.py"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
state.add_op(
|
|
33
|
+
files.directory,
|
|
34
|
+
tags=["setup"],
|
|
35
|
+
name=f"Create basedir {basedir}",
|
|
36
|
+
path=basedir,
|
|
37
|
+
mode="755",
|
|
38
|
+
present=True,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
state.run()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Can the be run with:
|
|
45
|
+
|
|
46
|
+
``` bash
|
|
47
|
+
> python deploy.py --help
|
|
48
|
+
usage: pyinfra_cli_api [-h] [--limit [LIMIT ...]] [--tags [TAGS ...]] [--debug-inventory] [-y] [inventory]
|
|
49
|
+
|
|
50
|
+
positional arguments:
|
|
51
|
+
inventory Inventory file, override the `inventory` parameter
|
|
52
|
+
|
|
53
|
+
options:
|
|
54
|
+
-h, --help show this help message and exit
|
|
55
|
+
--limit [LIMIT ...] Further restrict the target hosts by name and group name.
|
|
56
|
+
--tags [TAGS ...] Restrict the operations.
|
|
57
|
+
--debug-inventory Display the inventory.
|
|
58
|
+
-y, --run-without-confirmation Run without confirmation
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Restricting hosts at the `playbook`/`deploy` level.
|
|
62
|
+
|
|
63
|
+
A limit parameter can be passed on initialization.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from pyinfra_cli_api import cli
|
|
67
|
+
|
|
68
|
+
state = cli(
|
|
69
|
+
limit=["dedi-nginx"]
|
|
70
|
+
)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The cli still support the `--limit` parameter to further restrict it if needed.
|
|
74
|
+
|
|
75
|
+
## Running an operation on a specific/different host (`delegate_to`)
|
|
76
|
+
|
|
77
|
+
A separate `connect` parameter can be specified to retrieve the host facts (host need to be defined in the inventory).
|
|
78
|
+
When adding an operation the host can be specified instead of the default one defined by the `limit`.
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from pyinfra.operations import files
|
|
82
|
+
from pyinfra_cli_api import cli
|
|
83
|
+
|
|
84
|
+
state = cli(
|
|
85
|
+
limit=["nginx"],
|
|
86
|
+
connect=["@local"],
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
local = state.get_host("@local")
|
|
90
|
+
|
|
91
|
+
state.add_op(
|
|
92
|
+
files.download,
|
|
93
|
+
tags=["setup"],
|
|
94
|
+
name="Download Ketesa",
|
|
95
|
+
src=f"https://github.com/etkecc/ketesa/releases/download/{ketesa_version}/ketesa.tar.gz",
|
|
96
|
+
dest=f"{local_tmp}/ketesa-{ketesa_version}.tar.gz",
|
|
97
|
+
host=[local],
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Tags support to filter operations
|
|
102
|
+
|
|
103
|
+
When adding an operation tags can be defined. If the `--tags` cli parameter is used then only operations with a matching tag will be considered.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
A library helper to use [pyinfra-cli](https://docs.pyinfra.com/en/3.x/cli.html) but with additional features.
|
|
2
|
+
|
|
3
|
+
Coming from `Ansible` I missed :
|
|
4
|
+
|
|
5
|
+
- Restricting hosts at the `playbook`/`deploy` level.
|
|
6
|
+
- Running an operation on a specific/different host (`delegate_to`)
|
|
7
|
+
- Tags support to filter operations
|
|
8
|
+
|
|
9
|
+
The idea is to initialize the `cli` in the `deploy` allowwing to directly pass some configurations.
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from pyinfra.operations import files
|
|
13
|
+
from pyinfra_cli_api import cli
|
|
14
|
+
|
|
15
|
+
state = cli(
|
|
16
|
+
inventory="inventory.py"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
state.add_op(
|
|
20
|
+
files.directory,
|
|
21
|
+
tags=["setup"],
|
|
22
|
+
name=f"Create basedir {basedir}",
|
|
23
|
+
path=basedir,
|
|
24
|
+
mode="755",
|
|
25
|
+
present=True,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
state.run()
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Can the be run with:
|
|
32
|
+
|
|
33
|
+
``` bash
|
|
34
|
+
> python deploy.py --help
|
|
35
|
+
usage: pyinfra_cli_api [-h] [--limit [LIMIT ...]] [--tags [TAGS ...]] [--debug-inventory] [-y] [inventory]
|
|
36
|
+
|
|
37
|
+
positional arguments:
|
|
38
|
+
inventory Inventory file, override the `inventory` parameter
|
|
39
|
+
|
|
40
|
+
options:
|
|
41
|
+
-h, --help show this help message and exit
|
|
42
|
+
--limit [LIMIT ...] Further restrict the target hosts by name and group name.
|
|
43
|
+
--tags [TAGS ...] Restrict the operations.
|
|
44
|
+
--debug-inventory Display the inventory.
|
|
45
|
+
-y, --run-without-confirmation Run without confirmation
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Restricting hosts at the `playbook`/`deploy` level.
|
|
49
|
+
|
|
50
|
+
A limit parameter can be passed on initialization.
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from pyinfra_cli_api import cli
|
|
54
|
+
|
|
55
|
+
state = cli(
|
|
56
|
+
limit=["dedi-nginx"]
|
|
57
|
+
)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The cli still support the `--limit` parameter to further restrict it if needed.
|
|
61
|
+
|
|
62
|
+
## Running an operation on a specific/different host (`delegate_to`)
|
|
63
|
+
|
|
64
|
+
A separate `connect` parameter can be specified to retrieve the host facts (host need to be defined in the inventory).
|
|
65
|
+
When adding an operation the host can be specified instead of the default one defined by the `limit`.
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from pyinfra.operations import files
|
|
69
|
+
from pyinfra_cli_api import cli
|
|
70
|
+
|
|
71
|
+
state = cli(
|
|
72
|
+
limit=["nginx"],
|
|
73
|
+
connect=["@local"],
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
local = state.get_host("@local")
|
|
77
|
+
|
|
78
|
+
state.add_op(
|
|
79
|
+
files.download,
|
|
80
|
+
tags=["setup"],
|
|
81
|
+
name="Download Ketesa",
|
|
82
|
+
src=f"https://github.com/etkecc/ketesa/releases/download/{ketesa_version}/ketesa.tar.gz",
|
|
83
|
+
dest=f"{local_tmp}/ketesa-{ketesa_version}.tar.gz",
|
|
84
|
+
host=[local],
|
|
85
|
+
)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Tags support to filter operations
|
|
89
|
+
|
|
90
|
+
When adding an operation tags can be defined. If the `--tags` cli parameter is used then only operations with a matching tag will be considered.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pyinfra-cli-api"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "pyinfra cli api helper"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Timshel", email = "timshel@github.com" },
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE.md"]
|
|
11
|
+
|
|
12
|
+
requires-python = ">=3.11,<4.0"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"pyinfra==3.11.2",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
repository = "https://github.com/timshel/pyinfra-cli-api"
|
|
19
|
+
|
|
20
|
+
[dependency-groups]
|
|
21
|
+
dev = [
|
|
22
|
+
"mypy==1.17.1",
|
|
23
|
+
"ruff==0.14.0",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
docs = [
|
|
27
|
+
"pdoc==16.0.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[tool.ruff]
|
|
31
|
+
line-length = 150
|
|
32
|
+
target-version = "py310"
|
|
33
|
+
extend-exclude = ["*.json", "*.yaml", "*.yml"]
|
|
34
|
+
force-exclude = true
|
|
35
|
+
|
|
36
|
+
[tool.ruff.lint]
|
|
37
|
+
# ruff defaults + pyupgrade
|
|
38
|
+
select = ["E4", "E7", "E9", "F", "UP"]
|
|
39
|
+
ignore = []
|
|
40
|
+
|
|
41
|
+
[tool.ruff.lint.isort]
|
|
42
|
+
combine-as-imports = true
|
|
43
|
+
|
|
44
|
+
[tool.mypy]
|
|
45
|
+
check_untyped_defs = true
|
|
46
|
+
ignore_missing_imports = true
|
|
47
|
+
enable_error_code = "explicit-override"
|
|
48
|
+
files = "src/pyinfra_cli_api"
|
|
49
|
+
|
|
50
|
+
[tool.uv.workspace]
|
|
51
|
+
members = [
|
|
52
|
+
"temp",
|
|
53
|
+
]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
from collections.abc import Collection
|
|
3
|
+
|
|
4
|
+
from .api import CliApi
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def cli(
|
|
8
|
+
inventory: str | None = None,
|
|
9
|
+
limit: Collection[str] = [],
|
|
10
|
+
connect: Collection[str] = [],
|
|
11
|
+
) -> CliApi:
|
|
12
|
+
"""
|
|
13
|
+
Cli helper to setup the ``CliApi``
|
|
14
|
+
:param inventory: The default inventory file name, can be override by the `ìnventory` argument.
|
|
15
|
+
:param limit: Restrict the inventory to only a subset of hosts, further limited with `--limit` argument.
|
|
16
|
+
:param connect: A subset of ``pyinfra.api.host.Host`` from which to retrieve facts but not included in default operations.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
parser = argparse.ArgumentParser(prog="pyinfra-cli-api")
|
|
20
|
+
if inventory:
|
|
21
|
+
parser.add_argument("inventory", nargs="?", type=str, default=inventory, help="Inventory file")
|
|
22
|
+
else:
|
|
23
|
+
parser.add_argument("inventory", type=str, help="Inventory file, override the `inventory` parameter")
|
|
24
|
+
|
|
25
|
+
parser.add_argument("--limit", nargs="*", type=str, default=[], help="Further restrict the target hosts by name and group name.")
|
|
26
|
+
parser.add_argument("--tags", nargs="*", type=str, default=[], help="Restrict the operations.")
|
|
27
|
+
parser.add_argument("--debug-inventory", action="store_true", help="Display the inventory.")
|
|
28
|
+
parser.add_argument("-y", "--run-without-confirmation", action="store_true", help="Run without confirmation")
|
|
29
|
+
args = parser.parse_args()
|
|
30
|
+
|
|
31
|
+
state = CliApi(
|
|
32
|
+
inventory=args.inventory,
|
|
33
|
+
limit=limit,
|
|
34
|
+
sub_limit=args.limit,
|
|
35
|
+
connect=connect,
|
|
36
|
+
debug_inventory=args.debug_inventory,
|
|
37
|
+
run_without_confirmation=args.run_without_confirmation,
|
|
38
|
+
tags=args.tags,
|
|
39
|
+
)
|
|
40
|
+
state.connect()
|
|
41
|
+
return state
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
import logging
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable, Collection, Iterable
|
|
6
|
+
from rich.prompt import Confirm
|
|
7
|
+
|
|
8
|
+
from pyinfra import logger
|
|
9
|
+
from pyinfra.context import ctx_host, ctx_state
|
|
10
|
+
|
|
11
|
+
from pyinfra.api import Config, Inventory, State
|
|
12
|
+
from pyinfra.api.connect import connect_all, disconnect_all
|
|
13
|
+
from pyinfra.api.host import Host
|
|
14
|
+
from pyinfra.api.operations import run_ops
|
|
15
|
+
from pyinfra.api.output import set_echo, set_formatter
|
|
16
|
+
from pyinfra.api.state import StateStage
|
|
17
|
+
|
|
18
|
+
from pyinfra_cli.cli import _get_inventory_pattern_matches
|
|
19
|
+
from pyinfra_cli.console import console, echo, format_text
|
|
20
|
+
from pyinfra_cli.inventory import make_inventory_from_files
|
|
21
|
+
from pyinfra_cli.log import LogHandler, LogFormatter
|
|
22
|
+
from pyinfra_cli.prints import (
|
|
23
|
+
print_inventory,
|
|
24
|
+
print_meta,
|
|
25
|
+
print_results,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _setup_logging(global_log_level, pyinfra_log_level):
|
|
30
|
+
set_formatter(format_text)
|
|
31
|
+
set_echo(echo)
|
|
32
|
+
|
|
33
|
+
logging.basicConfig(level=global_log_level)
|
|
34
|
+
|
|
35
|
+
handler = LogHandler()
|
|
36
|
+
formatter = LogFormatter()
|
|
37
|
+
handler.setFormatter(formatter)
|
|
38
|
+
logger.addHandler(handler)
|
|
39
|
+
logger.propagate = False
|
|
40
|
+
logger.setLevel(pyinfra_log_level)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _host_limit(inventory: Inventory, limit: Collection[Host] | None, patterns: Collection[str], connect: Collection[str], option_name: str):
|
|
44
|
+
if patterns:
|
|
45
|
+
hosts = _get_inventory_pattern_matches(inventory, [*patterns, *connect], option_name)
|
|
46
|
+
return list(set(limit) & set(hosts)) if isinstance(limit, Collection) else hosts
|
|
47
|
+
else:
|
|
48
|
+
return limit
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class CliApi:
|
|
52
|
+
_connect: set[str]
|
|
53
|
+
_run_without_confirmation: bool
|
|
54
|
+
_tags: set[str]
|
|
55
|
+
|
|
56
|
+
state: State
|
|
57
|
+
|
|
58
|
+
def __init__(
|
|
59
|
+
self,
|
|
60
|
+
inventory: str,
|
|
61
|
+
limit: Collection[str] = [],
|
|
62
|
+
sub_limit: Collection[str] = [],
|
|
63
|
+
connect: Collection[str] = [],
|
|
64
|
+
debug_inventory: bool = False,
|
|
65
|
+
run_without_confirmation: bool = False,
|
|
66
|
+
tags: Collection[str] = [],
|
|
67
|
+
global_log_level=logging.WARN,
|
|
68
|
+
pyinfra_log_level=logging.INFO,
|
|
69
|
+
):
|
|
70
|
+
"""
|
|
71
|
+
Load the inventory and initialize the internal ``pyinfra.state``
|
|
72
|
+
|
|
73
|
+
:param inventory: The inventory file name
|
|
74
|
+
:param limit: Restrict the inventory to only a subset of hosts
|
|
75
|
+
:param sub_limit: Restrict the inventory to only a subset of hosts (applied after `limit`)
|
|
76
|
+
:param connect: A subset of ``pyinfra.api.host.Host`` from which to retrieve states but not included in default operations.
|
|
77
|
+
:param tags: Allow to filter operations.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
self._connect = set(connect)
|
|
81
|
+
self._run_without_confirmation = run_without_confirmation
|
|
82
|
+
self._tags = set(tags)
|
|
83
|
+
|
|
84
|
+
_setup_logging(global_log_level, pyinfra_log_level)
|
|
85
|
+
|
|
86
|
+
logger.info(f"--> Loading inventory from: {inventory}")
|
|
87
|
+
py_inventory = make_inventory_from_files(inventory)
|
|
88
|
+
config = Config()
|
|
89
|
+
|
|
90
|
+
initial_limit = _host_limit(py_inventory, None, limit, connect, "deploy-limit")
|
|
91
|
+
initial_limit = _host_limit(py_inventory, initial_limit, sub_limit, connect, "--limit")
|
|
92
|
+
|
|
93
|
+
self.state = State(inventory=py_inventory, config=config, initial_limit=initial_limit)
|
|
94
|
+
|
|
95
|
+
if debug_inventory:
|
|
96
|
+
print_inventory(self.state)
|
|
97
|
+
|
|
98
|
+
def _disconnect(self):
|
|
99
|
+
if self.state.initialised:
|
|
100
|
+
logger.info("--> Disconnecting from hosts...")
|
|
101
|
+
# Triggers any executor disconnect requirements
|
|
102
|
+
disconnect_all(self.state)
|
|
103
|
+
|
|
104
|
+
def connect(self):
|
|
105
|
+
"""
|
|
106
|
+
Connect to the hosts to gather facts
|
|
107
|
+
"""
|
|
108
|
+
try:
|
|
109
|
+
logger.info("--> Connecting to hosts...")
|
|
110
|
+
self.state.set_stage(StateStage.Connect)
|
|
111
|
+
connect_all(self.state)
|
|
112
|
+
|
|
113
|
+
logger.info("--> Preparing operation files...")
|
|
114
|
+
self.state.set_stage(StateStage.Prepare)
|
|
115
|
+
except:
|
|
116
|
+
self._disconnect()
|
|
117
|
+
raise
|
|
118
|
+
|
|
119
|
+
def get_active_hosts(self) -> Iterable[Host]:
|
|
120
|
+
"""
|
|
121
|
+
Return the list of ``pyinfra.api.host.Host`` on which operations will apply.
|
|
122
|
+
"""
|
|
123
|
+
return filter(lambda h: h.name not in self._connect, self.state.inventory.get_active_hosts())
|
|
124
|
+
|
|
125
|
+
def get_host(self, name: str) -> Host:
|
|
126
|
+
return self.state.inventory.get_host(name)
|
|
127
|
+
|
|
128
|
+
def add_op(self, op_func: Callable, *args, **kwargs):
|
|
129
|
+
"""
|
|
130
|
+
Prepare & add an operation to ``pyinfra.state`` by executing it on all hosts.
|
|
131
|
+
|
|
132
|
+
:param op_func (function): the operation function from one of the modules, ie ``server.user``
|
|
133
|
+
:param host (Collection[Host]): Override the default operation target to only those specific hosts.
|
|
134
|
+
:param tags (Collection[str]): The tags relevant to this operation, operation will be ignored if no tags match.
|
|
135
|
+
:param args: passed to the operation function
|
|
136
|
+
:param kwargs: passed to the operation function. If ``Callable``, invoked with the ``pyinfra.api.host.Host`` as parameter.
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
hosts = kwargs.pop("host", self.get_active_hosts())
|
|
140
|
+
tags = kwargs.pop("tags", [])
|
|
141
|
+
|
|
142
|
+
if self._tags and tags and not (self._tags & set(tags)):
|
|
143
|
+
return {}
|
|
144
|
+
|
|
145
|
+
with ctx_state.use(self.state):
|
|
146
|
+
results = {}
|
|
147
|
+
for op_host in hosts:
|
|
148
|
+
with ctx_host.use(op_host):
|
|
149
|
+
resolved_args = kwargs.copy()
|
|
150
|
+
for key, value in resolved_args.items():
|
|
151
|
+
if inspect.isfunction(value):
|
|
152
|
+
resolved_args[key] = value(op_host)
|
|
153
|
+
|
|
154
|
+
results[op_host] = op_func(*args, **resolved_args)
|
|
155
|
+
|
|
156
|
+
return results
|
|
157
|
+
|
|
158
|
+
def run(self):
|
|
159
|
+
"""
|
|
160
|
+
Compute change then if applicable run them and display the results.
|
|
161
|
+
"""
|
|
162
|
+
try:
|
|
163
|
+
if self.state.get_op_order():
|
|
164
|
+
logger.info("--> Detected changes:")
|
|
165
|
+
print_meta(self.state)
|
|
166
|
+
else:
|
|
167
|
+
logger.info("--> No change to apply")
|
|
168
|
+
sys.exit(0)
|
|
169
|
+
|
|
170
|
+
if not self._run_without_confirmation:
|
|
171
|
+
if not self._do_confirm():
|
|
172
|
+
sys.exit(0)
|
|
173
|
+
|
|
174
|
+
logger.info("--> Beginning operation run...")
|
|
175
|
+
self.state.set_stage(StateStage.Execute)
|
|
176
|
+
run_ops(self.state)
|
|
177
|
+
|
|
178
|
+
logger.info("--> Results:")
|
|
179
|
+
self.state.set_stage(StateStage.Disconnect)
|
|
180
|
+
print_results(self.state)
|
|
181
|
+
|
|
182
|
+
finally:
|
|
183
|
+
self._disconnect()
|
|
184
|
+
|
|
185
|
+
def _do_confirm(self) -> bool:
|
|
186
|
+
console.print()
|
|
187
|
+
console.print(" Detected changes displayed above, skip this step with -y")
|
|
188
|
+
warning_count = self.state.get_warning_counter()
|
|
189
|
+
if warning_count > 0:
|
|
190
|
+
console.print(
|
|
191
|
+
f" {warning_count} warnings shown during change detection, see above",
|
|
192
|
+
style="yellow",
|
|
193
|
+
)
|
|
194
|
+
return Confirm.ask(" Execute?", console=console, default=True)
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyinfra-cli-api
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: pyinfra cli api helper
|
|
5
|
+
Author-email: Timshel <timshel@github.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: repository, https://github.com/timshel/pyinfra-cli-api
|
|
8
|
+
Requires-Python: <4.0,>=3.11
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.md
|
|
11
|
+
Requires-Dist: pyinfra==3.11.2
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
A library helper to use [pyinfra-cli](https://docs.pyinfra.com/en/3.x/cli.html) but with additional features.
|
|
15
|
+
|
|
16
|
+
Coming from `Ansible` I missed :
|
|
17
|
+
|
|
18
|
+
- Restricting hosts at the `playbook`/`deploy` level.
|
|
19
|
+
- Running an operation on a specific/different host (`delegate_to`)
|
|
20
|
+
- Tags support to filter operations
|
|
21
|
+
|
|
22
|
+
The idea is to initialize the `cli` in the `deploy` allowwing to directly pass some configurations.
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from pyinfra.operations import files
|
|
26
|
+
from pyinfra_cli_api import cli
|
|
27
|
+
|
|
28
|
+
state = cli(
|
|
29
|
+
inventory="inventory.py"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
state.add_op(
|
|
33
|
+
files.directory,
|
|
34
|
+
tags=["setup"],
|
|
35
|
+
name=f"Create basedir {basedir}",
|
|
36
|
+
path=basedir,
|
|
37
|
+
mode="755",
|
|
38
|
+
present=True,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
state.run()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Can the be run with:
|
|
45
|
+
|
|
46
|
+
``` bash
|
|
47
|
+
> python deploy.py --help
|
|
48
|
+
usage: pyinfra_cli_api [-h] [--limit [LIMIT ...]] [--tags [TAGS ...]] [--debug-inventory] [-y] [inventory]
|
|
49
|
+
|
|
50
|
+
positional arguments:
|
|
51
|
+
inventory Inventory file, override the `inventory` parameter
|
|
52
|
+
|
|
53
|
+
options:
|
|
54
|
+
-h, --help show this help message and exit
|
|
55
|
+
--limit [LIMIT ...] Further restrict the target hosts by name and group name.
|
|
56
|
+
--tags [TAGS ...] Restrict the operations.
|
|
57
|
+
--debug-inventory Display the inventory.
|
|
58
|
+
-y, --run-without-confirmation Run without confirmation
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Restricting hosts at the `playbook`/`deploy` level.
|
|
62
|
+
|
|
63
|
+
A limit parameter can be passed on initialization.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from pyinfra_cli_api import cli
|
|
67
|
+
|
|
68
|
+
state = cli(
|
|
69
|
+
limit=["dedi-nginx"]
|
|
70
|
+
)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The cli still support the `--limit` parameter to further restrict it if needed.
|
|
74
|
+
|
|
75
|
+
## Running an operation on a specific/different host (`delegate_to`)
|
|
76
|
+
|
|
77
|
+
A separate `connect` parameter can be specified to retrieve the host facts (host need to be defined in the inventory).
|
|
78
|
+
When adding an operation the host can be specified instead of the default one defined by the `limit`.
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from pyinfra.operations import files
|
|
82
|
+
from pyinfra_cli_api import cli
|
|
83
|
+
|
|
84
|
+
state = cli(
|
|
85
|
+
limit=["nginx"],
|
|
86
|
+
connect=["@local"],
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
local = state.get_host("@local")
|
|
90
|
+
|
|
91
|
+
state.add_op(
|
|
92
|
+
files.download,
|
|
93
|
+
tags=["setup"],
|
|
94
|
+
name="Download Ketesa",
|
|
95
|
+
src=f"https://github.com/etkecc/ketesa/releases/download/{ketesa_version}/ketesa.tar.gz",
|
|
96
|
+
dest=f"{local_tmp}/ketesa-{ketesa_version}.tar.gz",
|
|
97
|
+
host=[local],
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Tags support to filter operations
|
|
102
|
+
|
|
103
|
+
When adding an operation tags can be defined. If the `--tags` cli parameter is used then only operations with a matching tag will be considered.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE.md
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/pyinfra_cli_api/__init__.py
|
|
5
|
+
src/pyinfra_cli_api/api.py
|
|
6
|
+
src/pyinfra_cli_api.egg-info/PKG-INFO
|
|
7
|
+
src/pyinfra_cli_api.egg-info/SOURCES.txt
|
|
8
|
+
src/pyinfra_cli_api.egg-info/dependency_links.txt
|
|
9
|
+
src/pyinfra_cli_api.egg-info/requires.txt
|
|
10
|
+
src/pyinfra_cli_api.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyinfra==3.11.2
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyinfra_cli_api
|