wolsocketproxy 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wolsocketproxy
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: A socket proxy with wake-on-lan feature.
5
5
  Author-email: Song Fuchang <song.fc@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -17,6 +17,7 @@ Requires-Dist: aiohttp==3.13.5
17
17
  Requires-Dist: dataclass-wizard==0.39.1
18
18
  Requires-Dist: icmplib==3.0.4
19
19
  Requires-Dist: redfish==3.3.5
20
+ Requires-Dist: setproctitle==1.3.7
20
21
  Requires-Dist: wakeonlan==3.1.0
21
22
  Provides-Extra: dev
22
23
  Requires-Dist: wolsocketproxy[lint]; extra == "dev"
@@ -25,7 +26,7 @@ Provides-Extra: lint
25
26
  Requires-Dist: ty; extra == "lint"
26
27
  Requires-Dist: ruff; extra == "lint"
27
28
  Provides-Extra: build
28
- Requires-Dist: build[virtualenv]==1.4.2; extra == "build"
29
+ Requires-Dist: build[virtualenv]==1.5.0; extra == "build"
29
30
  Dynamic: license-file
30
31
 
31
32
  # wol-socket-proxy
@@ -96,7 +97,7 @@ The `wolsocketproxy.conf` has the following structure:
96
97
  // "http" - You must specify "online_check_http_url"
97
98
  // "ping" - You must specify "ip_address"
98
99
  "online_check_http_url": "http://192.168.1.124/health", // The URL used to check if machine is online, optional
99
- "online_check_timeout": 300, // Timeout when checking machine is online, seconds, optional, default is 300
100
+ "online_check_timeout": 300, // Timeout when checking machine is online, seconds, optional, default is 60
100
101
  "online_check_http_expected_code": 200, // Expected HTTP status code when using "http" method, optional, default is 200
101
102
  "ipmi_force_reset_if_power_up_failed": true, // Use IPMI power reset if this machine is not online after timeout, optional, default is false
102
103
  "ipmi_max_reset_try_count": 3 // Max IPMI power reset retry count before giving-up, optional, default is 3
@@ -129,3 +130,35 @@ The `wolsocketproxy.conf` has the following structure:
129
130
  ]
130
131
  }
131
132
  ```
133
+
134
+ ## Additional modes
135
+
136
+ ### Keep-alive daemon mode
137
+
138
+ This mode is used for running on target machine with auto-suspend (e.g. running `circadian` or similar), to keep the target
139
+ machine from auto suspending when running tasks.
140
+
141
+ Use the following command on the target machine to enter this mode:
142
+
143
+ ```bash
144
+ wolsocketproxy -k
145
+ ```
146
+
147
+ The config file `wolsocketproxy.conf` should look like the following:
148
+
149
+
150
+ ```json5
151
+ // Keep-alive daemon mode
152
+ {
153
+ "listen_address": "0.0.0.0", // The address to listen for HTTP server, default is 127.0.0.1
154
+ "listen_port": 18080, // The port to listen for HTTP server, default is 18080
155
+ "watchdog_feed_interval": 60, // Watchdog feed interval, seconds, default is 600
156
+ "keep_alive_method": "special_process", // How to keep target machine alive, default is "special_process"
157
+ // "special_process" - Fork a child process with specified name
158
+ "special_process_name": "wsp-keepalive", // The display name of the forked child process, default is "wsp-keepalive"
159
+ }
160
+ ```
161
+
162
+ You need to periodically send an HTTP GET request to `/watchdog/feed` at the `listen_port` in `watchdog_feed_interval` time,
163
+ or the special process will be killed.
164
+ You can combine this mode with `circadian`'s `process_block` config to keep it from auto-suspending.
@@ -66,7 +66,7 @@ The `wolsocketproxy.conf` has the following structure:
66
66
  // "http" - You must specify "online_check_http_url"
67
67
  // "ping" - You must specify "ip_address"
68
68
  "online_check_http_url": "http://192.168.1.124/health", // The URL used to check if machine is online, optional
69
- "online_check_timeout": 300, // Timeout when checking machine is online, seconds, optional, default is 300
69
+ "online_check_timeout": 300, // Timeout when checking machine is online, seconds, optional, default is 60
70
70
  "online_check_http_expected_code": 200, // Expected HTTP status code when using "http" method, optional, default is 200
71
71
  "ipmi_force_reset_if_power_up_failed": true, // Use IPMI power reset if this machine is not online after timeout, optional, default is false
72
72
  "ipmi_max_reset_try_count": 3 // Max IPMI power reset retry count before giving-up, optional, default is 3
@@ -99,3 +99,35 @@ The `wolsocketproxy.conf` has the following structure:
99
99
  ]
100
100
  }
101
101
  ```
