예제 #1
0
def env_prod():
    environment = Environment(
        "production",
        basedir=os.path.dirname(__file__) + "/fixture/basic_service",
    )
    environment.load()
    return environment
예제 #2
0
class Deployment(object):

    environment = None

    def __init__(self, env_name, host_name, overrides, host_data, timeout,
                 platform):
        self.env_name = env_name
        self.host_name = host_name
        self.overrides = overrides
        self.host_data = host_data
        self.timeout = timeout
        self.platform = platform

    def load(self):
        from batou.environment import Environment

        self.environment = Environment(self.env_name, self.timeout,
                                       self.platform)
        self.environment.deployment = self
        self.environment.load()
        self.environment.overrides = self.overrides
        for hostname, data in self.host_data.items():
            self.environment.hosts[hostname].data.update(data)
        self.environment.configure()

    def deploy(self, root, predict_only):
        root = self.environment.get_root(root, self.host_name)
        root.component.deploy(predict_only)
예제 #3
0
 def load(self):
     from batou.environment import Environment
     self.environment = Environment(
         self.env_name, self.timeout, self.platform)
     self.environment.deployment = self
     self.environment.load()
     self.environment.overrides = self.overrides
     self.environment.configure()
예제 #4
0
def test_load_should_use_config(sample_service):
    e = Environment("test-with-env-config")
    e.load()
    assert e.service_user == "joe"
    assert e.host_domain == "example.com"
    assert e.branch == "release"

    assert e.hosts["foo1"].service_user == "bob"
예제 #5
0
def test_resolver_overrides(sample_service):
    e = Environment('test-resolver')
    e.load()
    assert {'localhost': '127.0.0.2'} == e._resolve_override
    assert {'localhost': '::2'} == e._resolve_v6_override

    assert '127.0.0.2' == batou.utils.resolve('localhost')
    assert '::2' == batou.utils.resolve_v6('localhost', 0)
예제 #6
0
def test_resolver_overrides(sample_service):
    e = Environment("test-resolver")
    e.load()
    assert {"localhost": "127.0.0.2"} == e._resolve_override
    assert {"localhost": "::2"} == e._resolve_v6_override

    assert "127.0.0.2" == batou.utils.resolve("localhost")
    assert "::2" == batou.utils.resolve_v6("localhost", 0)
예제 #7
0
def test_load_hosts_should_load_single_hosts_section(add_root):
    e = Environment("name")
    config = Config(None)
    config.config.read_string("""
[hosts]
foo = bar
    """)
    e.load_hosts(config)
    add_root.assert_called_once_with("bar", e.hosts["foo"], [], False)
예제 #8
0
def test_load_hosts_should_load_single_hosts_section(add_root):
    e = Environment('name')
    config = Config(None)
    config.config.read_string("""
[hosts]
foo = bar
    """)
    e.load_hosts(config)
    add_root.assert_called_once_with('bar', 'foo', [], False)
예제 #9
0
def test_multiple_components(sample_service):
    e = Environment('test-multiple-components')
    e.load()
    components = dict(
        (host, list(sorted(c.name for c in e.root_dependencies(host=host))))
        for host in sorted(e.hosts.keys()))
    assert components == dict(localhost=['hello1', 'hello2'],
                              otherhost=['hello3', 'hello4'],
                              thishost=['hello5', 'hello6'])
예제 #10
0
def test_normalize_hostname_regression_11156():
    # The issue here was that we used "rstrip" which works on characters,
    # not substrings. Having the domain (example.com) start with an eee
    # causes the hostname to get stripped of it's "eee"s ending in an
    # empty hostname accidentally.
    e = Environment('name')
    e.hosts['eee.example.com'] = host = Mock()
    e.host_domain = 'example.com'
    assert host == e.get_host('eee')
예제 #11
0
def test_load_hosts_should_load_multi_hosts_section(add_root):
    pass
    e = Environment("name")
    config = Config(None)
    config.config.read_string("""
[host:foo]
components = bar
    """)
    e.load_hosts(config)
    add_root.assert_called_once_with("bar", e.hosts['foo'], [], False)
예제 #12
0
def test_host_data_is_passed_to_host_object():
    e = Environment('name')
    config = Config(None)
    config.config.read_string("""
[host:foo]
components = bar
data-alias = baz
    """)
    e.load_hosts(config)
    assert 'baz' == e.hosts['foo'].data['alias']
예제 #13
0
def test_host_data_is_passed_to_host_object():
    e = Environment("name")
    config = Config(None)
    config.config.read_string("""
[host:foo]
components = bar
data-alias = baz
    """)
    e.load_hosts(config)
    assert "baz" == e.hosts["foo"].data["alias"]
예제 #14
0
def test_load_hosts_single_should_use_env_platform(add_root):
    e = Environment('name')
    e.platform = mock.sentinel.platform
    config = Config(None)
    config.config.read_string("""
[hosts]
foo = bar
    """)
    e.load_hosts(config)
    assert e.hosts['foo'].platform == mock.sentinel.platform
예제 #15
0
def test_load_hosts_multi_should_use_env_platform(add_root):
    e = Environment('name')
    e.platform = mock.sentinel.platform
    config = Config(None)
    config.config.read_string("""
[host:foo]
components = bar
ignore = True
    """)
    e.load_hosts(config)
    assert e.hosts['foo'].platform == mock.sentinel.platform
예제 #16
0
def test_load_hosts_multi_should_use_ignore_flag(add_root):
    pass
    e = Environment('name')
    config = Config(None)
    config.config.read_string("""
[host:foo]
components = bar
ignore = True
    """)
    e.load_hosts(config)
    assert e.hosts['foo'].ignore
