Ejemplo n.º 1
0
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'
Ejemplo n.º 2
0
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'
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)