def run(self, params, args): (bigmnt, ) = self.fillParams([ ('bigmnt', 'n'), ]) bigmnt = self.str2bool(bigmnt) if bigmnt: # we just need to print the bigest partition mnt point vm = rocks.vm.VM(self.db) self.beginOutput() for host in self.getHostnames(args): self.addOutput(host, vm.getLargestPartition(host)) self.endOutput(padChar='') return self.beginOutput() for host in self.getHostnames(args): self.db.execute("""select p.device, p.mountpoint, p.sectorstart, p.partitionsize, p.partitionid, p.fstype, p.partitionflags, p.formatflags from partitions p, nodes n where n.name='%s' and n.id=p.node order by device""" % host) for row in self.db.fetchall(): self.addOutput(host, row) self.endOutput(header=[ 'host', 'device', 'mountpoint', 'start', 'size', 'id', 'type', 'flags', 'formatflags' ])
def run(self, params, args): file, = self.fillParams([("file", None)]) hosts = self.getHostnames(args) if len(hosts) < 1: self.abort("must supply host") if file and len(hosts) > 1: self.abort('if you supply the "file" parameter, ' + "then you only can specify one VM host") vm = rocks.vm.VM(self.db) for host in hosts: physhost = vm.getPhysHost(host) if not file: diskprefix = vm.getLargestPartition(physhost) if diskprefix: file = os.path.join(diskprefix, "xen/disks/%s.saved" % host) if physhost and file: # # send the restore command to the physical node # import rocks.vmconstant hipervisor = libvirt.open(rocks.vmconstant.connectionURL % physhost) hipervisor.restore(file)
def run(self, params, args): file, = self.fillParams( [('file', None)] ) hosts = self.getHostnames(args) if len(hosts) < 1: self.abort('must supply host') if file and len(hosts) > 1: self.abort('if you supply the "file" parameter, ' + 'then you only can specify one VM host') vm = rocks.vm.VM(self.db) for host in hosts: physhost = vm.getPhysHost(host) if not file: diskprefix = vm.getLargestPartition(physhost) if diskprefix: file = os.path.join(diskprefix, 'xen/disks/%s.saved' % host) if physhost and file: # # send the restore command to the physical node # import rocks.vmconstant hipervisor = libvirt.open( rocks.vmconstant.connectionURL % physhost) hipervisor.restore(file)
def run(self, params, args): file, = self.fillParams( [('file', None)] ) hosts = self.getHostnames(args) if len(hosts) < 1: self.abort('must supply host') if file and len(hosts) > 1: self.abort('if you supply the "file" parameter, ' + 'then you only can specify one VM host') import rocks.vm vm = rocks.vm.VM(self.db) for host in hosts: physhost = vm.getPhysHost(host) if not file: diskprefix = vm.getLargestPartition(physhost) if diskprefix: file = os.path.join(diskprefix, 'kvm/disks/%s.saved' % host) # # we need to start vlan interface if any # self.command('sync.host.vlan', [host]) if physhost and file: # # send the restore command to the physical node # import rocks.vmconstant hipervisor = libvirt.open( rocks.vmconstant.connectionURL % physhost) hipervisor.restore(file)
def run(self, params, args): (bigmnt,) = self.fillParams( [ ('bigmnt', 'n'), ]) bigmnt = self.str2bool(bigmnt) if bigmnt: # we just need to print the bigest partition mnt point vm = rocks.vm.VM(self.db) self.beginOutput() for host in self.getHostnames(args): self.addOutput(host, vm.getLargestPartition(host)) self.endOutput(padChar='') return self.beginOutput() for host in self.getHostnames(args): self.db.execute("""select p.device, p.mountpoint, p.sectorstart, p.partitionsize, p.partitionid, p.fstype, p.partitionflags, p.formatflags from partitions p, nodes n where n.name='%s' and n.id=p.node order by device""" % host) for row in self.db.fetchall(): self.addOutput(host, row) self.endOutput(header=['host', 'device', 'mountpoint', 'start','size', 'id', 'type', 'flags', 'formatflags'])
def run(self, params, args): vars = [ ('XENDOMAINS_SYSRQ', '""'), ('XENDOMAINS_USLEEP', '100000'), ('XENDOMAINS_CREATE_USLEEP', '5000000'), ('XENDOMAINS_MIGRATE', '""'), ('XENDOMAINS_SHUTDOWN', '"--halt --wait"'), ('XENDOMAINS_SHUTDOWN_ALL', '"--all --halt --wait"'), ('XENDOMAINS_RESTORE', 'true'), ('XENDOMAINS_AUTO_ONLY', 'false'), ('XENDOMAINS_STOP_MAXWAIT', '300') ] vm = rocks.vm.VM(self.db) hosts = self.getHostnames(args) if len(hosts) != 1: self.abort('must only supply one host') self.addText('<file name="/etc/sysconfig/xendomains">') host = hosts[0] for var, default in vars: attr = self.db.getHostAttr(host, var) if not attr: attr = default self.addText('%s=%s\n' % (var, attr)) # # special case for XENDOMAINS_SAVE and XENDOMAINS_AUTO. # if an attribute is not set for these variables, then # we want to put them on the largest partition on the # host # part = vm.getLargestPartition(host) attr = self.db.getHostAttr(host, 'XENDOMAINS_SAVE') if not attr: attr = '%s/xen/save' % part self.addText('XENDOMAINS_SAVE=%s\n' % (attr)) xendomains_save = attr attr = self.db.getHostAttr(host, 'XENDOMAINS_AUTO') if not attr: attr = '%s/xen/auto' % part self.addText('XENDOMAINS_AUTO=%s\n' % (attr)) xendomains_auto = attr self.addText('</file>\n\n') self.addText('mkdir -p %s\n' % xendomains_save) self.addText('mkdir -p %s\n' % xendomains_auto)
def run(self, params, args): vars = [('XENDOMAINS_SYSRQ', '""'), ('XENDOMAINS_USLEEP', '100000'), ('XENDOMAINS_CREATE_USLEEP', '5000000'), ('XENDOMAINS_MIGRATE', '""'), ('XENDOMAINS_SHUTDOWN', '"--halt --wait"'), ('XENDOMAINS_SHUTDOWN_ALL', '"--all --halt --wait"'), ('XENDOMAINS_RESTORE', 'true'), ('XENDOMAINS_AUTO_ONLY', 'false'), ('XENDOMAINS_STOP_MAXWAIT', '300')] vm = rocks.vm.VM(self.db) hosts = self.getHostnames(args) if len(hosts) != 1: self.abort('must only supply one host') self.addText('<file name="/etc/sysconfig/xendomains">') host = hosts[0] for var, default in vars: attr = self.db.getHostAttr(host, var) if not attr: attr = default self.addText('%s=%s\n' % (var, attr)) # # special case for XENDOMAINS_SAVE and XENDOMAINS_AUTO. # if an attribute is not set for these variables, then # we want to put them on the largest partition on the # host # part = vm.getLargestPartition(host) attr = self.db.getHostAttr(host, 'XENDOMAINS_SAVE') if not attr: attr = '%s/xen/save' % part self.addText('XENDOMAINS_SAVE=%s\n' % (attr)) xendomains_save = attr attr = self.db.getHostAttr(host, 'XENDOMAINS_AUTO') if not attr: attr = '%s/xen/auto' % part self.addText('XENDOMAINS_AUTO=%s\n' % (attr)) xendomains_auto = attr self.addText('</file>\n\n') self.addText('mkdir -p %s\n' % xendomains_save) self.addText('mkdir -p %s\n' % xendomains_auto)
def run(self, params, args): file, = self.fillParams( [('file', None)] ) hosts = self.getHostnames(args) if len(hosts) < 1: self.abort('must supply host') if file and len(hosts) > 1: self.abort('if you supply the "file" parameter, ' + 'then you only can specify one VM host') import rocks.vm vm = rocks.vm.VM(self.db) for host in hosts: physhost = vm.getPhysHost(host) if not physhost: continue if not file: diskprefix = vm.getLargestPartition(physhost) if diskprefix: # # create a default file name # file = os.path.join(os.path.join( diskprefix, 'kvm/disks'), '%s.saved' % host) if physhost and file: # # send the save command to the physical node # import rocks.vmconstant hipervisor = libvirt.open( rocks.vmconstant.connectionURL % physhost) domU = hipervisor.lookupByName(host) domU.save(file)
def run(self, params, args): file, = self.fillParams([('file', None)]) hosts = self.getHostnames(args) if len(hosts) < 1: self.abort('must supply host') if file and len(hosts) > 1: self.abort('if you supply the "file" parameter, ' + 'then you only can specify one VM host') import rocks.vm vm = rocks.vm.VM(self.db) for host in hosts: physhost = vm.getPhysHost(host) if not physhost: continue if not file: diskprefix = vm.getLargestPartition(physhost) if diskprefix: # # create a default file name # file = os.path.join(os.path.join(diskprefix, 'kvm/disks'), '%s.saved' % host) if physhost and file: # # send the save command to the physical node # import rocks.vmconstant hipervisor = libvirt.open(rocks.vmconstant.connectionURL % physhost) domU = hipervisor.lookupByName(host) domU.save(file)
def run(self, params, args): nodes = self.newdb.getNodesfromNames( args, preload=['vm_defs', 'vm_defs.disks']) #some arguments parsing if len(nodes) != 1: self.abort('must supply only one host') else: node = nodes[0] host = node.name (credentialDir, outputpath, imagename) = self.fillParams([('credentialdir', ), ('outputpath', ), ('imagename', )]) if not credentialDir: credentialDir = "~/.ec2/" if not imagename: imagename = "" if node.vm_defs.physNode: physhost = node.vm_defs.physNode.name else: self.abort("Impossible to fetch the physical node.") #ok we have to bunble the vm host runnning on physhost # #let's check that the machine is not running print 'physhost is %s; host is %s' % (physhost, host) import rocks state = rocks.db.vmextend.getStatus(node) if state != 'nostate': self.abort("The vm " + host + " is still running (" + state + "). Please shut it down before running this command.") #which outputpath should we use...??? if not outputpath: # find the largest partition on the remote node # and use it as the directory prefix import rocks.vm vm = rocks.vm.VM(self.db) vbd_type = 'file' prefix = vm.getLargestPartition(physhost) if not prefix: self.abort('could not find a partition on ' + 'host (%s) to hold the ' % host + 'VM\'s bundle') outputpath = prefix outputpath = outputpath + "/ec2/bundles/" + host # ------------------- clear the outputpath and mkdir if it doesn't exist print "Creating output directories" output = self.command( 'run.host', [physhost, 'rm -rf %s' % outputpath, 'collate=true']) output = self.command( 'run.host', [physhost, 'mkdir -p %s' % outputpath, 'collate=true']) if len(output) > 1: self.abort('We can not create the directory ' + outputpath + ' please check that is not mounted or used') # -------------------- mount the file systems of the vm #TODO check is this the right way to figure out the disk image?? print "Mounting file systems" rows = self.db.execute("""select vmd.prefix, vmd.name from nodes n, vm_disks vmd, vm_nodes vm where vmd.Vm_Node = vm.id and vm.node = n.id and n.name = '%s';""" % host) if rows != 1: self.abort('We can\'t figure out the disk of the virtual' + ' machine %s' % host) (prefix, name) = self.db.fetchall()[0] diskVM = os.path.join(prefix, name) output = self.command( 'run.host', [physhost, "mkdir -p /mnt/rocksimage", 'collate=true']) if len(output) > 1: self.abort('Problem with making the directory /mnt/rocksimage ' + 'on host ' + physhost + '. Error: ' + output) #creating the /dev to be mounted output = self.command('run.host', [ physhost, "kpartx -a -v %s | head -n 1|awk '{print $3}'" % diskVM, 'collate=true' ]) devPath = '/dev/mapper/' + output.strip() output = self.command('run.host', [physhost, "ls " + devPath]) if len(output) > 1: self.abort('Problem mounting ' + diskVM + ' on host ' + physhost + '. Error: ' + output) #ok the device is ready we can mount output = self.command( 'run.host', [physhost, "mount " + devPath + " /mnt/rocksimage"]) #print "exec: mount " + devPath + " /mnt/rocksimage" if len(output) > 1: self.abort('Problem mounting the image: ' + output) # 2. -------------------- copy the credential over... print "Copying credential directory" retval = os.system('scp -qr %s %s:%s/.ec2 ' % (credentialDir, physhost, outputpath)) if retval != 0: self.terminate(physhost, diskVM, outputpath) self.abort('Could not copy the credential directory: ' + credentialDir + ' to the output path: ' + outputpath) # ------------------------ removing root password print "Removing root password" #toremove the password #"sed -i -e 's/root:[^:]\{1,\}:/root:!:/' /etc/shadow" output = self.command('run.host', [ physhost, "sed -i --expression='s/root:[^:]\{1,\}:/root:\!:/' /mnt/rocksimage/etc/shadow", 'collate=true' ]) if len(output) > 1: #aborting self.terminate(physhost, diskVM, outputpath) self.abort('Problem removing root password. Error: ' + output) # ------------------------ create fstab print "Fixing fstab" output = self.command('run.host', [ physhost, '''sed -i 's/.* \/ \(.*\)/\/dev\/xvde1 \/ \\1/g' /mnt/rocksimage/etc/fstab''', 'collate=true' ]) if len(output) > 1: self.terminate(physhost, diskVM, outputpath) self.abort('Could not fix the fstab on the the host: ' + physhost) # ------------------------ fix the grub.conf for ec2 # we need to take the options out of the kernel grub invocation # fix the partions address (hd0,0 must be hd0) print "Fixing grub" sedGrub = """#!/bin/bash BASEROOT=/mnt/rocksimage GRUBDIR=$BASEROOT/boot/grub cp $GRUBDIR/grub.conf $GRUBDIR/grub-orig.conf sed -i 's/hd0,0/hd0/g' $GRUBDIR/grub.conf sed -i 's/kernel \([^ ]*\) .*/kernel \\1 root=\/dev\/xvde1 console=ttyS0,115200/g' $GRUBDIR/grub.conf """ scriptName = '/tmp/fixgrub.sh' if not createScript(sedGrub, scriptName, physhost): self.terminate(physhost, diskVM, outputpath) self.abort('Could not fix grub.conf to the host: ' + physhost) self.command('run.host', [physhost, 'bash ' + scriptName]) if retval != 0: #restore original grub self.command('run.host', [ physhost, 'cp /mnt/rocksimage/boot/grub/grub-orig.conf /mnt/rocksimage/boot/grub/grub.conf' ]) self.terminate(physhost, diskVM, outputpath) self.abort('Could fix grub configuration for EC2 ') # now we need to unmount before we can bundle # new ec2-bundle-image output = self.command( 'run.host', [physhost, "umount /mnt/rocksimage", 'collate=true']) # print "removing parttion table" temp_disk_nopart = outputpath + "/disk.img" output = self.command( 'run.host', [physhost, 'dd if=%s of=%s bs=4M' % (devPath, temp_disk_nopart)]) output = self.command('run.host', [physhost, "kpartx -d -v %s " % diskVM]) # ------------------------ create the script print "Creating the bundle script" # we now support only 64 bit arch = 'x86_64' bundleScript = """#!/bin/bash if [ -n "$1" ] ; then IMAGENAME="-p $1" else IMAGENAME=" " fi #not necessary but... export EC2_HOME=/opt/ec2 OutputPath="%s" echo bundling... /opt/ec2/bin/ec2-bundle-image -d $OutputPath -i %s -c $OutputPath/.ec2/cert.pem -k $OutputPath/.ec2/pk.pem -u `cat $OutputPath/.ec2/user` $IMAGENAME --arch %s """ % (outputpath, temp_disk_nopart, arch) if not createScript(bundleScript, '%s/script.sh' % outputpath, physhost): self.abort('Could not copy the script to the host: ' + physhost) # ----------------------- run the script #execute it with 'chroot /mnt/rocksimage/ /mnt/ec2image/script.sh rocksdevel' print "Running the bundle script this step might take around 10-20 minutes" self.command( 'run.host', [physhost, '%s/script.sh %s' % (outputpath, imagename)]) #removing temporary image output = self.command('run.host', [physhost, "rm -rf " + temp_disk_nopart])
def run(self, params, args): nodes = self.newdb.getNodesfromNames(args, preload=['vm_defs', 'vm_defs.disks']) #some arguments parsing if len(nodes) != 1: self.abort('must supply only one host') else: node = nodes[0] host = node.name (credentialDir, outputpath, imagename) = self.fillParams( [('credentialdir', ), ('outputpath', ) , ('imagename', ) ] ) if not credentialDir: credentialDir = "~/.ec2/" if not imagename: imagename = "" if node.vm_defs.physNode: physhost = node.vm_defs.physNode.name else: self.abort("Impossible to fetch the physical node.") #ok we have to bunble the vm host runnning on physhost # #let's check that the machine is not running print 'physhost is %s; host is %s' % (physhost,host) import rocks state = rocks.db.vmextend.getStatus(node) if state != 'nostate': self.abort("The vm " + host + " is still running (" + state + "). Please shut it down before running this command.") #which outputpath should we use...??? if not outputpath: # find the largest partition on the remote node # and use it as the directory prefix import rocks.vm vm = rocks.vm.VM(self.db) vbd_type = 'file' prefix = vm.getLargestPartition(physhost) if not prefix: self.abort('could not find a partition on ' + 'host (%s) to hold the ' % host + 'VM\'s bundle') outputpath = prefix outputpath = outputpath + "/ec2/bundles/" + host # ------------------- clear the outputpath and mkdir if it doesn't exist print "Creating output directories" output = self.command('run.host', [physhost, 'rm -rf %s' % outputpath, 'collate=true']) output = self.command('run.host', [physhost, 'mkdir -p %s' % outputpath, 'collate=true']) if len(output) > 1: self.abort('We can not create the directory ' + outputpath + ' please check that is not mounted or used') # -------------------- mount the file systems of the vm #TODO check is this the right way to figure out the disk image?? print "Mounting file systems" rows = self.db.execute("""select vmd.prefix, vmd.name from nodes n, vm_disks vmd, vm_nodes vm where vmd.Vm_Node = vm.id and vm.node = n.id and n.name = '%s';""" % host) if rows != 1: self.abort('We can\'t figure out the disk of the virtual' + ' machine %s' % host) (prefix, name) = self.db.fetchall()[0] diskVM = os.path.join(prefix, name) output = self.command('run.host', [physhost, "mkdir -p /mnt/rocksimage", 'collate=true']) if len(output) > 1: self.abort('Problem with making the directory /mnt/rocksimage ' + 'on host ' + physhost + '. Error: ' + output) #creating the /dev to be mounted output = self.command('run.host', [physhost, "kpartx -a -v %s | head -n 1|awk '{print $3}'" % diskVM, 'collate=true']) devPath = '/dev/mapper/' + output.strip() output = self.command('run.host', [physhost, "ls " + devPath]) if len(output) > 1: self.abort('Problem mounting ' + diskVM + ' on host ' + physhost + '. Error: ' + output) #ok the device is ready we can mount output = self.command('run.host', [physhost, "mount " + devPath + " /mnt/rocksimage"]) #print "exec: mount " + devPath + " /mnt/rocksimage" if len(output) > 1: self.abort('Problem mounting the image: ' + output) # 2. -------------------- copy the credential over... print "Copying credential directory" retval = os.system('scp -qr %s %s:%s/.ec2 ' % (credentialDir, physhost, outputpath)) if retval != 0: self.terminate(physhost, diskVM, outputpath) self.abort('Could not copy the credential directory: ' + credentialDir + ' to the output path: ' + outputpath) # ------------------------ removing root password print "Removing root password" #toremove the password #"sed -i -e 's/root:[^:]\{1,\}:/root:!:/' /etc/shadow" output = self.command('run.host', [physhost, "sed -i --expression='s/root:[^:]\{1,\}:/root:\!:/' /mnt/rocksimage/etc/shadow", 'collate=true']) if len(output) > 1: #aborting self.terminate(physhost, diskVM, outputpath) self.abort('Problem removing root password. Error: ' + output) # ------------------------ create fstab print "Fixing fstab" output = self.command('run.host', [physhost, '''sed -i 's/.* \/ \(.*\)/\/dev\/xvde1 \/ \\1/g' /mnt/rocksimage/etc/fstab''', 'collate=true']) if len(output) > 1: self.terminate(physhost, diskVM, outputpath) self.abort('Could not fix the fstab on the the host: ' + physhost) # ------------------------ fix the grub.conf for ec2 # we need to take the options out of the kernel grub invocation # fix the partions address (hd0,0 must be hd0) print "Fixing grub" sedGrub = """#!/bin/bash BASEROOT=/mnt/rocksimage GRUBDIR=$BASEROOT/boot/grub cp $GRUBDIR/grub.conf $GRUBDIR/grub-orig.conf sed -i 's/hd0,0/hd0/g' $GRUBDIR/grub.conf sed -i 's/kernel \([^ ]*\) .*/kernel \\1 root=\/dev\/xvde1 console=ttyS0,115200/g' $GRUBDIR/grub.conf """ scriptName = '/tmp/fixgrub.sh' if not createScript(sedGrub, scriptName, physhost): self.terminate(physhost, diskVM, outputpath) self.abort('Could not fix grub.conf to the host: ' + physhost ) self.command('run.host', [ physhost, 'bash ' + scriptName ] ) if retval != 0: #restore original grub self.command('run.host', [ physhost, 'cp /mnt/rocksimage/boot/grub/grub-orig.conf /mnt/rocksimage/boot/grub/grub.conf']) self.terminate(physhost, diskVM, outputpath) self.abort('Could fix grub configuration for EC2 ' ) # now we need to unmount before we can bundle # new ec2-bundle-image output = self.command('run.host', [physhost, "umount /mnt/rocksimage",'collate=true']) # print "removing parttion table" temp_disk_nopart = outputpath + "/disk.img" output = self.command('run.host', [physhost, 'dd if=%s of=%s bs=4M' % (devPath, temp_disk_nopart)]) output = self.command('run.host', [physhost, "kpartx -d -v %s " % diskVM]) # ------------------------ create the script print "Creating the bundle script" # we now support only 64 bit arch = 'x86_64' bundleScript = """#!/bin/bash if [ -n "$1" ] ; then IMAGENAME="-p $1" else IMAGENAME=" " fi #not necessary but... export EC2_HOME=/opt/ec2 OutputPath="%s" echo bundling... /opt/ec2/bin/ec2-bundle-image -d $OutputPath -i %s -c $OutputPath/.ec2/cert.pem -k $OutputPath/.ec2/pk.pem -u `cat $OutputPath/.ec2/user` $IMAGENAME --arch %s """ % (outputpath, temp_disk_nopart, arch) if not createScript(bundleScript, '%s/script.sh' % outputpath, physhost): self.abort('Could not copy the script to the host: ' + physhost ) # ----------------------- run the script #execute it with 'chroot /mnt/rocksimage/ /mnt/ec2image/script.sh rocksdevel' print "Running the bundle script this step might take around 10-20 minutes" self.command('run.host', [physhost, '%s/script.sh %s' % (outputpath , imagename)]) #removing temporary image output = self.command('run.host', [physhost, "rm -rf " + temp_disk_nopart])
def addVMHost(self, host, membership, nodename, ip, subnet, mem, cpus, slice, mac, num_macs, disk, disksize, vlan, module, virt_type): rows = self.db.execute("""select id, rack, rank from nodes where name = '%s'""" % (host)) if rows == 1: nodeid, rack, rank = self.db.fetchone() else: self.abort('could not find an ID for host %s' % (host)) rows = self.db.execute("""select name from nodes""") knownhosts = self.db.fetchall() if not slice: # # find the next free slice in the database # rows = self.db.execute("""select max(slice) from vm_nodes where physnode = %s""" % (nodeid)) if rows > 0: max, = self.db.fetchone() if max: slice = int(max) + 1 else: slice = 0 # # special case where the user didn't specify the # slice *and* the nodename, then we are allowed to # increment the slice value until we find a unique # nodename # while not nodename: nodename = self.getNodename(membership, rack, rank, slice) if (nodename,) in knownhosts: nodename = None slice += 1 if not nodename: nodename = self.getNodename(membership, rack, rank, slice) if (nodename,) in knownhosts: # # make sure the nodename is not already in # the database # self.abort('nodename (%s) ' % nodename + \ 'is already in the databaase') if not disk: # # find the largest partition on the remote node # and use it as the directory prefix # vm = rocks.vm.VM(self.db) vbd_type = 'file' prefix = vm.getLargestPartition(host) device = 'hda' name = '%s.%s' % (nodename, device) mode = 'w' if not prefix: self.abort('could not find a partition on ' + 'host (%s) to hold the ' % host + 'VM\'s disk image') disk = '%s:%s,%s,%s' % (vbd_type, os.path.join(prefix, 'xen/disks', name), device, mode) if not mac: mac = self.getNextMac() if not ip: if membership == 'Hosted VM': ip = 'NULL' else: ip = "'%s'" % self.getNextIP(subnet) else: # # make sure the ip is single quoted # newip = "'%s'" % ip.strip("'") ip = newip if vlan: vlanids = vlan.split(',') else: vlanids = None if not module: module = 'NULL' # # we now have all the parameters -- add them to the database # self.addToDB(nodename, membership, ip, subnet, nodeid, rack, rank, mem, cpus, slice, mac, num_macs, disk, disksize, vlanids, module, virt_type) # # set the default installaction # if virt_type == 'para': self.command('set.host.installaction', [ nodename, 'install vm' ] ) # HVMs boot just like real hardware if virt_type == 'hvm': self.command('set.host.installaction', [ nodename, 'install' ] ) self.command('set.host.runaction', [ nodename, 'os' ] ) # # set the first boot state to 'install' # self.command('set.host.boot', [ nodename, 'action=install' ] ) # # print the name of the new VM # self.beginOutput() self.addOutput('', 'added VM %s on physical node %s' % (nodename, host)) self.endOutput()
def run(self, params, args): (args, tophyshost) = self.fillPositionalArgs(('physhost', )) file, = self.fillParams([('file', None)]) if len(args) != 1: self.abort('must supply only one VM host') vm = rocks.vm.VM(self.db) # # validate the 'to' physical host name # h = self.getHostnames([tophyshost]) hosts = self.getHostnames(args) host = hosts[0] fromphyshost = vm.getPhysHost(host) if tophyshost == fromphyshost: self.abort("The source physical host, and the destination " + "host are the same: " + tophyshost) if not file: filename = '%s.saved' % host fromdiskprefix = vm.getLargestPartition(fromphyshost) fromdir = os.path.join(fromdiskprefix, 'kvm/disks') fromsavefile = os.path.join(fromdir, filename) else: fromsavefile = file print "Saving the VM's current state." # # save the VM's running state # self.command('save.host.vm', [host, "file=%s" % fromsavefile]) # # copy the VM save file to the physical node # running state # todiskprefix = vm.getLargestPartition(tophyshost) todir = os.path.join(todiskprefix, 'kvm/disks') tosavefile = os.path.join(todir, filename) #make the directory #I can not use run.command cos it doesn't return the exit status cmd = preparedir % (tophyshost, tosavefile) debug = os.system(cmd) if debug != 0: self.abort( "Unabled to create destination directory on physical host, " + tophyshost) #copy the file cmd = copycmd % (fromphyshost, fromsavefile, tophyshost, tosavefile) debug = os.system(cmd) if debug != 0: self.abort("Unabled to copy vm state on the new physical host, " + tophyshost) # # copy the VMs disks to the 'from' host # rows = self.db.execute("""select vn.id from vm_nodes vn, nodes n where n.name = "%s" and vn.node = n.id""" % host) if rows < 1: return vmnodeid, = self.db.fetchone() rows = self.db.execute("""select vbd_type, prefix, name from vm_disks where vm_node = %s""" % vmnodeid) if rows > 0: for vbd_type, prefix, name in self.db.fetchall(): if vbd_type not in ['tap:aio', 'file']: continue print "Copying VM disk file to %s." % tophyshost filename = os.path.join(prefix, name) print "Moving virtual disk (%s) to %s" % \ (filename, tophyshost) toprefix = vm.getLargestPartition(tophyshost) todir = os.path.join(toprefix, 'kvm/disks') tofile = os.path.join(todir, name) #make the directory cmd = preparedir % (tophyshost, tofile) debug = os.system(cmd) if debug != 0: self.abort("Unabled to create destination " + "directory on physical host, " + tophyshost) #copy the file cmd = copycmd % (fromphyshost, filename, tophyshost, tofile) debug = os.system(cmd) if debug != 0: self.abort("Unabled to copy vm state on the new " + "physical host, " + tophyshost) # # update the disk specification in the database # self.db.execute("""update vm_disks set prefix = "%s" where vm_node = %s""" % (todir, vmnodeid)) # # update which physical node now houses this VM # rows = self.db.execute("""select id from nodes where name = "%s" """ % (tophyshost)) if rows < 1: return physhostid, = self.db.fetchone() self.db.execute("""update vm_nodes set physnode = %s where id = %s """ % (physhostid, vmnodeid)) # # update vlan on physhostid if it is not already present # rows = self.db.execute( """select vlanid from networks as net, nodes as n where n.id = net.node and n.name='%s'""" % host) if rows < 1: self.abort("Unable to find vlanid for host " + host) vlanid, = self.db.fetchone() if vlanid != None: # we are moving a virtual cluster node # hence we need to move its' vlan as well rows = self.db.execute("""select sub.name from networks as net, nodes as n, subnets sub where n.name='%s' and net.vlanid=%s and n.id=net.node and sub.id=net.subnet""" % (tophyshost, vlanid)) if rows < 1: self.abort("Unable to find subnet for host migration") subnet, = self.db.fetchone() self.addVlanToHost(tophyshost, vlanid, subnet) # # restore the VM's running state # print "Restarting the VM on %s." % (tophyshost) self.command('restore.host.vm', [host, "file=%s" % tosavefile])
def addVMHost(self, node, membership, subnet, nodename, ip, mem, cpus, slice, mac, num_macs, disk, disksize, vlan, virt_type): """node is a Node type and membership is a Membership type, subnet is a Subnet type. All other input params are strings""" s = self.newdb.getSession() knownhosts = s.query(Node.name).all() if not slice: # # find the next free slice in the database # slices = s.query(sqlalchemy.sql.func.max(VmNode.slice)).\ filter(VmNode.physNode == node).all() slice = 0 if slices[0][0]: slice = slices[0][0] # # special case where the user didn't specify the # slice *and* the nodename, then we are allowed to # increment the slice value until we find a unique # nodename # while not nodename: nodename = '%s-%s-%s-%s' % (membership.appliance.name, node.rack, node.rank, slice) if (nodename,) in knownhosts: nodename = None slice += 1 if not nodename: nodename = '%s-%s-%s-%s' % (membership.appliance.name, node.rack, node.rank, slice) if (nodename,) in knownhosts: # # make sure the nodename is not already in # the database # self.abort('nodename (%s) ' % nodename + \ 'is already in the databaase') if not disk: # # find the largest partition on the remote node # and use it as the directory prefix # vm = rocks.vm.VM(self.db) vbd_type = 'file' prefix = vm.getLargestPartition(node.name) device = 'vda' name = '%s.%s' % (nodename, device) mode = 'virtio' if not prefix: self.abort('could not find a partition on ' + 'host (%s) to hold the ' % node.name + 'VM\'s disk image') disk = '%s:%s,%s,%s' % (vbd_type, os.path.join(prefix, 'kvm/disks', name), device, mode) if not ip: if membership.name == 'Hosted VM': ip = None else: ip = self.getNextIP(subnet.name) if ip: ip = ip.strip("'") if vlan: vlanids = vlan.split(',') else: vlanids = None # # we now have all the parameters -- add them to the database # self.addToDB(node, membership, subnet, nodename, ip, mem, cpus, slice, mac, num_macs, disk, disksize, vlanids, virt_type) # write all this stuff to disk self.newdb.commit() self.newdb.renewConnection() # # set the default installaction # if virt_type == 'para': self.command('set.host.installaction', [ nodename, 'install vm' ] ) # HVMs boot just like real hardware if virt_type == 'hvm' and rocks.version_major == '7': # No tracker is the default install for VMs # Rocks 7 self.command('set.host.installaction', [nodename, 'install notracker']) self.command('add.host.attr',[nodename,'UseTracker','False']) self.command('set.host.runaction', [nodename, 'os']) # if virt_type == 'hvm' and rocks.version_major != '7': self.command('set.host.installaction', [nodename, 'install']) self.command('set.host.runaction', [nodename, 'os']) # set the first boot state to 'install' # self.command('set.host.boot', [nodename, 'action=install']) # # print the name of the new VM # self.beginOutput() self.addOutput('', 'added VM %s on physical node %s' % (nodename, node.name)) self.endOutput()
def run(self, params, args): (args, bucket) = self.fillPositionalArgs(('s3bucket',)) hosts = self.getHostnames(args) #some arguments parsing if len(hosts) != 1: self.abort('must supply only one host') else: host = hosts[0] (credentialDir, outputpath, imagename, location) = self.fillParams( [('credentialdir','~/.ec2'), ('outputpath', ) , ('imagename', 'image'), ('location', 'US') ] ) # # the name of the physical host that will boot # this VM host # rows = self.db.execute("""select vn.physnode from vm_nodes vn, nodes n where n.name = '%s' and n.id = vn.node""" % (host)) if rows == 1: physnodeid, = self.db.fetchone() else: self.abort("Impossible to fetch the physical node.") rows = self.db.execute("""select name from nodes where id = %s""" % (physnodeid)) if rows == 1: physhost, = self.db.fetchone() else: self.abort("Impossible to fetch the physical node.") #which outputpath should we use...??? if not outputpath: # find the largest partition on the remote node # and use it as the directory prefix import rocks.vm vm = rocks.vm.VM(self.db) vbd_type = 'file' prefix = vm.getLargestPartition(physhost) if not prefix: self.abort('could not find a partition on ' + 'host (%s) to hold the ' % host + 'VM\'s bundle') outputpath = prefix outputpath = outputpath + "/ec2/bundles/" + host # 2. -------------------- copy the credential over... print "Copying credential directory" retval = os.system('scp -qr %s/* %s:%s/.ec2 ' % (credentialDir, physhost, outputpath)) if retval != 0: self.abort('Could not copy the credential directory: ' + credentialDir + ' to the output path: ' + outputpath) # ------------------------ create the script print "Creating the script" manifest=outputpath + "/%s.manifest.xml" % imagename awsid = outputpath + "/.ec2/access-key" secretkey = outputpath + "/.ec2/access-key-secret" scriptTemp = self.createScript(location,bucket,manifest,awsid,secretkey) retval = os.system('scp -qr %s %s:%s/upload-script.sh ' % (scriptTemp, physhost,outputpath)) if retval != 0: self.abort('Could not copy the script to the host: ' + physhost ) # ----------------------- run the script #execute the upload script print "Running the upload script this step may take 30-60 minutes" print " depending on your connection to S3" output = self.command('run.host', [physhost, "%s/upload-script.sh" % outputpath, 'collate=true']) print output
def run(self, params, args): (args, tophyshost) = self.fillPositionalArgs(('physhost', )) file, = self.fillParams( [('file', None)] ) if len(args) != 1: self.abort('must supply only one VM host') vm = rocks.vm.VM(self.db) # # validate the 'to' physical host name # h = self.getHostnames([ tophyshost ]) hosts = self.getHostnames(args) host = hosts[0] fromphyshost = vm.getPhysHost(host) if tophyshost == fromphyshost: self.abort("The source physical host, and the destination " + "host are the same: " + tophyshost) if not file: filename = '%s.saved' % host fromdiskprefix = vm.getLargestPartition(fromphyshost) fromdir = os.path.join(fromdiskprefix, 'kvm/disks') fromsavefile = os.path.join(fromdir, filename) else: fromsavefile = file print "Saving the VM's current state." # # save the VM's running state # self.command('save.host.vm', [ host, "file=%s" % fromsavefile] ) # # copy the VM save file to the physical node # running state # todiskprefix = vm.getLargestPartition(tophyshost) todir = os.path.join(todiskprefix, 'kvm/disks') tosavefile = os.path.join(todir, filename) #make the directory #I can not use run.command cos it doesn't return the exit status cmd = preparedir % (tophyshost, tosavefile) debug = os.system(cmd) if debug != 0 : self.abort("Unabled to create destination directory on physical host, " + tophyshost) #copy the file cmd = copycmd % (fromphyshost, fromsavefile, tophyshost, tosavefile) debug = os.system(cmd) if debug != 0 : self.abort("Unabled to copy vm state on the new physical host, " + tophyshost) # # copy the VMs disks to the 'from' host # rows = self.db.execute("""select vn.id from vm_nodes vn, nodes n where n.name = "%s" and vn.node = n.id""" % host) if rows < 1: return vmnodeid, = self.db.fetchone() rows = self.db.execute("""select vbd_type, prefix, name from vm_disks where vm_node = %s""" % vmnodeid) if rows > 0: for vbd_type, prefix, name in self.db.fetchall(): if vbd_type not in [ 'tap:aio', 'file' ]: continue print "Copying VM disk file to %s." % tophyshost filename = os.path.join(prefix, name) print "Moving virtual disk (%s) to %s" % \ (filename, tophyshost) toprefix = vm.getLargestPartition(tophyshost) todir = os.path.join(toprefix, 'kvm/disks') tofile = os.path.join(todir, name) #make the directory cmd = preparedir % (tophyshost, tofile) debug = os.system(cmd) if debug != 0 : self.abort("Unabled to create destination " +"directory on physical host, " + tophyshost) #copy the file cmd = copycmd % (fromphyshost, filename, tophyshost, tofile) debug = os.system(cmd) if debug != 0 : self.abort("Unabled to copy vm state on the new " + "physical host, " + tophyshost) # # update the disk specification in the database # self.db.execute("""update vm_disks set prefix = "%s" where vm_node = %s""" % (todir, vmnodeid)) # # update which physical node now houses this VM # rows = self.db.execute("""select id from nodes where name = "%s" """ % (tophyshost)) if rows < 1: return physhostid, = self.db.fetchone() self.db.execute("""update vm_nodes set physnode = %s where id = %s """ % (physhostid, vmnodeid)) # # update vlan on physhostid if it is not already present # rows = self.db.execute("""select vlanid from networks as net, nodes as n where n.id = net.node and n.name='%s'""" % host) if rows < 1: self.abort("Unable to find vlanid for host " + host) vlanid, = self.db.fetchone() if vlanid != None: # we are moving a virtual cluster node # hence we need to move its' vlan as well rows = self.db.execute("""select sub.name from networks as net, nodes as n, subnets sub where n.name='%s' and net.vlanid=%s and n.id=net.node and sub.id=net.subnet""" % (tophyshost, vlanid)) if rows < 1: self.abort("Unable to find subnet for host migration") subnet, = self.db.fetchone() self.addVlanToHost(tophyshost, vlanid, subnet) # # restore the VM's running state # print "Restarting the VM on %s." % (tophyshost) self.command('restore.host.vm', [ host, "file=%s" % tosavefile])
def run(self, params, args): (args, tophyshost) = self.fillPositionalArgs(('physhost', )) file, = self.fillParams( [('file', None)] ) if len(args) != 1: self.abort('must supply only one VM host') vm = rocks.vm.VM(self.db) # # validate the 'to' physical host name # h = self.getHostnames([ tophyshost ]) hosts = self.getHostnames(args) host = hosts[0] fromphyshost = vm.getPhysHost(host) if not file: filename = '%s.saved' % host fromdiskprefix = vm.getLargestPartition(fromphyshost) fromdir = os.path.join(fromdiskprefix, 'xen/disks') fromsavefile = os.path.join(fromdir, filename) else: fromsavefile = file print "Saving the VM's current state." # # save the VM's running state # self.command('save.host.vm', [ host, "file=%s" % fromsavefile] ) fromsavefilestat = self.command('run.host', [ fromphyshost, statcmd % fromsavefile ]).strip() print "Copying the VM's current state to %s." % (tophyshost) # # copy the VM save file to the physical node # running state # todiskprefix = vm.getLargestPartition(tophyshost) todir = os.path.join(todiskprefix, 'xen/disks') tosavefile = os.path.join(todir, filename) cmd = copyfile % (fromphyshost, fromsavefile, fromsavefilestat, tosavefile) debug = self.command('run.host', [ tophyshost, 'command=%s' % cmd ]) # # copy the VMs disks to the 'from' host # rows = self.db.execute("""select vn.id from vm_nodes vn, nodes n where n.name = "%s" and vn.node = n.id""" % host) if rows < 1: return vmnodeid, = self.db.fetchone() rows = self.db.execute("""select vbd_type, prefix, name from vm_disks where vm_node = %s""" % vmnodeid) if rows > 0: for vbd_type, prefix, name in self.db.fetchall(): if vbd_type not in [ 'tap:aio', 'file' ]: continue print "Copying VM disk file to %s." % tophyshost filename = os.path.join(prefix, name) filestat = self.command('run.host', [ fromphyshost, statcmd % filename ]).strip() print "Moving virtual disk (%s) to %s" % \ (filename, tophyshost) toprefix = vm.getLargestPartition(tophyshost) todir = os.path.join(toprefix, 'xen/disks') tofile = os.path.join(todir, name) cmd = copyfile % (fromphyshost, filename, filestat, tofile) debug = self.command('run.host', [ tophyshost, 'command=%s' % cmd ]) # print 'run.host output: ', debug # # update the disk specification in the database # self.db.execute("""update vm_disks set prefix = "%s" where vm_node = %s""" % (todir, vmnodeid)) # # update which physical node now houses this VM # rows = self.db.execute("""select id from nodes where name = "%s" """ % (tophyshost)) if rows < 1: return physhostid, = self.db.fetchone() self.db.execute("""update vm_nodes set physnode = %s where id = %s """ % (physhostid, vmnodeid)) # # remove the VM configuration file on the 'from' physical host # cmd = 'rm -f /etc/xen/rocks/%s' % host self.command('run.host', [ fromphyshost, 'command=%s' % cmd ]) # # restore the VM's running state # print "Restarting the VM on %s." % (tophyshost) self.command('restore.host.vm', [ host, "file=%s" % tosavefile])
def addVMHost(self, node, membership, subnet, nodename, ip, mem, cpus, slice, mac, num_macs, disk, disksize, vlan, virt_type): """node is a Node type and membership is a Membership type, subnet is a Subnet type. All other input params are strings""" s = self.newdb.getSession() knownhosts = s.query(Node.name).all() if not slice: # # find the next free slice in the database # slices = s.query(sqlalchemy.sql.func.max(VmNode.slice)).\ filter(VmNode.physNode == node).all() slice = 0 if slices[0][0]: slice = slices[0][0] # # special case where the user didn't specify the # slice *and* the nodename, then we are allowed to # increment the slice value until we find a unique # nodename # while not nodename: nodename = '%s-%s-%s-%s' % (membership.appliance.name, node.rack, node.rank, slice) if (nodename, ) in knownhosts: nodename = None slice += 1 if not nodename: nodename = '%s-%s-%s-%s' % (membership.appliance.name, node.rack, node.rank, slice) if (nodename, ) in knownhosts: # # make sure the nodename is not already in # the database # self.abort('nodename (%s) ' % nodename + \ 'is already in the databaase') if not disk: # # find the largest partition on the remote node # and use it as the directory prefix # vm = rocks.vm.VM(self.db) vbd_type = 'file' prefix = vm.getLargestPartition(node.name) device = 'vda' name = '%s.%s' % (nodename, device) mode = 'virtio' if not prefix: self.abort('could not find a partition on ' + 'host (%s) to hold the ' % node.name + 'VM\'s disk image') disk = '%s:%s,%s,%s' % (vbd_type, os.path.join(prefix, 'kvm/disks', name), device, mode) if not ip: if membership.name == 'Hosted VM': ip = None else: ip = self.getNextIP(subnet.name) if ip: ip = ip.strip("'") if vlan: vlanids = vlan.split(',') else: vlanids = None # # we now have all the parameters -- add them to the database # self.addToDB(node, membership, subnet, nodename, ip, mem, cpus, slice, mac, num_macs, disk, disksize, vlanids, virt_type) # write all this stuff to disk self.newdb.commit() self.newdb.renewConnection() # # set the default installaction # if virt_type == 'para': self.command('set.host.installaction', [nodename, 'install vm']) # HVMs boot just like real hardware if virt_type == 'hvm' and rocks.version_major == '7': # No tracker is the default install for VMs # Rocks 7 self.command('set.host.installaction', [nodename, 'install notracker']) self.command('add.host.attr', [nodename, 'UseTracker', 'False']) self.command('set.host.runaction', [nodename, 'os']) # if virt_type == 'hvm' and rocks.version_major != '7': self.command('set.host.installaction', [nodename, 'install']) self.command('set.host.runaction', [nodename, 'os']) # set the first boot state to 'install' # self.command('set.host.boot', [nodename, 'action=install']) # # print the name of the new VM # self.beginOutput() self.addOutput( '', 'added VM %s on physical node %s' % (nodename, node.name)) self.endOutput()
def run(self, params, args): (args, bucket) = self.fillPositionalArgs(('s3bucket', )) hosts = self.getHostnames(args) #some arguments parsing if len(hosts) != 1: self.abort('must supply only one host') else: host = hosts[0] (credentialDir, outputpath, imagename, location) = self.fillParams([('credentialdir', '~/.ec2'), ('outputpath', ), ('imagename', 'image'), ('location', 'US')]) # # the name of the physical host that will boot # this VM host # rows = self.db.execute("""select vn.physnode from vm_nodes vn, nodes n where n.name = '%s' and n.id = vn.node""" % (host)) if rows == 1: physnodeid, = self.db.fetchone() else: self.abort("Impossible to fetch the physical node.") rows = self.db.execute("""select name from nodes where id = %s""" % (physnodeid)) if rows == 1: physhost, = self.db.fetchone() else: self.abort("Impossible to fetch the physical node.") #which outputpath should we use...??? if not outputpath: # find the largest partition on the remote node # and use it as the directory prefix import rocks.vm vm = rocks.vm.VM(self.db) vbd_type = 'file' prefix = vm.getLargestPartition(physhost) if not prefix: self.abort('could not find a partition on ' + 'host (%s) to hold the ' % host + 'VM\'s bundle') outputpath = prefix outputpath = outputpath + "/ec2/bundles/" + host # 2. -------------------- copy the credential over... print "Copying credential directory" retval = os.system('scp -qr %s/* %s:%s/.ec2 ' % (credentialDir, physhost, outputpath)) if retval != 0: self.abort('Could not copy the credential directory: ' + credentialDir + ' to the output path: ' + outputpath) # ------------------------ create the script print "Creating the script" manifest = outputpath + "/%s.manifest.xml" % imagename awsid = outputpath + "/.ec2/access-key" secretkey = outputpath + "/.ec2/access-key-secret" scriptTemp = self.createScript(location, bucket, manifest, awsid, secretkey) retval = os.system('scp -qr %s %s:%s/upload-script.sh ' % (scriptTemp, physhost, outputpath)) if retval != 0: self.abort('Could not copy the script to the host: ' + physhost) # ----------------------- run the script #execute the upload script print "Running the upload script this step may take 30-60 minutes" print " depending on your connection to S3" output = self.command( 'run.host', [physhost, "%s/upload-script.sh" % outputpath, 'collate=true']) print output