Project

General

Profile

Hacking API server » History » Version 18

Tom Clegg, 08/13/2014 09:46 AM

1 1 Tom Clegg
h1. Hacking API server
2
3
{{toc}}
4
5
h2. Source tree layout
6
7
Everything is in @/services/api@.
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 most of the generic API features like CRUD, authentication|
13
|/app/controllers/arvados/v1/|API methods other than generic CRUD (users#current, jobs#queue, ...)|
14
|/app/models/arvados_model.rb|Default Arvados model behavior: permissions, etag, uuid|
15
16
h2. Unlike a typical Rails project...
17
18
* Most responses are JSON. Very few HTML views. We don't normally talk to browsers, except during authentication.
19
* We assign UUID strings (see lib/assign_uuid.rb and app/models/arvados_model.rb)
20
* The @Links@ table emulates a graph database a la "RDF":http://www.rdfabout.com/quickintro.xpd. Much of the interesting information in Arvados is recorded as a Link between two other entities.
21
* For the most part, relations among objects are not expressed with the usual ActiveRelation features like belongs_to and has_many.
22
* Permissions: see below.
23
24
h2. Running in development mode
25
26 12 Tom Clegg
First, take care of the dependencies documented at http://doc.arvados.org/install/install-api-server.html.
27
28 1 Tom Clegg
Save something like this at @~/bin/apiserver@, make it executable, make sure ~/bin is in your path:
29
30 7 Tom Clegg
<pre>
31
#!/bin/sh
32 1 Tom Clegg
set -e
33
cd ~/arvados/services/api
34 7 Tom Clegg
if ! [ -e self-signed.key ]
35
then
36 8 Tom Clegg
  # Generate a self-signed SSL key
37
  openssl req -new -x509 -nodes -out ./self-signed.pem -keyout ./self-signed.key -days 3650 -subj /CN=localhost
38 7 Tom Clegg
fi
39
if [ -e /usr/local/rvm/bin/rvm ]
40
then
41
  rvmexec="rvm-exec 2.1.1"
42
else
43
  rvmexec=""
44
fi
45
export ARVADOS_WEBSOCKETS=true
46 1 Tom Clegg
export RAILS_ENV=development
47 7 Tom Clegg
$rvmexec bundle install
48 11 Tom Clegg
exec $rvmexec bundle exec passenger start -p3030 --ssl --ssl-certificate self-signed.pem --ssl-certificate-key self-signed.key
49 7 Tom Clegg
</pre>
50 1 Tom Clegg
51 9 Tom Clegg
Notes:
52
* Here we use passenger instead of webrick (which is what we'd get with "@rails server@") in order to serve websockets from the same process as https. (You also have the option of serving https and wss from separate processes -- see @services/api/config/application.default.yml@ -- but the simplest way is to run both in the same process by setting @ARVADOS_WEBSOCKETS=true@.)
53
* Webrick can make its own self-signed SSL certificate, but passenger expects you to provide a certificate & key yourself. The above script generates a key/certificate pair the first time it runs, and leaves it in @services/api/self-signed.*@ to reuse next time.
54
* @bundle install@ ensures your installed gems satisfy the requirements in Gemfile and (if you have changed it locally) update Gemfile.lock appropriately. This should do the right thing after you change Gemfile yourself, or change Gemfile/Gemfile.lock by updating/merging master.
55
* If you're relying on rvm to provide a suitable version of Ruby, "@rvm-exec@" should do the right thing here. You can change the @2.1.1@ argument to the version you want to use (hopefully >= 2.1.1). If your system Ruby is >= 2.1.1, rvm is unnecessary.
56 14 Radhika Chippada
* You can kill the server by running
57
58 15 Radhika Chippada
<pre>
59 14 Radhika Chippada
passenger stop --pid-file ~/arvados/services/api/tmp/pids/passenger.3030.pid
60 15 Radhika Chippada
</pre>
61 9 Tom Clegg
62 1 Tom Clegg
h2. Headaches to avoid
63
64
If you make a change that affects the discovery document, you need to clear a few caches before your client will see the change.
65
* Restart API server or: @touch tmp/restart.txt@
66
* Clear API server disk cache: @rake tmp:cache:clear@
67
* Clear SDK discovery doc cache on client side: @rm -r ~/.cache/arvados/@
68 4 Tom Clegg
69
Do not store symbol keys (or values) in serialized attributes.
70
* Rails supplies @params@ as a HashWithIndifferentAccess so @params['foo']@ and @params[:foo]@ are equivalent. This is usually convenient. However, here we often copy arrays and hashes from @params@ to the database, and from there to API responses. JSON does not have HashWithIndifferentAccess (or symbols) and we want these serialized attributes to behave predictably everywhere.
71
* API server's policy is that serialized attributes (like @properties@ on a link) always have strings instead of symbols: these attributes look the same in the database, in the API server Rails application, in the JSON response sent to clients, and in the JSON objects received from clients.
72
* There is no validation (yet!) to check for this.
73
74 18 Tom Clegg
When @script/crunch-dispatch.rb@ invokes @arv-run-pipeline-instance@ and @crunch-job@, it uses the version of arvados-cli specified in @Gemfile.lock@. Use @bundle update arvados-cli@ to update @Gemfile.lock@ to use the latest versions.
75
76 1 Tom Clegg
77
h2. Features
78
79
h3. Authentication
80
81
Involves
82
* UserSessionsController (in app/controllers/, not .../arvados/v1): this is an exceptional case where we actually talk to a browser.
83
84
h3. Permissions
85
86
Object-level permissions, aka ownership and sharing
87
* Models have their own idea of create/update permissions. Controllers don't worry about this.
88
* ArvadosModel updates/enforces modified_by_* and owner_uuid
89
* Lookups are not (yet) permission-restricted in the default scope, though. Controllers need to use Model.readable_by(user).
90
* ApplicationController uses an around_filter that verifies the supplied api_token and makes current_user available everywhere. If you need to override create/update permissions, use @act_as_system_user do ... end@.
91
* Unusual cases: KeepDisks and Collections can be looked up by inactive users (otherwise they wouldn't be able to read & clickthrough user agreements).
92
93
Controller-level permissions
94
* ApplicationController#require_auth_scope_all checks token scopes: currently, unless otherwise specified by a subclass controller, nothing is allowed unless scopes includes "all".
95
* ApplicationController has an admin_required filter available (not used by default)
96
97
h3. Error handling
98
99
* "Look up object by uuid, and send 404 if not found" is enabled by default, except for index/create actions.
100
101
h3. Routing
102
103
* API routes are in the @:arvados@ &rarr; @:v1@ namespace.
104
* Routes like @/jobs/queue@ have to come before @resources :jobs@ (otherwise @/jobs/queue@ will match @jobs#get(id=queue)@ first). (Better, we should rearrange these to use @resources :jobs do ...@ like in Workbench.)
105
* We use the standard Rails routes like @/jobs/:id@ but then we move params[:id] to params[:uuid] in our before_filters.
106
107
h3. Tests
108
109 10 Tom Clegg
* Run tests with @rvm-exec 2.1.1 bundle exec rake test@
110
* If prompted, migrate your test database by running @RAILS_ENV=test rvm-exec 2.1.1 bundle exec rake db:migrate@
111
* As above, you can leave out @rvm-exec 2.1.1@ if your system Ruby version is suitable. But don't leave out @bundle exec@.
112
* Run just the unit tests with @[...] rake test:units@ (or @test:functionals@ or @test:integration@).
113
* Run just a single test class (file) by specifying the file, like @[...] rake TEST=test/unit/owner_test.rb@ (save time in your "did that fix the failing test?" phase!)
114 1 Tom Clegg
* Functional tests need to authenticate themselves with @authorize_with :active@ (where @:active@ refers to an ApiClientAuthorization fixture)
115 10 Tom Clegg
* There is a deficit of tests, especially unit tests. This is a bug! It doesn't mean we don't want to test things.
116
117 1 Tom Clegg
118
h3. Discovery document
119
120
* Mostly, but not yet completely, generated by introspection (descendants of ArvadosModel are inspected at run time). But some controllers/actions are skipped, and some actions are renamed (e.g., Rails calls it "show" but everyone else calls it "get").
121
* Handled by Arvados::V1::SchemaController#index (used to be in #discovery_document before #1750). See @config/routes.rb@
122
* Must be available to anonymous clients.
123
* Has no tests! We test it by trying all of our SDKs against it.
124
125
h2. Development patterns
126
127
h3. Add a model
128
129
In shell:
130
* @rails g model FizzBuzz@
131
132
In @app/models/fizzbuzz.rb@:
133
* Change base class from @ActiveRecord::Base@ to @ArvadosModel@.
134
* Add some more standard behavior.
135
136
<pre><code class="ruby">
137 13 Peter Amstutz
include HasUuid
138 1 Tom Clegg
include KindAndEtag
139
include CommonApiTemplate
140
</code></pre>
141
142
In @db/migrate/{timestamp}_create_fizzbuzzes.rb@:
143
* Add the generic attribute columns.
144
* Run @t.timestamps@ and add (at least!) a @:uuid@ index.
145
146
<pre><code class="ruby">
147
class CreateFizzBuzz < ActiveRecord::Migration
148
  def change
149
    create_table :fizzbuzzes do |t|
150
      t.string :uuid, :null => false
151
      t.string :owner_uuid, :null => false
152
      t.string :modified_by_client_uuid
153
      t.string :modified_by_user_uuid
154
      t.datetime :modified_at
155
      t.text :properties
156
157
      t.timestamps
158
    end
159
    add_index :humans, :uuid, :unique => true
160
  end
161
end
162
</code></pre>
163
164
Apply the migration:
165
* @rake db:migrate@
166
* @RAILS_ENV=test rake db:migrate@ (to migrate your test database too)
167
* Inspect the resulting @db/schema.rb@ and include it in your commit.
168
* Don't forget to @git add@ the new migration and model files.
169
170
h3. Add an attribute to a model
171
172
* Generate migration as usual
173
<pre>
174
rails g migration AddBazQuxToFooBar baz_qux:column_type_goes_here
175
</pre>
176
* Consider adding null constraints and a default value to the @add_column@ statement in the migration in @db/migrate/timestamp_add_baz_qux_to_foo_bar.rb@:
177
<pre><code class="ruby">, null: false, default: false</code></pre>
178
* Consider adding an index
179 16 Tom Clegg
* You probably want to add it to the API response template(s) so clients can see it: @app/models/model_name.rb@ &rarr; @api_accessible :user ...@
180 1 Tom Clegg
* Sometimes it's only visible to privileged users; see @ping_secret@ in @app/models/keep_disk.rb@
181
* If it's a serialized attribute, add @serialize :the_attribute_name, Hash@ to the model. Always specify Hash or Array!
182 3 Tom Clegg
* Run @rake db:migrate@ and inspect your @db/schema.rb@ and include the new @schema.rb@ in the *same commit* as your @db/migrate/*.rb@ migration script.
183 1 Tom Clegg
* Run @rake tmp:cache:clear@ and @touch tmp/restart.txt@ in your dev apiserver, to force it to generate a new REST discovery document.
184
185
h3. Add a controller
186
187 17 Tom Clegg
* @rails g controller Arvados::V1::FizzBuzzes@
188 1 Tom Clegg
* Avoid adding top-level controllers like @app/controllers/fizz_buzzes_controller.rb@.
189
* Avoid adding top-level routes. Everything should be in @namespace :arvados@ &rarr; @namespace :v1@ except oddballs like login/logout actions.
190
191
h3. Add a controller action
192
193
Add a route in @config/routes.rb@.
194
* Choose an appropriate HTTP method: GET has no side effects. POST creates something. PUT replaces/updates something.
195
* Use the block form:
196
<pre><code class="ruby">
197
resources :fizz_buzzes do
198
  # If the action operates on an object, i.e., a uuid is required,
199
  # this generates a route /arvados/v1/fizz_buzzes/{uuid}/blurfl
200
  post 'blurfl', on: :member
201
  # If not, this generates a route /arvados/v1/fizz_buzzes/flurbl
202
  get 'flurbl', on: :collection
203
end
204
</code></pre>
205
206
In @app/controllers/arvados/v1/fizz_buzzes_controller.rb@:
207
208
* Add a method to the controller class.
209
* Skip the "find_object" before_filters if it's a collection action.
210
* Specify required/optional parameters using a class method @_action_requires_parameters@.
211
<pre><code class="ruby">
212
skip_before_filter :find_object_by_uuid, only: [:flurbl]
213
skip_before_filter :render_404_if_no_object, only: [:flurbl]
214
215
def blurfl
216
  @object.do_whatever_blurfl_does!
217
  show
218
end
219
220
def self._flurbl_requires_parameters
221
  {
222
    qux: { type: 'integer', required: true, description: 'First flurbl qux must match this qux.' }
223
  }
224
end
225
def flurbl
226
  @object = model_class.where('qux = ?', params[:qux]).first
227
  show
228
end
229
</code></pre>
230
231
h3. Add a configuration parameter
232 6 Tom Clegg
233 1 Tom Clegg
* Add it to @config/application.default.yml@ with a sensible default value. (Don't fall back to default values at time of use, or define defaults in other places!)
234
* If there is no sensible default value, like @secret_token@: specify @~@ (i.e., nil) in @application.default.yml@ *and* put a default value in the @test@ section of @config/application.yml.example@ that will make tests pass.
235
* If there is a sensible default value for development/test but not for production, like return address for notification email messages, specify the test/dev default in the @common@ section @application.default.yml@ but specify @~@ (nil) in the @production@ section. This prevents someone from installing or *updating a production server* with defaults that don't make sense in production!
236 6 Tom Clegg
* Use @Rails.configuration.config_setting_name@ to retrieve the configured value. There is no need to check whether it is nil or missing: in those cases, "rake config:check" would have failed and the application would have refused to start.
237 2 Tom Clegg
238
239
h3. Add a test fixture
240
241
Generate last part of uuid from command line:
242
<pre><code class="ruby">ruby -e 'puts rand(2**512).to_s(36)[0..14]'
243
j0wqrlny07k1u12</code></pre>
244
Generate uuid from @rails console@:
245
<pre><code class="ruby">Group.generate_uuid
246
=> "xyzzy-j7d0g-8nw4r6gnnkixw1i"
247 1 Tom Clegg
</code></pre>