1 class role::cryptoportfolio (
5 Optional[String] $env = "prod",
6 Optional[String] $webhook_url = undef,
8 String $pg_user_replication,
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,
20 ensure_resource('exec', 'systemctl daemon-reload', {
21 command => '/usr/bin/systemctl daemon-reload',
25 include "base_installation"
27 include "profile::tools"
28 include "profile::postgresql"
29 include "profile::apache"
30 include "profile::xmr_stak"
32 $password_seed = lookup("base_installation::puppet_pass_seed")
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}"
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")
44 $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
46 $cf_bot_app = "${home}/bot"
47 $cf_bot_app_conf = "${home}/bot_config.ini"
48 $cf_bot_app_reports = "${home}/bot_reports"
50 file { "/var/lib/postgres/data/certs":
53 owner => $::profile::postgresql::pg_user,
54 group => $::profile::postgresql::pg_user,
55 require => File["/var/lib/postgres"],
58 file { "/var/lib/postgres/data/certs/cert.pem":
59 source => "file:///etc/letsencrypt/live/$web_host/cert.pem",
62 owner => $::profile::postgresql::pg_user,
63 group => $::profile::postgresql::pg_user,
64 require => [Letsencrypt::Certonly[$web_host], File["/var/lib/postgres/data/certs"]]
67 file { "/var/lib/postgres/data/certs/privkey.pem":
68 source => "file:///etc/letsencrypt/live/$web_host/privkey.pem",
71 owner => $::profile::postgresql::pg_user,
72 group => $::profile::postgresql::pg_user,
73 require => [Letsencrypt::Certonly[$web_host], File["/var/lib/postgres/data/certs"]]
76 postgresql::server::config_entry { "wal_level":
80 postgresql::server::config_entry { "ssl":
82 require => Letsencrypt::Certonly[$web_host],
85 postgresql::server::config_entry { "ssl_cert_file":
86 value => "/var/lib/postgres/data/certs/cert.pem",
87 require => Letsencrypt::Certonly[$web_host],
90 postgresql::server::config_entry { "ssl_key_file":
91 value => "/var/lib/postgres/data/certs/privkey.pem",
92 require => Letsencrypt::Certonly[$web_host],
95 postgresql::server::db { $pg_db:
97 password => postgresql_password($pg_user, $pg_password),
100 postgresql_psql { "CREATE PUBLICATION ${pg_db}_publication FOR ALL TABLES":
102 unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${pg_db}_publication'",
105 postgresql::server::role { $pg_user_replication:
108 password_hash => postgresql_password($pg_user_replication, $pg_replication_password),
111 postgresql::server::database_grant { $pg_user_replication:
113 privilege => "CONNECT",
114 role => $pg_user_replication,
117 postgresql::server::grant { "all tables in schema:public:$pg_user_replication":
119 role => $pg_user_replication,
120 privilege => "SELECT",
121 object_type => "ALL TABLES IN SCHEMA",
122 object_name => "public",
125 postgresql::server::grant { "all sequences in schema:public:$pg_user_replication":
127 role => $pg_user_replication,
128 privilege => "SELECT",
129 object_type => "ALL SEQUENCES IN SCHEMA",
130 object_name => "public",
133 postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
137 address => '127.0.0.1/32',
138 auth_method => 'md5',
141 postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
145 address => '::1/128',
146 auth_method => 'md5',
150 postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
153 user => $pg_user_replication,
154 address => 'immae.eu',
155 auth_method => 'md5',
159 class { 'apache::mod::headers': }
160 apache::vhost { $web_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/$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;
175 file { "/usr/local/bin/slack-notify":
177 source => "puppet:///modules/role/cryptoportfolio/slack-notify.py",
180 unless empty($bot_version) {
181 ensure_packages(["python", "python-pip"])
184 ensure => "directory",
188 require => User["$user:"],
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,
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]],
204 exec { "py-cryptoportfolio-dependencies":
207 environment => ["HOME=${home}"],
208 command => "/usr/bin/make install",
209 require => User["$user:"],
212 File[$cf_bot_app_conf],
213 Cron["py-cryptoportfolio-before"],
214 Cron["py-cryptoportfolio-after"],
218 file { $cf_bot_app_conf:
222 content => template("role/cryptoportfolio/bot_config.ini.erb"),
225 Archive["${home}/trader_${bot_version}.tar.gz"],
229 cron { "py-cryptoportfolio-before":
231 command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --before",
233 weekday => 7, # Sunday
236 environment => ["HOME=${home}","PATH=/usr/bin/"],
238 File[$cf_bot_app_conf],
239 Archive["${home}/trader_${bot_version}.tar.gz"]
243 cron { "py-cryptoportfolio-after":
245 command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --after",
247 weekday => 1, # Monday
250 environment => ["HOME=${home}","PATH=/usr/bin/"],
252 File[$cf_bot_app_conf],
253 Archive["${home}/trader_${bot_version}.tar.gz"]
257 unless empty($webhook_url) {
258 exec { "bot-slack-notify":
262 "P_WEBHOOK=${webhook_url}",
263 "P_VERSION=${bot_version}",
264 "P_HOST=${web_host}",
265 "P_HTTPS=${web_ssl}",
267 command => "/usr/local/bin/slack-notify",
268 require => File["/usr/local/bin/slack-notify"],
269 subscribe => Exec["py-cryptoportfolio-dependencies"],
274 # FIXME: restore backup
275 unless empty($front_version) {
276 ensure_packages(["go", "npm", "nodejs", "yarn"])
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",
287 ensure => "directory",
291 require => User["$user:"],
294 file { "${home}/front":
296 target => $cf_front_app,
297 before => File[$cf_front_app],
300 file { "/etc/systemd/system/cryptoportfolio-app.service":
304 content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
305 notify => Exec["systemctl daemon-reload"],
308 service { 'cryptoportfolio-app':
311 subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
313 File["/etc/systemd/system/cryptoportfolio-app.service"],
314 Postgresql::Server::Db[$pg_db]
317 exec { "dump $pg_db structure":
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",
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,
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]],
337 Exec["web-cryptoportfolio-dependencies"],
343 file { $cf_front_app_api_conf:
347 content => template("role/cryptoportfolio/api_conf.toml.erb"),
348 before => Exec["go-cryptoportfolio-app"],
353 environment => ["HOME=${home}"],
354 creates => "${home}/go/bin/dep",
355 command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep",
358 exec { "go-cryptoportfolio-dependencies":
359 cwd => $cf_front_app,
361 environment => ["HOME=${home}"],
362 command => "${home}/go/bin/dep ensure",
365 exec { "go-cryptoportfolio-app":
366 cwd => $cf_front_app_api_workdir,
368 environment => ["HOME=${home}"],
369 command => "/usr/bin/make build",
374 file { $cf_front_app_static_conf:
378 content => template("role/cryptoportfolio/static_conf.env.erb"),
379 before => Exec["web-cryptoportfolio-build"],
382 exec { "web-cryptoportfolio-dependencies":
383 cwd => "${cf_front_app}/cmd/web",
385 environment => ["HOME=${home}"],
386 command => "/usr/bin/make install",
388 require => [Package["npm"], Package["nodejs"], Package["yarn"]]
390 exec { "web-cryptoportfolio-build":
391 cwd => "${cf_front_app}/cmd/web",
393 environment => ["HOME=${home}"],
394 path => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"],
395 command => "/usr/bin/make static ENV=${env}",
399 unless empty($webhook_url) {
400 exec { "front-slack-notify":
404 "P_WEBHOOK=${webhook_url}",
405 "P_VERSION=${front_version}",
406 "P_HOST=${web_host}",
407 "P_HTTPS=${web_ssl}",
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"]],