Add auto-updating of GeoLite2 database

This commit is contained in:
JonnyWong16 2019-12-25 11:17:45 -08:00
parent 41c9369b43
commit b83eb2e763
5 changed files with 69 additions and 21 deletions

View file

@ -1178,20 +1178,25 @@
</div>
</div>
<p class="help-block">
Enter your MaxMind License Key. You can register for a license key <a href="${anon_url('https://www.maxmind.com/en/geolite2/signup')}" target="_blank">here</a>.
Enter your MaxMind License Key to install the GeoLite2 database. You can register for a license key <a href="${anon_url('https://www.maxmind.com/en/geolite2/signup')}" target="_blank">here</a>.
</p>
</div>
<div class="form-group">
<label for="geoip_db">GeoIP Database File</label> ${docker_msg | n}
<label for="geoip_db">GeoLite2 Database File</label> ${docker_msg | n}
<div class="row">
<div class="col-md-6">
<div class="col-md-9">
<div class="input-group">
<input type="text" class="form-control" id="geoip_db" name="geoip_db" value="${config['geoip_db']}" ${docker_setting}>
<div class="btn-group">
<button class="btn btn-form" type="button" id="install_geoip_db">${'Update' if plexpy.CONFIG.GEOIP_DB_INSTALLED else 'Install'}</button>
<span class="input-group-btn">
<button class="btn btn-form" type="button" id="install_geoip_db">${'Update' if config["geoip_db_installed"] else 'Install'}</button>
<button class="btn btn-form" type="button" id="uninstall_geoip_db">Uninstall</button>
</span>
</div>
</div>
</div>
<p class="help-block">
The GeoLite2 database is used to geolocate IP addresses. Database last updated <strong><span id="geoip_db_updated">never</span></strong>.
</p>
</div>
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
@ -2818,24 +2823,43 @@ $(document).ready(function() {
openPlexXML('/api/resources', true, {includeHttps: 1});
});
if ("${kwargs.get('install_geoip')}" == 'true') {
if ("${kwargs.get('install_geoip')}" === 'true') {
gotoSetting('notifications', 'geoip_db')
}
if ("${config['geoip_db_installed']}" > "0") {
$("#geoip_db_updated").text(moment("${config['geoip_db_installed']}", "X").fromNow());
}
$("#install_geoip_db").click(function () {
var msg = 'Are you sure you want to install the GeoLite2 database?<br /><br />' +
'The database is used to lookup IP address geolocation info.<br />' +
'The database will be downloaded from <a href="${anon_url("https://dev.maxmind.com/geoip/geoip2/geolite2/")}" target="_blank">MaxMind</a>, <br />' +
'and requires <strong>100MB</strong> of free space to install in your Tautulli directory.<br />'
'and requires <strong>100MB</strong> of free space to install in your Tautulli directory.<br />';
var url = 'install_geoip_db';
confirmAjaxCall(url, msg, null, 'Installing GeoLite2 database.', function () {$('#install_geoip_db').text('Update');});
if ($(this).text() === 'Update') {
url += '?update=true';
}
confirmAjaxCall(url, msg, null, 'Installing GeoLite2 database.', function (result) {
if (result.result === "success") {
$('#install_geoip_db').text('Update');
$('#geoip_db_updated').text(moment(result.updated, "X").fromNow());
}
getSchedulerTable();
});
});
$("#uninstall_geoip_db").click(function () {
var msg = 'Are you sure you want to uninstall the GeoLite2 database?<br /><br />' +
'You will not be able to lookup IP address geolocation info.';
var url = 'uninstall_geoip_db';
confirmAjaxCall(url, msg, null, 'Uninstalling GeoLite2 database.', function () {$('#install_geoip_db').text('Install');});
confirmAjaxCall(url, msg, null, 'Uninstalling GeoLite2 database.', function (result) {
if (result.result === "success") {
$('#install_geoip_db').text('Install');
$('#geoip_db_updated').text('never');
}
getSchedulerTable();
});
});
});
</script>

View file

