How to use the bundlewrap.items.Item function in bundlewrap

To help you get started, we’ve selected a few bundlewrap examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bundlewrap / bundlewrap / tests_legacy / unit / items / item_tests.py View on Github external
def test_interactive_abort(self, ask_interactively):
        status_before = MagicMock()
        status_before.correct = False
        status_before.fixable = True
        status_before.skipped = False
        item = MockItem(MagicMock(), "item1", {}, skip_validation=True)
        item.get_status = MagicMock(return_value=status_before)
        item.ask = MagicMock(return_value="?")
        item.fix = MagicMock()
        result = item.apply(interactive=True)
        self.assertFalse(item.fix.called)
        assert ask_interactively.call_count == 1
        self.assertEqual(result, Item.STATUS_SKIPPED)
github bundlewrap / bundlewrap / tests_legacy / unit / node_tests.py View on Github external
class MockBundle(object):
    name = "mock"
    bundle_dir = ""
    bundle_data_dir = ""
    items = []


class MockItem(Item):
    BUNDLE_ATTRIBUTE_NAME = "mock"
    ITEM_TYPE_NAME = "type1"
    NEEDS_STATIC = []
    _APPLY_RESULT = Item.STATUS_OK

    def apply(self, *args, **kwargs):
        return self._APPLY_RESULT
del Item.__reduce__  # we don't need the custom pickle-magic for our
                     # MockItems


def get_mock_item(itype, name, deps_static, deps):
    bundle = MockBundle()
    bundle.node = MockNode()
    item = MockItem(bundle, name, {'needs': deps}, skip_validation=True)
    item.ITEM_TYPE_NAME = itype
    item.NEEDS_STATIC = deps_static
    return item


class ApplyItemsTest(TestCase):
    """
    Tests bundlewrap.node.apply_items.
    """
github bundlewrap / bundlewrap / tests_legacy / unit / node_tests.py View on Github external
from bundlewrap.repo import Repository
from bundlewrap.utils import names


class MockNode(object):
    name = "mocknode"


class MockBundle(object):
    name = "mock"
    bundle_dir = ""
    bundle_data_dir = ""
    items = []


class MockItem(Item):
    BUNDLE_ATTRIBUTE_NAME = "mock"
    ITEM_TYPE_NAME = "type1"
    NEEDS_STATIC = []
    _APPLY_RESULT = Item.STATUS_OK

    def apply(self, *args, **kwargs):
        return self._APPLY_RESULT
del Item.__reduce__  # we don't need the custom pickle-magic for our
                     # MockItems


def get_mock_item(itype, name, deps_static, deps):
    bundle = MockBundle()
    bundle.node = MockNode()
    item = MockItem(bundle, name, {'needs': deps}, skip_validation=True)
    item.ITEM_TYPE_NAME = itype
github bundlewrap / bundlewrap / tests_legacy / unit / node_tests.py View on Github external
def test_fixed(self):
        item_results = (
            ("item_id", Item.STATUS_FIXED, None),
        )
        output_result = ApplyResult(MagicMock(), item_results)
        self.assertEqual(output_result.correct, 0)
        self.assertEqual(output_result.fixed, 1)
        self.assertEqual(output_result.skipped, 0)
        self.assertEqual(output_result.failed, 0)
github bundlewrap / bundlewrap / tests_legacy / unit / deps_tests.py View on Github external
from unittest import TestCase

try:
    from unittest.mock import MagicMock
except ImportError:
    from mock import MagicMock

from bundlewrap import deps
from bundlewrap.exceptions import BundleError
from bundlewrap.items import Item

from .node_tests import get_mock_item


class MockItem(Item):
    BUNDLE_ATTRIBUTE_NAME = "mock"
    ITEM_TYPE_NAME = "mock"
    NEEDS_STATIC = []

    def get_canned_actions(self):
        return {
            'action1': {
                'command': "true",
            },
        }


class FlattenDependenciesTest(TestCase):
    """
    Tests bundlewrap.deps._flatten_dependencies.
    """
github bundlewrap / bundlewrap / bundlewrap / items / groups.py View on Github external
def _parse_group_line(line):
    """
    Parses a line from /etc/group and returns the information as a
    dictionary.
    """
    result = dict(zip(
        ('groupname', 'password', 'gid', 'members'),
        line.strip().split(":"),
    ))
    result['gid'] = result['gid']
    del result['password']  # nothing useful here
    return result


