예제 #1
0
    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()
예제 #2
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)
예제 #3
0
    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()