Support bundled ubridge, Ref: #314

This commit is contained in:
ziajka 2019-03-05 10:43:31 +01:00
parent c40767879f
commit b7b2bd4592
5 changed files with 61 additions and 29 deletions

View File

@ -11,19 +11,13 @@ const isWin = /^win/.test(process.platform);
let runningServers = {}; let runningServers = {};
exports.getLocalServerPath = async () => { exports.getLocalServerPath = async () => {
const lookupDirectories = [ let binary = isWin ? 'gns3server.exe': 'gns3server';
__dirname, return findBinary('exe.', binary);
path.dirname(app.getPath('exe')) }
];
for(var directory of lookupDirectories) { exports.getUbridgePath = async () => {
const serverPath = await findLocalServerPath(directory); let binary = isWin ? 'ubridge.exe': 'ubridge';
if(serverPath !== undefined) { return findBinary('ubridge', binary);
return serverPath;
}
}
return;
} }
exports.startLocalServer = async (server) => { exports.startLocalServer = async (server) => {
@ -44,7 +38,21 @@ exports.stopAllLocalServers = async () => {
return await stopAll(); 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'); const distDirectory = path.join(baseDirectory, 'dist');
if (!fs.existsSync(distDirectory)) { if (!fs.existsSync(distDirectory)) {
@ -53,26 +61,22 @@ async function findLocalServerPath(baseDirectory) {
const files = fs.readdirSync(distDirectory); const files = fs.readdirSync(distDirectory);
let serverPath = null; let binaryPath = null;
files.forEach((directory) => { files.forEach((directory) => {
if(directory.startsWith('exe.')) { if(directory.startsWith(binaryDirectory)) {
if (isWin) { binaryPath = path.join(baseDirectory, 'dist', directory, filename);
serverPath = path.join(baseDirectory, 'dist', directory, 'gns3server.exe');
}
else {
serverPath = path.join(baseDirectory, 'dist', directory, 'gns3server');
}
} }
}); });
if(serverPath !== null && fs.existsSync(serverPath)) { if(binaryPath !== null && fs.existsSync(binaryPath)) {
return serverPath; return binaryPath;
} }
return; return;
} }
function getServerArguments(server, overrides, configPath) { function getServerArguments(server, overrides, configPath) {
let serverArguments = []; let serverArguments = [];
if(server.host) { if(server.host) {
@ -183,6 +187,9 @@ async function configure(configPath, server) {
if(server.port) { if(server.port) {
config.port = server.port; config.port = server.port;
} }
if(server.ubridge_path) {
config.ubridge_path = server.ubridge_path;
}
fs.writeFileSync(configPath, ini.stringify(config, { section: 'Server' })); fs.writeFileSync(configPath, ini.stringify(config, { section: 'Server' }));
} }

View File

@ -38,7 +38,7 @@ WORKING_DIR = os.path.join(FILE_DIR, 'tmp')
SOURCE_ZIP = os.path.join(WORKING_DIR, 'gns3-server.source.zip') SOURCE_ZIP = os.path.join(WORKING_DIR, 'gns3-server.source.zip')
SOURCE_DESTINATION = os.path.join(WORKING_DIR, 'source') SOURCE_DESTINATION = os.path.join(WORKING_DIR, 'source')
BINARIES_EXTENSION = platform.system() == "Windows" and ".exe" or "" BINARIES_EXTENSION = platform.system() == "Windows" and ".exe" or ""
UBRIDGE_VERSION = 'LATEST' # or for eg. 0.9.14
def download(url, output): def download(url, output):
print("Downloading {} to {}".format(url, output)) print("Downloading {} to {}".format(url, output))
@ -88,20 +88,24 @@ def download_dependencies_command(arguments):
response = requests.get(url) response = requests.get(url)
response.raise_for_status() response.raise_for_status()
releases = response.json() 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 # on Windows download cygwin1.dll and ubridge.exe
if platform.system() == "Windows": if platform.system() == "Windows":
ubridge_dir = os.path.join(output_directory, 'ubridge') ubridge_dir = os.path.join(output_directory, 'ubridge')
os.makedirs(ubridge_dir, exist_ok=True) os.makedirs(ubridge_dir, exist_ok=True)
cygwin_file = os.path.join(ubridge_dir, 'cygwin1.dll') 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) download(cygwin_url, cygwin_file)
print('Downloaded cygwin1.dll to {}'.format(cygwin_file)) print('Downloaded cygwin1.dll to {}'.format(cygwin_file))
ubridge_file = os.path.join(ubridge_dir, 'ubridge.exe') 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) download(ubridge_url, ubridge_file)
print('Downloaded ubridge.exe to {}'.format(ubridge_file)) print('Downloaded ubridge.exe to {}'.format(ubridge_file))

View File

@ -16,6 +16,10 @@
<input matInput tabindex="1" formControlName="path" placeholder="Local server path" /> <input matInput tabindex="1" formControlName="path" placeholder="Local server path" />
</mat-form-field> </mat-form-field>
<mat-form-field *ngIf="serverForm.get('location').value === 'local'">
<input matInput tabindex="1" formControlName="ubridge_path" placeholder="Ubridge path" />
</mat-form-field>
<mat-form-field> <mat-form-field>
<input matInput tabindex="1" formControlName="host" placeholder="Host" /> <input matInput tabindex="1" formControlName="host" placeholder="Host" />
</mat-form-field> </mat-form-field>

View File

@ -18,6 +18,7 @@ export class AddServerDialogComponent implements OnInit {
'name': new FormControl('', [ Validators.required ]), 'name': new FormControl('', [ Validators.required ]),
'location': new FormControl(''), 'location': new FormControl(''),
'path': new FormControl(''), 'path': new FormControl(''),
'ubridge_path': new FormControl(''),
'host': new FormControl('', [ Validators.required ]), 'host': new FormControl('', [ Validators.required ]),
'port': new FormControl('', [ Validators.required, Validators.min(1) ]), 'port': new FormControl('', [ Validators.required, Validators.min(1) ]),
'authorization': new FormControl('none'), 'authorization': new FormControl('none'),
@ -72,24 +73,39 @@ export class AddServerDialogComponent implements OnInit {
return; return;
} }
async getDefaultUbridgePath() {
if(this.electronService.isElectronApp) {
return await this.electronService.remote.require('./local-server.js').getUbridgePath();
}
return;
}
async ngOnInit() { async ngOnInit() {
this.locations = await this.getLocations(); this.locations = await this.getLocations();
const defaultLocalServerPath = await this.getDefaultLocalServerPath(); const defaultLocalServerPath = await this.getDefaultLocalServerPath();
const defaultUbridgePath = await this.getDefaultUbridgePath();
this.serverForm.get('location').valueChanges.subscribe((location: string) => { this.serverForm.get('location').valueChanges.subscribe((location: string) => {
const pathControl = this.serverForm.get('path'); const pathControl = this.serverForm.get('path');
const ubridgePathControl = this.serverForm.get('ubridge_path');
if(location === 'local') { if(location === 'local') {
pathControl.setValue(defaultLocalServerPath); pathControl.setValue(defaultLocalServerPath);
pathControl.setValidators([Validators.required]); pathControl.setValidators([Validators.required]);
ubridgePathControl.setValue(defaultUbridgePath);
ubridgePathControl.setValidators([Validators.required]);
} }
else { else {
pathControl.setValue(''); pathControl.setValue('');
pathControl.clearValidators(); pathControl.clearValidators();
ubridgePathControl.setValue('');
ubridgePathControl.clearValidators();
} }
[pathControl].forEach((control) => { [pathControl, ubridgePathControl].forEach((control) => {
control.updateValueAndValidity({ control.updateValueAndValidity({
onlySelf: true onlySelf: true
}); });

View File

@ -9,6 +9,7 @@ export class Server {
host: string; host: string;
port: number; port: number;
path: string; path: string;
ubridge_path: string;
authorization: ServerAuthorization; authorization: ServerAuthorization;
login: string; login: string;
password: string; password: string;