Skip to content

Commit f9b16a3

Browse files
committed
Added: Set parameters from env vars, allow setting url base from command line. Closes #710
1 parent 841ccc9 commit f9b16a3

10 files changed

+982
-20
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# NZB Hydra changelog
22

33
----------
4+
### 0.2.233
5+
Added: Set URL base from command line, set some of the parameters using environment variables. See [#710](https://github.com/theotherp/nzbhydra/issues/710).
6+
47
### 0.2.232
58
Fixed: Results from nzb.su were reported as passworded (not my fault)
69

libs/configargparse.py

+953
Large diffs are not rendered by default.

nzbhydra.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import glob
1111
import subprocess
1212
import os
13-
import argparse
1413
import webbrowser
1514
import nzbhydra
1615

@@ -28,6 +27,7 @@
2827
import nzbhydra.config as config
2928

3029
import requests
30+
import configargparse
3131

3232
requests.packages.urllib3.disable_warnings()
3333

@@ -164,6 +164,7 @@ def run(arguments):
164164

165165
host = config.settings.main.host if arguments.host is None else arguments.host
166166
port = config.settings.main.port if arguments.port is None else arguments.port
167+
nzbhydra.urlBase = config.settings.main.urlBase if arguments.urlbase is None else arguments.urlbase
167168

168169
socksproxy = config.settings.main.socksProxy if arguments.socksproxy is None else arguments.socksproxy
169170
if socksproxy:
@@ -196,6 +197,8 @@ def run(arguments):
196197
f.host = config.settings.main.host
197198
f.port = port
198199
f.scheme = "https" if config.settings.main.ssl else "http"
200+
if nzbhydra.urlBase is not None:
201+
f.path = nzbhydra.urlBase + "/"
199202
logger.notice("Starting web app on %s:%d" % (f.host, port))
200203
if not arguments.nobrowser and config.settings.main.startupBrowser:
201204
if arguments.restarted:
@@ -225,18 +228,19 @@ def _stop_worker_threads():
225228

226229
if __name__ == '__main__':
227230

228-
parser = argparse.ArgumentParser(description='NZBHydra')
229-
parser.add_argument('--config', action='store', help='Settings file to load', default="settings.cfg")
230-
parser.add_argument('--database', action='store', help='Database file to load', default="nzbhydra.db")
231+
parser = configargparse.ArgParser(description='NZBHydra')
232+
parser.add_argument('--config', action='store', env_var="hydra_config", help='Settings file to load. Can also be set using the environment variable "hydra_config"', default="settings.cfg")
233+
parser.add_argument('--database', action='store', env_var="hydra_database", help='Database file to load. Can also be set using the environment variable "hydra_database"', default="nzbhydra.db")
231234
parser.add_argument('--logfile', action='store', help='Log file. If set overwrites config value', default=None)
232-
parser.add_argument('--host', '-H', action='store', help='Host to run on')
233-
parser.add_argument('--port', '-p', action='store', help='Port to run on', type=int)
234-
parser.add_argument('--nobrowser', action='store_true', help='Don\'t open URL on startup', default=False)
235+
parser.add_argument('--host', '-H', action='store', env_var="hydra_host", help='Host to run on. Can also be set using the environment variable "hydra_host"')
236+
parser.add_argument('--port', '-p', action='store', env_var="hydra_port", help='Port to run on. Can also be set using the environment variable "hydra_port"', type=int)
237+
parser.add_argument('--urlbase', '-u', action='store', env_var="hydra_urlbase", help='URL base (e.g. "/nzbhydra"). Can also be set using the environment variable "hydra_urlbase"', default=None)
238+
parser.add_argument('--nobrowser', action='store_true', env_var="hydra_nobrowser", help='Don\'t open URL on startup', default=False)
235239
parser.add_argument('--daemon', '-D', action='store_true', help='Run as daemon. *nix only', default=False)
236240
parser.add_argument('--pidfile', action='store', help='PID file. Only relevant with daemon argument', default="nzbhydra.pid")
237241
parser.add_argument('--quiet', '-q', action='store_true', help='Quiet (no output)', default=False)
238-
parser.add_argument('--restarted', action='store_true', help=argparse.SUPPRESS, default=False)
239-
parser.add_argument('--clearloganddb', action='store_true', help=argparse.SUPPRESS, default=False)
242+
parser.add_argument('--restarted', action='store_true', help=configargparse.SUPPRESS, default=False)
243+
parser.add_argument('--clearloganddb', action='store_true', help=configargparse.SUPPRESS, default=False)
240244
parser.add_argument('--socksproxy', action='store', help='SOCKS proxy to use in format socks5://user:pass@host:port', default=None)
241245

242246
args, unknown = parser.parse_known_args()

nzbhydra/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
configFile = None
66
databaseFile = None
77
databaseLock = thread.allocate_lock()
8+
urlBase = None
89

910
def getBasePath():
1011
try:

nzbhydra/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def get_root_url():
104104
f.scheme = request.scheme
105105
f.host = furl(request.host_url).host
106106
f.port = config.settings.main.port
107-
if config.settings.main.urlBase:
108-
f.path = config.settings.main.urlBase
107+
if nzbhydra.urlBase:
108+
f.path = nzbhydra.urlBase
109109
return str(f) + "/"
110110

111111

nzbhydra/tests/test_Api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def assertApiUrl(self, url, shouldBeExternal, shouldbeLocal):
7878

7979
def testGetRootUrl(self):
8080
with web.app.test_request_context('/nzbhydra/'):
81-
config.settings.main.urlBase = None
81+
nzbhydra.urlBase = None
8282
config.settings.main.port = 5075
8383
self.assertEqual("http://localhost:5075/", api.get_root_url())
8484

85-
config.settings.main.urlBase = "/nzbhydra"
85+
nzbhydra.urlBase = "/nzbhydra"
8686
self.assertEqual("http://localhost:5075/nzbhydra/", api.get_root_url())

nzbhydra/tests/test_IntegrationTestsSearch.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def testFullStackTvSearch(self, requestsMock):
316316
# @requests_mock.Mocker()
317317
# def testBaseUrl(self, requestsMock):
318318
# web.app.template_folder = "../templates"
319-
# config.settings.main.urlBase = "/nzbhydra"
319+
# nzbhydra.urlBase = "/nzbhydra"
320320
#
321321
# expectedItems = self.prepareSearchMocks(requestsMock, 1, 1)
322322
# with web.app.test_request_context('/nzbhydra/'):

nzbhydra/web.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, app):
7979
self.app = app
8080

