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