forked from xenserver/xsconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XSConsoleState.py
150 lines (121 loc) · 4.92 KB
/
XSConsoleState.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Copyright (c) 2007-2009 Citrix Systems Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import re, os, pickle
from XSConsoleBases import *
from XSConsoleLang import *
from XSConsoleLog import *
class State:
instance = None
savePath = '/etc/xsconsole'
saveLeafname = 'state.txt'
thisVersion = 9
#***
#*** Increment thisVersion (above) when adding attributes to this object
#***
def __init__(self):
self.version = self.thisVersion
self.authTimeoutSeconds = 5*60
self.passwordChangeRequired = False # IsPasswordSet now takes care of this
self.modified = True
self.rebootMessage = None
self.weStoppedXAPI = False
self.verboseBoot = False
self.keymap = None
self.sleepSeconds = 30*60
@classmethod
def SaveFilename(self):
return self.savePath+'/'+self.saveLeafname
@classmethod
def Inst(cls):
# Load the saved state if we can, otherwise create a default object
if cls.instance is None:
isFirstBoot = True
try:
if os.path.isfile(cls.SaveFilename()):
saveFile = open(cls.SaveFilename(), "r")
unpickler = pickle.Unpickler(saveFile)
cls.instance = unpickler.load()
saveFile.close()
isFirstBoot = False
if cls.instance.version != cls.instance.thisVersion:
# Version mismatch - don't use the state information
cls.instance = None
XSLog('State file version mismatch - discarding')
except Exception, e:
cls.instance = None
if cls.instance is None:
cls.instance = State()
XSLog('No saved state available - using default state')
# Fill in pseudo-state
cls.instance.isFirstBoot = isFirstBoot
cls.instance.MakeSane()
return cls.instance
def AuthTimeoutSeconds(self):
return self.authTimeoutSeconds
def PasswordChangeRequired(self):
return self.passwordChangeRequired
def PasswordChangeRequiredSet(self, inValue):
self.passwordChangeRequired = inValue
self.modified = True
def RebootMessage(self):
return self.rebootMessage
def RebootMessageSet(self, inValue):
self.rebootMessage = inValue
self.modified = True
def VerboseBoot(self):
return self.verboseBoot
def VerboseBootSet(self, inValue):
self.verboseBoot = inValue
self.modified = True
def Keymap(self):
return self.keymap
def KeymapSet(self, inValue):
self.keymap = inValue
self.modified = True
def IsFirstBoot(self):
return self.isFirstBoot
def WeStoppedXAPI(self):
return self.weStoppedXAPI
def WeStoppedXAPISet(self, inValue):
self.weStoppedXAPI = inValue
self.modified = True
def AuthTimeoutSecondsSet(self, inSeconds): # Don't call this directly - use Auth.TimeoutSecondsSet
if inSeconds < 60:
raise Exception("Cannot set a session timeout of less than one minute")
if self.authTimeoutSeconds != inSeconds:
self.authTimeoutSeconds = inSeconds
self.modified = True
def AuthTimeoutMinutes(self):
return int((self.AuthTimeoutSeconds() + 30) / 60)
def SleepSeconds(self):
return self.sleepSeconds
def MakeSane(self):
self.authTimeoutSeconds = int(self.authTimeoutSeconds)
if self.authTimeoutSeconds < 60:
AuthTimeoutSecondsSet(60)
def SaveIfRequired(self):
if self.modified:
self.MakeSane()
try:
if not os.path.isdir(self.savePath):
os.mkdir(self.savePath, 0700)
saveFile = open(self.SaveFilename(), "w")
pickler = pickle.Pickler(saveFile)
self.modified = False # Set unmodified before saving
pickler.dump(self)
saveFile.close()
XSLog('Saved state file')
except Exception, e:
XSLogFailure('Failed to save state file', e)