0
我需要一個Ruby功能,可以在100%的時間內告訴我這臺機器是否爲EC2實例,即使DNS在我們的EC2實例上發生故障時也是如此。如何判斷我的機器是否爲EC2實例?
,我們使用的功能是:
def is_ec2?
require 'socket'
Socket::gethostbyname('instance-data.ec2.internal.')
true
rescue
false
end
除了當DNS爆發,每一個EC2機認爲這不是一個EC2的機器,壞的事情發生了,像生產機器刪除其自己的SSL證書,並與當地JIANGSU作者:孫軍框的證書替換他們..
在Python中,我們使用:
@memoized
def is_ec2():
# This is a 99% check to avoid the need to wait for the timeout.
# Our VM's have this file. Our dev VM's don't.
if not os.path.isfile('/sys/hypervisor/uuid'):
return False
try:
result = boto.utils.get_instance_metadata()
if result == {}:
return False
return True
except Exception:
return False
除了使用wget -q -O - http://169.254.169.254/latest/meta-data
而不是boto.utils.get_instance_metadata()
,那會起作用嗎?
爲什麼不檢查亞馬遜池中的IP? – taro
不起作用。我們不控制我們的本地網絡,並且我們很少在我們本地盒子的亞馬遜IP空間內。好主意,但。 –