_repository.py 23.7 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# coding: utf-8
# OceanBase Deploy.
# Copyright (C) 2021 OceanBase
#
# This file is part of OceanBase Deploy.
#
# OceanBase Deploy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OceanBase Deploy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OceanBase Deploy.  If not, see <https://www.gnu.org/licenses/>.


from __future__ import absolute_import, division, print_function

import os
import sys
R
Rongfeng Fu 已提交
25
import time
O
oceanbase-admin 已提交
26 27
import hashlib
from glob import glob
F
v1.6.0  
frf12 已提交
28 29
from multiprocessing import cpu_count
from multiprocessing.pool import Pool
O
oceanbase-admin 已提交
30

R
Rongfeng Fu 已提交
31
from _rpm import Package, PackageInfo, Version
O
oceanbase-admin 已提交
32 33 34
from _arch import getBaseArch
from tool import DirectoryUtil, FileUtil, YamlLoader
from _manager import Manager
R
Rongfeng Fu 已提交
35
from _plugin import InstallPlugin
O
oceanbase-admin 已提交
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


class LocalPackage(Package):

    class RpmObject(object):

        def __init__(self, headers, files):
            self.files = files
            self.opens = {}
            self.headers = headers

        def __exit__(self, *arg, **kwargs):
            for path in self.opens:
                self.opens[path].close()

        def __enter__(self):
            self.__exit__()
            self.opens = {}
            return self

        def extractfile(self, name):
            if name not in self.files:
                raise KeyError("member %s could not be found" % name)
            path = self.files[name]
            if path not in self.opens:
                self.opens[path] = open(path, 'rb')
            return self.opens[path]

    def __init__(self, path, name, version, files, release=None, arch=None):
        self.name = name
R
Rongfeng Fu 已提交
66 67
        self.set_version(version)
        self.set_release(release if release else time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())))
O
oceanbase-admin 已提交
68 69 70
        self.md5 = None
        self.arch = arch if arch else getBaseArch()
        self.headers = {}
R
Rongfeng Fu 已提交
71
        self.files = self.get_all_files(files)
O
oceanbase-admin 已提交
72 73 74
        self.path = path
        self.package()

R
Rongfeng Fu 已提交
75 76 77
    def __hash__(self):
        return hash(self.path)

R
Rongfeng Fu 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    @staticmethod
    def get_all_files(source_files):
        files = {}
        for src_path, target_path in source_files.items():
            if not os.path.isdir(target_path) or os.path.islink(target_path):
                files[src_path] = target_path
            else:
                files[src_path+'/'] = target_path
                for fp in LocalPackage.list_dir(target_path):
                    files[os.path.join(src_path, os.path.relpath(fp, target_path))] = fp
        return files

    @staticmethod
    def list_dir(path):
        files = []
        for fn in os.listdir(path):
            fp = os.path.join(path, fn)
            if not os.path.isdir(fp) or os.path.islink(fp):
                files.append(fp)
            else:
                files += LocalPackage.list_dir(fp)
        return files

O
oceanbase-admin 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    def package(self):
        count = 0
        dirnames = []
        filemd5s = []
        filemodes = []
        basenames = []
        dirindexes = []
        filelinktos = []
        dirnames_map = {}
        m_sum = hashlib.md5()
        for src_path in self.files:
            target_path = self.files[src_path]
            dirname, basename = os.path.split(src_path)
            if dirname not in dirnames_map:
                dirnames.append(dirname)
                dirnames_map[dirname] = count
                count += 1
R
Rongfeng Fu 已提交
118 119 120 121 122 123 124 125
            if basename:
                basenames.append(basename)
                dirindexes.append(dirnames_map[dirname])
                if os.path.islink(target_path):
                    filemd5s.append('')
                    filelinktos.append(os.readlink(target_path))
                    filemodes.append(-24065)
                else:
F
v1.6.0  
frf12 已提交
126
                    m_value = FileUtil.checksum(target_path)
R
Rongfeng Fu 已提交
127 128 129 130
                    m_sum.update(m_value)
                    filemd5s.append(m_value)
                    filelinktos.append('')
                    filemodes.append(os.stat(target_path).st_mode)
O
oceanbase-admin 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
        self.headers = {
            'dirnames': dirnames,
            'filemd5s': filemd5s,
            'filemodes': filemodes,
            'basenames': basenames,
            'dirindexes': dirindexes,
            'filelinktos': filelinktos,
        }
        self.md5 = m_sum.hexdigest()

    def open(self):
        return self.RpmObject(self.headers, self.files)


