]> git.openstreetmap.org Git - chef.git/blob - cookbooks/osqa/recipes/default.rb
Merge remote-tracking branch 'github/pull/422'
[chef.git] / cookbooks / osqa / recipes / default.rb
1 #
2 # Cookbook:: osqa
3 # Recipe:: default
4 #
5 # Copyright:: 2011, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 include_recipe "accounts"
21 include_recipe "apache"
22 include_recipe "memcached"
23 include_recipe "python"
24 include_recipe "tools"
25
26 package "python-dev"
27 package "libmysqlclient-dev"
28 package "libpq-dev"
29
30 python_directory = "/opt/osqa-python"
31
32 python_virtualenv python_directory do
33   interpreter "/usr/bin/python2"
34 end
35
36 python_package "Django" do
37   python_virtualenv python_directory
38   version "1.6.11"
39 end
40
41 python_package "Markdown" do
42   python_virtualenv python_directory
43   version "2.4"
44 end
45
46 python_package "python-memcached" do
47   python_virtualenv python_directory
48   version "1.53"
49 end
50
51 python_package "python-openid" do
52   python_virtualenv python_directory
53   version "2.2.5"
54 end
55
56 python_package "psycopg2" do
57   python_virtualenv python_directory
58   version "2.7.6.1"
59 end
60
61 python_package "South" do
62   python_virtualenv python_directory
63   version "0.7.6"
64 end
65
66 python_package "html5lib" do
67   python_virtualenv python_directory
68   version "0.999"
69 end
70
71 apache_module "rewrite"
72 apache_module "wsgi"
73
74 node[:osqa][:sites].each do |site|
75   site_name = site[:name]
76   site_aliases = site[:aliases] || []
77   directory = site[:directory] || "/srv/#{site_name}"
78   site_user = site[:user] || node[:osqa][:user]
79   site_user = Etc.getpwuid(site_user).name if site_user.is_a?(Integer)
80   site_group = site[:group] || node[:osqa][:group] || Etc.getpwnam(site_user).gid
81   site_group = Etc.getgrgid(site_group).name if site_group.is_a?(Integer)
82   database_name = site[:database_name] || node[:osqa][:database_name]
83   database_user = site[:database_user] || node[:osqa][:database_user]
84   database_password = site[:database_user] || node[:osqa][:database_password]
85   backup_name = site[:backup]
86
87   ssl_certificate site_name do
88     domains [site_name] + site_aliases
89     notifies :reload, "service[apache2]"
90   end
91
92   apache_site site_name do
93     template "apache.erb"
94     directory directory
95     variables :user => site_user, :group => site_group, :aliases => site_aliases, :python_home => python_directory
96   end
97
98   directory directory do
99     owner site_user
100     group site_group
101     mode "755"
102   end
103
104   execute "osqa-migrate" do
105     action :nothing
106     command "python manage.py migrate forum"
107     cwd "#{directory}/osqa"
108     user site_user
109     group site_group
110     notifies :reload, "service[apache2]"
111   end
112
113   git "#{directory}/osqa" do
114     action :sync
115     repository "https://git.openstreetmap.org/public/osqa.git"
116     revision "live"
117     depth 1
118     user site_user
119     group site_group
120     notifies :run, "execute[osqa-migrate]"
121   end
122
123   directory "#{directory}/upfiles" do
124     user site_user
125     group site_group
126     mode "755"
127   end
128
129   template "#{directory}/osqa/osqa.wsgi" do
130     source "osqa.wsgi.erb"
131     owner site_user
132     group site_group
133     mode "644"
134     variables :directory => directory
135     notifies :reload, "service[apache2]"
136   end
137
138   settings = edit_file "#{directory}/osqa/settings_local.py.dist" do |line|
139     line.gsub!(/^( *)'ENGINE': '.*',/, "\\1'ENGINE': 'django.db.backends.postgresql_psycopg2',")
140     line.gsub!(/^( *)'NAME': '.*',/, "\\1'NAME': '#{database_name}',")
141     line.gsub!(/^( *)'USER': '.*',/, "\\1'USER': '#{database_user}',")
142     line.gsub!(/^( *)'PASSWORD': '.*',/, "\\1'PASSWORD': '#{database_password}',")
143     line.gsub!(/^ALLOWED_HOSTS = .*/, "ALLOWED_HOSTS = ('help.openstreetmap.org',)")
144     line.gsub!(/^CACHE_BACKEND = .*/, "CACHE_BACKEND = 'memcached://127.0.0.1:11211/'")
145     line.gsub!(%r{^APP_URL = 'http://'}, "APP_URL = 'https://#{site_name}'")
146     line.gsub!(%r{^TIME_ZONE = 'America/New_York'}, "TIME_ZONE = 'Europe/London'")
147     line.gsub!(/^DISABLED_MODULES = \[([^\]]+)\]/, "DISABLED_MODULES = [\\1, 'localauth', 'facebookauth', 'oauthauth']")
148
149     line
150   end
151
152   file "#{directory}/osqa/settings_local.py" do
153     owner site_user
154     group site_group
155     mode "644"
156     content settings
157     notifies :reload, "service[apache2]"
158   end
159
160   template "/etc/cron.daily/#{backup_name}-backup" do
161     source "backup.cron.erb"
162     owner "root"
163     group "root"
164     mode "755"
165     variables :name => backup_name, :directory => directory, :user => site_user, :database => database_name
166   end
167 end