102
+
103
+ ## Additional modes
104
+
105
+ ### Keep-alive daemon mode
106
+
107
+ This mode is used for running on target machine with auto-suspend (e.g. running `circadian` or similar), to keep the target
108
+ machine from auto suspending when running tasks.
109
+
110
+ Use the following command on the target machine to enter this mode:
111
+
112
+ ```bash
113
+ wolsocketproxy -k
114
+ ```
115
+
116
+ The config file `wolsocketproxy.conf` should look like the following:
117
+
118
+
119
+ ```json5
120
+ // Keep-alive daemon mode
121
+ {
122
+ "listen_address": "0.0.0.0", // The address to listen for HTTP server, default is 127.0.0.1
123
+ "listen_port": 18080, // The port to listen for HTTP server, default is 18080
124
+ "watchdog_feed_interval": 60, // Watchdog feed interval, seconds, default is 600
125
+ "keep_alive_method": "special_process", // How to keep target machine alive, default is "special_process"
126
+ // "special_process" - Fork a child process with specified name
127
+ "special_process_name": "wsp-keepalive", // The display name of the forked child process, default is "wsp-keepalive"
128
+ }
129
+ ```
130
+
131
+ You need to periodically send an HTTP GET request to `/watchdog/feed` at the `listen_port` in `watchdog_feed_interval` time,
132
+ or the special process will be killed.
133
+ You can combine this mode with `circadian`'s `process_block` config to keep it from auto-suspending.
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "wolsocketproxy"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "A socket proxy with wake-on-lan feature."
9
9
  authors = [
10
10
  {name = "Song Fuchang", email = "song.fc@gmail.com"}
@@ -23,6 +23,7 @@ dependencies = [
23
23
  "dataclass-wizard==0.39.1",
24
24
  "icmplib==3.0.4",
25
25
  "redfish==3.3.5",
26
+ "setproctitle==1.3.7",
26
27
  "wakeonlan==3.1.0",
27
28
  ]
28
29
 
@@ -36,7 +37,7 @@ lint = [
36
37
  "ruff",
37
38
  ]
38
39
  build = [
39
- "build[virtualenv]==1.4.2",
40
+ "build[virtualenv]==1.5.0",
40
41
  ]
41
42
 
42
43
  [project.urls]
@@ -52,6 +53,7 @@ src = ["src"]
52
53
  target-version = "py312"
53
54
  lint.select = ["ALL"]
54
55
  lint.ignore = [
56
+ "ASYNC110",
55
57
  "COM812",
56
58
  "D100",
57
59
  "D101",
@@ -62,6 +64,7 @@ lint.ignore = [
62
64
  "D203",
63
65
  "D211",
64
66
  "D213",
67
+ "DTZ005",
65
68
  "S101",
66
69
  "EM102",
67
70
  "FBT001",
@@ -0,0 +1,75 @@
1
+ import json
2
+ import logging
3
+ from argparse import ArgumentParser
4
+ from pathlib import Path
5
+ from sys import stdout
6
+
7
+ import dataclass_wizard
8
+
9
+ from wolsocketproxy.keepalive import KeepAliveConfig, KeepAliveDaemon
10
+ from wolsocketproxy.proxy import Proxy, ProxyConfig
11
+
12
+
13
+ def main() -> None:
14
+ logging.basicConfig(
15
+ format="%(asctime)s %(levelname)-8s %(name)s %(message)s",
16
+ level=logging.INFO,
17
+ datefmt="%Y-%m-%d %H:%M:%S",
18
+ handlers=[
19
+ logging.StreamHandler(
20
+ stream=stdout,
21
+ ),
22
+ ],
23
+ force=True,
24
+ )
25
+
26
+ log = logging.getLogger()
27
+
28
+ parser = ArgumentParser(prog="wolsocketproxy", description="A socket proxy with wake-on-lan feature.")
29
+
30
+ parser.add_argument(
31
+ "-k",
32
+ "--keep-alive",
33
+ dest="keep_alive_mode",
34
+ default=False,
35
+ action="store_true",
36
+ help="Start in keep-alive daemon mode",
37
+ )
38
+
39
+ parser.add_argument(
40
+ "-c",
41
+ "--config",
42
+ help="The config file to use, default lookup order: /etc/wolsocketproxy.conf, ./wolsocketproxy.conf",
43
+ )
44
+
45
+ args = parser.parse_args()
46
+ config_path: Path
47
+
48
+ if "config" in args and args.config is not None:
49
+ config_path = Path(args.config)
50
+ else:
51
+ config_path = Path("/etc/wolsocketproxy.conf")
52
+
53
+ if not config_path.exists():
54
+ config_path = Path("./wolsocketproxy.conf")
55
+
56
+ if not config_path.exists():
57
+ log.error("Config file path %s does not exist!", config_path)
58
+ return
59
+
60
+ config_data = json.loads(config_path.read_text())
61
+
62
+ if args.keep_alive_mode:
63
+ config = dataclass_wizard.fromdict(KeepAliveConfig, config_data)
64
+
65
+ log.info("Loaded keep-alive mode config from %s", config_path)
66
+
67
+ keep_alive_daemon = KeepAliveDaemon(config)
68
+ keep_alive_daemon.start()
69
+ else:
70
+ config = dataclass_wizard.fromdict(ProxyConfig, config_data)
71
+
72
+ log.info("Loaded proxy mode config from %s", config_path)
73
+
74
+ proxy = Proxy(config)
75
+ proxy.start()
@@ -0,0 +1,153 @@
1
+ import asyncio
2
+ import logging
3
+ import os
4
+ import signal
5
+ import time
6
+ from dataclasses import dataclass
7
+ from datetime import datetime
8
+ from threading import Thread
9
+ from typing import Literal
10
+
11
+ from aiohttp import web
12
+ from aiohttp.web import Request, Response
13
+ from setproctitle import setproctitle
14
+
15
+
16
+ @dataclass
17
+ class KeepAliveConfig:
18
+ listen_address: str = "127.0.0.1"
19
+ listen_port: int = 18080
20
+
21
+ watchdog_feed_interval: int = 600
22
+
23
+ keep_alive_method: Literal["special_process"] = "special_process"
24
+
25
+ special_process_name: str | None = "wsp-keepalive"
26
+
27
+
28
+ class KeepAliveDaemon:
29
+ _log: logging.Logger = logging.getLogger(__name__)
30
+
31
+ _config: KeepAliveConfig
32
+ _web_app: web.Application
33
+
34
+ _watchdog_last_feed_time: datetime
35
+ _watchdog_is_hungry: bool = False
36
+
37
+ _special_process_id: int = -1
38
+
39
+ def __init__(self, config: KeepAliveConfig) -> None:
40
+ self._config = config
41
+
42
+ self._web_app = web.Application()
43
+
44
+ self._web_app.add_routes(
45
+ [
46
+ web.get("/watchdog/feed", self._handle_watchdog_feed)
47
+ ]
48
+ )
49
+
50
+ async def _watchdog_timer(self) -> None:
51
+ while True:
52
+ previous_is_hungry = self._watchdog_is_hungry
53
+ last_feed_interval = datetime.now() - self._watchdog_last_feed_time
54
+
55
+ if last_feed_interval.total_seconds() > self._config.watchdog_feed_interval:
56
+ self._watchdog_is_hungry = True
57
+ else:
58
+ self._watchdog_is_hungry = False
59
+
60
+ if not previous_is_hungry and self._watchdog_is_hungry:
61
+ Thread(target=self._on_watchdog_hungry, daemon=True).start()
62
+ elif previous_is_hungry and not self._watchdog_is_hungry:
63
+ Thread(target=self._on_watchdog_feed, daemon=True).start()
64
+
65
+ await asyncio.sleep(1)
66
+
67
+ def _on_watchdog_hungry(self) -> None:
68
+ self._log.warning("Watchdog is hungry!")
69
+
70
+ if self._special_process_id > 0:
71
+ os.kill(self._special_process_id, signal.SIGKILL)
72
+ os.waitpid(self._special_process_id, 0)
73
+ self._log.warning("Killed special process PID %d", self._special_process_id)
74
+ self._special_process_id = -1
75
+
76
+ def _on_watchdog_feed(self) -> None:
77
+ self._log.info("Watchdog is feed.")
78
+
79
+ if self._config.keep_alive_method == "special_process" and self._special_process_id < 0:
80
+ self._start_special_process()
81
+
82
+ def _special_process(self) -> None:
83
+ assert self._config.special_process_name is not None
84
+ setproctitle(self._config.special_process_name)
85
+
86
+ while True:
87
+ time.sleep(60)
88
+
89
+ if os.getppid() == 1:
90
+ os._exit(0)
91
+
92
+ def _start_special_process(self) -> bool:
93
+ self._special_process_id = os.fork()
94
+
95
+ if self._special_process_id == 0:
96
+ self._special_process()
97
+ return True
98
+
99
+ self._log.info(
100
+ "Started special process with name %s, PID %d",
101
+ self._config.special_process_name, self._special_process_id
102
+ )
103
+
104
+ return False
105
+
106
+ def start(self) -> None:
107
+ self._watchdog_last_feed_time = datetime.now()
108
+ self._watchdog_is_hungry = False
109
+
110
+ if self._config.keep_alive_method == "special_process": # noqa: SIM102
111
+ if self._start_special_process():
112
+ return
113
+
114
+ loop = asyncio.new_event_loop()
115
+
116
+ def _start_loop() -> None:
117
+ asyncio.set_event_loop(loop)
118
+ loop.run_forever()
119
+
120
+ Thread(target=_start_loop, daemon=True).start()
121
+
122
+ watchdog_task = asyncio.run_coroutine_threadsafe(self._watchdog_timer(), loop)
123
+
124
+ self._log.info(
125
+ "Keep-alive daemon started at %s:%d, watchdog feed interval %ds.",
126
+ self._config.listen_address, self._config.listen_port, self._config.watchdog_feed_interval
127
+ )
128
+
129
+ web.run_app(self._web_app, host=self._config.listen_address, port=self._config.listen_port)
130
+
131
+ self._log.info("Stopping...")
132
+
133
+ watchdog_task.cancel()
134
+ loop.stop()
135
+
136
+ if self._special_process_id > 0:
137
+ os.kill(self._special_process_id, signal.SIGKILL)
138
+ os.waitpid(self._special_process_id, 0)
139
+
140
+ self._log.info("Stopped.")
141
+
142
+ async def _handle_watchdog_feed(self, _: Request) -> Response:
143
+ last_feed = self._watchdog_last_feed_time
144
+ self._watchdog_last_feed_time = datetime.now()
145
+ interval = (self._watchdog_last_feed_time - last_feed).total_seconds()
146
+
147
+ return web.json_response(
148
+ {
149
+ "status": "ok",
150
+ "last_feed": last_feed.strftime("%Y-%m-%d %H:%M:%S"),
151
+ "interval": interval,
152
+ }
153
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wolsocketproxy
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: A socket proxy with wake-on-lan feature.
5
5
  Author-email: Song Fuchang <song.fc@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -17,6 +17,7 @@ Requires-Dist: aiohttp==3.13.5
17
17
  Requires-Dist: dataclass-wizard==0.39.1
18
18
  Requires-Dist: icmplib==3.0.4
19
19
  Requires-Dist: redfish==3.3.5
20
+ Requires-Dist: setproctitle==1.3.7
20
21
  Requires-Dist: wakeonlan==3.1.0
21
22
  Provides-Extra: dev
22
23
  Requires-Dist: wolsocketproxy[lint]; extra == "dev"
@@ -25,7 +26,7 @@ Provides-Extra: lint
25
26
  Requires-Dist: ty; extra == "lint"
26
27
  Requires-Dist: ruff; extra == "lint"
27
28
  Provides-Extra: build
28
- Requires-Dist: build[virtualenv]==1.4.2; extra == "build"
29
+ Requires-Dist: build[virtualenv]==1.5.0; extra == "build"
29
30
  Dynamic: license-file
30
31
 
31
32
  # wol-socket-proxy
@@ -96,7 +97,7 @@ The `wolsocketproxy.conf` has the following structure:
96
97
  // "http" - You must specify "online_check_http_url"
97
98
  // "ping" - You must specify "ip_address"
98
99
  "online_check_http_url": "http://192.168.1.124/health", // The URL used to check if machine is online, optional
99
- "online_check_timeout": 300, // Timeout when checking machine is online, seconds, optional, default is 300
100
+ "online_check_timeout": 300, // Timeout when checking machine is online, seconds, optional, default is 60
100
101
  "online_check_http_expected_code": 200, // Expected HTTP status code when using "http" method, optional, default is 200
101
102
  "ipmi_force_reset_if_power_up_failed": true, // Use IPMI power reset if this machine is not online after timeout, optional, default is false
102
103
  "ipmi_max_reset_try_count": 3 // Max IPMI power reset retry count before giving-up, optional, default is 3
@@ -129,3 +130,35 @@ The `wolsocketproxy.conf` has the following structure:
129
130
  ]
130
131
  }
131
132
  ```
133
+
134
+ ## Additional modes
135
+
136
+ ### Keep-alive daemon mode
137
+
138
+ This mode is used for running on target machine with auto-suspend (e.g. running `circadian` or similar), to keep the target
139
+ machine from auto suspending when running tasks.
140
+
141
+ Use the following command on the target machine to enter this mode:
142
+
143
+ ```bash
144
+ wolsocketproxy -k
145
+ ```
146
+
147
+ The config file `wolsocketproxy.conf` should look like the following:
148
+
149
+
150
+ ```json5
151
+ // Keep-alive daemon mode
152
+ {
153
+ "listen_address": "0.0.0.0", // The address to listen for HTTP server, default is 127.0.0.1
154
+ "listen_port": 18080, // The port to listen for HTTP server, default is 18080
155
+ "watchdog_feed_interval": 60, // Watchdog feed interval, seconds, default is 600
156
+ "keep_alive_method": "special_process", // How to keep target machine alive, default is "special_process"
157
+ // "special_process" - Fork a child process with specified name
158
+ "special_process_name": "wsp-keepalive", // The display name of the forked child process, default is "wsp-keepalive"
159
+ }
160
+ ```
161
+
162
+ You need to periodically send an HTTP GET request to `/watchdog/feed` at the `listen_port` in `watchdog_feed_interval` time,
163
+ or the special process will be killed.
164
+ You can combine this mode with `circadian`'s `process_block` config to keep it from auto-suspending.
@@ -3,6 +3,7 @@ README.md
3
3
  pyproject.toml
4
4
  src/wolsocketproxy/__init__.py
5
5
  src/wolsocketproxy/__main__.py
6
+ src/wolsocketproxy/keepalive.py
6
7
  src/wolsocketproxy/monitor.py
7
8
  src/wolsocketproxy/proxy.py
8
9
  src/wolsocketproxy/utils.py
@@ -2,10 +2,11 @@ aiohttp==3.13.5
2
2
  dataclass-wizard==0.39.1
3
3
  icmplib==3.0.4
4
4
  redfish==3.3.5
5
+ setproctitle==1.3.7
5
6
  wakeonlan==3.1.0
6
7
 
7
8
  [build]
8
- build[virtualenv]==1.4.2
9
+ build[virtualenv]==1.5.0
9
10
 
10
11
  [dev]
11
12
  wolsocketproxy[lint]
@@ -1,44 +0,0 @@
1
- import json
2
- import logging
3
- from argparse import ArgumentParser
4
- from pathlib import Path
5
-
6
- import dataclass_wizard
7
-
8
- from wolsocketproxy.proxy import Proxy, ProxyConfig
9
-
10
-
11
- def main() -> None:
12
- logging.basicConfig(level=logging.INFO)
13
- log = logging.getLogger()
14
-
15
- parser = ArgumentParser(prog="wolsocketproxy", description="A socket proxy with wake-on-lan feature.")
16
-
17
- parser.add_argument(
18
- "-c",
19
- "--config",
20
- help="The config file to use, default lookup order: /etc/wolsocketproxy.conf, ./wolsocketproxy.conf",
21
- )
22
-
23
- args = parser.parse_args()
24
- config_path: Path
25
-
26
- if "config" in args and args.config is not None:
27
- config_path = Path(args.config)
28
- else:
29
- config_path = Path("/etc/wolsocketproxy.conf")
30
-
31
- if not config_path.exists():
32
- config_path = Path("./wolsocketproxy.conf")
33
-
34
- if not config_path.exists():
35
- log.error("Config file path %s does not exist!", config_path)
36
- return
37
-
38
- config_data = json.loads(config_path.read_text())
39
- config = dataclass_wizard.fromdict(ProxyConfig, config_data)
40
-
41
- log.info("Loaded config from %s", config_path)
42
-
43
- proxy = Proxy(config)
44
- proxy.start()
File without changes
File without changes