summaryrefslogtreecommitdiffstats
path: root/geo-replication/syncdaemon/argsupgrade.py
blob: 18edb6ba5b7ddd75f905688ef3e16b8c69b7dd03 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Converts old style args into new style args

import sys
from argparse import ArgumentParser
import socket
import os

from syncdutils import GsyncdError
from conf import GLUSTERD_WORKDIR


def gethostbyname(hnam):
    """gethostbyname wrapper"""
    try:
        return socket.gethostbyname(hnam)
    except socket.gaierror:
        ex = sys.exc_info()[1]
        raise GsyncdError("failed to resolve %s: %s" %
                          (hnam, ex.strerror))


def slave_url(urldata):
    urldata = urldata.replace("ssh://", "")
    host, vol = urldata.split("::")
    vol = vol.split(":")[0]
    return "%s::%s" % (host, vol)


def init_gsyncd_template_conf():
    path = GLUSTERD_WORKDIR + "/geo-replication/gsyncd_template.conf"
    dname = os.path.dirname(path)
    if not os.path.exists(dname):
        try:
            os.mkdir(dname)
        except OSError:
            pass

    if not os.path.exists(path):
        fd = os.open(path, os.O_CREAT | os.O_RDWR)
        os.close(fd)


def init_gsyncd_session_conf(master, slave):
    slave = slave_url(slave)
    master = master.strip(":")
    slavehost, slavevol = slave.split("::")
    slavehost = slavehost.split("@")[-1]

    # Session Config File
    path = "%s/geo-replication/%s_%s_%s/gsyncd.conf" % (
        GLUSTERD_WORKDIR, master, slavehost, slavevol)

    if os.path.exists(os.path.dirname(path)) and not os.path.exists(path):
        fd = os.open(path, os.O_CREAT | os.O_RDWR)
        os.close(fd)


def init_gsyncd_conf(path):
    dname = os.path.dirname(path)
    if not os.path.exists(dname):
        try:
            os.mkdir(dname)
        except OSError:
            pass

    if os.path.exists(dname) and not os.path.exists(path):
        fd = os.open(path, os.O_CREAT | os.O_RDWR)
        os.close(fd)


