Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1 | # pylint: disable-msg=C0111 |
| 2 | |
Aviv Keshet | 8c1bb42 | 2013-09-13 17:13:36 -0700 | [diff] [blame] | 3 | import os, shlex, sys, optparse |
mbligh | b7dcc7f | 2008-06-02 19:34:25 +0000 | [diff] [blame] | 4 | |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 5 | from autotest_lib.client.common_lib import host_protections, utils |
jadmanski | fbc1f0a | 2008-07-09 14:12:54 +0000 | [diff] [blame] | 6 | |
mbligh | b7dcc7f | 2008-06-02 19:34:25 +0000 | [diff] [blame] | 7 | |
| 8 | class base_autoserv_parser(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 9 | """Custom command-line options parser for autoserv. |
mbligh | b7dcc7f | 2008-06-02 19:34:25 +0000 | [diff] [blame] | 10 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 11 | We can't use the general getopt methods here, as there will be unknown |
| 12 | extra arguments that we pass down into the control file instead. |
| 13 | Thus we process the arguments by hand, for which we are duly repentant. |
| 14 | Making a single function here just makes it harder to read. Suck it up. |
| 15 | """ |
| 16 | def __init__(self): |
| 17 | self.args = sys.argv[1:] |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 18 | self.parser = optparse.OptionParser(usage="%prog [options] [control-file]") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 19 | self.setup_options() |
mbligh | b7dcc7f | 2008-06-02 19:34:25 +0000 | [diff] [blame] | 20 | |
mbligh | f82a182 | 2009-02-26 00:47:14 +0000 | [diff] [blame] | 21 | # parse an empty list of arguments in order to set self.options |
| 22 | # to default values so that codepaths that assume they are always |
| 23 | # reached from an autoserv process (when they actually are not) |
| 24 | # will still work |
| 25 | self.options, self.args = self.parser.parse_args(args=[]) |
| 26 | |
mbligh | b7dcc7f | 2008-06-02 19:34:25 +0000 | [diff] [blame] | 27 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 28 | def setup_options(self): |
| 29 | self.parser.add_option("-m", action="store", type="string", |
| 30 | dest="machines", |
| 31 | help="list of machines") |
| 32 | self.parser.add_option("-M", action="store", type="string", |
| 33 | dest="machines_file", |
| 34 | help="list of machines from file") |
| 35 | self.parser.add_option("-c", action="store_true", |
| 36 | dest="client", default=False, |
| 37 | help="control file is client side") |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 38 | self.parser.add_option("-s", action="store_true", |
| 39 | dest="server", default=False, |
| 40 | help="control file is server side") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 41 | self.parser.add_option("-r", action="store", type="string", |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 42 | dest="results", default=None, |
| 43 | help="specify results directory") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 44 | self.parser.add_option("-l", action="store", type="string", |
| 45 | dest="label", default='', |
| 46 | help="label for the job") |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 47 | self.parser.add_option("-G", action="store", type="string", |
| 48 | dest="group_name", default='', |
| 49 | help="The host_group_name to store in keyvals") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 50 | self.parser.add_option("-u", action="store", type="string", |
| 51 | dest="user", |
| 52 | default=os.environ.get('USER'), |
| 53 | help="username for the job") |
| 54 | self.parser.add_option("-P", action="store", type="string", |
| 55 | dest="parse_job", |
| 56 | default='', |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 57 | help="Parse the results of the job using this " |
| 58 | "execution tag. Accessable in control " |
| 59 | "files as job.tag.") |
| 60 | self.parser.add_option("--execution-tag", action="store", type="string", |
| 61 | dest="execution_tag", default='', |
| 62 | help="Accessable in control files as job.tag; " |
| 63 | "Defaults to the value passed to -P.") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 64 | self.parser.add_option("-i", action="store_true", |
| 65 | dest="install_before", default=False, |
| 66 | help="reinstall machines before running the job") |
| 67 | self.parser.add_option("-I", action="store_true", |
| 68 | dest="install_after", default=False, |
| 69 | help="reinstall machines after running the job") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 70 | self.parser.add_option("-v", action="store_true", |
| 71 | dest="verify", default=False, |
| 72 | help="verify the machines only") |
| 73 | self.parser.add_option("-R", action="store_true", |
| 74 | dest="repair", default=False, |
| 75 | help="repair the machines") |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 76 | self.parser.add_option("-C", "--cleanup", action="store_true", |
| 77 | default=False, |
| 78 | help="cleanup all machines after the job") |
Alex Miller | df15ec5 | 2014-02-28 18:18:48 -0800 | [diff] [blame] | 79 | self.parser.add_option("--provision", action="store_true", |
| 80 | default=False, |
Alex Miller | 667b5f2 | 2014-02-28 15:33:39 -0800 | [diff] [blame] | 81 | help="Provision the machine.") |
| 82 | self.parser.add_option("--job-labels", action="store", |
| 83 | help="Comma seperated job labels.") |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 84 | self.parser.add_option("-T", "--reset", action="store_true", |
| 85 | default=False, |
| 86 | help="Reset (cleanup and verify) all machines" |
| 87 | "after the job") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 88 | self.parser.add_option("-n", action="store_true", |
| 89 | dest="no_tee", default=False, |
showard | 10d8417 | 2009-06-18 23:16:50 +0000 | [diff] [blame] | 90 | help="no teeing the status to stdout/err") |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 91 | self.parser.add_option("-N", action="store_true", |
| 92 | dest="no_logging", default=False, |
showard | 10d8417 | 2009-06-18 23:16:50 +0000 | [diff] [blame] | 93 | help="no logging") |
| 94 | self.parser.add_option('--verbose', action='store_true', |
| 95 | help='Include DEBUG messages in console output') |
| 96 | self.parser.add_option('--no_console_prefix', action='store_true', |
| 97 | help='Disable the logging prefix on console ' |
| 98 | 'output') |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 99 | self.parser.add_option("-p", "--write-pidfile", action="store_true", |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 100 | dest="write_pidfile", default=False, |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 101 | help="write pidfile (pidfile name is determined " |
| 102 | "by --pidfile-label") |
| 103 | self.parser.add_option("--pidfile-label", action="store", |
| 104 | default="autoserv", |
| 105 | help="Determines filename to use as pidfile (if " |
| 106 | "-p is specified). Pidfile will be " |
| 107 | ".<label>_execute. Default to autoserv.") |
| 108 | self.parser.add_option("--use-existing-results", action="store_true", |
| 109 | help="Indicates that autoserv is working with " |
| 110 | "an existing results directory") |
mbligh | a85d467 | 2009-05-13 20:43:54 +0000 | [diff] [blame] | 111 | self.parser.add_option("-a", "--args", dest='args', |
| 112 | help="additional args to pass to control file") |
jadmanski | fbc1f0a | 2008-07-09 14:12:54 +0000 | [diff] [blame] | 113 | protection_levels = [host_protections.Protection.get_attr_name(s) |
| 114 | for i, s in host_protections.choices] |
| 115 | self.parser.add_option("--host-protection", action="store", |
| 116 | type="choice", dest="host_protection", |
| 117 | default=host_protections.default, |
| 118 | choices=protection_levels, |
| 119 | help="level of host protection during repair") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 120 | self.parser.add_option("--ssh-user", action="store", |
| 121 | type="string", dest="ssh_user", |
| 122 | default="root", |
| 123 | help=("specify the user for ssh" |
| 124 | "connections")) |
| 125 | self.parser.add_option("--ssh-port", action="store", |
| 126 | type="int", dest="ssh_port", |
| 127 | default=22, |
| 128 | help=("specify the port to use for " |
| 129 | "ssh connections")) |
| 130 | self.parser.add_option("--ssh-pass", action="store", |
| 131 | type="string", dest="ssh_pass", |
| 132 | default="", |
| 133 | help=("specify the password to use " |
| 134 | "for ssh connections")) |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 135 | self.parser.add_option("--install-in-tmpdir", action="store_true", |
| 136 | dest="install_in_tmpdir", default=False, |
| 137 | help=("by default install autotest clients in " |
| 138 | "a temporary directory")) |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 139 | self.parser.add_option("--collect-crashinfo", action="store_true", |
| 140 | dest="collect_crashinfo", default=False, |
| 141 | help="just run crashinfo collection") |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 142 | self.parser.add_option("--control-filename", action="store", |
| 143 | type="string", default=None, |
| 144 | help=("filename to use for the server control " |
| 145 | "file in the results directory")) |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 146 | self.parser.add_option("--test-retry", action="store", |
| 147 | type="int", default=0, |
| 148 | help=("Num of times to retry a test that failed " |
| 149 | "[default: %default]")) |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 150 | self.parser.add_option("--verify_job_repo_url", action="store_true", |
| 151 | dest="verify_job_repo_url", default=False, |
| 152 | help=("Verify that the job_repo_url of the host " |
| 153 | "has staged packages for the job.")) |
Christopher Wiley | f594c5e | 2013-07-03 18:25:30 -0700 | [diff] [blame] | 154 | self.parser.add_option("--no_collect_crashinfo", action="store_true", |
| 155 | dest="skip_crash_collection", default=False, |
| 156 | help=("Turns off crash collection to shave time " |
| 157 | "off test runs.")) |
Christopher Wiley | 8a91f23 | 2013-07-09 11:02:27 -0700 | [diff] [blame] | 158 | self.parser.add_option("--disable_sysinfo", action="store_true", |
| 159 | dest="disable_sysinfo", default=False, |
| 160 | help="Turns off sysinfo collection to shave " |
| 161 | "time off test runs.") |
Aviv Keshet | 18ee314 | 2013-08-12 15:01:51 -0700 | [diff] [blame] | 162 | self.parser.add_option("--ssh_verbosity", action="store", |
| 163 | dest="ssh_verbosity", default=0, |
| 164 | type="choice", choices=["0", "1", "2", "3"], |
| 165 | help=("Verbosity level for ssh, between 0 " |
| 166 | "and 3 inclusive. [default: 0]")) |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 167 | self.parser.add_option("--ssh_options", action="store", |
Fang Deng | 6cc20de | 2013-09-06 15:47:32 -0700 | [diff] [blame] | 168 | dest="ssh_options", default='', |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 169 | help=("A string giving command line flags " |
| 170 | "that will be included in ssh commands")) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 171 | |
| 172 | |
| 173 | def parse_args(self): |
mbligh | f82a182 | 2009-02-26 00:47:14 +0000 | [diff] [blame] | 174 | self.options, self.args = self.parser.parse_args() |
mbligh | 012623f | 2009-05-13 20:44:26 +0000 | [diff] [blame] | 175 | if self.options.args: |
Aviv Keshet | 8c1bb42 | 2013-09-13 17:13:36 -0700 | [diff] [blame] | 176 | self.args += shlex.split(self.options.args) |
mbligh | b7dcc7f | 2008-06-02 19:34:25 +0000 | [diff] [blame] | 177 | |
| 178 | |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 179 | site_autoserv_parser = utils.import_site_class( |
| 180 | __file__, "autotest_lib.server.site_autoserv_parser", |
| 181 | "site_autoserv_parser", base_autoserv_parser) |
mbligh | b7dcc7f | 2008-06-02 19:34:25 +0000 | [diff] [blame] | 182 | |
| 183 | class autoserv_parser(site_autoserv_parser): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 184 | pass |
mbligh | b7dcc7f | 2008-06-02 19:34:25 +0000 | [diff] [blame] | 185 | |
| 186 | |
| 187 | # create the one and only one instance of autoserv_parser |
| 188 | autoserv_parser = autoserv_parser() |