def print_all_settings(): for path in settings.uconfig().default_order: if path.strip() == '': continue if path not in settings.uconfig().public_options: continue value = settings.uconfig().data.get(path, '').replace('\n', ' ') label = settings.uconfig().labels.get(path, '') info = settings.uconfig().infos.get(path, '') print ' %s %s' % (path.ljust(50), value.ljust(20)) return 0
def cmd_set_directly(opts, args, overDict): def print_all_settings(): for path in settings.uconfig().default_order: if path.strip() == '': continue if path not in settings.uconfig().public_options: continue value = settings.uconfig().data.get(path, '').replace('\n', ' ') label = settings.uconfig().labels.get(path, '') info = settings.uconfig().infos.get(path, '') print ' %s %s' % (path.ljust(50), value.ljust(20)) return 0 name = args[1].lower() if name in [ 'list' ]: return print_all_settings() path = '' if len(args) < 2 else args[1] path = option_name_to_path(name, path) if path != '': old_is = settings.uconfig().get(path) if len(args) > 2: value = ' '.join(args[2:]) settings.uconfig().set(path, unicode(value)) settings.uconfig().update() print ' XML path: %s' % path print ' label: %s' % settings.uconfig().get(path, 'label') print ' info: %s' % settings.uconfig().get(path, 'info') print ' value: %s' % settings.uconfig().get(path) if len(args) > 2: print ' modified: [%s]->[%s]' % (old_is, value) return 0
def cmd_register(opts, args, overDict): if len(args) < 2: return 2 if len(args) >= 3: import lib.settings as settings settings.uconfig().set('backup.private-key-size', str(args[2])) settings.uconfig().update() import lib.automat as automat import initializer import shutdowner initializer.A('run-cmd-line-register', args[1]) reactor.run() shutdowner.A('reactor-stopped') automat.objects().clear() print return 0
def SendSettings(doAck=False, packetID=None): """ Say what eccmap we are using for recovery info. How many suppliers we want (probably same as used by eccmap but just in case). Say how much disk we are donating now and home much we need from our suppliers. The response should be an Ack packet from Central server and also it can sent us a new lsit of suppliers with ListContacts packet. """ if packetID is None: packetID = packetid.UniqueID() sdict = {} sdict['s'] = str(settings.getCentralNumSuppliers()) # donated = DiskSpace(s=settings.getCentralMegabytesDonated()) # needed = DiskSpace(s=settings.getCentralMegabytesNeeded()) # sdict['d'] = str(donated.getValueMb()) # sdict['n'] = str(needed.getValueMb()) sdict['d'] = str(diskspace.GetMegaBytesFromString(settings.getCentralMegabytesDonated())) sdict['n'] = str(diskspace.GetMegaBytesFromString(settings.getCentralMegabytesNeeded())) sdict['e'] = settings.getECC() sdict['p'] = str(settings.BasePricePerGBDay()) sdict['e1'] = str(settings.getEmergencyEmail()) sdict['e2'] = str(settings.getEmergencyPhone()) sdict['e3'] = str(settings.getEmergencyFax()) sdict['e4'] = str(settings.getEmergencyOther()).replace('\n', '<br>') sdict['mf'] = settings.getEmergencyFirstMethod() sdict['ms'] = settings.getEmergencySecondMethod() sdict['ie'] = misc.readExternalIP() sdict['il'] = misc.readLocalIP() sdict['nm'] = str(settings.uconfig('personal.personal-name')) sdict['sn'] = str(settings.uconfig('personal.personal-surname')) sdict['nn'] = str(settings.uconfig('personal.personal-nickname')) sdict['bt'] = str(settings.uconfig('personal.personal-betatester')) i = 0 for idurl in contacts.getCorrespondentIDs(): sdict['f%03d' % i] = idurl i += 1 data = dhnio._pack_dict(sdict) pid = send2central(commands.Settings(), data, doAck, packetID) dhnio.Dprint(4, "central_service.SendSettings PacketID=[%s]" % pid) return pid
def doSaveUpdates(self, arg): shedule = dhnupdate.blank_shedule(arg) settings.uconfig().set("updates.updates-shedule", dhnupdate.shedule_to_string(shedule)) settings.uconfig().update()
def doSaveContacts(self, arg): settings.uconfig().set("emergency.emergency-email", arg.get("email", "").strip()) settings.uconfig().set("personal.personal-name", arg.get("name", "")) settings.uconfig().set("personal.personal-surname", arg.get("surname", "")) settings.uconfig().set("personal.personal-nickname", arg.get("nickname", "")) if self.role == "BETA_TEST": settings.uconfig().set("personal.personal-betatester", "True") if self.role_args and self.role_args.get("development", "").lower() == "true": settings.uconfig().set("logs.debug-level", "10") settings.uconfig().set("logs.stream-enable", "True") dhnio.SetDebug(10) settings.uconfig().update()
def doSaveStorage(self, arg): needed = arg.get("needed", "") donated = arg.get("donated", "") customersdir = arg.get("customersdir", "") localbackupsdir = arg.get("localbackupsdir", "") restoredir = arg.get("restoredir", "") if needed: settings.uconfig().set("central-settings.needed-megabytes", needed + "MB") if donated: settings.uconfig().set("central-settings.shared-megabytes", donated + "MB") if customersdir: settings.uconfig().set("folder.folder-customers", customersdir) if localbackupsdir: settings.uconfig().set("folder.folder-backups", localbackupsdir) if restoredir: settings.uconfig().set("folder.folder-restore", restoredir) if self.role == "MOST_SECURE": settings.uconfig().set("general.general-local-backups-enable", "False") settings.uconfig().update()
def doRestoreSave(self, arg): settings.uconfig().set('central-settings.desired-suppliers', '0') settings.uconfig().set('central-settings.needed-megabytes', '0Mb') settings.uconfig().set('central-settings.shared-megabytes', '0Mb') settings.uconfig().update()
def run(opts, args, overDict, pars): """ The entry point, this is called from `p2p.dhnmain` to process command line arguments. """ print 'Copyright 2006, DataHaven.NET LTD. of Anguilla. All rights reserved.' if overDict: settings.override_dict(overDict) dhnio.init() settings.init() if not opts or opts.debug is None: dhnio.SetDebug(0) appList = dhnio.find_process([ 'dhnmain.exe', 'dhnmain.py', 'dhn.py', 'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$', ]) running = len(appList) > 0 cmd = '' if len(args) > 0: cmd = args[0].lower() #---help--- if cmd in ['help', 'h']: import help if len(args) >= 2 and args[1].lower() == 'schedule': print help.schedule_format() elif len(args) >= 2 and args[1].lower() == 'settings': print settings.uconfig().print_all() else: print help.help() print pars.format_option_help() return 0 #---backup--- elif cmd in ['backup', 'backups', 'bk']: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_backups(opts, args, overDict) #---restore--- elif cmd in ['restore', 're']: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_restore(opts, args, overDict) #---schedule--- elif cmd in ['schedule', 'shed', 'sched', 'sh']: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_schedule(opts, args, overDict) #---suppliers--- elif cmd in [ 'suppliers', 'supplier', 'sup', 'supp', 'sp', ]: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_suppliers(opts, args, overDict) #---customers--- elif cmd in [ 'customers', 'customer', 'cus', 'cust', 'cs', ]: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_customers(opts, args, overDict) #---register--- elif cmd == 'register': if running: print 'DataHaven.NET already started.\n' return 0 return cmd_register(opts, args, overDict) #---recover--- elif cmd == 'recover': if running: print 'DataHaven.NET already started.\n' return 0 return cmd_recover(opts, args, overDict) #---key--- elif cmd == 'key': return cmd_key(opts, args, overDict) #---stats--- elif cmd in [ 'stats', 'st' ]: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_stats(opts, args, overDict) #---version--- elif cmd in [ 'version', 'v', 'ver' ]: revnum = dhnio.ReadTextFile(settings.RevisionNumberFile()).strip() repo, location = misc.ReadRepoLocation() print 'revision: ', revnum print 'repository:', repo return 0 #---states--- elif cmd in [ 'states', 'sta', 'automats', 'auto' ]: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_states(opts, args, overDict) #---reconnect--- elif cmd in [ 'reconnect', ]: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_reconnect(opts, args, overDict) #---set--- elif cmd == 'set': if len(args) == 1 or args[1].lower() in [ 'help', '?' ]: import help print help.settings_help() return 0 if not running: cmd_set_directly(opts, args, overDict) return 0 return cmd_set_request(opts, args, overDict) #---memory--- elif cmd == 'memory': if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_memory(opts, args, overDict) #---money--- elif cmd == 'money': if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_money(opts, args, overDict) elif cmd == 'storage': if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_storage(opts, args, overDict) # elif cmd == 'uninstall': # return cmd_uninstall(opts, args, overDict) return 2
def run(UI='', options=None, args=None, overDict=None): """ In the method `main()` program firstly checks the command line arguments and then calls this method to start the whole process. This initialize some low level modules and finally create an instance of `initializer()` state machine and send it an event "run". """ import lib.dhnio as dhnio dhnio.Dprint(6, 'dhnmain.run sys.path=%s' % str(sys.path)) #---USE_TRAY_ICON--- try: from dhnicon import USE_TRAY_ICON dhnio.Dprint(4, 'dhnmain.run USE_TRAY_ICON='+str(USE_TRAY_ICON)) if dhnio.Linux() and not dhnio.X11_is_running(): USE_TRAY_ICON = False if USE_TRAY_ICON: from twisted.internet import wxreactor wxreactor.install() except: USE_TRAY_ICON = False dhnio.DprintException() if USE_TRAY_ICON: if dhnio.Linux(): icons_dict = { 'red': 'icon-red-24x24.png', 'green': 'icon-green-24x24.png', 'gray': 'icon-gray-24x24.png', } else: icons_dict = { 'red': 'icon-red.png', 'green': 'icon-green.png', 'gray': 'icon-gray.png', } import dhnicon icons_path = str(os.path.abspath(os.path.join(dhnio.getExecutableDir(), 'icons'))) dhnio.Dprint(4, 'dhnmain.run call dhnicon.init(%s)' % icons_path) dhnicon.init(icons_path, icons_dict) def _tray_control_func(cmd): if cmd == 'exit': #import dhninit #dhninit.shutdown_exit() import shutdowner shutdowner.A('stop', 'exit') dhnicon.SetControlFunc(_tray_control_func) dhnio.Dprint(4, 'dhnmain.run want to import twisted.internet.reactor') try: from twisted.internet import reactor except: dhnio.DprintException() sys.exit('Error initializing reactor in dhnmain.py\n') #---settings--- import lib.settings as settings if overDict: settings.override_dict(overDict) settings.init() if not options or options.debug is None: dhnio.SetDebug(settings.getDebugLevel()) #---logfile---- if dhnio.EnableLog and dhnio.LogFile is not None: dhnio.Dprint(2, 'dhnmain.run want to switch log files') if dhnio.Windows() and dhnio.isFrozen(): dhnio.StdOutRedirectingStop() dhnio.CloseLogFile() dhnio.OpenLogFile(settings.MainLogFilename()+'-'+time.strftime('%y%m%d%H%M%S')+'.log') if dhnio.Windows() and dhnio.isFrozen(): dhnio.StdOutRedirectingStart() #---memdebug--- if settings.uconfig('logs.memdebug-enable') == 'True': try: import lib.memdebug as memdebug memdebug_port = int(settings.uconfig('logs.memdebug-port')) memdebug.start(memdebug_port) reactor.addSystemEventTrigger('before', 'shutdown', memdebug.stop) dhnio.Dprint(2, 'dhnmain.run memdebug web server started on port %d' % memdebug_port) except: dhnio.DprintException() #---process ID--- try: pid = os.getpid() pid_file_path = os.path.join(settings.MetaDataDir(), 'processid') dhnio.WriteFile(pid_file_path, str(pid)) dhnio.Dprint(2, 'dhnmain.run wrote process id [%s] in the file %s' % (str(pid), pid_file_path)) except: dhnio.DprintException() # #---reactor.callLater patch--- # if dhnio.Debug(12): # patchReactorCallLater(reactor) # monitorDelayedCalls(reactor) dhnio.Dprint(2,"dhnmain.run UI=[%s]" % UI) if dhnio.Debug(10): dhnio.Dprint(0, '\n' + dhnio.osinfofull()) dhnio.Dprint(4, 'dhnmain.run import automats') #---START!--- import lib.automat as automat automat.LifeBegins(dhnio.LifeBeginsTime) automat.OpenLogFile(settings.AutomatsLog()) import initializer import shutdowner dhnio.Dprint(4, 'dhnmain.run send event "run" to initializer()') #reactor.callLater(0, initializer.A, 'run', UI) initializer.A('run', UI) #reactor.addSystemEventTrigger('before', 'shutdown', lambda : initializer.A('reactor-stopped')) dhnio.Dprint(2, 'dhnmain.run calling reactor.run()') reactor.run() dhnio.Dprint(2, 'dhnmain.run reactor stopped') # this will call initializer() without reactor.callLater(0, ... ) # we do not have any timers initializer() so do not worry #initializer.A('reactor-stopped', use_reactor = False) shutdowner.A('reactor-stopped') dhnio.Dprint(2, 'dhnmain.run finished, EXIT') automat.objects().clear() automat.CloseLogFile() ## import threading ## dhnio.Dprint(0, 'threads:') ## for t in threading.enumerate(): ## dhnio.Dprint(0, ' '+str(t)) dhnio.CloseLogFile() if dhnio.Windows() and dhnio.isFrozen(): dhnio.StdOutRedirectingStop() return 0
def _saveRequestedSettings(self, newpacket): sd = dhnio._unpack_dict(newpacket.Payload) settings.uconfig().set('central-settings.needed-megabytes', sd.get('n', str(settings.DefaultNeededMb()))+'MB') settings.uconfig().set('central-settings.shared-megabytes', sd.get('d', str(settings.DefaultDonatedMb()))+'MB') settings.uconfig().set('central-settings.desired-suppliers', sd.get('s', '2')) settings.uconfig().set('emergency.emergency-email', sd.get('e1', '')) settings.uconfig().set('emergency.emergency-phone', sd.get('e2', '')) settings.uconfig().set('emergency.emergency-fax', sd.get('e3', '')) settings.uconfig().set('emergency.emergency-text', sd.get('e4', '').replace('<br>', '\n')) settings.uconfig().update() reactor.callLater(0, self.automat, 'request-settings-ack', newpacket)