Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / cifs_mount.py
1 """
2 Mount cifs clients.  Unmount when finished.
3 """
4 import contextlib
5 import logging
6 import os
7
8 from teuthology import misc as teuthology
9 from teuthology.orchestra import run
10
11 log = logging.getLogger(__name__)
12
13 @contextlib.contextmanager
14 def task(ctx, config):
15     """
16     Mount/unmount a cifs client.
17
18     The config is optional and defaults to mounting on all clients. If
19     a config is given, it is expected to be a list of clients to do
20     this operation on.
21
22     Example that starts smbd and mounts cifs on all nodes::
23
24         tasks:
25         - ceph:
26         - samba:
27         - cifs-mount:
28         - interactive:
29
30     Example that splits smbd and cifs:
31
32         tasks:
33         - ceph:
34         - samba: [samba.0]
35         - cifs-mount: [client.0]
36         - ceph-fuse: [client.1]
37         - interactive:
38
39     Example that specifies the share name:
40
41         tasks:
42         - ceph:
43         - ceph-fuse:
44         - samba:
45             samba.0:
46                 cephfuse: "{testdir}/mnt.0"
47         - cifs-mount:
48             client.0:
49                 share: cephfuse
50
51     :param ctx: Context
52     :param config: Configuration
53     """
54     log.info('Mounting cifs clients...')
55
56     if config is None:
57         config = dict(('client.{id}'.format(id=id_), None)
58                   for id_ in teuthology.all_roles_of_type(ctx.cluster, 'client'))
59     elif isinstance(config, list):
60         config = dict((name, None) for name in config)
61
62     clients = list(teuthology.get_clients(ctx=ctx, roles=config.keys()))
63
64     from .samba import get_sambas
65     samba_roles = ['samba.{id_}'.format(id_=id_) for id_ in teuthology.all_roles_of_type(ctx.cluster, 'samba')]
66     sambas = list(get_sambas(ctx=ctx, roles=samba_roles))
67     (ip, _) = sambas[0][1].ssh.get_transport().getpeername()
68     log.info('samba ip: {ip}'.format(ip=ip))
69
70     for id_, remote in clients:
71         mnt = os.path.join(teuthology.get_testdir(ctx), 'mnt.{id}'.format(id=id_))
72         log.info('Mounting cifs client.{id} at {remote} {mnt}...'.format(
73                 id=id_, remote=remote,mnt=mnt))
74
75         remote.run(
76             args=[
77                 'mkdir',
78                 '--',
79                 mnt,
80                 ],
81             )
82
83         rolestr = 'client.{id_}'.format(id_=id_)
84         unc = "ceph"
85         log.info("config: {c}".format(c=config))
86         if config[rolestr] is not None and 'share' in config[rolestr]:
87             unc = config[rolestr]['share']
88
89         remote.run(
90             args=[
91                 'sudo',
92                 'mount',
93                 '-t',
94                 'cifs',
95                 '//{sambaip}/{unc}'.format(sambaip=ip, unc=unc),
96                 '-o',
97                 'username=ubuntu,password=ubuntu',
98                 mnt,
99                 ],
100             )
101
102         remote.run(
103             args=[
104                 'sudo',
105                 'chown',
106                 'ubuntu:ubuntu',
107                 '{m}/'.format(m=mnt),
108                 ],
109             )
110
111     try:
112         yield
113     finally:
114         log.info('Unmounting cifs clients...')
115         for id_, remote in clients:
116             remote.run(
117                 args=[
118                     'sudo',
119                     'umount',
120                     mnt,
121                     ],
122                 )
123         for id_, remote in clients:
124             while True:
125                 try:
126                     remote.run(
127                         args=[
128                             'rmdir', '--', mnt,
129                             run.Raw('2>&1'),
130                             run.Raw('|'),
131                             'grep', 'Device or resource busy',
132                             ],
133                         )
134                     import time
135                     time.sleep(1)
136                 except Exception:
137                     break