]>
Commit | Line | Data |
---|---|---|
1 | class 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_conf_hourly = "${home}/bot_config_hourly.ini" | |
7 | $cf_bot_app_reports = "${home}/bot_reports" | |
8 | ||
9 | ensure_packages(["python", "python-pip"]) | |
10 | ||
11 | file { $cf_bot_app: | |
12 | ensure => "directory", | |
13 | mode => "0700", | |
14 | owner => $user, | |
15 | group => $group, | |
16 | require => User["$user:"], | |
17 | } | |
18 | ||
19 | archive { "${home}/trader_${trader_version}.tar.gz": | |
20 | path => "${home}/trader_${trader_version}.tar.gz", | |
21 | source => "https://release.immae.eu/cryptoportfolio/trader/trader_${trader_version}.tar.gz", | |
22 | checksum_type => "sha256", | |
23 | checksum => $trader_sha256, | |
24 | cleanup => false, | |
25 | extract => true, | |
26 | user => $user, | |
27 | username => lookup("base_installation::ldap_cn"), | |
28 | password => generate_password(24, $password_seed, "ldap"), | |
29 | extract_path => $cf_bot_app, | |
30 | require => [User["$user:"], File[$cf_bot_app]], | |
31 | } ~> | |
32 | exec { "py-cryptoportfolio-dependencies": | |
33 | cwd => $cf_bot_app, | |
34 | user => $user, | |
35 | environment => ["HOME=${home}"], | |
36 | command => "/usr/bin/make install", | |
37 | require => User["$user:"], | |
38 | refreshonly => true, | |
39 | before => [ | |
40 | File[$cf_bot_app_conf], | |
41 | Cron::Job::Multiple["py-cryptoportfolio"], | |
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_${trader_version}.tar.gz"], | |
54 | ], | |
55 | } | |
56 | ||
57 | cron::job::multiple { "py-cryptoportfolio": | |
58 | ensure => present, | |
59 | environment => ["HOME=${home}","PATH=/usr/bin/","CRON_TZ=UTC"], | |
60 | require => [ | |
61 | File[$cf_bot_app_conf], | |
62 | Archive["${home}/trader_${trader_version}.tar.gz"] | |
63 | ], | |
64 | jobs => [ | |
65 | { | |
66 | command => "cd $cf_bot_app ; python main.py --quiet --config $cf_bot_app_conf --before", | |
67 | user => $user, | |
68 | weekday => 7, # Sunday | |
69 | hour => 22, | |
70 | minute => 30, | |
71 | description => "Run before the cryptoportfolio update", | |
72 | }, | |
73 | { | |
74 | command => "cd $cf_bot_app ; python main.py --quiet --config $cf_bot_app_conf --after", | |
75 | user => $user, | |
76 | weekday => 1, # Monday | |
77 | hour => 0, | |
78 | minute => 0, | |
79 | description => "Run after the cryptoportfolio update", | |
80 | } | |
81 | ], | |
82 | } | |
83 | ||
84 | if ($environment == "production") { | |
85 | $monitored_key = 3 | |
86 | @profile::monitoring::local_service { "Last redis report is less than 2 hours old": | |
87 | local => { | |
88 | check_command => "check_date!redis-cli -s $redis_host GET /cryptoportfolio/$monitored_key/latest/date!2" | |
89 | } | |
90 | } | |
91 | ||
92 | @profile::monitoring::local_service { "Last bot report is less than one week old": | |
93 | sudos => { | |
94 | "naemon-cryptoportfolio-bot" => "naemon ALL=($user) NOPASSWD: /usr/bin/find $cf_bot_app_reports -mindepth 1 -maxdepth 1 -printf %T@?n", | |
95 | }, | |
96 | local => { | |
97 | check_command => "check_last_file_date!$cf_bot_app_reports!168!$user", | |
98 | }, | |
99 | } | |
100 | } | |
101 | ||
102 | if versioncmp($trader_version, "v1.3") >= 0 { | |
103 | file { $cf_bot_app_conf_hourly: | |
104 | owner => $user, | |
105 | group => $group, | |
106 | mode => "0600", | |
107 | content => template("role/cryptoportfolio/bot_config_hourly.ini.erb"), | |
108 | require => [ | |
109 | User["$user:"], | |
110 | Archive["${home}/trader_${trader_version}.tar.gz"], | |
111 | ], | |
112 | } | |
113 | ||
114 | cron::job::multiple { "py-cryptoportfolio-hourly": | |
115 | ensure => present, | |
116 | environment => ["HOME=${home}","PATH=/usr/bin/"], | |
117 | require => [ | |
118 | File[$cf_bot_app_conf_hourly], | |
119 | Archive["${home}/trader_${trader_version}.tar.gz"] | |
120 | ], | |
121 | jobs => [ | |
122 | { | |
123 | command => "cd $cf_bot_app ; python main.py --config $cf_bot_app_conf_hourly", | |
124 | user => $user, | |
125 | minute => 30, | |
126 | description => "Print the current state to redis", | |
127 | }, | |
128 | ], | |
129 | } | |
130 | } | |
131 | ||
132 | unless empty($webhook_url) { | |
133 | exec { "bot-slack-notify": | |
134 | refreshonly => true, | |
135 | environment => [ | |
136 | "P_PROJECT=Trader", | |
137 | "P_WEBHOOK=${webhook_url}", | |
138 | "P_VERSION=${trader_version}", | |
139 | "P_HOST=${web_host}", | |
140 | "P_HTTPS=${web_ssl}", | |
141 | ], | |
142 | command => "/usr/local/bin/slack-notify", | |
143 | require => File["/usr/local/bin/slack-notify"], | |
144 | subscribe => Exec["py-cryptoportfolio-dependencies"], | |
145 | } | |
146 | } | |
147 | } | |
148 |