def get_constellation_info(my_constellation): ''' Look up a given constellation on Redis @param my_constellation Constellation id (string) ''' for constellation in cloudsimd.list_constellations(): if constellation['constellation_name'] == my_constellation: return constellation return None
def change_mode(self, team): constellations = cloudsimd.list_constellations() const = [ x for x in constellations if x.has_key('constellation_name') and x['constellation_name'] == team['cloudsim'] ][0] ip = const['simulation_ip'] key_dir = const['constellation_directory'] ssh = sshclient.SshClient(key_dir, KEY_NAME, USER, ip) cmd = CMD % (self.oldmode, self.newmode) print('Executing on team %s: %s' % (team['team'], cmd)) sys.stdout.flush() ssh.cmd(cmd) print('Changed mode on %s from %s to %s' % (team['team'], self.oldmode, self.newmode)) sys.stdout.flush()
def go(args): ''' Function that run a command or copy a file in multiples machines. A thread is created to the job in every machine. A shared queue among the threads is used to capture the returned value of the commands, stdout, and stderr. @param args Command line arguments ''' # Retrieve command line arguments ssh_options = args.ssh_options machine_type = args.type subcommand = args.which if subcommand == 'run': arg = args.cmd stats_msg = 'Command executed in' elif subcommand == 'copy': arg = args.src dst = args.dst stats_msg = 'File uploaded into' if not os.path.exists(arg): print '%sFile not found: (%s)...Aborting%s' % (RED, arg, NORMAL) sys.exit(1) else: print 'Invalid subcommand (%s)...Aborting' % subcommand sys.exit(1) # Sanity check if getpass.getuser() != 'root': print "Invalid user, you should run this program as root...Aborting" sys.exit(1) # Counter for stats, threads for the job, and a queue for the returned vals11 counter = 0 threads = [] succeed_queue = Queue.Queue() if machine_type in ['cs', 'router']: try: # Import cloudsimd basepath = os.path.abspath( os.path.join(os.path.dirname(__file__), '..')) if not os.path.exists(os.path.join(basepath, 'cloudsimd')): print 'Your DB does not contain any remote CloudSim...Aborting' sys.exit(1) sys.path.insert(0, basepath) from cloudsimd import cloudsimd const_list = cloudsimd.list_constellations() except Exception, excep: print('%sError importing cloudsimd: %s%s' % (RED, repr(excep), NORMAL)) sys.exit(1) # Iterate over the list of CloudSims for constellation in const_list: try: name = constellation['constellation_name'] if name.startswith(CLOUDSIM_PREFIX) and machine_type == 'cs': machine = Ssh_cloudsim(name, constellation) elif name.startswith( CONST_PREFIX) and machine_type == 'router': machine = Ssh_router(name, constellation) else: continue if subcommand == 'run': params = [ssh_options, arg, 'ubuntu', succeed_queue] t = Thread(target=machine.run, args=params) elif subcommand == 'copy': params = [ssh_options, arg, 'ubuntu', dst, succeed_queue] t = Thread(target=machine.copy, args=params) threads.append(t) t.start() counter += 1 except Exception, excep: print('%sError running command: %s%s' % (RED, repr(excep), NORMAL)) counter -= 1