[209] | 1 | import validation |
---|
[1612] | 2 | from invirt.common import CodeError, InvalidInput |
---|
[209] | 3 | import random |
---|
| 4 | import sys |
---|
| 5 | import time |
---|
| 6 | import re |
---|
[265] | 7 | import cache_acls |
---|
[550] | 8 | import yaml |
---|
[209] | 9 | |
---|
[863] | 10 | from invirt.config import structs as config |
---|
[1001] | 11 | from invirt.database import Machine, Disk, Type, NIC, CDROM, session, meta |
---|
[1614] | 12 | from invirt.remctl import remctl as gen_remctl |
---|
[863] | 13 | |
---|
[209] | 14 | # ... and stolen from xend/uuid.py |
---|
| 15 | def randomUUID(): |
---|
| 16 | """Generate a random UUID.""" |
---|
| 17 | |
---|
| 18 | return [ random.randint(0, 255) for _ in range(0, 16) ] |
---|
| 19 | |
---|
| 20 | def uuidToString(u): |
---|
| 21 | """Turn a numeric UUID to a hyphen-seperated one.""" |
---|
| 22 | return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, |
---|
| 23 | "%02x" * 6]) % tuple(u) |
---|
| 24 | # end stolen code |
---|
| 25 | |
---|
[1614] | 26 | def remctl(*args, **kwargs): |
---|
[1618] | 27 | return gen_remctl(config.remote.hostname, |
---|
[1614] | 28 | principal='daemon/'+config.web.hostname, |
---|
[1618] | 29 | *args, **kwargs) |
---|
[1614] | 30 | |
---|
[209] | 31 | def lvcreate(machine, disk): |
---|
| 32 | """Create a single disk for a machine""" |
---|
| 33 | remctl('web', 'lvcreate', machine.name, |
---|
| 34 | disk.guest_device_name, str(disk.size)) |
---|
| 35 | |
---|
| 36 | def makeDisks(machine): |
---|
| 37 | """Update the lvm partitions to add a disk.""" |
---|
| 38 | for disk in machine.disks: |
---|
| 39 | lvcreate(machine, disk) |
---|
| 40 | |
---|
[629] | 41 | def getswap(disksize, memsize): |
---|
| 42 | """Returns the recommended swap partition size.""" |
---|
| 43 | return int(min(disksize / 4, memsize * 1.5)) |
---|
| 44 | |
---|
| 45 | def lvinstall(machine, autoinstall): |
---|
| 46 | disksize = machine.disks[0].size |
---|
| 47 | memsize = machine.memory |
---|
[1096] | 48 | swapsize = getswap(disksize, memsize) |
---|
| 49 | imagesize = disksize - swapsize |
---|
[629] | 50 | ip = machine.nics[0].ip |
---|
[1096] | 51 | remctl('control', machine.name, 'install', |
---|
| 52 | 'dist=%s' % autoinstall.distribution, |
---|
| 53 | 'mirror=%s' % autoinstall.mirror, |
---|
[1695] | 54 | 'arch=%s' % autoinstall.arch, |
---|
[1096] | 55 | 'imagesize=%s' % imagesize) |
---|
[629] | 56 | |
---|
[340] | 57 | def lvcopy(machine_orig_name, machine, rootpw): |
---|
| 58 | """Copy a golden image onto a machine's disk""" |
---|
| 59 | remctl('web', 'lvcopy', machine_orig_name, machine.name, rootpw) |
---|
| 60 | |
---|
[209] | 61 | def bootMachine(machine, cdtype): |
---|
| 62 | """Boot a machine with a given boot CD. |
---|
| 63 | |
---|
| 64 | If cdtype is None, give no boot cd. Otherwise, it is the string |
---|
| 65 | id of the CD (e.g. 'gutsy_i386') |
---|
| 66 | """ |
---|
[2097] | 67 | if cdtype is not None: |
---|
| 68 | out, err = remctl('control', machine.name, 'create', |
---|
| 69 | cdtype, err=True) |
---|
| 70 | else: |
---|
| 71 | out, err = remctl('control', machine.name, 'create', |
---|
| 72 | err=True) |
---|
| 73 | if 'already running' in err: |
---|
| 74 | raise InvalidInput('action', 'create', |
---|
| 75 | 'VM %s is already on' % machine.name) |
---|
| 76 | elif err: |
---|
[261] | 77 | raise CodeError('"%s" on "control %s create %s' |
---|
| 78 | % (err, machine.name, cdtype)) |
---|
[209] | 79 | |
---|
[629] | 80 | def createVm(username, state, owner, contact, name, description, memory, disksize, machine_type, cdrom, autoinstall): |
---|
[209] | 81 | """Create a VM and put it in the database""" |
---|
| 82 | # put stuff in the table |
---|
[1013] | 83 | session.begin() |
---|
[209] | 84 | try: |
---|
[609] | 85 | validation.Validate(username, state, name=name, description=description, owner=owner, memory=memory, disksize=disksize/1024.) |
---|
[209] | 86 | machine = Machine() |
---|
| 87 | machine.name = name |
---|
[609] | 88 | machine.description = description |
---|
[209] | 89 | machine.memory = memory |
---|
[228] | 90 | machine.owner = owner |
---|
[1709] | 91 | machine.administrator = None |
---|
[228] | 92 | machine.contact = contact |
---|
[209] | 93 | machine.uuid = uuidToString(randomUUID()) |
---|
| 94 | machine.boot_off_cd = True |
---|
[1013] | 95 | machine.type = machine_type |
---|
| 96 | session.save_or_update(machine) |
---|
| 97 | disk = Disk(machine=machine, |
---|
[572] | 98 | guest_device_name='hda', size=disksize) |
---|
[2211] | 99 | nic = NIC.query().filter_by(machine_id=None).filter_by(reusable=True).first() |
---|
[1013] | 100 | if not nic: #No IPs left! |
---|
[209] | 101 | raise CodeError("No IP addresses left! " |
---|
[879] | 102 | "Contact %s." % config.web.errormail) |
---|
[1013] | 103 | nic.machine = machine |
---|
[209] | 104 | nic.hostname = name |
---|
[1013] | 105 | session.save_or_update(nic) |
---|
| 106 | session.save_or_update(disk) |
---|
[265] | 107 | cache_acls.refreshMachine(machine) |
---|
[2295] | 108 | makeDisks(machine) |
---|
[1013] | 109 | session.commit() |
---|
[209] | 110 | except: |
---|
[1013] | 111 | session.rollback() |
---|
[209] | 112 | raise |
---|
[2020] | 113 | try: |
---|
| 114 | if autoinstall: |
---|
| 115 | lvinstall(machine, autoinstall) |
---|
| 116 | else: |
---|
| 117 | # tell it to boot with cdrom |
---|
| 118 | bootMachine(machine, cdrom) |
---|
| 119 | except CodeError, e: |
---|
| 120 | deleteVM(machine) |
---|
| 121 | raise |
---|
[209] | 122 | return machine |
---|
| 123 | |
---|
[554] | 124 | def getList(): |
---|
| 125 | """Return a dictionary mapping machine names to dicts.""" |
---|
[550] | 126 | value_string = remctl('web', 'listvms') |
---|
[574] | 127 | value_dict = yaml.load(value_string, yaml.CSafeLoader) |
---|
[554] | 128 | return value_dict |
---|
[209] | 129 | |
---|
| 130 | def parseStatus(s): |
---|
| 131 | """Parse a status string into nested tuples of strings. |
---|
| 132 | |
---|
| 133 | s = output of xm list --long <machine_name> |
---|
| 134 | """ |
---|
| 135 | values = re.split('([()])', s) |
---|
| 136 | stack = [[]] |
---|
| 137 | for v in values[2:-2]: #remove initial and final '()' |
---|
| 138 | if not v: |
---|
| 139 | continue |
---|
| 140 | v = v.strip() |
---|
| 141 | if v == '(': |
---|
| 142 | stack.append([]) |
---|
| 143 | elif v == ')': |
---|
| 144 | if len(stack[-1]) == 1: |
---|
| 145 | stack[-1].append('') |
---|
| 146 | stack[-2].append(stack[-1]) |
---|
| 147 | stack.pop() |
---|
| 148 | else: |
---|
| 149 | if not v: |
---|
| 150 | continue |
---|
| 151 | stack[-1].extend(v.split()) |
---|
| 152 | return stack[-1] |
---|
| 153 | |
---|
| 154 | def statusInfo(machine): |
---|
| 155 | """Return the status list for a given machine. |
---|
| 156 | |
---|
| 157 | Gets and parses xm list --long |
---|
| 158 | """ |
---|
[2097] | 159 | value_string, err_string = remctl('control', machine.name, 'list-long', |
---|
| 160 | err=True) |
---|
| 161 | if 'Unknown command' in err_string: |
---|
| 162 | raise CodeError("ERROR in remctl list-long %s is not registered" % |
---|
| 163 | (machine.name,)) |
---|
| 164 | elif 'is not on' in err_string: |
---|
| 165 | return None |
---|
| 166 | elif err_string: |
---|
| 167 | raise CodeError("ERROR in remctl list-long %s: %s" % |
---|
| 168 | (machine.name, err_string)) |
---|
[209] | 169 | status = parseStatus(value_string) |
---|
| 170 | return status |
---|
| 171 | |
---|
[662] | 172 | def listHost(machine): |
---|
| 173 | """Return the host a machine is running on""" |
---|
[2097] | 174 | out, err = remctl('control', machine.name, 'listhost', err=True) |
---|
| 175 | if err: |
---|
[662] | 176 | return None |
---|
[666] | 177 | return out.strip() |
---|
[662] | 178 | |
---|
[1615] | 179 | def vnctoken(machine): |
---|
| 180 | """Return a time-stamped VNC token""" |
---|
[2097] | 181 | out, err = remctl('control', machine.name, 'vnctoken', err=True) |
---|
| 182 | if err: |
---|
[1615] | 183 | return None |
---|
| 184 | return out.strip() |
---|
| 185 | |
---|
[209] | 186 | def deleteVM(machine): |
---|
| 187 | """Delete a VM.""" |
---|
[2097] | 188 | remctl('control', machine.name, 'destroy', err=True) |
---|
[1013] | 189 | session.begin() |
---|
[209] | 190 | delete_disk_pairs = [(machine.name, d.guest_device_name) |
---|
| 191 | for d in machine.disks] |
---|
| 192 | try: |
---|
[1013] | 193 | for mname, dname in delete_disk_pairs: |
---|
| 194 | remctl('web', 'lvremove', mname, dname) |
---|
[209] | 195 | for nic in machine.nics: |
---|
| 196 | nic.machine_id = None |
---|
| 197 | nic.hostname = None |
---|
[1013] | 198 | session.save_or_update(nic) |
---|
[209] | 199 | for disk in machine.disks: |
---|
[1013] | 200 | session.delete(disk) |
---|
| 201 | session.delete(machine) |
---|
| 202 | session.commit() |
---|
[209] | 203 | except: |
---|
[1013] | 204 | session.rollback() |
---|
[209] | 205 | raise |
---|
| 206 | |
---|
[2684] | 207 | def commandResult(username, state, command_name, machine_id, fields): |
---|
[209] | 208 | start_time = 0 |
---|
[2684] | 209 | machine = validation.Validate(username, state, machine_id=machine_id).machine |
---|
| 210 | action = command_name |
---|
| 211 | cdrom = fields.get('cdrom') |
---|
| 212 | if not cdrom: |
---|
| 213 | cdrom = None |
---|
[1074] | 214 | if cdrom is not None and not CDROM.query().filter_by(cdrom_id=cdrom).one(): |
---|
[209] | 215 | raise CodeError("Invalid cdrom type '%s'" % cdrom) |
---|
[2684] | 216 | if action not in "reboot create destroy shutdown delete".split(" "): |
---|
[209] | 217 | raise CodeError("Invalid action '%s'" % action) |
---|
[2684] | 218 | if action == 'reboot': |
---|
[2097] | 219 | if cdrom is not None: |
---|
| 220 | out, err = remctl('control', machine.name, 'reboot', cdrom, |
---|
| 221 | err=True) |
---|
| 222 | else: |
---|
| 223 | out, err = remctl('control', machine.name, 'reboot', |
---|
| 224 | err=True) |
---|
| 225 | if err: |
---|
| 226 | if re.match("machine '.*' is not on", err): |
---|
[209] | 227 | raise InvalidInput("action", "reboot", |
---|
| 228 | "Machine is not on") |
---|
| 229 | else: |
---|
[2097] | 230 | print >> sys.stderr, 'Error on reboot:' |
---|
| 231 | print >> sys.stderr, err |
---|
| 232 | raise CodeError('ERROR on remctl') |
---|
[209] | 233 | |
---|
[2684] | 234 | elif action == 'create': |
---|
[572] | 235 | if validation.maxMemory(username, state, machine) < machine.memory: |
---|
[209] | 236 | raise InvalidInput('action', 'Power on', |
---|
| 237 | "You don't have enough free RAM quota " |
---|
| 238 | "to turn on this machine.") |
---|
| 239 | bootMachine(machine, cdrom) |
---|
[2684] | 240 | elif action == 'destroy': |
---|
[2097] | 241 | out, err = remctl('control', machine.name, 'destroy', err=True) |
---|
| 242 | if err: |
---|
| 243 | if re.match("machine '.*' is not on", err): |
---|
[209] | 244 | raise InvalidInput("action", "Power off", |
---|
| 245 | "Machine is not on.") |
---|
| 246 | else: |
---|
[2097] | 247 | print >> sys.stderr, 'Error on power off:' |
---|
| 248 | print >> sys.stderr, err |
---|
| 249 | raise CodeError('ERROR on remctl') |
---|
[2684] | 250 | elif action == 'shutdown': |
---|
[2097] | 251 | out, err = remctl('control', machine.name, 'shutdown', err=True) |
---|
| 252 | if err: |
---|
| 253 | if re.match("machine '.*' is not on", err): |
---|
[209] | 254 | raise InvalidInput("action", "Shutdown", |
---|
| 255 | "Machine is not on.") |
---|
| 256 | else: |
---|
[2097] | 257 | print >> sys.stderr, 'Error on Shutdown:' |
---|
| 258 | print >> sys.stderr, err |
---|
| 259 | raise CodeError('ERROR on remctl') |
---|
[2684] | 260 | elif action == 'delete': |
---|
[209] | 261 | deleteVM(machine) |
---|
| 262 | |
---|
[572] | 263 | d = dict(user=username, |
---|
[209] | 264 | command=action, |
---|
| 265 | machine=machine) |
---|
| 266 | return d |
---|
| 267 | |
---|
| 268 | def resizeDisk(machine_name, disk_name, new_size): |
---|
| 269 | remctl("web", "lvresize", machine_name, disk_name, new_size) |
---|
| 270 | |
---|
| 271 | def renameMachine(machine, old_name, new_name): |
---|
| 272 | for disk in machine.disks: |
---|
| 273 | remctl("web", "lvrename", old_name, |
---|
| 274 | disk.guest_device_name, new_name) |
---|
| 275 | |
---|