def test_op(self): inventory = make_inventory() somehost = inventory.get_host('somehost') anotherhost = inventory.get_host('anotherhost') state = State(inventory, Config()) state.add_callback_handler(BaseStateCallback()) # Enable printing on this test to catch any exceptions in the formatting state.print_output = True state.print_input = True state.print_fact_info = True state.print_noop_info = True connect_all(state) add_op( state, files.file, '/var/log/pyinfra.log', user='******', group='pyinfra', mode='644', create_remote_dir=False, sudo=True, sudo_user='******', su_user='******', ignore_errors=True, env={ 'TEST': 'what', }, ) op_order = state.get_op_order() # Ensure we have an op assert len(op_order) == 1 first_op_hash = op_order[0] # Ensure the op name assert state.op_meta[first_op_hash]['names'] == {'Files/File'} # Ensure the commands assert state.ops[somehost][first_op_hash]['commands'] == [ StringCommand('touch /var/log/pyinfra.log'), StringCommand('chmod 644 /var/log/pyinfra.log'), StringCommand('chown pyinfra:pyinfra /var/log/pyinfra.log'), ] # Ensure the global kwargs (same for both hosts) somehost_global_kwargs = state.ops[somehost][first_op_hash]['global_kwargs'] assert somehost_global_kwargs['sudo'] is True assert somehost_global_kwargs['sudo_user'] == 'test_sudo' assert somehost_global_kwargs['su_user'] == 'test_su' assert somehost_global_kwargs['ignore_errors'] is True anotherhost_global_kwargs = state.ops[anotherhost][first_op_hash]['global_kwargs'] assert anotherhost_global_kwargs['sudo'] is True assert anotherhost_global_kwargs['sudo_user'] == 'test_sudo' assert anotherhost_global_kwargs['su_user'] == 'test_su' assert anotherhost_global_kwargs['ignore_errors'] is True # Ensure run ops works run_ops(state) # Ensure ops completed OK assert state.results[somehost]['success_ops'] == 1 assert state.results[somehost]['ops'] == 1 assert state.results[anotherhost]['success_ops'] == 1 assert state.results[anotherhost]['ops'] == 1 # And w/o errors assert state.results[somehost]['error_ops'] == 0 assert state.results[anotherhost]['error_ops'] == 0 # And with the different modes run_ops(state, serial=True) run_ops(state, no_wait=True) disconnect_all(state)
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) state.add_callback_handler(StateCallback()) # Connect to all the hosts print("Connecting...") connect_all(state) # Start adding operations print("Generating operations...") add_op( state, server.user, user="******", home="/home/pyinfra", shell="/bin/bash", sudo=True, )
def test_op(self): inventory = make_inventory() somehost = inventory.get_host("somehost") anotherhost = inventory.get_host("anotherhost") state = State(inventory, Config()) state.add_callback_handler(BaseStateCallback()) # Enable printing on this test to catch any exceptions in the formatting state.print_output = True state.print_input = True state.print_fact_info = True state.print_noop_info = True connect_all(state) add_op( state, files.file, "/var/log/pyinfra.log", user="******", group="pyinfra", mode="644", create_remote_dir=False, sudo=True, sudo_user="******", su_user="******", ignore_errors=True, env={ "TEST": "what", }, ) op_order = state.get_op_order() # Ensure we have an op assert len(op_order) == 1 first_op_hash = op_order[0] # Ensure the op name assert state.op_meta[first_op_hash]["names"] == {"Files/File"} # Ensure the commands assert state.ops[somehost][first_op_hash]["commands"] == [ StringCommand("touch /var/log/pyinfra.log"), StringCommand("chmod 644 /var/log/pyinfra.log"), StringCommand("chown pyinfra:pyinfra /var/log/pyinfra.log"), ] # Ensure the global kwargs (same for both hosts) somehost_global_kwargs = state.ops[somehost][first_op_hash][ "global_kwargs"] assert somehost_global_kwargs["sudo"] is True assert somehost_global_kwargs["sudo_user"] == "test_sudo" assert somehost_global_kwargs["su_user"] == "test_su" assert somehost_global_kwargs["ignore_errors"] is True anotherhost_global_kwargs = state.ops[anotherhost][first_op_hash][ "global_kwargs"] assert anotherhost_global_kwargs["sudo"] is True assert anotherhost_global_kwargs["sudo_user"] == "test_sudo" assert anotherhost_global_kwargs["su_user"] == "test_su" assert anotherhost_global_kwargs["ignore_errors"] is True # Ensure run ops works run_ops(state) # Ensure ops completed OK assert state.results[somehost]["success_ops"] == 1 assert state.results[somehost]["ops"] == 1 assert state.results[anotherhost]["success_ops"] == 1 assert state.results[anotherhost]["ops"] == 1 # And w/o errors assert state.results[somehost]["error_ops"] == 0 assert state.results[anotherhost]["error_ops"] == 0 # And with the different modes run_ops(state, serial=True) run_ops(state, no_wait=True) disconnect_all(state)