add support for server templates/inheritance
This commit is contained in:
		@@ -243,7 +243,7 @@ def sync(ctx, servers):
 | 
				
			|||||||
                                       label='Synchronizing') as bar:
 | 
					                                       label='Synchronizing') as bar:
 | 
				
			||||||
                    i = 0
 | 
					                    i = 0
 | 
				
			||||||
                    for state in outdated:
 | 
					                    for state in outdated:
 | 
				
			||||||
                        server.syncToVersion(state.plugin)
 | 
					                        server.syncToVersion(state.wantedVersion)
 | 
				
			||||||
                        i += 1
 | 
					                        i += 1
 | 
				
			||||||
                        bar.update(i)
 | 
					                        bar.update(i)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,11 +60,33 @@ class Config():
 | 
				
			|||||||
            'path': str(path)
 | 
					            '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):
 | 
					    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):
 | 
					    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):
 | 
					    def update_server(self, server, config):
 | 
				
			||||||
        self.config['servers'][server] = config
 | 
					        self.config['servers'][server] = config
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,9 +5,30 @@ import logging
 | 
				
			|||||||
import hashlib
 | 
					import hashlib
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import semantic_version
 | 
					import semantic_version
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
logger = logging.getLogger('mpm.server')
 | 
					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:
 | 
					class Server:
 | 
				
			||||||
    def __init__(self, name, config):
 | 
					    def __init__(self, name, config):
 | 
				
			||||||
        self.name = name
 | 
					        self.name = name
 | 
				
			||||||
@@ -79,14 +100,14 @@ class Server:
 | 
				
			|||||||
                    logger.debug("Running checksum search to find version of %s", pluginJar)
 | 
					                    logger.debug("Running checksum search to find version of %s", pluginJar)
 | 
				
			||||||
                    pluginPath = os.path.join(self.pluginPath, pluginJar)
 | 
					                    pluginPath = os.path.join(self.pluginPath, pluginJar)
 | 
				
			||||||
                    with open(pluginPath, 'rb') as fd:
 | 
					                    with open(pluginPath, 'rb') as fd:
 | 
				
			||||||
                        installedHash = hashlib.file_digest(fd, 'sha256')
 | 
					                        installedHash = digest(fd)
 | 
				
			||||||
                    pluginName = pluginJar.split('.jar')[0]
 | 
					                    pluginName = pluginJar.split('.jar')[0]
 | 
				
			||||||
                    defaultVersion = semantic_version.Version("0.0.0")
 | 
					                    defaultVersion = semantic_version.Version("0.0.0")
 | 
				
			||||||
                    for repo in repos:
 | 
					                    for repo in repos:
 | 
				
			||||||
                        for plugin in repo.plugins():
 | 
					                        for plugin in repo.plugins():
 | 
				
			||||||
                            if plugin.name == pluginName:
 | 
					                            if plugin.name == pluginName:
 | 
				
			||||||
                                with open(plugin.path, 'rb') as fd:
 | 
					                                with open(plugin.path, 'rb') as fd:
 | 
				
			||||||
                                    foundHash = hashlib.file_digest(fd, 'sha256')
 | 
					                                    foundHash = digest(fd)
 | 
				
			||||||
                                if foundHash.hexdigest() == installedHash.hexdigest():
 | 
					                                if foundHash.hexdigest() == installedHash.hexdigest():
 | 
				
			||||||
                                    return Plugin(pluginPath,
 | 
					                                    return Plugin(pluginPath,
 | 
				
			||||||
                                                  forceName=pluginName,
 | 
					                                                  forceName=pluginName,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user