externalized config params (incl etcd key) as env vars
This commit is contained in:
parent
9f4084fb53
commit
738d8bb8f9
2
.env
2
.env
|
@ -2,3 +2,5 @@ SYNCTHING_VERSION=latest
|
|||
PUID=0
|
||||
PGID=0
|
||||
TZ=Europe/London
|
||||
SYNC_LISTEN_PORT=22000
|
||||
SYNC_GUI_LISTEN_PORT=8384
|
|
@ -16,14 +16,17 @@ services:
|
|||
sut1:
|
||||
build: .
|
||||
links:
|
||||
- sync1:sync
|
||||
- sync1
|
||||
- etcd
|
||||
volumes:
|
||||
- sync1config:/config
|
||||
environment:
|
||||
- SYNCTHING_HOSTNAME=sync1
|
||||
- SYNCTHING_ADDRESS=tcp4://sync1
|
||||
- SYNCTHING_NODE_NAME=sync1
|
||||
- SYNCTHING_PUBLISH_ADDRESS=tcp4://sync1
|
||||
- SYNCTHING_DATA_PATH=/data
|
||||
- SYNCTHING_LOCAL_HOSTNAME=sync1
|
||||
- ETCD_HOSTNAME=etcd
|
||||
- ETCD_KEY=/syncthing_monitor/cluster1/cluster_info
|
||||
networks:
|
||||
- syncnet
|
||||
|
||||
|
@ -42,14 +45,17 @@ services:
|
|||
sut2:
|
||||
build: .
|
||||
links:
|
||||
- sync2:sync
|
||||
- sync2
|
||||
- etcd
|
||||
volumes:
|
||||
- sync2config:/config
|
||||
environment:
|
||||
- SYNCTHING_HOSTNAME=sync2
|
||||
- SYNCTHING_ADDRESS=tcp4://sync2
|
||||
- SYNCTHING_NODE_NAME=sync2
|
||||
- SYNCTHING_PUBLISH_ADDRESS=tcp4://sync2
|
||||
- SYNCTHING_DATA_PATH=/data
|
||||
- SYNCTHING_LOCAL_HOSTNAME=sync2
|
||||
- ETCD_HOSTNAME=etcd
|
||||
- ETCD_KEY=/syncthing_monitor/cluster1/cluster_info
|
||||
networks:
|
||||
- syncnet
|
||||
|
||||
|
|
|
@ -8,12 +8,18 @@ services:
|
|||
- sync
|
||||
volumes:
|
||||
- syncconfig:/config
|
||||
- syncdata:/data
|
||||
environment:
|
||||
- SYNCTHING_NODE_NAME=node1
|
||||
- SYNCTHING_PUBLISH_ADDRESS=tcp4://mypublichostname:${SYNC_LISTEN_PORT}
|
||||
- SYNCTHING_DATA_PATH=/data
|
||||
- SYNCTHING_LOCAL_HOSTNAME=sync
|
||||
- ETCD_HOSTNAME=etcd
|
||||
- ETCD_KEY=/syncthing_monitor/mycluster/myshare/cluster_info
|
||||
networks:
|
||||
- syncctlnet
|
||||
|
||||
sync:
|
||||
image: ghcr.io/linuxserver/syncthing:${SYNCTHING_VERSION}
|
||||
container_name: syncthing
|
||||
hostname: syncthing
|
||||
environment:
|
||||
- PUID=${PUID}
|
||||
- PGID=${PGID}
|
||||
|
@ -23,7 +29,13 @@ services:
|
|||
- syncdata:/data
|
||||
ports:
|
||||
- ${SYNC_LISTEN_PORT}:22000
|
||||
- ${SYNC_GUI_LISTEN_PORT}:8384
|
||||
networks:
|
||||
- syncctlnet
|
||||
|
||||
volumes:
|
||||
syncconfig:
|
||||
syncdata:
|
||||
|
||||
networks:
|
||||
syncctlnet:
|
|
@ -13,29 +13,34 @@ SHARED_FOLDER_LABEL = "syncthing_monitor_data"
|
|||
SYNCTHING_CONFIG_XML_PATH = '/config/config.xml'
|
||||
SYNCTHING_GUI_PORT = 8384
|
||||
ETCD_PORT = 2379
|
||||
ETCD_KEY = '/syncthing_monitor/cluster_info'
|
||||
|
||||
|
||||
def main():
|
||||
syncthing_mon = SyncthingMonitor(os.getenv('SYNCTHING_HOSTNAME'),
|
||||
os.getenv('SYNCTHING_ADDRESS'),
|
||||
os.getenv('SYNCTHING_DATA_PATH'),
|
||||
SYNCTHING_CONFIG_XML_PATH, "sync", SYNCTHING_GUI_PORT,
|
||||
"etcd", ETCD_PORT)
|
||||
syncthing_mon = SyncthingMonitor(os.getenv('SYNCTHING_NODE_NAME', 'sync'),
|
||||
os.getenv('SYNCTHING_PUBLISH_ADDRESS', 'tcp4://sync'),
|
||||
os.getenv('SYNCTHING_DATA_PATH', '/data'),
|
||||
os.getenv('SYNCTHING_CONFIG_XML_PATH', SYNCTHING_CONFIG_XML_PATH),
|
||||
os.getenv('SYNCTHING_LOCAL_HOSTNAME', 'sync'),
|
||||
os.getenv('SYNCTHING_GUI_PORT', SYNCTHING_GUI_PORT),
|
||||
os.getenv('ETCD_HOSTNAME', 'etcd'),
|
||||
os.getenv('ETCD_PORT', ETCD_PORT),
|
||||
os.getenv('ETCD_KEY', ETCD_KEY))
|
||||
syncthing_mon.start()
|
||||
|
||||
|
||||
class SyncthingMonitor:
|
||||
def __init__(self, syncthing_public_host, syncthing_address, syncthing_data_path,
|
||||
syncthing_config_xml_path, syncthing_hostname, syncthing_gui_port,
|
||||
etcd_hostname, etcd_port):
|
||||
self.syncthing_hostname = syncthing_public_host
|
||||
self.syncthing_address = syncthing_address
|
||||
def __init__(self, syncthing_node_name, syncthing_publish_address, syncthing_data_path,
|
||||
syncthing_config_xml_path, syncthing_local_hostname, syncthing_gui_port,
|
||||
etcd_hostname, etcd_port, etcd_key):
|
||||
self.syncthing_node_name = syncthing_node_name
|
||||
self.syncthing_publish_address = syncthing_publish_address
|
||||
self.syncthing_data_path = syncthing_data_path
|
||||
self.my_device_id = None
|
||||
self.syncthing = None
|
||||
self.etcd = EtcdClient(etcd_hostname, etcd_port)
|
||||
self.etcd = EtcdClient(etcd_hostname, etcd_port, etcd_key)
|
||||
self.syncthing_gui_port = syncthing_gui_port
|
||||
self.syncthing_hostname = syncthing_hostname
|
||||
self.syncthing_local_hostname = syncthing_local_hostname
|
||||
self.syncthing_config_xml_path = syncthing_config_xml_path
|
||||
|
||||
@retry
|
||||
|
@ -48,7 +53,7 @@ class SyncthingMonitor:
|
|||
self.syncthing_config_xml_path,
|
||||
self.syncthing_gui_port)
|
||||
|
||||
self.syncthing = SyncthingClient(api_key, self.syncthing_hostname, self.syncthing_gui_port)
|
||||
self.syncthing = SyncthingClient(api_key, self.syncthing_local_hostname, self.syncthing_gui_port)
|
||||
|
||||
@retry
|
||||
def start(self):
|
||||
|
@ -63,7 +68,7 @@ class SyncthingMonitor:
|
|||
self.syncthing.sync_config()
|
||||
|
||||
self.etcd.register_device_update_handler(self.update_devices)
|
||||
self.etcd.add_device_to_cluster(self.my_device_id, self.syncthing_hostname, self.syncthing_address)
|
||||
self.etcd.add_device_to_cluster(self.my_device_id, self.syncthing_node_name, self.syncthing_publish_address)
|
||||
|
||||
self.loop()
|
||||
|
||||
|
|
|
@ -7,17 +7,18 @@ CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info'
|
|||
|
||||
|
||||
class EtcdClient:
|
||||
def __init__(self, host, port):
|
||||
def __init__(self, host, port, key=CLUSTER_INFO_KEY):
|
||||
self.etcd = etcd3.client(host=host, port=port)
|
||||
self.key = key
|
||||
|
||||
def load_cluster_info(self):
|
||||
raw_value = self.etcd.get(CLUSTER_INFO_KEY)[0]
|
||||
raw_value = self.etcd.get(self.key)[0]
|
||||
if raw_value is None:
|
||||
return {'devices': []}
|
||||
return json.loads(raw_value)
|
||||
|
||||
@retry
|
||||
def add_device_to_cluster(self, device_id, hostname, address):
|
||||
def add_device_to_cluster(self, device_id, node_name, address):
|
||||
with self.etcd.lock('syncthing_monitor'):
|
||||
cluster_info = self.load_cluster_info()
|
||||
|
||||
|
@ -26,14 +27,14 @@ class EtcdClient:
|
|||
|
||||
new_device = {
|
||||
'id': device_id,
|
||||
'hostname': hostname,
|
||||
'node_name': node_name,
|
||||
'address': address
|
||||
}
|
||||
|
||||
cluster_info['devices'].append(new_device)
|
||||
|
||||
print("Updating etcd value for '{0}': {1}".format(CLUSTER_INFO_KEY, json.dumps(cluster_info)))
|
||||
self.etcd.put(CLUSTER_INFO_KEY, json.dumps(cluster_info))
|
||||
print("Updating etcd value for '{0}': {1}".format(self.key, json.dumps(cluster_info)))
|
||||
self.etcd.put(self.key, json.dumps(cluster_info))
|
||||
|
||||
@retry
|
||||
def get_device_list(self):
|
||||
|
@ -42,4 +43,4 @@ class EtcdClient:
|
|||
return device_list
|
||||
|
||||
def register_device_update_handler(self, device_update_handler):
|
||||
self.etcd.add_watch_callback(CLUSTER_INFO_KEY, device_update_handler)
|
||||
self.etcd.add_watch_callback(self.key, device_update_handler)
|
||||
|
|
|
@ -33,7 +33,7 @@ class SyncthingClient:
|
|||
|
||||
post_data = {
|
||||
"deviceID": device['id'],
|
||||
"name": device['hostname'],
|
||||
"name": device['node_name'],
|
||||
"addresses": [device_address],
|
||||
"compression": "metadata",
|
||||
"certName": "",
|
||||
|
|
Loading…
Reference in New Issue
Block a user