コード例 #1
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_no_lazy_inspect(method):

    s = Service(name='foo', client=flexmock())
    s._inspected = True
    s._inspect_data = {'State': {'Running': True}}

    getattr(s, method)()
コード例 #2
0
ファイル: mcloud_haproxy.py プロジェクト: pombredanne/mcloud
    def rebuild_haproxy(self, deployments=None, ticket_id=None):

        # generate new haproxy config
        all_deployments = yield self.dump()

        for deployment_name, config in all_deployments.items():

            # rebuild only needed deployments
            if deployments and not deployment_name in deployments:
                continue

            if ticket_id:
                self.rpc_server.task_progress('Updating haproxy config on deployment %s' % deployment_name, ticket_id)

            deployment = yield self.dep_controller.get(deployment_name)

            haproxy_path = os.path.expanduser('%s/haproxy/%s' % (self.settings.home_dir, deployment_name))
            if not os.path.exists(haproxy_path):
                os.makedirs(haproxy_path)

            template_path = os.path.join(haproxy_path, 'haproxy.tpl')
            haproxy_config_path = os.path.join(haproxy_path, 'haproxy.cfg')

            if not os.path.exists(template_path):
                with open(template_path, 'w+') as f:
                    f.write(HAPROXY_TPL)

            with open(template_path) as f:
                template = Template(f.read())

            config_rendered = template.render(config)

            with open(haproxy_config_path, 'w+') as f:
                f.write(config_rendered)

            haproxy = Service(client=deployment.get_client())
            haproxy.name = 'mcloud_haproxy'
            haproxy.image_builder = VirtualFolderImageBuilder({
                'Dockerfile': """
                    FROM haproxy:1.5
                    ADD haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

                    """,
                'haproxy.cfg': config_rendered

            })


            haproxy.ports = ['80/tcp:80', '443/tcp:443']
            # haproxy.volumes = [{
            #     'local': haproxy_path,
            #     'remote': '/etc/haproxy'
            # }]

            logger.info('Containers updated: dumping haproxy config.')

            if ticket_id:
                self.rpc_server.task_progress('updated %s - OK' % deployment_name, ticket_id)

            yield haproxy.rebuild()
コード例 #3
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_generate_config_volumes():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({txredisapi.Connection: redis})

    s = Service()
    s.name = 'my_service'
    s.volumes = [{
        'local': '/base/path/foo1',
        'remote': '/bar1'
    }, {
        'local': '/base/path/foo2',
        'remote': '/bar2'
    }, {
        'local': '/base/path/foo3',
        'remote': '/bar3'
    }]

    config = yield s._generate_config('foo')
    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo',

        # Volumes are not passed as of Docker 1.6 version
        # "Volumes": {
        #     "/bar1": {},
        #     "/bar2": {},
        #     "/bar3": {},
        # }
    }
コード例 #4
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_no_lazy_inspect_not_inspected(method):

    s = Service(name='foo', client=flexmock())
    s._inspected = False

    with pytest.raises(Service.NotInspectedYet):
        getattr(s, method)()
コード例 #5
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_no_lazy_inspect_not_inspected(method):

    s = Service(name='foo', client=flexmock())
    s._inspected = False

    with pytest.raises(Service.NotInspectedYet):
        getattr(s, method)()
コード例 #6
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_no_lazy_inspect(method):

    s = Service(name='foo', client=flexmock())
    s._inspected = True
    s._inspect_data = {'State': {'Running': True}}

    getattr(s, method)()
コード例 #7
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_wrong_inspect_data():

    s = Service()
    s._inspected = True
    s._inspect_data = {'foo': 'bar'}

    assert not s.is_running()
コード例 #8
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_generate_config_volumes():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({
        txredisapi.Connection: redis
    })

    s = Service()
    s.name = 'my_service'
    s.volumes = [
        {'local': '/base/path/foo1', 'remote': '/bar1'},
        {'local': '/base/path/foo2', 'remote': '/bar2'},
        {'local': '/base/path/foo3', 'remote': '/bar3'}
    ]

    config = yield s._generate_config('foo')
    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo',

        # Volumes are not passed as of Docker 1.6 version
        # "Volumes": {
        #     "/bar1": {},
        #     "/bar2": {},
        #     "/bar3": {},
        # }
    }
