Skip to content

Instantly share code, notes, and snippets.

@atodorov-storpool
Created May 15, 2017 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atodorov-storpool/4b0e87fe4586419a5fb11a357c00e345 to your computer and use it in GitHub Desktop.
Save atodorov-storpool/4b0e87fe4586419a5fb11a357c00e345 to your computer and use it in GitHub Desktop.
helper script to summon a deleted VM in OpenNebula.
#!/usr/bin/env python
#
# -------------------------------------------------------------------------- #
# Copyright 2017, StorPool (storpool.com) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
from sys import exit
try:
from lxml import etree
#print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
#print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
#print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
#print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
#print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")
def getEle(etp, name):
for e in etp.findall(name):
#print "getEle({0}):{1}".format(name,e.text)
return e
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("vmId", help="VM's ID")
parser.add_argument("vmState", help="VM's state")
parser.add_argument("dbConn", help="Database connection method details")
args = parser.parse_args()
print "VM:{0} STATE:{1} DB:{2}".format(args.vmId,args.vmState,args.dbConn)
dbConn = args.dbConn.split(":")
if dbConn[0] == 'sqlite3':
import sqlite3
dbSELECT = "SELECT * FROM ? WHERE oid=?"
dbUPDATE = "UPDATE ? SET body=?,state=? WHERE oid=?"
db = sqlite3.connect(dbConn[1])
elif dbConn[0] == 'mysql':
import mysql.connector
dbSELECT = "SELECT * FROM {0} WHERE oid = %s"
dbUPDATE = "UPDATE {0} SET body = %s, state = %s WHERE oid = %s"
dbconf = {
'user': dbConn[1] or 'oneadmin',
'password': dbConn[2] or '',
'host': dbConn[3] or '127.0.0.1',
'database': dbConn[4] or 'opennebula',
'raise_on_warnings': True,
}
db = mysql.connector.connect(**dbconf)
else:
print("Uknown connection type {0}".format(conn[0]))
exit(1)
dbc = db.cursor()
dbc.execute(dbSELECT.format('vm_pool'), (args.vmId,))
res = dbc.fetchone()
if res[2]:
xmlBody = res[2]
et = etree.XML(xmlBody, etree.XMLParser(strip_cdata=False,remove_blank_text=True))
changed = False
persistent = None
state_et = getEle(et,".//STATE")
print "Changing STATE from {0} to {1}".format(state_et.text,args.vmState)
state_et.text = args.vmState
#print etree.tostring(et,pretty_print=True)
dbc.execute(dbUPDATE.format('vm_pool'),(etree.tostring(et,pretty_print=False),args.vmState,args.vmId,))
db.commit()
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment