]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blobdiff - modules/role/manifests/cryptoportfolio.pp
Add letsencrypt
[perso/Immae/Projets/Puppet.git] / modules / role / manifests / cryptoportfolio.pp
index 16c2f960a47fabe31604842a65274b4f933b12c3..d2323a45bc0213f8fdd933e577c9db7f4a80975a 100644 (file)
 class role::cryptoportfolio {
   include "base_installation"
 
+  include "profile::tools"
   include "profile::postgresql"
+  include "profile::apache"
 
   $password_seed = lookup("base_installation::puppet_pass_seed") |$key| { {} }
 
-  postgresql::server::db { 'cryptoportfolio':
-    user =>  'cryptoportfolio',
-    password =>  postgresql_password('cryptoportfolio', generate_password(24, $password_seed, "postgres_cryptoportfolio")),
+  $cf_pg_user = "cryptoportfolio"
+  $cf_pg_db = "cryptoportfolio"
+  $cf_pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio")
+  $cf_pg_host = "localhost:5432"
+
+  $cf_user = "cryptoportfolio"
+  $cf_group = "cryptoportfolio"
+  $cf_home = "/opt/cryptoportfolio"
+  $cf_env = "prod"
+  $cf_front_app_host = "cryptoportfolio.immae.eu"
+  $cf_front_app_port = ""
+  $cf_front_app_ssl = "false"
+  $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"
+  $cf_front_app_api_conf = "${cf_home}/conf.toml"
+  $cf_front_app_api_secret = generate_password(24, $password_seed, "cryptoportfolio_api_secret")
+
+  $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
+
+  postgresql::server::db { $cf_pg_db:
+    user     =>  $cf_pg_user,
+    password =>  postgresql_password($cf_pg_user, $cf_pg_password)
   }
 
   postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
     type        => 'host',
-    database    => 'cryptoportfolio',
-    user        => 'cryptoportfolio',
+    database    => $cf_pg_db,
+    user        => $cf_pg_user,
     address     => '127.0.0.1/32',
     auth_method => 'md5',
     order       => "b0",
   }
   postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
     type        => 'host',
-    database    => 'cryptoportfolio',
-    user        => 'cryptoportfolio',
+    database    => $cf_pg_db,
+    user        => $cf_pg_user,
     address     => '::1/128',
     auth_method => 'md5',
     order       => "b0",
   }
 
-  ensure_packages("go")
+  letsencrypt::certonly { $cf_front_app_host: ;
+    default: * => $::profile::apache::letsencrypt_certonly_default;
+  }
 
-  class { 'nginx': }
+  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;
+  }
 
-  nginx::resource::server { 'cryptoportfolio.immae.eu':
-    listen_port => 80,
-    proxy       => 'http://localhost:8000',
+  user { $cf_user:
+    name       => $cf_user,
+    ensure     => "present",
+    managehome => true,
+    home       => $cf_home,
+    system     => true,
+    password   => '!!',
   }
+
+  $front_version = lookup("cryptoportfolio::front_version") |$key| { {} }
+  $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} }
+
+  unless empty($front_version) {
+    ensure_packages(["go", "npm", "nodejs", "yarn"])
+
+    file { [
+        "${cf_home}/go/",
+        "${cf_home}/go/src",
+        "${cf_home}/go/src/immae.eu",
+        "${cf_home}/go/src/immae.eu/Immae",
+        "${cf_home}/go/src/immae.eu/Immae/Projets",
+        "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies",
+        "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio",
+        $cf_front_app]:
+      ensure  => "directory",
+      mode    => "0700",
+      owner   => $cf_user,
+      group   => $cf_group,
+      require => User[$cf_user],
+    }
+
+    archive { "${cf_home}/${front_version}.tar.gz":
+      path          => "${cf_home}/${front_version}.tar.gz",
+      source        => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz",
+      checksum_type => "sha256",
+      checksum      => $front_sha256,
+      cleanup       => false,
+      extract       => true,
+      user          => "cryptoportfolio",
+      extract_path  => $cf_front_app,
+      require       => [User[$cf_user], File[$cf_front_app]],
+    }
+
+    file { "${cf_home}/front":
+      ensure  => "link",
+      target  => $cf_front_app,
+      require => Archive["/opt/cryptoportfolio/${front_version}.tar.gz"]
+    }
+
+    exec { "go-get-dep":
+      user        => $cf_user,
+      environment => ["HOME=${cf_home}"],
+      creates     => "${cf_home}/go/bin/dep",
+      command     => "/usr/bin/go get -u github.com/golang/dep/cmd/dep",
+      require     => User[$cf_user],
+    }
+
+    exec { "go-cryptoportfolio-dependencies":
+      cwd         => $cf_front_app,
+      user        => $cf_user,
+      environment => ["HOME=${cf_home}"],
+      creates     => "${cf_front_app}/vendor",
+      command     => "${cf_home}/go/bin/dep ensure",
+      require     => [Exec["go-get-dep"], Archive["${cf_home}/${front_version}.tar.gz"]],
+    }
+
+    exec { "go-cryptoportfolio-app":
+      cwd         => $cf_front_app_api_workdir,
+      user        => $cf_user,
+      environment => ["HOME=${cf_home}"],
+      creates     => $cf_front_app_api_bin,
+      command     => "/usr/bin/make build",
+      require     => Exec["go-cryptoportfolio-dependencies"],
+    }
+
+    file { "/etc/systemd/system/cryptoportfolio-app.service":
+      mode    => "0644",
+      owner   => "root",
+      group   => "root",
+      content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
+    } ~> exec { 'systemctl deamon-reload':
+      command     => '/usr/bin/systemctl daemon-reload',
+      refreshonly => true
+    }
+
+    service { 'cryptoportfolio-app':
+      enable  => true,
+      ensure  => "running",
+      require => [File["/etc/systemd/system/cryptoportfolio-app.service"]],
+    }
+
+    file { $cf_front_app_api_conf:
+      owner   => $cf_user,
+      group   => $cf_group,
+      mode    => "0600",
+      content => template("role/cryptoportfolio/api_conf.toml.erb"),
+    }
+
+    file { $cf_front_app_static_conf:
+      owner   => $cf_user,
+      group   => $cf_group,
+      mode    => "0600",
+      content => template("role/cryptoportfolio/static_conf.env.erb"),
+    }
+
+    exec { "web-cryptoportfolio-dependencies":
+      cwd         => "${cf_front_app}/cmd/web",
+      environment => ["HOME=${cf_home}"],
+      command     => "/usr/bin/make install",
+      creates     => "${cf_front_app}/cmd/web/node_modules",
+      require     => [Package["npm"], Package["nodejs"], Package["yarn"]]
+    }
+
+    exec { "web-cryptoportfolio-build":
+      cwd         => "${cf_front_app}/cmd/web",
+      environment => ["HOME=${cf_home}"],
+      command     => "/usr/bin/make static ENV=${cf_env}",
+      creates     => "${cf_front_app}/cmd/web/build/static",
+      require     => [File[$cf_front_app_static_conf], Exec["web-cryptoportfolio-dependencies"]]
+    }
+  }
+
 }