def remove_image_by_id(host_id, long_id, force=False, noprune=False): try: _image = Image_Module() if not _image.set_host(host_id).check_health(): return { "status": "failed", "result": { "error": _("Error, Unable to connect to docker host!") }, "notify_type": "failed" } _image.remove_by_id( long_id=long_id, force=force, noprune=noprune ) return { "status": "passed", "result": "{}", "notify_type": "passed" } except Exception as e: return { "status": "error", "result": { "error": str(e) }, "notify_type": "error" }
def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__image_module = Image_Module() self.__logger = self.__helpers.get_logger(__name__)
def tag_image_by_id(host_id, long_id, repository, tag=None, force=False): try: _image = Image_Module() if not _image.set_host(host_id).check_health(): return { "status": "failed", "result": { "error": _("Error, Unable to connect to docker host!") }, "notify_type": "failed" } result = _image.tag_by_id( long_id=long_id, repository=repository, tag=tag, force=force ) if result: return { "status": "passed", "result": "{}", "notify_type": "passed" } else: return { "status": "failed", "result": "{}", "notify_type": "failed" } except Exception as e: return { "status": "error", "result": { "error": str(e) }, "notify_type": "error" }
def build_image(host_id, fileobj, tag, rm=False, nocache=False): try: _image = Image_Module() if not _image.set_host(host_id).check_health(): return { "status": "failed", "result": { "error": _("Error, Unable to connect to docker host!") }, "notify_type": "failed" } result = _image.build( fileobj=fileobj, tag=tag, rm=rm, nocache=nocache ) if result: return { "status": "passed", "result": "{}", "notify_type": "passed" } else: return { "status": "failed", "result": "{}", "notify_type": "failed" } except Exception as e: return { "status": "error", "result": { "error": str(e) }, "notify_type": "error" }
def pull_image(host_id, image_name): try: _image = Image_Module() if not _image.set_host(host_id).check_health(): return { "status": "failed", "result": { "error": _("Error, Unable to connect to docker host!") }, "notify_type": "failed" } image_name = image_name.split(":") result = _image.pull( image_name[0], image_name[1] ) if result: return { "status": "passed", "result": "{}", "notify_type": "passed" } else: return { "status": "failed", "result": "{}", "notify_type": "failed" } except Exception as e: return { "status": "error", "result": { "error": str(e) }, "notify_type": "error" }
class Get_Image(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __image_id = None __host_module = None __image_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__image_module = Image_Module() self.__logger = self.__helpers.get_logger(__name__) def get(self, request, host_id, image_id): self.__user_id = request.user.id self.__host_id = host_id self.__image_id = image_id if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) if self.__image_module.set_host(self.__host_id).check_health(): _image = {'long_id': self.__image_id} return JsonResponse( self.__response.send_private_success([], {'image': _image})) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong with your host!") }], {'image': {}}))
def prune_all_unused_images(host_id): try: _image = Image_Module() if not _image.set_host(host_id).check_health(): return { "status": "failed", "result": { "error": _("Error, Unable to connect to docker host!") }, "notify_type": "failed" } result = _image.prune_all_unused() if result: return { "status": "passed", "result": "{}", "notify_type": "passed" } else: return { "status": "failed", "result": "{}", "notify_type": "failed" } except Exception as e: return { "status": "error", "result": { "error": str(e) }, "notify_type": "error" }
def check_health(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print("Host Up!") else: print("Host Down!")
def list_images(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print(_image.list())
def prune_all_unused_images(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print(_image.prune_all_unused())
def remove_image_by_name(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print(_image.remove_by_name(configs["repository"], configs["tag"]))
class Search_Community_Images(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __image_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__image_module = Image_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id self.__request.set_request(request) request_data = self.__request.get_request_data("post", {"term": ""}) self.__form.add_inputs({ 'term': { 'value': request_data["term"], 'validate': { 'not_empty': { 'error': _('Error! Search term is required!') }, 'length_between': { 'param': [1, 100], 'error': _('Error! a valid search term is required!') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) if self.__image_module.set_host(self.__host_id).check_health(): result = self.__image_module.search( self.__form.get_input_value("term")) print(result) return JsonResponse(self.__response.send_private_success([], {})) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong with your host!") }]))
def remove_image_by_id(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print( _image.remove_by_id(configs["image_id"], configs["force"] == "True"))
class Get_Images(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __image_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__image_module = Image_Module() self.__logger = self.__helpers.get_logger(__name__) def get(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) if self.__image_module.set_host(self.__host_id).check_health(): _host = self.__host_module.get_one_by_id(host_id) return JsonResponse( self.__response.send_private_success( [], { 'images': self.__format_image(self.__image_module.list(), host_id, _host.slug) })) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong with your host!") }], {'images': []})) def __format_image(self, images_list, host_id, host_slug): _image_list = [] for image in images_list: date = image["created"].split("T") image["created_at"] = date[0] image["size"] = image["size"]["MB"] _tags = [] for tag in image["tags"]: tag = tag.split(":") _tags.append({"name": tag[0], "version": tag[1]}) image["tags"] = _tags image["url"] = reverse("app.web.admin.hosts.view.image", kwargs={ 'host_slug': host_slug, 'image_id': image['long_id'] }) image["delete_url"] = reverse( "app.api.private.v1.admin.action.host.delete_image.endpoint", kwargs={'host_id': host_id}) _image_list.append(image) return _image_list
def pull_image(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print(_image.pull(configs["repository"], configs["tag"]))
def build_image(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print( _image.build(File().read(configs["dockerfile"]), configs["tag"]))
def tag_image_by_id(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print( _image.tag_by_id(configs["image_id"], configs["repository"], configs["tag"]))
def get_image_by_id(self, configs={}): _image = Image_Module() if _image.set_host(configs["host_id"]).check_health(): print(_image.get_by_id(configs["image_id"]))