def step(self, steps: Union[Step, List[Step]], hosts: Union[Host, List[Host]]) -> List[TaskResult]: """ Запустить шаг(и) на хост(ах) Возвращает объект TaskResult для получения результатов работы каждого шага на каждом хосте """ if not isinstance(steps, list) and not isinstance(steps, tuple): steps = [steps, ] if not isinstance(hosts, list) and not isinstance(hosts, tuple): hosts = [hosts, ] results = [] for host in hosts: with global_context.SetContext(host): for step in steps: step_name = _underscore(step.__class__.__name__) print(f"💃💃💃 Running {self.get_name()}:{step_name} at {host}") if not self.dry_run: r = TaskResult( host=host, step=step, result=step.run_with_context(host=host), ) results.append(r) return results
def test_install_ce_ubuntu(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): assert cmd.fs.is_file_exists("/usr/bin/docker") is False cmd.docker.install_ce_ubuntu() assert cmd.fs.is_file_exists("/usr/bin/docker") is True cmd.apt.remove("docker-ce")
def test_rsync(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): cmd.system.ssh_copy_id() assert cmd.fs.is_dir_exists("/docs") is False cmd.transfer.rsync("./docs", "/") assert cmd.fs.is_dir_exists("/docs") is True cmd.cli.run("rm -rf /docs")
def test_apt_install(suspend_capture, ubuntu_ssh_host): with suspend_capture: with global_context.SetContext(ubuntu_ssh_host): assert cmd.fs.is_file_exists("/usr/bin/mc") is False assert cmd.apt.install("mc", hide=True) is True assert cmd.apt.install("mc", hide=True) is False assert cmd.fs.is_file_exists("/usr/bin/mc") is True cmd.apt.remove("mc", hide=True)
def test_pty(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): result = cmd.cli.pty("ls -1 / | grep bin", hide=True) assert result.ok is True # Check response looks like root fs, filtered 'bin' root_files = result.stdout.split("\n") assert 'bin\r' in root_files assert 'sbin\r' in root_files
def test_install_compose(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): assert cmd.fs.is_file_exists( "/usr/local/bin/docker-compose") is False cmd.docker.install_compose() assert cmd.fs.is_file_exists( "/usr/local/bin/docker-compose") is True cmd.cli.run("rm /usr/local/bin/docker-compose")
def test_put_template(suspend_capture, host, mocker): with suspend_capture: with global_context.SetContext(host): mocker.patch( 'carnival.templates.j2_env', new=Environment( loader=DictLoader({"index.html": "Hello: {{ name }}"})), ) assert cmd.fs.is_file_exists("/index") is False cmd.transfer.put_template("index.html", "/index") assert cmd.fs.is_file_exists("/index") is True cmd.cli.run("rm /index")
def test_global_context(): h = Host("local") assert global_context.host is None assert global_context.conn is None with global_context.SetContext(h): assert global_context.host == h assert global_context.conn is not None assert isinstance(global_context.conn, Context) assert global_context.host is None assert global_context.conn is None
def test_mkdirs(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): assert cmd.fs.is_dir_exists("/tmp/.carnivaltestdir1") is False assert cmd.fs.is_dir_exists("/tmp/.carnivaltestdir2") is False cmd.fs.mkdirs("/tmp/.carnivaltestdir1", "/tmp/.carnivaltestdir2") assert cmd.fs.is_dir_exists("/tmp/.carnivaltestdir1") is True assert cmd.fs.is_dir_exists("/tmp/.carnivaltestdir2") is True cmd.cli.run(f"rm -rf /tmp/.carnivaltestdir1 /tmp/.carnivaltestdir2")
def test_run(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): result = cmd.cli.run("ls -1 /", hide=True) assert result.ok is True # Check response looks like root fs root_files = result.stdout.split("\n") assert 'bin' in root_files assert 'etc' in root_files assert 'usr' in root_files assert 'tmp' in root_files assert 'sbin' in root_files
def test_apt_get_installed_version(suspend_capture, ubuntu_ssh_host): with suspend_capture: with global_context.SetContext(ubuntu_ssh_host): assert cmd.fs.is_file_exists("/usr/bin/mc") is False assert cmd.apt.get_installed_version("mc") is None cmd.apt.install("mc", hide=True) assert cmd.fs.is_file_exists("/usr/bin/mc") is True ver = cmd.apt.get_installed_version("mc") assert ver in cmd.apt.get_pkg_versions("mc") assert isinstance(ver, str) assert " " not in ver cmd.apt.remove("mc", hide=True)
def test_apt_is_pkg_installed(suspend_capture, ubuntu_ssh_host): with suspend_capture: with global_context.SetContext(ubuntu_ssh_host): assert cmd.fs.is_file_exists("/usr/bin/mc") is False assert cmd.apt.is_pkg_installed("mc") is False cmd.apt.install("mc", hide=True) assert cmd.fs.is_file_exists("/usr/bin/mc") is True assert cmd.apt.is_pkg_installed("mc") is True ver = cmd.apt.get_installed_version("mc") assert cmd.apt.is_pkg_installed("mc", ver) is True assert cmd.apt.is_pkg_installed("mc", "3i2oxyom3x3 ") is False cmd.apt.remove("mc", hide=True)
def test_ssh_authorized_keys_ensure(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): assert cmd.system.ssh_authorized_keys_list() == [] cmd.system.ssh_authorized_keys_ensure(KEY) keys = cmd.system.ssh_authorized_keys_list() assert len(keys) == 1 assert KEY in keys # Check not added second time cmd.system.ssh_authorized_keys_ensure(KEY) keys = cmd.system.ssh_authorized_keys_list() assert len(keys) == 1 assert KEY in keys cmd.cli.run(f"rm ~/.ssh/authorized_keys", hide=True)
def test_ssh_authorized_keys_list(suspend_capture, ubuntu_ssh_host, centos_ssh_host): for host in [ubuntu_ssh_host, centos_ssh_host]: with suspend_capture: with global_context.SetContext(host): assert cmd.system.ssh_authorized_keys_list() == [] cmd.system.ssh_authorized_keys_add(KEY) keys = cmd.system.ssh_authorized_keys_list() assert len(keys) == 1 assert KEY in keys # Check not added second time cmd.system.ssh_authorized_keys_add(KEY) keys = cmd.system.ssh_authorized_keys_list() assert len(keys) == 1 assert KEY in keys cmd.cli.run("rm ~/.ssh/authorized_keys", hide=True)
def test_stop(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): cmd.systemd.stop("nginx", reload_daemon=True)
def test_is_dir_exists(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): assert cmd.fs.is_dir_exists("/etc") assert cmd.fs.is_dir_exists("/bin")
def test_log(capsys): with global_context.SetContext(Host("local")): utils.log("Hellotest") captured = capsys.readouterr() assert captured.out == "💃💃💃 local> Hellotest\n"
def test_restart(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): cmd.systemd.restart("nginx")
def test_disable(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): cmd.systemd.disable("nginx", reload_daemon=True, stop_now=True)
def test_daemon_reload(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): cmd.systemd.daemon_reload()
def test_get_current_user_id(suspend_capture, ubuntu_ssh_host, centos_ssh_host): for host in [ubuntu_ssh_host, centos_ssh_host]: with suspend_capture: with global_context.SetContext(host): assert cmd.system.get_current_user_id() == 0
def test_is_current_user_root(suspend_capture, ubuntu_ssh_host, centos_ssh_host): for host in [ubuntu_ssh_host, centos_ssh_host]: with suspend_capture: with global_context.SetContext(host): assert cmd.system.is_current_user_root() is True
def test_restart(suspend_capture, ubuntu_ssh_host, centos_ssh_host): for host in [ubuntu_ssh_host, centos_ssh_host]: with suspend_capture: with global_context.SetContext(host): cmd.systemd.restart("nginx")
def test_is_current_user_root(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): assert cmd.system.is_current_user_root() is True
def test_get_current_user_id(suspend_capture, host): with suspend_capture: with global_context.SetContext(host): assert cmd.system.get_current_user_id() == 0
def test_daemon_reload(suspend_capture, ubuntu_ssh_host, centos_ssh_host): for host in [ubuntu_ssh_host, centos_ssh_host]: with suspend_capture: with global_context.SetContext(host): cmd.systemd.daemon_reload()
def test_disable(suspend_capture, ubuntu_ssh_host, centos_ssh_host): for host in [ubuntu_ssh_host, centos_ssh_host]: with suspend_capture: with global_context.SetContext(host): cmd.systemd.disable("nginx", reload_daemon=True, stop_now=True)