def clone_container(self, container_id=None): c_id = utils.get_short_id(container_id) c = Container.objects.get(container_id=c_id) meta = c.get_meta() cfg = meta.get('Config') image = cfg.get('Image') command = ' '.join(cfg.get('Cmd')) # update port spec to specify the original NAT'd port port_mapping = meta.get('NetworkSettings').get('PortMapping') port_specs = [] if port_mapping: for x, y in port_mapping.items(): for k, v in y.items(): port_specs.append('{}/{}'.format(k, x.lower())) env = cfg.get('Env') mem = cfg.get('Memory') description = c.description volumes = cfg.get('Volumes') volumes_from = cfg.get('VolumesFrom') privileged = cfg.get('Privileged') owner = c.owner c_id, status = self.create_container(image, command, port_specs, env, mem, description, volumes, volumes_from, privileged, owner) return c_id, status
def get_containers(self, show_all=False): c = self._get_client() key = self._generate_container_cache_key(show_all) containers = cache.get(key) container_ids = [] if containers is None: try: containers = c.containers(all=show_all) except requests.ConnectionError: containers = [] # update meta data for x in containers: # only get first 12 chars of id (for metatdata) c_id = utils.get_short_id(x.get('Id')) # ignore stopped containers meta = c.inspect_container(c_id) m, created = Container.objects.get_or_create( container_id=c_id, host=self) from pprint import pprint pprint(json.dumps(meta)) m.meta = json.dumps(meta) m.save() container_ids.append(c_id) cache.set(key, containers, HOST_CACHE_TTL) return containers
def get_containers(self, show_all=False): c = client.Client(base_url='http://{0}:{1}'.format(self.hostname, self.port)) key = self._generate_container_cache_key(show_all) containers = cache.get(key) container_ids = [] if containers is None: try: containers = c.containers(all=show_all) except requests.ConnectionError: containers = [] # update meta data for x in containers: # only get first 12 chars of id (for metatdata) c_id = utils.get_short_id(x.get('Id')) # ignore stopped containers meta = c.inspect_container(c_id) m, created = Container.objects.get_or_create( container_id=c_id, host=self) m.is_running = meta.get('State', {}).get('Running', False) m.meta = json.dumps(meta) m.save() container_ids.append(c_id) # set extra containers to not running Container.objects.filter(host=self).exclude( container_id__in=container_ids).update(is_running=False) cache.set(key, containers, HOST_CACHE_TTL) return containers
def get_containers(self, show_all=False): c = client.Client( base_url='http://{0}:{1}'.format(self.hostname, self.port)) key = self._generate_container_cache_key(show_all) containers = cache.get(key) container_ids = [] if containers is None: try: containers = c.containers(all=show_all) except requests.ConnectionError: containers = [] # update meta data for x in containers: # only get first 12 chars of id (for metatdata) c_id = utils.get_short_id(x.get('Id')) # ignore stopped containers meta = c.inspect_container(c_id) m, created = Container.objects.get_or_create(container_id=c_id, host=self) m.is_running = meta.get('State', {}).get('Running', False) m.meta = json.dumps(meta) m.save() container_ids.append(c_id) # set extra containers to not running Container.objects.filter(host=self).exclude( container_id__in=container_ids).update(is_running=False) cache.set(key, containers, HOST_CACHE_TTL) return containers
def destroy_container(self, container_id=None): c = self._get_client() c_id = utils.get_short_id(container_id) c.kill(c_id) c.remove_container(c_id) # remove metadata Container.objects.filter(container_id=c_id).delete() self._invalidate_container_cache()
def stop_container(self, container_id=None): c_id = utils.get_short_id(container_id) c = Container.objects.get(container_id=c_id) if c.protected: raise ProtectedContainerError( _('Unable to stop container. Container is protected.')) c = self._get_client() c.stop(c_id) self._invalidate_container_cache()
def handle(self, *args, **options): hosts = Host.objects.filter(enabled=True) all_containers = [x.container_id for x in Container.objects.all()] for host in hosts: host_containers = [utils.get_short_id(c.get('Id')) \ for c in host.get_containers(show_all=True)] for c in all_containers: if c not in host_containers: print('Removing {}'.format(c)) Container.objects.get(container_id=c).delete()
def attach_container(request, host, container_id): h = Host.objects.get(name=host) c_id = utils.get_short_id(container_id) c = Container.objects.get(container_id=c_id) ctx = { 'container_id': c_id, 'container_name': c.description or c_id, 'host_url': '{0}:{1}'.format(h.hostname, h.port), } return render_to_response("containers/attach.html", ctx, context_instance=RequestContext(request))
def destroy_container(self, container_id=None): c_id = utils.get_short_id(container_id) c = Container.objects.get(container_id=c_id) if c.protected: raise ProtectedContainerError( _('Unable to destroy container. Container is protected.')) c = self._get_client() c.kill(c_id) c.remove_container(c_id) # remove metadata Container.objects.filter(container_id=c_id).delete() self._invalidate_container_cache()
def attach_container(request, host, container_id): h = Host.objects.get(name=host) c_id = utils.get_short_id(container_id) c = Container.objects.get(container_id=c_id) session_id = utils.generate_console_session(h, c) ctx = { 'container_id': c_id, 'container_name': c.description or c_id, 'ws_url': 'ws://{0}/console/{1}/'.format(request.META['HTTP_HOST'], session_id), } return render_to_response("containers/attach.html", ctx, context_instance=RequestContext(request))
def get_all_containers(cls, show_all=False, owner=None): hosts = Host.objects.filter(enabled=True) containers = [] # load containers if hosts: c_ids = [] for h in hosts: for c in h.get_containers(show_all=show_all): c_ids.append(utils.get_short_id(c.get('Id'))) # return metadata objects containers = Container.objects.filter( container_id__in=c_ids).filter(Q(owner=None) | Q(owner=owner)) return containers
def restart_container(self, container_id=None): from applications.models import Application c = self._get_client() c.restart(container_id) self._invalidate_container_cache() # reload containers to get proper port self.get_containers() # update hipache container = Container.objects.get( container_id=utils.get_short_id(container_id)) apps = Application.objects.filter(containers__in=[container]) for app in apps: app.update_config()
def get_all_containers(cls, show_all=False, owner=None): hosts = Host.objects.filter(enabled=True) containers = [] # load containers if hosts: c_ids = [] for h in hosts: for c in h.get_containers(show_all=show_all): c_ids.append(utils.get_short_id(c.get('Id'))) # return metadata objects containers = Container.objects.filter(container_id__in=c_ids).filter( Q(owner=None) | Q(owner=owner)) return containers
def get_running(cls, user=None): hosts = Host.objects.filter(enabled=True) containers = [] if hosts: c_ids = [] for h in hosts: for c in h.get_containers(): c_ids.append(utils.get_short_id(c.get('Id'))) # return metadata objects containers = Container.objects.filter( container_id__in=c_ids).filter(Q(owner=None)) if user: containers = containers.filter(Q(owner=request.user)) return containers
def get_running(cls, user=None): hosts = Host.objects.filter(enabled=True) containers = [] if hosts: c_ids = [] for h in hosts: for c in h.get_containers(): c_ids.append(utils.get_short_id(c.get('Id'))) # return metadata objects containers = Container.objects.filter(container_id__in=c_ids).filter( Q(owner=None)) if user: containers = containers.filter(Q(owner=request.user)) return containers
def _host_info(request): hosts = Host.objects.filter(enabled=True) # load containers cnt = [h.get_containers() for h in hosts][0] # get list of ids to filter Container metadata c_ids = [utils.get_short_id(x.get('Id')) for x in cnt] # return metadata objects containers = Container.objects.filter(container_id__in=c_ids).filter( Q(owner=None) | Q(owner=request.user)) ctx = { 'hosts': hosts, 'containers': containers, } return render_to_response('dashboard/_host_info.html', ctx, context_instance=RequestContext(request))
def destroy_container(self, container_id=None): c_id = utils.get_short_id(container_id) c = Container.objects.get(container_id=c_id) if c.protected: raise ProtectedContainerError( _('Unable to destroy container. Container is protected.')) c = self._get_client() try: c.kill(c_id) c.remove_container(c_id) except client.APIError: # ignore 404s from api if container not found pass # remove metadata Container.objects.filter(container_id=c_id).delete() self._invalidate_container_cache()
def _host_info(request): hosts = Host.objects.filter(enabled=True) # load containers cnt = [h.get_containers() for h in hosts][0] # get list of ids to filter Container metadata c_ids = [utils.get_short_id(x.get('Id')) for x in cnt] # return metadata objects containers = Container.objects.filter( container_id__in=c_ids).filter(Q(owner=None) | Q(owner=request.user)) ctx = { 'hosts': hosts, 'containers': containers, } return render_to_response('dashboard/_host_info.html', ctx, context_instance=RequestContext(request))
def obj_create(self, bundle, request=None, **kwargs): """ Override obj_create to launch containers and return metadata """ # HACK: get host id -- this should probably be some type of # reverse lookup from tastypie host_urls = bundle.data.get("hosts") # remove 'hosts' from data and pass rest to create_container del bundle.data["hosts"] # launch on hosts for host_url in host_urls: host_id = host_url.split("/")[-2] host = Host.objects.get(id=host_id) data = bundle.data c_id, status = host.create_container(**data) bundle.obj = Container.objects.get(container_id=utils.get_short_id(c_id)) bundle = self.full_hydrate(bundle) return bundle
def _host_info(request): hosts = Host.objects.filter(enabled=True) show_all = True if request.GET.has_key('showall') else False containers = None # load containers if hosts: c_ids = [] for h in hosts: for c in h.get_containers(show_all=show_all): c_ids.append(utils.get_short_id(c.get('Id'))) # return metadata objects containers = Container.objects.filter(container_id__in=c_ids).filter( Q(owner=None) | Q(owner=request.user)) ctx = { 'hosts': hosts, 'containers': containers, 'show_all': show_all, } return render_to_response('dashboard/_host_info.html', ctx, context_instance=RequestContext(request))
def obj_create(self, bundle, request=None, **kwargs): """ Override obj_create to launch containers and return metadata """ # HACK: get host id -- this should probably be some type of # reverse lookup from tastypie host_urls = bundle.data.get('hosts') # remove 'hosts' from data and pass rest to create_container del bundle.data['hosts'] # launch on hosts for host_url in host_urls: host_id = host_url.split('/')[-2] host = Host.objects.get(id=host_id) data = bundle.data c_id, status = host.create_container(**data) bundle.obj = Container.objects.get( container_id=utils.get_short_id(c_id)) bundle = self.full_hydrate(bundle) return bundle
def index(request): hosts = Host.objects.filter(enabled=True) show_all = True if request.GET.has_key('showall') else False containers = None # load containers if hosts: c_ids = [] for h in hosts: for c in h.get_containers(show_all=show_all): c_ids.append(utils.get_short_id(c.get('Id'))) # return metadata objects containers = Container.objects.filter(container_id__in=c_ids).filter( Q(owner=None) | Q(owner=request.user)) ctx = { 'hosts': hosts, 'containers': containers, 'show_all': show_all, } return render_to_response('containers/index.html', ctx, context_instance=RequestContext(request))
def get_containers(self, show_all=False): c = client.Client( base_url='http://{0}:{1}'.format(self.hostname, self.port)) key = self._generate_container_cache_key(show_all) containers = cache.get(key) container_ids = [] if containers is None: containers = c.containers(all=show_all) # update meta data for x in containers: # only get first 12 chars of id (for metatdata) c_id = utils.get_short_id(x.get('Id')) # ignore stopped containers meta = c.inspect_container(c_id) m, created = Container.objects.get_or_create(container_id=c_id, host=self) m.meta = json.dumps(meta) m.save() container_ids.append(c_id) cache.set(key, containers, HOST_CACHE_TTL) return containers
def get_containers(self, show_all=False): c = client.Client(base_url='http://{0}:{1}'.format(self.hostname, self.port)) key = self._generate_container_cache_key(show_all) containers = cache.get(key) container_ids = [] if containers is None: containers = c.containers(all=show_all) # update meta data for x in containers: # only get first 12 chars of id (for metatdata) c_id = utils.get_short_id(x.get('Id')) # ignore stopped containers meta = c.inspect_container(c_id) m, created = Container.objects.get_or_create( container_id=c_id, host=self) m.meta = json.dumps(meta) m.save() container_ids.append(c_id) cache.set(key, containers, HOST_CACHE_TTL) return containers
def get_containers(self, show_all=False): c = self._get_client() key = self._generate_container_cache_key(show_all) containers = cache.get(key) container_ids = [] if containers is None: try: containers = c.containers(all=show_all) except requests.ConnectionError: containers = [] # update meta data for x in containers: # only get first 12 chars of id (for metatdata) c_id = utils.get_short_id(x.get('Id')) # ignore stopped containers self._load_container_data(c_id) container_ids.append(c_id) # set extra containers to not running Container.objects.filter(host=self).exclude( container_id__in=container_ids).update(is_running=False) cache.set(key, containers, HOST_CACHE_TTL) return containers
def clone_container(self, container_id=None): c_id = utils.get_short_id(container_id) c = Container.objects.get(container_id=c_id) meta = c.get_meta() cfg = meta.get('Config') image = cfg.get('Image') command = ' '.join(cfg.get('Cmd')) # update port spec to specify the original NAT'd port port_mapping = meta.get('NetworkSettings').get('PortMapping') port_specs = [] if port_mapping: for x,y in port_mapping.items(): for k,v in y.items(): port_specs.append('{}/{}'.format(k,x.lower())) env = cfg.get('Env') mem = cfg.get('Memory') description = c.description volumes = cfg.get('Volumes') volumes_from = cfg.get('VolumesFrom') privileged = cfg.get('Privileged') owner = c.owner c_id, status = self.create_container(image, command, port_specs, env, mem, description, volumes, volumes_from, privileged, owner) return c_id, status