Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit d42cdfa

Browse files
author
Jay Logue
authored
Merge pull request #378 from openweave/issue/more-simnet-shell-fixes
More fixes to simnet's shell command [skip ci]
2 parents 86687b0 + bb7203a commit d42cdfa

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

src/tools/simnet/simnet.py

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import subprocess
2424
import re
2525
import time
26+
import tempfile
2627
try:
2728
import ipaddress
2829
except ImportError, ex:
@@ -188,20 +189,52 @@ def startShell(self, node, noOverridePrompt=False):
188189

189190
shell = os.path.realpath(os.path.abspath(os.environ['SHELL']))
190191

191-
if os.path.basename(shell) == 'bash' and not noOverridePrompt:
192-
promptOverride = [ '-c', '''exec $SHELL --rcfile <(cat ~/.bashrc; echo '[ "$SN_NO_OVERRIDE_PROMPT" ] || PS1="\e[1m[\$SN_NODE_NAME]\e[0m $PS1"')''' ]
193-
else:
194-
promptOverride = [ ]
192+
user = os.environ['SUDO_USER']
193+
userId = int(os.environ['SUDO_UID'])
194+
195+
nodeEnv = {}
196+
node.setEnvironVars(nodeEnv)
197+
198+
promptOverrideRCFile = None
199+
200+
try:
201+
202+
cmd = [ 'sudo', '-u', user, '--preserve-env=' + ','.join(nodeEnv.keys()), shell ]
203+
204+
if os.path.basename(shell) == 'bash' and not noOverridePrompt:
205+
promptOverrideRCFile = tempfile.NamedTemporaryFile()
206+
207+
promptOverrideRCFile.write(''':
208+
if test -f /etc/bash.bashrc; then
209+
. /etc/bash.bashrc
210+
fi
211+
if test -f ${HOME}/.bashrc; then
212+
. ${HOME}/.bashrc
213+
fi
214+
if [ `tput colors` -gt 0 ]; then
215+
PS1="\[\e[36m\][\$SN_NODE_NAME] \[\e[0m\]$PS1"
216+
else
217+
PS1="[\$SN_NODE_NAME] $PS1"
218+
fi
219+
''')
195220

196-
cmd = [ 'su', ] + promptOverride + [ os.environ['SUDO_USER'] ]
221+
promptOverrideRCFile.flush()
222+
223+
os.fchown(promptOverrideRCFile.fileno(), userId, userId)
224+
225+
cmd += [ '--rcfile', promptOverrideRCFile.name ]
197226

198-
if not node.isHostNode:
199-
cmd = [ 'ip', 'netns', 'exec', node.nsName ] + cmd
227+
if not node.isHostNode:
228+
cmd = [ 'ip', 'netns', 'exec', node.nsName ] + cmd
229+
230+
cmdEnv = os.environ.copy()
231+
cmdEnv.update(nodeEnv)
200232

201-
cmdEnv = os.environ.copy()
202-
node.setEnvironVars(cmdEnv)
203-
204-
subprocess.call(cmd, env=cmdEnv)
233+
subprocess.call(cmd, env=cmdEnv)
234+
235+
finally:
236+
if promptOverrideRCFile != None:
237+
promptOverrideRCFile.close()
205238

206239
def clearAll(self):
207240
verifyRoot()
@@ -1277,11 +1310,11 @@ def setEnvironVars(self, environ):
12771310
weaveConfig = '--node-id %016X ' % self.weaveNodeId
12781311
weaveConfig += '--fabric-id %016X ' % self.weaveFabricId
12791312
if self.wifiInterface:
1280-
weaveConfig += '--default-subnet 1 '
1313+
weaveConfig += '--subnet 1 '
12811314
elif self.threadInterface:
1282-
weaveConfig += '--default-subnet 6 '
1315+
weaveConfig += '--subnet 6 '
12831316
elif self.legacyInterface:
1284-
weaveConfig += '--default-subnet 2 '
1317+
weaveConfig += '--subnet 2 '
12851318
if self.useLwIP:
12861319
weaveConfig += self.getLwIPConfig()
12871320
environ['WEAVE_CONFIG'] = weaveConfig

0 commit comments

Comments
 (0)