Merge lp:~axwalk/juju-core/lp1212903-debug-hooks-prompt-take2 into lp:~go-bot/juju-core/trunk

Proposed by Andrew Wilkins
Status: Merged
Approved by: Andrew Wilkins
Approved revision: no longer in the source branch.
Merged at revision: 1681
Proposed branch: lp:~axwalk/juju-core/lp1212903-debug-hooks-prompt-take2
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 143 lines (+33/-16)
2 files modified
worker/uniter/debug/server.go (+5/-3)
worker/uniter/debug/server_test.go (+28/-13)
To merge this branch: bzr merge lp:~axwalk/juju-core/lp1212903-debug-hooks-prompt-take2
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+180470@code.launchpad.net

Commit message

debug-hooks: include unit/hook in PS1

Set a useful bash prompt, including the
unit and hook names. The tmux window name
gets truncated, so that's not super useful.

Fixes bug #1212903

https://codereview.appspot.com/13044043/

Description of the change

debug-hooks: include unit/hook in PS1

Set a useful bash prompt, including the
unit and hook names. The tmux window name
gets truncated, so that's not super useful.

Fixes bug #1212903

https://codereview.appspot.com/13044043/

To post a comment you must log in.
Revision history for this message
Andrew Wilkins (axwalk) wrote :

Reviewers: mp+180470_code.launchpad.net,

Message:
Please take a look.

Description:
debug-hooks: include unit/hook in PS1

Set a useful bash prompt, including the
unit and hook names. The tmux window name
gets truncated, so that's not super useful.

Fixes bug #1212903

https://code.launchpad.net/~axwalk/juju-core/lp1212903-debug-hooks-prompt-take2/+merge/180470

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/13044043/

Affected files:
   A [revision details]
   M worker/uniter/debug/server.go
   M worker/uniter/debug/server_test.go

Revision history for this message
Tim Penhey (thumper) wrote :

LGTM - the code looks fine, did you test it interactively?

https://codereview.appspot.com/13044043/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2013/08/16 05:03:44, thumper wrote:
> LGTM - the code looks fine, did you test it interactively?

