Skip to content

Commit 404f73b

Browse files
authored
Merge pull request #1088 from pbaylas/swag_ondemand_swag_urls
swag_ondemand mod: Use swag_url label to construct default container urls, if available
2 parents 459462c + 4323333 commit 404f73b

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This mod gives SWAG the ability to start containers on-demand when accessed thro
3434
![loading-page](.assets/loading-page.png)
3535

3636
Instead of showing a 502 error page, it can display a loading page and auto-refresh once the container is up.
37-
37+
3838
Add the following `include` to each proxy-conf where you wish to show the loading page inside the `server` section:
3939
```nginx
4040
server {
@@ -68,7 +68,7 @@ Or set the following label if using `swag-auto-proxy`:
6868
```
6969
### Labels:
7070
- `swag_ondemand=enable` - required for on-demand.
71-
- `swag_ondemand_urls=https://wake.domain.com,https://app.domain.com/up` - *optional* - overrides the monitored URLs for starting the container on-demand. Defaults to `https://somecontainer.,http://somecontainer.`.
71+
- `swag_ondemand_urls=https://wake.domain.com,https://app.domain.com/up` - *optional* - overrides the monitored URLs for starting the container on-demand. Defaults to using the value of the `swag_url` label, if you've already set it for `swag-auto-proxy`, or `https://somecontainer.,http://somecontainer.` otherwise.
7272

7373
### URLs:
7474
- Accessed URLs need to start with one of `swag_ondemand_urls` to be matched, for example, setting `swag_ondemand_urls=https://plex.` will apply to `https://plex.domain.com` and `https://plex.domain.com/something`.

root/app/swag-ondemand.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self):
2020
self.daemon = True
2121
self.ondemand_containers = {}
2222
self.init_docker()
23-
23+
2424
def init_docker(self):
2525
try:
2626
docker_host = os.environ.get("DOCKER_HOST", None)
@@ -37,20 +37,23 @@ def init_docker(self):
3737
def process_containers(self):
3838
containers = self.docker_client.containers.list(all=True, filters={ "label": ["swag_ondemand=enable"] })
3939
container_names = {container.name for container in containers}
40-
40+
4141
for container_name in list(self.ondemand_containers.keys()):
4242
if container_name in container_names:
4343
continue
4444
self.ondemand_containers.pop(container_name)
4545
logging.info(f"Stopped monitoring {container_name}")
46-
46+
4747
for container in containers:
48-
container_urls = container.labels.get("swag_ondemand_urls", f"https://{container.name}.,http://{container.name}.")
48+
default_url = container.labels.get("swag_url", f"{container.name}.").rstrip("*")
49+
container_urls = container.labels.get("swag_ondemand_urls", f"https://{default_url},http://{default_url}")
4950
if container.name not in self.ondemand_containers.keys():
5051
last_accessed = datetime.now()
51-
logging.info(f"Started monitoring {container.name}")
52+
logging.info(f"Started monitoring {container.name} for urls: {container_urls}")
5253
else:
5354
last_accessed = self.ondemand_containers[container.name]["last_accessed"]
55+
if container_urls != self.ondemand_containers[container.name]["urls"]:
56+
logging.info(f"Updated urls for {container.name} to: {container_urls}")
5457
self.ondemand_containers[container.name] = { "status": container.status, "urls": container_urls, "last_accessed": last_accessed }
5558

5659
def stop_containers(self):
@@ -62,12 +65,12 @@ def stop_containers(self):
6265
continue
6366
self.docker_client.containers.get(container_name).stop()
6467
logging.info(f"Stopped {container_name} after {STOP_THRESHOLD}s of inactivity")
65-
68+
6669
def start_containers(self):
6770
with last_accessed_urls_lock:
6871
last_accessed_urls_combined = ",".join(last_accessed_urls)
6972
last_accessed_urls.clear()
70-
73+
7174
for container_name in self.ondemand_containers.keys():
7275
accessed = False
7376
for ondemand_url in self.ondemand_containers[container_name]["urls"].split(","):
@@ -95,7 +98,7 @@ class LogReaderThread(threading.Thread):
9598
def __init__(self):
9699
super().__init__()
97100
self.daemon = True
98-
101+
99102
def tail(self, f):
100103
f.seek(0,2)
101104
inode = os.fstat(f.fileno()).st_ino
@@ -143,6 +146,6 @@ def run(self):
143146

144147
ContainerThread().start()
145148
LogReaderThread().start()
146-
149+
147150
while True:
148151
time.sleep(1)

0 commit comments

Comments
 (0)