1 class role::cryptoportfolio {
2 ensure_resource('exec', 'systemctl daemon-reload', {
3 command => '/usr/bin/systemctl daemon-reload',
7 include "base_installation"
9 include "profile::tools"
10 include "profile::postgresql"
11 include "profile::apache"
12 include "profile::xmr_stak"
14 $password_seed = lookup("base_installation::puppet_pass_seed") |$key| { {} }
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"
23 $cf_pg_host = "${cf_pg_hostname}:${cf_pg_port}"
25 $cf_user = "cryptoportfolio"
26 $cf_group = "cryptoportfolio"
27 $cf_home = "/opt/cryptoportfolio"
29 $cf_front_app_host = lookup("base_installation::system_hostname") |$key| { "example.com" }
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")
38 $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
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"
44 $cf_webhook_url = lookup("cryptoportfolio::slack_webhook") |$key| { "" }
46 file { "/var/lib/postgres/data/certs":
49 owner => $::profile::postgresql::pg_user,
50 group => $::profile::postgresql::pg_user,
51 require => File["/var/lib/postgres"],
54 file { "/var/lib/postgres/data/certs/cert.pem":
55 source => "file:///etc/letsencrypt/live/$cf_front_app_host/cert.pem",
58 owner => $::profile::postgresql::pg_user,
59 group => $::profile::postgresql::pg_user,
60 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
63 file { "/var/lib/postgres/data/certs/privkey.pem":
64 source => "file:///etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
67 owner => $::profile::postgresql::pg_user,
68 group => $::profile::postgresql::pg_user,
69 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
72 postgresql::server::config_entry { "wal_level":
76 postgresql::server::config_entry { "ssl":
78 require => Letsencrypt::Certonly[$cf_front_app_host],
81 postgresql::server::config_entry { "ssl_cert_file":
82 value => "/var/lib/postgres/data/certs/cert.pem",
83 require => Letsencrypt::Certonly[$cf_front_app_host],
86 postgresql::server::config_entry { "ssl_key_file":
87 value => "/var/lib/postgres/data/certs/privkey.pem",
88 require => Letsencrypt::Certonly[$cf_front_app_host],
91 postgresql::server::db { $cf_pg_db:
93 password => postgresql_password($cf_pg_user, $cf_pg_password),
96 postgresql_psql { "CREATE PUBLICATION ${cf_pg_db}_publication FOR ALL TABLES":
98 unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${cf_pg_db}_publication'",
101 postgresql::server::role { $cf_pg_user_replication:
104 password_hash => postgresql_password($cf_pg_user_replication, $cf_pg_replication_password),
107 postgresql::server::database_grant { $cf_pg_user_replication:
109 privilege => "CONNECT",
110 role => $cf_pg_user_replication,
113 postgresql::server::grant { "all tables in schema:public:$cf_pg_user_replication":
115 role => $cf_pg_user_replication,
116 privilege => "SELECT",
117 object_type => "ALL TABLES IN SCHEMA",
118 object_name => "public",
121 postgresql::server::grant { "all sequences in schema:public:$cf_pg_user_replication":
123 role => $cf_pg_user_replication,
124 privilege => "SELECT",
125 object_type => "ALL SEQUENCES IN SCHEMA",
126 object_name => "public",
129 postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
131 database => $cf_pg_db,
133 address => '127.0.0.1/32',
134 auth_method => 'md5',
137 postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
139 database => $cf_pg_db,
141 address => '::1/128',
142 auth_method => 'md5',
146 postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
148 database => $cf_pg_db,
149 user => $cf_pg_user_replication,
150 address => 'immae.eu',
151 auth_method => 'md5',
155 letsencrypt::certonly { $cf_front_app_host: ;
156 default: * => $::profile::apache::letsencrypt_certonly_default;
159 class { 'apache::mod::headers': }
160 apache::vhost { $cf_front_app_host:
163 manage_docroot => false,
164 proxy_dest => "http://localhost:8000",
165 request_headers => 'set X-Forwarded-Proto "https"',
167 ssl_cert => "/etc/letsencrypt/live/$cf_front_app_host/cert.pem",
168 ssl_key => "/etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
169 ssl_chain => "/etc/letsencrypt/live/$cf_front_app_host/chain.pem",
170 require => Letsencrypt::Certonly[$cf_front_app_host],
171 proxy_preserve_host => true;
172 default: * => $::profile::apache::apache_vhost_default;
184 file { "/usr/local/bin/slack-notify":
186 source => "puppet:///modules/role/cryptoportfolio/slack-notify.py",
189 $front_version = lookup("cryptoportfolio::front_version") |$key| { {} }
190 $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} }
192 $bot_version = lookup("cryptoportfolio::bot_version") |$key| { {} }
193 $bot_sha256 = lookup("cryptoportfolio::bot_sha256") |$key| { {} }
195 unless empty($bot_version) {
196 ensure_packages(["python", "python-pip"])
199 ensure => "directory",
203 require => User[$cf_user],
206 archive { "${cf_home}/trader_${bot_version}.tar.gz":
207 path => "${cf_home}/trader_${bot_version}.tar.gz",
208 source => "https://git.immae.eu/releases/cryptoportfolio/trader/trader_${bot_version}.tar.gz",
209 checksum_type => "sha256",
210 checksum => $bot_sha256,
214 username => $facts["ec2_metadata"]["hostname"],
215 password => generate_password(24, $password_seed, "ldap"),
216 extract_path => $cf_bot_app,
217 require => [User[$cf_user], File[$cf_bot_app]],
219 exec { "py-cryptoportfolio-dependencies":
222 environment => ["HOME=${cf_home}"],
223 command => "/usr/bin/make install",
224 require => User[$cf_user],
227 File[$cf_bot_app_conf],
228 Cron["py-cryptoportfolio-before"],
229 Cron["py-cryptoportfolio-after"],
233 file { $cf_bot_app_conf:
237 content => template("role/cryptoportfolio/bot_config.ini.erb"),
240 Archive["${cf_home}/trader_${bot_version}.tar.gz"],
244 cron { "py-cryptoportfolio-before":
246 command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --before",
247 user => "cryptoportfolio",
248 weekday => 7, # Sunday
251 environment => ["HOME=${cf_home}","PATH=/usr/bin/"],
253 File[$cf_bot_app_conf],
254 Archive["${cf_home}/trader_${bot_version}.tar.gz"]
258 cron { "py-cryptoportfolio-after":
260 command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --after",
261 user => "cryptoportfolio",
262 weekday => 1, # Monday
265 environment => ["HOME=${cf_home}","PATH=/usr/bin/"],
267 File[$cf_bot_app_conf],
268 Archive["${cf_home}/trader_${bot_version}.tar.gz"]
272 unless empty($cf_webhook_url) {
273 exec { "bot-slack-notify":
277 "P_WEBHOOK=${cf_webhook_url}",
278 "P_VERSION=${bot_version}",
279 "P_HOST=${cf_front_app_host}",
280 "P_HTTPS=${cf_front_app_ssl}",
282 command => "/usr/local/bin/slack-notify",
283 require => File["/usr/local/bin/slack-notify"],
284 subscribe => Exec["py-cryptoportfolio-dependencies"],
289 # FIXME: restore backup
290 unless empty($front_version) {
291 ensure_packages(["go", "npm", "nodejs", "yarn"])
296 "${cf_home}/go/src/immae.eu",
297 "${cf_home}/go/src/immae.eu/Immae",
298 "${cf_home}/go/src/immae.eu/Immae/Projets",
299 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies",
300 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio",
302 ensure => "directory",
306 require => User[$cf_user],
309 file { "${cf_home}/front":
311 target => $cf_front_app,
312 before => File[$cf_front_app],
315 file { "/etc/systemd/system/cryptoportfolio-app.service":
319 content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
320 notify => Exec["systemctl daemon-reload"],
323 service { 'cryptoportfolio-app':
326 subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
328 File["/etc/systemd/system/cryptoportfolio-app.service"],
329 Postgresql::Server::Db[$cf_pg_db]
332 exec { "dump $cf_pg_db structure":
334 user => $::profile::postgresql::pg_user,
335 group => $::profile::postgresql::pg_user,
336 command => "/usr/bin/pg_dump --schema-only --clean --no-publications $cf_pg_db > /var/lib/postgres/${cf_pg_db}.schema",
339 archive { "${cf_home}/front_${front_version}.tar.gz":
340 path => "${cf_home}/front_${front_version}.tar.gz",
341 source => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz",
342 checksum_type => "sha256",
343 checksum => $front_sha256,
347 username => $facts["ec2_metadata"]["hostname"],
348 password => generate_password(24, $password_seed, "ldap"),
349 extract_path => $cf_front_app,
350 require => [User[$cf_user], File[$cf_front_app]],
352 Exec["web-cryptoportfolio-dependencies"],
358 file { $cf_front_app_api_conf:
362 content => template("role/cryptoportfolio/api_conf.toml.erb"),
363 before => Exec["go-cryptoportfolio-app"],
368 environment => ["HOME=${cf_home}"],
369 creates => "${cf_home}/go/bin/dep",
370 command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep",
373 exec { "go-cryptoportfolio-dependencies":
374 cwd => $cf_front_app,
376 environment => ["HOME=${cf_home}"],
377 command => "${cf_home}/go/bin/dep ensure",
380 exec { "go-cryptoportfolio-app":
381 cwd => $cf_front_app_api_workdir,
383 environment => ["HOME=${cf_home}"],
384 command => "/usr/bin/make build",
389 file { $cf_front_app_static_conf:
393 content => template("role/cryptoportfolio/static_conf.env.erb"),
394 before => Exec["web-cryptoportfolio-build"],
397 exec { "web-cryptoportfolio-dependencies":
398 cwd => "${cf_front_app}/cmd/web",
400 environment => ["HOME=${cf_home}"],
401 command => "/usr/bin/make install",
403 require => [Package["npm"], Package["nodejs"], Package["yarn"]]
405 exec { "web-cryptoportfolio-build":
406 cwd => "${cf_front_app}/cmd/web",
408 environment => ["HOME=${cf_home}"],
409 path => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"],
410 command => "/usr/bin/make static ENV=${cf_env}",
414 unless empty($cf_webhook_url) {
415 exec { "front-slack-notify":
419 "P_WEBHOOK=${cf_webhook_url}",
420 "P_VERSION=${front_version}",
421 "P_HOST=${cf_front_app_host}",
422 "P_HTTPS=${cf_front_app_ssl}",
424 command => "/usr/local/bin/slack-notify",
425 require => File["/usr/local/bin/slack-notify"],
426 subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],