Skip to content

Commit 2ec1f34

Browse files
committed
feat(ctld): make socket operations async
1 parent b128cab commit 2ec1f34

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

src/middlewared/middlewared/ctld.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import socket
2+
import asyncio
23
from urllib.parse import quote
34

45

@@ -7,36 +8,36 @@ class CTLDControl:
78

89
def __init__(self):
910
self.sock = None
11+
self.loop = asyncio.get_event_loop()
1012

11-
def open(self):
13+
async def open(self):
1214
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
13-
self.sock.connect(self.socket)
15+
self.sock.setblocking(False)
16+
await self.loop.sock_connect(self.sock, self.socket)
1417

1518
def close(self):
1619
self.sock.close()
1720
self.sock = None
1821

19-
def cmd(self, command, id, args=[]):
22+
async def cmd(self, command, id, args=[]):
2023
line = ' '.join([command, quote(id), *args])
2124
line = line.encode()
2225

23-
if self.sock.send(line) != len(line):
24-
raise RuntimeError("Failed writing to socket")
25-
26-
ret = self.sock.recv(4096)
26+
await self.loop.sock_sendall(self.sock, line)
27+
ret = await self.loop.sock_recv(self.sock, 4096)
2728
ret = ret.decode()
2829

2930
if not ret.startswith("OK"):
3031
raise RuntimeError("ctld-control: {}".format(ret))
3132

32-
def __enter__(self):
33-
self.open()
33+
async def __aenter__(self):
34+
await self.open()
3435
return self
3536

36-
def __exit__(self, exc_type, exc_value, traceback):
37+
async def __aexit__(self, exc_type, exc_value, traceback):
3738
self.close()
3839

