]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - modules/role/manifests/cryptoportfolio.pp
Merge branch 'xmr_stak'
[perso/Immae/Projets/Puppet.git] / modules / role / manifests / cryptoportfolio.pp
1 class role::cryptoportfolio {
2 ensure_resource('exec', 'systemctl daemon-reload', {
3 command => '/usr/bin/systemctl daemon-reload',
4 refreshonly => true
5 })
6
7 include "base_installation"
8
9 include "profile::tools"
10 include "profile::postgresql"
11 include "profile::apache"
12 include "profile::xmr_stak"
13
14 $password_seed = lookup("base_installation::puppet_pass_seed") |$key| { {} }
15
16 $cf_pg_user = "cryptoportfolio"
17 $cf_pg_user_replication = "cryptoportfolio_replication"
18 $cf_pg_db = "cryptoportfolio"
19 $cf_pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio")
20 $cf_pg_replication_password = generate_password(24, $password_seed, "postgres_cryptoportfolio_replication")
21 $cf_pg_host = "localhost:5432"
22
23 $cf_user = "cryptoportfolio"
24 $cf_group = "cryptoportfolio"
25 $cf_home = "/opt/cryptoportfolio"
26 $cf_env = "prod"
27 $cf_front_app_host = "cryptoportfolio.immae.eu"
28 $cf_front_app_port = ""
29 $cf_front_app_ssl = "true"
30 $cf_front_app = "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front"
31 $cf_front_app_api_workdir = "${cf_front_app}/cmd/app"
32 $cf_front_app_api_bin = "${cf_front_app_api_workdir}/cryptoportfolio-app"
33 $cf_front_app_api_conf = "${cf_home}/conf.toml"
34 $cf_front_app_api_secret = generate_password(24, $password_seed, "cryptoportfolio_api_secret")
35
36 $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
37
38 file { "/var/lib/postgres/data/certs":
39 ensure => directory,
40 mode => "0700",
41 owner => $::profile::postgresql::pg_user,
42 group => $::profile::postgresql::pg_user,
43 require => File["/var/lib/postgres"],
44 }
45
46 file { "/var/lib/postgres/data/certs/cert.pem":
47 source => "file:///etc/letsencrypt/live/$cf_front_app_host/cert.pem",
48 mode => "0600",
49 links => "follow",
50 owner => $::profile::postgresql::pg_user,
51 group => $::profile::postgresql::pg_user,
52 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
53 }
54
55 file { "/var/lib/postgres/data/certs/privkey.pem":
56 source => "file:///etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
57 mode => "0600",
58 links => "follow",
59 owner => $::profile::postgresql::pg_user,
60 group => $::profile::postgresql::pg_user,
61 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
62 }
63
64 postgresql::server::config_entry { "wal_level":
65 value => "logical",
66 }
67
68 postgresql::server::config_entry { "ssl":
69 value => "on",
70 require => Letsencrypt::Certonly[$cf_front_app_host],
71 }
72
73 postgresql::server::config_entry { "ssl_cert_file":
74 value => "/var/lib/postgres/data/certs/cert.pem",
75 require => Letsencrypt::Certonly[$cf_front_app_host],
76 }
77
78 postgresql::server::config_entry { "ssl_key_file":
79 value => "/var/lib/postgres/data/certs/privkey.pem",
80 require => Letsencrypt::Certonly[$cf_front_app_host],
81 }
82
83 postgresql::server::db { $cf_pg_db:
84 user => $cf_pg_user,
85 password => postgresql_password($cf_pg_user, $cf_pg_password),
86 }
87 ->
88 postgresql_psql { "CREATE PUBLICATION ${cf_pg_db}_publication FOR ALL TABLES":
89 db => $cf_pg_db,
90 unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${cf_pg_db}_publication'",
91 }
92 ->
93 postgresql::server::role { $cf_pg_user_replication:
94 db => $cf_pg_db,
95 replication => true,
96 password_hash => postgresql_password($cf_pg_user_replication, $cf_pg_replication_password),
97 }
98 ->
99 postgresql::server::database_grant { $cf_pg_user_replication:
100 db => $cf_pg_db,
101 privilege => "CONNECT",
102 role => $cf_pg_user_replication,
103 }
104 ->
105 postgresql::server::grant { "all tables in schema:public:$cf_pg_user_replication":
106 db => $cf_pg_db,
107 role => $cf_pg_user_replication,
108 privilege => "SELECT",
109 object_type => "ALL TABLES IN SCHEMA",
110 object_name => "public",
111 }
112 ->
113 postgresql::server::grant { "all sequences in schema:public:$cf_pg_user_replication":
114 db => $cf_pg_db,
115 role => $cf_pg_user_replication,
116 privilege => "SELECT",
117 object_type => "ALL SEQUENCES IN SCHEMA",
118 object_name => "public",
119 }
120
121 postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
122 type => 'host',
123 database => $cf_pg_db,
124 user => $cf_pg_user,
125 address => '127.0.0.1/32',
126 auth_method => 'md5',
127 order => "b0",
128 }
129 postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
130 type => 'host',
131 database => $cf_pg_db,
132 user => $cf_pg_user,
133 address => '::1/128',
134 auth_method => 'md5',
135 order => "b0",
136 }
137
138 postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
139 type => 'hostssl',
140 database => $cf_pg_db,
141 user => $cf_pg_user_replication,
142 address => 'immae.eu',
143 auth_method => 'md5',
144 order => "b0",
145 }
146
147 letsencrypt::certonly { $cf_front_app_host: ;
148 default: * => $::profile::apache::letsencrypt_certonly_default;
149 }
150
151 class { 'apache::mod::headers': }
152 apache::vhost { $cf_front_app_host:
153 port => '443',
154 docroot => false,
155 manage_docroot => false,
156 proxy_dest => "http://localhost:8000",
157 request_headers => 'set X-Forwarded-Proto "https"',
158 ssl => true,
159 ssl_cert => "/etc/letsencrypt/live/$cf_front_app_host/cert.pem",
160 ssl_key => "/etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
161 ssl_chain => "/etc/letsencrypt/live/$cf_front_app_host/chain.pem",
162 require => Letsencrypt::Certonly[$cf_front_app_host],
163 proxy_preserve_host => true;
164 default: * => $::profile::apache::apache_vhost_default;
165 }
166
167 user { $cf_user:
168 name => $cf_user,
169 ensure => "present",
170 managehome => true,
171 home => $cf_home,
172 system => true,
173 password => '!!',
174 }
175
176 $front_version = lookup("cryptoportfolio::front_version") |$key| { {} }
177 $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} }
178
179 unless empty($front_version) {
180 ensure_packages(["go", "npm", "nodejs", "yarn"])
181
182 file { [
183 "${cf_home}/go/",
184 "${cf_home}/go/src",
185 "${cf_home}/go/src/immae.eu",
186 "${cf_home}/go/src/immae.eu/Immae",
187 "${cf_home}/go/src/immae.eu/Immae/Projets",
188 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies",
189 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio",
190 $cf_front_app]:
191 ensure => "directory",
192 mode => "0700",
193 owner => $cf_user,
194 group => $cf_group,
195 require => User[$cf_user],
196 }
197
198 archive { "${cf_home}/${front_version}.tar.gz":
199 path => "${cf_home}/${front_version}.tar.gz",
200 source => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz",
201 checksum_type => "sha256",
202 checksum => $front_sha256,
203 cleanup => false,
204 extract => true,
205 user => "cryptoportfolio",
206 extract_path => $cf_front_app,
207 require => [User[$cf_user], File[$cf_front_app]],
208 }
209
210 file { "${cf_home}/front":
211 ensure => "link",
212 target => $cf_front_app,
213 before => File[$cf_front_app],
214 } ~>
215 exec { "remove old ${cf_front_app} directory":
216 refreshonly => true,
217 user => $cf_user,
218 command => "/usr/bin/rm -rf ${cf_front_app}",
219 before => File[$cf_front_app],
220 }
221
222 exec { "go-get-dep":
223 user => $cf_user,
224 environment => ["HOME=${cf_home}"],
225 creates => "${cf_home}/go/bin/dep",
226 command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep",
227 require => User[$cf_user],
228 }
229
230 exec { "go-cryptoportfolio-dependencies":
231 cwd => $cf_front_app,
232 user => $cf_user,
233 environment => ["HOME=${cf_home}"],
234 creates => "${cf_front_app}/vendor",
235 command => "${cf_home}/go/bin/dep ensure",
236 require => [Exec["go-get-dep"], Archive["${cf_home}/${front_version}.tar.gz"]],
237 }
238
239 exec { "go-cryptoportfolio-app":
240 cwd => $cf_front_app_api_workdir,
241 user => $cf_user,
242 environment => ["HOME=${cf_home}"],
243 creates => $cf_front_app_api_bin,
244 command => "/usr/bin/make build",
245 require => Exec["go-cryptoportfolio-dependencies"],
246 }
247
248 file { "/etc/systemd/system/cryptoportfolio-app.service":
249 mode => "0644",
250 owner => "root",
251 group => "root",
252 content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
253 notify => Exec["systemctl daemon-reload"],
254 }
255
256 service { 'cryptoportfolio-app':
257 enable => true,
258 ensure => "running",
259 subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
260 require => [
261 File["/etc/systemd/system/cryptoportfolio-app.service"],
262 Postgresql::Server::Db[$cf_pg_db]
263 ],
264 } ~>
265 exec { "dump $cf_pg_db structure":
266 refreshonly => true,
267 user => $::profile::postgresql::pg_user,
268 group => $::profile::postgresql::pg_user,
269 command => "/usr/bin/pg_dump --schema-only --clean --no-publications $cf_pg_db > /var/lib/postgres/${cf_pg_db}.schema",
270 }
271
272 file { $cf_front_app_api_conf:
273 owner => $cf_user,
274 group => $cf_group,
275 mode => "0600",
276 content => template("role/cryptoportfolio/api_conf.toml.erb"),
277 }
278
279 file { $cf_front_app_static_conf:
280 owner => $cf_user,
281 group => $cf_group,
282 mode => "0600",
283 content => template("role/cryptoportfolio/static_conf.env.erb"),
284 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
285 }
286
287 exec { "web-cryptoportfolio-dependencies":
288 cwd => "${cf_front_app}/cmd/web",
289 environment => ["HOME=${cf_home}"],
290 command => "/usr/bin/make install",
291 creates => "${cf_front_app}/cmd/web/node_modules",
292 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
293 require => [Package["npm"], Package["nodejs"], Package["yarn"]]
294 }
295
296 exec { "remove build ${cf_front_app}/cmd/web/build/":
297 command => "/usr/bin/rm -rf '${cf_front_app}/cmd/web/build/'",
298 refreshonly => true,
299 before => Exec["web-cryptoportfolio-build"]
300 }
301
302 exec { "web-cryptoportfolio-build":
303 cwd => "${cf_front_app}/cmd/web",
304 environment => ["HOME=${cf_home}"],
305 command => "/usr/bin/make static ENV=${cf_env}",
306 creates => "${cf_front_app}/cmd/web/build/static",
307 require => [File[$cf_front_app_static_conf], Exec["web-cryptoportfolio-dependencies"]]
308 }
309 }
310 }