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