Bump version
[gitorious:mainline.git] / lib / gitorious.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2011-2014 Gitorious AS
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18 require "gitorious/configurable"
19 require "gitorious/mount_point"
20 require "gitorious/openid"
21 require "gitorious/kerberos"
22
23 module Gitorious
24   VERSION = "3.0.3"
25
26   # Application-wide configuration settings.
27   Configuration = Configurable.new("GITORIOUS")
28   Configuration.rename("gitorious_host", "host")
29   Configuration.rename("gitorious_client_host", "client_host")
30   Configuration.rename("gitorious_client_port", "client_port")
31   Configuration.rename("sender_email_address", "email_sender")
32   Configuration.rename("gitorious_support_email", "support_email")
33   Configuration.rename("gitorious_clone_host", "git_daemon_host")
34   Configuration.rename("use_ssl", "scheme", <<-EOF) { |use_ssl| use_ssl ? "https" : "http" }
35 The scheme setting should be set to "http"
36 or "https".
37   EOF
38   Configuration.rename("gitorious_user", "user")
39   Configuration.rename("repos_and_projects_private_by_default", "projects_default_private", <<-EOF)
40 Please note that this setting has been
41 split into two settings:
42 projects_default_private and repositories_default_private.
43   EOF
44   Configuration.rename("disable_record_throttling", "enable_record_throttling", <<-EOF) { |d| !d }
45 You should invert this value.
46   EOF
47   Configuration.rename("exception_notification_emails", "exception_recipients")
48   Configuration.rename("only_site_admins_can_create_projects", "enable_project_approvals")
49   Configuration.rename("hide_http_clone_urls", "enable_git_http", <<-MSG) { |d| !d }
50 You should invert this value.
51   MSG
52   Configuration.rename("hide_git_clone_urls", "enable_git_daemon", <<-MSG) { |d| !d }
53 You should invert this value.
54   MSG
55   Configuration.rename("custom_username_label", "username_label")
56   Configuration.rename("use_ldap_authorization", "enable_ldap_authorization")
57   Configuration.rename("show_license_descriptions_in_sidebar", "enable_sidebar_license_descriptions")
58
59   Configuration.on_deprecation do |old, new, comment|
60     $stderr.puts(<<-EOF)
61 WARNING! Setting '#{old}' in config/gitorious.yml is deprecated.
62 Use '#{new}' instead. Many configuration settings have changed
63 in Gitorious 3, please refer to config/gitorious.sample.yml for full documentation.
64 #{comment}
65     EOF
66   end
67
68   def self.site
69     return @site if @site && cache?
70     host = Gitorious::Configuration.get("host", "gitorious.local")
71     port = Gitorious::Configuration.get("port")
72     scheme = Gitorious::Configuration.get("scheme")
73     @site = Gitorious::HttpMountPoint.new(host, port, scheme)
74   end
75
76   def self.scheme; site.scheme; end
77   def self.host; site.host; end
78   def self.port; site.port; end
79   def self.ssl?; site.ssl?; end
80   def self.default_port?; site.default_port?; end
81   def self.url(path); site.url(path); end
82
83   def self.client
84     return @client if @client && cache?
85     host = Gitorious::Configuration.get("client_host", "localhost")
86     port = Gitorious::Configuration.get("client_port")
87     @client = Gitorious::HttpMountPoint.new(host, port)
88   end
89
90   def self.git_daemon
91     return @git_daemon if @git_daemon && cache?
92     return nil if !Gitorious::Configuration.get("enable_git_daemon", true)
93     host = Gitorious::Configuration.get("git_daemon_host") { Gitorious.host }
94     port = Gitorious::Configuration.get("git_daemon_port")
95     @git_daemon = Gitorious::GitMountPoint.new(host, port)
96   end
97
98   def self.ssh_daemon
99     return @ssh_daemon if @ssh_daemon && cache?
100     return nil if !Gitorious::Configuration.get("enable_ssh_daemon", true)
101     host = Gitorious::Configuration.get("ssh_daemon_host") { Gitorious.host }
102     port = Gitorious::Configuration.get("ssh_daemon_port") { 22 }
103     @ssh_daemon = Gitorious::GitSshMountPoint.new(Gitorious.user, host, port)
104   end
105
106   def self.git_http
107     return @git_http if @git_http && cache?
108     return nil if !Gitorious::Configuration.get("enable_git_http", true)
109     host = Gitorious::Configuration.get("git_http_host") { Gitorious.host }
110     port = Gitorious::Configuration.get("git_http_port") { Gitorious.port }
111     scheme = Gitorious::Configuration.get("git_http_scheme") { Gitorious.scheme }
112     @git_http = Gitorious::HttpMountPoint.new(host, port, scheme)
113   end
114
115   def self.default_remote_url(repository)
116     (ssh_daemon || git_daemon || git_http).url(repository.gitdir)
117   end
118
119   def self.email_sender
120     return @email_sender if @email_sender && cache?
121     default = "Gitorious <no-reply@#{host}>"
122     @email_sender = Gitorious::Configuration.get("email_sender", default)
123   end
124
125   def self.user
126     return @user if @user && cache?
127     @user = Gitorious::Configuration.get("user", "git")
128   end
129
130   def self.public?
131     return @public if !@public.nil? && cache?
132     @public = Gitorious::Configuration.get("public_mode", true)
133   end
134
135   def self.private_repositories?
136     return @private_repos if !@private_repos.nil? && cache?
137     @private_repos = Gitorious::Configuration.get("enable_private_repositories", false)
138   end
139
140   def self.projects_default_private?
141     return @projdefpriv if !@projdefpriv.nil? && cache?
142     @projdefpriv = private_repositories? && Gitorious::Configuration.get("projects_default_private", false)
143   end
144
145   def self.repositories_default_private?
146     return @repodefpriv if !@repodefpriv.nil? && cache?
147     @repodefpriv = private_repositories? && Gitorious::Configuration.get("repositories_default_private", false)
148   end
149
150   def self.support_email
151     return @support_email if @support_email && cache?
152     @support_email = Gitorious::Configuration.get("support_email") do
153       "gitorious-support@#{host}"
154     end
155   end
156
157   def self.remote_ops_ips
158     return @remote_ops_ips if @remote_ops_ips && cache?
159     ips = Gitorious::Configuration.get("remote_ops_ips", ["127.0.0.1"])
160     @remote_ops_ips = Array(ips)
161   end
162
163   def self.ops?(remote_addr)
164     remote_ops_ips.include?(remote_addr)
165   end
166
167   def self.archive_cache_dir
168     return @archive_cache_dir if @archive_cache_dir && cache?
169     @archive_cache_dir = Gitorious::Configuration.get("archive_cache_dir")
170   end
171
172   def self.archive_work_dir
173     return @archive_work_dir if @archive_work_dir && cache?
174     @archive_work_dir = Gitorious::Configuration.get("archive_work_dir")
175   end
176
177   def self.diff_timeout
178     return @diff_timeout if @diff_timeout && cache?
179     @diff_timeout = Gitorious::Configuration.get("merge_request_diff_timeout", 10).to_i
180   end
181
182   def self.frontend_server
183     return @frontend_server if @frontend_server && cache?
184     @frontend_server = Gitorious::Configuration.get("frontend_server", "nginx").downcase
185   end
186
187   def self.dot_org?
188     @is_gitorious_org = Gitorious::Configuration.get("is_gitorious_dot_org", false)
189   end
190
191   def self.git_binary
192     return @git_binary if @git_binary && cache?
193     @git_binary = Gitorious::Configuration.get("git_binary", "/usr/bin/env git")
194   end
195
196   def self.git_version
197     return @git_version if @git_version && cache?
198     @git_version = Gitorious::Configuration.get("git_version")
199   end
200
201   def self.site_name
202     return @site_name if @site_name && cache?
203     @site_name = Gitorious::Configuration.get("site_name", "Gitorious")
204   end
205
206   def self.registrations_enabled?
207     Gitorious::Configuration.get("enable_registrations", Gitorious.public?)
208   end
209
210   def self.max_tarball_size
211     return @max_tarball_size if @max_tarball_size && cache?
212     mts = Gitorious::Configuration.get("max_tarball_size", 0)
213     return mts.to_i * 1024 if mts =~ /k$/i
214     return mts.to_i * 1024 * 1024 if mts =~ /m$/i
215     return mts.to_i * 1024 * 1024 * 1024 if mts =~ /g$/i
216     @max_tarball_size = mts.to_i
217   end
218
219   def self.tarballable?(repository)
220     return true if max_tarball_size == 0 || repository.disk_usage.nil?
221     return repository.disk_usage <= max_tarball_size
222   end
223
224   def self.configured?
225     @configured
226   end
227
228   def self.configured!
229     @configured = true
230   end
231
232   def self.restrict_team_creation_to_site_admins?
233     Gitorious::Configuration.get("only_site_admins_can_create_teams")
234   end
235
236   def self.executor
237     return @executor if @executor
238
239     if Rails.env.test?
240       require 'gitorious/test_executor'
241       @executor = TestExecutor.new
242     else
243       require 'gitorious/command_executor'
244       @executor = CommandExecutor.new
245     end
246   end
247
248   def self.mirrors
249     return @mirrors if @mirrors
250     @mirrors = Gitorious::MirrorManager.new(Gitorious::Configuration.get('mirrors', []))
251   end
252
253   private
254   def self.cache?
255     return Rails.env.production? if defined?(Rails)
256     false
257   end
258 end