コード例 #9
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_wrong_inspect_data():

    s = Service()
    s._inspected = True
    s._inspect_data = {'foo': 'bar'}

    assert not s.is_running()
コード例 #10
0
ファイル: dns.py プロジェクト: hydface2/mcloud
    def dump(self, apps_list):
        apps = {
            'mcloud.lh': self.host_ip
        }

        for app in apps_list:
            for service in app['services']:
                apps[service['fullname']] = service['ip']

            if 'web_service' in app and app['web_service']:
                apps[app['fullname']] = app['web_ip']

            if 'public_urls' in app and app['public_urls'] and 'web_ip' in app:
                for target in app['public_urls']:
                    if not target['service']:
                        apps[target['url']] = app['web_ip']
                    else:
                        for service in app['services']:
                            if service['shortname'] == target['service']:
                                apps[target['url']] = service['ip']

        log.msg('Installing new dns list: %s' % str(apps))

        yield self.redis.delete('domain')

        if len(apps) > 1:
            yield self.redis.hmset('domain', apps)
        elif len(apps) == 1:
            yield self.redis.hset('domain', apps.keys()[0], apps.values()[0])

        log.msg('Restarting dnsmasq')

        # extra options
        cmd = []
        for url, ip in apps.items():
            cmd.append('--host-record=%s,%s' % (url, ip))


        self.dnsmasq = Service()
        self.dnsmasq.name = 'mcloud_dnsmasq'
        self.dnsmasq.image_builder = InlineDockerfileImageBuilder(source="""
        FROM ubuntu:14.04
        RUN apt-get update && apt-get install -y dnsmasq dnsutils && apt-get clean
        CMD dnsmasq -k %s --server=8.8.8.8 -u root
        """ % ' '.join(cmd))

        self.dnsmasq.ports = [
            '53/tcp:%s_53' % self.settings.dns_ip,
            '53/udp:%s_53' % self.settings.dns_ip,
        ]

        yield self.dnsmasq.create()
        self.app_controller.mark_internal(self.dnsmasq.id)

        yield self.dnsmasq.rebuild()
コード例 #11
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_generate_config():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({txredisapi.Connection: redis})

    s = Service()
    s.name = 'my_service'

    config = yield s._generate_config('foo')

    assert config == {"Hostname": 'my_service', "Image": 'foo'}
コード例 #12
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_inspect():

    s = Service(name='foo', client=flexmock())

    s.client.should_receive('inspect').with_args('foo').ordered().and_return(defer.succeed({'foo': 'bar'}))

    assert s.is_inspected() is False

    r = yield s.inspect()
    assert r == {'foo': 'bar'}

    assert s.is_inspected() is True

    assert s._inspect_data == {'foo': 'bar'}
コード例 #13
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_start_volumes_from():

    with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}):
        s = Service()
        s.name = 'my_service'
        s.volumes_from = ['foo', 'bar']

        flexmock(s)

        s.client = flexmock()

        s.client.should_receive('find_container_by_name').with_args(
            'my_service').once().and_return(defer.succeed('123abc'))
        s.client.should_receive('start_container').with_args(
            '123abc',
            ticket_id=123123,
            config={
                "VolumesFrom": ['foo', 'bar'],
                'DnsSearch': 'None.local',
                'Dns': ['local.dns']
            }).once().and_return(defer.succeed('boo'))
        s.should_receive('inspect').with_args().once().and_return(
            defer.succeed('baz'))

        r = yield s.start(ticket_id=123123)

        assert r == 'baz'
コード例 #14
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_start_ports():

    with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}):
        s = Service()
        s.name = 'my_service'
        s.ports = ['22/tcp']

        flexmock(s)

        s.client = flexmock()

        s.client.should_receive('find_container_by_name').with_args(
            'my_service').once().and_return(defer.succeed('123abc'))
        s.client.should_receive('start_container').with_args(
            '123abc',
            ticket_id=123123,
            config={
                "PortBindings": {
                    '22/tcp': [{}]
                },
                'DnsSearch': 'None.local',
                'Dns': ['local.dns']
            }).once().and_return(defer.succeed('boo'))
        s.should_receive('inspect').with_args().once().and_return(
            defer.succeed('baz'))

        r = yield s.start(ticket_id=123123)

        assert r == 'baz'
