]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blame - modules/role/manifests/cryptoportfolio.pp
Add bot package
[perso/Immae/Projets/Puppet.git] / modules / role / manifests / cryptoportfolio.pp
CommitLineData
57ae81ea 1class role::cryptoportfolio {
68bbff61
IB
2 ensure_resource('exec', 'systemctl daemon-reload', {
3 command => '/usr/bin/systemctl daemon-reload',
4 refreshonly => true
5 })
6
57ae81ea
IB
7 include "base_installation"
8
8af3ea1e 9 include "profile::tools"
57ae81ea 10 include "profile::postgresql"
2bb35074 11 include "profile::apache"
68bbff61 12 include "profile::xmr_stak"
57ae81ea
IB
13
14 $password_seed = lookup("base_installation::puppet_pass_seed") |$key| { {} }
15
159df176 16 $cf_pg_user = "cryptoportfolio"
b3015828 17 $cf_pg_user_replication = "cryptoportfolio_replication"
159df176
IB
18 $cf_pg_db = "cryptoportfolio"
19 $cf_pg_password = generate_password(24, $password_seed, "postgres_cryptoportfolio")
b3015828 20 $cf_pg_replication_password = generate_password(24, $password_seed, "postgres_cryptoportfolio_replication")
159df176
IB
21 $cf_pg_host = "localhost:5432"
22
23 $cf_user = "cryptoportfolio"
24 $cf_group = "cryptoportfolio"
25 $cf_home = "/opt/cryptoportfolio"
26 $cf_env = "prod"
27 $cf_front_app_host = "cryptoportfolio.immae.eu"
28 $cf_front_app_port = ""
e17078be 29 $cf_front_app_ssl = "true"
159df176
IB
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")
35
36 $cf_front_app_static_conf = "${cf_front_app}/cmd/web/env/prod.env"
37
236d7e6d
IB
38 $cf_bot_app = "${cf_home}/bot"
39
b3015828
IB
40 file { "/var/lib/postgres/data/certs":
41 ensure => directory,
42 mode => "0700",
43 owner => $::profile::postgresql::pg_user,
44 group => $::profile::postgresql::pg_user,
45 require => File["/var/lib/postgres"],
46 }
47
48 file { "/var/lib/postgres/data/certs/cert.pem":
49 source => "file:///etc/letsencrypt/live/$cf_front_app_host/cert.pem",
50 mode => "0600",
51 links => "follow",
52 owner => $::profile::postgresql::pg_user,
53 group => $::profile::postgresql::pg_user,
54 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
55 }
56
57 file { "/var/lib/postgres/data/certs/privkey.pem":
58 source => "file:///etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
59 mode => "0600",
60 links => "follow",
61 owner => $::profile::postgresql::pg_user,
62 group => $::profile::postgresql::pg_user,
63 require => [Letsencrypt::Certonly[$cf_front_app_host], File["/var/lib/postgres/data/certs"]]
64 }
65
66 postgresql::server::config_entry { "wal_level":
67 value => "logical",
68 }
69
70 postgresql::server::config_entry { "ssl":
71 value => "on",
72 require => Letsencrypt::Certonly[$cf_front_app_host],
73 }
74
75 postgresql::server::config_entry { "ssl_cert_file":
76 value => "/var/lib/postgres/data/certs/cert.pem",
77 require => Letsencrypt::Certonly[$cf_front_app_host],
78 }
79
80 postgresql::server::config_entry { "ssl_key_file":
81 value => "/var/lib/postgres/data/certs/privkey.pem",
82 require => Letsencrypt::Certonly[$cf_front_app_host],
83 }
84
159df176
IB
85 postgresql::server::db { $cf_pg_db:
86 user => $cf_pg_user,
b3015828
IB
87 password => postgresql_password($cf_pg_user, $cf_pg_password),
88 }
89 ->
90 postgresql_psql { "CREATE PUBLICATION ${cf_pg_db}_publication FOR ALL TABLES":
91 db => $cf_pg_db,
92 unless => "SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = '${cf_pg_db}_publication'",
93 }
94 ->
95 postgresql::server::role { $cf_pg_user_replication:
96 db => $cf_pg_db,
97 replication => true,
98 password_hash => postgresql_password($cf_pg_user_replication, $cf_pg_replication_password),
99 }
100 ->
101 postgresql::server::database_grant { $cf_pg_user_replication:
102 db => $cf_pg_db,
103 privilege => "CONNECT",
104 role => $cf_pg_user_replication,
105 }
106 ->
107 postgresql::server::grant { "all tables in schema:public:$cf_pg_user_replication":
108 db => $cf_pg_db,
109 role => $cf_pg_user_replication,
110 privilege => "SELECT",
111 object_type => "ALL TABLES IN SCHEMA",
112 object_name => "public",
113 }
114 ->
115 postgresql::server::grant { "all sequences in schema:public:$cf_pg_user_replication":
116 db => $cf_pg_db,
117 role => $cf_pg_user_replication,
118 privilege => "SELECT",
119 object_type => "ALL SEQUENCES IN SCHEMA",
120 object_name => "public",
57ae81ea
IB
121 }
122
6a919776
IB
123 postgresql::server::pg_hba_rule { 'allow localhost TCP access to cryptoportfolio user':
124 type => 'host',
159df176
IB
125 database => $cf_pg_db,
126 user => $cf_pg_user,
6a919776
IB
127 address => '127.0.0.1/32',
128 auth_method => 'md5',
129 order => "b0",
130 }
131 postgresql::server::pg_hba_rule { 'allow localhost ip6 TCP access to cryptoportfolio user':
132 type => 'host',
159df176
IB
133 database => $cf_pg_db,
134 user => $cf_pg_user,
6a919776
IB
135 address => '::1/128',
136 auth_method => 'md5',
137 order => "b0",
138 }
0a4ec379 139
b3015828
IB
140 postgresql::server::pg_hba_rule { 'allow TCP access to replication user from immae.eu':
141 type => 'hostssl',
142 database => $cf_pg_db,
143 user => $cf_pg_user_replication,
144 address => 'immae.eu',
145 auth_method => 'md5',
146 order => "b0",
147 }
148
e345248b
IB
149 letsencrypt::certonly { $cf_front_app_host: ;
150 default: * => $::profile::apache::letsencrypt_certonly_default;
151 }
152
153 class { 'apache::mod::headers': }
2bb35074 154 apache::vhost { $cf_front_app_host:
e345248b 155 port => '443',
2bb35074
IB
156 docroot => false,
157 manage_docroot => false,
158 proxy_dest => "http://localhost:8000",
e345248b
IB
159 request_headers => 'set X-Forwarded-Proto "https"',
160 ssl => true,
161 ssl_cert => "/etc/letsencrypt/live/$cf_front_app_host/cert.pem",
162 ssl_key => "/etc/letsencrypt/live/$cf_front_app_host/privkey.pem",
163 ssl_chain => "/etc/letsencrypt/live/$cf_front_app_host/chain.pem",
164 require => Letsencrypt::Certonly[$cf_front_app_host],
165 proxy_preserve_host => true;
166 default: * => $::profile::apache::apache_vhost_default;
0a4ec379 167 }
f67c5285 168
159df176
IB
169 user { $cf_user:
170 name => $cf_user,
76a321e1
IB
171 ensure => "present",
172 managehome => true,
159df176 173 home => $cf_home,
76a321e1
IB
174 system => true,
175 password => '!!',
176 }
177
178 $front_version = lookup("cryptoportfolio::front_version") |$key| { {} }
179 $front_sha256 = lookup("cryptoportfolio::front_sha256") |$key| { {} }
180
236d7e6d
IB
181 $bot_version = lookup("cryptoportfolio::bot_version") |$key| { {} }
182 $bot_sha256 = lookup("cryptoportfolio::bot_sha256") |$key| { {} }
183
184 unless empty($bot_version) {
185 ensure_packages(["python", "python-pip"])
186
187 file { $cf_bot_app:
188 ensure => "directory",
189 mode => "0700",
190 owner => $cf_user,
191 group => $cf_group,
192 require => User[$cf_user],
193 }
194
195 archive { "${cf_home}/${bot_version}.tar.gz":
196 path => "${cf_home}/${bot_version}.tar.gz",
197 source => "https://git.immae.eu/releases/cryptoportfolio/trader/trader_${bot_version}.tar.gz",
198 checksum_type => "sha256",
199 checksum => $bot_sha256,
200 cleanup => false,
201 extract => true,
202 user => "cryptoportfolio",
203 username => $facts["ec2_metadata"]["hostname"],
204 password => generate_password(24, $password_seed, "ldap"),
205 extract_path => $cf_bot_app,
206 require => [User[$cf_user], File[$cf_bot_app]],
207 } ~>
208 exec { "py-cryptoportfolio-dependencies":
209 cwd => $cf_bot_app,
210 user => $cf_user,
211 environment => ["HOME=${cf_home}"],
212 command => "/usr/bin/make install",
213 require => User[$cf_user],
214 refreshonly => true,
215 }
216 }
217
76a321e1 218 unless empty($front_version) {
159df176 219 ensure_packages(["go", "npm", "nodejs", "yarn"])
76a321e1 220
159df176
IB
221 file { [
222 "${cf_home}/go/",
223 "${cf_home}/go/src",
224 "${cf_home}/go/src/immae.eu",
225 "${cf_home}/go/src/immae.eu/Immae",
226 "${cf_home}/go/src/immae.eu/Immae/Projets",
227 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies",
228 "${cf_home}/go/src/immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio",
229 $cf_front_app]:
230 ensure => "directory",
76a321e1 231 mode => "0700",
159df176
IB
232 owner => $cf_user,
233 group => $cf_group,
234 require => User[$cf_user],
76a321e1
IB
235 }
236
159df176
IB
237 archive { "${cf_home}/${front_version}.tar.gz":
238 path => "${cf_home}/${front_version}.tar.gz",
76a321e1 239 source => "https://git.immae.eu/releases/cryptoportfolio/front/front_${front_version}.tar.gz",
76a321e1
IB
240 checksum_type => "sha256",
241 checksum => $front_sha256,
242 cleanup => false,
243 extract => true,
159df176 244 user => "cryptoportfolio",
6369d6cf
IB
245 username => $facts["ec2_metadata"]["hostname"],
246 password => generate_password(24, $password_seed, "ldap"),
159df176
IB
247 extract_path => $cf_front_app,
248 require => [User[$cf_user], File[$cf_front_app]],
76a321e1
IB
249 }
250
159df176 251 file { "${cf_home}/front":
76a321e1 252 ensure => "link",
159df176 253 target => $cf_front_app,
f099c453
IB
254 before => File[$cf_front_app],
255 } ~>
43c4ee4e 256 exec { "remove old ${cf_front_app} directory":
f099c453
IB
257 refreshonly => true,
258 user => $cf_user,
259 command => "/usr/bin/rm -rf ${cf_front_app}",
260 before => File[$cf_front_app],
159df176
IB
261 }
262
263 exec { "go-get-dep":
264 user => $cf_user,
265 environment => ["HOME=${cf_home}"],
266 creates => "${cf_home}/go/bin/dep",
267 command => "/usr/bin/go get -u github.com/golang/dep/cmd/dep",
268 require => User[$cf_user],
269 }
270
271 exec { "go-cryptoportfolio-dependencies":
272 cwd => $cf_front_app,
273 user => $cf_user,
274 environment => ["HOME=${cf_home}"],
275 creates => "${cf_front_app}/vendor",
276 command => "${cf_home}/go/bin/dep ensure",
277 require => [Exec["go-get-dep"], Archive["${cf_home}/${front_version}.tar.gz"]],
278 }
279
280 exec { "go-cryptoportfolio-app":
281 cwd => $cf_front_app_api_workdir,
282 user => $cf_user,
283 environment => ["HOME=${cf_home}"],
284 creates => $cf_front_app_api_bin,
285 command => "/usr/bin/make build",
286 require => Exec["go-cryptoportfolio-dependencies"],
287 }
288
289 file { "/etc/systemd/system/cryptoportfolio-app.service":
290 mode => "0644",
291 owner => "root",
292 group => "root",
293 content => template("role/cryptoportfolio/cryptoportfolio-app.service.erb"),
68bbff61 294 notify => Exec["systemctl daemon-reload"],
159df176
IB
295 }
296
297 service { 'cryptoportfolio-app':
f099c453
IB
298 enable => true,
299 ensure => "running",
300 subscribe => [Exec["go-cryptoportfolio-app"], Exec["web-cryptoportfolio-build"]],
301 require => [
b3015828
IB
302 File["/etc/systemd/system/cryptoportfolio-app.service"],
303 Postgresql::Server::Db[$cf_pg_db]
304 ],
43c4ee4e
IB
305 } ~>
306 exec { "dump $cf_pg_db structure":
307 refreshonly => true,
308 user => $::profile::postgresql::pg_user,
309 group => $::profile::postgresql::pg_user,
310 command => "/usr/bin/pg_dump --schema-only --clean --no-publications $cf_pg_db > /var/lib/postgres/${cf_pg_db}.schema",
159df176
IB
311 }
312
313 file { $cf_front_app_api_conf:
314 owner => $cf_user,
315 group => $cf_group,
316 mode => "0600",
317 content => template("role/cryptoportfolio/api_conf.toml.erb"),
318 }
319
320 file { $cf_front_app_static_conf:
321 owner => $cf_user,
322 group => $cf_group,
323 mode => "0600",
324 content => template("role/cryptoportfolio/static_conf.env.erb"),
e17078be 325 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
159df176
IB
326 }
327
328 exec { "web-cryptoportfolio-dependencies":
329 cwd => "${cf_front_app}/cmd/web",
330 environment => ["HOME=${cf_home}"],
331 command => "/usr/bin/make install",
332 creates => "${cf_front_app}/cmd/web/node_modules",
e17078be 333 notify => Exec["remove build ${cf_front_app}/cmd/web/build/"],
159df176
IB
334 require => [Package["npm"], Package["nodejs"], Package["yarn"]]
335 }
336
e17078be
IB
337 exec { "remove build ${cf_front_app}/cmd/web/build/":
338 command => "/usr/bin/rm -rf '${cf_front_app}/cmd/web/build/'",
339 refreshonly => true,
340 before => Exec["web-cryptoportfolio-build"]
341 }
342
159df176
IB
343 exec { "web-cryptoportfolio-build":
344 cwd => "${cf_front_app}/cmd/web",
345 environment => ["HOME=${cf_home}"],
6d512d3f 346 path => ["${cf_front_app}/cmd/web/node_modules/.bin/", "/usr/bin"],
159df176
IB
347 command => "/usr/bin/make static ENV=${cf_env}",
348 creates => "${cf_front_app}/cmd/web/build/static",
349 require => [File[$cf_front_app_static_conf], Exec["web-cryptoportfolio-dependencies"]]
76a321e1
IB
350 }
351 }
57ae81ea 352}