]>
Commit | Line | Data |
---|---|---|
1 | class role::cryptoportfolio ( | |
2 | String $user, | |
3 | String $group, | |
4 | String $home, | |
5 | Optional[String] $env = "prod", | |
6 | Optional[String] $webhook_url = undef, | |
7 | String $pg_user, | |
8 | String $pg_user_replication, | |
9 | String $pg_db, | |
10 | Optional[String] $pg_hostname = "localhost", | |
11 | Optional[String] $pg_port = "5432", | |
12 | Optional[String] $web_host = undef, | |
13 | Optional[String] $web_port = "", | |
14 | Optional[Boolean] $web_ssl = true, | |
15 | Optional[String] $front_version = undef, | |
16 | Optional[String] $front_sha256 = undef, | |
17 | Optional[String] $bot_version = undef, | |
18 | Optional[String] $bot_sha256 = undef, | |
19 | ) { | |
20 | ensure_resource('exec', 'systemctl daemon-reload', { | |
21 | command => '/usr/bin/systemctl daemon-reload', | |
22 | refreshonly => true | |
23 | }) | |
24 | ||
25 | include "base_installation" | |
26 | ||
27 | include "profile::tools" | |
28 | include "profile::postgresql" | |
29 | include "profile::apache" | |
30 | include "profile::xmr_stak" | |
31 | ||
32 | $password_seed = lookup("base_installation::puppet_pass_seed") | |
33 | ||
34 | $pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio") | |
35 | $pg_replication_password = generate_password(24, $password_seed, "postgres_cryptoportfolio_replication") | |
36 | $pg_host = "${pg_hostname}:${pg_port}" | |
37 | ||
38 | $cf_front_app = "${home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front" | |
39 | $cf_front_app_api_workdir = "${cf_front_app}/cmd/app" | |
40 | $cf_front_app_api_bin = "${cf_front_app_api_workdir}/cryptoportfolio-app" | |
41 | $cf_front_app_api_conf = "${home}/conf.toml" | |
42 | $cf_front_app_api_secret = generate_password(24, $password_seed, "cryptoportfolio_api_secret") | |
43 | ||
44 | $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env" | |
45 | ||
46 | $cf_bot_app = "${home}/bot" | |
47 | $cf_bot_app_conf = "${home}/bot_config.ini" | |
48 | $cf_bot_app_reports = "${home}/bot_reports" | |
49 | ||
50 | file { "/var/lib/postgres/data/certs": | |
51 | ensure => directory, | |
52 | mode => "0700", | |
53 | owner => $::profile::postgresql::pg_user, | |
54 | group => $::profile::postgresql::pg_user, | |
55 | require => File["/var/lib/postgres"], | |
56 | } | |
57 | ||
58 | file { "/var/lib/postgres/data/certs/cert.pem": | |
59 | source => "file:///etc/letsencrypt/live/$web_host/cert.pem", | |
60 | mode => "0600", | |
61 | links => "follow", | |
62 | owner => $::profile::postgresql::pg_user, | |
63 | group => $::profile::postgresql::pg_user, | |
64 | require => [Letsencrypt::Certonly[$web_host], File["/var/lib/postgres/data/certs"]] | |
65 | } | |
66 | ||
67 | file { "/var/lib/postgres/data/certs/privkey.pem": | |
68 | source => "file:///etc/letsencrypt/live/$web_host/privkey.pem", | |
69 | mode => "0600", | |
70 | links => "follow", | |
71 | owner => $::profile::postgresql::pg_user, | |
72 | group => $::profile::postgresql::pg_user, | |
73 | require => [Letsencrypt::Certonly[$web_host], File["/var/lib/postgres/data/certs"]] | |
74 | } | |
75 | ||
76 | postgresql::server::config_entry { "wal_level": | |
77 | value => "logical", | |
78 | } | |
79 | ||
80 | postgresql::server::config_entry { "ssl": | |
81 | value => "on", | |
82 | require => Letsencrypt::Certonly[$web_host], | |
83 | } | |
84 | ||
85 | postgresql::server::config_entry { "ssl_cert_file": | |
86 | value => "/var/lib/postgres/data/certs/cert.pem", | |
87 | require => Letsencrypt::Certonly[$web_host], | |
88 | } | |
89 | ||
90 | postgresql::server::config_entry { "ssl_key_file": | |
91 | value => "/var/lib/postgres/data/certs/privkey.pem", | |
92 | require => Letsencrypt::Certonly[$web_host], | |
93 | } | |
94 | ||
95 | postgresql::server::db { $pg_db: | |
96 | user => $pg_user, | |
97 | password => postgresql_password($pg_user, $pg_password), | |
98 | } | |
99 | -> | |
100 | postgresql_psql { "CREATE PUBLICATION ${pg_db}_publication FOR ALL TABLES": | |
101 | db => $pg_db, | |
102 | unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${pg_db}_publication'", | |
103 | } | |
104 | -> | |
105 | postgresql::server::role { $pg_user_replication: | |
106 | db => $pg_db, | |
107 | replication => true, | |
108 | password_hash => postgresql_password($pg_user_replication, $pg_replication_password), | |
109 | } | |
110 | -> | |
111 | postgresql::server::database_grant { $pg_user_replication: | |
112 | db => $pg_db, | |
113 | privilege => "CONNECT", | |
114 | role => $pg_user_replication, | |
115 | } | |
116 | -> | |
117 | postgresql::server::grant { "all tables in schema:public:$pg_user_replication": | |
118 | db => $pg_db, | |
119 | role => $pg_user_replication, | |
120 | privilege => "SELECT", | |
121 | object_type => "ALL TABLES IN SCHEMA", | |
122 | object_name => "public", | |
123 | } | |
124 | -> | |
125 | postgresql::server::grant { "all sequences in schema:public:$pg_user_replication": | |
126 | db => $pg_db, | |
127 | role => $pg_user_replication, | |
128 | privilege => "SELECT", | |
129 | object_type => "ALL SEQUENCES IN SCHEMA", | |
130 | object_name => "public", | |
131 | } | |
132 | ||
133 | postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user': | |
134 | type => 'host', | |
135 | database => $pg_db, | |
136 | user => $pg_user, | |
137 | address => '127.0.0.1/32', | |
138 | auth_method => 'md5', | |
139 | order => "b0", | |
140 | } | |
141 | postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user': | |
142 | type => 'host', | |
143 | database => $pg_db, | |
144 | user => $pg_user, | |
145 | address => '::1/128', | |
146 | auth_method => 'md5', | |
147 | order => "b0", | |
148 | } | |
149 | ||
150 | postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu': | |
151 | type => 'hostssl', | |
152 | database => $pg_db, | |
153 | user => $pg_user_replication, | |
154 | address => 'immae.eu', | |
155 | auth_method => 'md5', | |
156 | order => "b0", | |
157 | } | |
158 | ||
159 | class { 'apache::mod::headers': } | |
160 | apache::vhost { $web_host: | |
161 | port => '443', | |
162 | docroot => false, | |
163 | manage_docroot => false, | |
164 | proxy_dest => "http://localhost:8000", | |
165 | request_headers => 'set X-Forwarded-Proto "https"', | |
166 | ssl => true, | |
167 | ssl_cert => "/etc/letsencrypt/live/$web_host/cert.pem", | |
168 | ssl_key => "/etc/letsencrypt/live/$web_host/privkey.pem", | |
169 | ssl_chain => "/etc/letsencrypt/live/$web_host/chain.pem", | |
170 | require => Letsencrypt::Certonly[$web_host], | |
171 | proxy_preserve_host => true; | |
172 | default: * => $::profile::apache::apache_vhost_default; | |
173 | } | |
174 | ||
175 | file { "/usr/local/bin/slack-notify": | |
176 | mode => "0755", | |
177 | source => "puppet:///modules/role/cryptoportfolio/slack-notify.py", | |
178 | } | |
179 | ||
180 | unless empty($bot_version) { | |
181 | ensure_packages(["python", "python-pip"]) | |
182 | ||
183 | file { $cf_bot_app: | |
184 | ensure => "directory", | |
185 | mode => "0700", | |
186 | owner => $user, | |
187 | group => $group, | |
188 | require => User["$user:"], | |
189 | } | |
190 | ||
191 | archive { "${home}/trader_${bot_version}.tar.gz": | |
192 | path => "${home}/trader_${bot_version}.tar.gz", | |
193 | source => "https://git.immae.eu/releases/cryptoportfolio/trader/trader_${bot_version}.tar.gz", | |
194 | checksum_type => "sha256", | |
195 | checksum => $bot_sha256, | |
196 | cleanup => false, | |
197 | extract => true, | |
198 | user => $user, | |
199 | username => $facts["ec2_metadata"]["hostname"], | |
200 | password => generate_password(24, $password_seed, "ldap"), | |
201 | extract_path => $cf_bot_app, | |
202 | require => [User["$user:"], File[$cf_bot_app]], | |
203 | } ~> | |
204 | exec { "py-cryptoportfolio-dependencies": | |
205 | cwd => $cf_bot_app, | |
206 | user => $user, | |
207 | environment => ["HOME=${home}"], | |
208 | command => "/usr/bin/make install", | |
209 | require => User["$user:"], | |
210 | refreshonly => true, | |
211 | before => [ | |
212 | File[$cf_bot_app_conf], | |
213 | Cron["py-cryptoportfolio-before"], | |
214 | Cron["py-cryptoportfolio-after"], | |
215 | ] | |
216 | } | |
217 | ||
218 | file { $cf_bot_app_conf: | |
219 | owner => $user, | |
220 | group => $group, | |
221 | mode => "0600", | |
222 | content => template("role/cryptoportfolio/bot_config.ini.erb"), | |
223 | require => [ | |
224 | User["$user:"], | |
225 | Archive["${home}/trader_${bot_version}.tar.gz"], | |
226 | ], | |
227 | } | |
228 | ||
229 | cron { "py-cryptoportfolio-before": | |
230 | ensure => present, | |
231 | command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --before", | |
232 | user => $user, | |
233 | weekday => 7, # Sunday | |
234 | hour => 22, | |
235 | minute => 30, | |
236 | environment => ["HOME=${home}","PATH=/usr/bin/"], | |
237 | require => [ | |
238 | File[$cf_bot_app_conf], | |
239 | Archive["${home}/trader_${bot_version}.tar.gz"] | |
240 | ], | |
241 | } | |
242 | ||
243 | cron { "py-cryptoportfolio-after": | |
244 | ensure => present, | |
245 | command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --after", | |
246 | user => $user, | |
247 | weekday => 1, # Monday | |
248 | hour => 1, | |
249 | minute => 0, | |
250 | environment => ["HOME=${home}","PATH=/usr/bin/"], | |
251 | require => [ | |
252 | File[$cf_bot_app_conf], | |
253 | Archive["${home}/trader_${bot_version}.tar.gz"] | |
254 | ], | |
255 | } | |
256 | ||
257 | unless empty($webhook_url) { | |
258 | exec { "bot-slack-notify": | |
259 | refreshonly => true, | |
260 | environment => [ | |
261 | "P_PROJECT=Trader", | |
262 | "P_WEBHOOK=${webhook_url}", | |
263 | "P_VERSION=${bot_version}", | |
264 | "P_HOST=${web_host}", | |
265 | "P_HTTPS=${web_ssl}", | |
266 | ], | |
267 | command => "/usr/local/bin/slack-notify", | |
268 | require => File["/usr/local/bin/slack-notify"], | |
269 | subscribe => Exec["py-cryptoportfolio-dependencies"], | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | # FIXME: restore backup | |
275 | unless empty($front_version) { | |
276 | ensure_packages(["go", "npm", "nodejs", "yarn"]) | |
277 | ||
278 | file { [ | |
279 | "${home}/go/", | |
280 | "${home}/go/src", | |
281 | "${home}/go/src/immae.eu", | |
282 | "${home}/go/src/immae.eu/Immae", | |
283 | "${home}/go/src/immae.eu/Immae/Projets", | |
284 | "${home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies", | |
285 | "${home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio", | |
286 | $cf_front_app]: | |
287 | ensure => "directory", | |
288 | mode => "0700", | |
289 | owner => $user, | |
290 | group => $group, | |
291 | require => User["$user:"], | |
292 | } | |
293 | ||
294 | file { "${home}/front": | |
295 | ensure => "link", | |
296 | target => $cf_front_app, | |
297 | before => File[$cf_front_app], | |
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[$pg_db] | |
315 | ], | |
316 | } ~> | |
317 | exec { "dump $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 $pg_db > /var/lib/postgres/${pg_db}.schema", | |
322 | } | |
323 | ||
324 | archive { "${home}/front_${front_version}.tar.gz": | |
325 | path => "${home}/front_${front_version}.tar.gz", | |
326 | source => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz", | |
327 | checksum_type => "sha256", | |
328 | checksum => $front_sha256, | |
329 | cleanup => false, | |
330 | extract => true, | |
331 | user => $user, | |
332 | username => $facts["ec2_metadata"]["hostname"], | |
333 | password => generate_password(24, $password_seed, "ldap"), | |
334 | extract_path => $cf_front_app, | |
335 | require => [User["$user:"], File[$cf_front_app]], | |
336 | notify => [ | |
337 | Exec["web-cryptoportfolio-dependencies"], | |
338 | Exec["go-get-dep"], | |
339 | ] | |
340 | } | |
341 | ||
342 | # Api | |
343 | file { $cf_front_app_api_conf: | |
344 | owner => $user, | |
345 | group => $group, | |
346 | mode => "0600", | |
347 | content => template("role/cryptoportfolio/api_conf.toml.erb"), | |
348 | before => Exec["go-cryptoportfolio-app"], | |
349 | } | |
350 | ||
351 | exec { "go-get-dep": | |
352 | user => $user, | |
353 | environment => ["HOME=${home}"], | |
354 | creates => "${home}/go/bin/dep", | |
355 | command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep", | |
356 | refreshonly => true, | |
357 | } ~> | |
358 | exec { "go-cryptoportfolio-dependencies": | |
359 | cwd => $cf_front_app, | |
360 | user => $user, | |
361 | environment => ["HOME=${home}"], | |
362 | command => "${home}/go/bin/dep ensure", | |
363 | refreshonly => true, | |
364 | } ~> | |
365 | exec { "go-cryptoportfolio-app": | |
366 | cwd => $cf_front_app_api_workdir, | |
367 | user => $user, | |
368 | environment => ["HOME=${home}"], | |
369 | command => "/usr/bin/make build", | |
370 | refreshonly => true, | |
371 | } | |
372 | ||
373 | # Static pages | |
374 | file { $cf_front_app_static_conf: | |
375 | owner => $user, | |
376 | group => $group, | |
377 | mode => "0600", | |
378 | content => template("role/cryptoportfolio/static_conf.env.erb"), | |
379 | before => Exec["web-cryptoportfolio-build"], | |
380 | } | |
381 | ||
382 | exec { "web-cryptoportfolio-dependencies": | |
383 | cwd => "${cf_front_app}/cmd/web", | |
384 | user => $user, | |
385 | environment => ["HOME=${home}"], | |
386 | command => "/usr/bin/make install", | |
387 | refreshonly => true, | |
388 | require => [Package["npm"], Package["nodejs"], Package["yarn"]] | |
389 | } ~> | |
390 | exec { "web-cryptoportfolio-build": | |
391 | cwd => "${cf_front_app}/cmd/web", | |
392 | user => $user, | |
393 | environment => ["HOME=${home}"], | |
394 | path => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"], | |
395 | command => "/usr/bin/make static ENV=${env}", | |
396 | refreshonly => true, | |
397 | } | |
398 | ||
399 | unless empty($webhook_url) { | |
400 | exec { "front-slack-notify": | |
401 | refreshonly => true, | |
402 | environment => [ | |
403 | "P_PROJECT=Front", | |
404 | "P_WEBHOOK=${webhook_url}", | |
405 | "P_VERSION=${front_version}", | |
406 | "P_HOST=${web_host}", | |
407 | "P_HTTPS=${web_ssl}", | |
408 | ], | |
409 | command => "/usr/local/bin/slack-notify", | |
410 | require => File["/usr/local/bin/slack-notify"], | |
411 | subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]], | |
412 | } | |
413 | } | |
414 | } | |
415 | } |