コード例 #15
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_create():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({txredisapi.Connection: redis})

    s = Service()
    s.name = 'my_service'
    flexmock(s)

    s.image_builder = flexmock()
    s.image_builder.should_receive('build_image').with_args(ticket_id=123123, service=s).ordered().once()\
        .and_return(defer.succeed('boo'))

    s.client = flexmock()
    s.client.should_receive('create_container').with_args(
        {
            "Hostname": 'my_service',
            "Image": 'boo'
        },
        'my_service',
        ticket_id=123123).ordered().once().and_return('magic')

    s.should_receive('inspect').with_args().ordered().once().and_return(
        'magic')

    r = yield s.create(ticket_id=123123)

    assert r == 'magic'
コード例 #16
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_inspect():

    s = Service(name='foo', client=flexmock())

    s.client.should_receive('inspect').with_args('foo').ordered().and_return(
        defer.succeed({'foo': 'bar'}))

    assert s.is_inspected() is False

    r = yield s.inspect()
    assert r == {'foo': 'bar'}

    assert s.is_inspected() is True

    assert s._inspect_data == {'foo': 'bar'}
コード例 #17
0
def test_image_builder_prebuilt_already_built():

    with mock_docker() as docker_mock:
        dm = docker_mock
        """@type : flexmock.Mock"""

        #dm.should_receive('images').with_args(name='foo/bar').and_return([])

        dm.should_receive('pull').never()
        dm.should_receive('images').with_args(name='foo/bar').once(
        ).and_return(
            defer.succeed([{
                "RepoTags": [
                    "foo:bar",
                ],
                "Id":
                "8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c",
                "Created": 1365714795,
                "Size": 131506275,
                "VirtualSize": 131506275
            }]))

        s = Service(client=dm)

        builder = PrebuiltImageBuilder('foo/bar')

        result = yield builder.build_image(ticket_id=123123, service=s)
        assert result == 'foo/bar'
コード例 #18
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_generate_config_env():

    redis = flexmock()
    redis.should_receive('hgetall').and_return({})

    fake_inject({txredisapi.Connection: redis})

    s = Service()
    s.name = 'my_service'
    s.env = {'FOO': 'bar', 'BAZ': 'foo'}

    config = yield s._generate_config('foo')
    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo',
        "Env": ['FOO=bar', 'BAZ=foo']
    }
コード例 #19
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_start_volumes():

    with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}):
        s = Service()
        s.name = 'my_service'
        s.volumes = [
            {'local': '/base/path/foo1', 'remote': '/bar1'},
            {'local': '/base/path/foo2', 'remote': '/bar2'},
            {'local': '/base/path/foo3', 'remote': '/bar3'}
        ]
        flexmock(s)

        s.client = flexmock()

        s.client.should_receive('find_container_by_name').with_args('my_service').once().and_return(defer.succeed('123abc'))
        s.client.should_receive('start_container').with_args('123abc', ticket_id=123123, config={
            "Binds": ['/base/path/foo1:/bar1', '/base/path/foo2:/bar2', '/base/path/foo3:/bar3'],
            'DnsSearch': 'None.local',
            'Dns': ['local.dns']
        }).once().and_return(defer.succeed('boo'))
        s.should_receive('inspect').with_args().once().and_return(defer.succeed('baz'))

        r = yield s.start(ticket_id=123123)

        assert r == 'baz'
コード例 #20
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_create():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({
        txredisapi.Connection: redis
    })

    s = Service()
    s.name = 'my_service'
    flexmock(s)

    s.image_builder = flexmock()
    s.image_builder.should_receive('build_image').with_args(ticket_id=123123, service=s).ordered().once()\
        .and_return(defer.succeed('boo'))

    s.client = flexmock()
    s.client.should_receive('create_container').with_args({
        "Hostname": 'my_service',
        "Image": 'boo'
    }, 'my_service', ticket_id=123123).ordered().once().and_return('magic')

    s.should_receive('inspect').with_args().ordered().once().and_return('magic')

    r = yield s.create(ticket_id=123123)

    assert r == 'magic'
