def vm_remove(name): """Completely destroy the given virtual machine.""" # Check that the machine exists. with nostdout(): vms = list_virtual_machines() if not name in vms: print "VM %s does not exist." % name list_virtual_machines() return -1 # Grab its IP. vagrant_path = vm_path(name) ip = config.vms.get(name, 'ip') # Destroy the VM using Vagrant. call("cd %s && vagrant destroy" % vagrant_path, shell=True) # Remove the Vagrant directory. call("rm -Rf %s" % vagrant_path, shell=True) # Remove the entry from the hosts file. update_hostfile(ip) # Remove the section in the configuration file. config.vms.remove_section(name) config.vms.write() print "Virtual machine removed." return 0
def __init__(self, all_sentences): #self.infersent = torch.load('SentenceModels/infersent.allnli.pickle', map_location=lambda storage, loc: storage) self.infersent = torch.load('SentenceModels/infersent.allnli.pickle') self.infersent.set_glove_path('data/glove.840B.300d.txt') with utils.nostdout(): self.infersent.build_vocab(all_sentences)
def remove_provision(name): with nostdout(): provisions = provisions_list() if not name in provisions: print "No such registered provision." return -1 Provision.remove(name, config.provisions) print "Provision removed." return 0
def bind_provision(name, vm_name): # Check that the machine exists. with nostdout(): vms = list_virtual_machines() if not vm_name in vms: print "VM %s does not exist." % name list_virtual_machines() return -1 with nostdout(): provisions = provisions_list() if not name in provisions: print "No such registered provision." return -1 provision = Provision.load(name, config.provisions) provisioners_path = config.core.get("core", "provisioners_templates") vmpath = vm_path(vm_name) template_path = path.join(provisioners_path, provision.provisioner) snippet = render_template(template_path) vagrant_path = path.join(vmpath, "Vagrantfile") vagrantfile = open(vagrant_path, 'r') vf_content = vagrantfile.read() vagrantfile.close() vf_new_content = vf_content.replace('# VM:PROVISIONER', snippet) vagrantfile = open(vagrant_path, 'w') vagrantfile.write(vf_new_content) vagrantfile.close() provision.src.initialize(vmpath) config.vms.set(vm_name, 'provision', name) config.vms.write() print "Provision %s bound to %s." % (name, vm_name)
def wrap(name): # All those commands must check if the VM exists before being run. with nostdout(): vms = list_virtual_machines() if not name in vms: print "VM %s does not exist." % name list_virtual_machines() return -1 vagrant_path = vm_path(name) call("cd %s && vagrant %s" % (vagrant_path, command), shell=True) return 0
def add_provision(name, type, source, provisioner): name = normalize(name) with nostdout(): provisions = provisions_list() if name in provisions: print "A provision with the same name already exists." return -1 kwargs = { 'name': name, 'type': type, 'source': source, 'provisioner': provisioner } new_prov = Provision(**kwargs) new_prov.save(config.provisions) print "New provision registered."
def unbind_provision(vm_name): with nostdout(): vms = list_virtual_machines() if not vm_name in vms: print "VM %s does not exist." % vm_name list_virtual_machines() return -1 try: provision = config.vms.get(vm_name, 'provision') except NoOptionError: print "No provision is bound to this VM." return -1 provision = Provision.load(provision, config.provisions) config.vms.remove_option(vm_name, 'provision') config.vms.write() vmpath = vm_path(vm_name) provision.src.clean(vmpath) vf_buffer = "" vagrant_path = path.join(vmpath, "Vagrantfile") vagrantfile = open(vagrant_path, 'r') skip = False for line in vagrantfile.readlines(): if "VM:PROVISIONER:START" in line: skip = True vf_buffer = vf_buffer + "# VM:PROVISIONER\n" elif "VM:PROVISIONER:STOP" in line: skip = False elif not skip: vf_buffer = vf_buffer + line + '\n' vagrantfile.close() vagrantfile = open(vagrant_path, 'w') vagrantfile.write(vf_buffer) vagrantfile.close() print "Provision unbound from %s." % vm_name
def eigen_slr(fm, T, lam, seed=1): N = fm.N S, L, D = fm.covariances() # genetate security returns R = fm.R(T=T, seed=seed) # sample covariance matrix W = R.T.dot(R) / T ## use initial values for L0 and S0 to speed up with nostdout(): st = SLR(W, lam=lam, gam=6 / N) #L0=L, S0=S+D, rit = 1000, val = eigsh(st['L'], 1, which='LA', maxiter=int(1e9), return_eigenvectors=False)[0] return val
) parser.add_argument( "--users", "-u", dest="users", metavar="P", nargs="+", required=False, default=[], help="list of people to check in with (usernames only)", ) parser.add_argument( "--from-cron", "-c", action="store_true", help="Silence all logging statements (stdout)." ) parser.add_argument( "--debug", "-d", action="store_true", help="Log all events bagelbot can see." ) parser.add_argument( "--s3-sync", "-s", action="store_true", help="Synchronize SHELVE_FILE with AWS S3 before and after checking attendance.", ) parsed_args = parser.parse_args() if parsed_args.from_cron: with nostdout(): main(parsed_args) else: main(parsed_args)
"-u", dest="users", metavar="P", nargs="+", required=False, default=[], help="list of people to check in with (usernames only)", ) parser.add_argument("--from-cron", "-c", action="store_true", help="Silence all logging statements (stdout).") parser.add_argument("--debug", "-d", action="store_true", help="Log all events bagelbot can see.") parser.add_argument( "--s3-sync", "-s", action="store_true", help= "Synchronize SHELVE_FILE with AWS S3 before and after checking attendance.", ) parsed_args = parser.parse_args() if parsed_args.from_cron: with nostdout(): main(parsed_args) else: main(parsed_args)
def analyze(self): self.report.filebasename = file_basename(self.config.filename) self.report.filedirname = file_dirname(self.config.filename) self.report.filemimetype = file_mimetype(self.config.filename) magicresult = file_magic(self.config.filename) self.report.filemagic = "%s (%s)" % (magicresult["match"]["longname"], magicresult["match"]["shortname"]) if magicresult["match"] else None self.report.hashes.crc32 = file_hashes(self.config.filename, 'crc32') self.report.hashes.md5 = file_hashes(self.config.filename, 'md5') self.report.hashes.sha1 = file_hashes(self.config.filename, 'sha1') self.report.hashes.sha256 = file_hashes(self.config.filename, 'sha256') self.report.hashes.sha512 = file_hashes(self.config.filename, 'sha512') self.report.hashes.ssdeep = file_hashes(self.config.filename, 'ssdeep') with nostdout(): self.report.subfiles = file_subfiles(self.config.filename) # this might take some time to finish # based on the filesize, runtime might increase # will be autodisabled based on statsfilesizelimit config option if self.config.enableentropycompressionstats: stats = objdict(file_entropy_compression_stats(self.config.filename)) self.report.filesize = stats.filesizeinbytes self.report.fileminsize = float(stats.minfilesize) self.report.filecompressionratio = float(stats.compressionratio) self.report.fileentropy = float(stats.entropy) self.report.fileentropycategory = stats.entropycategory # this might take some time to finish # based on the filesize, runtime might increase # should be autodisabled based on (statsfilesizelimit) config option if self.config.enablefilevisualization: self.report.visual.pngrgb = file_to_pngimage(self.config.filename) self.report.visual.pnggray = file_to_pngimage(self.config.filename, enable_colors=False) rh = identicon(self.report.hashes.sha256) self.report.visual.identicon = rh.identicon if rh.success else None config = Config() config.x_title = 'Bytes' config.y_title = 'Frequency' config.x_scale = .25 config.y_scale = .25 config.width = 900 config.height = 300 config.title_font_size = 9 config.tooltip_font_size = 0 config.tooltip_border_radius = 0 config.no_data_text = "" config.show_legend = False config.show_only_major_dots = True config.human_readable = False config.show_y_labels = False config.fill = True config.style = CleanStyle bar_chart = pygal.Bar(config) # if enableentropycompressionstats config option is disabled, stats won't be generated above # as such we need to explicitly generate, on-demand if not stats: stats = objdict(file_entropy_compression_stats(self.config.filename)) bar_chart.add('', stats.bytefreqlist) self.report.visual.bytefreqhistogram = bar_chart.render(is_unicode=False) # pygal inserts a copyright symbol in rendered chart output # need to explicitly clean it before returning pygalregex = re.compile(r"\xc2\xa9") self.report.visual.bytefreqhistogram = pygalregex.sub("", self.report.visual.bytefreqhistogram) else: self.report.visual.pngrgb = None self.report.visual.pnggray = None self.report.visual.identicon = None self.report.visual.bytefreqhistogram = None # done with analysis, normalize report and return self.report = dict_normalize(self.report)
def add(name, base): """Create and register a new virtual machine.""" # Normalize the name of the VM name = normalize(name) # Check for doubles with nostdout(): vms = list_virtual_machines() if name in vms: print "A virtual machine with the same name already exists." return -1 # Grab the base IP from the config. base_ip = config.core.get("core", "base_ip") # The base IP should ends with a . (actually it should have the form of # xxx.xxx.xxx.). base_ip = base_ip if base_ip.endswith('.') else (base_ip + '.') # We will allocate an IP to the new virtual machine by incrementing the # byte of the IP address. # So let's start by gathering already used bytes. used_bytes = [int(config.vms.get(sec, 'ip').split('.')[-1]) for sec in config.vms.sections()] # Now create a list of every possible bytes. They must be between 0 and # 255, but: # * They must not be already used. # * They must not be 0 or 1, because they are reserved by Vagrant. byte_range = [i for i in range(2, 256) if not i in used_bytes] # Well, sorry, you'll need to patch virtual-manager to continue. if not byte_range: print "You've reach the maximum number of virtual machines. Well done." return -1 # Form the complete IP address. ip = base_ip + str(byte_range[0]) # Create a dict to substitute the template of the Vagrantfile. mapping = { 'ip': ip, 'base': base, } # Create the directory. vagrant_dir = vm_path(name) makedirs(path.join(vagrant_dir, 'link')) # Grab the default vagrant file. vagrant_template_path = config.core.get("core", "vagrant_template") vagrant_final_path = path.join(vagrant_dir, "Vagrantfile") render_template(vagrant_template_path, mapping, vagrant_final_path) # Create the configuration file. config.vms.add_section(name) config.vms.set(name, 'ip', ip) config.vms.write() # Update hosts file. newline = ["%s\t%s\n" % (ip, name)] update_hostfile(ip, newline) # Starts the virtual machine. call("/bin/bash -c 'cd %s && vagrant up'" % vagrant_dir, shell=True) print "Virtual machine successfully created" return 0