]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blobdiff - modules/role/manifests/cryptoportfolio.pp
Add xmr-stak
[perso/Immae/Projets/Puppet.git] / modules / role / manifests / cryptoportfolio.pp
index 0f26527e179f08f4dc078f661ec082b34e6e46ed..5860f90a8163b1c796df9cbbc5474e951bb60a26 100644 (file)
@@ -1,13 +1,23 @@
 class role::cryptoportfolio {
+  ensure_resource('exec', 'systemctl daemon-reload', {
+    command     => '/usr/bin/systemctl daemon-reload',
+    refreshonly =>  true
+  })
+
   include "base_installation"
 
+  include "profile::tools"
   include "profile::postgresql"
+  include "profile::apache"
+  include "profile::xmr_stak"
 
   $password_seed = lookup("base_installation::puppet_pass_seed") |$key| { {} }
 
   $cf_pg_user = "cryptoportfolio"
+  $cf_pg_user_replication = "cryptoportfolio_replication"
   $cf_pg_db = "cryptoportfolio"
   $cf_pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio")
+  $cf_pg_replication_password = generate_password(24, $password_seed, "postgres_cryptoportfolio_replication")
   $cf_pg_host = "localhost:5432"
 
   $cf_user = "cryptoportfolio"
@@ -16,7 +26,7 @@ class role::cryptoportfolio {
   $cf_env = "prod"
   $cf_front_app_host = "cryptoportfolio.immae.eu"
   $cf_front_app_port = ""
-  $cf_front_app_ssl = "false"
+  $cf_front_app_ssl = "true"
   $cf_front_app = "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front"
   $cf_front_app_api_workdir = "${cf_front_app}/cmd/app"
   $cf_front_app_api_bin = "${cf_front_app_api_workdir}/cryptoportfolio-app"
@@ -25,9 +35,87 @@ class role::cryptoportfolio {
 
   $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
 
+  file { "/var/lib/postgres/data/certs":
+    ensure  => directory,
+    mode    => "0700",
+    owner   => $::profile::postgresql::pg_user,
+    group   => $::profile::postgresql::pg_user,
+    require => File["/var/lib/postgres"],
+  }
+
+  file { "/var/lib/postgres/data/certs/cert.pem":
+    source  => "file:///etc/letsencrypt/live/$cf_front_app_host/cert.pem",
+    mode    => "0600",
+    links   => "follow",
+    owner   => $::profile::postgresql::pg_user,
+    group   => $::profile::postgresql::pg_user,
+    require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
+  }
+
+  file { "/var/lib/postgres/data/certs/privkey.pem":
+    source  => "file:///etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
+    mode    => "0600",
+    links   => "follow",
+    owner   => $::profile::postgresql::pg_user,
+    group   => $::profile::postgresql::pg_user,
+    require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
+  }
+
+  postgresql::server::config_entry { "wal_level":
+    value   => "logical",
+  }
+
+  postgresql::server::config_entry { "ssl":
+    value   => "on",
+    require => Letsencrypt::Certonly[$cf_front_app_host],
+  }
+
+  postgresql::server::config_entry { "ssl_cert_file":
+    value   => "/var/lib/postgres/data/certs/cert.pem",
+    require => Letsencrypt::Certonly[$cf_front_app_host],
+  }
+
+  postgresql::server::config_entry { "ssl_key_file":
+    value   => "/var/lib/postgres/data/certs/privkey.pem",
+    require => Letsencrypt::Certonly[$cf_front_app_host],
+  }
+
   postgresql::server::db { $cf_pg_db:
     user     =>  $cf_pg_user,
-    password =>  postgresql_password($cf_pg_user, $cf_pg_password)
+    password =>  postgresql_password($cf_pg_user, $cf_pg_password),
+  }
+  ->
+  postgresql_psql { "CREATE PUBLICATION ${cf_pg_db}_publication FOR ALL TABLES":
+    db     => $cf_pg_db,
+    unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${cf_pg_db}_publication'",
+  }
+  ->
+  postgresql::server::role { $cf_pg_user_replication:
+    db            => $cf_pg_db,
+    replication   => true,
+    password_hash => postgresql_password($cf_pg_user_replication, $cf_pg_replication_password),
+  }
+  ->
+  postgresql::server::database_grant { $cf_pg_user_replication:
+    db        => $cf_pg_db,
+    privilege => "CONNECT",
+    role      => $cf_pg_user_replication,
+  }
+  ->
+  postgresql::server::grant { "all tables in schema:public:$cf_pg_user_replication":
+    db          => $cf_pg_db,
+    role        => $cf_pg_user_replication,
+    privilege   => "SELECT",
+    object_type => "ALL TABLES IN SCHEMA",
+    object_name => "public",
+  }
+  ->
+  postgresql::server::grant { "all sequences in schema:public:$cf_pg_user_replication":
+    db          => $cf_pg_db,
+    role        => $cf_pg_user_replication,
+    privilege   => "SELECT",
+    object_type => "ALL SEQUENCES IN SCHEMA",
+    object_name => "public",
   }
 
   postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
@@ -47,11 +135,33 @@ class role::cryptoportfolio {
     order       => "b0",
   }
 
-  class { 'nginx': }
+  postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
+    type        => 'hostssl',
+    database    => $cf_pg_db,
+    user        => $cf_pg_user_replication,
+    address     => 'immae.eu',
+    auth_method => 'md5',
+    order       => "b0",
+  }
+
+  letsencrypt::certonly { $cf_front_app_host: ;
+    default: * => $::profile::apache::letsencrypt_certonly_default;
+  }
 
-  nginx::resource::server { $cf_front_app_host:
-    listen_port => 80,
-    proxy       => 'http://localhost:8000',
+  class { 'apache::mod::headers': }
+  apache::vhost { $cf_front_app_host:
+    port                => '443',
+    docroot             => false,
+    manage_docroot      => false,
+    proxy_dest          => "http://localhost:8000",
+    request_headers     => 'set X-Forwarded-Proto "https"',
+    ssl                 => true,
+    ssl_cert            => "/etc/letsencrypt/live/$cf_front_app_host/cert.pem",
+    ssl_key             => "/etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
+    ssl_chain           => "/etc/letsencrypt/live/$cf_front_app_host/chain.pem",
+    require             => Letsencrypt::Certonly[$cf_front_app_host],
+    proxy_preserve_host => true;
+    default: *          => $::profile::apache::apache_vhost_default;
   }
 
   user { $cf_user:
@@ -100,7 +210,13 @@ class role::cryptoportfolio {
     file { "${cf_home}/front":
       ensure  => "link",
       target  => $cf_front_app,
-      require => Archive["/opt/cryptoportfolio/${front_version}.tar.gz"]
+      before => File[$cf_front_app],
+    } ~>
+    exec { "remove old ${cf_front_app} directory":
+      refreshonly => true,
+      user        => $cf_user,
+      command     => "/usr/bin/rm -rf ${cf_front_app}",
+      before      => File[$cf_front_app],
     }
 
     exec { "go-get-dep":
@@ -134,15 +250,23 @@ class role::cryptoportfolio {
       owner   => "root",
       group   => "root",
       content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
-    } ~> exec { 'systemctl deamon-reload':
-      command     => '/usr/bin/systemctl daemon-reload',
-      refreshonly => true
+      notify  => Exec["systemctl daemon-reload"],
     }
 
     service { 'cryptoportfolio-app':
-      enable  => true,
-      ensure  => "running",
-      require => [File["/etc/systemd/system/cryptoportfolio-app.service"]],
+      enable    => true,
+      ensure    => "running",
+      subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
+      require   => [
+        File["/etc/systemd/system/cryptoportfolio-app.service"],
+        Postgresql::Server::Db[$cf_pg_db]
+      ],
+    } ~>
+    exec { "dump $cf_pg_db structure":
+      refreshonly => true,
+      user        => $::profile::postgresql::pg_user,
+      group       => $::profile::postgresql::pg_user,
+      command     => "/usr/bin/pg_dump --schema-only --clean --no-publications $cf_pg_db > /var/lib/postgres/${cf_pg_db}.schema",
     }
 
     file { $cf_front_app_api_conf:
@@ -157,6 +281,7 @@ class role::cryptoportfolio {
       group   => $cf_group,
       mode    => "0600",
       content => template("role/cryptoportfolio/static_conf.env.erb"),
+      notify  => Exec["remove build ${cf_front_app}/cmd/web/build/"],
     }
 
     exec { "web-cryptoportfolio-dependencies":
@@ -164,9 +289,16 @@ class role::cryptoportfolio {
       environment => ["HOME=${cf_home}"],
       command     => "/usr/bin/make install",
       creates     => "${cf_front_app}/cmd/web/node_modules",
+      notify      => Exec["remove build ${cf_front_app}/cmd/web/build/"],
       require     => [Package["npm"], Package["nodejs"], Package["yarn"]]
     }
 
+    exec { "remove build ${cf_front_app}/cmd/web/build/":
+      command     => "/usr/bin/rm -rf '${cf_front_app}/cmd/web/build/'",
+      refreshonly => true,
+      before      => Exec["web-cryptoportfolio-build"]
+    }
+
     exec { "web-cryptoportfolio-build":
       cwd         => "${cf_front_app}/cmd/web",
       environment => ["HOME=${cf_home}"],
@@ -175,5 +307,4 @@ class role::cryptoportfolio {
       require     => [File[$cf_front_app_static_conf], Exec["web-cryptoportfolio-dependencies"]]
     }
   }
-
 }