]>
Commit | Line | Data |
---|---|---|
57ae81ea | 1 | class role::cryptoportfolio { |
68bbff61 IB |
2 | ensure_resource('exec', 'systemctl daemon-reload', { |
3 | command => '/usr/bin/systemctl daemon-reload', | |
4 | refreshonly => true | |
5 | }) | |
6 | ||
57ae81ea IB |
7 | include "base_installation" |
8 | ||
8af3ea1e | 9 | include "profile::tools" |
57ae81ea | 10 | include "profile::postgresql" |
2bb35074 | 11 | include "profile::apache" |
68bbff61 | 12 | include "profile::xmr_stak" |
57ae81ea IB |
13 | |
14 | $password_seed = lookup("base_installation::puppet_pass_seed") |$key| { {} } | |
15 | ||
159df176 | 16 | $cf_pg_user = "cryptoportfolio" |
b3015828 | 17 | $cf_pg_user_replication = "cryptoportfolio_replication" |
159df176 IB |
18 | $cf_pg_db = "cryptoportfolio" |
19 | $cf_pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio") | |
b3015828 | 20 | $cf_pg_replication_password = generate_password(24, $password_seed, "postgres_cryptoportfolio_replication") |
c49d00da IB |
21 | $cf_pg_hostname = "localhost" |
22 | $cf_pg_port = "5432" | |
23 | $cf_pg_host = "${cf_pg_hostname}:${cf_pg_port}" | |
159df176 IB |
24 | |
25 | $cf_user = "cryptoportfolio" | |
26 | $cf_group = "cryptoportfolio" | |
27 | $cf_home = "/opt/cryptoportfolio" | |
28 | $cf_env = "prod" | |
85abd2fd | 29 | $cf_front_app_host = lookup("base_installation::system_hostname") |$key| { "example.com" } |
159df176 | 30 | $cf_front_app_port = "" |
e17078be | 31 | $cf_front_app_ssl = "true" |
159df176 IB |
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 | ||
236d7e6d | 40 | $cf_bot_app = "${cf_home}/bot" |
c49d00da IB |
41 | $cf_bot_app_conf = "${cf_home}/bot_config.ini" |
42 | $cf_bot_app_reports = "${cf_home}/bot_reports" | |
236d7e6d | 43 | |
b3015828 IB |
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 | ||
159df176 IB |
89 | postgresql::server::db { $cf_pg_db: |
90 | user => $cf_pg_user, | |
b3015828 IB |
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", | |
57ae81ea IB |
125 | } |
126 | ||
6a919776 IB |
127 | postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user': |
128 | type => 'host', | |
159df176 IB |
129 | database => $cf_pg_db, |
130 | user => $cf_pg_user, | |
6a919776 IB |
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', | |
159df176 IB |
137 | database => $cf_pg_db, |
138 | user => $cf_pg_user, | |
6a919776 IB |
139 | address => '::1/128', |
140 | auth_method => 'md5', | |
141 | order => "b0", | |
142 | } | |
0a4ec379 | 143 | |
b3015828 IB |
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 | ||
e345248b IB |
153 | letsencrypt::certonly { $cf_front_app_host: ; |
154 | default: * => $::profile::apache::letsencrypt_certonly_default; | |
155 | } | |
156 | ||
157 | class { 'apache::mod::headers': } | |
2bb35074 | 158 | apache::vhost { $cf_front_app_host: |
e345248b | 159 | port => '443', |
2bb35074 IB |
160 | docroot => false, |
161 | manage_docroot => false, | |
162 | proxy_dest => "http://localhost:8000", | |
e345248b IB |
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; | |
0a4ec379 | 171 | } |
f67c5285 | 172 | |
159df176 IB |
173 | user { $cf_user: |
174 | name => $cf_user, | |
76a321e1 IB |
175 | ensure => "present", |
176 | managehome => true, | |
159df176 | 177 | home => $cf_home, |
76a321e1 IB |
178 | system => true, |
179 | password => '!!', | |
180 | } | |
181 | ||
182 | $front_version = lookup("cryptoportfolio::front_version") |$key| { {} } | |
183 | $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} } | |
184 | ||
236d7e6d IB |
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 | ||
3f868d0b IB |
199 | archive { "${cf_home}/trader_${bot_version}.tar.gz": |
200 | path => "${cf_home}/trader_${bot_version}.tar.gz", | |
236d7e6d IB |
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, | |
c17e63ed | 206 | user => $cf_user, |
236d7e6d IB |
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, | |
2542900d IB |
219 | before => [ |
220 | File[$cf_bot_app_conf], | |
221 | Cron["py-cryptoportfolio-before"], | |
222 | Cron["py-cryptoportfolio-after"], | |
223 | ] | |
236d7e6d | 224 | } |
c49d00da IB |
225 | |
226 | file { $cf_bot_app_conf: | |
227 | owner => $cf_user, | |
228 | group => $cf_group, | |
229 | mode => "0600", | |
9a6b0098 IB |
230 | content => template("role/cryptoportfolio/bot_config.ini.erb"), |
231 | require => [ | |
232 | User[$cf_user], | |
3f868d0b | 233 | Archive["${cf_home}/trader_${bot_version}.tar.gz"], |
9a6b0098 | 234 | ], |
c49d00da | 235 | } |
2542900d IB |
236 | |
237 | cron { "py-cryptoportfolio-before": | |
238 | ensure => present, | |
239 | command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --before", | |
240 | user => "cryptoportfolio", | |
241 | weekday => 7, # Sunday | |
242 | hour => 22, | |
243 | minute => 30, | |
244 | environment => ["HOME=${cf_home}","PATH=/usr/bin/"], | |
245 | require => [ | |
246 | File[$cf_bot_app_conf], | |
3f868d0b | 247 | Archive["${cf_home}/trader_${bot_version}.tar.gz"] |
2542900d IB |
248 | ], |
249 | } | |
250 | ||
251 | cron { "py-cryptoportfolio-after": | |
252 | ensure => present, | |
253 | command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --after", | |
254 | user => "cryptoportfolio", | |
255 | weekday => 1, # Monday | |
256 | hour => 1, | |
9a6b0098 | 257 | minute => 0, |
2542900d IB |
258 | environment => ["HOME=${cf_home}","PATH=/usr/bin/"], |
259 | require => [ | |
260 | File[$cf_bot_app_conf], | |
3f868d0b | 261 | Archive["${cf_home}/trader_${bot_version}.tar.gz"] |
2542900d IB |
262 | ], |
263 | } | |
236d7e6d IB |
264 | } |
265 | ||
9a6b0098 | 266 | # FIXME: restore backup |
76a321e1 | 267 | unless empty($front_version) { |
159df176 | 268 | ensure_packages(["go", "npm", "nodejs", "yarn"]) |
76a321e1 | 269 | |
159df176 IB |
270 | file { [ |
271 | "${cf_home}/go/", | |
272 | "${cf_home}/go/src", | |
273 | "${cf_home}/go/src/immae.eu", | |
274 | "${cf_home}/go/src/immae.eu/Immae", | |
275 | "${cf_home}/go/src/immae.eu/Immae/Projets", | |
276 | "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies", | |
277 | "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio", | |
278 | $cf_front_app]: | |
279 | ensure => "directory", | |
76a321e1 | 280 | mode => "0700", |
159df176 IB |
281 | owner => $cf_user, |
282 | group => $cf_group, | |
283 | require => User[$cf_user], | |
76a321e1 IB |
284 | } |
285 | ||
159df176 | 286 | file { "${cf_home}/front": |
76a321e1 | 287 | ensure => "link", |
159df176 | 288 | target => $cf_front_app, |
f099c453 | 289 | before => File[$cf_front_app], |
159df176 IB |
290 | } |
291 | ||
292 | file { "/etc/systemd/system/cryptoportfolio-app.service": | |
293 | mode => "0644", | |
294 | owner => "root", | |
295 | group => "root", | |
296 | content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"), | |
68bbff61 | 297 | notify => Exec["systemctl daemon-reload"], |
159df176 IB |
298 | } |
299 | ||
300 | service { 'cryptoportfolio-app': | |
f099c453 IB |
301 | enable => true, |
302 | ensure => "running", | |
303 | subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]], | |
304 | require => [ | |
b3015828 IB |
305 | File["/etc/systemd/system/cryptoportfolio-app.service"], |
306 | Postgresql::Server::Db[$cf_pg_db] | |
307 | ], | |
43c4ee4e IB |
308 | } ~> |
309 | exec { "dump $cf_pg_db structure": | |
310 | refreshonly => true, | |
311 | user => $::profile::postgresql::pg_user, | |
312 | group => $::profile::postgresql::pg_user, | |
313 | command => "/usr/bin/pg_dump --schema-only --clean --no-publications $cf_pg_db > /var/lib/postgres/${cf_pg_db}.schema", | |
159df176 IB |
314 | } |
315 | ||
3f868d0b IB |
316 | archive { "${cf_home}/front_${front_version}.tar.gz": |
317 | path => "${cf_home}/front_${front_version}.tar.gz", | |
383fa18a IB |
318 | source => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz", |
319 | checksum_type => "sha256", | |
320 | checksum => $front_sha256, | |
321 | cleanup => false, | |
322 | extract => true, | |
323 | user => $cf_user, | |
324 | username => $facts["ec2_metadata"]["hostname"], | |
325 | password => generate_password(24, $password_seed, "ldap"), | |
326 | extract_path => $cf_front_app, | |
327 | require => [User[$cf_user], File[$cf_front_app]], | |
328 | notify => [ | |
329 | Exec["web-cryptoportfolio-dependencies"], | |
330 | Exec["go-get-dep"], | |
331 | ] | |
332 | } | |
333 | ||
334 | # Api | |
159df176 IB |
335 | file { $cf_front_app_api_conf: |
336 | owner => $cf_user, | |
337 | group => $cf_group, | |
338 | mode => "0600", | |
339 | content => template("role/cryptoportfolio/api_conf.toml.erb"), | |
383fa18a IB |
340 | before => Exec["go-cryptoportfolio-app"], |
341 | } | |
342 | ||
343 | exec { "go-get-dep": | |
344 | user => $cf_user, | |
345 | environment => ["HOME=${cf_home}"], | |
346 | creates => "${cf_home}/go/bin/dep", | |
347 | command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep", | |
348 | refreshonly => true, | |
349 | } ~> | |
350 | exec { "go-cryptoportfolio-dependencies": | |
351 | cwd => $cf_front_app, | |
352 | user => $cf_user, | |
353 | environment => ["HOME=${cf_home}"], | |
354 | command => "${cf_home}/go/bin/dep ensure", | |
355 | refreshonly => true, | |
356 | } ~> | |
357 | exec { "go-cryptoportfolio-app": | |
358 | cwd => $cf_front_app_api_workdir, | |
359 | user => $cf_user, | |
360 | environment => ["HOME=${cf_home}"], | |
361 | command => "/usr/bin/make build", | |
362 | refreshonly => true, | |
159df176 IB |
363 | } |
364 | ||
383fa18a | 365 | # Static pages |
159df176 IB |
366 | file { $cf_front_app_static_conf: |
367 | owner => $cf_user, | |
368 | group => $cf_group, | |
369 | mode => "0600", | |
370 | content => template("role/cryptoportfolio/static_conf.env.erb"), | |
383fa18a | 371 | before => Exec["web-cryptoportfolio-build"], |
159df176 IB |
372 | } |
373 | ||
374 | exec { "web-cryptoportfolio-dependencies": | |
375 | cwd => "${cf_front_app}/cmd/web", | |
c17e63ed | 376 | user => $cf_user, |
159df176 IB |
377 | environment => ["HOME=${cf_home}"], |
378 | command => "/usr/bin/make install", | |
e17078be | 379 | refreshonly => true, |
383fa18a IB |
380 | require => [Package["npm"], Package["nodejs"], Package["yarn"]] |
381 | } ~> | |
159df176 IB |
382 | exec { "web-cryptoportfolio-build": |
383 | cwd => "${cf_front_app}/cmd/web", | |
c17e63ed | 384 | user => $cf_user, |
159df176 | 385 | environment => ["HOME=${cf_home}"], |
6d512d3f | 386 | path => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"], |
159df176 | 387 | command => "/usr/bin/make static ENV=${cf_env}", |
383fa18a | 388 | refreshonly => true, |
76a321e1 IB |
389 | } |
390 | } | |
57ae81ea | 391 | } |