Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 97 lines (78 sloc) 3.075 kb
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
1 #!/usr/bin/env python
2 #
3 # Read in all the packages installed in dom0 and sum up the
4 # "memory-requirement-mb" attributes of the <repository> nodes. (Default
5 # is 0.) Then work out what memory-target *should* be and set it.
6 #
7 # To calculate memory-target we add these to (memory-static-min + 100MiB) and
8 # truncate this value to between memory-static-min and memory-static-max.
9
10
11 import XenAPI
12 import sys
13 import os
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
14 import time
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
15 import traceback
16 import xml.dom.minidom
17 from xml.parsers.expat import ExpatError
18
13b8348 @jonludlam Move everything over to using Fhs.ml / @ substitution
jonludlam authored
19 INSTALLED_REPOS_DIR = '@ETCDIR@/installed-repos'
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
20
21 def main():
22 session = XenAPI.xapi_local()
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
23 logged_in = False
6ba99fe @robhoes CA-65535: Make set-dom0-memory-target-from-packs more robust
robhoes authored
24 # read dom0 uuid out of inventory file
13b8348 @jonludlam Move everything over to using Fhs.ml / @ substitution
jonludlam authored
25 uuid = filter(lambda x: x.startswith('CONTROL_DOMAIN_UUID'), open('@INVENTORY@').readlines())[0].strip().split('=')[1].strip("'")
6ba99fe @robhoes CA-65535: Make set-dom0-memory-target-from-packs more robust
robhoes authored
26 oref = rec = None
27 for tries in range(180):
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
28 try:
29 session.xenapi.login_with_password("", "")
30 logged_in = True
6ba99fe @robhoes CA-65535: Make set-dom0-memory-target-from-packs more robust
robhoes authored
31
32 # get dom0's DB record
33 oref = session.xenapi.VM.get_by_uuid(uuid)
34 rec = session.xenapi.VM.get_record(oref)
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
35 break
36 except:
37 # repeat after a delay
38 time.sleep(1)
6ba99fe @robhoes CA-65535: Make set-dom0-memory-target-from-packs more robust
robhoes authored
39 if oref == None:
40 if logged_in:
41 session.xenapi.session.logout()
42 print >> sys.stderr, "Xapi is not ready. Aborting."
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
43 return 1
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
44
45 static_min = int(rec['memory_static_min'])
46 static_max = int(rec['memory_static_max'])
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
47 current_target = int(rec['memory_target'])
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
48
49 additional_mb = 0
50
51 packs = os.listdir(INSTALLED_REPOS_DIR)
52 for pack in packs:
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
53 try:
54 xmldoc = xml.dom.minidom.parse(os.path.join(INSTALLED_REPOS_DIR, pack, 'XS-REPOSITORY'))
55 additional_mb_str = xmldoc.documentElement.getAttribute('memory-requirement-mb')
56 if additional_mb_str:
57 additional_mb += int(additional_mb_str)
58 except:
59 pass
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
60
61 # new memory target
62 memory_target = static_min + (100<<20) + (additional_mb<<20)
63
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
64 print >> sys.stdout, "Memory required by all installed packages: %d" % memory_target
65
66 if memory_target <= current_target:
67 print >> sys.stdout, "Current target %d greater, skipping" % current_target
68 return 0
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
69
70 if memory_target > static_max:
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
71 print >> sys.stdout, "Truncating to static-max: %d" % static_max
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
72 memory_target = static_max
73
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
74 print >> sys.stdout, "Setting VM.memory_target: %d" % memory_target
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
75
76 # Next line commented out because this doesn't seem to work - maybe a bug in the Python bindings
77 #session.xenapi.VM.set_memory_target_live(oref, memory_target)
78
79 # Do this instead
80 import subprocess
81 subprocess.call(['xe','vm-memory-target-set', 'uuid=%s' % uuid, 'target=%d' % memory_target])
82
83 session.xenapi.session.logout()
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
84 return 0
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
85
86 if __name__ == '__main__':
87 rc = 1
88 try:
89 rc = main()
90 except:
91 ex = sys.exc_info()
92 err = traceback.format_exception(*ex)
93 for exline in err:
0e5c84f @srowe CA-37466: retry xapi login to handle transient errors
srowe authored
94 print >> sys.stderr, exline
d583680 @alexz CP-1302: Create tool to adjust dom0 memory limit
alexz authored
95
96 sys.exit(rc)
Something went wrong with that request. Please try again.