def test_run_command(self): """test run_command function""" try: main.run_command('fake_command') assert False except Exception: assert True try: main.run_command('ls') assert True except Exception: assert False
def update_links(zone, app, data, tenant=None): """Update actions on a link""" tstr = " -tenant=%s " % (tenant) if tenant else "" ret_val = {} for link in data: if link["new_state"] == "DEFINED_POLICY" or link[ "new_state"] == "DENIED_POLICY": ret_val["policies"] = ret_val.get("policies", 0) + 1 if link["state"] == "BASELINE_ALERT": ret_val["alerts"] = ret_val.get("alerts", 0) + 1 elif link["new_state"] == "SNOOZED_POLICY": if link["state"] == "BASELINE_ALERT": ret_val["alerts"] = ret_val.get("alerts", 0) + 1 ret_val["policies"] = ret_val.get("policies", 0) + 1 if not ret_val: ret_val['empty'] = {"success": "Empty policy request"} else: if g_debug: print(yaml.dump(data)) rc = run_command("%s api -zone %s -app %s -update-links %s" % (g_araalictl_path, zone, app, tstr), in_text=yaml.dump(data), result=True, strip=False) assert rc[0] == 0, rc[1] ret_val = json.loads(rc[1]) return ret_val
def fog_uninstall(tenant_id): """Uninstall AWS mode fog""" rc = run_command("%s uninstall-fog -tenant=%s -mode=aws" % (g_araalictl_path, tenant_id), result=True, strip=False) assert rc[0] == 0, rc[1]
def fog_install(tenant_id, nodes=1): """Install AWS mode fog""" rc = run_command("%s install-fog -tenant=%s -mode=aws -nodes=%d" % (g_araalictl_path, tenant_id, nodes), result=True, strip=False) assert rc[0] == 0, rc[1]
def tenant_delete(tenant_id): """Delete a subtenant""" rc = run_command("%s tenant -op=del -id=%s" % (g_araalictl_path, tenant_id), result=True, strip=False) assert rc[0] == 0, rc[1]
def get_fogs(tenant=None): """Get all fogs""" tstr = " -tenant=%s " % (tenant) if tenant else "" rc = run_command("%s api -fetch-fogs %s" % (g_araalictl_path, tstr), result=True, strip=False) assert rc[0] == 0, rc[1] return json.loads(rc[1])
def get_links(zone, app, tenant=None): """Get links for a zone and app""" tstr = " -tenant=%s " % (tenant) if tenant else "" rc = run_command("%s api -zone %s -app %s -fetch-links %s" % (g_araalictl_path, zone, app, tstr), result=True, strip=False) assert rc[0] == 0, rc[1] return yaml.load(rc[1], yaml.SafeLoader)
def get_zones(full=False, tenant=None): """Get zones and apps for tenant""" tstr = " -tenant=%s " % (tenant) if tenant else "" rc = run_command("%s api -fetch-zone-apps %s %s" % (g_araalictl_path, "-full" if full else "", tstr), result=True, strip=False) assert rc[0] == 0, rc[1] return yaml.load(rc[1], yaml.SafeLoader)
def get_compute(zone, app, tenant=None): """Get compute for a zone and app""" tstr = " -tenant=%s " % (tenant) if tenant else "" rc = run_command("%s api -zone %s -app %s -fetch-compute %s" % (g_araalictl_path, zone, app, tstr), result=True, strip=False) assert rc[0] == 0, rc[1] return json.loads(rc[1])
def fetch_flows(data, tenant=None): """Update actions on a link""" tstr = " -tenant=%s " % (tenant) if tenant else "" if g_debug: print(yaml.dump(data)) rc = run_command("%s api -fetch-flows %s" % (g_araalictl_path, tstr), in_text=yaml.dump(data), result=True, strip=False) assert rc[0] == 0, rc[1] return yaml.load(rc[1], yaml.SafeLoader)
def get_agents(tenant=None): """Get all agents""" tstr = " -tenant=%s " % (tenant) if tenant else "" for fog in get_fogs(tenant): rc = run_command("%s api -fetch-agents -agentid %s %s" % (g_araalictl_path, fog, tstr), result=True, strip=False) assert rc[0] == 0, rc[1] obj = json.loads(rc[1]) for o in obj: yield {"agent": o, "fog": fog}
def fog_setupconfig(tenant_id, dns_name, vpc_id, subnet_id, key_name, dns_hosted_zone_id, instance_type="m5.large"): """Setup config required to install a fog""" rc = run_command("%s config -tenant=%s Fog=%s" % (g_araalictl_path, tenant_id, dns_name), result=True, strip=False) assert rc[0] == 0, rc[1] cfg_str = "VPCIDParameter=%s,SubnetIDParameter=%s,KeyNameParameter=%s,HostedZoneParameter=%s,InstanceTypeParameter=%s,ReadOnlyRoleParameter=" % ( vpc_id, subnet_id, key_name, dns_hosted_zone_id, instance_type) rc = run_command("%s config -tenant=%s InternalFogInstallParams=%s" % (g_araalictl_path, tenant_id, cfg_str), result=True, strip=False) assert rc[0] == 0, rc[1]
def ping(zone, app, dst, port, agent_id, tenant=None): """ping dst from src""" tstr = " -tenant=%s " % (tenant) if tenant else "" rc = run_command("%s api -ping=src=%s/%s,dst=%s:%s -agentid %s %s" % (g_araalictl_path, zone, app, dst, port, agent_id, tstr), result=True, strip=False) if rc[1].decode().strip() == "open": return True print("*** z=%s a=%s dst=%s port=%s agent=%s tenant=%s rc=%s" % (zone, app, dst, port, agent_id, tenant, rc)) return False
def lambda_handler(event, context): params = parse_qs(event['body']) token = params['token'][0] if token != expected_token: logger.error("Request token (%s) does not match expected", token) return respond(Exception('Invalid request token')) user = params['user_name'][0] command = params['command'][0] channel = params['channel_name'][0] command_text = params['text'][0] return run_command(user, command, channel, command_text)
def enforce(data, service=False, tenant=None): """Enforce zone app or service.""" tstr = " -tenant=%s " % (tenant) if tenant else "" ostr = " -enforce-service " if service else " -enforce-zone-app " ret_val = {} if not data: ret_val['empty'] = {"success": "Empty enforcement request"} else: if g_debug: print(yaml.dump(data)) rc = run_command("%s api %s %s" % (g_araalictl_path, ostr, tstr), result=True, strip=False) assert rc[0] == 0, rc[1] ret_val = json.loads(rc[1]) return ret_val
def get_apps(tenant=None): """Get all apps""" tstr = " -tenant=%s " % (tenant) if tenant else "" for agent in get_agents(tenant): if agent["agent"][:len("ak8s.")] == "ak8s.": continue if agent["agent"] in ["invalid", "Unknown"]: continue rc = run_command( "%s api -fetch-apps -agentid %s -fogid %s %s" % (g_araalictl_path, agent["agent"], agent["fog"], tstr), result=True, strip=False) if rc[0] != 0: print("*** %s: %s" % (agent["agent"], rc[1])) continue obj = json.loads(rc[1]) for o in obj["apps"]: yield { "agent": agent["agent"], "fog": agent["fog"], "zone": obj["zone"], "app": o }
return line_number = 2 for line in f: line_number += 1 line = line.strip().split() if len(line) != 8: print("Expected 8 columns on line {}!".format(line_number)) return try: line = map(int, line) except ValueError: print("One of the columns on line {} is not an integer!".format(line_number)) return primes.extend(line) print("Read {:,} primes!".format(len(primes))) return primes if __name__ == "__main__": import main main.run_command(sys.argv[1:], "square_partitions", { "consecutive_evens": (consecutive_evens, [[3, 5, 9, 13, 15, 29, 33, 35, 47, 51], 6]), "evens_from_odds": (evens_from_odds, [100, 3, 0]), "evens_from_odds_recursive": (evens_from_odds_recursive, [100, 0, 3]), "modcount": (print_mod_count, [[], 4]), "naive_sieve": (naive_sieve, [10000]), "read_primes": (read_primes, [1]), "sieve": (sieve, [10000]), "square_partitions": (square_partitions, [2020]), })
def tenant_create(tenant_id, user_email, tenant_name=None, user_name=None): """Create a subtenant""" cmd = "%s tenant -op=add -id=%s -name=\"%s\" -user-email=%s -user-name=\"%s\"" % ( g_araalictl_path, tenant_id, tenant_name, user_email, user_name) rc = run_command(cmd, result=True, strip=False) assert rc[0] == 0, rc[1]
def deauth(): cmd = "sudo %s authorize -clean" % g_araalictl_path rc = run_command(cmd, result=True, strip=False) assert rc[0] == 0, rc[1]
def auth(token): cmd = "sudo %s authorize -token=- -local" % (g_araalictl_path) rc = run_command(cmd, in_text=token, result=True, strip=False) assert rc[0] == 0, rc[1]
def help(): """get araalictl help""" print( run_command("%s -h" % (g_araalictl_path), result=True, debug=False)[1])
def setup_class(cls): LOG.info('Setup Tests') cls.mock_database = mock_database_path cls.db_conn = KeyValueHelper(cls.mock_database) main.run_command('memcached -d -p 1234') cls.client = Client(('localhost', 1234))
print("The max_number parameter must be an integer >= 4!") return if not (isinstance(mod, int) and mod > 2): print("The mod parameter must be an integer > 2!") return if not check_residue_filter(residue_filter): return init(max_number) filter_primes(residue_filter) count = [0] * mod for p in primes: count[p % mod] += 1 print_mod_count(count) if __name__ == "__main__": import main main.run_command( sys.argv[1:], "partitions", { "gaps": (gap_info, [10000, 20]), "maxevens": (goldbach_max_evens, [10000, 2.5, 2.5, ()]), "modcount": (residue_distribution, [10000, 4, ()]), "modstats": (goldbach_mod_stats, [10000, 4, 1, 3, (), ()]), "partitions": (print_goldbach_partitions, [14]), "sliding": (goldbach_sliding_window, [10000, 1, 6, False]), "verify": (goldbach_verify, [10000, ()]), })
mysql.init() files=browser.browse() for _file in files : #main.run_command("cp "+str(_file)+" "+universal.current_dir) src=str(_file) universal.filename="" temp=len(_file)-1 while _file[temp]!="/": universal.filename=_file[temp]+universal.filename temp-=1 dst=str(universal.current_dir+'/'+str(universal.filename)) copyfile(src,dst) universal.logfile=universal.filename #as univeral.filename changes in main mysql.createconnection() main.initial() mysql.closeconnection() main.run_command("rm "+universal.logfile) #year=input("year\n") #s=main.run_command("ls "+str(year),1).split("\n") #fappend.close() #for x in s: # universal.filename=x # main.run_command("mv "+str(year)+"/"+str(x)+" "+universal.current_dir) # main.initial() # main.run_command("mv "+universal.current_dir+"/"+str(x)+" "+str(year)) #fappend=open("log.txt",'a') #fappend.write("\n********"+"\n"+str(year)+"\n*************\n\n\n") #fappend.close() #i=input("Filename\n")