public
Description: Gitorious aims to provide a great way of doing distributed opensource code collaboration.
Homepage: http://gitorious.org/projects/gitorious
Clone URL: git://github.com/dysinger/gitorious.git
Task system for async repos and ssh keys handling
js (author)
Sun Dec 16 09:12:25 -0800 2007
commit  cce2b1a27706cd2c83bc391301cad68c69840ce6
tree    ffb8e4a3e44219303f9451fbe0dca4cb3eb0a3d6
parent  98aded9fc2f8d0c586ba145da2fe3b396b321342
...
37
38
39
40
 
41
42
43
44
 
 
45
46
47
48
 
 
49
50
51
52
53
54
 
 
 
 
55
56
57
...
71
72
73
74
 
 
75
76
77
78
 
 
79
80
81
...
88
89
90
 
 
 
 
91
...
37
38
39
 
40
41
42
 
 
43
44
45
46
 
 
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
...
75
76
77
 
78
79
80
81
82
 
83
84
85
86
87
...
94
95
96
97
98
99
100
101
0
@@ -37,21 +37,25 @@ class Repository < ActiveRecord::Base
0
   end
0
   
0
   def full_repository_path
0
- File.expand_path(File.join(BASE_REPOSITORY_DIR, gitdir))
0
+ self.class.full_path_from_partial_path(gitdir)
0
   end
0
   
0
- def create_git_repository
0
- git_backend.create(full_repository_path)
0
+ def self.create_git_repository(path)
0
+ git_backend.create(full_path_from_partial_path(path))
0
   end
0
   
0
- def delete_git_repository
0
- git_backend.delete!(full_repository_path)
0
+ def self.delete_git_repository(path)
0
+ git_backend.delete!(full_path_from_partial_path(path))
0
   end
0
   
0
   def has_commits?
0
     git_backend.repository_has_commits?(full_repository_path)
0
   end
0
   
0
+ def self.git_backend
0
+ RAILS_ENV == "test" ? MockGitBackend : GitBackend
0
+ end
0
+
0
   def git_backend
0
     RAILS_ENV == "test" ? MockGitBackend : GitBackend
0
   end
0
@@ -71,11 +75,13 @@ class Repository < ActiveRecord::Base
0
   end
0
   
0
   def create_new_repos_task
0
- Task.create!(:target => self, :command => "create_git_repository")
0
+ Task.create!(:target_class => self.class.name,
0
+ :command => "create_git_repository", :arguments => gitdir)
0
   end
0
   
0
   def create_delete_repos_task
0
- Task.create!(:target => self, :command => "delete_git_repository")
0
+ Task.create!(:target_class => self.class.name,
0
+ :command => "delete_git_repository", :arguments => gitdir)
0
   end
0
     
0
   protected
0
@@ -88,4 +94,8 @@ class Repository < ActiveRecord::Base
0
     def add_user_as_committer
0
       committers << user
0
     end
0
+
0
+ def self.full_path_from_partial_path(path)
0
+ File.expand_path(File.join(BASE_REPOSITORY_DIR, path))
0
+ end
0
 end
...
22
23
24
25
 
26
27
 
28
29
30
 
31
32
 
33
34
35
36
 
 
37
38
39
40
 
 
41
42
43
...
22
23
24
 
25
26
 
27
28
29
 
30
31
 
32
33
34
35
 
36
37
38
39
40
 
