<div dir="ltr"><div><div><div>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 :<br><br>def run_command(container, command, env={}):<br> env['LANG'] = "C.UTF-8"<br> env['TERM'] = "xterm"<br> return container.attach_wait(<br> lxc.attach_run_command, command,<br> extra_env_vars=env, env_policy=lxc.LXC_ATTACH_CLEAR_ENV)<br><br>run_command(container, ["env", ])<br><br></div>returns :<br><br>PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin<br>container=lxc<br>TERM=xterm<br>LANG=C.UTF-8<br><br></div>I managed to find it thanks to its use in <a href="http://bazaar.launchpad.net/~ubuntu-lxc/lxc/steam-lxc/view/head:/steam-lxc">http://bazaar.launchpad.net/~ubuntu-lxc/lxc/steam-lxc/view/head:/steam-lxc</a><br><br></div><div>I couldn't find it documented, have I bad googling skills or is there a place where I could have found it ?<br></div><div>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 ?<br><br></div><div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 14, 2017 at 6:32 PM, Benoit Barthelet <span dir="ltr"><<a href="mailto:benoit.barthelet@gmail.com" target="_blank">benoit.barthelet@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">thanks, that is exactly what I wanted !<br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Tue, Mar 14, 2017 at 1:37 PM, Stéphane Graber <span dir="ltr"><<a href="mailto:stgraber@ubuntu.com" target="_blank">stgraber@ubuntu.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><span>On Tue, Mar 14, 2017 at 11:22:26AM +0100, Benoit Barthelet wrote:<br>
> ultimately I'd like to do this in python:<br>
><br>
> lxc-attach --clear-env -n lxcws -v TERM=xterm<br>
><br>
> So far I managed to clear the env variables doing the following, but I<br>
> didn't find a way to pass the TERM env variable.<br>
><br>
> container.attach_wait(lxc.atta<wbr>ch_run_command,<br>
> ["apt-get", "dist-upgrade", "-y"],<br>
> env_policy=lxc.LXC_ATTACH_CLE<wbr>AR_ENV)<br>
<br>
</span>Easiest I think is to use your own attach function instead of the<br>
generic lxc.attach_run_command. In that function you can then set<br>
os.environ as you want and call subprocess to run the command you want.<br>
<br>
In the lxc-ci code we have something like this:<br>
<br>
def execute(self, cmd, cwd="/"):<br>
def run_command(args):<br>
cmd, cwd = args<br>
<br>
os.environ['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin<wbr>'<br>
os.environ['HOME'] = '/root'<br>
if "env" in config and "proxy" in config['env']:<br>
os.environ["DEBIAN_FRONTEND"] = "noninteractive"<br>
os.environ['http_proxy'] = config['env']['proxy']<br>
os.environ['https_proxy'] = config['env']['proxy']<br>
<br>
return subprocess.call(cmd, cwd=cwd)<br>
<br>
if isinstance(cmd, str):<br>
pid = self.container.init_pid<br>
cmdpath = "/proc/%d/root/tmp/exec_script<wbr>" % pid<br>
with open(cmdpath, "w+") as fd:<br>
fd.write(cmd)<br>
os.chmod(cmdpath, 0o755)<br>
cmd = ["/tmp/exec_script"]<br>
<br>
print(" ==> Executing: \"%s\" in %s" % (" ".join(cmd), cwd))<br>
return self.container.attach_wait(run<wbr>_command,<br>
(cmd, cwd),<br>
env_policy=lxc.LXC_ATTACH_CLEA<wbr>R_ENV)<br>
<span class="m_-1103414415817315688HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Stéphane Graber<br>
Ubuntu developer<br>
<a href="http://www.ubuntu.com" rel="noreferrer" target="_blank">http://www.ubuntu.com</a><br>
</font></span><br></div></div>______________________________<wbr>_________________<br>
lxc-users mailing list<br>
<a href="mailto:lxc-users@lists.linuxcontainers.org" target="_blank">lxc-users@lists.linuxcontainer<wbr>s.org</a><br>
<a href="http://lists.linuxcontainers.org/listinfo/lxc-users" rel="noreferrer" target="_blank">http://lists.linuxcontainers.o<wbr>rg/listinfo/lxc-users</a><br></blockquote></div><span class=""><br><br clear="all"><br>-- <br><div class="m_-1103414415817315688gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr">benoit barthelet<br><a href="http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE" target="_blank">http://pgp.mit.edu/pks/lookup?<wbr>op=get&search=<wbr>0xF150E01A72F6D2EE</a><br></div></div></div></div>
</span></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr">benoit barthelet<br><a href="http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE" target="_blank">http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE</a><br></div></div></div></div>
</div>