コード例 #21
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_generate_config():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({
        txredisapi.Connection: redis
    })

    s = Service()
    s.name = 'my_service'

    config = yield s._generate_config('foo')

    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo'
    }
コード例 #22
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_generate_config_env():

    redis = flexmock()
    redis.should_receive('hgetall').and_return({})

    fake_inject({
        txredisapi.Connection: redis
    })

    s = Service()
    s.name = 'my_service'
    s.env = {'FOO': 'bar', 'BAZ': 'foo'}

    config = yield s._generate_config('foo')
    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo',
        "Env": ['FOO=bar', 'BAZ=foo']
    }
コード例 #23
0
    def task_sync_stop(self, ticket_id, app_name, sync_ticket_id):

        app = yield self.app_controller.get(app_name)

        client = yield app.get_client()

        s = Service(client=client)
        s.app_name = app_name
        s.name = '%s_%s_%s' % (app_name, '_rsync_', sync_ticket_id)

        yield s.inspect()

        if s.is_running():
            self.task_log(ticket_id, 'Stopping rsync container.')
            yield s.stop(ticket_id)

        if s.is_created():
            self.task_log(ticket_id, 'Destroying rsync container.')
            yield s.destroy(ticket_id)
コード例 #24
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_service_init():

    builder = flexmock(read=lambda: 'boo')
    s = Service(image_builder=builder,
                name='foo',
                volumes=[{
                    'foo': 'bar'
                }],
                command='some --cmd',
                env={'baz': 'bar'})

    assert s.image_builder == builder
    assert s.name == 'foo'
    assert s.volumes == [{'foo': 'bar'}]
    assert s.command == 'some --cmd'
    assert s.env == {'baz': 'bar'}
コード例 #25
0
ファイル: tasks.py プロジェクト: hydface2/mcloud
    def task_sync_stop(self, ticket_id, app_name, sync_ticket_id):

        app = yield self.app_controller.get(app_name)

        client = yield app.get_client()

        s = Service(client=client)
        s.app_name = app_name
        s.name = '%s_%s_%s' % (app_name, '_rsync_', sync_ticket_id)

        yield s.inspect()

        if s.is_running():
            self.task_log(ticket_id, 'Stopping rsync container.')
            yield s.stop(ticket_id)

        if s.is_created():
            self.task_log(ticket_id, 'Destroying rsync container.')
            yield s.destroy(ticket_id)
コード例 #26
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_start_volumes():

    with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}):
        s = Service()
        s.name = 'my_service'
        s.volumes = [{
            'local': '/base/path/foo1',
            'remote': '/bar1'
        }, {
            'local': '/base/path/foo2',
            'remote': '/bar2'
        }, {
            'local': '/base/path/foo3',
            'remote': '/bar3'
        }]
        flexmock(s)

        s.client = flexmock()

        s.client.should_receive('find_container_by_name').with_args(
            'my_service').once().and_return(defer.succeed('123abc'))
        s.client.should_receive('start_container').with_args(
            '123abc',
            ticket_id=123123,
            config={
                "Binds": [
                    '/base/path/foo1:/bar1', '/base/path/foo2:/bar2',
                    '/base/path/foo3:/bar3'
                ],
                'DnsSearch':
                'None.local',
                'Dns': ['local.dns']
            }).once().and_return(defer.succeed('boo'))
        s.should_receive('inspect').with_args().once().and_return(
            defer.succeed('baz'))

        r = yield s.start(ticket_id=123123)

        assert r == 'baz'
コード例 #27
0
def test_image_builder_prebuilt():

    with mock_docker() as docker_mock:
        dm = docker_mock
        """@type : flexmock.Mock"""

        #dm.should_receive('images').with_args(name='foo/bar').and_return([])

        dm.should_receive('images').with_args(
            name='foo/bar').once().and_return(defer.succeed([]))
        dm.should_receive('pull').with_args(
            name='foo/bar', ticket_id=123123,
            tag=None).once().and_return(defer.succeed(['foo', 'bar', 'baz']))

        s = Service(client=dm)

        builder = PrebuiltImageBuilder('foo/bar')

        result = yield builder.build_image(ticket_id=123123, service=s)
        assert result == 'foo/bar'
