22
22
from hubblestack .exceptions import CommandExecutionError
23
23
24
24
log = logging .getLogger (__name__ )
25
- isFipsEnabled = True if 'usedforsecurity' in getfullargspec (hashlib .new ).kwonlyargs else False
25
+ IS_FIPS_ENABLED = True if "usedforsecurity" in getfullargspec (hashlib .new ).kwonlyargs else False
26
+
26
27
27
28
def __virtual__ ():
28
29
"""
@@ -85,13 +86,9 @@ def ping(host, timeout=False, return_boolean=False):
85
86
"""
86
87
if timeout :
87
88
if __grains__ ["kernel" ] == "SunOS" :
88
- cmd = "ping -c 4 {1} {0}" .format (
89
- timeout , __utils__ ["network.sanitize_host" ](host )
90
- )
89
+ cmd = "ping -c 4 {1} {0}" .format (timeout , __utils__ ["network.sanitize_host" ](host ))
91
90
else :
92
- cmd = "ping -W {0} -c 4 {1}" .format (
93
- timeout , __utils__ ["network.sanitize_host" ](host )
94
- )
91
+ cmd = "ping -W {0} -c 4 {1}" .format (timeout , __utils__ ["network.sanitize_host" ](host ))
95
92
else :
96
93
cmd = "ping -c 4 {0}" .format (__utils__ ["network.sanitize_host" ](host ))
97
94
if return_boolean :
@@ -212,10 +209,7 @@ def _netinfo_openbsd():
212
209
Get process information for network connections using fstat
213
210
"""
214
211
ret = {}
215
- _fstat_re = re .compile (
216
- r"internet(6)? (?:stream tcp 0x\S+ (\S+)|dgram udp (\S+))"
217
- r"(?: [<>=-]+ (\S+))?$"
218
- )
212
+ _fstat_re = re .compile (r"internet(6)? (?:stream tcp 0x\S+ (\S+)|dgram udp (\S+))" r"(?: [<>=-]+ (\S+))?$" )
219
213
out = __mods__ ["cmd.run" ]("fstat" )
220
214
for line in out .splitlines ():
221
215
try :
@@ -245,9 +239,9 @@ def _netinfo_openbsd():
245
239
else :
246
240
remote_addr = "." .join (remote_addr .rsplit (":" , 1 ))
247
241
248
- ret .setdefault (local_addr , {}).setdefault (remote_addr , {}).setdefault (
249
- proto , {}
250
- ). setdefault ( pid , {})[ "user" ] = user
242
+ ret .setdefault (local_addr , {}).setdefault (remote_addr , {}).setdefault (proto , {}). setdefault ( pid , {})[
243
+ "user"
244
+ ] = user
251
245
ret [local_addr ][remote_addr ][proto ][pid ]["cmd" ] = cmd
252
246
return ret
253
247
@@ -259,18 +253,16 @@ def _netinfo_freebsd_netbsd():
259
253
ret = {}
260
254
# NetBSD requires '-n' to disable port-to-service resolution
261
255
out = __mods__ ["cmd.run" ](
262
- "sockstat -46 {0} | tail -n+2" .format (
263
- "-n" if __grains__ ["kernel" ] == "NetBSD" else ""
264
- ),
256
+ "sockstat -46 {0} | tail -n+2" .format ("-n" if __grains__ ["kernel" ] == "NetBSD" else "" ),
265
257
python_shell = True ,
266
258
)
267
259
for line in out .splitlines ():
268
260
user , cmd , pid , _ , proto , local_addr , remote_addr = line .split ()
269
261
local_addr = "." .join (local_addr .rsplit (":" , 1 ))
270
262
remote_addr = "." .join (remote_addr .rsplit (":" , 1 ))
271
- ret .setdefault (local_addr , {}).setdefault (remote_addr , {}).setdefault (
272
- proto , {}
273
- ). setdefault ( pid , {})[ "user" ] = user
263
+ ret .setdefault (local_addr , {}).setdefault (remote_addr , {}).setdefault (proto , {}). setdefault ( pid , {})[
264
+ "user"
265
+ ] = user
274
266
ret [local_addr ][remote_addr ][proto ][pid ]["cmd" ] = cmd
275
267
return ret
276
268
@@ -364,9 +356,7 @@ def _netstat_bsd():
364
356
try :
365
357
# Master pid for this connection will be the pid whose ppid isn't
366
358
# in the subset dict we created above
367
- master_pid = next (
368
- iter (x for x , y in conn_ppid .items () if y not in ptr )
369
- )
359
+ master_pid = next (iter (x for x , y in conn_ppid .items () if y not in ptr ))
370
360
except StopIteration :
371
361
continue
372
362
ret [idx ]["user" ] = ptr [master_pid ]["user" ]
@@ -636,10 +626,7 @@ def _netstat_route_freebsd():
636
626
out = __mods__ ["cmd.run" ](cmd , python_shell = True )
637
627
for line in out .splitlines ():
638
628
comps = line .split ()
639
- if (
640
- __grains__ ["os" ] == "FreeBSD"
641
- and int (__grains__ .get ("osmajorrelease" , 0 )) < 10
642
- ):
629
+ if __grains__ ["os" ] == "FreeBSD" and int (__grains__ .get ("osmajorrelease" , 0 )) < 10 :
643
630
ret .append (
644
631
{
645
632
"addr_family" : "inet" ,
@@ -939,16 +926,14 @@ def traceroute(host):
939
926
# Modern traceroute for Linux, version 2.0.19, Dec 10 2012
940
927
# Darwin and FreeBSD traceroute version looks like: Version 1.4a12+[FreeBSD|Darwin]
941
928
942
- version_raw = re .findall (
943
- r".*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)" , version_out
944
- )[0 ]
929
+ version_raw = re .findall (r".*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)" , version_out )[0 ]
945
930
log .debug ("traceroute_version_raw: %s" , version_raw )
946
931
traceroute_version = []
947
- for t in version_raw :
932
+ for i in version_raw :
948
933
try :
949
- traceroute_version .append (int (t ))
934
+ traceroute_version .append (int (i ))
950
935
except ValueError :
951
- traceroute_version .append (t )
936
+ traceroute_version .append (i )
952
937
953
938
if len (traceroute_version ) < 3 :
954
939
traceroute_version .append (0 )
@@ -1272,9 +1257,7 @@ def ip_addrs(interface=None, include_loopback=False, cidr=None, type=None):
1272
1257
1273
1258
salt '*' network.ip_addrs
1274
1259
"""
1275
- addrs = __utils__ ["network.ip_addrs" ](
1276
- interface = interface , include_loopback = include_loopback
1277
- )
1260
+ addrs = __utils__ ["network.ip_addrs" ](interface = interface , include_loopback = include_loopback )
1278
1261
if cidr :
1279
1262
return [i for i in addrs if __utils__ ["network.in_subnet" ](cidr , [i ])]
1280
1263
else :
@@ -1285,7 +1268,6 @@ def ip_addrs(interface=None, include_loopback=False, cidr=None, type=None):
1285
1268
return addrs
1286
1269
1287
1270
1288
-
1289
1271
def ip_addrs6 (interface = None , include_loopback = False , cidr = None ):
1290
1272
"""
1291
1273
Returns a list of IPv6 addresses assigned to the host. ::1 is ignored,
@@ -1304,9 +1286,7 @@ def ip_addrs6(interface=None, include_loopback=False, cidr=None):
1304
1286
1305
1287
salt '*' network.ip_addrs6
1306
1288
"""
1307
- addrs = __utils__ ["network.ip_addrs6" ](
1308
- interface = interface , include_loopback = include_loopback
1309
- )
1289
+ addrs = __utils__ ["network.ip_addrs6" ](interface = interface , include_loopback = include_loopback )
1310
1290
if cidr :
1311
1291
return [i for i in addrs if __utils__ ["network.in_subnet" ](cidr , [i ])]
1312
1292
return addrs
@@ -1365,15 +1345,9 @@ def mod_hostname(hostname):
1365
1345
if hostname is None :
1366
1346
return False
1367
1347
1368
- hostname_cmd = __utils__ ["path.which" ]("hostnamectl" ) or __utils__ ["path.which" ](
1369
- "hostname"
1370
- )
1348
+ hostname_cmd = __utils__ ["path.which" ]("hostnamectl" ) or __utils__ ["path.which" ]("hostname" )
1371
1349
if __utils__ ["platform.is_sunos" ]():
1372
- uname_cmd = (
1373
- "/usr/bin/uname"
1374
- if __utils__ ["platform.is_smartos" ]()
1375
- else __utils__ ["path.which" ]("uname" )
1376
- )
1350
+ uname_cmd = "/usr/bin/uname" if __utils__ ["platform.is_smartos" ]() else __utils__ ["path.which" ]("uname" )
1377
1351
check_hostname_cmd = __utils__ ["path.which" ]("check-hostname" )
1378
1352
1379
1353
# Grab the old hostname so we know which hostname to change and then
@@ -1398,12 +1372,16 @@ def mod_hostname(hostname):
1398
1372
1399
1373
if hostname_cmd .endswith ("hostnamectl" ):
1400
1374
result = __mods__ ["cmd.run_all" ](
1401
- "{0} set-hostname {1}" .format (hostname_cmd , hostname ,)
1375
+ "{0} set-hostname {1}" .format (
1376
+ hostname_cmd ,
1377
+ hostname ,
1378
+ )
1402
1379
)
1403
1380
if result ["retcode" ] != 0 :
1404
1381
log .debug (
1405
1382
"{0} was unable to set hostname. Error: {1}" .format (
1406
- hostname_cmd , result ["stderr" ],
1383
+ hostname_cmd ,
1384
+ result ["stderr" ],
1407
1385
)
1408
1386
)
1409
1387
return False
@@ -1435,9 +1413,7 @@ def mod_hostname(hostname):
1435
1413
# new hostname
1436
1414
if __grains__ ["os_family" ] == "RedHat" :
1437
1415
with __utils__ ["files.fopen" ]("/etc/sysconfig/network" , "r" ) as fp_ :
1438
- network_c = [
1439
- __utils__ ["stringutils.to_unicode" ](_l ) for _l in fp_ .readlines ()
1440
- ]
1416
+ network_c = [__utils__ ["stringutils.to_unicode" ](_l ) for _l in fp_ .readlines ()]
1441
1417
1442
1418
with __utils__ ["files.fopen" ]("/etc/sysconfig/network" , "w" ) as fh_ :
1443
1419
for net in network_c :
@@ -1446,9 +1422,7 @@ def mod_hostname(hostname):
1446
1422
quote_type = __utils__ ["stringutils.is_quoted" ](old_hostname )
1447
1423
fh_ .write (
1448
1424
__utils__ ["stringutils.to_str" ](
1449
- "HOSTNAME={1}{0}{1}\n " .format (
1450
- __utils__ ["stringutils.dequote" ](hostname ), quote_type
1451
- )
1425
+ "HOSTNAME={1}{0}{1}\n " .format (__utils__ ["stringutils.dequote" ](hostname ), quote_type )
1452
1426
)
1453
1427
)
1454
1428
else :
@@ -1459,13 +1433,9 @@ def mod_hostname(hostname):
1459
1433
if __grains__ ["lsb_distrib_id" ] == "nilrt" :
1460
1434
str_hostname = __utils__ ["stringutils.to_str" ](hostname )
1461
1435
nirtcfg_cmd = "/usr/local/natinst/bin/nirtcfg"
1462
- nirtcfg_cmd += " --set section=SystemSettings,token='Host_Name',value='{0}'" .format (
1463
- str_hostname
1464
- )
1436
+ nirtcfg_cmd += " --set section=SystemSettings,token='Host_Name',value='{0}'" .format (str_hostname )
1465
1437
if __mods__ ["cmd.run_all" ](nirtcfg_cmd )["retcode" ] != 0 :
1466
- raise CommandExecutionError (
1467
- "Couldn't set hostname to: {0}\n " .format (str_hostname )
1468
- )
1438
+ raise CommandExecutionError ("Couldn't set hostname to: {0}\n " .format (str_hostname ))
1469
1439
elif __grains__ ["os_family" ] == "OpenBSD" :
1470
1440
with __utils__ ["files.fopen" ]("/etc/myname" , "w" ) as fh_ :
1471
1441
fh_ .write (__utils__ ["stringutils.to_str" ](hostname + "\n " ))
@@ -1475,11 +1445,7 @@ def mod_hostname(hostname):
1475
1445
with __utils__ ["files.fopen" ]("/etc/nodename" , "w" ) as fh_ :
1476
1446
fh_ .write (__utils__ ["stringutils.to_str" ](hostname .split ("." )[0 ] + "\n " ))
1477
1447
with __utils__ ["files.fopen" ]("/etc/defaultdomain" , "w" ) as fh_ :
1478
- fh_ .write (
1479
- __utils__ ["stringutils.to_str" ](
1480
- "." .join (hostname .split ("." )[1 :]) + "\n "
1481
- )
1482
- )
1448
+ fh_ .write (__utils__ ["stringutils.to_str" ]("." .join (hostname .split ("." )[1 :]) + "\n " ))
1483
1449
1484
1450
return True
1485
1451
@@ -1520,9 +1486,7 @@ def connect(host, port=None, **kwargs):
1520
1486
timeout = kwargs .get ("timeout" , 5 )
1521
1487
family = kwargs .get ("family" , None )
1522
1488
1523
- if hubblestack .utils .validate .net .ipv4_addr (host ) or hubblestack .utils .validate .net .ipv6_addr (
1524
- host
1525
- ):
1489
+ if hubblestack .utils .validate .net .ipv4_addr (host ) or hubblestack .utils .validate .net .ipv6_addr (host ):
1526
1490
address = host
1527
1491
else :
1528
1492
address = "{0}" .format (__utils__ ["network.sanitize_host" ](host ))
@@ -1544,14 +1508,10 @@ def connect(host, port=None, **kwargs):
1544
1508
else :
1545
1509
__family = 0
1546
1510
1547
- (family , socktype , _proto , garbage , _address ) = socket .getaddrinfo (
1548
- address , port , __family , 0 , __proto
1549
- )[0 ]
1511
+ (family , socktype , _proto , garbage , _address ) = socket .getaddrinfo (address , port , __family , 0 , __proto )[0 ]
1550
1512
except socket .gaierror :
1551
1513
ret ["result" ] = False
1552
- ret ["comment" ] = "Unable to resolve host {0} on {1} port {2}" .format (
1553
- host , proto , port
1554
- )
1514
+ ret ["comment" ] = "Unable to resolve host {0} on {1} port {2}" .format (host , proto , port )
1555
1515
return ret
1556
1516
1557
1517
try :
@@ -1561,7 +1521,7 @@ def connect(host, port=None, **kwargs):
1561
1521
if proto == "udp" :
1562
1522
# Generate a random string of a
1563
1523
# decent size to test UDP connection
1564
- if isFipsEnabled :
1524
+ if IS_FIPS_ENABLED :
1565
1525
md5h = hashlib .md5 (usedforsecurity = False )
1566
1526
else :
1567
1527
md5h = hashlib .md5 ()
@@ -1575,15 +1535,11 @@ def connect(host, port=None, **kwargs):
1575
1535
skt .shutdown (2 )
1576
1536
except Exception as exc : # pylint: disable=broad-except
1577
1537
ret ["result" ] = False
1578
- ret ["comment" ] = "Unable to connect to {0} ({1}) on {2} port {3}" .format (
1579
- host , _address [0 ], proto , port
1580
- )
1538
+ ret ["comment" ] = "Unable to connect to {0} ({1}) on {2} port {3}" .format (host , _address [0 ], proto , port )
1581
1539
return ret
1582
1540
1583
1541
ret ["result" ] = True
1584
- ret ["comment" ] = "Successfully connected to {0} ({1}) on {2} port {3}" .format (
1585
- host , _address [0 ], proto , port
1586
- )
1542
+ ret ["comment" ] = "Successfully connected to {0} ({1}) on {2} port {3}" .format (host , _address [0 ], proto , port )
1587
1543
return ret
1588
1544
1589
1545
@@ -1694,9 +1650,7 @@ def _mod_bufsize_linux(iface, *args, **kwargs):
1694
1650
if not kwargs :
1695
1651
return ret
1696
1652
if args :
1697
- ret ["comment" ] = "Unknown arguments: " + " " .join (
1698
- [str (item ) for item in args ]
1699
- )
1653
+ ret ["comment" ] = "Unknown arguments: " + " " .join ([str (item ) for item in args ])
1700
1654
return ret
1701
1655
eargs = ""
1702
1656
for kw in ["rx" , "tx" , "rx-mini" , "rx-jumbo" ]:
@@ -1800,13 +1754,17 @@ def default_route(family=None):
1800
1754
if __grains__ ["kernel" ] == "Linux" :
1801
1755
default_route ["inet" ] = ["0.0.0.0" , "default" ]
1802
1756
default_route ["inet6" ] = ["::/0" , "default" ]
1803
- elif __grains__ ["os" ] in [
1804
- "FreeBSD" ,
1805
- "NetBSD" ,
1806
- "OpenBSD" ,
1807
- "MacOS" ,
1808
- "Darwin" ,
1809
- ] or __grains__ ["kernel" ] in ("SunOS" , "AIX" ):
1757
+ elif (
1758
+ __grains__ ["os" ]
1759
+ in [
1760
+ "FreeBSD" ,
1761
+ "NetBSD" ,
1762
+ "OpenBSD" ,
1763
+ "MacOS" ,
1764
+ "Darwin" ,
1765
+ ]
1766
+ or __grains__ ["kernel" ] in ("SunOS" , "AIX" )
1767
+ ):
1810
1768
default_route ["inet" ] = ["default" ]
1811
1769
default_route ["inet6" ] = ["default" ]
1812
1770
else :
@@ -1820,10 +1778,7 @@ def default_route(family=None):
1820
1778
continue
1821
1779
ret .append (route )
1822
1780
else :
1823
- if (
1824
- route ["destination" ] in default_route ["inet" ]
1825
- or route ["destination" ] in default_route ["inet6" ]
1826
- ):
1781
+ if route ["destination" ] in default_route ["inet" ] or route ["destination" ] in default_route ["inet6" ]:
1827
1782
ret .append (route )
1828
1783
1829
1784
return ret
@@ -2015,9 +1970,7 @@ def ip_networks(interface=None, include_loopback=False, verbose=False):
2015
1970
salt '*' network.list_networks interface=docker0,enp*
2016
1971
salt '*' network.list_networks interface=eth*
2017
1972
"""
2018
- return __utils__ ["network.ip_networks" ](
2019
- interface = interface , include_loopback = include_loopback , verbose = verbose
2020
- )
1973
+ return __utils__ ["network.ip_networks" ](interface = interface , include_loopback = include_loopback , verbose = verbose )
2021
1974
2022
1975
2023
1976
def ip_networks6 (interface = None , include_loopback = False , verbose = False ):
@@ -2040,9 +1993,7 @@ def ip_networks6(interface=None, include_loopback=False, verbose=False):
2040
1993
salt '*' network.list_networks6 interface=docker0,enp*
2041
1994
salt '*' network.list_networks6 interface=eth*
2042
1995
"""
2043
- return __utils__ ["network.ip_networks6" ](
2044
- interface = interface , include_loopback = include_loopback , verbose = verbose
2045
- )
1996
+ return __utils__ ["network.ip_networks6" ](interface = interface , include_loopback = include_loopback , verbose = verbose )
2046
1997
2047
1998
2048
1999
def fqdns ():
@@ -2054,8 +2005,8 @@ def fqdns():
2054
2005
# fqdns
2055
2006
2056
2007
# Possible value for h_errno defined in netdb.h
2057
- HOST_NOT_FOUND = 1
2058
- NO_DATA = 4
2008
+ HOST_NOT_FOUND = 1 ## pylint: disable=invalid-name
2009
+ NO_DATA = 4 ## pylint: disable=invalid-name
2059
2010
2060
2011
fqdns = set ()
2061
2012
0 commit comments