I did indeed (I probably would've missed the PS1 reset issue otherwise)

https://codereview.appspot.com/13044043/

Revision history for this message
John A Meinel (jameinel) wrote :

It looks a bit odd that we expand the ENV vars early. It might not be an
actual problem, but it does seem strange.

Did you fire multiple concurrent hooks to make sure it didn't overwrite
the prompt in the other terminal?

https://codereview.appspot.com/13044043/diff/1/worker/uniter/debug/server_test.go
File worker/uniter/debug/server_test.go (right):

https://codereview.appspot.com/13044043/diff/1/worker/uniter/debug/server_test.go#newcode203
worker/uniter/debug/server_test.go:203: c.Assert(contents, jc.Contains,
fmt.Sprintf(`PS1="%s:%s %% "`, s.ctx.Unit, hookName))
This looks like you are expecting the PS1 line to read
  "unit-0:db-relation-changed %"

I would have thought it would read:

"$JUJU_UNIT_NAME:$JUJU_HOOK_NAME"

Are we expanding the $* stuff early rather than late?

It would seem like this would cause problems when having multiple hooks
firing,
but maybe we rewrite the shell script on every hook?

https://codereview.appspot.com/13044043/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2013/08/18 12:44:29, jameinel wrote:
> It looks a bit odd that we expand the ENV vars early. It might not be
an actual
> problem, but it does seem strange.

It doesn't matter. Each time a debug hook session is started, the server
creates a new temporary directory. The env.sh script is written to this
temporary directory and sourced only by the client for that session.

> Did you fire multiple concurrent hooks to make sure it didn't
overwrite the
> prompt in the other terminal?

I didn't, but how would you? Aren't hooks serialised?

https://codereview.appspot.com/13044043/diff/1/worker/uniter/debug/server_test.go
> File worker/uniter/debug/server_test.go (right):

https://codereview.appspot.com/13044043/diff/1/worker/uniter/debug/server_test.go#newcode203
> worker/uniter/debug/server_test.go:203: c.Assert(contents,
jc.Contains,
> fmt.Sprintf(`PS1="%s:%s %% "`, s.ctx.Unit, hookName))
> This looks like you are expecting the PS1 line to read
> "unit-0:db-relation-changed %"

> I would have thought it would read:

> "$JUJU_UNIT_NAME:$JUJU_HOOK_NAME"

> Are we expanding the $* stuff early rather than late?

> It would seem like this would cause problems when having multiple
hooks firing,
> but maybe we rewrite the shell script on every hook?

Correct. The current environment is written to a temporary file for each
session, then that script is sourced by the tmux (debug hooks) client
shell.

https://codereview.appspot.com/13044043/

Revision history for this message
William Reade (fwereade) wrote :
Revision history for this message
Go Bot (go-bot) wrote :

Attempt to merge into lp:juju-core failed due to conflicts:

text conflict in worker/uniter/debug/server_test.go

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'worker/uniter/debug/server.go'
2--- worker/uniter/debug/server.go 2013-07-31 05:37:22 +0000
3+++ worker/uniter/debug/server.go 2013-08-20 01:32:40 +0000
4@@ -79,17 +79,19 @@
5 export JUJU_DEBUG=$(mktemp -d)
6 exec > $JUJU_DEBUG/debug.log >&1
7
8+# Set a useful prompt.
9+export PS1="$JUJU_UNIT_NAME:$JUJU_HOOK_NAME % "
10+
11 # Save environment variables and export them for sourcing.
12 FILTER='^\(LS_COLORS\|LESSOPEN\|LESSCLOSE\|PWD\)='
13-env | grep -v $FILTER > $JUJU_DEBUG/env.sh
14-sed -i 's/^/export /' $JUJU_DEBUG/env.sh
15+export | grep -v $FILTER > $JUJU_DEBUG/env.sh
16
17 # Create an internal script which will load the hook environment.
18 cat > $JUJU_DEBUG/hook.sh <<END
19 #!/bin/bash
20 . $JUJU_DEBUG/env.sh
21 echo \$\$ > $JUJU_DEBUG/hook.pid
22-exec /bin/bash
23+exec /bin/bash --noprofile --norc
24 END
25 chmod +x $JUJU_DEBUG/hook.sh
26
27
28=== modified file 'worker/uniter/debug/server_test.go'
29--- worker/uniter/debug/server_test.go 2013-08-19 11:20:02 +0000
30+++ worker/uniter/debug/server_test.go 2013-08-20 01:32:40 +0000
31@@ -4,6 +4,7 @@
32 package debug_test
33
34 import (
35+ "fmt"
36 "io/ioutil"
37 "os"
38 "os/exec"
39@@ -13,7 +14,7 @@
40
41 gc "launchpad.net/gocheck"
42
43- "launchpad.net/juju-core/testing/checkers"
44+ jc "launchpad.net/juju-core/testing/checkers"
45 "launchpad.net/juju-core/worker/uniter/debug"
46 )
47
48@@ -77,7 +78,7 @@
49 session, err = s.ctx.FindSession()
50 c.Assert(session, gc.IsNil)
51 c.Assert(err, gc.NotNil)
52- c.Assert(err, checkers.Satisfies, os.IsNotExist)
53+ c.Assert(err, jc.Satisfies, os.IsNotExist)
54
55 // Hooks file is present, empty.
56 err = ioutil.WriteFile(s.ctx.ClientFileLock(), []byte{}, 0777)
57@@ -86,8 +87,8 @@
58 c.Assert(session, gc.NotNil)
59 c.Assert(err, gc.IsNil)
60 // If session.hooks is empty, it'll match anything.
61- c.Assert(session.MatchHook(""), gc.Equals, true)
62- c.Assert(session.MatchHook("something"), gc.Equals, true)
63+ c.Assert(session.MatchHook(""), jc.IsTrue)
64+ c.Assert(session.MatchHook("something"), jc.IsTrue)
65
66 // Hooks file is present, non-empty
67 err = ioutil.WriteFile(s.ctx.ClientFileLock(), []byte(`hooks: [foo, bar, baz]`), 0777)
68@@ -96,12 +97,12 @@
69 c.Assert(session, gc.NotNil)
70 c.Assert(err, gc.IsNil)
71 // session should only match "foo", "bar" or "baz".
72- c.Assert(session.MatchHook(""), gc.Equals, false)
73- c.Assert(session.MatchHook("something"), gc.Equals, false)
74- c.Assert(session.MatchHook("foo"), gc.Equals, true)
75- c.Assert(session.MatchHook("bar"), gc.Equals, true)
76- c.Assert(session.MatchHook("baz"), gc.Equals, true)
77- c.Assert(session.MatchHook("foo bar baz"), gc.Equals, false)
78+ c.Assert(session.MatchHook(""), jc.IsFalse)
79+ c.Assert(session.MatchHook("something"), jc.IsFalse)
80+ c.Assert(session.MatchHook("foo"), jc.IsTrue)
81+ c.Assert(session.MatchHook("bar"), jc.IsTrue)
82+ c.Assert(session.MatchHook("baz"), jc.IsTrue)
83+ c.Assert(session.MatchHook("foo bar baz"), jc.IsFalse)
84 }
85
86 func (s *DebugHooksServerSuite) TestRunHookExceptional(c *gc.C) {
87@@ -126,7 +127,7 @@
88 expected := time.Now().Add(time.Second)
89 err = session.RunHook("myhook", s.tmpdir, os.Environ())
90 after := time.Now()
91- c.Assert(after, checkers.TimeBetween(expected.Add(-100*time.Millisecond), expected.Add(100*time.Millisecond)))
92+ c.Assert(after, jc.TimeBetween(expected.Add(-100*time.Millisecond), expected.Add(100*time.Millisecond)))
93 c.Assert(err, gc.ErrorMatches, "signal: killed")
94 c.Assert(cmd.Wait(), gc.IsNil)
95 }
96@@ -138,6 +139,8 @@
97 c.Assert(session, gc.NotNil)
98 c.Assert(err, gc.IsNil)
99
100+ const hookName = "myhook"
101+
102 // Run the hook in debug mode with the exit flock held,
103 // and also create the .pid file. We'll populate it with
104 // an invalid PID; this will cause the server process to
105@@ -146,13 +149,13 @@
106 c.Assert(cmd.Start(), gc.IsNil)
107 ch := make(chan error)
108 go func() {
109- ch <- session.RunHook("myhook", s.tmpdir, os.Environ())
110+ ch <- session.RunHook(hookName, s.tmpdir, os.Environ())
111 }()
112 var debugdir os.FileInfo
113 for i := 0; i < 10; i++ {
114 tmpdir, err := os.Open(s.tmpdir)
115 if err != nil {
116- c.Fatalf("Faiked to open $TMPDIR: %s", err)
117+ c.Fatalf("Failed to open $TMPDIR: %s", err)
118 }
119 fi, err := tmpdir.Readdir(-1)
120 if err != nil {
121@@ -176,6 +179,9 @@
122 if debugdir == nil {
123 c.Error("could not find hook.sh")
124 } else {
125+ envsh := filepath.Join(s.tmpdir, debugdir.Name(), "env.sh")
126+ s.verifyEnvshFile(c, envsh, hookName)
127+
128 hookpid := filepath.Join(s.tmpdir, debugdir.Name(), "hook.pid")
129 err = ioutil.WriteFile(hookpid, []byte("not a pid"), 0777)
130 c.Assert(err, gc.IsNil)
131@@ -187,3 +193,12 @@
132 }
133 cmd.Process.Kill() // kill flock
134 }
135+
136+func (s *DebugHooksServerSuite) verifyEnvshFile(c *gc.C, envshPath string, hookName string) {
137+ data, err := ioutil.ReadFile(envshPath)
138+ c.Assert(err, gc.IsNil)
139+ contents := string(data)
140+ c.Assert(contents, jc.Contains, fmt.Sprintf("JUJU_UNIT_NAME=%q", s.ctx.Unit))
141+ c.Assert(contents, jc.Contains, fmt.Sprintf("JUJU_HOOK_NAME=%q", hookName))
142+ c.Assert(contents, jc.Contains, fmt.Sprintf(`PS1="%s:%s %% "`, s.ctx.Unit, hookName))
143+}

Subscribers

People subscribed via source and target branches

to status/vote changes: