gritano 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gritano.rb CHANGED
@@ -2,4 +2,4 @@ ROOT_PATH = File.expand_path(File.dirname(__FILE__))
2
2
 
3
3
  require File.join(ROOT_PATH, '/gritano/models')
4
4
  require File.join(ROOT_PATH, '/gritano/console')
5
- require File.join(ROOT_PATH, '/gritano/command')
5
+ require File.join(ROOT_PATH, '/gritano/cli')
@@ -0,0 +1,33 @@
1
+ module Gritano
2
+ module CLI
3
+ def CLI.execute(cmd, stdin = STDIN, home_dir = Etc.getpwuid.dir, repo_dir = Etc.getpwuid.dir)
4
+ Gritano::Console.remote_console(false)
5
+ console = Gritano::Console::Gritano.new(stdin, home_dir, repo_dir)
6
+ begin
7
+ output = console.execute(cmd)
8
+ if output[0]
9
+ output[1]
10
+ else
11
+ "error: #{output[1]}"
12
+ end
13
+ rescue
14
+ console.execute(["help"])[1]
15
+ end
16
+ end
17
+
18
+ def CLI.check(cmd, login, stdin = STDIN, home_dir = Etc.getpwuid.dir, repo_dir = Etc.getpwuid.dir)
19
+ Gritano::Console.remote_console(true)
20
+ console = Gritano::Console::Check.new(stdin, home_dir, repo_dir)
21
+ begin
22
+ output = console.execute(cmd + [login])
23
+ if output[0]
24
+ output[1]
25
+ else
26
+ "error: #{output[1]}"
27
+ end
28
+ rescue
29
+ console.execute(["help"])[1]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,242 +1,25 @@
1
- require "terminal-table"
1
+ require File.join(ROOT_PATH, 'gritano/console/base')
2
+ require File.join(ROOT_PATH, 'gritano/console/executor')
3
+ require File.join(ROOT_PATH, 'gritano/console/installer')
4
+ require File.join(ROOT_PATH, 'gritano/console/gritano')
5
+ require File.join(ROOT_PATH, 'gritano/console/check')
2
6
 
3
7
  module Gritano
4
- class Console
5
-
6
- attr_accessor :repo_path
7
- attr_accessor :ssh_path
8
-
9
- def initialize(stdin)
10
- @repo_path = nil
11
- @ssh_path = nil
12
- @stdin = stdin
13
- end
14
-
15
- def execute(argv)
16
- send(argv[0].gsub(':', '_'), argv[1..-1])
17
- end
18
-
19
- def user_list(argv)
20
- users = User.all
21
- msg = Terminal::Table.new do |t|
22
- t << ['user']
23
- t << :separator
24
- users.each do |user|
25
- t.add_row [user.login]
26
- end
27
- end
28
- msg = "there is no user registered" if users.count == 0
29
- return [true, msg]
30
- end
31
-
32
- def user_key_list(argv)
33
- login, = argv
34
- user = User.find_by_login(login)
35
- if user
36
- keys = user.keys
37
- msg = Terminal::Table.new do |t|
38
- t << ['keys']
39
- t << :separator
40
- keys.each do |key|
41
- t.add_row [key.name]
42
- end
43
- end
44
- msg = "there is no key registered" if keys.count == 0
45
- return [true, msg]
8
+ module Console
9
+ def Console.remote_console(remote)
10
+ if remote
11
+ Base.bin_name = "ssh git@host.com admin:"
12
+ Check.bin_name = "ssh git@host.com "
13
+ Executor.bin_name = "ssh git@host.com admin:"
14
+ Gritano.bin_name = "ssh git@host.com admin:"
15
+ Installer.bin_name = "ssh git@host.com admin:"
46
16
  else
