def copy_image(self, img1, dest_project, img2=None): """ Create a deep copy of src image :param img1: Name of src image :param dest_project: Name of the project where des image will be created :param img2: Name of des image :return: True on successful completion """ try: if not self.is_admin and (self.proj != dest_project): raise AuthorizationFailedException() dest_pid = self.__does_project_exist(dest_project) self.db.image.copy_image(self.proj, img1, dest_pid, img2) if img2 is not None: ceph_name = self.get_ceph_image_name_from_project( img2, dest_project) else: ceph_name = self.get_ceph_image_name_from_project( img1, dest_project) self.fs.clone( self.get_ceph_image_name_from_project(img1, self.proj), constants.DEFAULT_SNAPSHOT_NAME, ceph_name) self.fs.flatten(ceph_name) self.fs.snap_image(ceph_name, constants.DEFAULT_SNAPSHOT_NAME) self.fs.snap_protect(ceph_name, constants.DEFAULT_SNAPSHOT_NAME) return self.__return_success(True) except (DBException, FileSystemException) as e: logger.exception('') return self.__return_error(e)
def list_projects(self): try: if not self.is_admin: raise AuthorizationFailedException() projects = self.db.project.fetch_projects() return self.__return_success(projects) except (DBException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def delete_project(self, project): try: if not self.is_admin: raise AuthorizationFailedException() self.db.project.delete_with_name(project) return self.__return_success(True) except (DBException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def add_project(self, project, network, id): try: if not self.is_admin: raise AuthorizationFailedException() self.db.project.insert(project, network, id) return self.__return_success(True) except (DBException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def unmap_image(self, rbd_name): try: if not self.is_admin: raise AuthorizationFailedException() self.fs.unmap(rbd_name) return self.__return_success(True) except (FileSystemException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def umount_image(self, img): try: if not self.is_admin: raise AuthorizationFailedException() ceph_img_name = self.__get_ceph_image_name(img) self.iscsi.remove_target(ceph_img_name) return self.__return_success(True) except (ISCSIException, DBException) as e: logger.exception('') return self.__return_error(e)
def move_image(self, img1, dest_project, img2): try: if not self.is_admin and (self.proj != dest_project): raise AuthorizationFailedException() dest_pid = self.__does_project_exist(dest_project) self.db.image.move_image(self.proj, img1, dest_pid, img2) return self.__return_success(True) except DBException as e: logger.exception('') return self.__return_error(e)
def map_image(self, img): try: if not self.is_admin: raise AuthorizationFailedException() ceph_img_name = self.__get_ceph_image_name(img) rbd_name = self.fs.map(ceph_img_name) return self.__return_success(rbd_name) except (FileSystemException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def unmount_mapped_image(self, mount_path): try: if not self.is_admin: raise AuthorizationFailedException() command = "umount " + mount_path shell.call(command, sudo=True) os.rmdir(mount_path) return self.__return_success(True) except (shell_exceptions.CommandFailedException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def add_image(self, project, img, id, snap, parent, public): try: if not self.is_admin: raise AuthorizationFailedException() parent_id = None if parent is not None: parent_id = self.db.image.fetch_id_with_name_from_project( parent, project) pid = self.__does_project_exist(project) self.db.image.insert(img, pid, parent_id, public, snap, id) return self.__return_success(True) except (DBException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def show_mounted(self): try: if not self.is_admin: raise AuthorizationFailedException() mappings = self.iscsi.list_targets() swapped_mappings = {} for k, v in mappings.iteritems(): img_id = self.__extract_id(k) if self.proj == self.db.image.fetch_project_with_id(img_id): swapped_mappings[self.db.image.fetch_name_with_id( img_id)] = v return self.__return_success(swapped_mappings) except (ISCSIException, DBException) as e: logger.exception('') return self.__return_error(e)
def vulnerability_detection(self, mount_path): try: if not self.is_admin: raise AuthorizationFailedException() crawler_path = "/root/ims_latest_new/ims/common/agentless-system-crawler/crawler/crawler.py" url_path = "/root/ims_latest_new/ims/common/crawler_output/test.csv" command = "python " + crawler_path + " --features os,package --crawlmode MOUNTPOINT --mountpoint " + mount_path + " --url file://" + url_path csv_frame = shell.call(command, sudo=True) json_frame = csv2json(url_path + ".0") report = readFrame(json_frame) return self.__return_success(report) except (shell_exceptions.CommandFailedException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def mount_mapped_image(self, rbd_map_path, mount_path): try: if not self.is_admin: raise AuthorizationFailedException() try: os.stat(mount_path) except: os.mkdir(mount_path) command = "mount " + rbd_map_path + " " + mount_path shell.call(command, sudo=True) return self.__return_success(True) except (shell_exceptions.CommandFailedException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)
def copy_image(self, img1, dest_project, img2=None): try: if not self.is_admin and (self.proj != dest_project): raise AuthorizationFailedException() dest_pid = self.__does_project_exist(dest_project) self.db.image.copy_image(self.proj, img1, dest_pid, img2) if img2 is not None: ceph_name = self.__get_ceph_image_name(img2, dest_project) else: ceph_name = self.__get_ceph_image_name(img1, dest_project) self.fs.clone(self.__get_ceph_image_name(img1, self.proj), constants.DEFAULT_SNAPSHOT_NAME, ceph_name) self.fs.snap_image(ceph_name, constants.DEFAULT_SNAPSHOT_NAME) self.fs.snap_protect(ceph_name, constants.DEFAULT_SNAPSHOT_NAME) return self.__return_success(True) except (DBException, FileSystemException) as e: logger.exception('') return self.__return_error(e)
def bmi_introspect(self, node): try: report = None if not self.is_admin: raise AuthorizationFailedException() snap_name = node + "_snap" print("snap name = ", snap_name) print("node name = ", node) snap_res = self.create_snapshot(node, snap_name) print(snap_res) if snap_res[constants.STATUS_CODE_KEY] == 200: print('snapshot created successfully') # finding img file for this node list_img = [] list_ret = self.list_all_images() if list_ret[constants.STATUS_CODE_KEY] == 200: images = list_ret[constants.RETURN_VALUE_KEY] for image in images: if image[1] == node: list_img.append(image[1]) # this stores the ceph image name img = None if len(list_img) != 0: img = list_img[0] print('Got Ceph Image') map_ret = self.map_image(img) if map_ret[constants.STATUS_CODE_KEY] == 200: print('Image mapped successfully') rbd_name = map_ret[constants.RETURN_VALUE_KEY] rbd_mapped_path = rbd_name + 'p2' mount_path = '/mnt/' + node mount_ret = self.mount_mapped_image( rbd_mapped_path, mount_path) if mount_ret[constants.STATUS_CODE_KEY] == 200: print('Image mounted successfully') vd_ret = self.vulnerability_detection(mount_path) if vd_ret[constants.STATUS_CODE_KEY] == 200: print('Vulnerability Detection Successful') report = vd_ret[constants.RETURN_VALUE_KEY] else: print('error in vulnerability detection') unmount_ret = self.unmount_mapped_image(mount_path) if unmount_ret[constants.STATUS_CODE_KEY] == 200: print('Image unmounted successfully') else: print('Error in unmounting image') else: print('Error in mounting image') unmap_ret = self.unmap_image(rbd_name) if unmap_ret[constants.STATUS_CODE_KEY] == 200: print('Image unmapped successfully') else: print(unmap_ret[constants.MESSAGE_KEY]) else: print(map_ret[constants.MESSAGE_KEY]) del_snap_res = self.remove_image(snap_name) if del_snap_res[constants.STATUS_CODE_KEY] == 200: print('Snapshot has been deleted') else: print('error in deleting snapshot') else: print('error in snapshot creation') return self.__return_success(report) except (shell_exceptions.CommandFailedException, AuthorizationFailedException) as e: logger.exception('') return self.__return_error(e)