@ -39,6 +39,7 @@ import activity_pinger
import common
import database
import datafactory
import helpers
import libraries
import logger
import mobile_app
@ -444,6 +445,8 @@ def initialize_scheduler():
hours=backup_hours, minutes=0, seconds=0, args=(True, True))
schedule_job(config.make_backup, 'Backup Tautulli config',
hours=backup_hours, minutes=0, seconds=0, args=(True, True))
schedule_job(helpers.update_geoip_db, 'Update GeoLite2 database',
hours=12 * bool(CONFIG.GEOIP_DB_INSTALLED > 1), minutes=0, seconds=0)
if WS_CONNECTED and CONFIG.PMS_IP and CONFIG.PMS_TOKEN:
schedule_job(plextv.get_server_resources, 'Refresh Plex server URLs',

View file

@ -197,7 +197,8 @@ SCHEDULER_LIST = [
'Refresh libraries list',
'Refresh Plex server URLs',
'Backup Tautulli database',
'Backup Tautulli config'
'Backup Tautulli config',
'Update GeoLite2 database'
]
DATE_TIME_FORMATS = [

View file

@ -584,7 +584,16 @@ def is_valid_ip(address):
return False
def install_geoip_db():
def update_geoip_db():
if plexpy.CONFIG.GEOIP_DB_INSTALLED > 1:
logger.info(u"Tautulli Helpers :: Checking for GeoLite2 database updates.")
now = int(time.time())
if now - plexpy.CONFIG.GEOIP_DB_INSTALLED >= 2592000: # 30 days
return install_geoip_db(update=True)
logger.info(u"Tautulli Helpers :: GeoLite2 database already updated within the last 30 days.")
def install_geoip_db(update=False):
if not plexpy.CONFIG.MAXMIND_LICENSE_KEY:
logger.error(u"Tautulli Helpers :: Failed to download GeoLite2 database file from MaxMind: Missing MaxMindLicense Key")
return False
@ -655,25 +664,33 @@ def install_geoip_db():
except Exception as e:
logger.warn(u"Tautulli Helpers :: Failed to remove temporary GeoLite2 gzip file: %s" % e)
logger.debug(u"Tautulli Helpers :: GeoLite2 database installed successfully.")
plexpy.CONFIG.__setattr__('GEOIP_DB', geolite2_db_path)
plexpy.CONFIG.__setattr__('GEOIP_DB_INSTALLED', int(time.time()))
plexpy.CONFIG.write()
return True
logger.debug(u"Tautulli Helpers :: GeoLite2 database installed successfully.")
if not update:
plexpy.schedule_job(update_geoip_db, 'Update GeoLite2 database', hours=12, minutes=0, seconds=0)
return plexpy.CONFIG.GEOIP_DB_INSTALLED
def uninstall_geoip_db():
logger.debug(u"Tautulli Helpers :: Uninstalling the GeoLite2 database...")
try:
os.remove(plexpy.CONFIG.GEOIP_DB)
plexpy.CONFIG.__setattr__('GEOIP_DB_INSTALLED', 0)
plexpy.CONFIG.write()
except Exception as e:
logger.error(u"Tautulli Helpers :: Failed to uninstall the GeoLite2 database: %s" % e)
return False
plexpy.CONFIG.__setattr__('GEOIP_DB_INSTALLED', 0)
plexpy.CONFIG.write()
logger.debug(u"Tautulli Helpers :: GeoLite2 database uninstalled successfully.")
plexpy.schedule_job(update_geoip_db, 'Update GeoLite2 database', hours=0, minutes=0, seconds=0)
return True

View file

@ -2821,6 +2821,7 @@ class WebInterface(object):
"newsletter_custom_dir": plexpy.CONFIG.NEWSLETTER_CUSTOM_DIR,
"win_sys_tray": checked(plexpy.CONFIG.WIN_SYS_TRAY),
"geoip_db": plexpy.CONFIG.GEOIP_DB,
"geoip_db_installed": plexpy.CONFIG.GEOIP_DB_INSTALLED,
"maxmind_license_key": plexpy.CONFIG.MAXMIND_LICENSE_KEY
}
@ -3057,15 +3058,17 @@ class WebInterface(object):
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))
@addtoapi()
def install_geoip_db(self, **kwargs):
def install_geoip_db(self, update=False, **kwargs):
""" Downloads and installs the GeoLite2 database """
result = helpers.install_geoip_db()
update = True if update == 'true' else False
result = helpers.install_geoip_db(update=update)
if result:
return {'result': 'success', 'message': 'GeoLite2 database installed successful.'}
return {'result': 'success', 'message': 'GeoLite2 database installed successful.', 'updated': result}
else:
return {'result': 'error', 'message': 'GeoLite2 database install failed.'}
return {'result': 'error', 'message': 'GeoLite2 database install failed.', 'updated': 0}
@cherrypy.expose
@cherrypy.tools.json_out()