add support for server templates/inheritance

This commit is contained in:
Torrie Fischer 2023-08-20 10:45:08 +02:00
parent 5e7c703cca
commit 0a2a9d5e09
3 changed files with 48 additions and 5 deletions

View File

@ -243,7 +243,7 @@ def sync(ctx, servers):
label='Synchronizing') as bar:
i = 0
for state in outdated:
server.syncToVersion(state.plugin)
server.syncToVersion(state.wantedVersion)
i += 1
bar.update(i)
else:

View File

@ -60,11 +60,33 @@ class Config():
'path': str(path)
})
def template(self, name):
return self.config['templates'].get(name, dict())
def mergeConfig(self, serverConf):
baseConf = serverConf
baseConf['plugins'] = []
mergedPlugins = {}
templates = [self.template(n) for n in serverConf['inherit']]
for template in templates:
for plugin in template.get('plugins', []):
if plugin['name'] not in mergedPlugins:
mergedPlugins[plugin['name']] = dict()
mergedPlugins[plugin['name']].update(plugin)
for plugin in serverConf['plugins']:
if plugin['name'] not in mergedPlugins:
mergedPlugins[plugin['name']] = dict()
mergedPlugins[plugin['name']].update(plugin)
for plugin in mergedPlugins:
baseConf['plugins'].append(mergedPlugins[plugin])
print(baseConf)
return baseConf
def servers(self):
return [Server(name, c) for (name, c) in self.config['servers'].items()]
return [Server(name, self.mergeConfig(c)) for (name, c) in self.config['servers'].items()]
def server(self, name):
return Server(name, self.config['servers'][name])
return Server(name, self.mergeConfig(self.config['servers'][name]))
def update_server(self, server, config):
self.config['servers'][server] = config

View File

@ -5,9 +5,30 @@ import logging
import hashlib
import os
import semantic_version
import sys
logger = logging.getLogger('mpm.server')
def digest(fd):
if sys.version_info.minor >= 11:
return hashlib.file_digest(fd, 'sha256')
else:
hasher = hashlib.sha256()
while True:
buf = fd.read(1024)
if not buf:
break
hasher.update(buf)
return hasher
class Template:
def __init__(self, name, config):
self.name = name
self.config = config
def plugins(self):
return [PluginSpec(p['name'], p.get('version', '*')) for p in self.config['plugins']]
class Server:
def __init__(self, name, config):
self.name = name
@ -79,14 +100,14 @@ class Server:
logger.debug("Running checksum search to find version of %s", pluginJar)
pluginPath = os.path.join(self.pluginPath, pluginJar)
with open(pluginPath, 'rb') as fd:
installedHash = hashlib.file_digest(fd, 'sha256')
installedHash = digest(fd)
pluginName = pluginJar.split('.jar')[0]
defaultVersion = semantic_version.Version("0.0.0")
for repo in repos:
for plugin in repo.plugins():
if plugin.name == pluginName:
with open(plugin.path, 'rb') as fd:
foundHash = hashlib.file_digest(fd, 'sha256')
foundHash = digest(fd)
if foundHash.hexdigest() == installedHash.hexdigest():
return Plugin(pluginPath,
forceName=pluginName,