Merge lp:~cjwatson/launchpad/utilities-run-as into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 18500
Proposed branch: lp:~cjwatson/launchpad/utilities-run-as
Merge into: lp:launchpad
Diff against target: 40 lines (+36/-0)
1 file modified
utilities/run-as (+36/-0)
To merge this branch: bzr merge lp:~cjwatson/launchpad/utilities-run-as
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+332290@code.launchpad.net

Commit message

Add a utility to make it easier to run Launchpad code inside "lxc exec".

Description of the change

I've had this lying around as ~/bin/lp-su for a while, but it seems more generally useful. Running the test suite in a running container becomes:

  lxc exec "$container_name" -- $PWD/utilities/run-as $USER bin/with-xvfb bin/test -vvc

... which is still a hefty pile of adverbs, but isn't too bad.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'utilities/run-as'
--- utilities/run-as 1970-01-01 00:00:00 +0000
+++ utilities/run-as 2017-10-16 11:23:24 +0000
@@ -0,0 +1,36 @@
1#! /usr/bin/python
2#
3# Copyright 2017 Canonical Ltd. This software is licensed under the
4# GNU Affero General Public License version 3 (see the file LICENSE).
5
6"""Run a command as another user and with the proper environment.
7
8This can only be run as root, and makes it easier to run Launchpad code
9inside "lxc exec". (sudo in xenial breaks without a tty, so cannot be used
10here.)
11"""
12
13from __future__ import absolute_import, print_function, unicode_literals
14
15import os
16import pwd
17import resource
18import sys
19
20
21user = sys.argv[1]
22pw = pwd.getpwnam(user)
23os.setresgid(pw.pw_gid, pw.pw_gid, pw.pw_gid)
24os.setresuid(pw.pw_uid, pw.pw_uid, pw.pw_uid)
25os.environ["HOME"] = pw.pw_dir
26os.environ["SHELL"] = pw.pw_shell
27os.environ["USER"] = user
28os.environ["LOGNAME"] = user
29os.chdir(os.path.dirname(os.path.dirname(__file__)))
30# The current default is 1048576, which is rather over the top and causes
31# GPGME-based tests to be extremely slow. See:
32# https://lists.gnupg.org/pipermail/gnupg-devel/2017-September/033086.html
33soft_nofile, hard_nofile = resource.getrlimit(resource.RLIMIT_NOFILE)
34if hard_nofile > 4096:
35 resource.setrlimit(resource.RLIMIT_NOFILE, (min(soft_nofile, 4096), 4096))
36os.execvp(sys.argv[2], sys.argv[2:])