From b7b2bd45928251bb3538c7ab5a99cfddd8128a9a Mon Sep 17 00:00:00 2001 From: ziajka Date: Tue, 5 Mar 2019 10:43:31 +0100 Subject: [PATCH] Support bundled ubridge, Ref: #314 --- local-server.js | 53 +++++++++++-------- scripts/build.py | 14 +++-- .../add-server-dialog.component.html | 4 ++ .../add-server-dialog.component.ts | 18 ++++++- src/app/models/server.ts | 1 + 5 files changed, 61 insertions(+), 29 deletions(-) diff --git a/local-server.js b/local-server.js index 840a0c28..659eefdb 100644 --- a/local-server.js +++ b/local-server.js @@ -11,19 +11,13 @@ const isWin = /^win/.test(process.platform); let runningServers = {}; exports.getLocalServerPath = async () => { - const lookupDirectories = [ - __dirname, - path.dirname(app.getPath('exe')) - ]; + let binary = isWin ? 'gns3server.exe': 'gns3server'; + return findBinary('exe.', binary); +} - for(var directory of lookupDirectories) { - const serverPath = await findLocalServerPath(directory); - if(serverPath !== undefined) { - return serverPath; - } - } - - return; +exports.getUbridgePath = async () => { + let binary = isWin ? 'ubridge.exe': 'ubridge'; + return findBinary('ubridge', binary); } exports.startLocalServer = async (server) => { @@ -44,7 +38,21 @@ exports.stopAllLocalServers = async () => { return await stopAll(); } -async function findLocalServerPath(baseDirectory) { +async function findBinary(binaryDirectory, filename) { + const lookupDirectories = [ + __dirname, + path.dirname(app.getPath('exe')) + ]; + + for(var directory of lookupDirectories) { + const serverPath = await findBinaryInDirectory(directory, binaryDirectory, filename); + if(serverPath !== undefined) { + return serverPath; + } + } +} + +async function findBinaryInDirectory(baseDirectory, binaryDirectory, filename) { const distDirectory = path.join(baseDirectory, 'dist'); if (!fs.existsSync(distDirectory)) { @@ -53,26 +61,22 @@ async function findLocalServerPath(baseDirectory) { const files = fs.readdirSync(distDirectory); - let serverPath = null; + let binaryPath = null; files.forEach((directory) => { - if(directory.startsWith('exe.')) { - if (isWin) { - serverPath = path.join(baseDirectory, 'dist', directory, 'gns3server.exe'); - } - else { - serverPath = path.join(baseDirectory, 'dist', directory, 'gns3server'); - } + if(directory.startsWith(binaryDirectory)) { + binaryPath = path.join(baseDirectory, 'dist', directory, filename); } }); - if(serverPath !== null && fs.existsSync(serverPath)) { - return serverPath; + if(binaryPath !== null && fs.existsSync(binaryPath)) { + return binaryPath; } return; } + function getServerArguments(server, overrides, configPath) { let serverArguments = []; if(server.host) { @@ -183,6 +187,9 @@ async function configure(configPath, server) { if(server.port) { config.port = server.port; } + if(server.ubridge_path) { + config.ubridge_path = server.ubridge_path; + } fs.writeFileSync(configPath, ini.stringify(config, { section: 'Server' })); } diff --git a/scripts/build.py b/scripts/build.py index fcff9969..74908da6 100644 --- a/scripts/build.py +++ b/scripts/build.py @@ -38,7 +38,7 @@ WORKING_DIR = os.path.join(FILE_DIR, 'tmp') SOURCE_ZIP = os.path.join(WORKING_DIR, 'gns3-server.source.zip') SOURCE_DESTINATION = os.path.join(WORKING_DIR, 'source') BINARIES_EXTENSION = platform.system() == "Windows" and ".exe" or "" - +UBRIDGE_VERSION = 'LATEST' # or for eg. 0.9.14 def download(url, output): print("Downloading {} to {}".format(url, output)) @@ -88,20 +88,24 @@ def download_dependencies_command(arguments): response = requests.get(url) response.raise_for_status() releases = response.json() - last_release = releases[0] - + + if UBRIDGE_VERSION == 'LATEST': + release = releases[0] + else: + release = list(filter(lambda x: x['tag_name'] == "v{}".format(UBRIDGE_VERSION), releases))[0] + # on Windows download cygwin1.dll and ubridge.exe if platform.system() == "Windows": ubridge_dir = os.path.join(output_directory, 'ubridge') os.makedirs(ubridge_dir, exist_ok=True) cygwin_file = os.path.join(ubridge_dir, 'cygwin1.dll') - cygwin_url = list(filter(lambda x: x['name'] == 'cygwin1.dll', last_release['assets']))[0]['url'] + cygwin_url = list(filter(lambda x: x['name'] == 'cygwin1.dll', release['assets']))[0]['browser_download_url'] download(cygwin_url, cygwin_file) print('Downloaded cygwin1.dll to {}'.format(cygwin_file)) ubridge_file = os.path.join(ubridge_dir, 'ubridge.exe') - ubridge_url = list(filter(lambda x: x['name'] == 'ubridge.exe', last_release['assets']))[0]['url'] + ubridge_url = list(filter(lambda x: x['name'] == 'ubridge.exe', release['assets']))[0]['browser_download_url'] download(ubridge_url, ubridge_file) print('Downloaded ubridge.exe to {}'.format(ubridge_file)) diff --git a/src/app/components/servers/add-server-dialog/add-server-dialog.component.html b/src/app/components/servers/add-server-dialog/add-server-dialog.component.html index 61265773..6f7e3ada 100644 --- a/src/app/components/servers/add-server-dialog/add-server-dialog.component.html +++ b/src/app/components/servers/add-server-dialog/add-server-dialog.component.html @@ -16,6 +16,10 @@ + + + + diff --git a/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts b/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts index 6d045ab8..b0221308 100644 --- a/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts +++ b/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts @@ -18,6 +18,7 @@ export class AddServerDialogComponent implements OnInit { 'name': new FormControl('', [ Validators.required ]), 'location': new FormControl(''), 'path': new FormControl(''), + 'ubridge_path': new FormControl(''), 'host': new FormControl('', [ Validators.required ]), 'port': new FormControl('', [ Validators.required, Validators.min(1) ]), 'authorization': new FormControl('none'), @@ -72,24 +73,39 @@ export class AddServerDialogComponent implements OnInit { return; } + async getDefaultUbridgePath() { + if(this.electronService.isElectronApp) { + return await this.electronService.remote.require('./local-server.js').getUbridgePath(); + } + return; + } + async ngOnInit() { this.locations = await this.getLocations(); const defaultLocalServerPath = await this.getDefaultLocalServerPath(); + const defaultUbridgePath = await this.getDefaultUbridgePath(); this.serverForm.get('location').valueChanges.subscribe((location: string) => { const pathControl = this.serverForm.get('path'); + const ubridgePathControl = this.serverForm.get('ubridge_path'); if(location === 'local') { pathControl.setValue(defaultLocalServerPath); pathControl.setValidators([Validators.required]); + + ubridgePathControl.setValue(defaultUbridgePath); + ubridgePathControl.setValidators([Validators.required]); } else { pathControl.setValue(''); pathControl.clearValidators(); + + ubridgePathControl.setValue(''); + ubridgePathControl.clearValidators(); } - [pathControl].forEach((control) => { + [pathControl, ubridgePathControl].forEach((control) => { control.updateValueAndValidity({ onlySelf: true }); diff --git a/src/app/models/server.ts b/src/app/models/server.ts index 2ad4ccd1..cf8649f3 100644 --- a/src/app/models/server.ts +++ b/src/app/models/server.ts @@ -9,6 +9,7 @@ export class Server { host: string; port: number; path: string; + ubridge_path: string; authorization: ServerAuthorization; login: string; password: string;