39-
def auth_group_set(self, id, type_, auths):
40+
async def auth_group_set(self, id, type_, auths):
4041
"""
4142
Parameters
4243
----------
@@ -54,12 +55,12 @@ def auth_group_set(self, id, type_, auths):
5455

5556
args += ["auth={}".format(quote(i)) for i in a]
5657

57-
return self.cmd("auth-group-set", id, args)
58+
await self.cmd("auth-group-set", id, args)
5859

59-
def auth_group_del(self, id):
60-
return self.cmd("auth-group-del", id, [])
60+
async def auth_group_del(self, id):
61+
await self.cmd("auth-group-del", id, [])
6162

62-
def lun_set(self, id, ctl_lun, path, blocksize, serial, device_id, size, pblocksize=None, **options):
63+
async def lun_set(self, id, ctl_lun, path, blocksize, serial, device_id, size, pblocksize=None, **options):
6364
args=[
6465
"ctl-lun={}".format(ctl_lun),
6566
"path={}".format(quote(path)),
@@ -77,12 +78,12 @@ def lun_set(self, id, ctl_lun, path, blocksize, serial, device_id, size, pblocks
7778
for key, value in options.items():
7879
args.append("option={}={}".format(key, quote(str(value))))
7980

80-
return self.cmd("lun-set", id, args)
81+
await self.cmd("lun-set", id, args)
8182

82-
def lun_del(self, id):
83-
return self.cmd("lun-del", id)
83+
async def lun_del(self, id):
84+
await self.cmd("lun-del", id)
8485

85-
def target_add(self, id, alias=None, pgs=[], ag=None):
86+
async def target_add(self, id, alias=None, pgs=[], ag=None):
8687
args = []
8788

8889
if alias is not None:
@@ -96,10 +97,10 @@ def target_add(self, id, alias=None, pgs=[], ag=None):
9697
if ag is not None:
9798
args.append('auth-group={}'.format(ag))
9899

99-
return self.cmd("target-add", id, args)
100+
await self.cmd("target-add", id, args)
100101

101-
def target_del(self, id):
102-
return self.cmd("target-del", id)
102+
async def target_del(self, id):
103+
await self.cmd("target-del", id)
103104

104-
def target_set_luns(self, id, luns=[]):
105-
return self.cmd("target-set-lun", id, luns)
105+
async def target_set_luns(self, id, luns=[]):
106+
await self.cmd("target-set-lun", id, luns)

src/middlewared/middlewared/plugins/iscsi.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,15 @@ async def ctld_set(self, tag):
468468
for i in await self.middleware.call(
469469
'datastore.query', 'services.iscsitargetauthcredential', [['iscsi_target_auth_tag', '=', tag]]
470470
)]
471-
with ctld.CTLDControl() as c:
471+
async with ctld.CTLDControl() as c:
472472
for grp in await self.middleware.call(
473473
'datastore.query', 'services.iscsitargetgroups', [['iscsi_target_authgroup', '=', tag]]
474474
):
475475
type_ = grp['iscsi_target_authtype']
476476
if type_ == 'CHAP Mutual':
477477
type_ = 'chap-mutual'
478478
agname = 'ag4tg%d_%d' % (grp['iscsi_target']['id'], grp['id'])
479-
c.auth_group_set(agname, type_, auths)
479+
await c.auth_group_set(agname, type_, auths)
480480

481481

482482
class iSCSITargetExtentModel(sa.Model):
@@ -1088,8 +1088,8 @@ async def ctld_set(self, e):
10881088
if e['ro']:
10891089
options['readonly'] = "on"
10901090

1091-
with ctld.CTLDControl() as c:
1092-
c.lun_set(
1091+
async with ctld.CTLDControl() as c:
1092+
await c.lun_set(
10931093
e['name'],
10941094
e['id'] - 1,
10951095
path,
@@ -1103,8 +1103,8 @@ async def ctld_set(self, e):
11031103

11041104
@private
11051105
async def ctld_del(self, name):
1106-
with ctld.CTLDControl() as c:
1107-
c.lun_del(name)
1106+
async with ctld.CTLDControl() as c:
1107+
await c.lun_del(name)
11081108

11091109

11101110
class iSCSITargetAuthorizedInitiatorModel(sa.Model):
@@ -1606,15 +1606,15 @@ async def ctld_add(self, target):
16061606
'agname': 'ag4tg%d_%d' % (target['id'], grp['id']),
16071607
})
16081608

1609-
with ctld.CTLDControl() as c:
1609+
async with ctld.CTLDControl() as c:
16101610
pgs = []
16111611

16121612
for i in groups:
16131613
type_ = i['grp']['iscsi_target_authtype']
16141614
if type_ == 'CHAP Mutual':
16151615
type_ = 'chap-mutual'
16161616

1617-
c.auth_group_set(i['agname'], type_.lower(), i['auths'])
1617+
await c.auth_group_set(i['agname'], type_.lower(), i['auths'])
16181618

16191619
pgtag = i['grp']['iscsi_target_portalgroup']['iscsi_target_portal_tag']
16201620

@@ -1625,19 +1625,19 @@ async def ctld_add(self, target):
16251625
pgs.append('pg%d:%s' % (pgtag, i['agname']))
16261626

16271627
# create target
1628-
c.target_add(name, alias, pgs)
1628+
await c.target_add(name, alias, pgs)
16291629

16301630
@private
16311631
async def ctld_del(self, target, tgg):
16321632
name = await self.target_name(target)
16331633

1634-
with ctld.CTLDControl() as c:
1635-
c.target_del(name)
1634+
async with ctld.CTLDControl() as c:
1635+
await c.target_del(name)
16361636

16371637
for grp in tgg:
16381638
agname = 'ag4tg%d_%d' % (target['id'], grp['id'])
16391639

1640-
c.auth_group_del(agname)
1640+
await c.auth_group_del(agname)
16411641

16421642
@private
16431643
async def ctld_set_luns(self, target):
@@ -1673,8 +1673,8 @@ async def ctld_set_luns(self, target):
16731673
lundef = ''
16741674
luns.append('lun{}{}'.format(lunidx, lundef))
16751675

1676-
with ctld.CTLDControl() as c:
1677-
c.target_set_luns(name, luns)
1676+
async with ctld.CTLDControl() as c:
1677+
await c.target_set_luns(name, luns)
16781678

16791679

16801680
class iSCSITargetToExtentModel(sa.Model):

0 commit comments

Comments
 (0)