blob: 4564fc1fc1b746a4ef679d176c3f925c635a6ce4 [file] [log] [blame]
mblighdcd57a82007-07-11 23:06:47 +00001#!/usr/bin/python
2#
3# Copyright 2007 Google Inc. Released under the GPL v2
4
5"""This module defines the base classes for the Host hierarchy.
6
7Implementation details:
8You should import the "hosts" package instead of importing each type of host.
9
10 Host: a machine on which you can run programs
11 RemoteHost: a remote machine on which you can run programs
12 CmdResult: contain the results of a Host.run() command execution
13"""
14
15__author__ = """mbligh@google.com (Martin J. Bligh),
16poirier@google.com (Benjamin Poirier),
17stutsman@google.com (Ryan Stutsman)"""
18
19
20import time
21import textwrap
mbligh6a641262007-07-23 23:33:31 +000022import bootloader
mblighdcd57a82007-07-11 23:06:47 +000023
24class Host(object):
25 """This class represents a machine on which you can run programs.
26
27 It may be a local machine, the one autoserv is running on, a remote
28 machine or a virtual machine.
29
30 Implementation details:
31 This is an abstract class, leaf subclasses must implement the methods
32 listed here. You must not instantiate this class but should
33 instantiate one of those leaf subclasses."""
34
35 bootloader = None
36
37 def __init__(self):
38 super(Host, self).__init__()
mbligh6a641262007-07-23 23:33:31 +000039 self.bootloader= bootloader.Bootloader(self)
mblighdcd57a82007-07-11 23:06:47 +000040
41 def run(self, command):
42 pass
43
44 def reboot(self):
45 pass
46
47 def get_file(self, source, dest):
48 pass
49
50 def send_file(self, source, dest):
51 pass
52
53 def get_tmp_dir(self):
54 pass
55
56 def is_up(self):
57 pass
58
59 def wait_up(self, timeout):
60 pass
61
62 def wait_down(self, timeout):
63 pass
64
65 def get_num_cpu(self):
66 pass
67
68 def install(self, installableObject):
69 installableObject.install(self)
70
71
72# site_host.py may be non-existant or empty, make sure that an appropriate
73# SiteHost class is created nevertheless
74try:
75 from site_host import SiteHost
76except ImportError:
77 pass
78
79if not "SiteHost" in dir():
80 class SiteHost(Host):
81 def __init__(self):
82 super(SiteHost, self).__init__()
83
84
85class RemoteHost(SiteHost):
86 """This class represents a remote machine on which you can run
87 programs.
88
89 It may be accessed through a network, a serial line, ...
90 It is not the machine autoserv is running on.
91
92 Implementation details:
93 This is an abstract class, leaf subclasses must implement the methods
94 listed here and in parent classes which have no implementation. They
95 may reimplement methods which already have an implementation. You
96 must not instantiate this class but should instantiate one of those
97 leaf subclasses."""
98
99 hostname= None
100
101 def __init__(self):
102 super(RemoteHost, self).__init__()
103
104
105class CmdResult(object):
106 """
107 Command execution result.
108
109 Modified from the original Autoserv code, local_cmd.py:
110 Copyright jonmayer@google.com (Jonathan Mayer),
111 mbligh@google.com (Martin J. Bligh)
112 Released under the GPL, v2
113
114 command: String containing the command line itself
115 exit_status: Integer exit code of the process
116 stdout: String containing stdout of the process
117 stderr: String containing stderr of the process
118 duration: Elapsed wall clock time running the process
119 aborted: Signal that caused the command to terminate (0 if none)
120 """
121
122 def __init__(self):
123 super(CmdResult, self).__init__()
124 self.command = ""
125 self.exit_status = None
126 self.stdout = ""
127 self.stderr = ""
128 self.duration = 0
129 self.aborted= False
130
131 def __repr__(self):
132 wrapper= textwrap.TextWrapper(width=78,
133 initial_indent="\n ", subsequent_indent=" ")
134
135 stdout= self.stdout.rstrip(" \n")
136 if stdout:
137 stdout= "\nStdout:\n%s" % (stdout,)
138
139 stderr= self.stderr.rstrip(" \n")
140 if stderr:
141 stderr= "\nStderr:\n%s" % (stderr,)
142
143 return ("* Command: %s\n"
144 "Exit status: %s\n"
145 "Duration: %s\n"
146 "Aborted: %s"
147 "%s"
148 "%s"
149 % (wrapper.fill(self.command), self.exit_status,
150 self.duration, self.aborted, stdout, stderr))