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