def run(self): try: device_mapping_list = utils.get_device_mappings(self.conf) except exceptions.AgentExecutableException as ex: return plugin_base.PluginReply(1, message=str(ex)) return plugin_base.PluginReply(0, reply_type="device_mapping_array", reply_object=device_mapping_list)
def mount_block_volume(self): if not self.args.devices: return 0 if len(self.args.devices) > 1 and\ self.args.raidLevel.upper() == "NONE": raise plugin_exceptions.AgentPluginException( "Must specify a RAID volume with mounting multiple devices at " "once.") if len(self.args.devices) > 1: target_device = "md0" else: target_device = self.args.devices[0] _g_logger.debug("target device is " + target_device) td = target_device if self.args.encryptedFsEncryptionKey is not None: encrypted_device = "es" + target_device td = encrypted_device self._install_deps(_g_platform_dep_crypto_installer) device_mappings = utils.get_device_mappings(self.conf) for mapping in device_mappings: if mapping["device_id"] == td: return 0 if len(self.args.devices) > 1: self.configure_raid(target_device) if self.args.formatVolume: if self.args.encryptedFsEncryptionKey: key_file_path = None try: key_file_path = self.write_key_file(True) self.setup_encryption(target_device, encrypted_device, key_file_path) utils.open_encrypted_device(self.conf, target_device, encrypted_device, key_file_path) target_device = encrypted_device finally: if key_file_path: utils.safe_delete(key_file_path) self.format(target_device) elif self.args.encryptedFsEncryptionKey: key_file_path = None try: key_file_path = self.write_key_file(True) utils.open_encrypted_device(self.conf, target_device, encrypted_device, key_file_path) target_device = encrypted_device finally: plugin_utils.safe_delete(key_file_path) utils.mount(self.conf, target_device, self.args.fileSystem, self.args.mountPoint) return 0
def test_mount_encryption(self): enc_str = "ENCRYPTED_FILE_ENV" if enc_str not in os.environ: raise unittest.SkipTest("set %s to try encryption tests" % enc_str) enc_key = os.environ["ENCRYPTED_FILE_ENV"] mount_point = os.path.join(tempfile.mkdtemp(), "mnt") device_id = "sdb" mappings = utils.get_device_mappings(self.conf_obj) for dm in mappings: if dm['device_id'] == device_id: if dm['mount_point'] != mount_point: raise Exception("The device is already mounted") doc = { "command": "mount_volume", "arguments": { "formatVolume": True, "fileSystem": "ext3", "raidLevel": "NONE", "encryptedFsEncryptionKey": base64.b64encode(enc_key.encode()).decode(), "mountPoint": mount_point, "devices": [device_id] } } req_rpc = self._rpc_wait_reply(doc) r = req_rpc.get_reply() jd = r["payload"]["reply_object"] while jd["job_status"] in ["WAITING", "RUNNING"]: jd = self._get_job_description(jd["job_id"]) self.assertEqual(jd["job_status"], "COMPLETE") arguments = {"deviceId": "es" + device_id, "encrypted": True} doc = {"command": "unmount_volume", "arguments": arguments} req_reply = self._rpc_wait_reply(doc) r = req_reply.get_reply() self.assertEqual(r["payload"]["return_code"], 0)
def run(self): device_mappings = utils.get_device_mappings(self.conf) platform = self.conf.platform_name try: device_id = _check_if_device_encrypted(self.args.deviceId, device_mappings) (d_id, mount_point, encrypted) = _find_device_to_unmount(self.conf, device_id, platform, device_mappings) if "No such volume" in mount_point: return plugin_base.PluginReply(1, reply_type="void", error_message=mount_point) self.unmount(mount_point) if encrypted: _g_logger.info("Attempting to close encrypted device %s" % d_id) _close_encrypted_device(self.conf, d_id) return plugin_base.PluginReply(0, reply_type="void") except exceptions.AgentExecutableException as aex: return plugin_base.PluginReply(1, reply_type="void", error_message=str(aex))
def test_mount_variety(self): mount_point = os.path.join(tempfile.mkdtemp(), "mnt") if os.path.exists("/dev/sdb"): device_id = "sdb" elif os.path.exists("/dev/hdb"): device_id = "hdb" else: raise unittest.SkipTest("No second drive was found") mappings = utils.get_device_mappings(self.conf_obj) for dm in mappings: if dm['device_id'] == device_id: os.system("umount " + dm['mount_point']) print(device_id) mappings = utils.get_device_mappings(self.conf_obj) for dm in mappings: if dm['device_id'] == device_id: if dm['mount_point'] != mount_point: raise unittest.SkipTest("The device is already mounted") doc = { "command": "mount_volume", "arguments": { "formatVolume": True, "fileSystem": "ext3", "raidLevel": "NONE", "encryptedFsEncryptionKey": None, "mountPoint": mount_point, "devices": [device_id] } } print("trying to mount") req_rpc = self._rpc_wait_reply(doc) r = req_rpc.get_reply() jd = r["payload"]["reply_object"] while jd["job_status"] in ["WAITING", "RUNNING"]: jd = self._get_job_description(jd["job_id"]) self.assertEqual(jd["job_status"], "COMPLETE") print("done mount") found = False mappings = utils.get_device_mappings(self.conf_obj) for dm in mappings: if dm['device_id'] == device_id: found = True self.assertEqual(dm['mount_point'], mount_point) self.assertTrue(found) arguments = { "deviceId": device_id, } doc = {"command": "unmount_volume", "arguments": arguments} req_reply = self._rpc_wait_reply(doc) r = req_reply.get_reply() self.assertEqual(r["payload"]["return_code"], 0) mount_point = os.path.join(tempfile.mkdtemp(), "mnt2") doc = { "command": "mount_volume", "arguments": { "formatVolume": True, "fileSystem": "ext3", "raidLevel": "NONE", "encryptedFsEncryptionKey": None, "mountPoint": mount_point, "devices": [device_id] } } req_rpc = self._rpc_wait_reply(doc) r = req_rpc.get_reply() jd = r["payload"]["reply_object"] while jd["job_status"] in ["WAITING", "RUNNING"]: jd = self._get_job_description(jd["job_id"]) self.assertEqual(jd["job_status"], "COMPLETE") print("done format") arguments = { "deviceId": device_id, } doc = {"command": "unmount_volume", "arguments": arguments} req_reply = self._rpc_wait_reply(doc) r = req_reply.get_reply() self.assertEqual(r["payload"]["return_code"], 0)