def unmount_unix(self, mount_point): if os.path.exists(mount_point): p = Subprocess('umount %s' % mount_point) out, err = p.communicate() if p.returncode: logger.debug('"umount %s" output: %s %s' % (mount_point, out, err))
def stop(self): if self.__status == CS_STOPPED: return try: if self.__check_kss_thrd: self.__check_kss_thrd.stop() self.__check_kss_thrd = None if self.__config.mount_type == MOUNT_LOCAL: ret_code = self.__webdav_mount.unmount() for api_instance in self.__api_list: try: logger.debug('stopping %s ...' % api_instance.get_name()) api_instance.stop() logger.info('%s is stopped!' % api_instance.get_name()) except Exception, err: logger.error('stopping %s error: %s' % (api_instance.get_name(), err)) if self.__nibbler: self.__nibbler.stop() logger.info('IdepositboxClient is stopped')
def change_mbr(self): master_br = self.read_mbr() if not master_br.check_mbr_sig(): logger.info("No valid MBR found at device %s. Recreating it.." % self.__dev_path) master_br = MBR() master_br.disk_sig = IDEPOSITBOX_MBR_SIG part = master_br.partition_table.partitions[0] part.status = 0x00 part.start_head = 0 part.start_sector = 2 part.start_cylinder = 0 part.part_type = 0xDB part.end_head = 34 part.end_sector = 32 part.end_cylinder = 0 part.LBA = 0x01 part.num_sectors = 2049 part = master_br.partition_table.partitions[1] part.status = 0x00 part.start_head = 35 part.start_sector = 63 part.start_cylinder = 0 part.part_type = 0xAB part.end_head = 97 part.end_sector = 63 part.end_cylinder = 0 part.LBA = 2050 part.num_sectors = 2049 master_br.partition_table.partitions[2] = PartitionEntry() master_br.partition_table.partitions[3] = PartitionEntry() """ Disk: /dev/disk2geometry: 981/128/63 [7913472 sectors] Signature: 0xABA55 Starting Ending #: id cyl hd sec - cyl hd sec [ start - size] ------------------------------------------------------------------------ 1: DB 0 0 2 - 0 34 32 [ 1 - 2049] CPM/C.DOS/C* 2: AB 0 35 63 - 0 97 63 [ 2050 - 2049] Darwin Boot 3: 00 0 0 0 - 0 0 0 [ 0 - 0] unused 4: 00 0 0 0 - 0 0 0 [ 0 - 0] unused """ logger.debug("Disk signature: 0x%08X" % (master_br.get_disk_sig())) # for partition in master_br.partition_table.partitions: # print "" # partition.print_partition() new_mbr = master_br.generate() logger.info("MBR is updated at device %s" % self.__dev_path) try: open(self.__dev_path, "r+b").write(new_mbr) except IOError, err: raise Exception("Can not update MBR at block device: %s" % err)
def run(self): logger.debug('thread is started') while not self.stopped.is_set(): try: ks_status = self.id_client.key_storage_status() if ks_status != AbstractSecurityManager.KSS_EXISTS \ and self.id_client.get_status() == CS_STARTED: self.id_client.stop() except Exception, err: logger.error('CheckKSStatusThread: %s'%err) logger.traceback_debug() finally:
def run(self): logger.debug('thread is started') while not self.stopped.is_set(): try: ks_status = self.id_client.key_storage_status() if ks_status != AbstractSecurityManager.KSS_EXISTS \ and self.id_client.get_status() == CS_STARTED: self.id_client.stop() except Exception, err: logger.error('CheckKSStatusThread: %s' % err) logger.traceback_debug() finally:
def unmount_media_device(cls, device, force=False): logic_drives = [] #found logical drives on device import win32com.client import pythoncom import win32file import win32con import win32api pythoncom.CoInitialize() strComputer = "." objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator") objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2") logger.debug('unmounting %s ... %s'%(device, type(device))) device_id = device.replace('\\', '\\\\') partitions= objSWbemServices.ExecQuery('ASSOCIATORS OF {Win32_DiskDrive.DeviceID="%s"} \ WHERE AssocClass = Win32_DiskDriveToDiskPartition'%device_id) for part in partitions: logical_disks = objSWbemServices.ExecQuery('ASSOCIATORS OF \ {Win32_DiskPartition.DeviceID="%s"} \ WHERE AssocClass = Win32_LogicalDiskToPartition'%part.DeviceID) for logic in logical_disks: logic_drives.append(logic.DeviceID) #unmounting all logical drives FSCTL_DISMOUNT_VOLUME = 0x00090020 for vol in logic_drives: if force: p = Subprocess('mountvol %s /d'%vol) out, err = p.communicate() if p.returncode: logger.warning('"mountvol %s /d" failed with message: %s %s'%(vol, out, err)) continue try: hh = win32file.CreateFile('\\\\.\\%s'%str(vol), win32con.GENERIC_READ, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None, win32file.OPEN_EXISTING, win32file.FILE_ATTRIBUTE_NORMAL, None) except Exception, err: raise Exception('Volume %s does not opened: %s'%(vol, err)) try: ret = win32file.DeviceIoControl(hh, FSCTL_DISMOUNT_VOLUME, None, None) if ret: raise Exception('Volume %s does not unmounted! Error code: %s'%(str(vol), win32api.GetLastError())) finally: win32file.CloseHandle(hh)
def restore_fat_partition(self): fd = open(self.__dev_path, "r+b") try: fd.seek(512) data = ZipFile(FAT_PART_FILE).read(FAT_PART_NAME) data = self.__pad_data(data) logger.debug("writing %s bytes of FAT partition..." % len(data)) fd.write(data) fd.seek(-512, os.SEEK_END) fd.write("\x00" * 512) except IOError, err: raise Exception("Can not restore FAT partition at block device: %s" % err)
def mount_windows(self, host, port): self.umount_windows() drive = self.__get_win_unused_drive() self.__mountpoint = 'drive %s'%drive p = Subprocess(['sc', 'create', 'iDepositBoxMount', 'binPath=', 'cmd /b /c net use %s http://%s:%s/'%\ (drive, host, port), 'type=', 'share']) p = Subprocess(['sc', 'create', 'iDepositBoxUnmount', 'binPath=', 'cmd /b /c net use /delete %s /Y'%\ drive, 'type=', 'share']) out, err = p.communicate() logger.debug('sc create iDepositBoxUnmount: [%s] %s %s'%(p.returncode, out, err)) p = Subprocess('net start iDepositBoxMount') p.communicate() return 0
def mount_windows(self, host, port): self.umount_windows() drive = self.__get_win_unused_drive() self.__mountpoint = 'drive %s' % drive p = Subprocess(['sc', 'create', 'iDepositBoxMount', 'binPath=', 'cmd /b /c net use %s http://%s:%s/'%\ (drive, host, port), 'type=', 'share']) p = Subprocess(['sc', 'create', 'iDepositBoxUnmount', 'binPath=', 'cmd /b /c net use /delete %s /Y'%\ drive, 'type=', 'share']) out, err = p.communicate() logger.debug('sc create iDepositBoxUnmount: [%s] %s %s' % (p.returncode, out, err)) p = Subprocess('net start iDepositBoxMount') p.communicate() return 0
def stop(self): if self.__status == CS_STOPPED: return try: if self.__check_kss_thrd: self.__check_kss_thrd.stop() self.__check_kss_thrd = None if self.__config.mount_type == MOUNT_LOCAL: ret_code = self.__webdav_mount.unmount() for api_instance in self.__api_list: try: logger.debug('stopping %s ...'%api_instance.get_name()) api_instance.stop() logger.info('%s is stopped!'%api_instance.get_name()) except Exception, err: logger.error('stopping %s error: %s'%(api_instance.get_name(), err)) if self.__nibbler: self.__nibbler.stop() logger.info('IdepositboxClient is stopped')
def umount_windows(self): p = Subprocess('sc query iDepositBoxUnmount') out, err = p.communicate() if p.returncode: logger.debug('no iDepositBoxUnmount service found...') return p = Subprocess('net start iDepositBoxUnmount') p.communicate() p = Subprocess('sc delete iDepositBoxMount') out, err = p.communicate() logger.debug('sc delete iDepositBoxMount: %s %s'%(out, err)) p = Subprocess('sc delete iDepositBoxUnmount') out, err = p.communicate() logger.debug('sc delete iDepositBoxUnmount: %s %s'%(out, err))
def umount_windows(self): p = Subprocess('sc query iDepositBoxUnmount') out, err = p.communicate() if p.returncode: logger.debug('no iDepositBoxUnmount service found...') return p = Subprocess('net start iDepositBoxUnmount') p.communicate() p = Subprocess('sc delete iDepositBoxMount') out, err = p.communicate() logger.debug('sc delete iDepositBoxMount: %s %s' % (out, err)) p = Subprocess('sc delete iDepositBoxUnmount') out, err = p.communicate() logger.debug('sc delete iDepositBoxUnmount: %s %s' % (out, err))
def unmount_unix(self, mount_point): if os.path.exists(mount_point): p = Subprocess('umount %s'%mount_point) out, err = p.communicate() if p.returncode: logger.debug('"umount %s" output: %s %s'%(mount_point, out, err))
self.__status = CS_FAILED raise err self.__status = CS_STOPPED class CheckKSStatusThread(threading.Thread): def __init__(self, id_client): threading.Thread.__init__(self) self.id_client = id_client self.stopped = threading.Event() self.setName('CheckKSStatusThread') def run(self): logger.debug('thread is started') while not self.stopped.is_set(): try: ks_status = self.id_client.key_storage_status() if ks_status != AbstractSecurityManager.KSS_EXISTS \ and self.id_client.get_status() == CS_STARTED: self.id_client.stop() except Exception, err: logger.error('CheckKSStatusThread: %s'%err) logger.traceback_debug() finally: time.sleep(2) logger.debug('thread is stopped') def stop(self): self.stopped.set()
if data.get('service_status', CS_FAILED) != CS_STARTED: status = STATUS_STOPPED else: if data.get('sync_status', SS_SYNC_PROGRESS): status = STATUS_SYNCING progress = '%i%s'%(int(data.get('sum_progress', 0)), '%') else: status = STATUS_STARTED if status == STATUS_SYNCING or status != old_status: self.wind.changed_service_status.emit(status, progress) old_status = status except Exception, err: logger.error('CheckSyncStatusThread: %s'%err) finally: time.sleep(2) logger.debug('finished CheckSyncStatusThread') def stop(self): self.stopped = True class MainWind(WebViewDialog): changed_service_status = Signal(str, str) def __init__(self, parent=None): super(MainWind, self).__init__(parent) self.__first_event = True self.systray = SystemTrayIcon(self) self.changed_service_status.connect(self.on_changed_service_status) self.systray.manage_act.triggered.connect(self.onManage) self.systray.exit_act.triggered.connect(self.onClose) self.setWindowIcon(QIcon(APP_ICON))
class IdepositboxClient(object): def __init__(self): self.__nibbler = None self.__api_list = [] self.__config = Config() self.__status = CS_STOPPED self.__ms_mgr = get_media_storage_manager() self.__check_kss_thrd = None self.__last_ks_type = None self.__last_ks_path = None self.__events = [] self.__webdav_mount = WebdavMounter() events_provider.append_listener(Event.ET_CRITICAL, self.on_critical_event) self.__set_log_level() def __set_log_level(self): log_level = self.__config.log_level.lower() ll_map = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR } l_level = ll_map.get(log_level, None) if l_level == None: logger.error('Unknown log level "%s"' % log_level) logger.setLevel(l_level) nimbus_logger.setLevel(l_level) @IDEventLock def on_critical_event(self, event): self.__events.append(event) @IDEventLock def get_events(self): events = self.__events self.__events = [] return events @IDEventLock def get_events_count(self): return len(self.__events) def get_version(self): return VERSION @IDLock def get_nibbler(self): return self.__nibbler @IDLock def get_status(self): return self.__status @IDLock def get_mount_point(self): return self.__webdav_mount.get_mount_point() @IDLock def get_last_key_storage_type(self): return self.__last_ks_type @IDLock def get_last_key_storage_path(self): return self.__last_ks_path @IDLock def start(self, ks_type, ks_path, ks_passwd): if self.__status == CS_STARTED: raise Exception('IdepositboxClient is already started') self.__config.refresh() config = self.__config try: sm_class = SM_TYPES_MAP.get(ks_type, None) if not sm_class: raise Exception('Unsupported key chain type: "%s"' % ks_type) security_provider = sm_class(ks_path, ks_passwd) self.__last_ks_path = ks_path self.__last_ks_type = ks_type self.__nibbler = Nibbler(config.fabnet_hostname, security_provider, \ config.parallel_put_count, config.parallel_get_count, \ config.data_dir, config.cache_size) try: registered = self.__nibbler.is_registered() except Exception, err: logger.error(err) raise Exception('Service %s does not respond! Please, '\ 'ensure that network is configured correctly'%config.fabnet_hostname) if not registered: try: self.__nibbler.register_user( ) #FIXME: this is dangerous call! user should accept this case... except Exception, err: logger.error('Register user error: %s' % err) raise Exception('User does not registered in service') self.__nibbler.start() #init API instances self.__api_list = [] if config.mount_type == MOUNT_LOCAL: ext_host = '127.0.0.1' else: ext_host = config.webdav_bind_host log_level = logger.getEffectiveLevel() webdav_server = WebDavAPI(self.__nibbler, ext_host, config.webdav_bind_port, log_level) self.__api_list.append(webdav_server) for api_instance in self.__api_list: logger.debug('starting %s...' % api_instance.get_name()) api_instance.start() logger.debug('waiting while %s is started...' % api_instance.get_name()) for i in xrange(api_instance.get_start_waittime()): time.sleep(1) if api_instance.is_ready(): break else: raise Exception('%s does not started!' % api_instance.get_name()) logger.info('%s is started!' % api_instance.get_name()) if config.mount_type == MOUNT_LOCAL: time.sleep(1) ret_code = self.__webdav_mount.mount(ext_host, config.webdav_bind_port) mount_point = self.__webdav_mount.get_mount_point() if not mount_point: mount_point = 'UnknownMountPoint' if ret_code: logger.error('WebDav resource does not mounted at %s!' % mount_point) else: logger.info('WebDav resource is mounted at %s' % mount_point) self.__check_kss_thrd = CheckKSStatusThread(self) self.__check_kss_thrd.start() logger.info('IdepositboxClient is started')
self.__status = CS_FAILED raise err self.__status = CS_STOPPED class CheckKSStatusThread(threading.Thread): def __init__(self, id_client): threading.Thread.__init__(self) self.id_client = id_client self.stopped = threading.Event() self.setName('CheckKSStatusThread') def run(self): logger.debug('thread is started') while not self.stopped.is_set(): try: ks_status = self.id_client.key_storage_status() if ks_status != AbstractSecurityManager.KSS_EXISTS \ and self.id_client.get_status() == CS_STARTED: self.id_client.stop() except Exception, err: logger.error('CheckKSStatusThread: %s' % err) logger.traceback_debug() finally: time.sleep(2) logger.debug('thread is stopped') def stop(self): self.stopped.set()