41
42
43
44
45
0
@@ -22,22 +22,24 @@ class SshKey < ActiveRecord::Base
0
     %Q{\n### END KEY #{self.id || "nil"} ###}
0
   end
0
   
0
- def add_to_authorized_keys(key_file_class=SshKeyFile)
0
+ def self.add_to_authorized_keys(keydata, key_file_class=SshKeyFile)
0
     key_file = key_file_class.new
0
- key_file.add_key(self.to_key)
0
+ key_file.add_key(keydata)
0
   end
0
   
0
- def delete_from_authorized_keys(key_file_class=SshKeyFile)
0
+ def self.delete_from_authorized_keys(keydata, key_file_class=SshKeyFile)
0
     key_file = key_file_class.new
0
- key_file.delete_key(self.to_key)
0
+ key_file.delete_key(keydata)
0
   end
0
   
0
   def create_new_task
0
- Task.create!(:target => self, :command => "add_to_authorized_keys")
0
+ Task.create!(:target_class => self.class.name,
0
+ :command => "add_to_authorized_keys", :arguments => self.to_key)
0
   end
0
   
0
   def create_delete_task
0
- Task.create!(:target => self, :command => "delete_from_authorized_keys")
0
+ Task.create!(:target_class => self.class.name,
0
+ :command => "delete_from_authorized_keys", :arguments => self.to_key)
0
   end
0
   
0
   protected
...
5
6
7
8
9
10
 
 
 
 
 
11
12
13
14
 
15
16
 
 
17
18
19
...
5
6
7
 
 
 
8
9
10
11
12
13
14
15
 
16
17
 
18
19
20
21
22
0
@@ -5,15 +5,18 @@ class Task < ActiveRecord::Base
0
     find(:all, :conditions => {:performed => false})
0
   end
0
   
0
- def self.perform_all_pending!
0
- find_all_pending.each do |task|
0
- task.perform!
0
+ def self.perform_all_pending!(log=RAILS_DEFAULT_LOGGER)
0
+ tasks_to_perform = find_all_pending
0
+ log.info("Got #{tasks_to_perform.size.inspect} tasks to perform...")
0
+ tasks_to_perform.each do |task|
0
+ task.perform!(log)
0
     end
0
   end
0
   
0
- def perform!
0
+ def perform!(log=RAILS_DEFAULT_LOGGER)
0
     transaction do
0
- target.send(command)
0
+ log.info("Performing Task #{self.id.inspect}: #{target_class}::#{command}(#{arguments[0..64].inspect}..)")
0
+ target_class.constantize.send(command, arguments)
0
       self.performed = true
0
       self.performed_at = Time.now
0
       save!
...
1
2
3
4
5
 
6
 
7
8
9
10
11
12
13
14
15
...
1
2
3
 
 
4
5
6
7
8
9
10
 
 
11
12
13
0
@@ -1,15 +1,13 @@
0
 class CreateTasks < ActiveRecord::Migration
0
   def self.up
0
     create_table :tasks do |t|
0
- t.integer :target_id
0
- t.string :target_type
0
+ t.string :target_class
0
       t.string :command
0
+ t.text :arguments
0
       t.boolean :performed, :default => false
0
       t.datetime :performed_at
0
       t.timestamps
0
     end
0
- add_index :tasks, :target_id
0
- add_index :tasks, :target_type
0
     add_index :tasks, :performed
0
   end
0
 
...
81
82
83
84
85
 
86
 
87
88
89
90
91
92
93
94
95
96
97
...
81
82
83
 
 
84
85
86
87
88
89
90
91
92
 
 
93
94
95
0
@@ -81,17 +81,15 @@ ActiveRecord::Schema.define(:version => 16) do
0
   end
0
 
0
   create_table "tasks", :force => true do |t|
0
- t.integer "target_id"
0
- t.string "target_type"
0
+ t.string "target_class"
0
     t.string "command"
0
+ t.text "arguments"
0
     t.boolean "performed", :default => false
0
     t.datetime "performed_at"
0
     t.datetime "created_at"
0
     t.datetime "updated_at"
0
   end
0
 
0
- add_index "tasks", ["target_id"], :name => "index_tasks_on_target_id"
0
- add_index "tasks", ["target_type"], :name => "index_tasks_on_target_type"
0
   add_index "tasks", ["performed"], :name => "index_tasks_on_performed"
0
 
0
   create_table "users", :force => true do |t|
...
1
2
3
4
5
 
6
 
7
8
9
10
11
12
 
13
 
 
 
 
14
...
1
2
3
 
 
4
5
6
7
8
9
10
 
 
11
12
13
14
15
16
17
0
@@ -1,14 +1,17 @@
0
 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
0
 create_repo:
0
   id: 1
0
- target_id: 1
0
- target_type: Repository
0
+ target_class: Repository
0
   command: create_git_repository
0
+ arguments: foo/bar.git
0
   performed: 0
0
     
0
 add_key:
0
   id: 2
0
- target_id: 1
0
- target_type: SshKey
0
+ target_class: SshKey
0
   command: add_to_authorized_keys
0
+ arguments: |-
0
+ ### START KEY 2 ###
0
+ command="gitorious johan",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG9zbXplaGlpZnZ0a3VyZWFc2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRiZ2hzdm1xY3Rvc216ZWhpaWZ2dGt1cmVhc3NnZGpweGlzcWpsYnhlYm95emd4Zm9mcWU= foo@example.com
0
+ ### END KEY 2 ###
0
   performed: 0
...
81
82
83
84
85
 
 
86
87
88
89
90
 
 
91
92
93
...
130
131
132
133
 
134
 
135
136
137
...
139
140
141
142
 
143
 
144
145
...
81
82
83
 
 
84
85
86
87
88
 
 
89
90
91
92
93
...
130
131
132
 
133
134
135
136
137
138
...
140
141
142
 
143
144
145
146
147
0
@@ -81,13 +81,13 @@ describe Repository do
0
   end
0
   
0
   it "inits the git repository" do
0
- @repository.git_backend.should_receive(:create).with(@repository.full_repository_path).and_return(true)
0
- @repository.create_git_repository
0
+ Repository.git_backend.should_receive(:create).with(@repository.full_repository_path).and_return(true)
0
+ Repository.create_git_repository(@repository.gitdir)
0
   end
0
   
0
   it "deletes a repository" do
0
- @repository.git_backend.should_receive(:delete!).with(@repository.full_repository_path).and_return(true)
0
- @repository.delete_git_repository
0
+ Repository.git_backend.should_receive(:delete!).with(@repository.full_repository_path).and_return(true)
0
+ Repository.delete_git_repository(@repository.gitdir)
0
   end
0
   
0
   it "knows if has commits" do
0
@@ -130,8 +130,9 @@ describe Repository do
0
     proc{
0
       @repository.save!
0
     }.should change(Task, :count)
0
- task = Task.find(:first, :conditions => ["target_id = ?", @repository.id], :order => "id desc")
0
+ task = Task.find(:first, :conditions => ["target_class = 'Repository'"], :order => "id desc")
0
     task.command.should == "create_git_repository"
0
+ task.arguments.should match(/#{@repository.gitdir}$/)
0
   end
0
   
0
   it "creates a Task on destroy" do
0
@@ -139,7 +140,8 @@ describe Repository do
0
     proc{
0
       @repository.destroy
0
     }.should change(Task, :count)
0
- task = Task.find(:first, :conditions => ["target_id = ?", @repository.id], :order => "id desc")
0
+ task = Task.find(:first, :conditions => ["target_class = 'Repository'"], :order => "id desc")
0
     task.command.should == "delete_git_repository"
0
+ task.arguments.should match(/#{@repository.gitdir}$/)
0
   end
0
 end
...
63
64
65
66
 
67
68
69
...
71
72
73
74
 
75
76
77
...
79
80
81
82
 
83
 
84
85
86
87
88
 
89
90
91
92
 
93
 
94
95
...
63
64
65
 
66
67
68
69
...
71
72
73
 
74
75
76
77
...
79
80
81
 
82
83
84
85
86
87
88
89
90
91
92
93
 
94
95
96
97
98
0
@@ -63,7 +63,7 @@ describe SshKey do
0
     ssh_key = new_key
0
     ssh_key_file_mock.should_receive(:new).and_return(ssh_key_file_mock)
0
     ssh_key_file_mock.should_receive(:add_key).with(ssh_key.to_key).and_return(true)
0
- ssh_key.add_to_authorized_keys(ssh_key_file_mock)
0
+ SshKey.add_to_authorized_keys(ssh_key.to_key, ssh_key_file_mock)
0
   end
0
   
0
   it "removes itself to the authorized keys file" do
0
@@ -71,7 +71,7 @@ describe SshKey do
0
     ssh_key = new_key
0
     ssh_key_file_mock.should_receive(:new).and_return(ssh_key_file_mock)
0
     ssh_key_file_mock.should_receive(:delete_key).with(ssh_key.to_key).and_return(true)
0
- ssh_key.delete_from_authorized_keys(ssh_key_file_mock)
0
+ SshKey.delete_from_authorized_keys(ssh_key.to_key, ssh_key_file_mock)
0
   end
0
   
0
   it "creates a Task on create and update" do
0
@@ -79,17 +79,20 @@ describe SshKey do
0
     proc{
0
       ssh_key.save!
0
     }.should change(Task, :count)
0
- task = Task.find(:first, :conditions => ["target_id = ?", ssh_key.id], :order => "id desc")
0
+ task = Task.find(:first, :conditions => ["target_class = 'SshKey'"], :order => "id desc")
0
     task.command.should == "add_to_authorized_keys"
0
+ task.arguments.should == ssh_key.to_key
0
   end
0
   
0
   it "creates a Task on destroy" do
0
     ssh_key = new_key
0
     ssh_key.save!
0
+ keydata = ssh_key.to_key.dup
0
     proc{
0
       ssh_key.destroy
0
     }.should change(Task, :count)
0
- task = Task.find(:first, :conditions => ["target_id = ?", ssh_key.id], :order => "id desc")
0
+ task = Task.find(:first, :conditions => ["target_class = 'SshKey'"], :order => "id desc")
0
     task.command.should == "delete_from_authorized_keys"
0
+ task.arguments.should == keydata
0
   end
0
 end
...
10
11
12
13
 
 
14
15
16
...
10
11
12
 
13
14
15
16
17
0
@@ -10,7 +10,8 @@ describe Task do
0
   end
0
   
0
   it "performs a task" do
0
- @task.target.should_receive(@task.command).and_return(true)
0
+ @task.target_class.constantize.should_receive(@task.command) \
0
+ .with(@task.arguments).and_return(true)
0
     @task.perform!
0
     @task.reload
0
     @task.performed?.should == true
...
1
2
3
4
5
 
 
...
1
2
3
 
4
5
6
0
@@ -1,4 +1,5 @@
0
 cache/*
0
 pids/*
0
 sessions/*
0
-sockets/*
0
\ No newline at end of file
0
+sockets/*
0
+*lockfile

Comments

    No one has commented yet.