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 = "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")
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 file { "/var/lib/postgres/data/certs":
47 owner => $::profile::postgresql::pg_user,
48 group => $::profile::postgresql::pg_user,
49 require => File["/var/lib/postgres"],
52 file { "/var/lib/postgres/data/certs/cert.pem":
53 source => "file:///etc/letsencrypt/live/$cf_front_app_host/cert.pem",
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"]]
61 file { "/var/lib/postgres/data/certs/privkey.pem":
62 source => "file:///etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
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"]]
70 postgresql::server::config_entry { "wal_level":
74 postgresql::server::config_entry { "ssl":
76 require => Letsencrypt::Certonly[$cf_front_app_host],
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],
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],
89 postgresql::server::db { $cf_pg_db:
91 password => postgresql_password($cf_pg_user, $cf_pg_password),
94 postgresql_psql { "CREATE PUBLICATION ${cf_pg_db}_publication FOR ALL TABLES":
96 unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${cf_pg_db}_publication'",
99 postgresql::server::role { $cf_pg_user_replication:
102 password_hash => postgresql_password($cf_pg_user_replication, $cf_pg_replication_password),
105 postgresql::server::database_grant { $cf_pg_user_replication:
107 privilege => "CONNECT",
108 role => $cf_pg_user_replication,
111 postgresql::server::grant { "all tables in schema:public:$cf_pg_user_replication":
113 role => $cf_pg_user_replication,
114 privilege => "SELECT",
115 object_type => "ALL TABLES IN SCHEMA",
116 object_name => "public",
119 postgresql::server::grant { "all sequences in schema:public:$cf_pg_user_replication":
121 role => $cf_pg_user_replication,
122 privilege => "SELECT",
123 object_type => "ALL SEQUENCES IN SCHEMA",
124 object_name => "public",
127 postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
129 database => $cf_pg_db,
131 address => '127.0.0.1/32',
132 auth_method => 'md5',
135 postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
137 database => $cf_pg_db,
139 address => '::1/128',
140 auth_method => 'md5',
144 postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
146 database => $cf_pg_db,
147 user => $cf_pg_user_replication,
148 address => 'immae.eu',
149 auth_method => 'md5',
153 letsencrypt::certonly { $cf_front_app_host: ;
154 default: * => $::profile::apache::letsencrypt_certonly_default;
157 class { 'apache::mod::headers': }
158 apache::vhost { $cf_front_app_host:
161 manage_docroot => false,
162 proxy_dest => "http://localhost:8000",
163 request_headers => 'set X-Forwarded-Proto "https"',
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;
182 $front_version = lookup("cryptoportfolio::front_version") |$key| { {} }
183 $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} }
185 $bot_version = lookup("cryptoportfolio::bot_version") |$key| { {} }
186 $bot_sha256 = lookup("cryptoportfolio::bot_sha256") |$key| { {} }
188 unless empty($bot_version) {
189 ensure_packages(["python", "python-pip"])
192 ensure => "directory",
196 require => User[$cf_user],
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,
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]],
212 exec { "py-cryptoportfolio-dependencies":
215 environment => ["HOME=${cf_home}"],
216 command => "/usr/bin/make install",
217 require => User[$cf_user],
221 file { $cf_bot_app_conf:
225 content => template("role/cryptoportfolio/bot_config.ini.erb")
229 unless empty($front_version) {
230 ensure_packages(["go", "npm", "nodejs", "yarn"])
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",
241 ensure => "directory",
245 require => User[$cf_user],
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,
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]],
262 file { "${cf_home}/front":
264 target => $cf_front_app,
265 before => File[$cf_front_app],
267 exec { "remove old ${cf_front_app} directory":
270 command => "/usr/bin/rm -rf ${cf_front_app}",
271 before => File[$cf_front_app],
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],
282 exec { "go-cryptoportfolio-dependencies":
283 cwd => $cf_front_app,
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"]],
291 exec { "go-cryptoportfolio-app":
292 cwd => $cf_front_app_api_workdir,
294 environment => ["HOME=${cf_home}"],
295 creates => $cf_front_app_api_bin,
296 command => "/usr/bin/make build",
297 require => Exec["go-cryptoportfolio-dependencies"],
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[$cf_pg_db]
317 exec { "dump $cf_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 $cf_pg_db > /var/lib/postgres/${cf_pg_db}.schema",
324 file { $cf_front_app_api_conf:
328 content => template("role/cryptoportfolio/api_conf.toml.erb"),
331 file { $cf_front_app_static_conf:
335 content => template("role/cryptoportfolio/static_conf.env.erb"),
336 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
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"]]
348 exec { "remove build ${cf_front_app}/cmd/web/build/":
349 command => "/usr/bin/rm -rf '${cf_front_app}/cmd/web/build/'",
351 before => Exec["web-cryptoportfolio-build"]
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"]]