def _update_platform_settings(settings): #platform_type = os.path.basename(schema_namespace) platform_type = settings['platform_type'] settings['type'] = platform_type if platform_type == 'nectar' or platform_type == 'csrack': settings['username'] = '******' #fixme avoid hardcoding settings['private_key_name'] = settings['private_key'] settings['private_key'] = os.path.join(storage.get_bdp_root_path(), settings['private_key_path']) settings['root_path'] = '/home/ec2-user' # fixme avoid hardcoding settings['scheme'] = 'ssh' elif platform_type == 'nci': settings['private_key'] = os.path.join(storage.get_bdp_root_path(), settings['private_key_path']) settings['host'] = settings['ip_address'] settings['scheme'] = 'ssh' elif platform_type == 'unix': settings['private_key'] = os.path.join(storage.get_bdp_root_path(), settings['private_key_path']) settings['host'] = settings['ip_address'] settings['scheme'] = 'ssh' elif platform_type == 'mytardis': settings['mytardis_host'] = settings['ip_address'] settings['mytardis_user'] = settings['username'] settings['mytardis_password'] = settings['password']
def generate_cloud_key(parameters): logger.debug('generating key') key_generated = True message = 'Key generated successfully' bdp_root_path = storage.get_bdp_root_path() key_name = parameters['private_key'] key_absolute_path = os.path.join(bdp_root_path, parameters['private_key_path']) key_dir = os.path.dirname(key_absolute_path) if not os.path.exists(key_dir): os.makedirs(key_dir) try: platform_type = parameters['platform_type'] logger.debug('platform_type=%s' % platform_type) parameters['key_dir'] = key_dir if platform_type in ['csrack', 'nectar', 'amazon']: create_key_pair(parameters) create_ssh_security_group(parameters) else: return False, 'Unknown cloud platform' except EC2ResponseError as e: if 'Unauthorized' in e.error_code: key_generated = False message = 'Unauthorized access to %s' % platform_type else: key_generated = False message = e.error_code except Exception as e: key_generated = False message = e return key_generated, message
def _remote_path_exists(remote_path, parameters, passwd_auth=False): password = '' if 'password' in parameters.keys(): password = parameters['password'] paramiko_settings = { 'username': parameters['username'], 'password': password } if (not passwd_auth) and 'private_key_path' in parameters: paramiko_settings['key_filename'] = os.path.join( storage.get_bdp_root_path(), parameters['private_key_path']) ssh_settings = { 'params': paramiko_settings, 'host': parameters['ip_address'], 'root': "/" } exists = True message = 'Remote path [%s] exists' % remote_path logger.debug("_remote_path_exists") try: fs = storage.RemoteStorage(settings=ssh_settings) fs.listdir(remote_path) except paramiko.AuthenticationException, e: message = 'Unauthorized access to %s' % parameters['ip_address'] exists = False
def generate_cloud_key(parameters): logger.debug('generating key') key_generated = True message = 'Key generated successfully' bdp_root_path = storage.get_bdp_root_path() key_name = parameters['private_key'] key_absolute_path = os.path.join(bdp_root_path, parameters['private_key_path']) key_dir = os.path.dirname(key_absolute_path) if not os.path.exists(key_dir): os.makedirs(key_dir) try: platform_type = parameters['platform_type'] logger.debug('platform_type=%s' % platform_type) parameters['key_dir'] = key_dir if platform_type == 'csrack' or platform_type == 'nectar': create_key_pair(parameters) create_ssh_security_group(parameters) else: return False, 'Unknown cloud platform' except EC2ResponseError as e: if 'Unauthorized' in e.error_code: key_generated = False message = 'Unauthorized access to %s' % platform_type else: key_generated = False message = e.error_code except Exception as e: key_generated = False message = e return key_generated, message
def _remote_path_exists(remote_path, parameters, passwd_auth=False): password = '' if 'password' in parameters.keys(): password = parameters['password'] if not parameters['port']: parameters['port'] = 22 paramiko_settings = {'username': parameters['username'], 'password': password, 'port': parameters['port']} if (not passwd_auth) and 'private_key_path' in parameters: paramiko_settings['key_filename'] = os.path.join( storage.get_bdp_root_path(), parameters['private_key_path'] ) if not parameters['port']: parameters['port'] = 22 ssh_settings = {'params': paramiko_settings, 'host': parameters['ip_address'], 'port': parameters['port'], 'root': "/"} exists = True message = 'Remote path [%s] exists' % remote_path logger.debug("_remote_path_exists") try: fs = storage.RemoteStorage(settings=ssh_settings) fs.listdir(remote_path) except paramiko.AuthenticationException, e: message = 'Unauthorized access to %s' % parameters['ip_address'] exists = False
def update_platform_settings(self, settings): try: platform_type = settings['platform_type'] except KeyError: logger.error("settings=%s" % settings) raise settings['type'] = platform_type if platform_type == 'nci': settings['private_key'] = os.path.join(storage.get_bdp_root_path(), settings['private_key_path']) settings['host'] = settings['ip_address'] settings['scheme'] = 'ssh' elif platform_type == 'rfs': settings['private_key'] = os.path.join(storage.get_bdp_root_path(), settings['private_key_path']) settings['host'] = settings['ip_address'] settings['scheme'] = 'ssh'
def update_platform_settings(self, settings): try: platform_type = settings['platform_type'] except KeyError: logger.error("settings=%s" % settings) raise settings['type'] = platform_type if platform_type == 'nci': settings['private_key'] = os.path.join( storage.get_bdp_root_path(), settings['private_key_path']) settings['host'] = settings['ip_address'] settings['scheme'] = 'ssh' elif platform_type == 'rfs': settings['private_key'] = os.path.join( storage.get_bdp_root_path(), settings['private_key_path']) settings['host'] = settings['ip_address'] settings['scheme'] = 'ssh'
def update_platform_settings(self, settings): try: platform_type = settings['platform_type'] except KeyError: logger.error("settings=%s" % settings) raise settings['type'] = platform_type if platform_type in ['csrack', 'amazon']: #settings['username'] = '******' #fixme avoid hardcoding settings['username'] = '******' #fixme avoid hardcoding settings['private_key_name'] = settings['private_key'] settings['private_key'] = os.path.join(storage.get_bdp_root_path(), settings['private_key_path']) settings['root_path'] = '/home/centos' # fixme avoid hardcoding settings['scheme'] = 'ssh' elif platform_type in ['nectar']: settings['username'] = '******' #fixme avoid hardcoding settings['private_key_name'] = settings['private_key'] settings['private_key'] = os.path.join(storage.get_bdp_root_path(), settings['private_key_path']) settings['root_path'] = '/home/ec2-user' # fixme avoid hardcoding settings['scheme'] = 'ssh'
def update_platform_settings(self, settings): try: platform_type = settings['platform_type'] except KeyError: logger.error("settings=%s" % settings) raise settings['type'] = platform_type if platform_type in ['csrack', 'amazon']: #settings['username'] = '******' #fixme avoid hardcoding settings['username'] = '******' #fixme avoid hardcoding settings['private_key_name'] = settings['private_key'] settings['private_key'] = os.path.join( storage.get_bdp_root_path(), settings['private_key_path']) settings['root_path'] = '/home/centos' # fixme avoid hardcoding settings['scheme'] = 'ssh' elif platform_type in ['nectar']: settings['username'] = '******' #fixme avoid hardcoding settings['private_key_name'] = settings['private_key'] settings['private_key'] = os.path.join( storage.get_bdp_root_path(), settings['private_key_path']) settings['root_path'] = '/home/ec2-user' # fixme avoid hardcoding settings['scheme'] = 'ssh'
def generate_rfs_key(parameters): key_generated = True message = 'Key generated successfully' password = '' if 'password' in parameters.keys(): password = parameters['password'] ssh_settings = {'username': parameters['username'], 'port': parameters['port'], 'password': password} storage_settings = {'params': ssh_settings, 'host': parameters['ip_address'], 'root': "/"} bdp_root_path = storage.get_bdp_root_path() key_name_org = os.path.splitext(os.path.basename(parameters['private_key_path']))[0] key_name = key_name_org private_key_absolute_path = os.path.join(bdp_root_path, parameters['private_key_path']) key_dir = os.path.dirname(private_key_absolute_path) if not os.path.exists(key_dir): os.makedirs(key_dir) counter = 1 while os.path.exists(os.path.join(key_dir, key_name)): key_name = '%s_%d' % (key_name_org, counter) counter += 1 parameters['private_key_path'] = os.path.join(os.path.dirname( parameters['private_key_path']), key_name) private_key_absolute_path = os.path.join(bdp_root_path, parameters['private_key_path']) public_key_absolute_path = '%s.pub' % private_key_absolute_path remote_key_path = os.path.join(parameters['home_path'], '.ssh', ('%s.pub' % key_name)) authorized_remote_path = os.path.join(parameters['home_path'], '.ssh', 'authorized_keys') try: private_key = paramiko.RSAKey.generate(1024) private_key.write_private_key_file(private_key_absolute_path) public_key = paramiko.RSAKey(filename=private_key_absolute_path) public_key_content = '%s %s' % (public_key.get_name(), public_key.get_base64()) f = open(public_key_absolute_path, 'w') f.write("\n%s\n" % public_key_content) f.close() fs = storage.RemoteStorage(settings=storage_settings) fs.save(remote_key_path, ContentFile(public_key_content)) ssh_client = open_connection(parameters['ip_address'], ssh_settings) #command = 'cat %s >> %s' % (remote_key_path, authorized_remote_path) space = " " command = 'echo %s >> %s; echo %s >> %s; echo %s >> %s; chmod 600 %s' % ( space, authorized_remote_path, public_key_content, authorized_remote_path, space, authorized_remote_path, authorized_remote_path) command_out, errs = run_command_with_status(ssh_client, command) if errs: if 'Permission denied' in errs: key_generated = False message = 'Permission denied to copy public key to %s/.ssh/authorized_keys' % parameters['home_path'] else: raise IOError except AuthError: key_generated = False message = 'Unauthorized access to %s' % parameters['ip_address'] except socket.gaierror, e: key_generated = False if 'Name or service not known' in e: message = 'Unknown IP address [%s]' % parameters['ip_address'] else: message = '[%s]: %s, %s' % (parameters['ip_address'], e.__doc__, e.strerror)
def generate_unix_key(parameters): key_generated = True message = 'Key generated successfully' password = '' if 'password' in parameters.keys(): password = parameters['password'] ssh_settings = {'username': parameters['username'], 'password': password} storage_settings = { 'params': ssh_settings, 'host': parameters['ip_address'], 'root': "/" } bdp_root_path = storage.get_bdp_root_path() key_name_org = os.path.splitext( os.path.basename(parameters['private_key_path']))[0] key_name = key_name_org private_key_absolute_path = os.path.join(bdp_root_path, parameters['private_key_path']) key_dir = os.path.dirname(private_key_absolute_path) if not os.path.exists(key_dir): os.makedirs(key_dir) counter = 1 while os.path.exists(os.path.join(key_dir, key_name)): key_name = '%s_%d' % (key_name_org, counter) counter += 1 parameters['private_key_path'] = os.path.join( os.path.dirname(parameters['private_key_path']), key_name) private_key_absolute_path = os.path.join(bdp_root_path, parameters['private_key_path']) public_key_absolute_path = '%s.pub' % private_key_absolute_path remote_key_path = os.path.join(parameters['home_path'], '.ssh', ('%s.pub' % key_name)) authorized_remote_path = os.path.join(parameters['home_path'], '.ssh', 'authorized_keys') try: private_key = paramiko.RSAKey.generate(1024) private_key.write_private_key_file(private_key_absolute_path) public_key = paramiko.RSAKey(filename=private_key_absolute_path) public_key_content = '%s %s' % (public_key.get_name(), public_key.get_base64()) f = open(public_key_absolute_path, 'w') f.write("\n%s\n" % public_key_content) f.close() fs = storage.RemoteStorage(settings=storage_settings) fs.save(remote_key_path, ContentFile(public_key_content)) ssh_client = open_connection(parameters['ip_address'], ssh_settings) #command = 'cat %s >> %s' % (remote_key_path, authorized_remote_path) space = " " command = 'echo %s >> %s; echo %s >> %s; echo %s >> %s; chmod 600 %s' % ( space, authorized_remote_path, public_key_content, authorized_remote_path, space, authorized_remote_path, authorized_remote_path) command_out, errs = run_command_with_status(ssh_client, command) if errs: if 'Permission denied' in errs: key_generated = False message = 'Permission denied to copy public key to %s/.ssh/authorized_keys' % parameters[ 'home_path'] else: raise IOError except AuthError: key_generated = False message = 'Unauthorized access to %s' % parameters['ip_address'] except socket.gaierror, e: key_generated = False if 'Name or service not known' in e: message = 'Unknown IP address [%s]' % parameters['ip_address'] else: message = '[%s]: %s, %s' % (parameters['ip_address'], e.__doc__, e.strerror)