Commit 5daeffd1 authored by Rafael Monnerat's avatar Rafael Monnerat

re6stnet-registry: Let parameters be upgraded by a cron, and avoid to bang

 Bang on every update is not reliable.

 Now the script on cron contacts re6st registry to collect informations and
 the script itself parameters of the slaves. This will reduce the load in
 and speed up the update of the slave on slapos master.
parent 3b9c0e5d
......@@ -204,13 +204,12 @@ class Recipe(GenericBaseRecipe):
service_dict = dict(token_base_path=token_list_path,
token_json=token_save_path,
db=self.options['db-path'],
partition_id=self.computer_partition_id,
computer_id=self.computer_id,
registry_url=registry_url)
service_dict['server_url'] = self.server_url
service_dict['cert_file'] = self.cert_file
service_dict['key_file'] = self.key_file
registry_url=registry_url
server_url=self.server_url,
cert_file=self.cert_file,
key_file=self.key_file)
request_add = self.createPythonScript(
self.options['manager-wrapper'].strip(),
......@@ -218,37 +217,5 @@ class Recipe(GenericBaseRecipe):
)
path_list.append(request_add)
# Send connection parameters of slave instances
if token_dict:
self.slap.initializeConnection(self.server_url, self.key_file,
self.cert_file)
computer_partition = self.slap.registerComputerPartition(
self.computer_id,
self.computer_partition_id)
for slave_reference, token in token_dict.iteritems():
try:
status_file = os.path.join(token_list_path, '%s.status' % slave_reference)
status = self.readFile(status_file) or 'New token requested'
msg = status
if status == 'TOKEN_ADDED':
msg = 'Token is ready for use'
elif status == 'TOKEN_USED':
msg = 'Token not available, it has been used to generate re6stnet certificate.'
ipv6_file = os.path.join(token_list_path, '%s.ipv6' % slave_reference)
ipv6 = self.readFile(ipv6_file) or '::'
ipv4_file = os.path.join(token_list_path, '%s.ipv4' % slave_reference)
node_ipv4 = self.readFile(ipv4_file) or '0.0.0.0'
computer_partition.setConnectionDict(
{'token':token, '1_info':msg, 'ipv6': ipv6, 'ipv4': node_ipv4},
slave_reference)
except Exception:
self.logger.fatal("Error while sending slave %s informations: %s",
slave_reference, traceback.format_exc())
return path_list
......@@ -9,7 +9,6 @@ import traceback
import logging
from re6st import registry
log = logging.getLogger('SLAPOS-RE6STNET')
logging.basicConfig(level=logging.INFO)
......@@ -40,24 +39,21 @@ def updateFile(file_path, value):
return True
return False
def bang(args):
computer_guid = args['computer_id']
partition_id = args['partition_id']
def getComputerPartition(server_url, key_file, cert_file, computer_guid, partition_id):
slap = slapos.slap.slap()
# Redeploy instance to update published information
slap.initializeConnection(args['server_url'], args['key_file'],
args['cert_file'])
partition = slap.registerComputerPartition(computer_guid=computer_guid,
slap.initializeConnection(server_url,
key_file,
cert_file)
return slap.registerComputerPartition(computer_guid=computer_guid,
partition_id=partition_id)
partition.bang(message='Published parameters changed!')
log.info("Bang with message 'parameters changed'...")
def requestAddToken(client, base_token_path):
time.sleep(3)
path_list = [x for x in os.listdir(base_token_path) if x.endswith('.add')]
updated = False
log.info("Searching tokens to add at %s and found %s." % (base_token_path, path_list))
if not path_list:
......@@ -79,19 +75,16 @@ def requestAddToken(client, base_token_path):
traceback.format_exc()))
continue
if result and result == token:
if result in (token, None):
# update information
log.info("New token added for slave instance %s. Updating file status..." %
reference)
status_file = os.path.join(base_token_path, '%s.status' % reference)
updateFile(status_file, 'TOKEN_ADDED')
os.unlink(request_file)
updated = True
else:
log.debug('Bad token. Request add token fail for %s...' % request_file)
return updated
def requestRemoveToken(client, base_token_path):
path_list = [x for x in os.listdir(base_token_path) if x.endswith('.remove')]
......@@ -150,7 +143,7 @@ def requestRevoqueCertificate(args):
log.info("Failed to revoke email for %s" % reference)
def checkService(client, base_token_path, token_json):
def checkService(client, base_token_path, token_json, computer_partition):
token_dict = loadJsonFile(token_json)
updated = False
if not token_dict:
......@@ -167,64 +160,77 @@ def checkService(client, base_token_path, token_json):
if not client.isToken(str(token)):
# Token is used to register client
updated = True
updateFile(status_file, 'TOKEN_USED')
log.info("Token status of %s updated to 'used'." % slave_reference)
msg = readFile(status_file)
log.info("Token %s has %s State." % (status_file, msg))
status = readFile(status_file)
log.info("Token %s has %s State." % (status_file, status))
ipv6 = "::"
ipv4 = "0.0.0.0"
msg = status
if status == 'TOKEN_ADDED':
msg = 'Token is ready for use'
elif status == 'TOKEN_USED':
msg = 'Token not available, it has been used to generate re6stnet certificate.'
if msg == 'TOKEN_USED':
try:
log.info("Dumping ipv6...")
email = '%s@slapos' % slave_reference.lower()
if status == 'TOKEN_USED':
try:
ipv6 = client.getIPv6Address(str(email))
ipv6_file = os.path.join(base_token_path, '%s.ipv6' % slave_reference)
ipv6_changed = updateFile(ipv6_file, ipv6)
except Exception:
log.info('Error for dump ipv6 for %s... \n %s' % (slave_reference,
traceback.format_exc()))
continue
log.info("%s, IPV6 = %s" % (slave_reference, ipv6))
log.info("Dumping ipv4...")
try:
ipv4 = client.getIPv4Information(str(email)) or "0.0.0.0"
ipv4_file = os.path.join(base_token_path, '%s.ipv4' % slave_reference)
ipv4_changed = updateFile(ipv4_file, ipv4)
except Exception:
log.info('Error for dump ipv4 for %s... \n %s' % (slave_reference,
traceback.format_exc()))
continue
log.info("%s, IPV4 = %s" % (slave_reference, ipv4))
except IOError:
log.debug('Error when writing in file %s. Could not update status of %s...' %
(status_file, slave_reference))
if not updated or ipv4_changed or ipv6_changed:
updated = True
try:
log.info("Update parameters for %s" % slave_reference)
# Normalise the values as simple strings to be on the same format that
# the values which come from master.
computer_partition.setConnectionDict({'token': str(token),
'1_info': str(msg),
'ipv6': str(ipv6),
'ipv4': str(ipv4)},
slave_reference)
except Exception:
log.fatal("Error while sending slave %s informations: %s",
slave_reference, traceback.format_exc())
return updated
def manage(args, can_bang=True):
computer_guid = args['computer_id']
partition_id = args['partition_id']
server_url = args['server_url']
key_file = args['key_file']
cert_file = args['cert_file']
client = registry.RegistryClient(args['registry_url'])
base_token_path = args['token_base_path']
token_json = args['token_json']
log.info("ADD TOKEN")
# Request Add new tokens
has_new_token = requestAddToken(client, base_token_path)
requestAddToken(client, base_token_path)
log.info("Remove TOKEN")
# Request delete removed token
requestRemoveToken(client, base_token_path)
# check status of all token
changed = checkService(client, base_token_path, token_json)
if (has_new_token or changed) and can_bang:
bang(args)
computer_partition = getComputerPartition(server_url, key_file,
cert_file, computer_guid, partition_id)
log.info("Update Services")
# check status of all token
checkService(client, base_token_path,
token_json, computer_partition)
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