def get(self): if self.host is None: click.secho("Problem parsing your configuration file", fg='red') os._exit(1) k = Kvirt(host=self.host, port=self.port, user=self.user, protocol=self.protocol, url=self.url) if k.conn is None: click.secho("Couldnt connect to specify hypervisor %s. Leaving..." % self.host, fg='red') os._exit(1) return k
def setup_class(self): self.host = os.environ.get('KVIRT_HOST', '127.0.0.1') self.user = os.environ.get('KVIRT_USER', 'root') self.path = os.environ.get('KVIRT_PATH', '') self.virttype = os.environ.get('KVIRT_TYPE', 'kvm') self.libvirt_user = os.environ.get('KVIRT_LIBVIRT_USER', 'qemu') k = Kvirt(self.host) name = "test_%s" % ''.join(random.choice(string.lowercase) for i in range(5)) self.name = name self.conn = k
def __init__(self): self.inventory = {} self.read_cli_args() inifile = "%s/kcli.yml" % os.environ.get('HOME') if not os.path.exists(inifile): ini = {'default': {'client': 'local'}, 'local': {}} print "Using local hypervisor as no kcli.yml was found..." else: with open(inifile, 'r') as entries: ini = yaml.load(entries) if 'default' not in ini or 'client' not in ini['default']: print "Missing default section in config file. Leaving..." os._exit(1) client = ini['default']['client'] if client not in ini: print "Missing section for client %s in config file. Leaving..." % client os._exit(1) options = ini[client] host = options.get('host', '127.0.0.1') port = options.get('port', None) user = options.get('user', 'root') protocol = options.get('protocol', 'ssh') self.k = Kvirt(host=host, port=port, user=user, protocol=protocol) if self.k.conn is None: print "Couldnt connect to specify hypervisor %s. Leaving..." % host os._exit(1) # Called with `--list`. if self.args.list: self.inventory = self.get() # Called with `--host [hostname]`. elif self.args.host: # Not implemented, since we return _meta info `--list`. self.inventory = empty() # If no groups or vars are present, return an empty inventory. else: self.inventory = empty() print json.dumps(self.inventory)
def bootstrap(genfile, auto, name, host, port, user, protocol, url, pool, poolpath): """Bootstrap hypervisor, creating config file and optionally pools and network""" click.secho("Bootstrapping env", fg='green') if genfile or auto: if host is None and url is None: url = 'qemu:///system' host = '127.0.0.1' if pool is None: pool = 'default' if poolpath is None: poolpath = '/var/lib/libvirt/images' if '/dev' in poolpath: pooltype = 'logical' else: pooltype = 'dir' nets = { 'default': { 'cidr': '192.168.122.0/24' }, 'cinet': { 'cidr': '192.168.5.0/24' } } # disks = [{'size': 10}] if host == '127.0.0.1': ini = { 'default': { 'client': 'local' }, 'local': { 'pool': pool, 'nets': ['default'] } } else: if name is None: name = host ini = {'default': {'client': name}} ini[name] = {'host': host, 'pool': pool, 'nets': ['default']} if protocol is not None: ini[name]['protocol'] = protocol if user is not None: ini[name]['user'] = user if port is not None: ini[name]['port'] = port if url is not None: ini[name]['url'] = url else: ini = {'default': {}} default = ini['default'] click.secho("We will configure kcli together !", fg='blue') if name is None: name = raw_input( "Enter your default client name[local]: ") or 'local' client = name if pool is None: pool = raw_input("Enter your default pool[default]: ") or 'default' default['pool'] = pool size = raw_input("Enter your client first disk size[10]: ") or '10' default['disks'] = [{'size': size}] net = raw_input( "Enter your client first network[default]: ") or 'default' default['nets'] = [net] cloudinit = raw_input("Use cloudinit[True]: ") or 'True' default['cloudinit'] = cloudinit diskthin = raw_input("Use thin disks[True]: ") or 'True' default['diskthin'] = diskthin ini['default']['client'] = client ini[client] = {} client = ini[client] if host is None: host = raw_input( "Enter your client hostname/ip[localhost]: ") or 'localhost' client['host'] = host if url is None: url = raw_input("Enter your client url: ") or None if url is not None: client['url'] = url else: if protocol is None: protocol = raw_input( "Enter your client protocol[ssh]: ") or 'ssh' client['protocol'] = protocol if port is None: port = raw_input("Enter your client port: ") or None if port is not None: client['port'] = port user = raw_input("Enter your client user[root]: ") or 'root' client['user'] = user pool = raw_input("Enter your client pool[%s]: " % default['pool']) or default['pool'] client['pool'] = pool poolcreate = raw_input("Create pool if not there[Y]: ") or 'Y' if poolcreate == 'Y': poolpath = raw_input( "Enter yourpool path[/var/lib/libvirt/images]: " ) or '/var/lib/libvirt/images' else: poolpath = None client['pool'] = pool size = raw_input( "Enter your client first disk size[%s]: " % default['disks'][0]['size']) or default['disks'][0]['size'] client['disks'] = [{'size': size}] net = raw_input("Enter your client first network[%s]: " % default['nets'][0]) or default['nets'][0] client['nets'] = [net] nets = {} netcreate = raw_input("Create net if not there[Y]: ") or 'Y' if netcreate == 'Y': cidr = raw_input( "Enter cidr [192.168.122.0/24]: ") or '192.168.122.0/24' nets[net] = {'cidr': cidr, 'dhcp': True} cinetcreate = raw_input( "Create cinet network for uci demos if not there[N]") or 'N' if cinetcreate == 'Y': nets['cinet'] = {'cidr': '192.168.5.0/24', 'dhcp': True} cloudinit = raw_input("Use cloudinit for this client[%s]: " % default['cloudinit']) or default['cloudinit'] client['cloudinit'] = cloudinit diskthin = raw_input("Use thin disks for this client[%s]: " % default['diskthin']) or default['diskthin'] client['diskthin'] = diskthin k = Kvirt(host=host, port=port, user=user, protocol=protocol, url=url) if k.conn is None: click.secho("Couldnt connect to specify hypervisor %s. Leaving..." % host, fg='red') os._exit(1) k.bootstrap(pool=pool, poolpath=poolpath, pooltype=pooltype, nets=nets) # TODO: # DOWNLOAD CIRROS ( AND CENTOS7? ) IMAGES TO POOL ? path = os.path.expanduser('~/kcli.yml') if os.path.exists(path): copyfile(path, "%s.bck" % path) with open(path, 'w') as conf_file: yaml.safe_dump(ini, conf_file, default_flow_style=False, encoding='utf-8', allow_unicode=True) click.secho("Environment bootstrapped!", fg='green')
def main(): argument_spec = { "host": { "default": '127.0.0.1', "type": "str" }, "port": { "default": '22', "type": "str" }, "user": { "default": 'root', "type": "str" }, "protocol": { "default": 'ssh', "type": "str", 'choices': ['ssh', 'tcp'] }, "url": { "default": None, "type": "str" }, "state": { "default": "present", "choices": ['present', 'absent'], "type": 'str' }, "name": { "required": True, "type": "str" }, "description": { "default": 'kvirt', "type": "str" }, "numcpus": { "default": 2, "type": "int" }, "memory": { "default": 512, "type": "int" }, "pool": { "default": 'default', "type": "str" }, "template": { "type": "str" }, } module = AnsibleModule(argument_spec=argument_spec) # url = module.params['url'] if 'url' in module.params else None # k = Kvirt(host=module.params['host'], port=module.params['port'], user=module.params['user'], protocol=module.params['protocol'], url=url) k = Kvirt(host=module.params['host'], port=module.params['port'], user=module.params['user'], protocol=module.params['protocol']) name = module.params['name'] exists = k.exists(name) state = module.params['state'] if state == 'present': if exists: changed = False skipped = True meta = {'result': 'skipped'} else: template = module.params[ 'template'] if 'template' in module.params else None # description = module.params['description'] if 'description' in module.params else None pool = module.params[ 'pool'] if 'pool' in module.params else 'default' numcpus = module.params[ 'numcpus'] if 'numcpus' in module.params else 2 memory = module.params[ 'memory'] if 'numcpus' in module.params else '512' # meta = k.create(name=name, description=description, numcpus=numcpus, memory=memory, pool=pool, template=template, disks=disks, disksize=disksize, diskthin=diskthin, diskinterface=diskinterface, nets=nets, iso=iso, vnc=vnc, cloudinit=cloudinit, start=start, keys=keys, cmds=cmds, ips=ips, netmasks=netmasks, gateway=gateway, dns=dns, domain=domain) meta = k.create(name=name, numcpus=numcpus, memory=memory, pool=pool, template=template) # meta = k.create(name) changed = True skipped = False else: if exists: meta = k.delete(name) changed = True skipped = False else: changed = False skipped = True meta = {'result': 'skipped'} module.exit_json(changed=changed, skipped=skipped, meta=meta)