def upgrade():
    # Create dummy template conf(empty), hack to avoid glusterd
    # fail when it does stat to check the existence.
    init_gsyncd_template_conf()

    if "--monitor" in sys.argv:
        # python gsyncd.py --path=/bricks/b1
        # --monitor -c gsyncd.conf
        # --iprefix=/var :gv1
        # --glusterd-uuid=f26ac7a8-eb1b-4ea7-959c-80b27d3e43d0
        # f241::gv2
        p = ArgumentParser()
        p.add_argument("master")
        p.add_argument("slave")
        p.add_argument("--glusterd-uuid")
        p.add_argument("-c")
        p.add_argument("--iprefix")
        p.add_argument("--path", action="append")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        # Overwrite the sys.argv after rearrange
        init_gsyncd_session_conf(pargs.master, pargs.slave)
        sys.argv = [
            sys.argv[0],
            "monitor",
            pargs.master.strip(":"),
            slave_url(pargs.slave),
            "--local-node-id",
            pargs.glusterd_uuid
        ]
    elif "--status-get" in sys.argv:
        # -c gsyncd.conf --iprefix=/var :gv1 f241::gv2
        # --status-get --path /bricks/b1
        p = ArgumentParser()
        p.add_argument("master")
        p.add_argument("slave")
        p.add_argument("-c")
        p.add_argument("--path")
        p.add_argument("--iprefix")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        init_gsyncd_session_conf(pargs.master, pargs.slave)

        sys.argv = [
            sys.argv[0],
            "status",
            pargs.master.strip(":"),
            slave_url(pargs.slave),
            "--local-path",
            pargs.path
        ]
    elif "--canonicalize-url" in sys.argv:
        # This can accept multiple URLs and converts each URL to the
        # format ssh://USER@IP:gluster://127.0.0.1:VOLUME
        # This format not used in gsyncd, but added for glusterd compatibility
        p = ArgumentParser()
        p.add_argument("--canonicalize-url", nargs="+")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        for url in pargs.canonicalize_url:
            host, vol = url.split("::")
            host = host.replace("ssh://", "")
            remote_addr = host
            if "@" not in remote_addr:
                remote_addr = "root@" + remote_addr

            user, hname = remote_addr.split("@")

            print("ssh://%s@%s:gluster://127.0.0.1:%s" % (
                user, gethostbyname(hname), vol))

        sys.exit(0)
    elif "--normalize-url" in sys.argv:
        # Adds schema prefix as ssh://
        # This format not used in gsyncd, but added for glusterd compatibility
        p = ArgumentParser()
        p.add_argument("--normalize-url")
        pargs = p.parse_known_args(sys.argv[1:])[0]
        print("ssh://%s" % slave_url(pargs.normalize_url))
        sys.exit(0)
    elif "--config-get-all" in sys.argv:
        #  -c gsyncd.conf --iprefix=/var :gv1 f241::gv2 --config-get-all
        p = ArgumentParser()
        p.add_argument("master")
        p.add_argument("slave")
        p.add_argument("-c")
        p.add_argument("--iprefix")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        init_gsyncd_session_conf(pargs.master, pargs.slave)

        sys.argv = [
            sys.argv[0],
            "config-get",
            pargs.master.strip(":"),
            slave_url(pargs.slave),
            "--show-defaults",
            "--use-underscore"
        ]
    elif "--verify" in sys.argv and "spawning" in sys.argv:
        # Just checks that able to spawn gsyncd or not
        sys.exit(0)
    elif "--slavevoluuid-get" in sys.argv:
        # --slavevoluuid-get f241::gv2
        p = ArgumentParser()
        p.add_argument("--slavevoluuid-get")
        p.add_argument("-c")
        p.add_argument("--iprefix")
        pargs = p.parse_known_args(sys.argv[1:])[0]
        host, vol = pargs.slavevoluuid_get.split("::")

        # Modified sys.argv
        sys.argv = [
            sys.argv[0],
            "voluuidget",
            host,
            vol
        ]
    elif "--config-set-rx" in sys.argv:
        # Not required since default conf is not generated
        # and custom conf generated only when required
        # -c gsyncd.conf --config-set-rx remote-gsyncd
        # /usr/local/libexec/glusterfs/gsyncd . .
        # Touch the gsyncd.conf file and create session
        # directory if required
        p = ArgumentParser()
        p.add_argument("-c", dest="config_file")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        # If not template conf then it is trying to create
        # session config, create a empty file instead
        if pargs.config_file.endswith("gsyncd.conf"):
            init_gsyncd_conf(pargs.config_file)
        sys.exit(0)
    elif "--create" in sys.argv:
        # To update monitor status file
        # --create Created -c gsyncd.conf
        # --iprefix=/var :gv1 f241::gv2
        p = ArgumentParser()
        p.add_argument("--create")
        p.add_argument("master")
        p.add_argument("slave")
        p.add_argument("-c")
        p.add_argument("--iprefix")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        init_gsyncd_session_conf(pargs.master, pargs.slave)

        # Modified sys.argv
        sys.argv = [
            sys.argv[0],
            "monitor-status",
            pargs.master.strip(":"),
            slave_url(pargs.slave),
            pargs.create
        ]
    elif "--config-get" in sys.argv:
        # -c gsyncd.conf --iprefix=/var :gv1 f241::gv2 --config-get pid-file
        p = ArgumentParser()
        p.add_argument("--config-get")
        p.add_argument("master")
        p.add_argument("slave")
        p.add_argument("-c")
        p.add_argument("--iprefix")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        init_gsyncd_session_conf(pargs.master, pargs.slave)

        # Modified sys.argv
        sys.argv = [
            sys.argv[0],
            "config-get",
            pargs.master.strip(":"),
            slave_url(pargs.slave),
            "--only-value",
            "--show-defaults",
            "--name",
            pargs.config_get.replace("_", "-")
        ]
    elif "--config-set" in sys.argv:
        # ignore session-owner
        if "session-owner" in sys.argv:
            sys.exit(0)

        # --path=/bricks/b1  -c gsyncd.conf :gv1 f241::gv2
        # --config-set log_level DEBUG
        p = ArgumentParser()
        p.add_argument("master")
        p.add_argument("slave")
        p.add_argument("--config-set", nargs=2)
        p.add_argument("-c")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        init_gsyncd_session_conf(pargs.master, pargs.slave)

        # Modified sys.argv
        sys.argv = [
            sys.argv[0],
            "config-set",
            pargs.master.strip(":"),
            slave_url(pargs.slave),
            pargs.config_set[0],
            pargs.config_set[1]
        ]
    elif "--config-check" in sys.argv:
        # --config-check georep_session_working_dir
        p = ArgumentParser()
        p.add_argument("--config-check")
        p.add_argument("-c")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        # Modified sys.argv
        sys.argv = [
            sys.argv[0],
            "config-check",
            pargs.config_check.replace("_", "-")
        ]
    elif "--config-del" in sys.argv:
        # -c gsyncd.conf --iprefix=/var :gv1 f241::gv2 --config-del log_level
        p = ArgumentParser()
        p.add_argument("--config-del")
        p.add_argument("master")
        p.add_argument("slave")
        p.add_argument("-c")
        p.add_argument("--iprefix")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        init_gsyncd_session_conf(pargs.master, pargs.slave)

        # Modified sys.argv
        sys.argv = [
            sys.argv[0],
            "config-reset",
            pargs.master.strip(":"),
            slave_url(pargs.slave),
            pargs.config_del.replace("_", "-")
        ]
    elif "--delete" in sys.argv:
        # --delete -c gsyncd.conf --iprefix=/var
        # --path-list=--path=/bricks/b1  :gv1 f241::gv2
        p = ArgumentParser()
        p.add_argument("--reset-sync-time", action="store_true")
        p.add_argument("--path-list")
        p.add_argument("master")
        p.add_argument("slave")
        p.add_argument("--iprefix")
        p.add_argument("-c")
        pargs = p.parse_known_args(sys.argv[1:])[0]

        init_gsyncd_session_conf(pargs.master, pargs.slave)

        paths = pargs.path_list.split("--path=")
        paths = ["--path=%s" % x.strip() for x in paths if x.strip() != ""]

        # Modified sys.argv
        sys.argv = [
            sys.argv[0],
            "delete",
            pargs.master.strip(":"),
            slave_url(pargs.slave)
        ]
        sys.argv += paths

        if pargs.reset_sync_time:
            sys.argv.append("--reset-sync-time")