Project

General

Profile

Hacking Workbench » History » Version 6

Misha Zatsman, 04/22/2014 08:08 PM

1 1 Tom Clegg
h1. Hacking Workbench
2
3
{{toc}}
4
5
h2. Source tree layout
6
7
Everything is in @/apps/workbench@.
8
9
Key pieces to know about before going much further:
10
11
|/|Usual Rails project layout|
12
|/app/controllers/application_controller.rb|Controller superclass with authentication setup, error handling, and generic CRUD actions|
13
|/app/controllers/*.rb|Actions other than generic CRUD (users#activity, jobs#generate_provenance, ...)|
14
|/app/models/arvados_base.rb|Default Arvados model behavior and ActiveRecord-like accessors and introspection features|
15
|/app/models/arvados_resource_list.rb|ActiveRelation-like class (what you get from Model.where() etc.)|
16
17
h2. Unlike a typical Rails project...
18
19
* Most responses are JSON. Very few HTML views. We don't normally talk to browsers, except during authentication.
20
* We assign UUID strings (see lib/assign_uuid.rb and app/models/arvados_model.rb)
21
* The Arvados query API is fairly limited and doesn't accept SQL statements, so Workbench has to work harder to get what it needs. (Things will improve when Arvados accepts SPARQL queries.)
22
* Workbench itself only has the privileges of the Workbench user: when making Arvados API calls, it uses the API token provided by the user.
23
24
h2. Unlike what you might expect...
25
26
* Workbench doesn't use the Ruby SDK. It uses a sort of baked-in Rails SDK.
27
** TODO: move it out of Workbench into a gem.
28
** TODO: use the Ruby SDK under the hood.
29
30
h2. Running in development mode
31
32 2 Misha Zatsman
h3. SSL certificates
33
34 4 Tom Clegg
You can get started quickly with SSL by generating a self-signed certificate:
35 1 Tom Clegg
36
 openssl req -new -x509 -nodes -out ~/self-signed.pem -keyout ~/self-signed.key -days 3650 -subj '/CN=arvados.example.com'
37
38
Alternatively, download a set from the bottom of the [[API server]] page.
39 2 Misha Zatsman
40
h3. Download and configure
41 1 Tom Clegg
42 2 Misha Zatsman
Follow "these instructions":http://doc.arvados.org/install/install-workbench-app.html to download the source and configure your workbench instance.
43 3 Misha Zatsman
44 4 Tom Clegg
h3. Start the server
45 1 Tom Clegg
46 4 Tom Clegg
Save something like the following at @~/bin/workbench@, make it executable[1], make sure @~/bin@ is in your path[2]:
47 1 Tom Clegg
48
 #!/bin/sh
49
set -e
50
cd ~/arvados/apps/workbench
51
export RAILS_ENV=development
52 5 Tom Clegg
bundle install --path=vendor/bundle
53 4 Tom Clegg
exec bundle exec passenger start -p 3031 --ssl --ssl-certificate ~/self-signed.pem --ssl-certificate-key ~/self-signed.key
54 1 Tom Clegg
55
The first time you run the above it will take a while to install all the ruby gems. In particular @Installing nokogiri@ takes a while
56
57
Once you see:
58
59
 =============== Phusion Passenger Standalone web server started ===============
60
61
You can visit your server at:
62
63 4 Tom Clegg
 @https://{ip-or-host}:3031/@
64
65 6 Misha Zatsman
You can kill your server with @ctrl-C@ but if you get disconnected from the terminal, it will continue running. You can kill it by running
66
67
 @ps x |grep nginx |grep master@
68
69
And then
70
71
 @kill ####@
72
73
Replacing #### with the number in the left column returned by ps
74
75 4 Tom Clegg
fn1. @chmod +x ~/bin/workbench@
76
77
fn2. In Debian systems, the default .profile adds ~/bin to your path, but only if it exists when you log in. If you just created ~/bin, doing @exec bash -login@ or @source .profile@ should make ~/bin appear in your path.
78
79
h2. Running tests
80
81
The test suite brings up an API server in test mode, and runs browser tests with Firefox.
82
83
Make sure API server has its dependencies in place.
84
85
<pre>
86 5 Tom Clegg
(cd ../../services/api && RAILS_ENV=test bundle install --path=vendor/bundle)
87 4 Tom Clegg
</pre>
88
89
Install headless testing tools.
90
91
<pre>
92
sudo apt-get install xvfb iceweasel
93
</pre>
94
95
(Install firefox instead of iceweasel if you're not using Debian.)
96
97
Run the test suite.
98
99
<pre>
100
RAILS_ENV=test bundle exec rake test
101
</pre>
102 1 Tom Clegg
103
h2. Loading state from API into models
104
105
If your model makes an API call that returns the new state of an object, load the new attributes into the local model with @private_reload@:
106
107
<pre><code class="ruby">
108
  api_response = $arvados_api_client.api(...)
109
  private_reload api_response
110
</code></pre>
111
112
h2. Features
113
114
h3. Authentication
115
116
ApplicationController uses an around_filter to make sure the user is logged in, redirect to Arvados to complete the login procedure if not, and store the user's API token in Thread.current[:arvados_api_token] if so.
117
118
The @current_user@ helper returns User.current if the user is logged in, otherwise nil. (Generally, only special pages like "welcome" and "error" get displayed to users who aren't logged in.)
119
120
h3. Default filter behavior
121
122
@before_filter :find_object_by_uuid@
123
124
* This is enabled by default, @except :index, :create@.
125
* It renames the @:id@ param to @:uuid@. (The Rails default routing rules use @:id@ to accept params in path components, but @params[:uuid]@ makes more sense everywhere else in our code.)
126
* If you define a collection method (where there's no point looking up an object with the :id supplied in the request), skip this.
127
128
<pre><code class="ruby">
129
  skip_before_filter :find_object_by_uuid, only: [:action_that_takes_no_uuid_param]
130
</code></pre>
131
132
h3. Error handling
133
134
ApplicationController has a render_error method that shows a standard error page. (It's not very good, but it's better than a default Rails stack trace.)
135
136
In a controller you get there like this
137
138
<pre><code class="ruby">
139
  @errors = ['I could not achieve what you wanted.']
140
  render_error status: 500
141
</code></pre>
142
143
You can also do this, anywhere
144
145
<pre><code class="ruby">
146
  raise 'My spoon is too big.'
147
</code></pre>
148
149
The @render_error@ method sends JSON or HTML to the client according to the Accept header in the request (it sends JSON if JavaScript was requested), so reasonable things happen whether or not the request is AJAX.
150
151
h2. Development patterns
152
153
h3. Add a model
154
155
Currently, when the API provides a new model, we need to generate a corresponding model in Workbench: it's not smart enough to pick up the list of models from the API server's discovery document.
156
157
_(Need to fill in details here)_
158
# @rails generate model ....@
159
# Delete migration
160
# Change base class
161
# (probably more steps to fill in here)
162
163
Model _attributes_, on the other hand, are populated automatically.
164
165
h3. Add a configuration knob
166
167
Same situation as API server. See [[Hacking API Server]].
168
169
h3. Add an API method
170
171
Workbench is not yet smart enough to look in the discovery document for supported API methods. You need to add a method to the appropriate model class before you can use it in the Workbench app.
172
173
h3. Writing tests
174
175
(TODO)
176
177
h3. AJAX using Rails UJS (remote:true with JavaScript response)
178
179
This pattern is the best way to make a button/link that invokes an asynchronous action on the Workbench server side, i.e., before/without navigating away from the current page.
180
181
# Add <code class="ruby">remote: true</code> to a link or button. This makes Rails put a <code class="html">data-remote="true"</code> attribute in the HTML element. Say, in @app/views/fizz_buzzes/index.html.erb@:
182
<pre><code class="ruby">
183
<%= link_to "Blurfl", blurfl_fizz_buzz_url(id: @object.uuid), {class: 'btn btn-primary', remote: true} %>
184
</code></pre>
185
# Ensure the targeted action responds appropriately to both "js" and "html" requests. At minimum:
186
<pre><code class="ruby">
187
class FizzBuzzesController
188
  #...
189
  def blurfl
190
    @howmany = 1
191
    #...
192
    respond_to do |format|
193
      format.js
194
      format.html
195
    end
196
  end
197
end
198
</code></pre>
199
# The @html@ view is used if this is a normal page load (presumably this means the client has turned off JS).
200
#* @app/views/fizz_buzz/blurfl.html.erb@
201
<pre><code>
202
<p>I am <%= @howmany %></p>
203
</code></pre>
204
# The @js@ view is used if this is an AJAX request. It renders as JavaScript code which will be executed in the browser. Say, in @app/views/fizz_buzz/blurfl.js.erb@:
205
<pre><code class="javascript">
206
window.alert('I am <%= @howmany %>');
207
</code></pre>
208
# The browser opens an alert box:
209
<pre>
210
I am 1
211
</pre>
212
# A common task is to render a partial and use it to update part of the page. Say the partial is in @app/views/fizz_buzz/_latest_news.html.erb@:
213
<pre><code class="javascript">
214
var new_content = "<%= escape_javascript(render partial: 'latest_news') %>";
215
if ($('div#latest-news').html() != new_content)
216
   $('div#latest-news').html(new_content);
217
</code></pre>
218
219
*TODO: error handling*
220
221
h3. AJAX invoked from custom JavaScript (JSON response)
222
223
(and error handling)
224
225
h3. Add JavaScript triggers and fancy behavior
226
227
Some guidelines for implementing stuff nicely in JavaScript:
228
* Don't rely on the DOM being loaded before your script is loaded.
229
** If you need to inspect/alter the DOM as soon as it's loaded, make a setup function that fires on "document ready" and "ajax:complete".
230
** jQuery's delegated event pattern can help keep your code clean. See http://api.jquery.com/on/
231
<pre><code class="javascript">
232
// worse:
233
$('table.fizzbuzzer tr').
234
    on('mouseover', function(e, xhr) {
235
        console.log("This only works if the table exists when this setup script is executed.");
236
    });
237
// better:
238
$(document).
239
    on('mouseover', 'table.fizzbuzzer tr', function(e, xhr) {
240
        console.log("This works even if the table appears (or has the fizzbuzzer class added) later.");
241
    });
242
</code></pre>
243
244
* If your code really only makes sense for a particular view, rather than embedding @<script>@ tags in the middle of the page,
245
** use this:
246
<pre><code class="ruby">
247
<% content_for :js do %>
248
console.log("hurray, this goes in HEAD");
249
<% end %>
250
</code></pre>
251
** or, if your code should run after [most of] the DOM is loaded:
252
<pre><code class="ruby">
253
<% content_for :footer_js do %>
254
console.log("hurray, this runs at the bottom of the BODY element in the default layout.");
255
<% end %>
256
</code></pre>
257
258
* Don't just write JavaScript on the @fizz_buzzes/blurfl@ page and rely on the fact that the only @table@ element on the page is the one you want to attach your special behavior to. Instead, add a class to the table, and use a jQuery selector to attach special behavior to it.
259
** In @app/views/fizz_buzzes/blurfl.html.erb@
260
<pre>
261
<table class="fizzbuzzer">
262
 <tr>
263
  <td>fizz</td><td>buzz</td>
264
 </tr>
265
</table>
266
</pre>
267
** In @app/assets/javascripts/fizz_buzzes.js@
268
<pre><code class="javascript">
269
<% content_for :js do %>
270
$(document).on('mouseover', 'table.fizzbuzzer tr', function() {
271
    console.log('buzz');
272
});
273
<% end %>
274
</code></pre>
275
** Advantage: You can reuse the special behavior in other tables/pages/classes
276
** Advantage: The JavaScript can get compiled, minified, cached in the browser, etc., instead of being rendered with every page view
277
** Advantage: The JavaScript code is available regardless of how the content got into the DOM (regular page view, partial update with AJAX)
278
279
h3. Invoking selected-things picker
280
281
(TODO)
282
283
h3. Tabs/panes on index & show pages
284
285
(TODO)
286
287
h3. User notifications
288
289
(TODO)
290
291
h3. Customizing breadcrumbs
292
293
(TODO)
294
295
h3. Making a page accessible before login
296
297
(TODO)
298
299
h3. Making a page accessible to non-active users
300
301
(TODO)