-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvillas_node.py
More file actions
115 lines (76 loc) · 3.04 KB
/
villas_node.py
File metadata and controls
115 lines (76 loc) · 3.04 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
# SPDX-FileCopyrightText: 2014-2025 The VILLASframework Authors
# SPDX-License-Identifier: Apache-2.0
import threading
from villas.node.node import Node
from villas.controller.components.manager import Manager
from villas.controller.components.gateways.villas_node import VILLASnodeGateway
class VILLASnodeManager(Manager):
def __init__(self, **args):
self.autostart = args.get('autostart', False)
self.api_url = args.get('api_url', 'http://localhost:8080')
self.api_url_external = args.get('api_url_external', self.api_url)
args['api_url'] = self.api_url
self.thread_stop = threading.Event()
self.thread = threading.Thread(target=self.reconcile_periodically)
self.node = Node(**args)
self._status = self.node.status
args['uuid'] = self._status.get('uuid')
super().__init__(**args)
def reconcile_periodically(self):
while not self.thread_stop.wait(2):
self.reconcile()
def reconcile(self):
try:
self._status = self.node.status
self._nodes = self.node.nodes
for node in self._nodes:
self.logger.debug('Found node %s on gateway: %s',
node['name'], node)
if node['uuid'] in self.components:
ic = self.components[node['uuid']]
# Update state
ic.change_state(node['state'])
else:
ic = VILLASnodeGateway(self, node)
self.add_component(ic)
self.change_state('running')
except Exception as e:
self.change_to_error('failed to reconcile',
exception=str(e),
args=str(e.args))
@property
def status(self):
status = super().status
status['status']['villas_node_version'] = self._status.get('version')
return status
def on_ready(self):
if self.autostart and not self.node.is_running():
self.start()
self.thread.start()
super().on_ready()
def on_shutdown(self):
self.thread_stop.set()
self.thread.join()
return super().on_shutdown()
def start(self, payload):
self.node.start()
self.change_state('starting')
def stop(self, payload):
if self.node.is_running():
self.node.stop()
self.change_state('idle')
# Once the gateway shutsdown, all the gateway nodes are also shutdown
for node in self._nodes:
node.change_state('shutdown')
def pause(self, payload):
self.node.pause()
self.change_state('paused')
# Once the gateway shutsdown, all the gateway nodes are also shutdown
for node in self._nodes:
node.change_state('paused')
def resume(self, payload):
self.node.resume()
def reset(self, payload):
self.node.restart()
def load_config(self, payload):
self.node.load_config(payload['config'])