Skip to content

Commit

Permalink
CP-45985: Update static-vdis to python3
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Cheng <stephen.cheng@cloud.com>
  • Loading branch information
stephenchengCloud committed Dec 11, 2023
1 parent a96bdd4 commit 46d755e
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions scripts/static-vdis
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# Common functions for managing statically-attached (ie onboot, without xapi) VDIs

from __future__ import print_function
import sys, os, subprocess, json, urlparse, itertools

import sys, os, subprocess, json, urllib.parse
import os.path
import time
import XenAPI, inventory, xmlrpclib
import XenAPI, inventory, xmlrpc.client

main_dir = "/etc/xensource/static-vdis"

Expand All @@ -22,27 +22,21 @@ def call_volume_plugin(name, command, args):
cmd_args.extend(args)
# on Python >= 3.3 a timout can be set, not on 2.7
# when porting please add a timeout
output = subprocess.check_output(cmd_args)
output = subprocess.check_output(cmd_args, universal_newlines=True)
return json.loads(output)

def call_datapath_plugin(name, command, args):
args = [ xapi_storage_script + "/datapath/" + name + "/" + command, "static-vdis" ] + args
output = subprocess.check_output(args)
output = subprocess.check_output(args, universal_newlines=True)
return json.loads(output)

def read_whole_file(filename):
f = open(filename)
try:
return reduce(lambda x, y: x + y.decode('utf8'), f.readlines(), "").strip()
finally:
f.close()

with open(filename, 'r', encoding='utf-8') as f:
return ''.join(map(str.strip, f.readlines()))

def write_whole_file(filename, contents):
f = open(filename, "w")
try:
f.write(contents.encode('utf8'))
finally:
f.close()
with open(filename, "w", encoding='utf-8') as f:
f.write(contents)

def load(name):
"""Return a dictionary describing a single static VDI"""
Expand Down Expand Up @@ -70,7 +64,7 @@ def wait_for_corosync_quorum():
cmd_args = ['xcli', 'diagnostics', '--json', 'static-vdis']
quorate = False
while not quorate:
output = subprocess.check_output(cmd_args)
output = subprocess.check_output(cmd_args, universal_newlines=True)
output_map = json.loads(output)
quorate = output_map.get('is_quorate')
if not quorate:
Expand All @@ -85,17 +79,17 @@ def check_clusterstack(ty):
def sr_attach(ty, device_config):
check_clusterstack(ty)