コード例 #28
0
def test_image_builder_build():

    with mock_docker() as client:

        builder = DockerfileImageBuilder(
            os.path.join(os.path.dirname(__file__), '_files/ct_bash'))

        flexmock(builder)

        from mcloud.service import Service
        s = Service(client=client)

        builder.should_receive('create_archive').once().and_return(
            defer.succeed('foo'))

        client.should_receive('build_image').with_args(
            'foo', ticket_id=123123).and_return(defer.succeed('baz'))

        result = yield builder.build_image(ticket_id=123123, service=s)

        assert result == 'baz'
コード例 #29
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_start():

    with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}):

        s = Service()
        s.name = 'my_service'
        flexmock(s)

        s.client = flexmock()

        s.client.should_receive('find_container_by_name').with_args('my_service').once().and_return(defer.succeed('123abc'))
        s.client.should_receive('start_container').with_args('123abc', ticket_id=123123, config={'DnsSearch': 'None.local', 'Dns': ['local.dns']}).once().and_return(defer.succeed('boo'))
        s.should_receive('inspect').with_args().once().and_return(defer.succeed('baz'))

        r = yield s.start(ticket_id=123123)

        assert r == 'baz'
コード例 #30
0
ファイル: mcloud_haproxy.py プロジェクト: hydface2/mcloud
    def rebuild_haproxy(self, deployments=None, ticket_id=None):

        # generate new haproxy config
        all_deployments = yield self.dump()

        for deployment_name, config in all_deployments.items():

            # rebuild only needed deployments
            if deployments and not deployment_name in deployments:
                continue

            if ticket_id:
                self.rpc_server.task_progress(
                    'Updating haproxy config on deployment %s' %
                    deployment_name, ticket_id)

            deployment = yield self.dep_controller.get(deployment_name)

            haproxy_path = os.path.expanduser(
                '%s/haproxy/%s' % (self.settings.home_dir, deployment_name))
            if not os.path.exists(haproxy_path):
                os.makedirs(haproxy_path)

            template_path = os.path.join(haproxy_path, 'haproxy.tpl')
            haproxy_config_path = os.path.join(haproxy_path, 'haproxy.cfg')

            if not os.path.exists(template_path):
                with open(template_path, 'w+') as f:
                    f.write(HAPROXY_TPL)

            with open(template_path) as f:
                template = Template(f.read())

            config_rendered = template.render(config)

            with open(haproxy_config_path, 'w+') as f:
                f.write(config_rendered)

            haproxy = Service(client=deployment.get_client())
            haproxy.name = 'mcloud_haproxy'
            haproxy.image_builder = VirtualFolderImageBuilder({
                'Dockerfile':
                """
                    FROM haproxy:1.5
                    ADD haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

                    """,
                'haproxy.cfg':
                config_rendered
            })

            haproxy.ports = ['80/tcp:80', '443/tcp:443']
            # haproxy.volumes = [{
            #     'local': haproxy_path,
            #     'remote': '/etc/haproxy'
            # }]

            logger.info('Containers updated: dumping haproxy config.')

            if ticket_id:
                self.rpc_server.task_progress(
                    'updated %s - OK' % deployment_name, ticket_id)

            yield haproxy.rebuild()