8181
def __call__(self, environ, start_response):
82-
base_url = config.settings.main.urlBase
82+
base_url = nzbhydra.urlBase # config.settings.main.urlBase
8383
if base_url is not None and base_url.endswith("/"):
8484
base_url = base_url[:-1]
8585
if base_url is not None and base_url != "":
@@ -505,10 +505,10 @@ def login():
505505
session["rememberMe"] = token
506506
session["username"] = u["username"]
507507
session["token"] = create_token(u)
508-
return redirect(("/" + config.settings.main.urlBase + "/").replace("//", "/") if config.settings.main.urlBase else "/")
508+
return redirect(("/" + nzbhydra.urlBase + "/").replace("//", "/") if nzbhydra.urlBase else "/")
509509
ip = getIp() if config.settings.main.logging.logIpAddresses else "<HIDDENIP>"
510510
logger.warn("Unsuccessful form login for user %s from IP %s" % (username, ip))
511-
return redirect(("/" + config.settings.main.urlBase + "/").replace("//", "/") if config.settings.main.urlBase else "/" + "login")
511+
return redirect(("/" + nzbhydra.urlBase + "/").replace("//", "/") if nzbhydra.urlBase else "/" + "login")
512512

513513

514514
@app.route('/auth/logout', methods=['POST'])
@@ -553,7 +553,7 @@ def sendUserInfos():
553553
@requires_auth("main", disableAuthForForm=True)
554554
def base(path):
555555
logger.debug("Sending index.html")
556-
base_url = ("/" + config.settings.main.urlBase + "/").replace("//", "/") if config.settings.main.urlBase else "/"
556+
base_url = ("/" + nzbhydra.urlBase + "/").replace("//", "/") if nzbhydra.urlBase else "/"
557557
_, currentVersion = get_current_version()
558558

559559
user = getUserByName(session["username"]) if "username" in session.keys() else None
@@ -578,7 +578,7 @@ def base(path):
578578
@app.route('/login')
579579
def loginview():
580580
logger.debug("Sending login.html")
581-
base_url = ("/" + config.settings.main.urlBase + "/").replace("//", "/") if config.settings.main.urlBase else "/"
581+
base_url = ("/" + nzbhydra.urlBase + "/").replace("//", "/") if nzbhydra.urlBase else "/"
582582
return render_template("login.html", base_url=base_url, theme=config.settings.main.theme + ".css", )
583583

584584

version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.232
1+
0.2.233

0 commit comments

Comments
 (0)