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 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)