コード例 #31
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_service_api():
    from twisted.python import log

    redis = yield txredisapi.Connection(dbid=2)
    yield redis.flushdb()

    yield redis.set('mcloud-ticket-id', 123122)

    eb = EventBus(redis)
    yield eb.connect()

    redis = flexmock()
    redis.should_receive('hgetall').and_return({})
    redis.should_receive('hget').and_return(None)

    fake_inject({
        EventBus: eb,
        'dns-server': 'local.dns',
        'dns-search-suffix': 'local',
        IDockerClient: DockerTwistedClient(),
        txredisapi.Connection: redis
    })

    name = 'test.foo'

    s = Service(
        image_builder=DockerfileImageBuilder(os.path.join(os.path.dirname(__file__), '_files/ct_bash')),
        name=name,
        client=inject.instance(IDockerClient)
    )

    class Printer(object):
        def publish(self, *args):
            print args

    s.client.message_publisher = Printer()

    yield s.inspect()

    if s.is_created():
        yield s.stop(ticket_id=123123)
        yield s.destroy(ticket_id=123123)

    assert not s.is_created()
    assert not s.is_running()

    yield s.create(ticket_id=123123)
    assert s.is_created()
    assert not s.is_running()

    yield s.start(ticket_id=123123)
    assert s.is_created()
    assert s.is_running()


    yield s.stop(ticket_id=123123)
    assert s.is_created()
    assert not s.is_running()

    yield s.destroy(ticket_id=123123)
    assert not s.is_created()
    assert not s.is_running()

#
#
#
# @pytest.inlineCallbacks
# def test_volume_snapshot():
#     from twisted.python import log
#     log.startLogging(sys.stdout)
#
#     redis = yield txredisapi.Connection(dbid=2)
#     yield redis.flushdb()
#
#     yield redis.set('mcloud-ticket-id', 123122)
#
#     eb = EventBus(redis)
#     yield eb.connect()
#
#     with injector({EventBus: eb, 'dns-server': 'local.dns', 'dns-search-suffix': 'local', IDockerClient: DockerTwistedClient()}):
#
#         name = 'test.foo'
#
#         s = Service(
#             image_builder=DockerfileImageBuilder(os.path.join(os.path.dirname(__file__), '_files/ct_bash')),
#             name=name,
#             volumes=[
#                 {'local': os.path.join(os.path.dirname(__file__), '_files/boo'), 'remote': '/var/foo'}
#             ]
#         )
#
#         yield s.create(ticket_id=123123)
#         yield s.start(ticket_id=123123)
#
#         assert s.is_created()
#         assert s.is_running()
#
#
#
#         yield s.destroy(ticket_id=123123)
コード例 #32
0
    def task_sync(self, ticket_id, app_name, service_name, volume):

        app = yield self.app_controller.get(app_name)

        config = yield app.load()
        client = yield app.get_client()

        s = Service(client=client)
        s.app_name = app_name
        s.name = '%s_%s_%s' % (app_name, '_rsync_', ticket_id)
        s.image_builder = PrebuiltImageBuilder(image='modera/rsync')
        s.ports = [873]

        if service_name:

            if not volume:
                raise VolumeNotFound(
                    'In case of service name is provided, volume name is mandatory!'
                )

            services = config.get_services()

            service_full_name = '%s.%s' % (service_name, app_name)
            try:
                service = services[service_full_name]

                all_volumes = service.list_volumes()
                if not volume in all_volumes:
                    raise VolumeNotFound('Volume with name %s no found!' %
                                         volume)

                volume_name = volume
                s.volumes_from = service_full_name

            except KeyError:
                raise VolumeNotFound('Service with name %s was not found!' %
                                     service_name)

        else:
            s.volumes = [{'local': app.config['path'], 'remote': '/volume'}]
            volume_name = '/volume'

        s.env = {
            'USERNAME':
            ''.join(
                random.choice(string.ascii_lowercase + string.digits)
                for _ in range(32)),
            'PASSWORD':
            ''.join(
                random.choice(string.ascii_lowercase + string.punctuation +
                              string.digits) for _ in range(32)),
            'ALLOW':
            '*'
        }

        yield s.start(ticket_id)

        deployment = yield app.get_deployment()

        if deployment.local:
            sync_host = 'me'
        else:
            sync_host = deployment.host

        print s.public_ports()
        defer.returnValue({
            'env': s.env,
            'container': s.name,
            'host': sync_host,
            'port': s.public_ports()['873/tcp'][0]['HostPort'],
            'volume': volume_name,
            'ticket_id': ticket_id
        })
コード例 #33
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_not_inspected():

    s = Service()
    assert s.is_inspected() is False
