コード例 #1
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()
コード例 #2
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'
コード例 #3
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'
コード例 #4
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
        })
コード例 #5
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
        })
コード例 #6
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()