Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/build/lib/airflow/utils/net.py: 36%

22 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

1# 

2# Licensed to the Apache Software Foundation (ASF) under one 

3# or more contributor license agreements. See the NOTICE file 

4# distributed with this work for additional information 

5# regarding copyright ownership. The ASF licenses this file 

6# to you under the Apache License, Version 2.0 (the 

7# "License"); you may not use this file except in compliance 

8# with the License. You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, 

13# software distributed under the License is distributed on an 

14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 

15# KIND, either express or implied. See the License for the 

16# specific language governing permissions and limitations 

17# under the License. 

18from __future__ import annotations 

19 

20import socket 

21from functools import lru_cache 

22 

23from airflow.configuration import conf 

24 

25 

26# patched version of socket.getfqdn() - see https://github.com/python/cpython/issues/49254 

27@lru_cache(maxsize=None) 

28def getfqdn(name=""): 

29 """Get fully qualified domain name from name. 

30 An empty argument is interpreted as meaning the local host. 

31 """ 

32 name = name.strip() 

33 if not name or name == "0.0.0.0": 

34 name = socket.gethostname() 

35 try: 

36 addrs = socket.getaddrinfo(name, None, 0, socket.SOCK_DGRAM, 0, socket.AI_CANONNAME) 

37 except OSError: 

38 pass 

39 else: 

40 for addr in addrs: 

41 if addr[3]: 

42 name = addr[3] 

43 break 

44 return name 

45 

46 

47def get_host_ip_address(): 

48 """Fetch host ip address.""" 

49 return socket.gethostbyname(getfqdn()) 

50 

51 

52def get_hostname(): 

53 """ 

54 Fetch the hostname using the callable from the config or using 

55 `airflow.utils.net.getfqdn` as a fallback. 

56 """ 

57 return conf.getimport("core", "hostname_callable", fallback="airflow.utils.net.getfqdn")()