Skip to content

Commit

Permalink
change ipv4 detection method (microsoft#3860)
Browse files Browse the repository at this point in the history
Co-authored-by: liuzhe <zhe.liu@microsoft.com>
  • Loading branch information
liuzhe-lz and liuzhe committed Jun 23, 2021
1 parent 27e123d commit 51c6afd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
1 change: 0 additions & 1 deletion dependencies/required.txt
@@ -1,7 +1,6 @@
astor
hyperopt == 0.1.2
json_tricks
netifaces
psutil
pyyaml
requests
Expand Down
9 changes: 0 additions & 9 deletions nni/tools/nnictl/config_schema.py
Expand Up @@ -5,7 +5,6 @@
import logging
import os

import netifaces
from schema import And, Optional, Or, Regex, Schema, SchemaError
from nni.tools.package_utils import (
create_validator_instance,
Expand Down Expand Up @@ -493,7 +492,6 @@ def validate_extras(self, experiment_config):
self.validate_tuner_adivosr_assessor(experiment_config)
self.validate_pai_trial_conifg(experiment_config)
self.validate_kubeflow_operators(experiment_config)
self.validate_eth0_device(experiment_config)
self.validate_hybrid_platforms(experiment_config)
self.validate_frameworkcontroller_trial_config(experiment_config)

Expand Down Expand Up @@ -599,13 +597,6 @@ def validate_pai_trial_conifg(self, experiment_config):
print_warning(warning_information.format('outputDir'))
self.validate_pai_config_path(experiment_config)

def validate_eth0_device(self, experiment_config):
'''validate whether the machine has eth0 device'''
if experiment_config.get('trainingServicePlatform') not in ['local'] \
and not experiment_config.get('nniManagerIp') \
and 'eth0' not in netifaces.interfaces():
raise SchemaError('This machine does not contain eth0 network device, please set nniManagerIp in config file!')

def validate_hybrid_platforms(self, experiment_config):
required_config_name_map = {
'remote': 'machineList',
Expand Down
29 changes: 13 additions & 16 deletions ts/nni_manager/common/utils.ts
Expand Up @@ -8,6 +8,7 @@ import { randomBytes } from 'crypto';
import * as cpp from 'child-process-promise';
import * as cp from 'child_process';
import { ChildProcess, spawn, StdioOptions } from 'child_process';
import * as dgram from 'dgram';
import * as fs from 'fs';
import * as net from 'net';
import * as os from 'os';
Expand Down Expand Up @@ -217,28 +218,24 @@ function cleanupUnitTest(): void {
setExperimentStartupInfo(true, 'unittest', 8080, 'unittest', undefined, logLevel);
}

let cachedipv4Address: string = '';
let cachedIpv4Address: string | null = null;

/**
* Get IPv4 address of current machine
* Get IPv4 address of current machine.
*/
function getIPV4Address(): string {
if (cachedipv4Address && cachedipv4Address.length > 0) {
return cachedipv4Address;
if (cachedIpv4Address !== null) {
return cachedIpv4Address;
}

const networkInterfaces = os.networkInterfaces();
if (networkInterfaces.eth0) {
for (const item of networkInterfaces.eth0) {
if (item.family === 'IPv4') {
cachedipv4Address = item.address;
return cachedipv4Address;
}
}
} else {
throw Error(`getIPV4Address() failed because os.networkInterfaces().eth0 is undefined. Please specify NNI manager IP in config.`);
}
// creates "udp connection" to a non-exist target, and get local address of the connection.
// since udp is connectionless, this does not send actual packets.
const socket = dgram.createSocket('udp4');
socket.connect(1, '192.0.2.0');
cachedIpv4Address = socket.address().address;
socket.close();

throw Error('getIPV4Address() failed because no valid IPv4 address found.')
return cachedIpv4Address;
}

/**
Expand Down

0 comments on commit 51c6afd

Please sign in to comment.