summaryrefslogtreecommitdiffstats
path: root/qa/tasks/cifs_mount.py
blob: b282b0b7dfb52ff0b5a763bcc32fa664d6420161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Mount cifs clients.  Unmount when finished.
"""
import contextlib
import logging
import os

from teuthology import misc as teuthology
from teuthology.orchestra import run

log = logging.getLogger(__name__)

@contextlib.contextmanager
def task(ctx, config):
    """
    Mount/unmount a cifs client.

    The config is optional and defaults to mounting on all clients. If
    a config is given, it is expected to be a list of clients to do
    this operation on.

    Example that starts smbd and mounts cifs on all nodes::

        tasks:
        - ceph:
        - samba:
        - cifs-mount:
        - interactive:

    Example that splits smbd and cifs:

        tasks:
        - ceph:
        - samba: [samba.0]
        - cifs-mount: [client.0]
        - ceph-fuse: [client.1]
        - interactive:

    Example that specifies the share name:

        tasks:
        - ceph:
        - ceph-fuse:
        - samba:
            samba.0:
                cephfuse: "{testdir}/mnt.0"
        - cifs-mount:
            client.0:
                share: cephfuse

    :param ctx: Context
    :param config: Configuration
    """
    log.info('Mounting cifs clients...')

    if config is None:
        config = dict(('client.{id}'.format(id=id_), None)
                  for id_ in teuthology.all_roles_of_type(ctx.cluster, 'client'))
    elif isinstance(config, list):
        config = dict((name, None) for name in config)

    clients = list(teuthology.get_clients(ctx=ctx, roles=config.keys()))

    from .samba import get_sambas
    samba_roles = ['samba.{id_}'.format(id_=id_) for id_ in teuthology.all_roles_of_type(ctx.cluster, 'samba')]
    sambas = list(get_sambas(ctx=ctx, roles=samba_roles))
    (ip, _) = sambas[0][1].ssh.get_transport().getpeername()
    log.info('samba ip: {ip}'.format(ip=ip))

    for id_, remote in clients:
        mnt = os.path.join(teuthology.get_testdir(ctx), 'mnt.{id}'.format(id=id_))
        log.info('Mounting cifs client.{id} at {remote} {mnt}...'.format(
                id=id_, remote=remote,mnt=mnt))

        remote.run(
            args=[
                'mkdir',
                '--',
                mnt,
                ],
            )

        rolestr = 'client.{id_}'.format(id_=id_)
        unc = "ceph"
        log.info("config: {c}".format(c=config))
        if config[rolestr] is not None and 'share' in config[rolestr]:
            unc = config[rolestr]['share']

        remote.run(
            args=[
                'sudo',
                'mount',
                '-t',
                'cifs',
                '//{sambaip}/{unc}'.format(sambaip=ip, unc=unc),
                '-o',
                'username=ubuntu,password=ubuntu',
                mnt,
                ],
            )

        remote.run(
            args=[
                'sudo',
                'chown',
                'ubuntu:ubuntu',
                '{m}/'.format(m=mnt),
                ],
            )

    try:
        yield
    finally:
        log.info('Unmounting cifs clients...')
        for id_, remote in clients:
            remote.run(
                args=[
                    'sudo',
                    'umount',
                    mnt,
                    ],
                )
        for id_, remote in clients:
            while True:
                try:
                    remote.run(
                        args=[
                            'rmdir', '--', mnt,
                            run.Raw('2>&1'),
                            run.Raw('|'),
                            'grep', 'Device or resource busy',
                            ],
                        )
                    import time
                    time.sleep(1)
                except Exception:
                    break