コード例 #34
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_inspected():

    s = Service()
    s._inspected = True

    assert s.is_inspected() is True
コード例 #35
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_service_api():
    from twisted.python import log

    redis = yield txredisapi.Connection(dbid=2)
    yield redis.flushdb()

    yield redis.set('mcloud-ticket-id', 123122)

    eb = EventBus(redis)
    yield eb.connect()

    redis = flexmock()
    redis.should_receive('hgetall').and_return({})
    redis.should_receive('hget').and_return(None)

    fake_inject({
        EventBus: eb,
        'dns-server': 'local.dns',
        'dns-search-suffix': 'local',
        IDockerClient: DockerTwistedClient(),
        txredisapi.Connection: redis
    })

    name = 'test.foo'

    s = Service(image_builder=DockerfileImageBuilder(
        os.path.join(os.path.dirname(__file__), '_files/ct_bash')),
                name=name,
                client=inject.instance(IDockerClient))

    class Printer(object):
        def publish(self, *args):
            print args

    s.client.message_publisher = Printer()

    yield s.inspect()

    if s.is_created():
        yield s.stop(ticket_id=123123)
        yield s.destroy(ticket_id=123123)

    assert not s.is_created()
    assert not s.is_running()

    yield s.create(ticket_id=123123)
    assert s.is_created()
    assert not s.is_running()

    yield s.start(ticket_id=123123)
    assert s.is_created()
    assert s.is_running()

    yield s.stop(ticket_id=123123)
    assert s.is_created()
    assert not s.is_running()

    yield s.destroy(ticket_id=123123)
    assert not s.is_created()
    assert not s.is_running()


#
#
#
# @pytest.inlineCallbacks
# def test_volume_snapshot():
#     from twisted.python import log
#     log.startLogging(sys.stdout)
#
#     redis = yield txredisapi.Connection(dbid=2)
#     yield redis.flushdb()
#
#     yield redis.set('mcloud-ticket-id', 123122)
#
#     eb = EventBus(redis)
#     yield eb.connect()
#
#     with injector({EventBus: eb, 'dns-server': 'local.dns', 'dns-search-suffix': 'local', IDockerClient: DockerTwistedClient()}):
#
#         name = 'test.foo'
#
#         s = Service(
#             image_builder=DockerfileImageBuilder(os.path.join(os.path.dirname(__file__), '_files/ct_bash')),
#             name=name,
#             volumes=[
#                 {'local': os.path.join(os.path.dirname(__file__), '_files/boo'), 'remote': '/var/foo'}
#             ]
#         )
#
#         yield s.create(ticket_id=123123)
#         yield s.start(ticket_id=123123)
#
#         assert s.is_created()
#         assert s.is_running()
#
#
#
#         yield s.destroy(ticket_id=123123)
コード例 #36
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_not_inspected():

    s = Service()
    assert s.is_inspected() is False
コード例 #37
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_inspected():

    s = Service()
    s._inspected = True

    assert s.is_inspected() is True
コード例 #38
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_is_inspected():

    s = Service()

    assert not s.is_inspected()
