dee33fc5e56ee3b5964c7be8612f35e86d8a4555
[barrelfish] / tools / harness / machines / eth.py
1 ##########################################################################
2 # Copyright (c) 2009-2016 ETH Zurich.
3 # All rights reserved.
4 #
5 # This file is distributed under the terms in the attached LICENSE file.
6 # If you do not find this file, copies can be found by writing to:
7 # ETH Zurich D-INFK, Universitaetstr 6, CH-8092 Zurich. Attn: Systems Group.
8 ##########################################################################
9
10 import os, getpass, subprocess, socket, pty
11 import debug, eth_machinedata
12 from machines import Machine, MachineLockedError, MachineFactory,\
13     MachineOperations
14
15 from subprocess_timeout import wait_or_terminate
16
17 TFTP_PATH='/home/netos/tftpboot'
18 TOOLS_PATH='/home/netos/tools/bin'
19 RACKBOOT=os.path.join(TOOLS_PATH, 'rackboot.sh')
20 RACKPOWER=os.path.join(TOOLS_PATH, 'rackpower')
21
22 class ETHBaseMachine(Machine):
23     _machines = None
24
25     def __init__(self, options,
26                  operations,
27                  serial_binary='serial_pc16550d',
28                  **kwargs):
29
30         super(ETHBaseMachine, self).__init__(options, operations,
31                                              serial_binary=serial_binary,
32                                              **kwargs)
33
34     def get_perfcount_type(self):
35         return self._perfcount_type
36
37 class ETHBaseMachineOperations(MachineOperations):
38
39     def __init__(self, machine):
40         super(ETHBaseMachineOperations, self).__init__(machine)
41         self.lockprocess = None
42         self.masterfd = None
43
44     def _get_console_status(self):
45         raise NotImplementedError
46
47     def lock(self):
48         """Use conserver to lock the machine."""
49
50         # find out current status of console
51         cstate = self._get_console_status()
52         # check that nobody else has it open for writing
53         myuser = getpass.getuser()
54         parts = cstate.strip().split(':')
55         conname, child, contype, details, users, state = parts[:6]
56         if users:
57             for userinfo in users.split(','):
58                 mode, username, host, port = userinfo.split('@')[:4]
59                 if 'w' in mode and username != myuser:
60                     raise MachineLockedError # Machine is not free
61
62         # run a console in the background to 'hold' the lock and read output
63         debug.verbose('starting "console %s"' % self._machine.get_machine_name())
64         # run on a PTY to work around terminal mangling code in console
65         (self.masterfd, slavefd) = pty.openpty()
66         self.lockprocess = subprocess.Popen(["console", self._machine.get_machine_name()],
67                                             close_fds=True,
68                                             stdout=slavefd, stdin=slavefd)
69         os.close(slavefd)
70         # XXX: open in binary mode with no buffering
71         # otherwise select.select() may block when there is data in the buffer
72         self.console_out = os.fdopen(self.masterfd, 'rb', 0)
73
74     def unlock(self):
75         if self.lockprocess is None:
76             return # noop
77         debug.verbose('quitting console process (%d)' % self.lockprocess.pid)
78         # os.kill(self.lockprocess.pid, signal.SIGTERM)
79         os.write(self.masterfd, "\x05c.")
80         wait_or_terminate(self.lockprocess)
81         self.lockprocess = None
82         self.masterfd = None
83
84     # this expects a pexpect object for `consolectrl`
85     def force_write(self, consolectrl):
86         try:
87             consolectrl.send('\x05cf')
88         except:
89             print "Unable to force write control through consolectrl, trying masterfd"
90             os.write(self.masterfd, "\x05cf")
91
92     def get_output(self):
93         return self.console_out
94
95
96 class ETHMachine(ETHBaseMachine):
97     _machines = eth_machinedata.machines
98
99     def __init__(self, options, **kwargs):
100         super(ETHMachine, self).__init__(options, ETHMachineOperations(self), **kwargs)
101
102     def get_buildall_target(self):
103         return self.get_bootarch().upper() + "_Full"
104
105     def get_xphi_ncores(self):
106         if 'xphi_ncores' in self._machines[self.name] :
107             return self._machines[self.name]['xphi_ncores']
108         else :
109             return 0
110
111     def get_xphi_ncards(self):
112         if 'xphi_ncards' in self._machines[self.name] :
113             return self._machines[self.name]['xphi_ncards']
114         else :
115             return 0
116
117     def get_xphi_ram_gb(self):
118         if 'xphi_ram_gb' in self._machines[self.name] :
119             return self._machines[self.name]['xphi_ram_gb']
120         else :
121             return 0
122
123     def get_xphi_tickrate(self):
124         if 'xphi_tickrate' in self._machines[self.name] :
125             return self._machines[self.name]['xphi_tickrate']
126         else :
127             return 0
128
129     def get_hostname(self):
130         return self.get_machine_name() + '.in.barrelfish.org'
131
132     def get_ip(self):
133         return socket.gethostbyname(self.get_hostname())
134
135 class ETHMachineOperations(ETHBaseMachineOperations):
136
137     def __init__(self, machine):
138         super(ETHMachineOperations, self).__init__(machine)
139
140     def get_tftp_dir(self):
141         user = getpass.getuser()
142         return os.path.join(TFTP_PATH, user, self._machine.name + "_harness")
143
144     def get_tftp_subdir(self):
145         user = getpass.getuser()
146         return os.path.join(user, self._machine.name + "_harness")
147
148     def _write_menu_lst(self, data, path):
149         debug.verbose('writing %s' % path)
150         debug.debug(data)
151         with open(path, 'w') as f:
152             f.write(data)
153
154
155     def _get_menu_lst_name(self):
156         if self._machine.get_bootarch() == "armv8":
157             return "hagfish.cfg"
158         else:
159             return "menu.lst"
160
161     def _set_menu_lst(self, relpath):
162         ip_menu_name = os.path.join(TFTP_PATH, self._get_menu_lst_name() + "." + self._machine.get_ip())
163         debug.verbose('relinking %s to %s' % (ip_menu_name, relpath))
164         os.remove(ip_menu_name)
165         os.symlink(relpath, ip_menu_name)
166
167     def set_bootmodules(self, modules):
168         fullpath = os.path.join(self.get_tftp_dir(), self._get_menu_lst_name())
169         relpath = os.path.relpath(fullpath, TFTP_PATH)
170         tftppath = '/' + os.path.relpath(self.get_tftp_dir(), TFTP_PATH)
171         self._write_menu_lst(modules.get_menu_data(tftppath), fullpath)
172         self._set_menu_lst(relpath)
173
174     def _get_console_status(self):
175         debug.verbose('executing "console -i %s" to check state' %
176                       self._machine.get_machine_name())
177         proc = subprocess.Popen(["console", "-i", self._machine.get_machine_name()],
178                                 stdout=subprocess.PIPE)
179         line = proc.communicate()[0]
180         assert(proc.returncode == 0)
181         return line
182
183     def __rackboot(self, args):
184         debug.checkcmd([RACKBOOT] + args + [self._machine.get_machine_name()])
185
186     def setup(self):
187         if self._machine.get_bootarch() == "armv8":
188             self.__rackboot(["-b", "-H", "-n"])
189         else:
190             self.__rackboot(["-b", "-n"])
191
192     def __rackpower(self, arg):
193         try:
194             debug.checkcmd([RACKPOWER, arg, self._machine.get_machine_name()])
195         except subprocess.CalledProcessError:
196             debug.warning("rackpower %s %s failed" %
197                           (arg, self._machine.get_machine_name()))
198
199     def reboot(self):
200         self.__rackpower('-r')
201
202     def shutdown(self):
203         self.__rackpower('-d')
204
205
206 for n in sorted(ETHMachine._machines.keys()):
207     class TmpMachine(ETHMachine):
208         name = n
209     MachineFactory.addMachine(n, TmpMachine, **ETHMachine._machines[n])