def gen_keys(key='', key_path_dir=''):
 key_basename = ('key-' + uuid4().hex)
 if (not key_path_dir):
  key_path_dir = os.path.join(KEY_DIR, 'role_key', key_basename)
 private_key = os.path.join(key_path_dir, 'id_rsa')
 public_key = os.path.join(key_path_dir, 'id_rsa.pub')
 mkdir(key_path_dir, mode=755)
 if (not key):
  key = RSAKey.generate(2048)
  key.write_private_key_file(private_key)
 else:
  key_file = os.path.join(key_path_dir, 'id_rsa')
  with open(key_file, 'w') as f:
   f.write(key)
   f.close()
  with open(key_file) as f:
   try:
    key = RSAKey.from_private_key(f)
   except SSHException as e:
    shutil.rmtree(key_path_dir, ignore_errors=True)
    raise SSHException(e)
 os.chmod(private_key, 420)
 with open(public_key, 'w') as content_file:
  for data in [key.get_name(), ' ', key.get_base64(), (' %s@%s' % ('jumpserver', os.uname()[1]))]:
   content_file.write(data)
 return key_path_dir

def gen_keys ( key = '' , key_path_dir = '' ) : key_basename = ( 'key-' + uuid4 ( ) . hex ) if ( not key_path_dir ) : key_path_dir = os . path . join ( KEY_DIR , 'role_key' , key_basename ) private_key = os . path . join ( key_path_dir , 'id_rsa' ) public_key = os . path . join ( key_path_dir , 'id_rsa.pub' ) mkdir ( key_path_dir , mode = 755 ) if ( not key ) : key = RSAKey . generate ( 2048 ) key . write_private_key_file ( private_key ) else : key_file = os . path . join ( key_path_dir , 'id_rsa' ) with open ( key_file , 'w' ) as f : f . write ( key ) f . close ( ) with open ( key_file ) as f : try : key = RSAKey . from_private_key ( f ) except SSHException as e : shutil . rmtree ( key_path_dir , ignore_errors = True ) raise SSHException ( e ) os . chmod ( private_key , 420 ) with open ( public_key , 'w' ) as content_file : for data in [ key . get_name ( ) , ' ' , key . get_base64 ( ) , ( ' %s@%s' % ( 'jumpserver' , os . uname ( ) [ 1 ] ) ) ] : content_file . write ( data ) return key_path_dir
generate rsa keys of nbits bits .