def list_selected_hosts(): return 'You are working now on %s\n\nas full list: %s\n' % ( term.render('${BOLD}') + ', '.join( condense_hosts2(condense_hosts(TARGET_SETTINGS['hosts']))) + term.render('${NORMAL}'), ', '.join(TARGET_SETTINGS['hosts']), )
def list_selected_hosts(): return 'You are working now on %s\n\nas full list: %s\n' % ( term.render('${BOLD}') + ', '.join( condense_hosts2( condense_hosts( TARGET_SETTINGS['hosts']))) + term.render('${NORMAL}'), ', '.join(TARGET_SETTINGS['hosts']), )
def test_condense_hosts(self): TARGET_SETTINGS = { 'hosts': ["foobar01", "foobar02", "foobar03", "bazbar01", "bazbar02", "bazbar03"]} result = ' '.join( condense_hosts2(condense_hosts(TARGET_SETTINGS['hosts']))) condensed_hosts = '{baz,foo}bar[01..03]' self.assertEqual(result, condensed_hosts)
def test_condense_hosts(self): TARGET_SETTINGS = { 'hosts': [ "foobar01", "foobar02", "foobar03", "bazbar01", "bazbar02", "bazbar03" ] } result = ' '.join( condense_hosts2(condense_hosts(TARGET_SETTINGS['hosts']))) condensed_hosts = '{baz,foo}bar[01..03]' self.assertEqual(result, condensed_hosts)
def load_settings_and_create_dirs(log_to_file=True): TARGET_BASENAME = 'target' global TARGET_SETTINGS TARGET_SETTINGS = load_target_file(TARGET_BASENAME) initialize_broadcast_client() os.umask(2) try: os.makedirs(OUT_DIR) except OSError as e: if e.errno != 17: # 17: file exists root_logger.critical('Cannot create cache directory %s on %s' % (OUT_DIR, USER_INFO['yadt_host'])) root_logger.exception(e) sys.exit(1) pass global TODAY TODAY = time.strftime('%Y-%m-%d') TIME_FORMAT = '%Y-%m-%d--%H-%M-%S' global STARTED_ON STARTED_ON = time.strftime(TIME_FORMAT) global term term = yadtshell.TerminalController.TerminalController() global ybc if broadcasterconf_imported: ybc = broadcasterconf.create(TARGET_SETTINGS['name']) else: ybc = DummyBroadcaster() LOG_DIR = os.path.join(LOG_DIR_PREFIX, TODAY, TARGET_SETTINGS['name']) try: os.makedirs(LOG_DIR) except OSError as e: if e.errno != 17: # 17: file exists root_logger.critical('cannot write to log dir %s' % LOG_DIR) root_logger.exception(e) sys.exit(1) global log_file log_file = create_next_log_file_name_with_command_arguments_as_tag( log_dir=LOG_DIR, target_name=TARGET_SETTINGS['name'], command_start_timestamp=STARTED_ON, user_name=USER_INFO['user'], source_host=USER_INFO['yadt_host'].split('.')[0], command_arguments=sys.argv ) formatter = logging.Formatter( '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s', '%Y%m%d-%H%M%S') if log_to_file: file_handler = logging.FileHandler(log_file) file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(formatter) root_logger.addHandler(file_handler) logger.debug(yaml.dump(USER_INFO, default_flow_style=False)) logger.debug('Called "{0}"'.format(' '.join(sys.argv))) logger.debug('output dir is %s' % OUTPUT_DIR) OUT_TARGET_FILE = os.path.join(OUT_DIR, TARGET_BASENAME) try: changed = not filecmp.cmp(TARGET_BASENAME, OUT_TARGET_FILE) if changed: logger.info( 'target settings have changed since last call, thus cleaning cached data') except OSError: changed = True if changed: shutil.rmtree(OUT_DIR) os.makedirs(OUT_DIR) shutil.copy2(TARGET_BASENAME, OUT_TARGET_FILE) global VIEW_SETTINGS VIEW_SETTINGS_FILE = 'view' try: view_file = open(VIEW_SETTINGS_FILE) VIEW_SETTINGS = yaml.load(view_file) view_file.close() except Exception: logger.debug( '"view" file not found, falling back to default values: %s' % VIEW_SETTINGS) hosts_condensed_filename = os.path.join(OUT_DIR, 'hosts_condensed') with open(hosts_condensed_filename, 'w') as hosts_condensed_file: condensed = condense_hosts2(condense_hosts(TARGET_SETTINGS['hosts'])) hosts_condensed_file.write(' '.join(condensed)) def list_selected_hosts(): return 'You are working now on %s\n\nas full list: %s\n' % ( term.render('${BOLD}') + ', '.join( condense_hosts2( condense_hosts( TARGET_SETTINGS['hosts']))) + term.render('${NORMAL}'), ', '.join(TARGET_SETTINGS['hosts']), ) identity = TARGET_SETTINGS.get('identity') login = TARGET_SETTINGS.get('login') credentials = '' if identity: credentials += ' -i %(identity)s' % locals() if login: credentials += ' -l %(login)s' % locals() CONNECTIONS_DIR = os.path.join(OUTPUT_DIR, 'connections') global SSH_CONTROL_PATH SSH_CONTROL_PATH = os.path.join(CONNECTIONS_DIR, '%h') try: os.makedirs(CONNECTIONS_DIR) except OSError: pass global SSH SSH = 'ssh -o ControlPath=%s -A %s -T -o ConnectTimeout=4 -o BatchMode=yes -o CheckHostIP=no -o StrictHostKeyChecking=no -q' % ( SSH_CONTROL_PATH, credentials) global tracking_id tracking_id = None
shutil.copy2(TARGET_SETTINGS_FILE, OUT_TARGET_FILE) global VIEW_SETTINGS VIEW_SETTINGS = {'info-view': ['matrix', 'color', 'maxcols']} VIEW_SETTINGS_FILE = 'view' try: view_file = open(VIEW_SETTINGS_FILE) VIEW_SETTINGS = yaml.load(view_file) view_file.close() except: logger.debug('"view" file not found, falling back to default values: %s' % VIEW_SETTINGS) hosts_condensed_file = open(os.path.join(OUT_DIR, 'hosts_condensed'), 'w') print >> hosts_condensed_file, ', '.join(condense_hosts2(condense_hosts(TARGET_SETTINGS['hosts']))) hosts_condensed_file.close() def list_selected_hosts(): return 'You are working now on %s\n\nas full list: %s\n' % ( term.render('${BOLD}') + ', '.join(condense_hosts2(condense_hosts(TARGET_SETTINGS['hosts']))) + term.render('${NORMAL}'), ', '.join(TARGET_SETTINGS['hosts']), ) identity = TARGET_SETTINGS.get('identity') login = TARGET_SETTINGS.get('login') credentials = '' if identity: credentials += ' -i %(identity)s' % locals() if login: credentials += ' -l %(login)s' % locals()