-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpioperantctl.py
executable file
·330 lines (271 loc) · 10.1 KB
/
rpioperantctl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
# this script is for starting an stopping notebooks on raspberry pis from a remote server via ssh
import subprocess
import pandas as pd
import numpy as np
import argparse
import time
def ssh_magpi(server="magpi01", is_magpi=False):
""" opens a subprocess SSHing into magpi rpis
Arguments:
is_magpi (bool): if the current computer is magpi, or another server (e.g. txori)
"""
if is_magpi:
# ssh into magpi rpi
sshProcess = subprocess.Popen(
["ssh", "-T", server],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
bufsize=0,
)
else:
# ssh into magpi server
sshProcess = subprocess.Popen(
["ssh", "-T", "bird@magpi"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
bufsize=0,
)
# ssh into specific magpi
sshProcess.stdin.write("ssh -T " + server + "\n")
return sshProcess
def get_panel_subject_behavior(
is_magpi=False, psb_loc="/home/bird/opdat/panel_subject_behavior"
):
""" gets panel subject behavior from magpi serverpsb_loc
"""
if is_magpi:
command = ["cat", psb_loc]
else:
command = ["ssh", "bird@magpi", "cat", psb_loc]
cat = subprocess.Popen(command, stdout=subprocess.PIPE)
psb = [line.decode("utf-8") for line in cat.stdout]
return psb
def parse_panel_subject_behavior(psb):
""" parses panel subject behavior into a pandas dataframe
"""
process_df = pd.DataFrame(columns=["panel", "enabled", "subj", "dir", "behavior"])
for line in psb:
if line[0] not in ["#", "\n"]:
splitline = line.split()
process_df.loc[len(process_df)] = [
splitline[0], # panel
splitline[1], # enabled
"B" + splitline[2], # subject
splitline[3].replace("<3>", splitline[2]), # subject
" ".join(splitline[4:])
.replace("<3>", splitline[2])
.replace("<1>", "1"), # command
# .replace("<1>", splitline[0]), # command
]
return process_df
def find_running_commands(server, process, user="bird", is_magpi=False):
"""determines if a specific process is running on a server (magpi)
"""
# ssh into magpi
sshProcess = ssh_magpi(server=server, is_magpi=is_magpi)
# search for python processes
sshProcess.stdin.write("ps -ef | grep '" + process + "'\n")
# close connection
sshProcess.stdin.close()
# get output of commands
out = sshProcess.stdout.readlines()
# subset output of sshprocess to what is returned by ps -ef
commands = []
for line in out:
if (
(line[: len(user)] == user)
& (len(line.split()) > 8)
& (process in line)
& ("grep" not in line)
):
commands.append(line)
return commands
def find_behavior_PID(behavior, processes_formatted, running_processes):
""" find PID of running behavior from porcesses information
"""
return [
i.split()[1]
for i in np.array(running_processes)[behavior == np.array(processes_formatted)]
]
def pyoperantctl(process_df, is_magpi=False):
""" the main pioperantctl based upon the panel_subject_behavior
"""
processes_to_kill = pd.DataFrame(columns=["Magpi", "command", "PID"])
processes_to_start = pd.DataFrame(columns=["Magpi", "command"])
# for
for idx, row in process_df.iterrows():
# find all running behavioral processes on magpi
running_processes = find_running_commands(
str(row.panel), process="pyoperant/scripts/behave", is_magpi=is_magpi,
)
# format processes to the same as in panel_subject_behavior
processes_formatted = [
" ".join(process.split()[-6:]).split("/")[-1]
for process in running_processes
]
# if there should be a process running
if row.enabled == "1":
# if the process is running
if row.behavior in processes_formatted:
behavior_PIDs = find_behavior_PID(
row.behavior, processes_formatted, running_processes
)
print(
"Panel {} | Process already running: {} | PID(s): {}".format(
row.panel, row.behavior, behavior_PIDs
)
)
else:
print(
"Panel {} | Process needs to start: {}".format(
row.panel, row.behavior
)
)
processes_to_start.loc[len(processes_to_start)] = [
row.panel,
row.behavior,
]
# if the process should not be a process running
elif row.enabled == "0":
# if behavior running and should not be, kill it
if row.behavior in processes_formatted:
# determine PID of processesthat need to be killed
behavior_PIDs = find_behavior_PID(
row.behavior, processes_formatted, running_processes
)
# add processes to kill to dataframe
processes_to_kill.loc[len(processes_to_kill)] = [
row.panel,
row.behavior,
behavior_PIDs,
]
print(
"Panel {} | Process needs to be killed: {} | PID(s): {}".format(
row.panel, row.behavior, behavior_PIDs
)
)
else:
print(
"Panel {} | Process is correctly not running: {}".format(
row.panel, row.behavior
)
)
# run through all behaviors running, if they are not the correct behavior kill them
for pi, process in enumerate(processes_formatted):
# if the wrong behavior process is running it needs to be killed
if process != row.behavior:
PID = running_processes[pi].split()[1]
print(
"Panel {} | Process needs to be killed: {} | PID: {}".format(
row.panel, process, PID
)
)
# add process to list of processes to kill
processes_to_kill.loc[len(processes_to_kill)] = [
row.panel,
running_processes[pi],
[running_processes[pi].split()[1]],
]
return processes_to_kill, processes_to_start
def kill_behaviors(processes_to_kill, is_magpi=False):
for idx, row in processes_to_kill.iterrows():
for pid in row.PID:
server = str(row.Magpi)
print('Killing "{}" at {} in {}'.format(row.command, pid, server))
# ssh into magpi
sshProcess = ssh_magpi(server, is_magpi=is_magpi)
# search for python processes
print("kill {}".format(pid))
sshProcess.stdin.write("kill {}".format(pid))
# close connection
sshProcess.stdin.close()
# get output of commands
# out = sshProcess.stdout.readlines()
def start_behaviors(processes_to_start, is_magpi=False):
for idx, row in processes_to_start.iterrows():
server = str(row.Magpi)
print('Starting "{}" in {}'.format(row.command, server))
# ssh into magpi
sshProcess = ssh_magpi(server=server, is_magpi=is_magpi)
# search for python processes
command = "nohup /home/bird/pyoperant/scripts/" + row.command + " &"
# print(command)
sshProcess.stdin.write(command)
# make sure the process is running
# time.sleep(.25)
# close connection
sshProcess.stdin.close()
# make sure command is running
rc = find_running_commands(server, process=row.command, is_magpi=is_magpi)
if len(rc) > 1:
print("Start failed")
# get output of commands
# out = sshProcess.stdout.readlines()
# for line in out:
# print(line)
def str2bool(v):
""" allows multiple types of argument values to be equal to "True" or "False"
"""
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
def get_args():
parser = argparse.ArgumentParser(
description="View PIDs of behaviors and kill/start them"
)
parser.add_argument(
"-s",
type=str2bool,
nargs="?",
const=True,
default=False,
help="start behaviors that are not currently running but should be",
)
parser.add_argument(
"-k",
type=str2bool,
nargs="?",
const=True,
default=False,
help="kill behaviors that should not be running",
)
parser.add_argument(
"-is_magpi",
type=str2bool,
nargs="?",
const=True,
default=True,
help="if this script is running on magpi, or another computer (e.g. Txori)",
)
parser.add_argument(
"-psb_loc",
dest="psb_loc",
type=str,
default="/home/bird/opdat/panel_subject_behavior",
)
return parser.parse_args()
def main():
# parse arguments
args = get_args()
# retrieve panel subject behavior
psb = get_panel_subject_behavior(psb_loc=args.psb_loc, is_magpi=args.is_magpi)
# parse panel subject behavior
process_df = parse_panel_subject_behavior(psb)
# find running processes, compare to panel_subject_behavior
processes_to_kill, processes_to_start = pyoperantctl(
process_df, is_magpi=args.is_magpi
)
# start/kill processes
if args.k:
kill_behaviors(processes_to_kill, is_magpi=args.is_magpi)
if args.s:
start_behaviors(processes_to_start, is_magpi=args.is_magpi)
return
if __name__ == "__main__":
main()