Exemplo n.º 1
0
def make_inventory(hosts=('somehost', 'anotherhost'), **kwargs):
    return Inventory(
        (hosts, {}),
        test_group=([
            'somehost',
        ], {
            'group_data': 'hello world',
        }),
        ssh_user='******',
        **kwargs
    )
Exemplo n.º 2
0
    def test_create_inventory_host_data(self):
        default_data = {"default_data": "default_data", "host_data": "none"}
        somehost_data = {"host_data": "host_data"}

        inventory = Inventory((
            [("somehost", somehost_data), "anotherhost"],
            default_data,
        ), )

        assert inventory.get_data() == default_data
        assert inventory.get_host_data("somehost") == somehost_data
        assert inventory.get_host_data("anotherhost") == {}
        assert inventory.get_host("anotherhost").data.host_data == "none"
Exemplo n.º 3
0
def make_inventory(hosts=("somehost", "anotherhost"), **kwargs):
    override_data = kwargs.pop("override_data", {})
    if "ssh_user" not in override_data:
        override_data["ssh_user"] = "******"

    return Inventory(
        (hosts, {}),
        override_data=override_data,
        test_group=(
            [
                "somehost",
            ],
            {
                "group_data": "hello world",
            },
        ),
        **kwargs,
    )
Exemplo n.º 4
0
    def test_get_from_kwargs(self):
        config = Config(SUDO="config-value")
        inventory = Inventory(([("somehost", {"sudo": "host-value"})], {}))
        somehost = inventory.get_host("somehost")

        state = State(config=config, inventory=inventory)
        somehost.current_deploy_kwargs = {
            "sudo": "deploy-kwarg-value",
            "sudo_user": "******",
        }

        kwargs, keys = pop_global_arguments(
            {"sudo": "kwarg-value"},
            state=state,
            host=somehost,
        )
        assert kwargs["sudo"] == "kwarg-value"
        assert kwargs["sudo_user"] == "deploy-kwarg-user"
        assert "sudo" in keys
Exemplo n.º 5
0
 def __init__(self):
     self.inventory = Inventory(([], {}))
     self.config = Config()
Exemplo n.º 6
0
def make_inventory(inventory_filename,
                   deploy_dir=None,
                   limit=None,
                   ssh_user=None,
                   ssh_key=None,
                   ssh_key_password=None,
                   ssh_port=None,
                   ssh_password=None):
    '''
    Builds a ``pyinfra.api.Inventory`` from the filesystem. If the file does not exist
    and doesn't contain a / attempts to use that as the only hostname.
    '''

    if ssh_port is not None:
        ssh_port = int(ssh_port)

    file_groupname = None

    try:
        attrs = exec_file(inventory_filename, return_locals=True)

        groups = {
            key: value
            for key, value in six.iteritems(attrs) if key.isupper()
        }

        # Used to set all the hosts to an additional group - that of the filename
        # ie inventories/dev.py means all the hosts are in the dev group, if not present
        file_groupname = path.basename(inventory_filename).split(
            '.')[0].upper()

    except IOError as e:
        # If a /, definitely not a hostname
        if '/' in inventory_filename:
            raise CliError('{0}: {1}'.format(e.strerror, inventory_filename))

        # Otherwise we assume the inventory is actually a hostname or list of hostnames
        groups = {'ALL': inventory_filename.split(',')}

    all_data = {}

    if 'ALL' in groups:
        all_hosts = groups.pop('ALL')

        if isinstance(all_hosts, tuple):
            all_hosts, all_data = all_hosts

    # Build ALL out of the existing hosts if not defined
    else:
        all_hosts = []
        for hosts in groups.values():
            # Groups can be a list of hosts or tuple of (hosts, data)
            hosts = hosts[0] if isinstance(hosts, tuple) else hosts

            for host in hosts:
                # Hosts can be a hostname or tuple of (hostname, data)
                hostname = host[0] if isinstance(host, tuple) else host

                if hostname not in all_hosts:
                    all_hosts.append(hostname)

    groups['ALL'] = (all_hosts, all_data)

    # Apply the filename group if not already defined
    if file_groupname and file_groupname not in groups:
        groups[file_groupname] = all_hosts

    # In pyinfra an inventory is a combination of (hostnames + data). However, in CLI
    # mode we want to be define this in separate files (inventory / group data). The
    # issue is we want inventory access within the group data files - but at this point
    # we're not ready to make an Inventory. So here we just create a fake one, and attach
    # it to pseudo_inventory while we import the data files.
    fake_groups = {
        # In API mode groups *must* be tuples of (hostnames, data)
        name: group if isinstance(group, tuple) else (group, {})
        for name, group in six.iteritems(groups)
    }
    fake_inventory = Inventory((all_hosts, all_data), **fake_groups)
    pseudo_inventory.set(fake_inventory)

    # For each group load up any data
    for name, hosts in six.iteritems(groups):
        data = {}

        if isinstance(hosts, tuple):
            hosts, data = hosts

        data_filename = path.join(deploy_dir, 'group_data',
                                  '{0}.py'.format(name.lower()))
        logger.debug('Looking for group data: {0}'.format(data_filename))

        if path.exists(data_filename):
            # Read the files locals into a dict
            attrs = exec_file(data_filename, return_locals=True)

            data.update({
                key: value
                for key, value in six.iteritems(attrs)
                if isinstance(value, ALLOWED_DATA_TYPES)
                and not key.startswith('_') and key.islower()
            })

        # Attach to group object
        groups[name] = (hosts, data)

    # Reset the pseudo inventory
    pseudo_inventory.reset()

    # Apply any limit to all_hosts
    if limit:
        # Limits can be groups
        limit_groupname = limit.upper()
        if limit_groupname in groups:
            all_hosts = [
                host[0] if isinstance(host, tuple) else host
                for host in groups[limit_groupname][0]
            ]

        # Or hostnames w/*wildcards
        else:
            all_hosts = [
                host for host in all_hosts
                if (isinstance(host, tuple) and fnmatch(host[0], limit)) or
                (isinstance(host, six.string_types) and fnmatch(host, limit))
            ]

        # Reassign the ALL group w/limit
        groups['ALL'] = (all_hosts, all_data)

    return Inventory(groups.pop('ALL'),
                     ssh_user=ssh_user,
                     ssh_key=ssh_key,
                     ssh_key_password=ssh_key_password,
                     ssh_port=ssh_port,
                     ssh_password=ssh_password,
                     **groups), file_groupname and file_groupname.lower()
