]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blobdiff - modules/role/manifests/cryptoportfolio.pp
Fetch node informations from LDAP
[perso/Immae/Projets/Puppet.git] / modules / role / manifests / cryptoportfolio.pp
index 9a2bfd2e54c2eae98f9c62e875349ec8ea31d7f3..1e39479ebdc08d9c59587dbc47f210aefa0cb382 100644 (file)
@@ -1,9 +1,15 @@
 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| { {} }
 
@@ -12,13 +18,15 @@ class role::cryptoportfolio {
   $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_pg_hostname = "localhost"
+  $cf_pg_port = "5432"
+  $cf_pg_host = "${cf_pg_hostname}:${cf_pg_port}"
 
   $cf_user = "cryptoportfolio"
   $cf_group = "cryptoportfolio"
   $cf_home = "/opt/cryptoportfolio"
   $cf_env = "prod"
-  $cf_front_app_host = "cryptoportfolio.immae.eu"
+  $cf_front_app_host = lookup("base_installation::system_hostname") |$key| { "example.com" }
   $cf_front_app_port = ""
   $cf_front_app_ssl = "true"
   $cf_front_app = "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front"
@@ -29,6 +37,10 @@ class role::cryptoportfolio {
 
   $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
 
+  $cf_bot_app = "${cf_home}/bot"
+  $cf_bot_app_conf = "${cf_home}/bot_config.ini"
+  $cf_bot_app_reports = "${cf_home}/bot_reports"
+
   file { "/var/lib/postgres/data/certs":
     ensure  => directory,
     mode    => "0700",
@@ -170,6 +182,89 @@ class role::cryptoportfolio {
   $front_version = lookup("cryptoportfolio::front_version") |$key| { {} }
   $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} }
 
+  $bot_version = lookup("cryptoportfolio::bot_version") |$key| { {} }
+  $bot_sha256 = lookup("cryptoportfolio::bot_sha256") |$key| { {} }
+
+  unless empty($bot_version) {
+    ensure_packages(["python", "python-pip"])
+
+    file { $cf_bot_app:
+      ensure  => "directory",
+      mode    => "0700",
+      owner   => $cf_user,
+      group   => $cf_group,
+      require => User[$cf_user],
+    }
+
+    archive { "${cf_home}/${bot_version}.tar.gz":
+      path          => "${cf_home}/${bot_version}.tar.gz",
+      source        => "https://git.immae.eu/releases/cryptoportfolio/trader/trader_${bot_version}.tar.gz",
+      checksum_type => "sha256",
+      checksum      => $bot_sha256,
+      cleanup       => false,
+      extract       => true,
+      user          => "cryptoportfolio",
+      username      => $facts["ec2_metadata"]["hostname"],
+      password      => generate_password(24, $password_seed, "ldap"),
+      extract_path  => $cf_bot_app,
+      require       => [User[$cf_user], File[$cf_bot_app]],
+    } ~>
+    exec { "py-cryptoportfolio-dependencies":
+      cwd         => $cf_bot_app,
+      user        => $cf_user,
+      environment => ["HOME=${cf_home}"],
+      command     => "/usr/bin/make install",
+      require     => User[$cf_user],
+      refreshonly => true,
+      before      => [
+        File[$cf_bot_app_conf],
+        Cron["py-cryptoportfolio-before"],
+        Cron["py-cryptoportfolio-after"],
+      ]
+    }
+
+    file { $cf_bot_app_conf:
+      owner   => $cf_user,
+      group   => $cf_group,
+      mode    => "0600",
+      content => template("role/cryptoportfolio/bot_config.ini.erb"),
+      require => [
+        User[$cf_user],
+        Archive["${cf_home}/${bot_version}.tar.gz"],
+      ],
+    }
+
+    cron { "py-cryptoportfolio-before":
+      ensure      => present,
+      command     => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --before",
+      user        => "cryptoportfolio",
+      weekday     => 7, # Sunday
+      hour        => 22,
+      minute      => 30,
+      environment => ["HOME=${cf_home}","PATH=/usr/bin/"],
+      require     => [
+        File[$cf_bot_app_conf],
+        Archive["${cf_home}/${bot_version}.tar.gz"]
+      ],
+    }
+
+    cron { "py-cryptoportfolio-after":
+      ensure      => present,
+      command     => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --after",
+      user        => "cryptoportfolio",
+      weekday     => 1, # Monday
+      hour        => 1,
+      minute      => 0,
+      environment => ["HOME=${cf_home}","PATH=/usr/bin/"],
+      require     => [
+        File[$cf_bot_app_conf],
+        Archive["${cf_home}/${bot_version}.tar.gz"]
+      ],
+    }
+  }
+
+  # FIXME: incorrect build for go app
+  # FIXME: restore backup
   unless empty($front_version) {
     ensure_packages(["go", "npm", "nodejs", "yarn"])
 
@@ -196,7 +291,9 @@ class role::cryptoportfolio {
       checksum      => $front_sha256,
       cleanup       => false,
       extract       => true,
-      user          => "cryptoportfolio",
+      user          => $cf_user,
+      username      => $facts["ec2_metadata"]["hostname"],
+      password      => generate_password(24, $password_seed, "ldap"),
       extract_path  => $cf_front_app,
       require       => [User[$cf_user], File[$cf_front_app]],
     }
@@ -244,9 +341,7 @@ 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':
@@ -298,11 +393,10 @@ class role::cryptoportfolio {
     exec { "web-cryptoportfolio-build":
       cwd         => "${cf_front_app}/cmd/web",
       environment => ["HOME=${cf_home}"],
+      path        => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"],
       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"]]
     }
   }
-
-  # TODO: xmr_stack
 }