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