|
1 | 1 | #!/usr/bin/env python3 |
2 | | -from pssh.clients import ParallelSSHClient |
3 | 2 | import json |
4 | | -import sys, getopt |
5 | 3 | import os |
6 | 4 | import argparse |
7 | | -from operator import itemgetter |
8 | | -from collections import OrderedDict |
| 5 | +import subprocess |
9 | 6 |
|
10 | 7 | def write_ordered_hostfile(ordered_hosts=[],hostfile=None): |
11 | 8 | #ordered_hostfile="ordered_hostfile" |
@@ -43,28 +40,47 @@ def write_ordered_rankfile(ordered_hosts=[],hostfile=None): |
43 | 40 | #with open('/etc/opt/oci-hpc/hostfile.tcp', 'r') as f: |
44 | 41 | hosts = f.read().splitlines() |
45 | 42 |
|
46 | | -client = ParallelSSHClient(hosts) |
47 | | -output = client.run_command('curl http://169.254.169.254/opc/v1/host/') |
48 | | -#print(output) |
49 | 43 |
|
50 | 44 | r = {} |
51 | | -for host_out in output: |
52 | | - j = json.loads(bytearray(''.join(list(host_out.stdout)).encode())) |
53 | | - #print(j) |
54 | | - if j['rackId'] in r: |
55 | | - r[j['rackId']].append( host_out.host ) |
56 | | - else: |
57 | | - r[j['rackId']] = [ host_out.host ] |
58 | | - |
59 | | - |
60 | 45 | friendly_name_to_system_hostname = {} |
61 | | -hostname_output = client.run_command('/usr/bin/hostname') |
62 | | -#print(hostname_output) |
63 | | -for host_out in hostname_output: |
64 | | - #j = bytearray(''.join(list(host_out.stdout)).encode()) |
65 | | - j = bytearray(''.join(list(host_out.stdout)).encode()) |
66 | | - friendly_name_to_system_hostname[host_out.host] = j.decode(encoding='ascii') |
67 | | - #print(j.decode(encoding='ascii')+" "+host_out.host) |
| 46 | +try: |
| 47 | + from pssh.clients import ParallelSSHClient |
| 48 | + client = ParallelSSHClient(hosts) |
| 49 | + output = client.run_command('curl http://169.254.169.254/opc/v1/host/') |
| 50 | + #print(output) |
| 51 | + for host_out in output: |
| 52 | + j = json.loads(bytearray(''.join(list(host_out.stdout)).encode())) |
| 53 | + #print(j) |
| 54 | + if j['rackId'] in r: |
| 55 | + r[j['rackId']].append( host_out.host ) |
| 56 | + else: |
| 57 | + r[j['rackId']] = [ host_out.host ] |
| 58 | + hostname_output = client.run_command('/usr/bin/hostname') |
| 59 | + #print(hostname_output) |
| 60 | + for host_out in hostname_output: |
| 61 | + #j = bytearray(''.join(list(host_out.stdout)).encode()) |
| 62 | + j = bytearray(''.join(list(host_out.stdout)).encode()) |
| 63 | + friendly_name_to_system_hostname[host_out.host] = j.decode(encoding='ascii') |
| 64 | + #print(j.decode(encoding='ascii')+" "+host_out.host) |
| 65 | +except ImportError: |
| 66 | + try: |
| 67 | + for h in hosts: |
| 68 | + out = subprocess.run(["ssh "+h+" \"curl -s http://169.254.169.254/opc/v1/host/\""],stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, universal_newlines=True, check=True) |
| 69 | + x = out.stdout.splitlines() |
| 70 | + del x[-1] |
| 71 | + del x[0] |
| 72 | + rackId_str = x[1].split(":")[1].replace('"','') |
| 73 | + rackId = rackId_str.replace(' ','') |
| 74 | + if rackId in r: |
| 75 | + r[rackId].append( h ) |
| 76 | + else: |
| 77 | + r[rackId] = [ h ] |
| 78 | + for h in hosts: |
| 79 | + out = subprocess.run(["ssh "+h+" /usr/bin/hostname"],stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, universal_newlines=True, check=True) |
| 80 | + x = out.stdout.splitlines() |
| 81 | + friendly_name_to_system_hostname[h] = x[0] |
| 82 | + except subprocess.CalledProcessError as e_process_error: |
| 83 | + exit(f"Error code: {e_process_error.returncode} Output: {e_process_error.output}") |
68 | 84 |
|
69 | 85 |
|
70 | 86 | ordered_hosts = [] |
|
0 commit comments