• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2010 Google Inc. All Rights Reserved.
2
3__author__ = 'asharif@google.com (Ahmad Sharif)'
4
5from fnmatch import fnmatch
6
7
8class Machine(object):
9  """Stores information related to machine and its state."""
10
11  def __init__(self, hostname, label, cpu, cores, os, username):
12    self.hostname = hostname
13    self.label = label
14    self.cpu = cpu
15    self.cores = cores
16    self.os = os
17    self.username = username
18
19    # MachineManager related attributes.
20    self.uses = 0
21    self.locked = False
22
23  def Acquire(self, exclusively):
24    assert not self.locked
25
26    if exclusively:
27      self.locked = True
28    self.uses += 1
29
30  def Release(self):
31    assert self.uses > 0
32
33    self.uses -= 1
34
35    if not self.uses:
36      self.locked = False
37
38  def __repr__(self):
39    return '{%s: %s@%s}' % (self.__class__.__name__, self.username,
40                            self.hostname)
41
42  def __str__(self):
43    return '\n'.join(
44        ['Machine Information:', 'Hostname: %s' % self.hostname, 'Label: %s' %
45         self.label, 'CPU: %s' % self.cpu, 'Cores: %d' % self.cores, 'OS: %s' %
46         self.os, 'Uses: %d' % self.uses, 'Locked: %s' % self.locked])
47
48
49class MachineSpecification(object):
50  """Helper class used to find a machine matching your requirements."""
51
52  def __init__(self, hostname='*', label='*', os='*', lock_required=False):
53    self.hostname = hostname
54    self.label = label
55    self.os = os
56    self.lock_required = lock_required
57    self.preferred_machines = []
58
59  def __str__(self):
60    return '\n'.join(['Machine Specification:', 'Name: %s' % self.name, 'OS: %s'
61                      % self.os, 'Lock required: %s' % self.lock_required])
62
63  def IsMatch(self, machine):
64    return all([not machine.locked, fnmatch(machine.hostname, self.hostname),
65                fnmatch(machine.label, self.label), fnmatch(machine.os,
66                                                            self.os)])
67
68  def AddPreferredMachine(self, hostname):
69    if hostname not in self.preferred_machines:
70      self.preferred_machines.append(hostname)
71