47
- return [false, "User #{login} is not registered"]
48
- end
49
- end
50
-
51
- def user_repo_list(argv)
52
- login, = argv
53
- user = User.find_by_login(login)
54
- if user
55
- repos = user.repositories
56
- msg = Terminal::Table.new do |t|
57
- t << ['repositories']
58
- t << :separator
59
- repos.each do |repo|
60
- t.add_row [repo.name]
61
- end
62
- end
63
- msg = "there is no repository registered" if repos.count == 0
64
- return [true, msg]
65
- else
66
- return [false, "User #{login} is not registered"]
67
- end
68
- end
69
-
70
- def user_add(argv)
71
- login, = argv
72
- user = User.new(login: login)
73
- return [true, "User #{login} added."] if user.save
74
- return [false, "#{user.errors.full_messages.join(", ")}."]
75
- end
76
-
77
- def user_rm(argv)
78
- login, = argv
79
- user = User.find_by_login(login)
80
- if user
81
- if user.destroy
82
- return [true, "User #{login} removed."]
83
- end
84
- end
85
- return [false, "User #{login} could not be removed."]
86
- end
87
-
88
- def user_key_add(argv)
89
- login, key_name, key_file = argv
90
- user = User.find_by_login(login)
91
- if user
92
- key = user.keys.create(name: key_name, key: @stdin.read)
93
- if key.valid?
94
- File.open(File.join(@ssh_path, 'authorized_keys'), 'w').write(Key.authorized_keys)
95
- return [true, "Key added successfully."]
96
- end
97
- end
98
- return [false, "Key could not be added."]
99
- end
100
-
101
- def user_key_rm(argv)
102
- login, key_name = argv
103
- key = Key.where(name: key_name).includes(:user).where("users.login" => login).limit(1)[0]
104
- if key
105
- if key.destroy
106
- File.open(File.join(@ssh_path, 'authorized_keys'), 'w').write(Key.authorized_keys)
107
- return [true, "Key removed successfully."]
108
- end
109
- end
110
- return [false, "Key could not be removed."]
111
- end
112
-
113
- def user_admin_add(argv)
114
- login, = argv
115
- user = User.find_by_login(login)
116
- if user
117
- user.admin = true
118
- if user.save
119
- return [true, "Now, user #{login} is an administrator"]
120
- end
121
- end
122
- return [false, "User #{login} could not be modified"]
123
- end
124
-
125
- def user_admin_rm(argv)
126
- login, = argv
127
- user = User.find_by_login(login)
128
- if user
129
- user.admin = false
130
- if user.save
131
- return [true, "Now, user #{login} is not an administrator"]
132
- end
133
- end
134
- return [false, "User #{login} could not be modified"]
135
- end
136
-
137
- def repo_list(argv)
138
- repos = Repository.all
139
- msg = Terminal::Table.new do |t|
140
- t << ['repositories']
141
- t << :separator
142
- repos.each do |repo|
143
- t.add_row [repo.name]
144
- end
145
- end
146
- msg = "there is no repository registered" if repos.count == 0
147
- return [true, msg]
148
- end
149
-
150
- def repo_add(argv)
151
- name, user_login = argv
152
- repo = Repository.new(name: name, path: @repo_path)
153
- if repo.save
154
- if user_login
155
- argv[1..-1].each do |login|
156
- user = User.find_by_login(login)
157
- if user
158
- user.add_access(repo, :read)
159
- user.add_access(repo, :write)
160
- end
161
- end
162
- end
163
- return [true, "Repository #{name} created successfully."]
164
- end
165
- return [false, "Repository #{name} could not be created."]
166
- end
167
-
168
- def repo_user_list(argv)
169
- name, = argv
170
- repo = Repository.find_by_name(name)
171
- if repo
172
- users = repo.users
173
- msg = Terminal::Table.new do |t|
174
- t << ['user', 'permission']
175
- t << :separator
176
- users.each do |user|
177
- permissions = ""
178
- user.permissions.find_by_repository_id(repo.id) do |p|
179
- permissions += "r" if p.is(:read)
180
- permissions += "w" if p.is(:write)
181
- end
182
- t.add_row [user.login, permissions]
183
- end
184
- end
185
- msg = "No user have access to this repository" if users.count == 0
186
- return [true, msg]
187
- end
188
- return [false, "Repository #{name} doesn't exist."]
189
- end
190
-
191
- def repo_rm(argv)
192
- name, = argv
193
- repo = Repository.find_by_name(name)
194
- if repo
195
- if repo.destroy
196
- return [true, "Repository #{name} removed successfully."]
197
- end
198
- end
199
- return [false, "Repository #{name} could not be removed."]
200
- end
201
-
202
- def repo_read_add(argv)
203
- repo_name, login = argv
204
- user = User.find_by_login(login)
205
- repo = Repository.find_by_name(repo_name)
206
- if repo and user
207
- return [true, "User #{login} has read access to #{repo_name}."] if user.add_access(repo, :read)
208
- end
209
- return [false, "An error occurred. Permissions was not modified."]
210
- end
211
-
212
- def repo_write_add(argv)
213
- repo_name, login = argv
214
- user = User.find_by_login(login)
215
- repo = Repository.find_by_name(repo_name)
216
- if repo and user
217
- return [true, "User #{login} has write access to #{repo_name}."] if user.add_access(repo, :write)
218
- end
219
- return [false, "An error occurred. Permissions was not modified."]
220
- end
221
-
222
- def repo_read_rm(argv)
223
- repo_name, login = argv
224
- user = User.find_by_login(login)
225
- repo = Repository.find_by_name(repo_name)
226
- if repo and user
227
- return [true, "User #{login} has not read access to #{repo_name}."] if user.remove_access(repo, :read)
228
- end
229
- return [false, "An error occurred. Permissions was not modified."]
230
- end
231
-
232
- def repo_write_rm(argv)
233
- repo_name, login = argv
234
- user = User.find_by_login(login)
235
- repo = Repository.find_by_name(repo_name)
236
- if repo and user
237
- return [true, "User #{login} has not write access to #{repo_name}."] if user.remove_access(repo, :write)
17
+ Base.bin_name = "gritano "
18
+ Check.bin_name = "gritano "
19
+ Executor.bin_name = "gritano "
20
+ Gritano.bin_name = "gritano "
21
+ Installer.bin_name = "gritano "
238
22
  end
