Colony
my most ambitious (and needlessly convoluted) project yet
Colony is a project that I started to challenge myself. It's goal is to work as a
self-sustainable network of nodes with some tasks to do. It's supposed to be managing itself in
the most effective and resourceful way possible.
Basic Flow
class Discover:
DISCOVER_PORT = 28149
MAGIC = b"OK"
@staticmethod
def listen(real_port, host="0.0.0.0"):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, Discover.DISCOVER_PORT))
s.listen()
while True:
conn, _ = s.accept()
try:
data = conn.recv(2)
if data == Discover.MAGIC:
conn.sendall(struct.pack("!H", real_port))
finally:
conn.close()
@staticmethod
def request(target_host, timeout=3):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
s.connect((target_host, Discover.DISCOVER_PORT))
s.sendall(Discover.MAGIC)
data = s.recv(2)
s.close()
return struct.unpack("!H", data)[0]
The device being already in the network sends a request to a potential node. All the potential nodes listen on port 28149 and if they hear the message "OK" (bytes) then it sends back port on which the actual colony stuff happens.
class Foreman(Role):
CONTINUOUS = True
LOOP_SECONDS_WAIT = 60
class COMMANDS(Enum):
pass
def __init__(self, context):
commands = {
}
super().__init__(commands, context)
...
def loop(self):
role_registry = self.context.get_role_registry()
roles = role_registry.roles
general_roles = sorted(
name for name, stats in roles.items() if stats.priority == RolePriority.GENERAL
)
critical_roles = sorted(
name for name, stats in roles.items() if stats.priority == RolePriority.CRITICAL
)
optional_roles = sorted(
name
for name, stats in roles.items()
if stats.priority not in (RolePriority.GENERAL, RolePriority.CRITICAL)
)
peers = self._peers_for_assignment()
if not peers:
return
live_roles = self._fetch_live_roles(peers)
critical_holders = self._assign_critical_holders(
peers, critical_roles, live_roles
)
desired = {peer: list(general_roles) for peer in peers}
for critical_role, holder in critical_holders.items():
desired[holder].append(critical_role)
for role_name in optional_roles:
role_stat = roles[role_name]
best_peer = self._best_peer_for_optional(peers, role_stat)
if best_peer is not None:
desired[best_peer].append(role_name)
for peer in peers:
peer.execute(
Unit.COMMANDS.SET_ROLES,
self._dedupe_preserve_order(desired[peer]),
)
Then it initializes the basic roles, either the basic ones that it needs for network to work properly or roles given to it by network's already existing Foreman role.
The docs rn are rather lackluster but the project itself is still in work (for which I need
more time now that I have both work and college) but the source code will be released as soon as
it barely works.