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