F
v1.6.0  
frf12 已提交
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
class ExtractFileInfo(object):

    def __init__(self, src_path, target_path, mode):
        self.src_path = src_path
        self.target_path = target_path
        self.mode = mode


class ParallerExtractWorker(object):

    def __init__(self, pkg, files, stdio=None):
        self.pkg = pkg
        self.files = files
        self.stdio = stdio
    
    @staticmethod
    def extract(worker):
        with worker.pkg.open() as rpm:
            for info in worker.files:
                if os.path.exists(info.target_path):
                    continue
                fd = rpm.extractfile(info.src_path)
                with FileUtil.open(info.target_path, 'wb', stdio=worker.stdio) as f:
                    FileUtil.copy_fileobj(fd, f)
                if info.mode != 0o744:
                    os.chmod(info.target_path, info.mode)


class ParallerExtractor(object):

F
v1.6.0  
frf12 已提交
175
    MAX_PARALLER = cpu_count() if cpu_count() else 8
F
v1.6.0  
frf12 已提交
176 177 178 179 180 181 182 183 184

    def __init__(self, pkg, files, stdio=None):
        self.pkg = pkg
        self.files = files
        self.stdio = stdio

    def extract(self):
        workers = []
        file_num = len(self.files)
F
v1.6.0  
frf12 已提交
185 186 187
        paraler = int(min(self.MAX_PARALLER, file_num))
        size = min(100, int(file_num / paraler))
        size = int(max(10, size))
F
v1.6.0  
frf12 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        index = 0
        while index < file_num:
            p_index = index + size
            workers.append(ParallerExtractWorker(
                self.pkg,
                self.files[index:p_index],
                stdio=self.stdio
            ))
            index = p_index
        
        pool = Pool(processes=paraler)
        try:
            results = pool.map(ParallerExtractWorker.extract, workers)
            for r in results:
                if not r:
                    return False
        except KeyboardInterrupt:
            if pool:
                pool.close()
                pool = None
        finally:
            pool and pool.close()


R
Rongfeng Fu 已提交
212
class Repository(PackageInfo):
O
oceanbase-admin 已提交
213 214 215 216 217
    
    _DATA_FILE = '.data'

    def __init__(self, name, repository_dir, stdio=None):
        self.repository_dir = repository_dir
R
Rongfeng Fu 已提交
218
        super(Repository, self).__init__(name, None, None, None, None)
O
oceanbase-admin 已提交
219 220
        self.stdio = stdio
        self._load()
R
Rongfeng Fu 已提交
221 222 223 224
    
    @property
    def hash(self):
        return self.md5
O
oceanbase-admin 已提交
225 226

    def __str__(self):
F
v1.5.0  
frf12 已提交
227
        return '%s-%s-%s-%s' % (self.name, self.version, self.release, self.hash)
O
oceanbase-admin 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

    def __hash__(self):
        return hash(self.repository_dir)

    def is_shadow_repository(self):
        if os.path.exists(self.repository_dir):
            return os.path.islink(self.repository_dir)
        return False

    @property
    def data_file_path(self):
        path = os.readlink(self.repository_dir) if os.path.islink(self.repository_dir) else self.repository_dir
        return os.path.join(path, Repository._DATA_FILE)

    def bin_list(self, plugin):
        files = []
        if self.version and self.hash:
R
Rongfeng Fu 已提交
245
            for file_item in plugin.file_list(self):
R
Rongfeng Fu 已提交
246
                if file_item.type == InstallPlugin.FileItemType.BIN:
O
oceanbase-admin 已提交
247 248 249 250 251 252
                    files.append(os.path.join(self.repository_dir, file_item.target_path))
        return files

    def file_list(self, plugin):
        files = []
        if self.version and self.hash:
R
Rongfeng Fu 已提交
253
            for file_item in plugin.file_list(self):
R
Rongfeng Fu 已提交
254 255 256 257 258
                path = os.path.join(self.repository_dir, file_item.target_path)
                if file_item.type == InstallPlugin.FileItemType.DIR:
                    files += DirectoryUtil.list_dir(path)
                else:
                    files.append(path)
O
oceanbase-admin 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271
        return files

    def file_check(self, plugin):
        for file_path in self.file_list(plugin):
            if not os.path.exists(file_path):
                return False
        return True

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.version == other.version and self.hash == other.hash
        if isinstance(other, dict):
            return self.version == other['version'] and self.hash == other['hash']
