[lxc-users] is there a lxc-attach -v switch equivalent in lxc python bindings ?

Benoit Barthelet benoit.barthelet at gmail.com
Wed Mar 15 09:49:05 UTC 2017


apologies, this was well described here :
https://lists.linuxcontainers.org/pipermail/lxc-devel/2013-August/004898.html

On Wed, Mar 15, 2017 at 9:15 AM, Benoit Barthelet <
benoit.barthelet at gmail.com> wrote:

> it seems like there's a built-in kwarg, extra_env_vars, that does the -v
> switch job of lxc-attach in fact, I wrapped it in a function here but it
> could be written without :
>
> def run_command(container, command, env={}):
>     env['LANG'] = "C.UTF-8"
>     env['TERM'] = "xterm"
>     return container.attach_wait(
>             lxc.attach_run_command, command,
>             extra_env_vars=env, env_policy=lxc.LXC_ATTACH_CLEAR_ENV)
>
> run_command(container, ["env", ])
>
> returns :
>
> PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
> container=lxc
> TERM=xterm
> LANG=C.UTF-8
>
> I managed to find it thanks to its use in http://bazaar.launchpad.net/~
> ubuntu-lxc/lxc/steam-lxc/view/head:/steam-lxc
>
> I couldn't find it documented, have I bad googling skills or is there a
> place where I could have found it ?
> If there isn't I'm ok to try document it, is there a dedicated place, wiki
> for that or it's not worth the effort ?
>
>
>
> On Tue, Mar 14, 2017 at 6:32 PM, Benoit Barthelet <
> benoit.barthelet at gmail.com> wrote:
>
>> thanks, that is exactly what I wanted !
>>
>> On Tue, Mar 14, 2017 at 1:37 PM, Stéphane Graber <stgraber at ubuntu.com>
>> wrote:
>>
>>> On Tue, Mar 14, 2017 at 11:22:26AM +0100, Benoit Barthelet wrote:
>>> > ultimately I'd like to do this in python:
>>> >
>>> > lxc-attach --clear-env -n lxcws -v TERM=xterm
>>> >
>>> > So far I managed to clear the env variables doing the following, but I
>>> > didn't find a way to pass the TERM env variable.
>>> >
>>> > container.attach_wait(lxc.attach_run_command,
>>> >                       ["apt-get", "dist-upgrade", "-y"],
>>> >                       env_policy=lxc.LXC_ATTACH_CLEAR_ENV)
>>>
>>> Easiest I think is to use your own attach function instead of the
>>> generic lxc.attach_run_command. In that function you can then set
>>> os.environ as you want and call subprocess to run the command you want.
>>>
>>> In the lxc-ci code we have something like this:
>>>
>>>     def execute(self, cmd, cwd="/"):
>>>         def run_command(args):
>>>             cmd, cwd = args
>>>
>>>             os.environ['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
>>>             os.environ['HOME'] = '/root'
>>>             if "env" in config and "proxy" in config['env']:
>>>                 os.environ["DEBIAN_FRONTEND"] = "noninteractive"
>>>                 os.environ['http_proxy'] = config['env']['proxy']
>>>                 os.environ['https_proxy'] = config['env']['proxy']
>>>
>>>             return subprocess.call(cmd, cwd=cwd)
>>>
>>>         if isinstance(cmd, str):
>>>             pid = self.container.init_pid
>>>             cmdpath = "/proc/%d/root/tmp/exec_script" % pid
>>>             with open(cmdpath, "w+") as fd:
>>>                 fd.write(cmd)
>>>             os.chmod(cmdpath, 0o755)
>>>             cmd = ["/tmp/exec_script"]
>>>
>>>         print(" ==> Executing: \"%s\" in %s" % (" ".join(cmd), cwd))
>>>         return self.container.attach_wait(run_command,
>>>                                           (cmd, cwd),
>>>                                           env_policy=lxc.LXC_ATTACH_CLEA
>>> R_ENV)
>>>
>>>
>>>
>>> --
>>> Stéphane Graber
>>> Ubuntu developer
>>> http://www.ubuntu.com
>>>
>>> _______________________________________________
>>> lxc-users mailing list
>>> lxc-users at lists.linuxcontainers.org
>>> http://lists.linuxcontainers.org/listinfo/lxc-users
>>>
>>
>>
>>
>> --
>> benoit barthelet
>> http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE
>>
>
>
>
> --
> benoit barthelet
> http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE
>



-- 
benoit barthelet
http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxcontainers.org/pipermail/lxc-users/attachments/20170315/7db6db0a/attachment.html>


More information about the lxc-users mailing list