239
- return [false, "An error occurred. Permissions was not modified."]
240
23
  end
241
24
  end
242
25
  end
@@ -0,0 +1,74 @@
1
+ require "terminal-table"
2
+
3
+ module Gritano
4
+ module Console
5
+ class Base
6
+
7
+ def initialize(stdin = STDIN, home_dir = Etc.getpwuid.dir)
8
+ @home_dir = home_dir
9
+ @stdin = stdin
10
+ end
11
+
12
+ def self.add_command(command, parameters = "", &block)
13
+ define_method(command.gsub(':', '_'), &block)
14
+ commands[command] = parameters
15
+ end
16
+
17
+ def self.before_each_command(&block)
18
+ define_method(:before_each_command_filter, &block);
19
+ end
20
+
21
+ def self.commands
22
+ @commands || @commands = Hash.new
23
+ end
24
+
25
+ def self.commands=(cmds)
26
+ @commands = cmds
27
+ end
28
+
29
+ def self.bin_name
30
+ @bin_name || "gritano "
31
+ end
32
+
33
+ def self.bin_name=(name)
34
+ @bin_name = name
35
+ end
36
+
37
+ def self.help
38
+ msg = " #{self.bin_name}[command]\n\n"
39
+ msg += " Examples:\n"
40
+ commands.each do |command, parameters|
41
+ msg += " #{self.bin_name}#{command} #{parameters}\n"
42
+ end
43
+ msg += "\n --\n v#{File.open(File.join(File.dirname(__FILE__), '..', '..', '..', 'VERSION')).readlines.join}"
44
+ msg
45
+ end
46
+
47
+ def execute_without_filters(argv)
48
+ send(argv[0].gsub(':', '_'), argv[1..-1])
49
+ end
50
+
51
+ def execute(argv)
52
+ send(:before_each_command_filter)
53
+ execute_without_filters(argv)
54
+ end
55
+
56
+ def check_gritano
57
+ unless File.exist?(File.join(@home_dir, '.gritano'))
58
+ puts "Error: First run 'gritano setup:prepare && gritano setup:install'"
59
+ exit
60
+ end
61
+ end
62
+
63
+ def check_git
64
+ if `which git` == ""
65
+ puts "Error: git must be installed on the local system"
66
+ exit
67
+ end
68
+ end
69
+
70
+ def before_each_command_filter
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,83 @@
1
+ module Gritano
2
+ module Console
3
+ class Check < Base
4
+
5
+ def initialize(stdin = STDIN, home_dir = Etc.getpwuid.dir, repo_path = Etc.getpwuid.dir)
6
+ @stdin = stdin
7
+ @home_dir = home_dir
8
+ @repo_path = repo_path
9
+ @ssh_path = File.join(@home_dir, '.ssh')
10
+ @executor = Executor.new(@stdin, @home_dir, @repo_path)
11
+ super(@stdin, @home_dir)
12
+ end
13
+
14
+ before_each_command do
15
+ check_git
16
+ check_gritano
17
+ ActiveRecord::Base.establish_connection(
18
+ YAML::load(File.open(File.join(@home_dir, '.gritano', 'database.yml'))))
19
+ end
20
+
21
+ add_command "version" do |argv|
22
+ version = "v#{File.open(File.join(File.dirname(__FILE__), '..', '..', '..', 'VERSION')).readlines.join}"
23
+ [true, version]
24
+ end
25
+
26
+ add_command "help" do |args|
27
+ [true, Check.help]
28
+ end
29
+
30
+ add_command "repo:list" do |args|
31
+ login, = args
32
+ @executor.execute(["user:repo:list"] + [login])
33
+ end
34
+
35
+ add_command "key:list" do |args|
36
+ login, = args
37
+ @executor.execute(["user:key:list"] + [login])
38
+ end
39
+
40
+ add_command "key:add", "keyname < key.pub" do |args|
41
+ keyname, login = args
42
+ @executor.execute(["user:key:add"] + [login, keyname])
43
+ end
44
+
45
+ add_command "key:rm", "keyname" do |args|
46
+ keyname, login = args
47
+ @executor.execute(["user:key:rm"] + [login, keyname])
48
+ end
49
+
50
+ add_command "admin:help" do |args|
51
+ gritano = Gritano.new(@stdin)
52
+ gritano.execute(["help"])
53
+ end
54
+
55
+ def method_missing(meth, *args, &block)
56
+ if meth.to_s =~ /^admin/
57
+ user = ::Gritano::User.find_by_login(args[0][-1])
58
+ if user.admin?
59
+ meth = meth.to_s[6..-1]
60
+ params = args[0][0..-2]
61
+ @executor.execute([meth] + params)
62
+ else
63
+ [false, "access denied"]
64
+ end
65
+ elsif meth.to_s =~ /^git-receive-pack/
66
+ repo, login = args[0]
67
+ Kernel.exec "git-receive-pack #{repo(repo).full_path}"
68
+ elsif meth.to_s =~ /^git-upload-pack/
69
+ repo, login = args[0]
70
+ Kernel.exec "git-upload-pack #{repo(repo).full_path}"
71
+ else
72
+ super
73
+ end
74
+ end
75
+
76
+ def repo(repo)
77
+ repo = repo.gsub("'", '').strip
78
+ ::Gritano::Repository.find_by_name(repo)
79
+ end
80
+
81
+ end
82
+ end
83
+ end