R
Rongfeng Fu 已提交
272
        return super(Repository, self).__eq__(other)
O
oceanbase-admin 已提交
273 274 275 276 277

    def _load(self):
        try:
            with open(self.data_file_path, 'r') as f:
                data = YamlLoader().load(f)
R
Rongfeng Fu 已提交
278 279 280 281
                self.set_version(data.get('version'))
                self.set_release(data.get('release'))
                self.md5 = data.get('hash')
                self.arch = data.get('arch')
R
Rongfeng Fu 已提交
282
                self.install_time = data.get('install_time', 0)
O
oceanbase-admin 已提交
283 284 285 286 287 288 289 290 291 292 293 294
        except:
            pass

    def _parse_path(self):
        if self.is_shadow_repository():
            path = os.readlink(self.repository_dir)
        else:
            path = self.repository_dir
        path = path.strip('/')
        path, _hash = os.path.split(path)
        path, version = os.path.split(path)
        if not self.version:
R
Rongfeng Fu 已提交
295
            self.set_version(version)
O
oceanbase-admin 已提交
296 297

    def _dump(self):
R
Rongfeng Fu 已提交
298
        data = {'version': self.version, 'hash': self.hash, 'release': self.release, 'arch': self.arch}
R
Rongfeng Fu 已提交
299 300
        if self.install_time:
            data['install_time'] = self.install_time
O
oceanbase-admin 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313
        try:
            with open(self.data_file_path, 'w') as f:
                YamlLoader().dump(data, f)
            return True
        except:
            self.stdio and getattr(self.stdio, 'exception', print)('dump %s to %s failed' % (data, self.data_file_path))
        return False

    def load_pkg(self, pkg, plugin):
        if self.is_shadow_repository():
            self.stdio and getattr(self.stdio, 'print', '%s is a shadow repository' % self)
            return False
        hash_path = os.path.join(self.repository_dir, '.hash')
F
v1.6.0  
frf12 已提交
314
        if self.hash == pkg.md5 and self.file_check(plugin) and self.install_time > plugin.check_value:
O
oceanbase-admin 已提交
315 316 317 318
            return True
        self.clear()
        try:
            with pkg.open() as rpm:
R
Rongfeng Fu 已提交
319
                file_map = plugin.file_map(pkg)
R
Rongfeng Fu 已提交
320 321 322 323 324 325 326 327 328 329
                need_dirs = {}
                need_files = {}
                for src_path in file_map:
                    file_item = file_map[src_path]
                    if file_item.type == InstallPlugin.FileItemType.DIR:
                        if not src_path.endswith('/'):
                            src_path += '/'
                        need_dirs[src_path] = file_item.target_path
                    else:
                        need_files[src_path] = file_item.target_path
O
oceanbase-admin 已提交
330 331 332 333 334 335 336 337
                files = {}
                links = {}
                dirnames = rpm.headers.get("dirnames")
                basenames = rpm.headers.get("basenames")
                dirindexes = rpm.headers.get("dirindexes")
                filelinktos = rpm.headers.get("filelinktos")
                filemd5s = rpm.headers.get("filemd5s")
                filemodes = rpm.headers.get("filemodes")
R
Rongfeng Fu 已提交
338 339
                dirs = sorted(need_dirs.keys(), reverse=True)
                format_str = lambda s: s.decode(errors='replace') if isinstance(s, bytes) else s
O
oceanbase-admin 已提交
340
                for i in range(len(basenames)):
R
Rongfeng Fu 已提交
341 342 343 344 345 346 347
                    if not filemd5s[i] and not filelinktos[i]:
                        continue
                    dir_path = format_str(dirnames[dirindexes[i]])
                    if not dir_path.startswith('./'):
                        dir_path = '.%s' % dir_path
                    file_name = format_str(basenames[i])
                    path = os.path.join(dir_path, file_name)
O
oceanbase-admin 已提交
348
                    files[path] = i
R
Rongfeng Fu 已提交
349 350 351
                    if path not in need_files:
                        for n_dir in need_dirs:
                            if path.startswith(n_dir):
R
Rongfeng Fu 已提交
352
                                need_files[path] = os.path.join(need_dirs[n_dir], path[len(n_dir):])
R
Rongfeng Fu 已提交
353
                                break
F
v1.6.0  
frf12 已提交
354 355
                
                need_extract_files = []
