root / trunk / python / create-compute-node.py @ 40
Historique | Voir | Annoter | Télécharger (5,56 ko)
1 | 4 | storres | #! /usr/bin/python
|
---|---|---|---|
2 | 4 | storres | #
|
3 | 4 | storres | #
|
4 | 4 | storres | # Define a main function to allow for function declaration in any order.
|
5 | 5 | storres | # The main program lives in the main function.
|
6 | 4 | storres | def main(): |
7 | 4 | storres | # System modules
|
8 | 4 | storres | import sys |
9 | 5 | storres | import re |
10 | 5 | storres | import string |
11 | 5 | storres | import commands |
12 | 4 | storres | # Local modules (must live in the same directory).
|
13 | 4 | storres | import ciab_user_functions as cuf |
14 | 4 | storres | from ciab_common_vars import (commonVariables) |
15 | 4 | storres | #
|
16 | 4 | storres | # Variables
|
17 | 4 | storres | nodeNumberString = ''
|
18 | 5 | storres | nodeAddress = ''
|
19 | 5 | storres | templatesDir = ''
|
20 | 5 | storres | templatesDirName = '/../templates'
|
21 | 4 | storres | |
22 | 5 | storres | # Yep, the final comma is correct: it's a list of variables!
|
23 | 4 | storres | nodeNumberString, = usage(sys.argv) |
24 | 5 | storres | # Must be all digits.
|
25 | 5 | storres | if re.match("\D", nodeNumberString): |
26 | 5 | storres | print
|
27 | 5 | storres | print
|
28 | 5 | storres | print sys.argv[0], ': invalid node number "', nodeNumberString, '".' |
29 | 5 | storres | print 'Aborting!' |
30 | 5 | storres | print
|
31 | 5 | storres | print
|
32 | 5 | storres | sys.exit(1)
|
33 | 5 | storres | # Make sure the number is whithin the limits.
|
34 | 5 | storres | if int(nodeNumberString) >= int(commonVariables['COMPUTE_NODE_MAX_NUM']): |
35 | 5 | storres | print
|
36 | 5 | storres | print
|
37 | 5 | storres | print sys.argv[0], ': invalid node number "', nodeNumberString, '".' |
38 | 5 | storres | print 'Must be <', commonVariables['COMPUTE_NODE_MAX_NUM'], '.' |
39 | 5 | storres | print 'Aborting!'
|
40 | 5 | storres | print
|
41 | 5 | storres | print
|
42 | 5 | storres | sys.exit(1)
|
43 | 5 | storres | |
44 | 5 | storres | # Nomalize to three digits.
|
45 | 5 | storres | while len(nodeNumberString) < 3: |
46 | 5 | storres | nodeNumberString = '0' + nodeNumberString
|
47 | 5 | storres | print 'Node number string: ', nodeNumberString |
48 | 5 | storres | |
49 | 5 | storres | currentNodeName = commonVariables['COMPUTE_NODE_NAME_PREFIX'] + \
|
50 | 5 | storres | nodeNumberString |
51 | 5 | storres | # Get the templates directory.
|
52 | 5 | storres | templatesDir = commands.getoutput('dirname ' + sys.argv[0]) + templatesDirName |
53 | 5 | storres | |
54 | 5 | storres | # Check if the virtual machine we want to build is not already running.
|
55 | 5 | storres | isRunning = commands.getoutput('xm list | grep ' + currentNodeName)
|
56 | 5 | storres | #isRunning = commands.getoutput('xm list | grep ' + 'compute-node-model')
|
57 | 5 | storres | if isRunning != '': |
58 | 5 | storres | print
|
59 | 5 | storres | print
|
60 | 5 | storres | print sys.argv[0], ': VM "', currentHostName, '" is already running.' |
61 | 5 | storres | print 'Aborting!' |
62 | 5 | storres | print
|
63 | 5 | storres | print
|
64 | 5 | storres | sys.exit(1)
|
65 | 5 | storres | # Create the VM system disk (a snapshot of the compute node model disk).
|
66 | 5 | storres | computeNodeSystemDisk = commonVariables['SYSTEM_DISK_CLONE_PREFIX'] + \
|
67 | 5 | storres | nodeNumberString + \ |
68 | 5 | storres | commonVariables['SYSTEM_DISK_CLONE_POSTFIX']
|
69 | 5 | storres | commandString = 'lvcreate -L ' + commonVariables['SYSTEM_DISK_CLONE_SIZE'] + \ |
70 | 5 | storres | ' -s ' +\
|
71 | 5 | storres | ' -n ' + computeNodeSystemDisk + \
|
72 | 5 | storres | ' ' + commonVariables['SYSTEM_DISK_MASTER'] |
73 | 5 | storres | #print commandString
|
74 | 5 | storres | status = 0
|
75 | 5 | storres | # FIXME!!!!!!!!!!!!!!!!!!!!!
|
76 | 5 | storres | #status,output = commands.getstatusoutput(commandString)
|
77 | 5 | storres | if status != 0 : |
78 | 5 | storres | print
|
79 | 5 | storres | print
|
80 | 5 | storres | print sys.argv[0], ': could not create VM system disk.' |
81 | 5 | storres | print output
|
82 | 5 | storres | print 'Aborting!' |
83 | 5 | storres | print
|
84 | 5 | storres | print
|
85 | 5 | storres | sys.exit(1)
|
86 | 5 | storres | # Create the VM swap volume
|
87 | 5 | storres | computeNodeSwapDisk = commonVariables['SWAP_DISK_PREFIX'] + \
|
88 | 5 | storres | nodeNumberString + \ |
89 | 5 | storres | commonVariables['SWAP_DISK_POSTFIX']
|
90 | 5 | storres | print computeNodeSwapDisk
|
91 | 5 | storres | commandString = 'lvcreate -L ' + commonVariables['SWAP_DISK_SIZE'] + \ |
92 | 5 | storres | ' -n ' + computeNodeSwapDisk + \
|
93 | 5 | storres | ' ' + commonVariables['SWAP_DISK_LOGICAL_VOLUME'] |
94 | 5 | storres | print commandString
|
95 | 5 | storres | # FIXME!!!!!!!!!!!!!!!!!!!!!
|
96 | 5 | storres | #status,output = commands.getstatusoutput(commandString)
|
97 | 5 | storres | if status != 0 : |
98 | 5 | storres | print
|
99 | 5 | storres | print
|
100 | 5 | storres | print sys.argv[0], ': could not create the VM swap disk.' |
101 | 5 | storres | print output
|
102 | 5 | storres | print 'Aborting!' |
103 | 5 | storres | print
|
104 | 5 | storres | print
|
105 | 5 | storres | sys.exit(1)
|
106 | 5 | storres | commandLine = 'mkswap ' + commonVariables['SWAP_DISK_LOGICAL_VOLUME'] + \ |
107 | 5 | storres | '/' + computeNodeSwapDisk
|
108 | 5 | storres | print commandString
|
109 | 5 | storres | # FIXME!!!!!!!!!!!!!!!!!!!!!
|
110 | 5 | storres | #status,output = commands.getstatusoutput(commandString)
|
111 | 5 | storres | if status != 0 : |
112 | 5 | storres | print
|
113 | 5 | storres | print
|
114 | 5 | storres | print sys.argv[0], ': could not format the VM swap disk.' |
115 | 5 | storres | print output
|
116 | 5 | storres | print 'Aborting!' |
117 | 5 | storres | print
|
118 | 5 | storres | print
|
119 | 5 | storres | sys.exit(1)
|
120 | 5 | storres | # Mount the VM system disk to copy all the specific files
|
121 | 5 | storres | commandString = 'mount ' + commonVariables['SYSTEM_DISK_LOGICAL_VOLUME'] + \ |
122 | 5 | storres | '/' + computeNodeSystemDisk + ' ' + \ |
123 | 5 | storres | commonVariables['COMPUTE_NODE_DISK_MOUNT_POINT']
|
124 | 5 | storres | print commandString
|
125 | 5 | storres | |
126 | 5 | storres | # FIXME!!!!!!!!!!!!!!!!!!!!!
|
127 | 5 | storres | #status,output = commands.getstatusoutput(commandString)
|
128 | 5 | storres | if status != 0 : |
129 | 5 | storres | print
|
130 | 5 | storres | print
|
131 | 5 | storres | print sys.argv[0], ': could not mount the VM system disk.' |
132 | 5 | storres | print output
|
133 | 5 | storres | print 'Aborting!' |
134 | 5 | storres | print
|
135 | 5 | storres | print
|
136 | 5 | storres | sys.exit(1)
|
137 | 5 | storres | |
138 | 5 | storres | # /etc/network/interfaces
|
139 | 5 | storres | currentFile = '/etc/network/interfaces'
|
140 | 5 | storres | commandString = 'cp ' + templatesDir + currentFile + ' ' +\ |
141 | 5 | storres | commonVariables['COMPUTE_NODE_DISK_MOUNT_POINT'] + \
|
142 | 5 | storres | currentFile |
143 | 5 | storres | print commandString
|
144 | 5 | storres | nodeAddress = commonVariables['COMPUTE_NODE_STATIC_NETWORK_PREFIX'] + '.' + \ |
145 | 5 | storres | nodeNumberString |
146 | 5 | storres | commandString = 'rpl __STATIC_ADDRESS__ ' + nodeAddress + ' ' + \ |
147 | 5 | storres | commonVariables['COMPUTE_NODE_DISK_MOUNT_POINT']
|
148 | 5 | storres | # cp templates$CURRENT_PATH/interfaces \
|
149 | 5 | storres | # $COMPUTE_NODE_DISK_MOUNT_POINT/$CURRENT_PATH
|
150 | 5 | storres | # ADDRESS="$COMPUTE_NODE_STATIC_NETWORK_PREFIX.$NODE_NUMBER"
|
151 | 5 | storres | # rpl __STATIC_ADDRESS__ $ADDRESS \
|
152 | 5 | storres | # ${COMPUTE_NODE_DISK_MOUNT_POINT}${CURRENT_PATH}/interfaces
|
153 | 5 | storres | |
154 | 5 | storres | print 'Current node name: ', currentNodeName |
155 | 4 | storres | print commonVariables.keys()
|
156 | 4 | storres | print commonVariables.values()
|
157 | 4 | storres | # End main
|
158 | 4 | storres | |
159 | 4 | storres | def usage(arguments): |
160 | 4 | storres | import sys |
161 | 4 | storres | if (len(arguments) < 2): |
162 | 4 | storres | print
|
163 | 4 | storres | print
|
164 | 4 | storres | print 'Missing parameter. Aborting' |
165 | 4 | storres | print
|
166 | 4 | storres | print 'Usage: ' + arguments[0] + ' node_number' |
167 | 4 | storres | print
|
168 | 4 | storres | print
|
169 | 4 | storres | sys.exit(1)
|
170 | 4 | storres | nodeNumberString = arguments[1]
|
171 | 5 | storres | # Yep, the final comma is correct: it's a list of variables!
|
172 | 4 | storres | return nodeNumberString,
|
173 | 4 | storres | |
174 | 4 | storres | if __name__ == "__main__": |
175 | 4 | storres | main() |