Added an option to not escalate privileges on tito build --install

When using `tito build --install` on a system without `sudo`, such as
inside of a Linux container build, the current implementation fails.
Furthermore, if the user is running `tito build --install` with the
correct level of privilege, it is not necessary to escalate privileges
further for the installation step.

This patch adds the `--dont-escalate-privileges` flag to `tito build`
and defaults it to `True`, which keeps the behavior backwards compat-
ible. Users will want to use this flag when building RPMs inside of a
container or when running `tito build` with the requisite permissions
for installing in the first place.

Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
This commit is contained in:
Steve Kuznetsov 2017-01-24 15:39:02 -05:00
parent bd21c5f551
commit b537fe0607
No known key found for this signature in database
GPG key ID: 366E054B30FC03A2
3 changed files with 18 additions and 6 deletions

View file

@ -72,6 +72,8 @@ class BuilderBase(object):
self.offline = self._get_optional_arg(kwargs, 'offline', False)
self.auto_install = self._get_optional_arg(kwargs, 'auto_install',
False)
self.escalate_privileges = self._get_optional_arg(kwargs, 'escalate',
True)
self.scl = self._get_optional_arg(args, 'scl', [None])[0] or \
self._get_optional_arg(kwargs, 'scl', '')
@ -306,7 +308,8 @@ class BuilderBase(object):
print
reinstall = self.package_manager.is_installed(self.project_name, self.build_version)
cmd = self.package_manager.install(do_install, reinstall=reinstall, auto=True, offline=True)
cmd = self.package_manager.install(do_install, reinstall=reinstall, auto=True, offline=True,
escalate=self.escalate_privileges)
print("%s" % cmd)
try:
run_command_print(cmd)
@ -1248,8 +1251,9 @@ def package_manager():
class Rpm(object):
def install(self, packages, **kwargs):
return "sudo rpm -U --force %s" % ' '.join(packages)
def install(self, packages, escalate=True, **kwargs):
escalation_cmd = "sudo" if escalate else ""
return "%s rpm -U --force %s" % (escalation_cmd, ' '.join(packages))
def builddep(self, spec):
raise NotImplementedError
@ -1270,13 +1274,14 @@ class Rpm(object):
class Dnf(Rpm):
def install(self, packages, reinstall=False, auto=False, offline=False, **kwargs):
def install(self, packages, reinstall=False, auto=False, offline=False, escalate=True, **kwargs):
action = "reinstall" if reinstall else "install"
args = list(filter(lambda x: x, [
"-C" if offline else None,
"-y" if auto else None,
]))
cmd = "sudo dnf %s %s" % (action, " ".join(args + packages))
escalation_cmd = "sudo" if escalate else ""
cmd = "%s dnf %s %s" % (escalation_cmd, action, " ".join(args + packages))
return " ".join(cmd.split())
def builddep(self, spec):
@ -1286,7 +1291,7 @@ class Dnf(Rpm):
class Yum(Rpm):
def install(self, packages, **kwargs):
# Not the sexiest implementation, but very short
return Dnf().install(packages, **kwargs).replace("sudo dnf", "sudo yum")
return Dnf().install(packages, **kwargs).replace("dnf", "yum")
def builddep(self, spec):
return "yum-builddep %s" % spec

View file

@ -307,6 +307,10 @@ class BuildModule(BaseCliModule):
action="store_true", default=False,
help="Install any binary rpms being built. (WARNING: " +
"uses sudo rpm -Uvh --force)")
self.parser.add_option("--no-sudo", dest="escalate",
action="store_false", default=True,
help="Don't escalate privileges when installing. Use when " +
"running this command with required privileges.")
self.parser.add_option("--dist", dest="dist", metavar="DISTTAG",
help="Dist tag to apply to srpm and/or rpm. (i.e. .el5)")

View file

@ -145,6 +145,9 @@ Install any binary RPMs being built.
WARNING: uses `sudo rpm -Uvh --force`
--no-sudo::
Don't escalate privileges when installing. Use when running this command with required privileges.
--dist='DISTTAG'::
Apply 'DISTTAG' to srpm and/or rpm. (e.g., ".el5")