~gandelman-a/pyjuju/bootstrap-fix

« back to all changes in this revision

Viewing changes to ensemble/control/debug_hooks.py

  • Committer: Gustavo Niemeyer
  • Date: 2011-06-14 16:29:50 UTC
  • mfrom: (239.5.9 debug-hook-fixes)
  • Revision ID: gustavo@niemeyer.net-20110614162950-v30h1hdele6gs3mm
Merged debug-hook-fixes branch [r=hazmat,bcsaller,jimbaker]

This branch fixes a number of problems in the debug-hooks functionality,
and switches to using tmux for solving some of them. For instance:

- joined and departed hooks are now valid
- Sometimes screen was being fired with the ubuntu user
- Sometimes screen was firing two independent sessions with the same name
- The exit handler was only called on HUP
- If the hook.sh shell died for whatever reason, it would hang forever

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
2
Command for debugging hooks on a service unit.
3
3
"""
 
4
import base64
4
5
import os
5
6
 
6
7
from twisted.internet.defer import inlineCallbacks, returnValue
34
35
def validate_hooks(client, unit_state, hook_names):
35
36
 
36
37
    # Assemble a list of valid hooks for the formula.
37
 
    valid_hooks = []
 
38
    valid_hooks = ["start", "stop", "install"]
38
39
    service_manager = ServiceStateManager(client)
39
40
    endpoints = yield service_manager.get_relation_endpoints(
40
41
        unit_state.service_name)
41
42
    endpoint_names = [ep.relation_name for ep in endpoints]
42
43
    for endpoint_name in endpoint_names:
43
 
        valid_hooks.append("%s-relation-changed" % endpoint_name)
44
 
        valid_hooks.append("%s-relation-broken" % endpoint_name)
45
 
    valid_hooks.extend(["start", "stop", "install"])
 
44
        valid_hooks.extend([
 
45
            endpoint_name + "-relation-joined",
 
46
            endpoint_name + "-relation-changed",
 
47
            endpoint_name + "-relation-departed",
 
48
            endpoint_name + "-relation-broken",
 
49
        ])
46
50
 
47
51
    # Verify the debug names.
48
52
    for hook_name in hook_names:
91
95
        "Enabling hook debug on unit (%r)..." % options.unit_name)
92
96
    yield unit.enable_hook_debug(options.hook_names)
93
97
 
94
 
    # Connect via ssh and start screen.
 
98
    # Connect via ssh and start tmux.
95
99
    options.log.info("Connecting to remote machine...")
96
 
    yield os.system(
97
 
        "ssh -t ubuntu@%s sudo byobu -xRS %s-hook-debug -t shell" % (
98
 
        ip_address, options.unit_name.replace("/", "-")))
 
100
 
 
101
    # Encode the script as base64 so that we can deliver it with a single
 
102
    # ssh command while still retaining standard input on the terminal fd.
 
103
    script = SCRIPT.replace("{unit_name}", options.unit_name)
 
104
    script_b64 = base64.encodestring(script).replace("\n", "").strip()
 
105
    cmd = '"F=`mktemp`; echo %s | base64 -d > \$F; . \$F"' % script_b64
 
106
 
 
107
    # Yield to facilitate testing.
 
108
    yield os.system("ssh -t ubuntu@%s 'sudo /bin/bash -c %s'" % (ip_address, cmd))
99
109
 
100
110
    options.log.info("Debug session ended.")
101
111
    # Ends hook debugging.
102
112
    yield client.close()
 
113
 
 
114
 
 
115
SCRIPT = r"""
 
116
# Wait for tmux to be installed.
 
117
while [ ! -f /usr/bin/tmux ]; do
 
118
    sleep 1
 
119
done
 
120
 
 
121
# Create a sane configuration (no green bars, please!)
 
122
[ -f ~/.tmux.conf ] || cat > ~/.tmux.conf <<END
 
123
 
 
124
# Status bar
 
125
set-option -g status-bg black
 
126
set-option -g status-fg white
 
127
 
 
128
set-window-option -g window-status-current-bg red
 
129
set-window-option -g window-status-current-attr bright
 
130
 
 
131
set-option -g status-right ''
 
132
 
 
133
# Panes
 
134
set-option -g pane-border-fg white
 
135
set-option -g pane-active-border-fg white
 
136
 
 
137
# Monitor activity on windows
 
138
set-window-option -g monitor-activity on
 
139
 
 
140
# Screen bindings, since people are more familiar with that.
 
141
set-option -g prefix C-a
 
142
bind C-a last-window
 
143
bind a send-key C-a
 
144
 
 
145
bind | split-window -h
 
146
bind - split-window -v
 
147
 
 
148
# Fix CTRL-PGUP/PGDOWN for vim
 
149
set-window-option -g xterm-keys on
 
150
 
 
151
# Prevent ESC key from adding delay and breaking Vim's ESC > arrow key
 
152
set-option -s escape-time 0
 
153
 
 
154
END
 
155
 
 
156
tmux new-session -d -s {unit_name} 2> /dev/null || true
 
157
tmux attach -t {unit_name}
 
158
"""