コード例 #39
0
ファイル: dns.py プロジェクト: hydface2/mcloud
class DnsPlugin(Plugin):
    implements(IMcloudPlugin, IServiceBuilder)


    eb = inject.attr(EventBus)
    app_controller = inject.attr(ApplicationController)
    redis = inject.attr(txredisapi.Connection)
    settings = inject.attr('settings')
    host_ip = inject.attr('host-ip')
    dns_search_suffix = inject.attr('dns-search-suffix')
    """ @var McloudConfiguration """

    @inlineCallbacks
    def dump(self, apps_list):
        apps = {
            'mcloud.lh': self.host_ip
        }

        for app in apps_list:
            for service in app['services']:
                apps[service['fullname']] = service['ip']

            if 'web_service' in app and app['web_service']:
                apps[app['fullname']] = app['web_ip']

            if 'public_urls' in app and app['public_urls'] and 'web_ip' in app:
                for target in app['public_urls']:
                    if not target['service']:
                        apps[target['url']] = app['web_ip']
                    else:
                        for service in app['services']:
                            if service['shortname'] == target['service']:
                                apps[target['url']] = service['ip']

        log.msg('Installing new dns list: %s' % str(apps))

        yield self.redis.delete('domain')

        if len(apps) > 1:
            yield self.redis.hmset('domain', apps)
        elif len(apps) == 1:
            yield self.redis.hset('domain', apps.keys()[0], apps.values()[0])

        log.msg('Restarting dnsmasq')

        # extra options
        cmd = []
        for url, ip in apps.items():
            cmd.append('--host-record=%s,%s' % (url, ip))


        self.dnsmasq = Service()
        self.dnsmasq.name = 'mcloud_dnsmasq'
        self.dnsmasq.image_builder = InlineDockerfileImageBuilder(source="""
        FROM ubuntu:14.04
        RUN apt-get update && apt-get install -y dnsmasq dnsutils && apt-get clean
        CMD dnsmasq -k %s --server=8.8.8.8 -u root
        """ % ' '.join(cmd))

        self.dnsmasq.ports = [
            '53/tcp:%s_53' % self.settings.dns_ip,
            '53/udp:%s_53' % self.settings.dns_ip,
        ]

        yield self.dnsmasq.create()
        self.app_controller.mark_internal(self.dnsmasq.id)

        yield self.dnsmasq.rebuild()

        # with open('/etc/resolv.conf', 'w+') as f:
        #     f.write('nameserver %s\n' % self.host_ip)
        #     f.write('nameserver 8.8.8.8')




    def configure_container_on_create(self, service, config):
        pass


    # @inlineCallbacks
    def configure_container_on_start(self, service, config):
        pass
        # config.update({
        #     "Dns": [self.host_ip],
        #     "DnsSearch": '%s.%s' % (service.app_name, self.dns_search_suffix)
        # })


    # @inlineCallbacks
    def setup(self):
        pass
        # self.eb.on('containers.updated', self.containers_updated)
        # log.msg('Dns plugin started')
        #
        # yield self.containers_updated()

    @inlineCallbacks
    def containers_updated(self, *args, **kwargs):
        data = yield self.app_controller.list()
        self.dump(data)
コード例 #40
0
ファイル: tasks.py プロジェクト: hydface2/mcloud
    def task_sync(self, ticket_id, app_name, service_name, volume):

        app = yield self.app_controller.get(app_name)

        config = yield app.load()
        client = yield app.get_client()

        s = Service(client=client)
        s.app_name = app_name
        s.name = '%s_%s_%s' % (app_name, '_rsync_', ticket_id)
        s.image_builder = PrebuiltImageBuilder(image='modera/rsync')
        s.ports = [873]

        if service_name:

            if not volume:
                raise VolumeNotFound('In case of service name is provided, volume name is mandatory!')

            services = config.get_services()

            service_full_name = '%s.%s' % (service_name, app_name)
            try:
                service = services[service_full_name]

                all_volumes = service.list_volumes()
                if not volume in all_volumes:
                    raise VolumeNotFound('Volume with name %s no found!' % volume)

                volume_name = volume
                s.volumes_from = service_full_name

            except KeyError:
                raise VolumeNotFound('Service with name %s was not found!' % service_name)

        else:
            s.volumes = [{
                             'local': app.config['path'],
                             'remote': '/volume'
                         }]
            volume_name = '/volume'

        s.env = {
            'USERNAME': ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32)),
            'PASSWORD': ''.join(
                random.choice(string.ascii_lowercase + string.punctuation + string.digits) for _ in range(32)),
            'ALLOW': '*'
        }

        yield s.start(ticket_id)

        deployment = yield app.get_deployment()

        if deployment.local:
            sync_host = 'me'
        else:
            sync_host = deployment.host

        print s.public_ports()
        defer.returnValue({
            'env': s.env,
            'container': s.name,
            'host': sync_host,
            'port': s.public_ports()['873/tcp'][0]['HostPort'],
            'volume': volume_name,
            'ticket_id': ticket_id
        })
コード例 #41
0
ファイル: test_service.py プロジェクト: hydface2/mcloud
def test_is_inspected():

    s = Service()

    assert not s.is_inspected()