Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Apr 2, 2015
2 parents 0a4eb38 + 5f747c8 commit ec22f0d
Show file tree
Hide file tree
Showing 30 changed files with 2,051 additions and 419 deletions.
119 changes: 67 additions & 52 deletions IM/ConfManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ class ConfManager(threading.Thread):
""" The file with the ansible steps to configure the second step of the the master node """
THREAD_SLEEP_DELAY = 5

def __init__(self, inf, auth):
def __init__(self, inf, auth, max_ctxt_time = 1e9):
threading.Thread.__init__(self)
self.inf = inf
self.auth = auth
self.init_time = time.time()
self.max_ctxt_time = max_ctxt_time
self._stop = False

def check_running_pids(self, vms_configuring):
Expand Down Expand Up @@ -104,24 +106,26 @@ def check_vm_ips(self, timeout = Config.WAIT_RUNNING_VM_TIMEOUT):
while not success and wait < timeout:
success = True
for vm in self.inf.get_vm_list():
ip = vm.getPublicIP()
if not ip:
if vm.hasPublicNet():
ip = vm.getPublicIP()
else:
ip = vm.getPrivateIP()

if not ip:
# If the IP is not Available try to update the info
vm.update_status(self.auth)

ip = vm.getPublicIP()
if not ip:
if vm.hasPublicNet():
ip = vm.getPublicIP()
else:
ip = vm.getPrivateIP()

if not ip:
success = False
break

if not success:
ConfManager.logger.warn("Inf ID: " + str(self.inf.id) + ": Error waiting all the VMs to have an IP")
ConfManager.logger.warn("Inf ID: " + str(self.inf.id) + ": Error waiting all the VMs to have a correct IP")
wait += self.THREAD_SLEEP_DELAY
time.sleep(self.THREAD_SLEEP_DELAY)
else:
Expand All @@ -136,6 +140,10 @@ def run(self):
vms_configuring = {}

while not self._stop:
if self.init_time + self.max_ctxt_time < time.time():
ConfManager.logger.debug("Inf ID: " + str(self.inf.id) + ": Max contextualization time passed. Exit thread.")
return

vms_configuring = self.check_running_pids(vms_configuring)

# If the queue is empty but there are vms configuring wait and test again
Expand Down Expand Up @@ -268,12 +276,13 @@ def generate_inventory(self, tmp_dir):
ifaces_im_vars = ''
for i in range(vm.getNumNetworkIfaces()):
iface_ip = vm.getIfaceIP(i)
ifaces_im_vars += ' IM_NODE_NET_' + str(i) + '_IP=' + iface_ip
if vm.getRequestedNameIface(i):
(nodename, nodedom) = vm.getRequestedNameIface(i, default_domain = Config.DEFAULT_DOMAIN)
ifaces_im_vars += ' IM_NODE_NET_' + str(i) + '_HOSTNAME=' + nodename
ifaces_im_vars += ' IM_NODE_NET_' + str(i) + '_DOMAIN=' + nodedom
ifaces_im_vars += ' IM_NODE_NET_' + str(i) + '_FQDN=' + nodename + "." + nodedom
if iface_ip:
ifaces_im_vars += ' IM_NODE_NET_' + str(i) + '_IP=' + iface_ip
if vm.getRequestedNameIface(i):
(nodename, nodedom) = vm.getRequestedNameIface(i, default_domain = Config.DEFAULT_DOMAIN)
ifaces_im_vars += ' IM_NODE_NET_' + str(i) + '_HOSTNAME=' + nodename
ifaces_im_vars += ' IM_NODE_NET_' + str(i) + '_DOMAIN=' + nodedom
ifaces_im_vars += ' IM_NODE_NET_' + str(i) + '_FQDN=' + nodename + "." + nodedom

# first try to use the public IP
ip = vm.getPublicIP()
Expand Down Expand Up @@ -333,8 +342,11 @@ def generate_etc_hosts(self, tmp_dir):
for vm in vm_group[group]:
for i in range(vm.getNumNetworkIfaces()):
if vm.getRequestedNameIface(i):
(nodename, nodedom) = vm.getRequestedNameIface(i, default_domain = Config.DEFAULT_DOMAIN)
hosts_out.write(vm.getIfaceIP(i) + " " + nodename + "." + nodedom + " " + nodename + "\r\n")
if vm.getIfaceIP(i):
(nodename, nodedom) = vm.getRequestedNameIface(i, default_domain = Config.DEFAULT_DOMAIN)
hosts_out.write(vm.getIfaceIP(i) + " " + nodename + "." + nodedom + " " + nodename + "\r\n")
else:
ConfManager.logger.warn("Inf ID: " + str(self.inf.id) + ": Net interface " + str(i) + " request a name, but it does not have an IP.")

# first try to use the public IP
ip = vm.getPublicIP()
Expand Down Expand Up @@ -488,8 +500,8 @@ def configure_master(self):

def wait_master(self):
"""
- selecciona la VM master
- espera a que arranque y este accesible por SSH
- Select the master VM
- Wait it to boot and has the SSH port open
"""
# First assure that ansible is installed in the master
if not self.inf.vm_master or self.inf.vm_master.destroy:
Expand Down Expand Up @@ -547,6 +559,7 @@ def wait_master(self):

