Skip to content

Commit

Permalink
Make IM python 3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Jan 4, 2017
1 parent e639ab4 commit 1637d8c
Show file tree
Hide file tree
Showing 50 changed files with 451 additions and 307 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ test/auth.dat
.coverage
dist
cover
*.pem
*.cer
inf.dat
radl
2 changes: 1 addition & 1 deletion IM/CloudInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def getCloudConnector(self):
try:
module = __import__('IM.connectors.' + self.type, fromlist=[self.type + "CloudConnector"])
return getattr(module, self.type + "CloudConnector")(self)
except Exception, ex:
except Exception as ex:
raise Exception("Cloud provider not supported: %s (error: %s)" % (self.type, str(ex)))

def __str__(self):
Expand Down
39 changes: 27 additions & 12 deletions IM/ConfManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
import shutil
import json
import copy
from StringIO import StringIO

try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from multiprocessing import Queue

from ansible.parsing.vault import VaultEditor

from IM.ansible_utils.ansible_launcher import AnsibleThread

import InfrastructureManager
import IM.InfrastructureManager
from IM.VirtualMachine import VirtualMachine
from IM.SSH import AuthenticationException
from IM.SSHRetry import SSHRetry
Expand Down Expand Up @@ -68,7 +72,7 @@ def check_running_pids(self, vms_configuring):
Update the status of the configuration processes
"""
res = {}
for step, vm_list in vms_configuring.iteritems():
for step, vm_list in vms_configuring.items():
for vm in vm_list:
if isinstance(vm, VirtualMachine):
# Update the info of the VM to check it is in a correct
Expand Down Expand Up @@ -113,7 +117,7 @@ def check_running_pids(self, vms_configuring):
def stop(self):
self._stop = True
# put a task to assure to wake up the thread
self.inf.ctxt_tasks.put((-10, 0, None, None))
self.inf.add_ctxt_tasks([(-10, 0, None, None)])
ConfManager.logger.debug(
"Inf ID: " + str(self.inf.id) + ": Stop Configuration thread.")
if self.ansible_process and self.ansible_process.is_alive():
Expand Down Expand Up @@ -803,7 +807,7 @@ def configure_master(self):

success = configured_ok

except Exception, ex:
except Exception as ex:
ConfManager.logger.exception(
"Inf ID: " + str(self.inf.id) + ": Error in the ansible installation process")
self.inf.add_cont_msg(
Expand Down Expand Up @@ -990,7 +994,7 @@ def generate_playbooks_and_hosts(self):
ssh.sftp_put_files(recipe_files)

self.inf.set_configured(True)
except Exception, ex:
except Exception as ex:
self.inf.set_configured(False)
ConfManager.logger.exception(
"Inf ID: " + str(self.inf.id) + ": Error generating playbooks.")
Expand All @@ -1004,7 +1008,7 @@ def relaunch_vm(self, vm, failed_cloud=False):
Remove and launch again the specified VM
"""
try:
removed = InfrastructureManager.InfrastructureManager.RemoveResource(
removed = IM.InfrastructureManager.InfrastructureManager.RemoveResource(
self.inf.id, vm.im_id, self.auth)
except:
ConfManager.logger.exception(
Expand All @@ -1025,7 +1029,7 @@ def relaunch_vm(self, vm, failed_cloud=False):
failed_clouds = []
if failed_cloud:
failed_clouds = [vm.cloud]
InfrastructureManager.InfrastructureManager.AddResource(
IM.InfrastructureManager.InfrastructureManager.AddResource(
self.inf.id, new_radl, self.auth, False, failed_clouds)

def wait_vm_running(self, vm, timeout, relaunch=False):
Expand Down Expand Up @@ -1161,6 +1165,17 @@ def wait_vm_ssh_acccess(self, vm, timeout):
# Timeout, return False
return False, "Timeout waiting SSH access."

@staticmethod
def cmp_credentials(creds, other_creds):
if len(creds) != len(other_creds):
return 1

for i in range(len(creds)):
if creds[i] != other_creds[i]:
return 1

return 0

def change_master_credentials(self, ssh):
"""
Chech the RADL of the VM master to see if we must change the user credentials
Expand All @@ -1175,7 +1190,7 @@ def change_master_credentials(self, ssh):
new_creds = self.inf.vm_master.getCredentialValues(new=True)
if len(list(set(new_creds))) > 1 or list(set(new_creds))[0] is not None:
change_creds = False
if cmp(new_creds, creds) != 0:
if self.cmp_credentials(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
Expand Down Expand Up @@ -1234,7 +1249,7 @@ def call_ansible(self, tmp_dir, inventory, playbook, ssh):
pk_out = open(gen_pk_file, 'w')
pk_out.write(ssh.private_key)
pk_out.close()
os.chmod(gen_pk_file, 0400)
os.chmod(gen_pk_file, 0o400)
else:
gen_pk_file = None

Expand Down Expand Up @@ -1414,7 +1429,7 @@ def configure_ansible(self, ssh, tmp_dir):
ConfManager.logger.debug("Inf ID: " + str(self.inf.id) +
": Ansible successfully configured in the master VM:\n" + msg + "\n\n")
self.inf.add_cont_msg("Ansible successfully configured in the master VM.")
except Exception, ex:
except Exception as ex:
ConfManager.logger.exception(
"Inf ID: " + str(self.inf.id) + ": Error configuring master node.")
self.inf.add_cont_msg("Error configuring master node: " + str(ex))
Expand Down Expand Up @@ -1456,7 +1471,7 @@ def create_general_conf_file(self, conf_file, vm_list):
_, vm_conf_data['private_key']) = creds
# If there are new creds to set to the VM
if len(list(set(new_creds))) > 1 or list(set(new_creds))[0] is not None:
if cmp(new_creds, creds) != 0:
if self.cmp_credentials(new_creds, creds) != 0:
(_, vm_conf_data['new_passwd'], vm_conf_data[
'new_public_key'], vm_conf_data['new_private_key']) = new_creds

Expand Down
21 changes: 14 additions & 7 deletions IM/InfrastructureInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
from uuid import uuid1
import json

from ganglia import ganglia_info
import ConfManager
from IM.ganglia import ganglia_info
import IM.ConfManager
from datetime import datetime
from radl.radl import RADL, Feature, deploy, system, contextualize_item
from radl.radl_json import parse_radl as parse_radl_json, dump_radl as dump_radl_json
from config import Config
from Queue import PriorityQueue
from IM.config import Config
try:
from Queue import PriorityQueue
except ImportError:
from queue import PriorityQueue
from IM.VirtualMachine import VirtualMachine
from IM.auth import Authentication

Expand Down Expand Up @@ -180,7 +183,11 @@ def add_cont_msg(self, msg):
"""
Add a line to the contextualization message
"""
self.cont_out += str(datetime.now()) + ": " + str(msg.decode('utf8', 'ignore')) + "\n"
try:
str_msg = str(msg.decode('utf8', 'ignore'))
except:
str_msg = msg
self.cont_out += str(datetime.now()) + ": " + str_msg + "\n"

def get_vm_list(self):
"""
Expand Down Expand Up @@ -359,7 +366,7 @@ def update_ganglia_info(self):
if now - self.last_ganglia_update > Config.GANGLIA_INFO_UPDATE_FREQUENCY:
try:
(success, msg) = ganglia_info.update_ganglia_info(self)
except Exception, ex:
except Exception as ex:
success = False
msg = str(ex)
else:
Expand Down Expand Up @@ -505,7 +512,7 @@ def Contextualize(self, auth, vm_list=None):
self.add_ctxt_tasks(ctxt_task)

if self.cm is None or not self.cm.isAlive():
self.cm = ConfManager.ConfManager(self, auth, max_ctxt_time)
self.cm = IM.ConfManager.ConfManager(self, auth, max_ctxt_time)
self.cm.start()
else:
# update the ConfManager reference to the inf object
Expand Down
4 changes: 2 additions & 2 deletions IM/InfrastructureList.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def load_data():
try:
inf_list = InfrastructureList._get_data_from_db(Config.DATA_DB)
InfrastructureList.infrastructure_list = inf_list
except Exception, ex:
except Exception as ex:
InfrastructureList.logger.exception("ERROR loading data. Correct or delete it!!")
sys.stderr.write("ERROR loading data: " + str(ex) + ".\nCorrect or delete it!! ")
sys.exit(-1)
Expand All @@ -114,7 +114,7 @@ def save_data(inf_id=None):
if not res:
InfrastructureList.logger.error("ERROR saving data.\nChanges not stored!!")
sys.stderr.write("ERROR saving data.\nChanges not stored!!")
except Exception, ex:
except Exception as ex:
InfrastructureList.logger.exception("ERROR saving data. Changes not stored!!")
sys.stderr.write("ERROR saving data: " + str(ex) + ".\nChanges not stored!!")

Expand Down
20 changes: 12 additions & 8 deletions IM/InfrastructureManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,7 @@ def AddResource(inf_id, radl_data, auth, context=True, failed_clouds=[]):
# NOTE: consider fake deploys (vm_number == 0)
deploys_group_cloud_list = {}
for deploy_group in deploy_groups:
suggested_cloud_ids = list(
set([d.cloud_id for d in deploy_group if d.cloud_id]))
suggested_cloud_ids = list(set([d.cloud_id for d in deploy_group if d.cloud_id]))
if len(suggested_cloud_ids) > 1:
raise Exception("Two deployments that have to be launched in the same cloud provider "
"are asked to be deployed in different cloud providers: %s" % deploy_group)
Expand All @@ -493,11 +492,16 @@ def AddResource(inf_id, radl_data, auth, context=True, failed_clouds=[]):
(suggested_cloud_ids[0], cloud_list[suggested_cloud_ids[0]])]
else:
cloud_list0 = cloud_list.items()
if d.vm_number:
scored_clouds = [(cloud_id, sum([d.vm_number * concrete_systems[cloud_id][d.id][1]
for d in deploy_group])) for cloud_id, _ in cloud_list0]
else:
scored_clouds = [(cloud_id, 1) for cloud_id, _ in cloud_list0]

scored_clouds = []
for cloud_id, _ in cloud_list0:
total = 0
for d in deploy_group:
if d.vm_number:
total += d.vm_number * concrete_systems[cloud_id][d.id][1]
else:
total += 1
scored_clouds.append((cloud_id, total))

ordered_cloud_list = [c.id for c in CloudInfo.get_cloud_list(auth)]
# reverse the list to use the reverse order in the sort function
Expand Down Expand Up @@ -559,7 +563,7 @@ def AddResource(inf_id, radl_data, auth, context=True, failed_clouds=[]):
if passwd and not new_passwd:
# The VM uses the VMI password, set to change it
random_password = ''.join(random.choice(
string.letters + string.digits) for _ in range(8))
string.ascii_letters + string.digits) for _ in range(8))
vm.info.systems[0].setCredentialValues(
password=random_password, new=True)

Expand Down
Loading

0 comments on commit 1637d8c

Please sign in to comment.