autoserv_parser.py revision b669cbdea7f4bfc9cbfcea48d9cf9ce62f0deff5
1# pylint: disable-msg=C0111
2
3import os, shlex, sys, optparse
4
5from autotest_lib.client.common_lib import host_protections, utils
6
7
8class base_autoserv_parser(object):
9    """Custom command-line options parser for autoserv.
10
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:]
18        self.parser = optparse.OptionParser(usage="%prog [options] [control-file]")
19        self.setup_options()
20
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
27
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")
38        self.parser.add_option("-s", action="store_true",
39                               dest="server", default=False,
40                               help="control file is server side")
41        self.parser.add_option("-r", action="store", type="string",
42                               dest="results", default=None,
43                               help="specify results directory")
44        self.parser.add_option("-l", action="store", type="string",
45                               dest="label", default='',
46                               help="label for the job")
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")
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='',
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.")
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")
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")
76        self.parser.add_option("-C", "--cleanup", action="store_true",
77                               default=False,
78                               help="cleanup all machines after the job")
79        self.parser.add_option("--provision", action="store_true",
80                               default=False,
81                               help="Provision the machine.")
82        self.parser.add_option("--job-labels", action="store",
83                               help="Comma seperated job labels.")
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")
88        self.parser.add_option("-n", action="store_true",
89                               dest="no_tee", default=False,
90                               help="no teeing the status to stdout/err")
91        self.parser.add_option("-N", action="store_true",
92                               dest="no_logging", default=False,
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')
99        self.parser.add_option("-p", "--write-pidfile", action="store_true",
100                               dest="write_pidfile", default=False,
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")
111        self.parser.add_option("-a", "--args", dest='args',
112                               help="additional args to pass to control file")
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")
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"))
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"))
139        self.parser.add_option("--collect-crashinfo", action="store_true",
140                               dest="collect_crashinfo", default=False,
141                               help="just run crashinfo collection")
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"))
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]"))
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."))
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."))
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.")
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]"))
167        self.parser.add_option("--ssh_options", action="store",
168                               dest="ssh_options", default='',
169                               help=("A string giving command line flags "
170                                     "that will be included in ssh commands"))
171        self.parser.add_option("--require-ssp", action="store_true",
172                               dest="require_ssp", default=False,
173                               help=("Force the autoserv process to run with "
174                                     "server-side packaging"))
175        self.parser.add_option("--warn-no-ssp", action="store_true",
176                               dest="warn_no_ssp", default=False,
177                               help=("Post a warning in autoserv log that the "
178                                     "process runs in a drone without server-"
179                                     "side packaging support, even though the "
180                                     "job requires server-side packaging"))
181        self.parser.add_option("--no_use_packaging", action="store_true",
182                               dest="no_use_packaging", default=False,
183                               help=("Disable install modes that use the "
184                                     "packaging system."))
185
186
187    def parse_args(self):
188        self.options, self.args = self.parser.parse_args()
189        if self.options.args:
190            self.args += shlex.split(self.options.args)
191
192
193site_autoserv_parser = utils.import_site_class(
194    __file__, "autotest_lib.server.site_autoserv_parser",
195    "site_autoserv_parser", base_autoserv_parser)
196
197class autoserv_parser(site_autoserv_parser):
198    pass
199
200
201# create the one and only one instance of autoserv_parser
202autoserv_parser = autoserv_parser()
203