예제 #17
0
def test_multiple_components(sample_service):
    e = Environment("test-multiple-components")
    e.load()
    components = dict(
        (host, list(sorted(c.name for c in e.root_dependencies(host=host))))
        for host in sorted(e.hosts.keys()))
    assert components == dict(
        localhost=["hello1", "hello2"],
        otherhost=["hello3", "hello4"],
        thishost=["hello5", "hello6"],
    )
예제 #18
0
    def load(self):
        from batou.environment import Environment

        self.environment = Environment(self.env_name, self.timeout,
                                       self.platform)
        self.environment.deployment = self
        self.environment.load()
        self.environment.overrides = self.overrides
        for hostname, data in self.host_data.items():
            self.environment.hosts[hostname].data.update(data)
        self.environment.configure()
예제 #19
0
def test_load_hosts_multi_should_use_host_platform_if_given(add_root):
    pass
    e = Environment('name')
    e.platform = mock.sentinel.platform
    config = Config(None)
    config.config.read_string("""
[host:foo]
components = bar
platform = specific
    """)
    e.load_hosts(config)
    assert e.hosts['foo'].platform == 'specific'
예제 #20
0
def test_load_hosts_should_break_on_duplicate_definition(add_root):
    e = Environment('name')
    config = Config(None)
    config.config.read_string("""
[hosts]
foo = bar
[host:foo]
components = bar
    """)
    e.load_hosts(config)
    assert e.exceptions
    assert 'foo' == e.exceptions[0].hostname
예제 #21
0
def test_load_hosts_should_merge_single_and_multi_definition(add_root):
    e = Environment('name')
    config = Config(None)
    config.config.read_string("""
[hosts]
foo = bar
[host:baz]
components = bar
    """)
    e.load_hosts(config)
    assert [mock.call('bar', 'foo', [], False),
            mock.call('bar', 'baz', [], False)] == \
        add_root.call_args_list
예제 #22
0
def test_load_hosts_should_merge_single_and_multi_definition(add_root):
    e = Environment("name")
    config = Config(None)
    config.config.read_string("""
[hosts]
foo = bar
[host:baz]
components = bar
    """)
    e.load_hosts(config)
    assert [
        mock.call("bar", e.hosts["foo"], [], False),
        mock.call("bar", e.hosts["baz"], [], False),
    ] == add_root.call_args_list
예제 #23
0
def test_components_for_host_can_be_retrieved_from_environment(sample_service):
    e = Environment('test-overlapping-components')
    e.load()

    def _get_components(hostname):
        return sorted(e.components_for(host=e.hosts[hostname]).keys())

    assert ['hello1', 'hello2'] == _get_components('localhost')
    assert ['hello3'] == _get_components('other')
    assert ['hello2', 'hello3'] == _get_components('this')

    localhost = e.hosts['localhost']
    assert 'hello1' in localhost.components
    assert 'hello2' in localhost.components
    assert 'hello3' not in localhost.components
예제 #24
0
def test_components_for_host_can_be_retrieved_from_environment(sample_service):
    e = Environment("test-overlapping-components")
    e.load()

    def _get_components(hostname):
        return sorted(e.components_for(host=e.hosts[hostname]).keys())

    assert ["hello1", "hello2"] == _get_components("localhost")
    assert ["hello3"] == _get_components("other")
    assert ["hello2", "hello3"] == _get_components("this")

    localhost = e.hosts["localhost"]
    assert "hello1" in localhost.components
    assert "hello2" in localhost.components
    assert "hello3" not in localhost.components
예제 #25
0
def root(tmpdir):
    environment = Environment("test", basedir=str(tmpdir))
    environment._set_defaults()
    os.chdir(str(tmpdir))

    class MyComponent(Component):
        pass

    compdef = ComponentDefinition(MyComponent)
    compdef.defdir = str(tmpdir)
    environment.components[compdef.name] = compdef
    root = environment.add_root(compdef.name, "host")
    root.prepare()
    root.component.deploy()
    return root
예제 #26
0
def test_log_in_component_configure_is_put_out(output, sample_service):
    e = Environment("test-with-provide-require")
    e.load()
    e.configure()
    log = "\n".join(c[0][0].strip() for c in output.call_args_list)
    # Provide is *always* logged first, due to provide/require ordering.
    assert (
        """\
Provide
Pre sub
Sub!
Post sub"""
        == log
    )

    output.reset_mock()
    for root in e.root_dependencies():
        root.component.deploy(True)

    log = "\n".join(c[0][0].strip() for c in output.call_args_list)
    assert (
        """\
localhost > Hello
localhost: <Hello (localhost) "Hello"> verify: asdf=None\
"""
        == log
    )
예제 #27
0
def env():
    env = Environment("test")
    for component in list(globals().values()):
        if not isinstance(component, type):
            continue
        if issubclass(component, Component):
            compdef = ComponentDefinition(component)
            env.components[compdef.name] = compdef
    return env
예제 #28
0
def test_load_ignores_predefined_environment_settings(sample_service):
    e = Environment('test-with-env-config')
    e.service_user = '******'
    e.host_domain = 'sample.com'
    e.branch = 'default'
    e.load()
    assert e.service_user == 'bob'
    assert e.host_domain == 'sample.com'
    assert e.branch == 'default'
예제 #29
0
def test_load_ignores_predefined_environment_settings(sample_service):
    e = Environment("test-with-env-config")
    e.service_user = "******"
    e.host_domain = "sample.com"
    e.branch = "default"
    e.load()
    assert e.service_user == "bob"
    assert e.host_domain == "sample.com"
    assert e.branch == "default"
예제 #30
0
def test_service_early_resource():
    env = Environment('dev',
                      basedir=os.path.dirname(__file__) +
                      '/fixture/service_early_resource')
    env.load()
    env.configure()
    assert env.resources.get('zeo') == ['127.0.0.1:9000']