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_host = "localhost:5432"
23 $cf_user = "cryptoportfolio"
24 $cf_group = "cryptoportfolio"
25 $cf_home = "/opt/cryptoportfolio"
27 $cf_front_app_host = "cryptoportfolio.immae.eu"
28 $cf_front_app_port = ""
29 $cf_front_app_ssl = "true"
30 $cf_front_app = "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front"
31 $cf_front_app_api_workdir = "${cf_front_app}/cmd/app"
32 $cf_front_app_api_bin = "${cf_front_app_api_workdir}/cryptoportfolio-app"
33 $cf_front_app_api_conf = "${cf_home}/conf.toml"
34 $cf_front_app_api_secret = generate_password(24, $password_seed, "cryptoportfolio_api_secret")
36 $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
38 $cf_bot_app = "${cf_home}/bot"
40 file { "/var/lib/postgres/data/certs":
43 owner => $::profile::postgresql::pg_user,
44 group => $::profile::postgresql::pg_user,
45 require => File["/var/lib/postgres"],
48 file { "/var/lib/postgres/data/certs/cert.pem":
49 source => "file:///etc/letsencrypt/live/$cf_front_app_host/cert.pem",
52 owner => $::profile::postgresql::pg_user,
53 group => $::profile::postgresql::pg_user,
54 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
57 file { "/var/lib/postgres/data/certs/privkey.pem":
58 source => "file:///etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
61 owner => $::profile::postgresql::pg_user,
62 group => $::profile::postgresql::pg_user,
63 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
66 postgresql::server::config_entry { "wal_level":
70 postgresql::server::config_entry { "ssl":
72 require => Letsencrypt::Certonly[$cf_front_app_host],
75 postgresql::server::config_entry { "ssl_cert_file":
76 value => "/var/lib/postgres/data/certs/cert.pem",
77 require => Letsencrypt::Certonly[$cf_front_app_host],
80 postgresql::server::config_entry { "ssl_key_file":
81 value => "/var/lib/postgres/data/certs/privkey.pem",
82 require => Letsencrypt::Certonly[$cf_front_app_host],
85 postgresql::server::db { $cf_pg_db:
87 password => postgresql_password($cf_pg_user, $cf_pg_password),
90 postgresql_psql { "CREATE PUBLICATION ${cf_pg_db}_publication FOR ALL TABLES":
92 unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${cf_pg_db}_publication'",
95 postgresql::server::role { $cf_pg_user_replication:
98 password_hash => postgresql_password($cf_pg_user_replication, $cf_pg_replication_password),
101 postgresql::server::database_grant { $cf_pg_user_replication:
103 privilege => "CONNECT",
104 role => $cf_pg_user_replication,
107 postgresql::server::grant { "all tables in schema:public:$cf_pg_user_replication":
109 role => $cf_pg_user_replication,
110 privilege => "SELECT",
111 object_type => "ALL TABLES IN SCHEMA",
112 object_name => "public",
115 postgresql::server::grant { "all sequences in schema:public:$cf_pg_user_replication":
117 role => $cf_pg_user_replication,
118 privilege => "SELECT",
119 object_type => "ALL SEQUENCES IN SCHEMA",
120 object_name => "public",
123 postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
125 database => $cf_pg_db,
127 address => '127.0.0.1/32',
128 auth_method => 'md5',
131 postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
133 database => $cf_pg_db,
135 address => '::1/128',
136 auth_method => 'md5',
140 postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
142 database => $cf_pg_db,
143 user => $cf_pg_user_replication,
144 address => 'immae.eu',
145 auth_method => 'md5',
149 letsencrypt::certonly { $cf_front_app_host: ;
150 default: * => $::profile::apache::letsencrypt_certonly_default;
153 class { 'apache::mod::headers': }
154 apache::vhost { $cf_front_app_host:
157 manage_docroot => false,
158 proxy_dest => "http://localhost:8000",
159 request_headers => 'set X-Forwarded-Proto "https"',
161 ssl_cert => "/etc/letsencrypt/live/$cf_front_app_host/cert.pem",
162 ssl_key => "/etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
163 ssl_chain => "/etc/letsencrypt/live/$cf_front_app_host/chain.pem",
164 require => Letsencrypt::Certonly[$cf_front_app_host],
165 proxy_preserve_host => true;
166 default: * => $::profile::apache::apache_vhost_default;
178 $front_version = lookup("cryptoportfolio::front_version") |$key| { {} }
179 $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} }
181 $bot_version = lookup("cryptoportfolio::bot_version") |$key| { {} }
182 $bot_sha256 = lookup("cryptoportfolio::bot_sha256") |$key| { {} }
184 unless empty($bot_version) {
185 ensure_packages(["python", "python-pip"])
188 ensure => "directory",
192 require => User[$cf_user],
195 archive { "${cf_home}/${bot_version}.tar.gz":
196 path => "${cf_home}/${bot_version}.tar.gz",
197 source => "https://git.immae.eu/releases/cryptoportfolio/trader/trader_${bot_version}.tar.gz",
198 checksum_type => "sha256",
199 checksum => $bot_sha256,
202 user => "cryptoportfolio",
203 username => $facts["ec2_metadata"]["hostname"],
204 password => generate_password(24, $password_seed, "ldap"),
205 extract_path => $cf_bot_app,
206 require => [User[$cf_user], File[$cf_bot_app]],
208 exec { "py-cryptoportfolio-dependencies":
211 environment => ["HOME=${cf_home}"],
212 command => "/usr/bin/make install",
213 require => User[$cf_user],
218 unless empty($front_version) {
219 ensure_packages(["go", "npm", "nodejs", "yarn"])
224 "${cf_home}/go/src/immae.eu",
225 "${cf_home}/go/src/immae.eu/Immae",
226 "${cf_home}/go/src/immae.eu/Immae/Projets",
227 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies",
228 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio",
230 ensure => "directory",
234 require => User[$cf_user],
237 archive { "${cf_home}/${front_version}.tar.gz":
238 path => "${cf_home}/${front_version}.tar.gz",
239 source => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz",
240 checksum_type => "sha256",
241 checksum => $front_sha256,
244 user => "cryptoportfolio",
245 username => $facts["ec2_metadata"]["hostname"],
246 password => generate_password(24, $password_seed, "ldap"),
247 extract_path => $cf_front_app,
248 require => [User[$cf_user], File[$cf_front_app]],
251 file { "${cf_home}/front":
253 target => $cf_front_app,
254 before => File[$cf_front_app],
256 exec { "remove old ${cf_front_app} directory":
259 command => "/usr/bin/rm -rf ${cf_front_app}",
260 before => File[$cf_front_app],
265 environment => ["HOME=${cf_home}"],
266 creates => "${cf_home}/go/bin/dep",
267 command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep",
268 require => User[$cf_user],
271 exec { "go-cryptoportfolio-dependencies":
272 cwd => $cf_front_app,
274 environment => ["HOME=${cf_home}"],
275 creates => "${cf_front_app}/vendor",
276 command => "${cf_home}/go/bin/dep ensure",
277 require => [Exec["go-get-dep"], Archive["${cf_home}/${front_version}.tar.gz"]],
280 exec { "go-cryptoportfolio-app":
281 cwd => $cf_front_app_api_workdir,
283 environment => ["HOME=${cf_home}"],
284 creates => $cf_front_app_api_bin,
285 command => "/usr/bin/make build",
286 require => Exec["go-cryptoportfolio-dependencies"],
289 file { "/etc/systemd/system/cryptoportfolio-app.service":
293 content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
294 notify => Exec["systemctl daemon-reload"],
297 service { 'cryptoportfolio-app':
300 subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
302 File["/etc/systemd/system/cryptoportfolio-app.service"],
303 Postgresql::Server::Db[$cf_pg_db]
306 exec { "dump $cf_pg_db structure":
308 user => $::profile::postgresql::pg_user,
309 group => $::profile::postgresql::pg_user,
310 command => "/usr/bin/pg_dump --schema-only --clean --no-publications $cf_pg_db > /var/lib/postgres/${cf_pg_db}.schema",
313 file { $cf_front_app_api_conf:
317 content => template("role/cryptoportfolio/api_conf.toml.erb"),
320 file { $cf_front_app_static_conf:
324 content => template("role/cryptoportfolio/static_conf.env.erb"),
325 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
328 exec { "web-cryptoportfolio-dependencies":
329 cwd => "${cf_front_app}/cmd/web",
330 environment => ["HOME=${cf_home}"],
331 command => "/usr/bin/make install",
332 creates => "${cf_front_app}/cmd/web/node_modules",
333 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
334 require => [Package["npm"], Package["nodejs"], Package["yarn"]]
337 exec { "remove build ${cf_front_app}/cmd/web/build/":
338 command => "/usr/bin/rm -rf '${cf_front_app}/cmd/web/build/'",
340 before => Exec["web-cryptoportfolio-build"]
343 exec { "web-cryptoportfolio-build":
344 cwd => "${cf_front_app}/cmd/web",
345 environment => ["HOME=${cf_home}"],
346 path => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"],
347 command => "/usr/bin/make static ENV=${cf_env}",
348 creates => "${cf_front_app}/cmd/web/build/static",
349 require => [File[$cf_front_app_static_conf], Exec["web-cryptoportfolio-dependencies"]]