supernova – manage multiple openstack environment

supernova is an easy to use tool that helps manage multiple openstack environments.

Details –

Name        : supernova
Arch        : noarch
Epoch       : 0
Version     : 2.2.0
Release     : 2.fc24
Size        : 62 k
Repo        : @System
From repo   : fedora
Summary     : Use novaclient with multiple OpenStack nova environments the easy way
URL         : https://github.com/major/supernova
License     : ASL 2.0
Description : supernova manages multiple nova environments without sourcing
: novarc files or mucking with environment variables.

To get started, start with installing supernova with

dnf install supernova

and after that you need to create the configuration file, typically “~/.supernova” with content such as:

1
2
3
4
5
6
7
8
9
10
[production]
OS_AUTH_URL=https://identity.api.rackspacecloud.com/v2.0/
OS_AUTH_SYSTEM=rackspace
OS_COMPUTE_API_VERSION=1.1
NOVA_RAX_AUTH=1
OS_REGION_NAME=<a class="zem_slink" title="Deutsche Flugzeug-Werke" href="http://en.wikipedia.org/wiki/Deutsche_Flugzeug-Werke" target="_blank" rel="wikipedia">DFW</a>
NOVA_SERVICE_NAME=cloudServersOpenStack
OS_PASSWORD=my_password
OS_USERNAME=my_username
OS_TENANT_NAME=my_tenant_name
1
2
3
4
5
6
7
8
9
10
[development]
OS_AUTH_URL=https://identity.api.rackspacecloud.com/v2.0/
OS_AUTH_SYSTEM=rackspace
OS_COMPUTE_API_VERSION=1.1
NOVA_RAX_AUTH=1
OS_REGION_NAME=DFW
NOVA_SERVICE_NAME=cloudServersOpenStack
OS_PASSWORD=my_password-devel
OS_USERNAME=my_username-devel
OS_TENANT_NAME=my_tenant_name
1
You can check out the configuration section of <a href="http://supernova.readthedocs.io/en/latest/gettingstarted/">Getting started</a> page.

And once you have created the configuration file, it is very simple. Some example commands

supernova [enviromment_name[,enviromment_name]...] [commands]

Examples

supernova prod list
supernova prod image-list
supernova prod flavor-list
supernova prod boot --image image_uuid --flavor flavor_id myserver.example.com

Python script to manage virtual machines with python API for libvirt.

Most of times I use virt-manager to manage VMs but sometimes, I have to manage VMs on other hosts over ssh when connected over VPN or while I am working remotely. GUI like virt-manager thus becomes slow, and hence I like to use cli commands and nothing is better than virsh. But here is simple script that I use sometimes to manage the VMs with CLI based menu. Hope you find it useful.

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 Amit Agarwal <http://blog.amit-agarwal.co.in>
# Python scripts to manage virtual machines.
# 
# Distributed under terms of the MIT license.

"""
python wrapper for libvirt
"""

import libvirt
import sys
import pprint
from xml.dom import minidom

import os

hv_host="localhost"
names = []



def chhost():
    global hv_host
    global uri
    global conn

    print "Host is now set to "+hv_host

    try:
        ui = raw_input ("Provide the uri in format [email protected] : ")
    except:
        ui=''

    if len(ui) > 2 :
        if uri.find("lxc") :
            print "Using LXC uri"
        else:
            uri = "qemu+ssh://"+ui+"/system"
    else:
        uri = "qemu:///system"

    print uri + " is set as URI"
    conn = libvirt.open(uri)
    if conn == None:
        print 'Failed to open connection to the hypervisor'
        sys.exit(1)
    else:
        print "Connection established with "+uri

def vms(state):

    global names
    global conn
    if state == 'running' :
        active_hosts = conn.listDomainsID()
        if (len (active_hosts) == 0 ) :
            print "No Running VMs"
            return
        for i in range ( len (active_hosts) ):
            dom0 = conn.lookupByID(active_hosts[i])
            print "\t%d\t%s"%(active_hosts[i], dom0.name())
        val = input ( "Enter the ID ")
        return int(val)
    else :
        names = conn.listDefinedDomains()
        for i in range ( len (names) ):
            print "\t%d\t%s" %(i, names[i])
        tostart = input("Please enter number to start the VM :: ")
        print "Starting VM "+names[tostart]
        return names[tostart]

def writer(stream, data, buffer):
    buffer.write(data)

def start():
    global names
    global conn
    tostart = vms('')
    dom0 = conn.lookupByName(tostart)
    dom0.create()

def stop():
    print "List currently running VMs"
    val = vms('running')
    dom0 = conn.lookupByID(val)
    dom0.shutdown()


def view():
    print "List currently running VMs"
    val = vms('running')
    dom0 = conn.lookupByID(val)
    os.system("virt-viewer " + dom0.name() + " &" );