R
Rongfeng Fu 已提交
356
                for src_path in need_files:
O
oceanbase-admin 已提交
357 358
                    if src_path not in files:
                        raise Exception('%s not found in packge' % src_path)
R
Rongfeng Fu 已提交
359 360 361
                    target_path = os.path.join(self.repository_dir, need_files[src_path])
                    if os.path.exists(target_path):
                        return
O
oceanbase-admin 已提交
362 363
                    idx = files[src_path]
                    if filemd5s[idx]:
F
v1.6.0  
frf12 已提交
364 365 366 367 368
                        need_extract_files.append(ExtractFileInfo(
                            src_path,
                            target_path,
                            filemodes[idx] & 0x1ff
                        ))
O
oceanbase-admin 已提交
369 370 371 372
                    elif filelinktos[idx]:
                        links[target_path] = filelinktos[idx]
                    else:
                        raise Exception('%s is directory' % src_path)
F
v1.6.0  
frf12 已提交
373 374
                
                ParallerExtractor(pkg, need_extract_files, stdio=self.stdio).extract()
R
Rongfeng Fu 已提交
375

O
oceanbase-admin 已提交
376
                for link in links:
R
Rongfeng Fu 已提交
377
                    self.stdio and getattr(self.stdio, 'verbose', print)('link %s to %s' % (links[link], link))
O
oceanbase-admin 已提交
378
                    os.symlink(links[link], link)
R
Rongfeng Fu 已提交
379
                for n_dir in need_dirs:
R
Rongfeng Fu 已提交
380
                    path = os.path.join(self.repository_dir, need_dirs[n_dir])
R
Rongfeng Fu 已提交
381 382
                    if not os.path.exists(path) and n_dir[:-1] in dirnames:
                        DirectoryUtil.mkdir(path)
R
Rongfeng Fu 已提交
383
                    if not os.path.isdir(path):
R
Rongfeng Fu 已提交
384
                        raise Exception('%s in %s is not dir.' % (pkg.path, n_dir))
R
Rongfeng Fu 已提交
385 386 387 388
            self.set_version(pkg.version)
            self.set_release(pkg.release)
            self.md5 = pkg.md5
            self.arch = pkg.arch
R
Rongfeng Fu 已提交
389
            self.install_time = time.time()
O
oceanbase-admin 已提交
390 391 392 393 394 395 396 397 398 399
            if self._dump():
                return True
            else:
                self.clear()
        except:
            self.stdio and getattr(self.stdio, 'exception', print)('failed to extract file from %s' % pkg.path)
            self.clear()
        return False

    def clear(self):
R
Rongfeng Fu 已提交
400 401 402 403
        if os.path.exists(self.repository_dir):
            return DirectoryUtil.rm(self.repository_dir, self.stdio) and DirectoryUtil.mkdir(self.repository_dir, stdio=self.stdio)
        return True

O
oceanbase-admin 已提交
404

R
Rongfeng Fu 已提交
405 406 407 408 409 410 411 412 413 414 415 416
class RepositoryVO(object):

    def __init__(self, name, version, release, arch, md5, path, tags=[]):
        self.name = name
        self.version = version
        self.release = release
        self.arch = arch
        self.md5 = md5
        self.path = path
        self.tags = tags


O
oceanbase-admin 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
class ComponentRepository(object):

    def __init__(self, name, repository_dir, stdio=None):
        self.repository_dir = repository_dir
        self.stdio = stdio
        self.name = name
        DirectoryUtil.mkdir(self.repository_dir, stdio=stdio)

    def get_instance_repositories(self, version):
        repositories = {}
        for tag in os.listdir(self.repository_dir):
            path = os.path.join(self.repository_dir, tag)
            if os.path.islink(path):
                continue
            repository = Repository(self.name, path, self.stdio)
            if repository.hash:
                repositories[repository.hash] = repository
        return repositories

    def get_shadow_repositories(self, version, instance_repositories={}):
        repositories = {}
        for tag in os.listdir(self.repository_dir):
            path = os.path.join(self.repository_dir, tag)
            if not os.path.islink(path):
                continue
            _, md5 = os.path.split(os.readlink(path))
            if md5 in instance_repositories:
                repositories[tag] = instance_repositories[md5]
            else:
                repository = Repository(self.name, path, self.stdio)
                if repository.hash:
                    repositories[repository.hash] = repository
        return repositories

