def infra(conf: Configuration): 'Provision and remove a `conf` infrastructure from Vagrant.' # Get the Vagrant provider vagrant_provider = Enos_vagrant(conf) # Setup the infra LOG.info("Provisioning machines...") roles, networks = vagrant_provider.init() # Extract the list of Hosts from Roles hosts = set([h for hs in roles.values() for h in hs]) LOG.info("Provisioning finished") try: # Let the user does it stuff # yield: Tuple[List[Host], Roles, List[Network]] yield hosts, roles, networks LOG.info('You can SSH on Hosts from another terminal with:') for h in hosts: LOG.info(f'- vagrant ssh {h.alias}') input('Press Enter to finish...') except Exception as e: LOG.error(f'Unexpected error: {e}') finally: # Tear down the infra LOG.info('Finished!') LOG.info('Destroying machines...') vagrant_provider.destroy() tmp_enoslib_path = '_tmp_enos_' LOG.info(f'Removing cache {tmp_enoslib_path}...') if os.path.isdir(tmp_enoslib_path): shutil.rmtree(tmp_enoslib_path)
from enoslib.infra.enos_vagrant.provider import Enos_vagrant from enoslib.infra.enos_vagrant.configuration import Configuration import logging import os logging.basicConfig(level=logging.INFO) conf = Configuration()\ .add_machine(roles=["control"], flavour="tiny", number=1)\ .add_machine(roles=["control", "compute"], flavour="tiny", number=1)\ .add_network(roles=["mynetwork"], cidr="192.168.42.0/24")\ .finalize() # claim the resources provider = Enos_vagrant(conf) roles, networks = provider.init() # generate an inventory compatible with ansible discover_networks(roles, networks) print(roles) # destroy the boxes provider.destroy()