PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/files/gitlab-cookbooks/gitlab/recipes/gitlab-rails.rb

https://gitlab.com/jmcgeheeiv/omnibus-gitlab
Ruby | 378 lines | 292 code | 46 blank | 40 comment | 7 complexity | 8f01daa907ae4fe346101cedc194cfa1 MD5 | raw file
  1. #
  2. # Copyright:: Copyright (c) 2012 Opscode, Inc.
  3. # Copyright:: Copyright (c) 2014 GitLab.com
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. account_helper = AccountHelper.new(node)
  19. gitlab_rails_source_dir = "/opt/gitlab/embedded/service/gitlab-rails"
  20. gitlab_shell_source_dir = "/opt/gitlab/embedded/service/gitlab-shell"
  21. gitlab_rails_dir = node['gitlab']['gitlab-rails']['dir']
  22. gitlab_rails_etc_dir = File.join(gitlab_rails_dir, "etc")
  23. gitlab_rails_static_etc_dir = "/opt/gitlab/etc/gitlab-rails"
  24. gitlab_rails_working_dir = File.join(gitlab_rails_dir, "working")
  25. gitlab_rails_tmp_dir = File.join(gitlab_rails_dir, "tmp")
  26. gitlab_rails_public_uploads_dir = node['gitlab']['gitlab-rails']['uploads_directory']
  27. gitlab_rails_log_dir = node['gitlab']['gitlab-rails']['log_directory']
  28. gitlab_ci_dir = node['gitlab']['gitlab-ci']['dir']
  29. gitlab_ci_builds_dir = node['gitlab']['gitlab-ci']['builds_directory']
  30. upgrade_status_dir = File.join(gitlab_rails_dir, "upgrade-status")
  31. ssh_dir = File.join(node['gitlab']['user']['home'], ".ssh")
  32. known_hosts = File.join(ssh_dir, "known_hosts")
  33. gitlab_app = "gitlab"
  34. gitlab_user = account_helper.gitlab_user
  35. gitlab_group = account_helper.gitlab_group
  36. # Explicitly try to create directory holding the logs to make sure
  37. # that the directory is created with correct permissions and not fallback
  38. # on umask of the process
  39. directory File.dirname(gitlab_rails_log_dir) do
  40. owner gitlab_user
  41. mode '0755'
  42. recursive true
  43. end
  44. if node['gitlab']['manage-storage-directories']['enable']
  45. # We create shared_path with 751 allowing other users to enter into the directories
  46. # It's needed, because by default the shared_path is used to store pages which are served by gitlab-www:gitlab-www
  47. directory node['gitlab']['gitlab-rails']['shared_path'] do
  48. owner gitlab_user
  49. group account_helper.web_server_group
  50. mode '0751'
  51. recursive true
  52. end
  53. [
  54. node['gitlab']['gitlab-rails']['artifacts_path'],
  55. node['gitlab']['gitlab-rails']['lfs_storage_path'],
  56. gitlab_rails_public_uploads_dir,
  57. gitlab_ci_builds_dir
  58. ].compact.each do |dir_name|
  59. directory dir_name do
  60. owner gitlab_user
  61. mode '0700'
  62. recursive true
  63. end
  64. end
  65. directory node['gitlab']['gitlab-rails']['pages_path'] do
  66. owner gitlab_user
  67. group account_helper.web_server_group
  68. mode '0750'
  69. recursive true
  70. end
  71. end
  72. [
  73. gitlab_rails_etc_dir,
  74. gitlab_rails_static_etc_dir,
  75. gitlab_rails_working_dir,
  76. gitlab_rails_tmp_dir,
  77. node['gitlab']['gitlab-rails']['gitlab_repository_downloads_path'],
  78. upgrade_status_dir,
  79. gitlab_rails_log_dir
  80. ].compact.each do |dir_name|
  81. directory dir_name do
  82. owner gitlab_user
  83. mode '0700'
  84. recursive true
  85. end
  86. end
  87. directory node['gitlab']['gitlab-rails']['backup_path'] do
  88. owner gitlab_user
  89. mode '0700'
  90. recursive true
  91. only_if { node['gitlab']['gitlab-rails']['manage_backup_path'] }
  92. end
  93. directory gitlab_rails_dir do
  94. owner gitlab_user
  95. mode '0755'
  96. recursive true
  97. end
  98. directory gitlab_ci_dir do
  99. owner gitlab_user
  100. mode '0755'
  101. recursive true
  102. end
  103. template File.join(gitlab_rails_static_etc_dir, "gitlab-rails-rc")
  104. dependent_services = []
  105. dependent_services << "service[unicorn]" if OmnibusHelper.should_notify?("unicorn")
  106. dependent_services << "service[sidekiq]" if OmnibusHelper.should_notify?("sidekiq")
  107. dependent_services << "service[mailroom]" if node['gitlab']['mailroom']['enable']
  108. redis_not_listening = OmnibusHelper.not_listening?("redis")
  109. postgresql_not_listening = OmnibusHelper.not_listening?("postgresql")
  110. template_symlink File.join(gitlab_rails_etc_dir, "secret") do
  111. link_from File.join(gitlab_rails_source_dir, ".secret")
  112. source "secret_token.erb"
  113. owner "root"
  114. group "root"
  115. mode "0644"
  116. variables(node['gitlab']['gitlab-rails'].to_hash)
  117. restarts dependent_services
  118. end
  119. template_symlink File.join(gitlab_rails_etc_dir, "database.yml") do
  120. link_from File.join(gitlab_rails_source_dir, "config/database.yml")
  121. source "database.yml.erb"
  122. owner "root"
  123. group "root"
  124. mode "0644"
  125. variables node['gitlab']['gitlab-rails'].to_hash
  126. helpers SingleQuoteHelper
  127. restarts dependent_services
  128. end
  129. if node['gitlab']['gitlab-rails']['redis_port']
  130. redis_auth = ":#{node['gitlab']['gitlab-rails']['redis_password']}@" if node['gitlab']['gitlab-rails']['redis_password']
  131. redis_url = "redis://#{redis_auth}#{node['gitlab']['gitlab-rails']['redis_host']}:#{node['gitlab']['gitlab-rails']['redis_port']}"
  132. else
  133. redis_url = "unix:#{node['gitlab']['gitlab-rails']['redis_socket']}"
  134. end
  135. gitlab_rails = if node['gitlab']['gitlab-ci']['db_key_base']
  136. node['gitlab']['gitlab-rails'].to_hash.merge!({ db_key_base: node['gitlab']['gitlab-ci']['db_key_base'] })
  137. else
  138. node['gitlab']['gitlab-rails']
  139. end
  140. template_symlink File.join(gitlab_rails_etc_dir, "secrets.yml") do
  141. link_from File.join(gitlab_rails_source_dir, "config/secrets.yml")
  142. source "secrets.yml.erb"
  143. owner "root"
  144. group "root"
  145. mode "0644"
  146. variables(gitlab_rails.to_hash)
  147. helpers SingleQuoteHelper
  148. restarts dependent_services
  149. end
  150. template_symlink File.join(gitlab_rails_etc_dir, "resque.yml") do
  151. link_from File.join(gitlab_rails_source_dir, "config/resque.yml")
  152. source "resque.yml.erb"
  153. owner "root"
  154. group "root"
  155. mode "0644"
  156. variables(:redis_url => redis_url)
  157. restarts dependent_services
  158. end
  159. template_symlink File.join(gitlab_rails_etc_dir, "aws.yml") do
  160. link_from File.join(gitlab_rails_source_dir, "config/aws.yml")
  161. owner "root"
  162. group "root"
  163. mode "0644"
  164. variables(node['gitlab']['gitlab-rails'].to_hash)
  165. restarts dependent_services
  166. unless node['gitlab']['gitlab-rails']['aws_enable']
  167. action :delete
  168. end
  169. end
  170. template_symlink File.join(gitlab_rails_etc_dir, "smtp_settings.rb") do
  171. link_from File.join(gitlab_rails_source_dir, "config/initializers/smtp_settings.rb")
  172. owner "root"
  173. group "root"
  174. mode "0644"
  175. variables(
  176. node['gitlab']['gitlab-rails'].to_hash.merge(
  177. :app => gitlab_app
  178. )
  179. )
  180. restarts dependent_services
  181. unless node['gitlab']['gitlab-rails']['smtp_enable']
  182. action :delete
  183. end
  184. end
  185. template_symlink File.join(gitlab_rails_etc_dir, "relative_url.rb") do
  186. link_from File.join(gitlab_rails_source_dir, "config/initializers/relative_url.rb")
  187. owner "root"
  188. group "root"
  189. mode "0644"
  190. variables(node['gitlab']['gitlab-rails'].to_hash)
  191. notifies :run, 'bash[generate assets]'
  192. restarts dependent_services
  193. unless node['gitlab']['gitlab-rails']['gitlab_relative_url']
  194. action :delete
  195. end
  196. end
  197. template_symlink File.join(gitlab_rails_etc_dir, "gitlab.yml") do
  198. link_from File.join(gitlab_rails_source_dir, "config/gitlab.yml")
  199. source "gitlab.yml.erb"
  200. helpers SingleQuoteHelper
  201. owner "root"
  202. group "root"
  203. mode "0644"
  204. variables(
  205. node['gitlab']['gitlab-rails'].to_hash.merge(
  206. gitlab_ci_all_broken_builds: node['gitlab']['gitlab-ci']['gitlab_ci_all_broken_builds'],
  207. gitlab_ci_add_pusher: node['gitlab']['gitlab-ci']['gitlab_ci_add_pusher'],
  208. builds_directory: gitlab_ci_builds_dir,
  209. git_annex_enabled: node['gitlab']['gitlab-shell']['git_annex_enabled'],
  210. pages_external_http: node['gitlab']['gitlab-pages']['external_http'],
  211. pages_external_https: node['gitlab']['gitlab-pages']['external_https']
  212. )
  213. )
  214. restarts dependent_services
  215. notifies :run, 'execute[clear the gitlab-rails cache]' unless redis_not_listening
  216. end
  217. template_symlink File.join(gitlab_rails_etc_dir, "rack_attack.rb") do
  218. link_from File.join(gitlab_rails_source_dir, "config/initializers/rack_attack.rb")
  219. source "rack_attack.rb.erb"
  220. owner "root"
  221. group "root"
  222. mode "0644"
  223. variables(node['gitlab']['gitlab-rails'].to_hash)
  224. restarts dependent_services
  225. end
  226. link File.join(gitlab_rails_source_dir, ".gitlab_shell_secret") do
  227. to File.join(gitlab_shell_source_dir, ".gitlab_shell_secret")
  228. end
  229. env_dir File.join(gitlab_rails_static_etc_dir, 'env') do
  230. variables(
  231. {
  232. 'HOME' => node['gitlab']['user']['home'],
  233. 'RAILS_ENV' => node['gitlab']['gitlab-rails']['environment'],
  234. }.merge(node['gitlab']['gitlab-rails']['env'])
  235. )
  236. restarts dependent_services
  237. end
  238. # replace empty directories in the Git repo with symlinks to /var/opt/gitlab
  239. {
  240. "/opt/gitlab/embedded/service/gitlab-rails/tmp" => gitlab_rails_tmp_dir,
  241. "/opt/gitlab/embedded/service/gitlab-rails/public/uploads" => gitlab_rails_public_uploads_dir,
  242. "/opt/gitlab/embedded/service/gitlab-rails/log" => gitlab_rails_log_dir
  243. }.each do |link_dir, target_dir|
  244. link link_dir do
  245. to target_dir
  246. end
  247. end
  248. legacy_sidekiq_log_file = File.join(gitlab_rails_log_dir, 'sidekiq.log')
  249. link legacy_sidekiq_log_file do
  250. to File.join(node['gitlab']['sidekiq']['log_directory'], 'current')
  251. not_if { File.exists?(legacy_sidekiq_log_file) }
  252. end
  253. # Make schema.rb writable for when we run `rake db:migrate`
  254. file "/opt/gitlab/embedded/service/gitlab-rails/db/schema.rb" do
  255. owner gitlab_user
  256. end
  257. # Link the VERSION file just for easier administration
  258. remote_file File.join(gitlab_rails_dir, 'VERSION') do
  259. source "file:///opt/gitlab/embedded/service/gitlab-rails/VERSION"
  260. end
  261. # Only run `rake db:migrate` when the gitlab-rails version has changed
  262. # Or migration failed for some reason
  263. remote_file File.join(gitlab_rails_dir, 'REVISION') do
  264. source "file:///opt/gitlab/embedded/service/gitlab-rails/REVISION"
  265. notifies :run, 'bash[generate assets]' if node['gitlab']['gitlab-rails']['gitlab_relative_url']
  266. end
  267. # If a version of ruby changes restart unicorn. If not, unicorn will fail to
  268. # reload until restarted
  269. file File.join(gitlab_rails_dir, "RUBY_VERSION") do
  270. content VersionHelper.version("/opt/gitlab/embedded/bin/ruby --version")
  271. notifies :restart, "service[unicorn]"
  272. end
  273. # We shipped packages with 'chown -R git' below for quite some time. That chown
  274. # was an unnecessary leftover from the manual installation guide; it is better
  275. # to just leave these files owned by root. If we just remove the 'chown git',
  276. # existing installations will keep 'git' as the owner, so we now explicitly
  277. # change the owner to root:root. Once we feel confident that enough versions
  278. # have been shipped we can maybe get rid of this 'chown' at some point.
  279. execute "chown -R root:root /opt/gitlab/embedded/service/gitlab-rails/public"
  280. execute "clear the gitlab-rails cache" do
  281. command "/opt/gitlab/bin/gitlab-rake cache:clear"
  282. action :nothing
  283. end
  284. bash "generate assets" do
  285. code <<-EOS
  286. set -e
  287. /opt/gitlab/bin/gitlab-rake assets:clean assets:precompile
  288. chown -R #{gitlab_user}:#{gitlab_group} #{gitlab_rails_tmp_dir}/cache
  289. EOS
  290. # We have to precompile assets as root because of permissions and ownership of files
  291. environment ({ 'NO_PRIVILEGE_DROP' => 'true', 'USE_DB' => 'false' })
  292. dependent_services.each do |sv|
  293. notifies :restart, sv
  294. end
  295. action :nothing
  296. end
  297. bitbucket_keys = node['gitlab']['gitlab-rails']['bitbucket']
  298. unless bitbucket_keys.nil?
  299. execute 'trust bitbucket.org fingerprint' do
  300. command "echo '#{bitbucket_keys['known_hosts_key']}' >> #{known_hosts}"
  301. user gitlab_user
  302. group gitlab_group
  303. not_if "grep '#{bitbucket_keys['known_hosts_key']}' #{known_hosts}"
  304. end
  305. file File.join(ssh_dir, 'bitbucket_rsa') do
  306. content "#{bitbucket_keys['private_key']}\n"
  307. owner gitlab_user
  308. group gitlab_group
  309. mode 0600
  310. end
  311. ssh_config_file = File.join(ssh_dir, 'config')
  312. bitbucket_host_config = "Host bitbucket.org\n IdentityFile ~/.ssh/bitbucket_rsa\n User #{node['gitlab']['user']['username']}"
  313. execute 'manage config for bitbucket import key' do
  314. command "echo '#{bitbucket_host_config}' >> #{ssh_config_file}"
  315. user gitlab_user
  316. group gitlab_group
  317. not_if "grep 'IdentityFile ~/.ssh/bitbucket_rsa' #{ssh_config_file}"
  318. end
  319. file File.join(ssh_dir, 'bitbucket_rsa.pub') do
  320. content "#{bitbucket_keys['public_key']}\n"
  321. owner gitlab_user
  322. group gitlab_group
  323. mode 0644
  324. end
  325. end
  326. #
  327. # Up to release 8.6 default config.ru was replaced with omnibus-based one.
  328. # After 8.6 this is not necessery. We can remove this file.
  329. #
  330. file File.join(gitlab_rails_dir, 'config.ru') do
  331. action :delete
  332. end