self.inf.set_configured(True)
except:
ConfManager.logger.exception("Inf ID: " + str(self.inf.id) + ": Error waiting the master VM to be running")
self.inf.set_configured(False)
else:
self.inf.set_configured(True)
Expand Down Expand Up @@ -748,16 +761,11 @@ def wait_vm_ssh_acccess(self, vm, timeout):
wait += delay
time.sleep(delay)
else:
ip = vm.getPrivateIP()
if ip != None:
ConfManager.logger.debug("Inf ID: " + str(self.inf.id) + ": " + 'VM ' + str(vm.id) + ' with private IP: ' + ip)
return False
else:
ConfManager.logger.debug("Inf ID: " + str(self.inf.id) + ": " + 'VM ' + str(vm.id) + ' with no IP')
# Update the VM info and wait to have a valid IP
wait += delay
time.sleep(delay)
vm.update_status(self.auth)
ConfManager.logger.debug("Inf ID: " + str(self.inf.id) + ": " + 'VM ' + str(vm.id) + ' with no IP')
# Update the VM info and wait to have a valid public IP
wait += delay
time.sleep(delay)
vm.update_status(self.auth)

# Timeout, return False
return False
Expand All @@ -769,34 +777,41 @@ def change_master_credentials(self, ssh):
Arguments:
- ssh(:py:class:`IM.SSH`): Object with the authentication data to access the master VM.
"""
creds = self.inf.vm_master.getCredentialValues()
(user, _, _, _) = creds
new_creds = self.inf.vm_master.getCredentialValues(new=True)
if len(list(set(new_creds))) > 1 or list(set(new_creds))[0] != None:
change_creds = False
if cmp(new_creds,creds) != 0:
(_, new_passwd, new_public_key, new_private_key) = new_creds
if new_passwd:
ConfManager.logger.info("Changing password to master VM")
(out, err, code) = ssh.execute('sudo bash -c \'echo "' + user + ':' + new_passwd + '" | /usr/sbin/chpasswd && echo "OK"\' 2> /dev/null')

if code == 0:
change_creds = True
ssh.password = new_passwd
else:
ConfManager.logger.error("Error changing password to master VM. " + out + err)
change_creds = False
try:
creds = self.inf.vm_master.getCredentialValues()
(user, passwd, _, _) = creds
new_creds = self.inf.vm_master.getCredentialValues(new=True)
if len(list(set(new_creds))) > 1 or list(set(new_creds))[0] != None:
change_creds = False
if cmp(new_creds,creds) != 0:
(_, new_passwd, new_public_key, new_private_key) = new_creds
# only change to the new password if there are a previous passwd value
if passwd and new_passwd:
ConfManager.logger.info("Changing password to master VM")
(out, err, code) = ssh.execute('sudo bash -c \'echo "' + user + ':' + new_passwd + '" | /usr/sbin/chpasswd && echo "OK"\' 2> /dev/null')

if code == 0:
change_creds = True
ssh.password = new_passwd
else:
ConfManager.logger.error("Error changing password to master VM. " + out + err)

if new_public_key and new_private_key:
ConfManager.logger.info("Changing public key to master VM")
(out, err, code) = ssh.execute('echo ' + new_public_key + ' >> .ssh/authorized_keys')
if code != 0:
ConfManager.logger.error("Error changing public key to master VM. " + out + err)
else:
change_creds = True
ssh.private_key = new_private_key

if new_public_key and new_private_key:
ConfManager.logger.info("Changing public key to master VM")
(out, err, code) = ssh.execute('echo ' + new_public_key + ' >> .ssh/authorized_keys')
if code != 0:
ConfManager.logger.error("Error changing public key to master VM. " + out + err)
else:
change_creds = True
ssh.private_key = new_private_key
if change_creds:
self.inf.vm_master.info.systems[0].updateNewCredentialValues()
except:
ConfManager.logger.exception("Error changing credentials to master VM.")

if change_creds:
self.inf.vm_master.info.systems[0].updateNewCredentialValues()
return change_creds

def call_ansible(self, tmp_dir, inventory, playbook, ssh):
"""
Expand Down
14 changes: 11 additions & 3 deletions IM/InfrastructureInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ def get_vm(self, str_vm_id):
"""
Get the VM with the specified ID (if it is not destroyed)
"""

vm_id = int(str_vm_id)
try:
vm_id = int(str_vm_id)
except:
raise IncorrectVMException()
if vm_id >= 0 and vm_id < len(self.vm_list):
vm = self.vm_list[vm_id]
if not vm.destroy:
Expand Down Expand Up @@ -347,6 +349,10 @@ def Contextualize(self, auth):
# get the contextualize steps specified in the RADL, or use the default value
contextualizes = self.radl.contextualize.get_contextualize_items_by_step({1:ctxts})

max_ctxt_time = self.radl.contextualize.max_time
if not max_ctxt_time:
max_ctxt_time = Config.MAX_CONTEXTUALIZATION_TIME

ctxt_task = []
ctxt_task.append((-2,0,self,['wait_master', 'check_vm_ips']))
ctxt_task.append((-1,0,self,['configure_master', 'generate_playbooks_and_hosts']))
Expand All @@ -372,5 +378,7 @@ def Contextualize(self, auth):
self.add_ctxt_tasks(ctxt_task)

if self.cm is None or not self.cm.isAlive():
self.cm = ConfManager.ConfManager(self,auth)
self.cm = ConfManager.ConfManager(self,auth,max_ctxt_time)
self.cm.start()
else:
self.cm.init_time = time.time()
Loading

0 comments on commit ec22f0d

Please sign in to comment.