Skip to content

Commit

Permalink
server limited running
Browse files Browse the repository at this point in the history
  • Loading branch information
a1fred committed Jun 20, 2022
1 parent 178985c commit fe65b7e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
18 changes: 16 additions & 2 deletions carnival/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def main() -> int:
>>> $ poetry run python -m carnival --help
>>> Usage: python -m carnival [OPTIONS] {help|test}...
>>> Options:
>>> --server Limit run on server addr
>>> --debug Turn on debug mode
>>> --no_validate Disable step validation
>>> --help Show this message and exit.
Expand All @@ -44,11 +45,24 @@ def main() -> int:
for_completion=is_completion_script(complete_var)
)

from carnival.role import role_repository
discovered_hosts: typing.Set[str] = set()
for _, roles in role_repository.items():
for role in roles:
discovered_hosts.add(role.host.addr)

@click.command()
@click.option('--server', required=False, help="Limit run on server addr",
type=click.Choice(list(discovered_hosts)), multiple=True)
@click.option('--debug', is_flag=True, default=False, help="Turn on debug mode")
@click.option('--no-validate', is_flag=True, default=False, help="Disable step validation")
@click.argument('tasks', required=True, type=click.Choice(list(task_types.keys())), nargs=-1)
def cli(debug: bool, no_validate: bool, tasks: typing.Iterable[str]) -> int:
def cli(
debug: bool,
no_validate: bool,
server: typing.List[str],
tasks: typing.Iterable[str],
) -> int:
colorama.init()

if debug is True:
Expand All @@ -63,7 +77,7 @@ def cli(debug: bool, no_validate: bool, tasks: typing.Iterable[str]) -> int:
has_errors = False
task_chain: typing.List["TaskBase"] = []
for task_class_str in tasks:
task = task_types[task_class_str](no_validate=no_validate)
task = task_types[task_class_str](no_validate=no_validate, servers=server)
is_valid = task.validate()
if is_valid is False:
has_errors = True
Expand Down
2 changes: 1 addition & 1 deletion carnival/internal_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def run(self) -> None:
task_list.sort()

for task_name in task_list:
task_types[task_name](no_validate=False).validate()
task_types[task_name](no_validate=False, servers=[]).validate()


class Roles(TaskBase):
Expand Down
15 changes: 10 additions & 5 deletions carnival/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ class TaskBase:
Строка помощи при вызове carnival help
"""

def __init__(self, no_validate: bool) -> None:
def __init__(self, no_validate: bool, servers: typing.List[str]) -> None:
self.no_validate = no_validate
self.servers = servers

@classmethod
def get_name(cls) -> str:
Expand Down Expand Up @@ -138,11 +139,15 @@ class Task(abc.ABC, typing.Generic[RoleT], TaskBase):

role: RoleT

def __init__(self, no_validate: bool) -> None:
super().__init__(no_validate=no_validate)
def __init__(self, no_validate: bool, servers: typing.List[str]) -> None:
super().__init__(no_validate=no_validate, servers=servers)
# Get role from generic
self.role_class: typing.Type[RoleT] = typing.get_args(self.__class__.__orig_bases__[0])[0] # type: ignore
self.hostroles: typing.List[RoleT] = self.role_class.resolve()

if self.servers:
self.hostroles = [x for x in self.hostroles if x.host.addr in self.servers]

if not self.hostroles:
print(f"[WARN]: not hosts for {self.role_class}", file=sys.stderr)

Expand Down Expand Up @@ -223,13 +228,13 @@ def get_validation_errors(self) -> typing.List[str]:
errors.append(f"{self.__class__.__name__} 'tasks' cannot be empty")

for task_class in self.tasks:
task = task_class(no_validate=self.no_validate)
task = task_class(no_validate=self.no_validate, servers=self.servers)
for error in task.get_validation_errors():
errors.append(f"{task_class.__name__} -> {error}")

return errors

def run(self) -> None:
for task_class in self.tasks:
task = task_class(no_validate=self.no_validate)
task = task_class(no_validate=self.no_validate, servers=self.servers)
task.run()
6 changes: 3 additions & 3 deletions tests/test_internal_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


def test_help(capsys):
internal_tasks.Help(True).run()
internal_tasks.Validate(False).run()
internal_tasks.Roles(True).run()
internal_tasks.Help(True, []).run()
internal_tasks.Validate(False, []).run()
internal_tasks.Roles(True, []).run()
8 changes: 4 additions & 4 deletions tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def run(self) -> None:
def get_validation_errors(self) -> typing.List[str]:
return []

t = DryTask(True)
t = DryTask(True, [])
assert t.get_name() == "dry_task"

class DryNameTask(TaskBase):
Expand All @@ -32,15 +32,15 @@ def get_validation_errors(self) -> typing.List[str]:
def run(self) -> None:
pass

t = DryNameTask(True)
t = DryNameTask(True, [])
assert t.get_name() == "nametask"


def test_task(noop_step, mocker):
spy = mocker.spy(noop_step, 'run')

with pytest.raises(NotImplementedError):
TaskBase(False).run() # type: ignore
TaskBase(False, None).run() # type: ignore

spy.assert_not_called()

Expand All @@ -59,7 +59,7 @@ class TestRole(Role):
class DryTask(Task[TestRole]):
def get_steps(self) -> typing.List[Step]:
return [noop_step, ]
t = DryTask(True)
t = DryTask(True, [])

t.run()
spy.assert_called()

0 comments on commit fe65b7e

Please sign in to comment.