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
|
# Copyright (c) 2019 SUSE Linux GmbH. All rights reserved.
#
# This file is part of kiwi.
#
# kiwi 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.
#
# kiwi 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 kiwi. If not, see <http://www.gnu.org/licenses/>
#
import os
import re
# project
from kiwi.command import Command
from kiwi.path import Path
from kiwi.utils.rpm import Rpm
class RpmDataBase:
"""
**Setup RPM database configuration**
"""
def __init__(self, root_dir, macro_file=None):
self.rpmdb_host = Rpm()
self.rpmdb_image = Rpm(root_dir, macro_file)
self.root_dir = root_dir
def has_rpm(self):
"""
Check if rpmdb binary was found in root_dir to indicate
that the rpm system is present.
"""
rpm_bin = Path.which(
'rpmdb', root_dir=self.root_dir, access_mode=os.X_OK
)
if not rpm_bin:
return False
return True
def rebuild_database(self):
"""
Rebuild image rpm database taking current macro setup into account
"""
Command.run(
['chroot', self.root_dir, 'rpmdb', '--rebuilddb']
)
def init_database(self):
Command.run(
['rpm', '--root', self.root_dir, '--initdb']
)
def set_macro_from_string(self, setting):
"""
Setup given macro setting in image rpm configuration.
The following format for the setting is expected:
macro_base_key_name%macro_value.
If this expression is not matched the macro setup will be
skipped. Please note the macro_base_key_name includes
the name of the macro as rpm expects it exlcuding the
leading '%' character.
Also note macro defintions must happen before calling
set_database_* methods in order to become effective
:param string setting: macro key and value as one string
"""
match = re.match('(.*)%(.*)', setting)
if match:
self.rpmdb_image.set_config_value(
match.group(1), match.group(2)
)
def write_config(self):
self.rpmdb_image.write_config()
def import_signing_key_to_image(self, key):
"""
Import the given signing key to the image rpm database
"""
Command.run(
[
'rpm', '--root', self.root_dir, '--import', key,
'--dbpath', self.rpmdb_host.expand_query('%_dbpath')
]
)
def set_database_to_host_path(self):
"""
Setup dbpath to point to the host rpm dbpath configuration
and write the configuration file
"""
self.rpmdb_image.set_config_value(
'_dbpath', self.rpmdb_host.get_query('_dbpath')
)
self.rpmdb_image.write_config()
def link_database_to_host_path(self):
"""
Create a link from host database location to image database
location. The link is only created if both locations differ
and if the host database location does not exist.
"""
rpm_host_dbpath = self.rpmdb_host.expand_query('%_dbpath')
rpm_image_dbpath = self.rpmdb_image.expand_query('%_dbpath')
if rpm_host_dbpath != rpm_image_dbpath:
root_rpm_host_dbpath = os.path.normpath(
os.sep.join([self.root_dir, rpm_host_dbpath])
)
if not os.path.exists(root_rpm_host_dbpath):
Path.create(os.path.dirname(root_rpm_host_dbpath))
host_to_root = ''.join(
'../' for i in os.path.dirname(
rpm_host_dbpath
).lstrip(os.sep).split(os.sep)
)
Command.run(
[
'ln', '-s', os.path.normpath(
os.sep.join([host_to_root, rpm_image_dbpath])
), root_rpm_host_dbpath
]
)
def set_database_to_image_path(self):
"""
Setup dbpath to point to the image rpm dbpath configuration
Rebuild the database such that it gets moved to the standard
path and delete KIWI's custom macro setup
"""
self.rpmdb_image.wipe_config()
rpm_image_dbpath = self.rpmdb_image.expand_query('%_dbpath')
rpm_host_dbpath = self.rpmdb_host.expand_query('%_dbpath')
if rpm_image_dbpath != rpm_host_dbpath:
root_rpm_image_dbpath = os.path.normpath(
os.sep.join([self.root_dir, rpm_image_dbpath])
)
root_rpm_host_dbpath = os.path.normpath(
os.sep.join([self.root_dir, rpm_host_dbpath])
)
if os.path.islink(root_rpm_host_dbpath):
self.rebuild_database()
return
if os.path.islink(root_rpm_image_dbpath):
os.unlink(root_rpm_image_dbpath)
else:
Path.wipe(root_rpm_image_dbpath)
self.rpmdb_image.set_config_value(
'_dbpath', rpm_host_dbpath
)
self.rpmdb_image.set_config_value(
'_dbpath_rebuild', self.rpmdb_image.get_query('_dbpath')
)
self.rpmdb_image.write_config()
self.rebuild_database()
self.rpmdb_image.wipe_config()
root_rpm_alternatives = os.sep.join(
[root_rpm_host_dbpath, 'alternatives']
)
if os.path.exists(root_rpm_alternatives):
Command.run(
['mv', root_rpm_alternatives, root_rpm_image_dbpath]
)
Path.wipe(root_rpm_host_dbpath)
|