aboutsummaryrefslogtreecommitdiff
path: root/modules/role/manifests/cryptoportfolio
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-13 20:04:52 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-13 22:52:47 +0100
commit39e05b4ec79c5dc07417a6cb1f0e1b2dcd111a1a (patch)
tree450cbcdec808189b8e08ed84bd3255dcfaadeebe /modules/role/manifests/cryptoportfolio
parenta386ce060c4c49d772bd4d03d6586012a266317e (diff)
downloadPuppet-39e05b4ec79c5dc07417a6cb1f0e1b2dcd111a1a.tar.gz
Puppet-39e05b4ec79c5dc07417a6cb1f0e1b2dcd111a1a.tar.zst
Puppet-39e05b4ec79c5dc07417a6cb1f0e1b2dcd111a1a.zip
Move cryptoporfolio module to sub-parts
Diffstat (limited to 'modules/role/manifests/cryptoportfolio')
-rw-r--r--modules/role/manifests/cryptoportfolio/apache.pp17
-rw-r--r--modules/role/manifests/cryptoportfolio/bot.pp101
-rw-r--r--modules/role/manifests/cryptoportfolio/front.pp158
-rw-r--r--modules/role/manifests/cryptoportfolio/notify.pp6
-rw-r--r--modules/role/manifests/cryptoportfolio/postgresql.pp116
5 files changed, 398 insertions, 0 deletions
diff --git a/modules/role/manifests/cryptoportfolio/apache.pp b/modules/role/manifests/cryptoportfolio/apache.pp
new file mode 100644
index 0000000..62d5447
--- /dev/null
+++ b/modules/role/manifests/cryptoportfolio/apache.pp
@@ -0,0 +1,17 @@
1class role::cryptoportfolio::apache inherits role::cryptoportfolio {
2 class { 'apache::mod::headers': }
3 apache::vhost { $web_host:
4 port => '443',
5 docroot => false,
6 manage_docroot => false,
7 proxy_dest => "http://localhost:8000",
8 request_headers => 'set X-Forwarded-Proto "https"',
9 ssl => true,
10 ssl_cert => "/etc/letsencrypt/live/$web_host/cert.pem",
11 ssl_key => "/etc/letsencrypt/live/$web_host/privkey.pem",
12 ssl_chain => "/etc/letsencrypt/live/$web_host/chain.pem",
13 require => Letsencrypt::Certonly[$web_host],
14 proxy_preserve_host => true;
15 default: * => $::profile::apache::apache_vhost_default;
16 }
17}
diff --git a/modules/role/manifests/cryptoportfolio/bot.pp b/modules/role/manifests/cryptoportfolio/bot.pp
new file mode 100644
index 0000000..a15c779
--- /dev/null
+++ b/modules/role/manifests/cryptoportfolio/bot.pp
@@ -0,0 +1,101 @@
1class role::cryptoportfolio::bot inherits role::cryptoportfolio {
2 $password_seed = lookup("base_installation::puppet_pass_seed")
3
4 $cf_bot_app = "${home}/bot"
5 $cf_bot_app_conf = "${home}/bot_config.ini"
6 $cf_bot_app_reports = "${home}/bot_reports"
7
8 ensure_packages(["python", "python-pip"])
9
10 file { $cf_bot_app:
11 ensure => "directory",
12 mode => "0700",
13 owner => $user,
14 group => $group,
15 require => User["$user:"],
16 }
17
18 archive { "${home}/trader_${bot_version}.tar.gz":
19 path => "${home}/trader_${bot_version}.tar.gz",
20 source => "https://git.immae.eu/releases/cryptoportfolio/trader/trader_${bot_version}.tar.gz",
21 checksum_type => "sha256",
22 checksum => $bot_sha256,
23 cleanup => false,
24 extract => true,
25 user => $user,
26 username => lookup("base_installation::ldap_cn"),
27 password => generate_password(24, $password_seed, "ldap"),
28 extract_path => $cf_bot_app,
29 require => [User["$user:"], File[$cf_bot_app]],
30 } ~>
31 exec { "py-cryptoportfolio-dependencies":
32 cwd => $cf_bot_app,
33 user => $user,
34 environment => ["HOME=${home}"],
35 command => "/usr/bin/make install",
36 require => User["$user:"],
37 refreshonly => true,
38 before => [
39 File[$cf_bot_app_conf],
40 Cron["py-cryptoportfolio-before"],
41 Cron["py-cryptoportfolio-after"],
42 ]
43 }
44
45 $pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio")
46 file { $cf_bot_app_conf:
47 owner => $user,
48 group => $group,
49 mode => "0600",
50 content => template("role/cryptoportfolio/bot_config.ini.erb"),
51 require => [
52 User["$user:"],
53 Archive["${home}/trader_${bot_version}.tar.gz"],
54 ],
55 }
56
57 cron { "py-cryptoportfolio-before":
58 ensure => present,
59 command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --before",
60 user => $user,
61 weekday => 7, # Sunday
62 hour => 22,
63 minute => 30,
64 environment => ["HOME=${home}","PATH=/usr/bin/"],
65 require => [
66 File[$cf_bot_app_conf],
67 Archive["${home}/trader_${bot_version}.tar.gz"]
68 ],
69 }
70
71 cron { "py-cryptoportfolio-after":
72 ensure => present,
73 command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf --after",
74 user => $user,
75 weekday => 1, # Monday
76 hour => 1,
77 minute => 0,
78 environment => ["HOME=${home}","PATH=/usr/bin/"],
79 require => [
80 File[$cf_bot_app_conf],
81 Archive["${home}/trader_${bot_version}.tar.gz"]
82 ],
83 }
84
85 unless empty($webhook_url) {
86 exec { "bot-slack-notify":
87 refreshonly => true,
88 environment => [
89 "P_PROJECT=Trader",
90 "P_WEBHOOK=${webhook_url}",
91 "P_VERSION=${bot_version}",
92 "P_HOST=${web_host}",
93 "P_HTTPS=${web_ssl}",
94 ],
95 command => "/usr/local/bin/slack-notify",
96 require => File["/usr/local/bin/slack-notify"],
97 subscribe => Exec["py-cryptoportfolio-dependencies"],
98 }
99 }
100}
101
diff --git a/modules/role/manifests/cryptoportfolio/front.pp b/modules/role/manifests/cryptoportfolio/front.pp
new file mode 100644
index 0000000..280ef8b
--- /dev/null
+++ b/modules/role/manifests/cryptoportfolio/front.pp
@@ -0,0 +1,158 @@
1class role::cryptoportfolio::front inherits role::cryptoportfolio {
2 ensure_resource('exec', 'systemctl daemon-reload', {
3 command => '/usr/bin/systemctl daemon-reload',
4 refreshonly => true
5 })
6
7 $password_seed = lookup("base_installation::puppet_pass_seed")
8
9 $cf_front_app = "${home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front"
10 $cf_front_app_api_workdir = "${cf_front_app}/cmd/app"
11 $cf_front_app_api_bin = "${cf_front_app_api_workdir}/cryptoportfolio-app"
12 $cf_front_app_api_conf = "${home}/conf.toml"
13 $cf_front_app_api_secret = generate_password(24, $password_seed, "cryptoportfolio_api_secret")
14
15 $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
16
17 ensure_packages(["go", "npm", "nodejs", "yarn"])
18
19 file { [
20 "${home}/go/",
21 "${home}/go/src",
22 "${home}/go/src/immae.eu",
23 "${home}/go/src/immae.eu/Immae",
24 "${home}/go/src/immae.eu/Immae/Projets",
25 "${home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies",
26 "${home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio",
27 $cf_front_app]:
28 ensure => "directory",
29 mode => "0700",
30 owner => $user,
31 group => $group,
32 require => User["$user:"],
33 }
34
35 file { "${home}/front":
36 ensure => "link",
37 target => $cf_front_app,
38 before => File[$cf_front_app],
39 }
40
41 file { "/etc/systemd/system/cryptoportfolio-app.service":
42 mode => "0644",
43 owner => "root",
44 group => "root",
45 content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
46 notify => Exec["systemctl daemon-reload"],
47 }
48
49 service { 'cryptoportfolio-app':
50 enable => true,
51 ensure => "running",
52 subscribe => [File[$cf_front_app_api_conf], Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
53 require => [
54 File["/etc/systemd/system/cryptoportfolio-app.service"],
55 Postgresql::Server::Db[$pg_db]
56 ],
57 } ~>
58 exec { "dump $pg_db structure":
59 refreshonly => true,
60 user => $::profile::postgresql::pg_user,
61 group => $::profile::postgresql::pg_user,
62 command => "/usr/bin/pg_dump --schema-only --clean --no-publications $pg_db > /var/lib/postgres/${pg_db}.schema",
63 }
64
65 archive { "${home}/front_${front_version}.tar.gz":
66 path => "${home}/front_${front_version}.tar.gz",
67 source => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz",
68 checksum_type => "sha256",
69 checksum => $front_sha256,
70 cleanup => false,
71 extract => true,
72 user => $user,
73 username => lookup("base_installation::ldap_cn"),
74 password => generate_password(24, $password_seed, "ldap"),
75 extract_path => $cf_front_app,
76 require => [User["$user:"], File[$cf_front_app]],
77 notify => [
78 Exec["web-cryptoportfolio-dependencies"],
79 Exec["go-get-dep"],
80 ]
81 }
82
83 # Api
84 $pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio")
85 $pg_host = "${pg_hostname}:${pg_port}"
86 file { $cf_front_app_api_conf:
87 owner => $user,
88 group => $group,
89 mode => "0600",
90 content => template("role/cryptoportfolio/api_conf.toml.erb"),
91 before => Exec["go-cryptoportfolio-app"],
92 }
93
94 exec { "go-get-dep":
95 user => $user,
96 environment => ["HOME=${home}"],
97 creates => "${home}/go/bin/dep",
98 command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep",
99 refreshonly => true,
100 } ~>
101 exec { "go-cryptoportfolio-dependencies":
102 cwd => $cf_front_app,
103 user => $user,
104 environment => ["HOME=${home}"],
105 command => "${home}/go/bin/dep ensure",
106 refreshonly => true,
107 } ~>
108 exec { "go-cryptoportfolio-app":
109 cwd => $cf_front_app_api_workdir,
110 user => $user,
111 environment => ["HOME=${home}"],
112 command => "/usr/bin/make build",
113 refreshonly => true,
114 }
115
116 # Static pages
117 file { $cf_front_app_static_conf:
118 owner => $user,
119 group => $group,
120 mode => "0600",
121 content => template("role/cryptoportfolio/static_conf.env.erb"),
122 before => Exec["web-cryptoportfolio-build"],
123 }
124
125 exec { "web-cryptoportfolio-dependencies":
126 cwd => "${cf_front_app}/cmd/web",
127 user => $user,
128 environment => ["HOME=${home}"],
129 command => "/usr/bin/make install",
130 refreshonly => true,
131 require => [Package["npm"], Package["nodejs"], Package["yarn"]]
132 } ~>
133 exec { "web-cryptoportfolio-build":
134 cwd => "${cf_front_app}/cmd/web",
135 user => $user,
136 environment => ["HOME=${home}"],
137 path => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"],
138 command => "/usr/bin/make static ENV=${env}",
139 refreshonly => true,
140 }
141
142 unless empty($webhook_url) {
143 exec { "front-slack-notify":
144 refreshonly => true,
145 environment => [
146 "P_PROJECT=Front",
147 "P_WEBHOOK=${webhook_url}",
148 "P_VERSION=${front_version}",
149 "P_HOST=${web_host}",
150 "P_HTTPS=${web_ssl}",
151 ],
152 command => "/usr/local/bin/slack-notify",
153 require => File["/usr/local/bin/slack-notify"],
154 subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
155 }
156 }
157
158}
diff --git a/modules/role/manifests/cryptoportfolio/notify.pp b/modules/role/manifests/cryptoportfolio/notify.pp
new file mode 100644
index 0000000..218312c
--- /dev/null
+++ b/modules/role/manifests/cryptoportfolio/notify.pp
@@ -0,0 +1,6 @@
1class role::cryptoportfolio::notify inherits role::cryptoportfolio {
2 file { "/usr/local/bin/slack-notify":
3 mode => "0755",
4 source => "puppet:///modules/role/cryptoportfolio/slack-notify.py",
5 }
6}
diff --git a/modules/role/manifests/cryptoportfolio/postgresql.pp b/modules/role/manifests/cryptoportfolio/postgresql.pp
new file mode 100644
index 0000000..cc4d2a9
--- /dev/null
+++ b/modules/role/manifests/cryptoportfolio/postgresql.pp
@@ -0,0 +1,116 @@
1class role::cryptoportfolio::postgresql inherits role::cryptoportfolio {
2 $password_seed = lookup("base_installation::puppet_pass_seed")
3
4 $pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio")
5 $pg_replication_password = generate_password(24, $password_seed, "postgres_cryptoportfolio_replication")
6
7 file { "/var/lib/postgres/data/certs":
8 ensure => directory,
9 mode => "0700",
10 owner => $::profile::postgresql::pg_user,
11 group => $::profile::postgresql::pg_user,
12 require => File["/var/lib/postgres"],
13 }
14
15 file { "/var/lib/postgres/data/certs/cert.pem":
16 source => "file:///etc/letsencrypt/live/$web_host/cert.pem",
17 mode => "0600",
18 links => "follow",
19 owner => $::profile::postgresql::pg_user,
20 group => $::profile::postgresql::pg_user,
21 require => [Letsencrypt::Certonly[$web_host], File["/var/lib/postgres/data/certs"]]
22 }
23
24 file { "/var/lib/postgres/data/certs/privkey.pem":
25 source => "file:///etc/letsencrypt/live/$web_host/privkey.pem",
26 mode => "0600",
27 links => "follow",
28 owner => $::profile::postgresql::pg_user,
29 group => $::profile::postgresql::pg_user,
30 require => [Letsencrypt::Certonly[$web_host], File["/var/lib/postgres/data/certs"]]
31 }
32
33 postgresql::server::config_entry { "wal_level":
34 value => "logical",
35 }
36
37 postgresql::server::config_entry { "ssl":
38 value => "on",
39 require => Letsencrypt::Certonly[$web_host],
40 }
41
42 postgresql::server::config_entry { "ssl_cert_file":
43 value => "/var/lib/postgres/data/certs/cert.pem",
44 require => Letsencrypt::Certonly[$web_host],
45 }
46
47 postgresql::server::config_entry { "ssl_key_file":
48 value => "/var/lib/postgres/data/certs/privkey.pem",
49 require => Letsencrypt::Certonly[$web_host],
50 }
51
52 postgresql::server::db { $pg_db:
53 user => $pg_user,
54 password => postgresql_password($pg_user, $pg_password),
55 }
56 ->
57 postgresql_psql { "CREATE PUBLICATION ${pg_db}_publication FOR ALL TABLES":
58 db => $pg_db,
59 unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${pg_db}_publication'",
60 }
61 ->
62 postgresql::server::role { $pg_user_replication:
63 db => $pg_db,
64 replication => true,
65 password_hash => postgresql_password($pg_user_replication, $pg_replication_password),
66 }
67 ->
68 postgresql::server::database_grant { $pg_user_replication:
69 db => $pg_db,
70 privilege => "CONNECT",
71 role => $pg_user_replication,
72 }
73 ->
74 postgresql::server::grant { "all tables in schema:public:$pg_user_replication":
75 db => $pg_db,
76 role => $pg_user_replication,
77 privilege => "SELECT",
78 object_type => "ALL TABLES IN SCHEMA",
79 object_name => "public",
80 }
81 ->
82 postgresql::server::grant { "all sequences in schema:public:$pg_user_replication":
83 db => $pg_db,
84 role => $pg_user_replication,
85 privilege => "SELECT",
86 object_type => "ALL SEQUENCES IN SCHEMA",
87 object_name => "public",
88 }
89
90 postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
91 type => 'host',
92 database => $pg_db,
93 user => $pg_user,
94 address => '127.0.0.1/32',
95 auth_method => 'md5',
96 order => "05-01",
97 }
98 postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
99 type => 'host',
100 database => $pg_db,
101 user => $pg_user,
102 address => '::1/128',
103 auth_method => 'md5',
104 order => "05-01",
105 }
106
107 postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
108 type => 'hostssl',
109 database => $pg_db,
110 user => $pg_user_replication,
111 address => 'immae.eu',
112 auth_method => 'md5',
113 order => "05-01",
114 }
115
116}