def viewip():
    val = vms('running')
    print "ID Returned is "+str(val)
    dom0 = conn.lookupByID(val)
    pprint.pprint (dom0.info())
    # pprint.pprint (dom0.GetInterfacesAddresses())

    for lease in conn.networkLookupByName("default").DHCPLeases():
        print(lease)

    raw_xml = dom0.XMLDesc (0)
    xml = minidom.parseString (raw_xml)
    interfaceTypes = xml.getElementsByTagName ( 'interface')
    for interfaceType in interfaceTypes :
        print ( 'interface: type=' +interfaceType.getAttribute ( 'type'))
        interfaceNodes = interfaceType.childNodes
        for interfaceNode in interfaceNodes :
            if interfaceNode.nodeName [0:1] != '#' :
                print ( '  ' +interfaceNode.nodeName)
                for attr in interfaceNode.attributes.keys ():
                    print ( '    ' +interfaceNode .attributes[attr].name + ' = ' + interfaceNode.attributes[attr].value)

    print 'View'


def state_to_string (state):
    statearr = [ "", "Running" ]
    return statearr[state[0]]

def info(dom):
    states = {
        libvirt.VIR_DOMAIN_NOSTATE: 'no state',
        libvirt.VIR_DOMAIN_RUNNING: 'running',
        libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
        libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
        libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
        libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
        libvirt.VIR_DOMAIN_CRASHED: 'crashed',
    }

    [state, maxmem, mem, ncpu, cputime] = dom.info()
    return '%s' % (states.get(state, state))

def viewdets():
    global conn
    global names
    columns = 3


    domains = map(conn.lookupByName, names)

    ids = conn.listDomainsID()
    running = map(conn.lookupByID, ids)



    print 'Defined domains:'
    for row in map(None, *[iter(domains)] * columns):
        for domain in row:
            if domain:
                print "%s, %s" %( dom.name(), info(domain)),
        print
    print

    print 'Running domains:'
    for row in map(None, *[iter(running)] * columns):
        for domain in row:
            if domain:
                print info(domain),
        print



    print "Id    Name                           State"
    print "-" * 52
    for dom in conn.listAllDomains() :
        [state, maxmem, mem, ncpu, cputime] = dom.info()
        print "%3s %-31s%s" %\
            (dom.ID() if dom.ID() > 0 else "-",
             dom.name(),
             info(dom))
        # if dom.ID() > 0:
        # cpu_stats = dom.getCPUStats(False)
        # for (i, cpu) in enumerate(cpu_stats):
        # print 'CPU #%d Time: %.2lf sec' % (i, cpu['cpu_time'] / 1000000000.0 )

    print "View all details"
    print names
    active_hosts = conn.listDomainsID()
    if  len ( active_hosts ) > 0 :
        for id in active_hosts:
            dom = conn.lookupByID(id)
            infos = dom.info()
            print 'ID = %d' % id
            print 'Name =  %s' % dom.name()
            print 'State = %d' % infos[0]
            print 'Max Memory = %d' % infos[1]
            print 'Number of virt CPUs = %d' % infos[3]
            print 'CPU Time (in ns) = %d' % infos[2]
            print ' '

def quit():
    print "Exit on user request"
    sys.exit(0)


def menu():
    mm = [ {
        'Description': 'Change the host',
        'function' : chhost,
    },
          {
              'Description' : 'Start a Container' ,
              'function' : start
          },
          {
              'Description' : 'View a container',
              'function' : view
          },
          {
              'Description' : 'Stop a container',
              'function' : stop
          },
          {
              'Description' : "Take Screenshot",
              'function' : screenshot,
          },
          {
              'Description' :'View IP of Container',
              'function' : viewip,
          },
          {
              'Description' : 'View all Details of Conatainer',
              'function' : viewdets,
          },
          {
              'Description' : 'Quit',
              'function' : quit,
          },

          ]
    print "Virsh wrapper by Amit Agarwal"
    i = 0
    for i in range ( len (mm) ):
        print "\t%d) %s" % (i, mm[i]['Description'] )

    try:
        val = int(raw_input ("Please Select ::"))
    except:
        val=-1

    print len(mm)
    if val >= 0 and val < len (mm) :
        mm[val]['function']()


uri = "qemu:///system"
conn = libvirt.open(uri)
if conn == None:
    print 'Failed to open connection to the hypervisor'
    sys.exit(1)

while 1:
    menu()

get the contents of whole site like some wiki or wikia

For wikis and wikia, generally if you are trying to get some url mirror, then websucker.py is an excellent option. This script is in the python sources so, to get this tool,

yumdownloader --source python

Install the rpm downloaded in current directory and then go to ~/rpmbuild/SOUURCES.  You should find a Python-*.tar.xz file here, just extract with

tar xvf Python*.tar.xz

and there you go, you should find the tool in Tools/webchecker/websucker.py.

Enhanced by Zemanta