- Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathwrapper.py
More file actions
Latest commit
164 lines (132 loc) · 4.3 KB
/
Copy pathwrapper.py
File metadata and controls
164 lines (132 loc) · 4.3 KB
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
importsys
importos
importsubprocess
classNode(object):
def__init__(self, address, port=22, directory="~/run", gpio_pins="", serialport="/dev/ttyUSB0", friendlyname="", verbose=False):
self.address=address
self.port=port
self.directory=directory
self.verbose=verbose
self.friendlyname=friendlyname
self.serialport=serialport
self.serialexport="MY_SERIALPORT="+self.serialport
self.gpio_pins=gpio_pins
self.gpio_pins_export="MY_GPIOPINS="+self.gpio_pins
defsyncfiles(self):
cmdline="rsync --exclude=wrapper.py --exclude=powercontrol.py --exclude=__pycache__ --exclude=*.pyc -q -e 'ssh -p %i' -r -h --progress ./ %s:%s"% (self.port, self.address, self.directory)
ifself.verbose:
print(cmdline)
os.system(cmdline)
defruncommand(self, command):
cmdline="ssh -t -p %i %s 'cd %s; %s %s %s'"% (self.port, self.address, self.directory, self.serialexport, self.gpio_pins_export, command)
ifself.verbose:
print(cmdline)
os.system(cmdline)
defgetfile(self, filename):
cmdline="rsync --exclude=wrapper.py --exclude=__pycache__ --exclude=*.pyc -q -e 'ssh -p %i' -r -h --progress %s:%s/%s ./"% (self.port, self.address, self.directory, filename)
ifself.verbose:
print(cmdline)
os.system(cmdline)
defsendfile(self, filename):
cmdline="rsync --exclude=wrapper.py --exclude=__pycache__ --exclude=*.pyc -q -e 'ssh -p %i' -r -h --progress ./%s %s:%s"% (self.port, filename, self.address, self.directory)
ifself.verbose:
print(cmdline)
os.system(cmdline)
defreset(self):
self.runcommand("python powercontrol.py reset")
defpower(self):
self.runcommand("python powercontrol.py power")
defforceoff(self):
self.runcommand("python powercontrol.py forceoff")
defpowerstatus(self):
self.runcommand("python powercontrol.py powerstatus")
defisbusy(self):
cmdline="ps aux | grep \"python .*/%s\""% (self.directory.replace("run", "r[u]n"))
try:
out=subprocess.check_output(["ssh", "-p", str(self.port), self.address, cmdline])
returnTrue
except:
returnFalse
def__str__(self):
return"%s:%i:%s:%s:%s (%s)"% (self.address, self.port, self.directory, self.serialport, self.gpio_pins, self.friendlyname)
# add nodes here
# nodenames are unique lowercase strings
nodes= {}
nodes["n1"] =Node("pi@example.com", 22, "~/run/ttyUSB0", "26,24,22", "/dev/ttyUSB0", "K8, AMD Sempron 3100+", True)
defprinthelp():
print("Usage: %s sync|run|getfile|sendfile|isbusy|runshellcommand|listnodes|help|reset|power|forceoff|powerstatus node filename"%sys.argv[0])
defprintnodes():
fork,vinnodes.items():
print("%s: %s"% (k,v))
defmain(args):
iflen(args) <2orargs[1] =="help":
printhelp()
returnFalse
command=args[1]
ifcommand=="listnodes":
printnodes()
returnTrue
# end single argument commands
iflen(args) <3:
printhelp()
returnFalse
# try to resolve node
nodename=args[2].lower()
try:
node=nodes[nodename]
except:
print("Unknown node %s"%nodename)
print("Known nodes:")
printnodes()
returnFalse
ifcommand=="sync":
node.syncfiles()
returnTrue
ifcommand=="isbusy":
ifnode.isbusy():
print("Node %s is currently busy"%nodename)
else:
print("Node %s is currently free"%nodename)
returnTrue
ifcommand=="reset":
node.reset()
returnTrue
ifcommand=="power":
node.power()
returnTrue
ifcommand=="forceoff":
node.forceoff()
returnTrue
ifcommand=="powerstatus":
node.powerstatus()
returnTrue
# end double argument commands
iflen(args) <4:
printhelp()
returnFalse
ifcommand=="getfile":
node.getfile(args[3])
returnTrue
ifcommand=="sendfile":
node.sendfile(args[3])
returnTrue
# concat all following args into one large arg with spaces
# this is hacky, but allows running commands with arguments without needing to quote them
iflen(args) >4:
foriinrange(4, len(args)):
args[3] =args[3] +" "+args[i]
ifcommand=="runshellcommand":
node.runcommand(args[3])
returnTrue
ifcommand=="run":
ifnode.isbusy():
print("Node %s is currently busy, not running command"%nodename)
returnFalse
node.syncfiles()
node.runcommand("python -u %s/%s"% (node.directory, args[3]))
returnTrue
print("Invalid syntax")
printhelp()
returnFalse
if__name__=="__main__":
main(sys.argv)