Class: Nanite::Admin

Inherits:
Object show all
Defined in:
lib/nanite/admin.rb

Overview

This is a Rack app for nanite-admin. You need to have an async capable version of Thin installed for this to work. See bin/nanite-admin for install instructions.

Constant Summary collapse

AsyncResponse =
[-1, {}, []].freeze

Instance Method Summary collapse

Constructor Details

#initialize(mapper) ⇒ Admin

Returns a new instance of Admin.



8
9
10
# File 'lib/nanite/admin.rb', line 8

def initialize(mapper)
  @mapper = mapper
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nanite/admin.rb', line 14

def call(env)
  req = Rack::Request.new(env)
  if cmd = req.params['command']
    @command = cmd
    @selection = req.params['type'] if req.params['type']
    
    options = {}
    case @selection
    when 'least_loaded', 'random', 'all', 'rr'
      options[:selector] = @selection
    else
      options[:target] = @selection
    end

    @mapper.request(cmd, req.params['payload'], options) do |response|
      env['async.callback'].call [200, {'Content-Type' => 'text/html'}, [layout(ul(response))]]
    end
    AsyncResponse
  else
    [200, {'Content-Type' => 'text/html'}, layout]
  end
end

#layout(content = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nanite/admin.rb', line 55

def layout(content=nil)
  %Q{
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns='http://www.w3.org/1999/xhtml'>
      <head>
        <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
        <meta content='en' http-equiv='Content-Language' />
        <meta content='Engineyard' name='author' />
        <title>Nanite Control Tower</title>

        <!-- Google AJAX Libraries API -->
        <script src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
          google.load("jquery", "1");
        </script>

        <script type="text/javascript">
        $(document).ready(function(){

          // set the focus to the payload field
          $("#payload").focus();

        });
        </script>

        <style>
          body {margin: 0; font-family: verdana; background-color: #fcfcfc;}
          ul {margin: 0; padding: 0; margin-left: 10px}
          li {list-style-type: none; margin-bottom: 6px}
          li .nanite {font-weight: bold; font-size: 12px}
          li .response {padding: 8px}
          h1, h2, h3 {margin-top: none; padding: none; margin-left: 40px;}
          h1 {font-size: 22px; margin-top: 40px; margin-bottom: 30px; border-bottom: 1px solid #ddd; padding-bottom: 6px;
            margin-right: 40px}
          h2 {font-size: 16px;}
          h3 {margin-left: 0; font-size: 14px}
          .section {border: 1px solid #ccc; background-color: #fefefe; padding: 10px; margin: 20px 40px; padding: 20px;
            font-size: 14px}
          #footer {text-align: center; color: #AAA; font-size: 12px}
        </style>

      </head>

      <body>
        <div id="header">
          <h1>Nanite Control Tower</h1>
        </div>

        <h2>#{@mapper.options[:vhost]}</h2>
        <div class="section">
          <form method="post" action="/">
            <input type="hidden" value="POST" name="_method"/>

              <label>Send</label>
              <select name="type">
                <option #{@selection == 'least_loaded' ? 'selected="true"' : ''} value="least_loaded">the least loaded nanite</option>
                <option #{@selection == 'random' ? 'selected="true"' : ''} value="random">a random nanite</option>
                <option #{@selection == 'all' ? 'selected="true"' : ''} value="all">all nanites</option>
                <option #{@selection == 'rr' ? 'selected="true"' : ''} value="rr">a nanite chosen by round robin</option>
                #{@mapper.cluster.nanites.map {|k,v| "<option #{@selection == k ? 'selected="true"' : ''} value='#{k}'>#{k}</option>" }.join}
              </select>

              <label>providing service</label>
              #{services}

              <label>the payload</label>
              <input type="text" class="text" name="payload" id="payload"/>

              <input type="submit" class="submit" value="Go!" name="submit"/>
          </form>

          #{"<h3>Responses</h3>" if content}
          #{content}
        </div>

        <h2>Running nanites</h2>
        <div class="section">
          #{"No nanites online." if @mapper.cluster.nanites.size == 0}
          <ul>
            #{@mapper.cluster.nanites.map {|k,v| "<li>identity : #{k}<br />load : #{v[:status]}<br />services : #{v[:services].inspect}</li>" }.join}
          </ul>
        </div>
        <div id="footer">
          Nanite #{Nanite::VERSION}
          <br />
          &copy; 2009 a bunch of random geeks
        </div>
      </body>
    </html>
  }
end

#servicesObject



37
38
39
40
41
42
43
44
# File 'lib/nanite/admin.rb', line 37

def services
  buf = "<select name='command'>"
  @mapper.cluster.nanites.map{|n,s| s[:services] }.flatten.each do |srv|
    buf << "<option value='#{srv}' #{@command == srv ? 'selected="true"' : ''}>#{srv}</option>"
  end
  buf << "</select>"
  buf
end

#ul(hash) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/nanite/admin.rb', line 46

def ul(hash)
  buf = "<ul>"
  hash.each do |k,v|
    buf << "<li><div class=\"nanite\">#{k}:</div><div class=\"response\">#{v.inspect}</div></li>"
  end
  buf << "</ul>"
  buf
end