Commit 67e6b1c4 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos.format: Get and upload public ipv4.

parent d62fa8e3
......@@ -49,6 +49,7 @@ import threading
import time
import traceback
import zipfile
from urllib2 import urlopen
import lxml.etree
import xml_marshaller.xml_marshaller
......@@ -124,6 +125,23 @@ class AddressGenerationError(Exception):
)
def getPublicIPv4Address():
test_list = [
{ "url": 'https://api.ipify.org/?format=json' , "json_key": "ip"},
{ "url": 'http://httpbin.org/ip', "json_key": "origin"},
{ "url": 'http://jsonip.com', "json_key": "ip"}]
previous = None
ipv4 = None
for test in test_list:
if ipv4 is not None:
previous = ipv4
try:
ipv4 = json.load(urlopen(test["url"]))[test["json_key"]]
except:
ipv4 = None
if ipv4 is not None and ipv4 == previous:
return ipv4
def callAndRead(argument_list, raise_on_error=True):
popen = subprocess.Popen(argument_list,
stdout=subprocess.PIPE,
......@@ -237,6 +255,7 @@ class Computer(object):
self.ipv6_interface = ipv6_interface
self.software_user = software_user
self.tap_gateway_interface = tap_gateway_interface
self.public_ipv4_address = None
def __getinitargs__(self):
return (self.reference, self.interface)
......@@ -273,11 +292,16 @@ class Computer(object):
# Can't find address
raise NoAddressOnInterface('No valid IPv6 found on %s.' % self.interface.name)
def update(self):
"""
Collect environmental hardware/network information.
"""
self.public_ipv4_address = getPublicIPv4Address()
def send(self, conf):
"""
Send a marshalled dictionary of the computer object serialized via_getDict.
"""
slap_instance = slap.slap()
connection_dict = {}
if conf.key_file and conf.cert_file:
......@@ -1227,6 +1251,7 @@ def do_format(conf):
if getattr(conf, 'certificate_repository_path', None):
mkdir_p(conf.certificate_repository_path, mode=0o700)
computer.update()
# Dumping and sending to the erp5 the current configuration
if not conf.dry_run:
computer.dump(path_to_xml=conf.computer_xml,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment