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