aboutsummaryrefslogtreecommitdiff
path: root/automation/common/machine.py
blob: 4db0db0dd37644e1e85b91415312b4a7a6fddc14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright 2010 Google Inc. All Rights Reserved.

__author__ = 'asharif@google.com (Ahmad Sharif)'

from fnmatch import fnmatch


class Machine(object):
  """Stores information related to machine and its state."""

  def __init__(self, hostname, label, cpu, cores, os, username):
    self.hostname = hostname
    self.label = label
    self.cpu = cpu
    self.cores = cores
    self.os = os
    self.username = username

    # MachineManager related attributes.
    self.uses = 0
    self.locked = False

  def Acquire(self, exclusively):
    assert not self.locked

    if exclusively:
      self.locked = True
    self.uses += 1

  def Release(self):
    assert self.uses > 0

    self.uses -= 1

    if not self.uses:
      self.locked = False

  def __repr__(self):
    return '{%s: %s@%s}' % (self.__class__.__name__, self.username,
                            self.hostname)

  def __str__(self):
    return '\n'.join(
        ['Machine Information:', 'Hostname: %s' % self.hostname, 'Label: %s' %
         self.label, 'CPU: %s' % self.cpu, 'Cores: %d' % self.cores, 'OS: %s' %
         self.os, 'Uses: %d' % self.uses, 'Locked: %s' % self.locked])


class MachineSpecification(object):
  """Helper class used to find a machine matching your requirements."""

  def __init__(self, hostname='*', label='*', os='*', lock_required=False):
    self.hostname = hostname
    self.label = label
    self.os = os
    self.lock_required = lock_required
    self.preferred_machines = []

  def __str__(self):
    return '\n'.join(['Machine Specification:', 'Name: %s' % self.name, 'OS: %s'
                      % self.os, 'Lock required: %s' % self.lock_required])

  def IsMatch(self, machine):
    return all([not machine.locked, fnmatch(machine.hostname, self.hostname),
                fnmatch(machine.label, self.label), fnmatch(machine.os,
                                                            self.os)])

  def AddPreferredMachine(self, hostname):
    if hostname not in self.preferred_machines:
      self.preferred_machines.append(hostname)