Support deployments of Gitorious outside of the root path
[gitorious:mainline.git] / lib / gitorious.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2011-2013 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
22 module Gitorious
23   VERSION = "3.0.0"
24
25   # Application-wide configuration settings.
26   Configuration = Configurable.new("GITORIOUS")
27   Configuration.rename("gitorious_host", "host")
28   Configuration.rename("gitorious_client_host", "client_host")
29   Configuration.rename("gitorious_client_port", "client_port")
30   Configuration.rename("sender_email_address", "email_sender")
31   Configuration.rename("gitorious_support_email", "support_email")
32   Configuration.rename("gitorious_clone_host", "git_daemon_host")
33   Configuration.rename("use_ssl", "scheme", <<-EOF) { |use_ssl| use_ssl ? "https" : "http" }
34 The scheme setting should be set to "http"
35 or "https".
36   EOF
37   Configuration.rename("gitorious_user", "user")
38   Configuration.rename("repos_and_projects_private_by_default", "projects_default_private", <<-EOF)
39 Please note that this setting has been
40 split into two settings:
41 projects_default_private and repositories_default_private.
42   EOF
43   Configuration.rename("disable_record_throttling", "enable_record_throttling", <<-EOF) { |d| !d }
44 You should invert this value.
45   EOF
46   Configuration.rename("exception_notification_emails", "exception_recipients")
47   Configuration.rename("only_site_admins_can_create_projects", "enable_project_approvals")
48   Configuration.rename("hide_http_clone_urls", "enable_git_http", <<-MSG) { |d| !d }
49 You should invert this value.
50   MSG
51   Configuration.rename("hide_git_clone_urls", "enable_git_daemon", <<-MSG) { |d| !d }
52 You should invert this value.
53   MSG
54   Configuration.rename("custom_username_label", "username_label")
55   Configuration.rename("use_ldap_authorization", "enable_ldap_authorization")
56   Configuration.rename("show_license_descriptions_in_sidebar", "enable_sidebar_license_descriptions")
57
58   Configuration.on_deprecation do |old, new, comment|
59     $stderr.puts(<<-EOF)
60 WARNING! Setting '#{old}' in config/gitorious.yml is deprecated.
61 Use '#{new}' instead. Many configuration settings have changed
62 in Gitorious 3, please refer to config/gitorious.sample.yml for full documentation.
63 #{comment}
64     EOF
65   end
66
67   def self.site
68     return @site if @site && cache?
69     host = Gitorious::Configuration.get("host", "gitorious.local")
70     port = Gitorious::Configuration.get("port")
71     scheme = Gitorious::Configuration.get("scheme")
72     path = Gitorious::Configuration.get("path")
73     @site = Gitorious::HttpMountPoint.new(host, port, scheme, path)
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.path; site.rootpath; end
80   def self.ssl?; site.ssl?; 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     path = Gitorious::Configuration.get("client_path") { Gitorious.path }
88     @client = Gitorious::HttpMountPoint.new(host, port, "http", path)
89   end
90
91   def self.git_daemon
92     return @git_daemon if @git_daemon && cache?
93     return nil if !Gitorious::Configuration.get("enable_git_daemon", true)
94     host = Gitorious::Configuration.get("git_daemon_host") { Gitorious.host }
95     port = Gitorious::Configuration.get("git_daemon_port")
96     @git_daemon = Gitorious::GitMountPoint.new(host, port)
97   end
98
99   def self.ssh_daemon
100     return @ssh_daemon if @ssh_daemon && cache?
101     return nil if !Gitorious::Configuration.get("enable_ssh_daemon", true)
102     host = Gitorious::Configuration.get("ssh_daemon_host") { Gitorious.host }
103     port = Gitorious::Configuration.get("ssh_daemon_port") { 22 }
104     @ssh_daemon = Gitorious::GitSshMountPoint.new(Gitorious.user, host, port)
105   end
106
107   def self.git_http
108     return @git_http if @git_http && cache?
109     return nil if !Gitorious::Configuration.get("enable_git_http", true)
110     host = Gitorious::Configuration.get("git_http_host") { Gitorious.host }
111     port = Gitorious::Configuration.get("git_http_port") { Gitorious.port }
112     scheme = Gitorious::Configuration.get("git_http_scheme") { Gitorious.scheme }
113     path = Gitorious::Configuration.get("git_http_path") { Gitorious.path }
114     @git_http = Gitorious::HttpMountPoint.new(host, port, scheme, path)
115   end
116
117   def self.email_sender
118     return @email_sender if @email_sender && cache?
119     default = "Gitorious <no-reply@#{host}>"
120     @email_sender = Gitorious::Configuration.get("email_sender", default)
121   end
122
123   def self.user
124     return @user if @user && cache?
125     @user = Gitorious::Configuration.get("user", "git")
126   end
127
128   def self.public?
129     return @public if !@public.nil? && cache?
130     @public = Gitorious::Configuration.get("public_mode", true)
131   end
132
133   def self.private_repositories?
134     return @private_repos if !@private_repos.nil? && cache?
135     @private_repos = Gitorious::Configuration.get("enable_private_repositories", false)
136   end
137
138   def self.projects_default_private?
139     return @projdefpriv if !@projdefpriv.nil? && cache?
140     @projdefpriv = private_repositories? && Gitorious::Configuration.get("projects_default_private", false)
141   end
142
143   def self.repositories_default_private?
144     return @repodefpriv if !@repodefpriv.nil? && cache?
145     @repodefpriv = private_repositories? && Gitorious::Configuration.get("repositories_default_private", false)
146   end
147
148   def self.support_email
149     return @support_email if @support_email && cache?
150     @support_email = Gitorious::Configuration.get("support_email") do
151       "gitorious-support@#{host}"
152     end
153   end
154
155   def self.remote_ops_ips
156     return @remote_ops_ips if @remote_ops_ips && cache?
157     ips = Gitorious::Configuration.get("remote_ops_ips", ["127.0.0.1"])
158     @remote_ops_ips = Array(ips)
159   end
160
161   def self.ops?(remote_addr)
162     remote_ops_ips.include?(remote_addr)
163   end
164
165   def self.archive_cache_dir
166     return @archive_cache_dir if @archive_cache_dir && cache?
167     @archive_cache_dir = Gitorious::Configuration.get("archive_cache_dir")
168   end
169
170   def self.archive_work_dir
171     return @archive_work_dir if @archive_work_dir && cache?
172     @archive_work_dir = Gitorious::Configuration.get("archive_work_dir")
173   end
174
175   def self.diff_timeout
176     return @diff_timeout if @diff_timeout && cache?
177     @diff_timeout = Gitorious::Configuration.get("merge_request_diff_timeout", 10).to_i
178   end
179
180   def self.frontend_server
181     return @frontend_server if @frontend_server && cache?
182     @frontend_server = Gitorious::Configuration.get("frontend_server", "nginx").downcase
183   end
184
185   def self.dot_org?
186     @is_gitorious_org = Gitorious::Configuration.get("is_gitorious_dot_org", true)
187   end
188
189   def self.git_binary
190     return @git_binary if @git_binary && cache?
191     @git_binary = Gitorious::Configuration.get("git_binary", "/usr/bin/env git")
192   end
193
194   def self.git_version
195     return @git_version if @git_version && cache?
196     @git_version = Gitorious::Configuration.get("git_version")
197   end
198
199   def self.site_name
200     return @site_name if @site_name && cache?
201     @site_name = Gitorious::Configuration.get("site_name", "Gitorious")
202   end
203
204   def self.registrations_enabled?
205     Gitorious::Configuration.get("enable_registrations", Gitorious.public?)
206   end
207
208   def self.configured?
209     @configured
210   end
211
212   def self.configured!
213     @configured = true
214   end
215
216   def self.restrict_team_creation_to_site_admins?
217     Gitorious::Configuration.get("only_site_admins_can_create_teams")
218   end
219
220   private
221   def self.cache?
222     return Rails.env.production? if defined?(Rails)
223     false
224   end
225 end