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