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