F
v1.5.0  
frf12 已提交
451 452
    def search_repository(self, version=None, tag=None, release=None):
        path_pattern = os.path.join(self.repository_dir, version or '*', tag or '*')
R
Rongfeng Fu 已提交
453
        repository = None
F
v1.5.0  
frf12 已提交
454
        for path in glob(path_pattern):
R
Rongfeng Fu 已提交
455
            n_repository = Repository(self.name, path, self.stdio)
F
v1.5.0  
frf12 已提交
456 457
            if release and release != n_repository.release:
                continue
R
Rongfeng Fu 已提交
458 459 460
            if n_repository.hash and n_repository > repository:
                repository = n_repository
        return repository
O
oceanbase-admin 已提交
461

F
v1.5.0  
frf12 已提交
462 463 464 465 466 467
    def get_repository(self, version=None, tag=None, release=None):
        if version or tag or release:
            return self.search_repository(version=version, tag=tag, release=release)
        else:
            return self.search_repository(tag=self.name) or self.search_repository()

R
Rongfeng Fu 已提交
468 469 470 471
    def get_repositories(self, version=None):
        if not version:
            version = '*'
        repositories = []
F
v1.5.0  
frf12 已提交
472 473
        path_pattern = os.path.join(self.repository_dir, version, '*')
        for path in glob(path_pattern):
R
Rongfeng Fu 已提交
474 475 476
            repository = Repository(self.name, path, self.stdio)
            if repository.hash:
                repositories.append(repository)
R
Rongfeng Fu 已提交
477
        return repositories
O
oceanbase-admin 已提交
478 479 480 481 482 483 484


class RepositoryManager(Manager):

    RELATIVE_PATH = 'repository'
    # repository目录结构为 ./repository/{component_name}/{version}/{tag or hash}

R
Rongfeng Fu 已提交
485
    def __init__(self, home_path, lock_manager=None, stdio=None):
O
oceanbase-admin 已提交
486 487
        super(RepositoryManager, self).__init__(home_path, stdio=stdio)
        self.repositories = {}
F
v1.5.0  
frf12 已提交
488
        self.component_repositories = {}
R
Rongfeng Fu 已提交
489 490 491 492 493 494 495 496 497
        self.lock_manager = lock_manager

    def _lock(self, read_only=False):
        if self.lock_manager:
            if read_only:
                return self.lock_manager.mirror_and_repo_sh_lock()
            else:
                return self.lock_manager.mirror_and_repo_ex_lock()
        return True
O
oceanbase-admin 已提交
498

R
Rongfeng Fu 已提交
499 500 501 502 503 504 505 506 507 508 509
    def _get_repository_vo(self, repository):
        return RepositoryVO(
            repository.name,
            repository.version,
            repository.release,
            repository.arch,
            repository.md5,
            repository.repository_dir,
            []
        )

R
Rongfeng Fu 已提交
510 511
    def get_repositories(self, name, version=None, instance=True):
        repositories = []
F
v1.5.0  
frf12 已提交
512
        for repository in self.get_component_repository(name).get_repositories(version):
R
Rongfeng Fu 已提交
513 514 515 516
            if instance and repository.is_shadow_repository() is False:
                repositories.append(repository)
        return repositories

R
Rongfeng Fu 已提交
517 518
    def get_repositories_view(self, name=None):
        if name:
F
v1.5.0  
frf12 已提交
519
            repositories = self.get_component_repository(name).get_repositories()
R
Rongfeng Fu 已提交
520 521
        else:
            repositories = []
F
v1.5.0  
frf12 已提交
522 523
            path_pattern = os.path.join(self.path, '*')
            for path in glob(path_pattern):
R
Rongfeng Fu 已提交
524
                _, name = os.path.split(path)
F
v1.5.0  
frf12 已提交
525
                repositories += self.get_component_repository(name).get_repositories()
R
Rongfeng Fu 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538

        repositories_vo = {}
        for repository in repositories:
            if repository.is_shadow_repository():
                repository_ist = self.get_instance_repository_from_shadow(repository)
                if repository_ist not in repositories_vo:
                    repositories_vo[repository_ist] = self._get_repository_vo(repository)
                _, tag = os.path.split(repository.repository_dir)
                repositories_vo[repository_ist].tags.append(tag)
            elif repository not in repositories_vo:
                repositories_vo[repository] = self._get_repository_vo(repository)
        return list(repositories_vo.values())