class Group(Item):
    """
    A group.
    """
    BUNDLE_ATTRIBUTE_NAME = "groups"
    ITEM_ATTRIBUTES = {
        'delete': False,
        'gid': None,
    }
    ITEM_TYPE_NAME = "group"
    REQUIRED_ATTRIBUTES = []

    @classmethod
    def block_concurrent(cls, node_os, node_os_version):
        # https://github.com/bundlewrap/bundlewrap/issues/367
        if node_os == 'openbsd':
            return [cls.ITEM_TYPE_NAME]
github bundlewrap / bundlewrap / bundlewrap / repo.py View on Github external
item classes globally.
        """
        if not isdir(path):
            return
        for root_dir, _dirs, files in walk(path):
            for filename in files:
                filepath = join(root_dir, filename)
                if not filename.endswith(".py") or \
                        not isfile(filepath) or \
                        filename.startswith("_"):
                    continue
                for name, obj in self.get_all_attrs_from_file(filepath).items():
                    if obj == items.Item or name.startswith("_"):
                        continue
                    try:
                        if issubclass(obj, items.Item) and not isabstract(obj):
                            yield obj
                    except TypeError:
                        pass
github bundlewrap / bundlewrap / bundlewrap / items / files.py View on Github external
def validator_content_type(item_id, value):
    if value not in CONTENT_PROCESSORS:
        raise BundleError(_(
            "invalid content_type for {item}: '{value}'"
        ).format(item=item_id, value=value))


ATTRIBUTE_VALIDATORS = defaultdict(lambda: lambda id, value: None)
ATTRIBUTE_VALIDATORS.update({
    'content_type': validator_content_type,
    'mode': validator_mode,
})


class File(Item):
    """
    A file.
    """
    BUNDLE_ATTRIBUTE_NAME = "files"
    ITEM_ATTRIBUTES = {
        'content': None,
        'content_type': 'text',
        'context': None,
        'delete': False,
        'encoding': "utf-8",
        'group': "root",
        'mode': "0644",
        'owner': "root",
        'source': None,
        'verify_with': None,
    }
github bundlewrap / bundlewrap / bundlewrap / items / svc_openbsd.py View on Github external
return node.run("rcctl set {} status on".format(quote(svcname)), may_fail=True)


def svc_enabled(node, svcname):
    result = node.run(
        "rcctl ls on | grep '^{}$'".format(svcname),
        may_fail=True,
    )
    return result.return_code == 0


def svc_disable(node, svcname):
    return node.run("rcctl set {} status off".format(quote(svcname)), may_fail=True)


class SvcOpenBSD(Item):
    """
    A service managed by OpenBSD rc.d.
    """
    BUNDLE_ATTRIBUTE_NAME = "svc_openbsd"
    ITEM_ATTRIBUTES = {
        'running': True,
        'enabled': True
    }
    ITEM_TYPE_NAME = "svc_openbsd"

    def __repr__(self):
        return "".format(
            self.name,
            self.attributes['running'],
            self.attributes['enabled'],
        )
github bundlewrap / bundlewrap / bundlewrap / items / git_deploy.py View on Github external
stdout=PIPE,
    )
    stdout, stderr = git_process.communicate()
    # FIXME integrate this into Item._command_results
    if git_process.returncode != 0:
        io.stderr(_("failed command: {}").format(" ".join(cmdline)))
        io.stderr(_("stdout:\n{}").format(stdout))
        io.stderr(_("stderr:\n{}").format(stderr))
        raise RuntimeError(_("`git {command}` failed in {dir}").format(
            command=cmdline[1],
            dir=repo_dir,
        ))
    return stdout.decode('utf-8').strip()


class GitDeploy(Item):
    """
    Facilitates deployment of a given rev from a local git repo to a
    node.
    """
    BUNDLE_ATTRIBUTE_NAME = "git_deploy"
    ITEM_ATTRIBUTES = {
        'repo': None,
        'rev': None,
        'use_xattrs': False,
    }
    ITEM_TYPE_NAME = "git_deploy"
    REQUIRED_ATTRIBUTES = ['repo', 'rev']

    def __repr__(self):
        return "".format(
            self.name,