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 file { "/var/lib/postgres/data/certs":
41 owner => $::profile::postgresql::pg_user,
42 group => $::profile::postgresql::pg_user,
43 require => File["/var/lib/postgres"],
46 file { "/var/lib/postgres/data/certs/cert.pem":
47 source => "file:///etc/letsencrypt/live/$cf_front_app_host/cert.pem",
50 owner => $::profile::postgresql::pg_user,
51 group => $::profile::postgresql::pg_user,
52 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
55 file { "/var/lib/postgres/data/certs/privkey.pem":
56 source => "file:///etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
59 owner => $::profile::postgresql::pg_user,
60 group => $::profile::postgresql::pg_user,
61 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
64 postgresql::server::config_entry { "wal_level":
68 postgresql::server::config_entry { "ssl":
70 require => Letsencrypt::Certonly[$cf_front_app_host],
73 postgresql::server::config_entry { "ssl_cert_file":
74 value => "/var/lib/postgres/data/certs/cert.pem",
75 require => Letsencrypt::Certonly[$cf_front_app_host],
78 postgresql::server::config_entry { "ssl_key_file":
79 value => "/var/lib/postgres/data/certs/privkey.pem",
80 require => Letsencrypt::Certonly[$cf_front_app_host],
83 postgresql::server::db { $cf_pg_db:
85 password => postgresql_password($cf_pg_user, $cf_pg_password),
88 postgresql_psql { "CREATE PUBLICATION ${cf_pg_db}_publication FOR ALL TABLES":
90 unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${cf_pg_db}_publication'",
93 postgresql::server::role { $cf_pg_user_replication:
96 password_hash => postgresql_password($cf_pg_user_replication, $cf_pg_replication_password),
99 postgresql::server::database_grant { $cf_pg_user_replication:
101 privilege => "CONNECT",
102 role => $cf_pg_user_replication,
105 postgresql::server::grant { "all tables in schema:public:$cf_pg_user_replication":
107 role => $cf_pg_user_replication,
108 privilege => "SELECT",
109 object_type => "ALL TABLES IN SCHEMA",
110 object_name => "public",
113 postgresql::server::grant { "all sequences in schema:public:$cf_pg_user_replication":
115 role => $cf_pg_user_replication,
116 privilege => "SELECT",
117 object_type => "ALL SEQUENCES IN SCHEMA",
118 object_name => "public",
121 postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
123 database => $cf_pg_db,
125 address => '127.0.0.1/32',
126 auth_method => 'md5',
129 postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
131 database => $cf_pg_db,
133 address => '::1/128',
134 auth_method => 'md5',
138 postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
140 database => $cf_pg_db,
141 user => $cf_pg_user_replication,
142 address => 'immae.eu',
143 auth_method => 'md5',
147 letsencrypt::certonly { $cf_front_app_host: ;
148 default: * => $::profile::apache::letsencrypt_certonly_default;
151 class { 'apache::mod::headers': }
152 apache::vhost { $cf_front_app_host:
155 manage_docroot => false,
156 proxy_dest => "http://localhost:8000",
157 request_headers => 'set X-Forwarded-Proto "https"',
159 ssl_cert => "/etc/letsencrypt/live/$cf_front_app_host/cert.pem",
160 ssl_key => "/etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
161 ssl_chain => "/etc/letsencrypt/live/$cf_front_app_host/chain.pem",
162 require => Letsencrypt::Certonly[$cf_front_app_host],
163 proxy_preserve_host => true;
164 default: * => $::profile::apache::apache_vhost_default;
176 $front_version = lookup("cryptoportfolio::front_version") |$key| { {} }
177 $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} }
179 unless empty($front_version) {
180 ensure_packages(["go", "npm", "nodejs", "yarn"])
185 "${cf_home}/go/src/immae.eu",
186 "${cf_home}/go/src/immae.eu/Immae",
187 "${cf_home}/go/src/immae.eu/Immae/Projets",
188 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies",
189 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio",
191 ensure => "directory",
195 require => User[$cf_user],
198 archive { "${cf_home}/${front_version}.tar.gz":
199 path => "${cf_home}/${front_version}.tar.gz",
200 source => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz",
201 checksum_type => "sha256",
202 checksum => $front_sha256,
205 user => "cryptoportfolio",
206 username => $facts["ec2_metadata"]["hostname"],
207 password => generate_password(24, $password_seed, "ldap"),
208 extract_path => $cf_front_app,
209 require => [User[$cf_user], File[$cf_front_app]],
212 file { "${cf_home}/front":
214 target => $cf_front_app,
215 before => File[$cf_front_app],
217 exec { "remove old ${cf_front_app} directory":
220 command => "/usr/bin/rm -rf ${cf_front_app}",
221 before => File[$cf_front_app],
226 environment => ["HOME=${cf_home}"],
227 creates => "${cf_home}/go/bin/dep",
228 command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep",
229 require => User[$cf_user],
232 exec { "go-cryptoportfolio-dependencies":
233 cwd => $cf_front_app,
235 environment => ["HOME=${cf_home}"],
236 creates => "${cf_front_app}/vendor",
237 command => "${cf_home}/go/bin/dep ensure",
238 require => [Exec["go-get-dep"], Archive["${cf_home}/${front_version}.tar.gz"]],
241 exec { "go-cryptoportfolio-app":
242 cwd => $cf_front_app_api_workdir,
244 environment => ["HOME=${cf_home}"],
245 creates => $cf_front_app_api_bin,
246 command => "/usr/bin/make build",
247 require => Exec["go-cryptoportfolio-dependencies"],
250 file { "/etc/systemd/system/cryptoportfolio-app.service":
254 content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
255 notify => Exec["systemctl daemon-reload"],
258 service { 'cryptoportfolio-app':
261 subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
263 File["/etc/systemd/system/cryptoportfolio-app.service"],
264 Postgresql::Server::Db[$cf_pg_db]
267 exec { "dump $cf_pg_db structure":
269 user => $::profile::postgresql::pg_user,
270 group => $::profile::postgresql::pg_user,
271 command => "/usr/bin/pg_dump --schema-only --clean --no-publications $cf_pg_db > /var/lib/postgres/${cf_pg_db}.schema",
274 file { $cf_front_app_api_conf:
278 content => template("role/cryptoportfolio/api_conf.toml.erb"),
281 file { $cf_front_app_static_conf:
285 content => template("role/cryptoportfolio/static_conf.env.erb"),
286 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
289 exec { "web-cryptoportfolio-dependencies":
290 cwd => "${cf_front_app}/cmd/web",
291 environment => ["HOME=${cf_home}"],
292 command => "/usr/bin/make install",
293 creates => "${cf_front_app}/cmd/web/node_modules",
294 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
295 require => [Package["npm"], Package["nodejs"], Package["yarn"]]
298 exec { "remove build ${cf_front_app}/cmd/web/build/":
299 command => "/usr/bin/rm -rf '${cf_front_app}/cmd/web/build/'",
301 before => Exec["web-cryptoportfolio-build"]
304 exec { "web-cryptoportfolio-build":
305 cwd => "${cf_front_app}/cmd/web",
306 environment => ["HOME=${cf_home}"],
307 path => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"],
308 command => "/usr/bin/make static ENV=${cf_env}",
309 creates => "${cf_front_app}/cmd/web/build/static",
310 require => [File[$cf_front_app_static_conf], Exec["web-cryptoportfolio-dependencies"]]