def images(): localImages = getLocalImages() profiles = getProfiles() remoteImages = getRemoteImages() remotePaesslerImages = getRemotePaesslerImages() nightlyImages = getNightlyImages() remoteImagesLink = Config().get( meta.APP_NAME, '{}.images.remote'.format(meta.APP_NAME.lower())) remotePaesslerImagesLink = Config().get( meta.APP_NAME, '{}.images.remote-paessler'.format(meta.APP_NAME.lower())) return render_template('images.html', currentpage='Images', localImages=localImages, remoteImages=remoteImages, remotePaesslerImages=remotePaesslerImages, nightlyImages=nightlyImages, profiles=profiles, jsData={ 'local': json.dumps(localImages), 'remote': json.dumps(remoteImages), 'remotePaessler': json.dumps(remotePaesslerImages), 'nightly': json.dumps(nightlyImages) }, memory=memory(), lxdui_current_version=VERSION, remoteImagesLink=remoteImagesLink, remotePaesslerImagesLink=remotePaesslerImagesLink)
def detailsRemotePaesslerImage(self, alias): try: remotePaesslerImagesLink = Config().get(meta.APP_NAME, '{}.images.remote-paessler'.format(meta.APP_NAME.lower())) verify = False if Config().get(meta.APP_NAME, '{}.lxd.sslverify'.format(meta.APP_NAME.lower())) == 'false' else True remoteClient = Client(endpoint=remotePaesslerImagesLink, verify=False) fingerprint = remoteClient.api.images.aliases[alias].get().json()['metadata']['target'] return remoteClient.api.images[fingerprint].get().json()['metadata'] except Exception as e: raise ValueError(e)
def delete(): """Delete certificates""" path = 'conf' key = Config().get(APP, '{}.ssl.key'.format(APP.lower())) cert = Config().get(APP, '{}.ssl.cert'.format(APP.lower())) try: os.remove(key) os.remove(cert) except IOError as e: click.echo(e)
def initAuth(app): APP = meta.APP_NAME tokenExpiration = int(Config().get(APP, '{}.jwt.token.expiration'.format(APP.lower()))) secretKey = Config().get(APP, '{}.jwt.secret.key'.format(APP.lower())) authUrlRule = Config().get(APP, '{}.jwt.auth.url.rule'.format(APP.lower())) if (tokenExpiration == None): tokenExpiration = 1200 app.config['SECRET_KEY'] = secretKey app.config['JWT_AUTH_URL_RULE'] = authUrlRule app.config['JWT_EXPIRATION_DELTA'] = timedelta(seconds=tokenExpiration) JWT(app, authenticate, identity)
def list(): """Show available certificates""" # path = Config().get('LXDUI', 'lxdui.conf.dir') key = Config().get(APP, '{}.ssl.key'.format(APP.lower())) cert = Config().get(APP, '{}.ssl.cert'.format(APP.lower())) path = 'conf' for root, dirs, files in os.walk(path): for file in files: name, ext = os.path.splitext(file) if ext in ['.key', '.crt']: click.echo(os.path.join(path, file))
def __init__(self, remoteHost='127.0.0.1'): logging.info('Accessing PyLXD client') verify = False if Config().get(meta.APP_NAME, '{}.lxd.remote.enable'.format(meta.APP_NAME.lower())) == 'true': try: remoteHost = Config().get(meta.APP_NAME, '{}.lxd.remote'.format(meta.APP_NAME.lower())) verify = False if Config().get(meta.APP_NAME, '{}.lxd.sslverify'.format(meta.APP_NAME.lower())) == 'false' else True except: pass self.client = Client(endpoint=remoteHost, verify=False, cert=None) else: self.client = Client(verify=False)
def downloadPaesslerImage(self, image): try: remotePaesslerImagesLink = Config().get(meta.APP_NAME, '{}.images.remote-paessler'.format(meta.APP_NAME.lower())) logging.info('Downloading remote image:', image) verify = False if Config().get(meta.APP_NAME,'{}.lxd.sslverify'.format(meta.APP_NAME.lower())) == 'false' else True remoteClient = Client(endpoint=remotePaesslerImagesLink, verify=False) try: remoteImage = remoteClient.images.get_by_alias(image) except: remoteImage = remoteClient.images.get(image) newImage = remoteImage.copy(self.client, auto_update=False, public=False, wait=True) return self.client.api.images[newImage.fingerprint].get().json()['metadata'] except Exception as e: logging.error('Failed to download image:') logging.exception(e) raise ValueError(e)
def listRemotePaesslerImages(self): try: remotePaesslerImagesLink = Config().get(meta.APP_NAME, '{}.images.remote-paessler'.format(meta.APP_NAME.lower())) logging.info('Reading remote image list') verify = False if Config().get(meta.APP_NAME, '{}.lxd.sslverify'.format(meta.APP_NAME.lower())) == 'false' else True CERTS_PATH = Config().get(meta.APP_NAME, '{}.conf.dir'.format(meta.APP_NAME.lower())) cert = CertRemotePaessler( cert=os.path.expanduser(os.path.join(CERTS_PATH, 'client.crt')), key=os.path.expanduser(os.path.join(CERTS_PATH, 'client.key')) ) remoteClient = Client(endpoint=remotePaesslerImagesLink, verify=verify, cert=cert) return remoteImagesList(remoteClient.api.images.aliases.get().json()) except Exception as e: logging.error('Failed to get remote container images: ') logging.exception(e) raise ValueError(e)
def detailsRemoteImage(self, alias): try: remoteImagesLink = Config().get(meta.APP_NAME, '{}.images.remote'.format(meta.APP_NAME.lower())) remoteClient = Client(endpoint=remoteImagesLink) fingerprint = remoteClient.api.images.aliases[alias].get().json()['metadata']['target'] return remoteClient.api.images[fingerprint].get().json()['metadata'] except Exception as e: raise ValueError(e)
def jwt_decode_handler(token): try: APP = meta.APP_NAME secretKey = Config().get(APP, '{}.jwt.secret.key'.format(APP.lower())) payload = jwt.decode(token, secretKey, algorithm='HS256') return True except jwt.InvalidTokenError: return False
def restart(): """Restart LXDUI""" port = int(Config().get('LXDUI', 'lxdui.port')) click.echo('Restarting with defaults.') core.stop() click.echo('Port = {} \nDebug = False\nMode = Foreground\n'.format(port)) time.sleep(3) core.start(port, False, uiPages)
def _doStart(args=None): port = 5000 try: port = int(Config().get('LXDUI', 'lxdui.port')) except: print('Please initialize {} first. e.g: {} init '.format( meta.APP_NAME, meta.APP_CLI_CMD)) exit() core.startApp(port, uiPages)
def listRemoteImages(self): try: remoteImagesLink = Config().get(meta.APP_NAME, '{}.images.remote'.format(meta.APP_NAME.lower())) logging.info('Reading remote image list') remoteClient = Client(endpoint=remoteImagesLink) return remoteImagesList(remoteClient.api.images.aliases.get().json()) except Exception as e: logging.error('Failed to get remote container images: ') logging.exception(e) raise ValueError(e)
def _start(debug=False): port = 5000 try: port = int(Config().get('LXDUI', 'lxdui.port')) except: print('Please initialize {} first. e.g: {} init '.format( meta.APP_NAME, meta.APP_CLI_CMD)) exit() core.start(port, debug, uiPages)
def initAuth(app): APP = meta.APP_NAME tokenExpiration = int(Config().get(APP, '{}.token.expiration'.format(APP.lower()))) if (tokenExpiration == None): tokenExpiration = 1200 app.config['SECRET_KEY'] = 'AC8d83&21Almnis710sds' app.config['JWT_AUTH_URL_RULE'] = '/api/user/login' app.config['JWT_EXPIRATION_DELTA'] = timedelta(seconds=tokenExpiration) JWT(app, authenticate, identity)
def add(cert, key): """Add preexisting certificates""" if (os.path.exists(key) and os.path.exists(cert)) and (os.path.isfile(key) and os.path.isfile(cert)): conf = Config() conf.set(APP, '{}.ssl.key'.format(APP.lower()), key) conf.set(APP, '{}.ssl.cert'.format(APP.lower()), cert) conf.save() else: log.info( "Error reading user provided key and certificate. key={} cert={} ". format(key, cert)) raise Exception("Please check your paths and try again")
def start(): """Start LXDUI""" ''' TODO: add -d option to start the server as a daemon ''' # if daemon: # #start in the background #click.echo("Starting %s" % APP) port = 5000 try: port = int(Config().get('LXDUI', 'lxdui.port')) except: print('Please initialize {} first. e.g: {} init '.format( meta.APP_NAME, meta.APP_CLI_CMD)) exit() core.startApp(port, uiPages)
def set(parameter, value): """Set a configuration parameter""" Config().set(APP, parameter, value)
def show(): """Show configured parameters""" Config().show()
def _doStop(args=None): click.echo("Stopping %s" % APP) subprocess.Popen("fuser -k {}/tcp".format(Config().get( 'LXDUI', 'lxdui.port')), shell=True).wait()