Skip to content

Commit

Permalink
Revert "change ipv4 detection method (microsoft#3860)"
Browse files Browse the repository at this point in the history
This reverts commit 51c6afd.
  • Loading branch information
SparkSnail committed Jul 8, 2021
1 parent 88bacda commit 6af2aa4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions dependencies/required.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
astor
hyperopt == 0.1.2
json_tricks
netifaces
psutil
pyyaml
requests
Expand Down
9 changes: 9 additions & 0 deletions nni/tools/nnictl/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -492,6 +493,7 @@ 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 @@ -597,6 +599,13 @@ 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: 16 additions & 13 deletions ts/nni_manager/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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 @@ -218,24 +217,28 @@ function cleanupUnitTest(): void {
setExperimentStartupInfo(true, 'unittest', 8080, 'unittest', undefined, logLevel);
}

let cachedIpv4Address: string | null = null;

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

// 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();
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.`);
}

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

/**
Expand Down

0 comments on commit 6af2aa4

Please sign in to comment.