F
v1.5.0  
frf12 已提交
539 540
    def get_component_repository(self, name):
        if name not in self.component_repositories:
R
Rongfeng Fu 已提交
541
            self._lock(True)
R
Rongfeng Fu 已提交
542
            path = os.path.join(self.path, name)
F
v1.5.0  
frf12 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
            self.component_repositories[name] = ComponentRepository(name, path, self.stdio)
        return self.component_repositories[name]

    def get_repository(self, name, version=None, tag=None, release=None, package_hash=None, instance=True):
        self.stdio.verbose(
            "Search repository {name} version: {version}, tag: {tag}, release: {release}, package_hash: {package_hash}".format(
                name=name, version=version, tag=tag, release=release, package_hash=package_hash))
        tag = tag or package_hash
        component_repository = self.get_component_repository(name)
        if version and tag:
            repository_dir = os.path.join(self.path, name, version, tag)
            if repository_dir in self.repositories:
                repository = self.repositories[repository_dir]
            else:
                repository = component_repository.get_repository(version=version, tag=tag, release=release)
O
oceanbase-admin 已提交
558
        else:
F
v1.5.0  
frf12 已提交
559 560 561 562 563 564 565 566 567 568 569
            repository = component_repository.get_repository(version=version, tag=tag, release=release)
        if not repository:
            return None
        else:
            if repository.repository_dir not in self.repositories:
                self.repositories[repository.repository_dir] = repository
            else:
                repository = self.repositories[repository.repository_dir]
            if not self._check_repository_pattern(repository, version=version, release=release, hash=package_hash):
                return None
        self.stdio.verbose("Found repository {}".format(repository))
O
oceanbase-admin 已提交
570 571
        return self.get_instance_repository_from_shadow(repository) if instance else repository

F
v1.5.0  
frf12 已提交
572 573 574 575 576 577 578
    def _check_repository_pattern(self, repository, **kwargs):
        for key in ["version", "release", "hash"]:
            current_value = getattr(repository, key)
            if kwargs.get(key) is not None and current_value != kwargs[key]:
                self.stdio.verbose("repository {} is {}, but {} is required".format(key, current_value, kwargs[key]))
                return False
        return True
O
oceanbase-admin 已提交
579 580 581 582

    def create_instance_repository(self, name, version, _hash):
        path = os.path.join(self.path, name, version, _hash)
        if path not in self.repositories:
R
Rongfeng Fu 已提交
583
            self._lock()
O
oceanbase-admin 已提交
584 585 586 587 588 589 590 591 592
            self._mkdir(path)
            repository = Repository(name, path, self.stdio)
            self.repositories[path] = repository
        return self.repositories[path]

    def get_repository_allow_shadow(self, name, version, tag=None):
        path = os.path.join(self.path, name, version, tag if tag else name)
        if os.path.exists(path):
            if path not in self.repositories:
R
Rongfeng Fu 已提交
593
                self._lock(True)
O
oceanbase-admin 已提交
594 595
                self.repositories[path] = Repository(name, path, self.stdio)
            return self.repositories[path]
F
v1.5.0  
frf12 已提交
596
        repository = Repository(name, path, self.stdio)
R
Rongfeng Fu 已提交
597
        repository.set_version(version)
O
oceanbase-admin 已提交
598 599 600 601 602
        return repository

    def create_tag_for_repository(self, repository, tag, force=False):
        if repository.is_shadow_repository():
            return False
R
Rongfeng Fu 已提交
603
        self._lock()
O
oceanbase-admin 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
        path = os.path.join(self.path, repository.name, repository.version, tag)
        if os.path.exists(path):
            if not os.path.islink(path):
                return False
            src_path = os.readlink(path)
            if os.path.normcase(src_path) == os.path.normcase(repository.repository_dir):
                return True
            if not force:
                return False
            DirectoryUtil.rm(path)
        try:
            os.symlink(repository.repository_dir, path)
            return True
        except:
            pass
        return False

    def get_instance_repository_from_shadow(self, repository):
        if not isinstance(repository, Repository) or not repository.is_shadow_repository():
            return repository
R
Rongfeng Fu 已提交
624
        self._lock(True)
O
oceanbase-admin 已提交
625 626 627 628 629 630 631 632
        try:
            path = os.readlink(repository.repository_dir)
            if path not in self.repositories:
                self.repositories[path] = Repository(repository.name, path, self.stdio)
            return self.repositories[path]
        except:
            pass
        return None