args = [arg for (k,v) in device_config.iteritems()
args = [arg for (k,v) in device_config.items()
for arg in ["--configuration", k, v]]
return call_volume_plugin(ty, "SR.attach", args)

def list():
def list_vdis():
all = []
try:
all = os.listdir(main_dir)
except:
pass
return map(load, all)
return list(map(load, all))

def fresh_name():
all = []
Expand Down Expand Up @@ -131,7 +125,7 @@ def to_string_list(d):
return l

def add(session, vdi_uuid, reason):
for existing in list():
for existing in list_vdis():
if existing['vdi-uuid'] == vdi_uuid:
if existing['delete-next-boot'] == "True":
# Undo the 'delete-next-boot' flag to reinstitute
Expand Down Expand Up @@ -159,7 +153,7 @@ def add(session, vdi_uuid, reason):

sm = None
all_sm = session.xenapi.SM.get_all_records()
for sm_ref in all_sm.keys():
for sm_ref in list(all_sm.keys()):
if all_sm[sm_ref]['type'] == ty:
sm = all_sm[sm_ref]
break
Expand Down Expand Up @@ -195,12 +189,12 @@ def add(session, vdi_uuid, reason):
fresh = fresh_name()
path = main_dir + "/" + fresh
os.mkdir(path)
for key in data.keys():
for key in list(data.keys()):
write_whole_file(path + "/" + key, data[key])

def delete(vdi_uuid):
found = False
for existing in list():
for existing in list_vdis():
if existing['vdi-uuid'] == vdi_uuid:
found = True
path = main_dir + "/" + existing['id']
Expand All @@ -215,7 +209,7 @@ def delete(vdi_uuid):
# Copied by util.py
def doexec(args, inputtext=None):
"""Execute a subprocess, then return its return code, stdout and stderr"""
proc = subprocess.Popen(args,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
proc = subprocess.Popen(args,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True, universal_newlines=True)
(stdout,stderr) = proc.communicate(inputtext)
rc = proc.returncode
return (rc,stdout,stderr)
Expand All @@ -227,41 +221,42 @@ def connect_smapiv1_nbd(params_nbd):


def call_backend_attach(driver, config):
args = map(lambda arg: arg.encode('utf8'), [driver, config])
args = [arg.encode('utf8') for arg in [driver, config]]
xml = doexec(args)
if xml[0] != 0:
raise Exception("SM_BACKEND_FAILURE(%d, %s, %s)" % xml)
xmlrpc = xmlrpclib.loads(xml[1])
xml_rpc = xmlrpc.client.loads(xml[1])

if 'params_nbd' in xmlrpc[0][0]:
if 'params_nbd' in xml_rpc[0][0]:
# Prefer NBD if available
return connect_smapiv1_nbd(xmlrpc[0][0]['params_nbd'])
return connect_smapiv1_nbd(xml_rpc[0][0]['params_nbd'])

try:
path = xmlrpc[0][0]['params']
path = xml_rpc[0][0]['params']
except:
path = xmlrpc[0][0]
path = xml_rpc[0][0]
return path

def call_backend_detach(driver, config):
params = xmlrpclib.loads(config)[0][0]
params = xmlrpc.client.loads(config)[0][0]
params['command'] = 'vdi_detach_from_config'
config = xmlrpclib.dumps(tuple([params]), params['command'])
config = xmlrpc.client.dumps(tuple([params]), params['command'])
xml = doexec([ driver, config ])
if xml[0] != 0:
raise Exception("SM_BACKEND_FAILURE(%d, %s, %s)" % xml)
xmlrpc = xmlrpclib.loads(xml[1])
xml_rpc = xmlrpc.client.loads(xml[1])
try:
res = xmlrpc[0][0]['params']
res = xml_rpc[0][0]['params']
except:
res = xmlrpc[0][0]
res = xml_rpc[0][0]
return res

def connect_nbd(path, exportname):
return subprocess.check_output(
['/opt/xensource/libexec/nbd_client_manager.py', 'connect',
'--path', path,
'--exportname', exportname]).strip()
'--exportname', exportname],
universal_newlines=True).strip()

def disconnect_nbd_device(nbd_device):
subprocess.check_call(
Expand All @@ -283,7 +278,7 @@ def parse_nbd_uri(uri):

def attach(vdi_uuid):
found = False
for existing in list():
for existing in list_vdis():
if existing['vdi-uuid'] == vdi_uuid:
found = True
if not('path' in existing):
Expand All @@ -305,7 +300,7 @@ def attach(vdi_uuid):
vol_key = read_whole_file(d + "/volume-key")
vol_uri = read_whole_file(d + "/volume-uri")
multipath = json.loads(read_whole_file(d + "/multipath"))
scheme = urlparse.urlparse(vol_uri).scheme
scheme = urllib.parse.urlparse(vol_uri).scheme
# Set the multipath flag if required
if multipath:
with open(MULTIPATH_FLAG, 'a'):
Expand All @@ -332,7 +327,7 @@ def attach(vdi_uuid):

def detach(vdi_uuid):
found = False
for existing in list():
for existing in list_vdis():
if existing['vdi-uuid'] == vdi_uuid:
if not ('disk' in existing):
return
Expand All @@ -350,7 +345,7 @@ def detach(vdi_uuid):
volume_plugin = read_whole_file(d + "/volume-plugin")
vol_key = read_whole_file(d + "/volume-key")
vol_uri = read_whole_file(d + "/volume-uri")
scheme = urlparse.urlparse(vol_uri).scheme
scheme = urllib.parse.urlparse(vol_uri).scheme
call_datapath_plugin(scheme, "Datapath.deactivate", [ vol_uri, "0" ])
call_datapath_plugin(scheme, "Datapath.detach", [ vol_uri, "0" ])
os.unlink(d + "/disk")
Expand All @@ -373,7 +368,7 @@ if __name__ == "__main__":
usage()

if sys.argv[1] == "list" and len(sys.argv) == 2:
for i in list ():
for i in list_vdis():
for line in to_string_list(i):
print(line)
print()
Expand Down

0 comments on commit 46d755e

Please sign in to comment.