Exemplo n.º 7
0
logging.basicConfig(level=logging.CRITICAL)

# Make our hosts and groups data (using the Vagrant connector in this case)
print("Loading Vagrant config...")
hosts = []
groups = defaultdict(lambda: ([], {}))

for name, data, group_names in make_names_data():
    hosts.append((name, data))
    for group_name in group_names:
        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)
Exemplo n.º 8
0
logging.basicConfig(level=logging.WARNING)
logging.getLogger('pyinfra').setLevel(logging.INFO)

# 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(
    (
        [
            'centos6.pyinfra',
            # Host-specific data can be attached in inventory
            ('centos7.pyinfra', {
                'systemd': True
            }),
            'ubuntu14.pyinfra',
            'debian7.pyinfra',
            'openbsd58.pyinfra'
        ],
        {}),
    bsd=(
        ['openbsd57.pyinfra'],
        {
            # Group-specific data can be attached like so
            'app_dir': '/opt/pyinfra/bsd'
        }),
    centos=(['centos6.pyinfra', 'centos7.pyinfra'], {}),
    ssh_user='******',
    ssh_key='./files/insecure_private_key')

# Now we create a new config (w/optional args)
config = Config(FAIL_PERCENT=50, TIMEOUT=1)

# Setup the pyinfra state for this deploy
Exemplo n.º 9
0
    def test_context_inventory_iter(self):
        inventory_obj = Inventory(("host", "anotherhost"))
        ctx_inventory.set(inventory_obj)
        assert ctx_inventory.isset() is True

        assert list(iter(inventory)) == list(iter(inventory_obj))
Exemplo n.º 10
0
    def test_context_inventory_len(self):
        inventory_obj = Inventory(("host", "anotherhost"))
        ctx_inventory.set(inventory_obj)
        assert ctx_inventory.isset() is True

        assert len(inventory) == len(inventory_obj)
Exemplo n.º 11
0
    def test_pseudo_inventory_iter(self):
        inventory = Inventory(('host', 'anotherhost'))
        pseudo_inventory.set(inventory)
        assert pseudo_inventory.isset() is True

        assert list(iter(pseudo_inventory)) == list(iter(inventory))
Exemplo n.º 12
0
    def test_pseudo_inventory_len(self):
        inventory = Inventory(('host', 'anotherhost'))
        pseudo_inventory.set(inventory)
        assert pseudo_inventory.isset() is True

        assert len(pseudo_inventory) == len(inventory)
Exemplo n.º 13
0
 def test_create_inventory(self):
     inventory = Inventory((["somehost", "anotherhost",
                             "anotherotherhost"], {}))
     assert len(inventory) == 3
Exemplo n.º 14
0
 def test_create_inventory_data(self):
     default_data = {"default_data": "default_data"}
     inventory = Inventory((["somehost"], default_data))
     assert inventory.get_data() == default_data
     assert inventory.get_host_data("somehost") == {}