-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedgeplug-push
More file actions
378 lines (324 loc) · 12.7 KB
/
Copy pathedgeplug-push
File metadata and controls
378 lines (324 loc) · 12.7 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python3
"""
EdgePlug CLI Updater Tool
This tool deploys agents to EdgePlug devices using gRPC over serial/Ethernet.
It provides secure agent deployment with manifest verification and rollback capabilities.
Usage:
edgeplug-push --device /dev/ttyUSB0 --agent agent.bin --manifest manifest.proto
edgeplug-push --device 192.168.1.100:50051 --agent agent.bin --manifest manifest.proto
"""
import argparse
import sys
import os
import time
import json
from typing import Optional, Dict, Any
import grpc
import serial
import struct
# Try to import the generated protobuf
try:
import edgeplug_pb2
import edgeplug_pb2_grpc
except ImportError:
print(
"Error: edgeplug_pb2 not found. Generate with: protoc --python_out=. edgeplug.proto"
)
sys.exit(1)
class EdgePlugUpdater:
"""EdgePlug agent deployment tool"""
def __init__(self, device: str, timeout: int = 30):
self.device = device
self.timeout = timeout
self.serial_conn = None
self.grpc_channel = None
self.grpc_stub = None
def connect_serial(self) -> bool:
"""Connect to device via serial"""
try:
self.serial_conn = serial.Serial(
port=self.device,
baudrate=115200,
timeout=self.timeout,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
)
print(f"Connected to {self.device} via serial")
return True
except Exception as e:
print(f"Failed to connect to {self.device}: {e}")
return False
def connect_grpc(self) -> bool:
"""Connect to device via gRPC"""
try:
self.grpc_channel = grpc.insecure_channel(self.device)
self.grpc_stub = edgeplug_pb2_grpc.EdgePlugStub(self.grpc_channel)
print(f"Connected to {self.device} via gRPC")
return True
except Exception as e:
print(f"Failed to connect to {self.device}: {e}")
return False
def connect(self) -> bool:
"""Connect to device using appropriate method"""
if ":" in self.device:
# gRPC connection (IP:port)
return self.connect_grpc()
else:
# Serial connection
return self.connect_serial()
def disconnect(self):
"""Disconnect from device"""
if self.serial_conn:
self.serial_conn.close()
if self.grpc_channel:
self.grpc_channel.close()
def send_serial_command(self, command: str, data: bytes = b"") -> Optional[bytes]:
"""Send command via serial"""
if not self.serial_conn:
return None
# Create packet: [length][command][data][checksum]
packet = struct.pack("<H", len(command) + len(data) + 3) # length
packet += command.encode("ascii") # command
packet += data # data
# Calculate checksum
checksum = sum(packet) & 0xFF
packet += struct.pack("B", checksum)
# Send packet
self.serial_conn.write(packet)
# Read response
try:
response_length = struct.unpack("<H", self.serial_conn.read(2))[0]
response = self.serial_conn.read(response_length)
return response
except Exception as e:
print(f"Failed to read response: {e}")
return None
def deploy_agent_serial(self, agent_data: bytes, manifest_data: bytes) -> bool:
"""Deploy agent via serial"""
print("Deploying agent via serial...")
# Send manifest first
response = self.send_serial_command("MANIFEST", manifest_data)
if not response or response[0] != 0: # 0 = success
print("Failed to send manifest")
return False
# Send agent data
response = self.send_serial_command("AGENT", agent_data)
if not response or response[0] != 0: # 0 = success
print("Failed to send agent")
return False
# Trigger update
response = self.send_serial_command("UPDATE")
if not response or response[0] != 0: # 0 = success
print("Failed to trigger update")
return False
print("Agent deployed successfully via serial")
return True
def deploy_agent_grpc(self, agent_data: bytes, manifest_data: bytes) -> bool:
"""Deploy agent via gRPC"""
print("Deploying agent via gRPC...")
try:
# Create deployment request
request = edgeplug_pb2.DeployAgentRequest(
agent_data=agent_data, manifest_data=manifest_data
)
# Send deployment request
response = self.grpc_stub.DeployAgent(request, timeout=self.timeout)
if response.status == edgeplug_pb2.DEPLOY_STATUS_SUCCESS:
print("Agent deployed successfully via gRPC")
return True
else:
print(f"Deployment failed: {response.error_message}")
return False
except Exception as e:
print(f"gRPC deployment failed: {e}")
return False
def deploy_agent(self, agent_path: str, manifest_path: str) -> bool:
"""Deploy agent to device"""
# Read agent data
try:
with open(agent_path, "rb") as f:
agent_data = f.read()
except Exception as e:
print(f"Failed to read agent file: {e}")
return False
# Read manifest data
try:
with open(manifest_path, "rb") as f:
manifest_data = f.read()
except Exception as e:
print(f"Failed to read manifest file: {e}")
return False
print(f"Agent size: {len(agent_data)} bytes")
print(f"Manifest size: {len(manifest_data)} bytes")
# Deploy using appropriate method
if self.grpc_stub:
return self.deploy_agent_grpc(agent_data, manifest_data)
else:
return self.deploy_agent_serial(agent_data, manifest_data)
def get_device_info(self) -> Optional[Dict[str, Any]]:
"""Get device information"""
if self.grpc_stub:
try:
request = edgeplug_pb2.GetDeviceInfoRequest()
response = self.grpc_stub.GetDeviceInfo(request, timeout=10)
return {
"device_id": response.device_id,
"firmware_version": response.firmware_version,
"runtime_version": response.runtime_version,
"active_agent": response.active_agent,
"memory_usage": response.memory_usage,
"uptime": response.uptime,
}
except Exception as e:
print(f"Failed to get device info via gRPC: {e}")
return None
else:
response = self.send_serial_command("INFO")
if response and response[0] == 0:
# Parse device info from response
try:
info_data = response[1:]
return json.loads(info_data.decode("ascii"))
except Exception as e:
print(f"Failed to parse device info: {e}")
return None
return None
def rollback_agent(self) -> bool:
"""Rollback to previous agent"""
print("Rolling back to previous agent...")
if self.grpc_stub:
try:
request = edgeplug_pb2.RollbackRequest()
response = self.grpc_stub.Rollback(request, timeout=10)
if response.status == edgeplug_pb2.ROLLBACK_STATUS_SUCCESS:
print("Rollback successful")
return True
else:
print(f"Rollback failed: {response.error_message}")
return False
except Exception as e:
print(f"gRPC rollback failed: {e}")
return False
else:
response = self.send_serial_command("ROLLBACK")
if response and response[0] == 0:
print("Rollback successful")
return True
else:
print("Rollback failed")
return False
def get_update_status(self) -> Optional[Dict[str, Any]]:
"""Get update status"""
if self.grpc_stub:
try:
request = edgeplug_pb2.GetUpdateStatusRequest()
response = self.grpc_stub.GetUpdateStatus(request, timeout=10)
return {
"update_in_progress": response.update_in_progress,
"active_slot": response.active_slot,
"successful_updates": response.successful_updates,
"failed_updates": response.failed_updates,
"last_update_time": response.last_update_time,
}
except Exception as e:
print(f"Failed to get update status via gRPC: {e}")
return None
else:
response = self.send_serial_command("STATUS")
if response and response[0] == 0:
try:
status_data = response[1:]
return json.loads(status_data.decode("ascii"))
except Exception as e:
print(f"Failed to parse update status: {e}")
return None
return None
def main():
parser = argparse.ArgumentParser(description="EdgePlug CLI Updater Tool")
parser.add_argument(
"--device", required=True, help="Device connection (serial port or IP:port)"
)
parser.add_argument("--agent", help="Agent binary file to deploy")
parser.add_argument("--manifest", help="Agent manifest file")
parser.add_argument(
"--timeout", type=int, default=30, help="Connection timeout in seconds"
)
parser.add_argument("--info", action="store_true", help="Get device information")
parser.add_argument("--status", action="store_true", help="Get update status")
parser.add_argument(
"--rollback", action="store_true", help="Rollback to previous agent"
)
parser.add_argument("--verbose", action="store_true", help="Enable verbose output")
args = parser.parse_args()
# Create updater
updater = EdgePlugUpdater(args.device, args.timeout)
try:
# Connect to device
if not updater.connect():
print("Failed to connect to device")
return 1
# Get device info
if args.info:
info = updater.get_device_info()
if info:
print("Device Information:")
for key, value in info.items():
print(f" {key}: {value}")
else:
print("Failed to get device information")
return 1
# Get update status
elif args.status:
status = updater.get_update_status()
if status:
print("Update Status:")
for key, value in status.items():
print(f" {key}: {value}")
else:
print("Failed to get update status")
return 1
# Rollback agent
elif args.rollback:
if updater.rollback_agent():
print("Rollback completed successfully")
else:
print("Rollback failed")
return 1
# Deploy agent
elif args.agent and args.manifest:
if not os.path.exists(args.agent):
print(f"Agent file not found: {args.agent}")
return 1
if not os.path.exists(args.manifest):
print(f"Manifest file not found: {args.manifest}")
return 1
print(f"Deploying agent: {args.agent}")
print(f"Manifest: {args.manifest}")
print(f"Device: {args.device}")
if updater.deploy_agent(args.agent, args.manifest):
print("Deployment completed successfully")
# Wait a moment and check status
time.sleep(2)
status = updater.get_update_status()
if status:
print("Final Status:")
for key, value in status.items():
print(f" {key}: {value}")
else:
print("Deployment failed")
return 1
else:
parser.print_help()
return 1
except KeyboardInterrupt:
print("\nOperation cancelled by user")
return 1
except Exception as e:
print(f"Error: {e}")
return 1
finally:
updater.disconnect()
return 0
if __name__ == "__main__":
sys.exit(main())