ConfigLMM 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -0
  3. data/CNAME +1 -0
  4. data/Examples/.lmm.state.yaml +159 -0
  5. data/Examples/ConfigLMM.mm.yaml +32 -0
  6. data/Examples/Implemented.mm.yaml +252 -4
  7. data/Examples/SmallBusiness.mm.yaml +492 -0
  8. data/Plugins/Apps/Answer/answer.lmm.rb +165 -0
  9. data/Plugins/Apps/Answer/answer@.service +40 -0
  10. data/Plugins/Apps/ArchiSteamFarm/ArchiSteamFarm.conf.erb +0 -3
  11. data/Plugins/Apps/ArchiSteamFarm/ArchiSteamFarm.lmm.rb +0 -1
  12. data/Plugins/Apps/Authentik/Authentik-ProxyOutpost.container +7 -1
  13. data/Plugins/Apps/Authentik/Authentik-Server.container +6 -1
  14. data/Plugins/Apps/Authentik/Authentik-Worker.container +6 -1
  15. data/Plugins/Apps/Authentik/Authentik.conf.erb +12 -7
  16. data/Plugins/Apps/Authentik/Authentik.lmm.rb +226 -61
  17. data/Plugins/Apps/BookStack/BookStack.conf.erb +0 -3
  18. data/Plugins/Apps/BookStack/BookStack.container +5 -0
  19. data/Plugins/Apps/BookStack/BookStack.lmm.rb +14 -3
  20. data/Plugins/Apps/Cassandra/Cassandra.lmm.rb +9 -19
  21. data/Plugins/Apps/ClickHouse/ClickHouse.container +28 -0
  22. data/Plugins/Apps/ClickHouse/ClickHouse.lmm.rb +113 -0
  23. data/Plugins/Apps/ClickHouse/Config/listen.yaml +2 -0
  24. data/Plugins/Apps/ClickHouse/Config/logger.yaml +8 -0
  25. data/Plugins/Apps/ClickHouse/Config/zookeepers.yaml +5 -0
  26. data/Plugins/Apps/ClickHouse/Connection.rb +96 -0
  27. data/Plugins/Apps/Discourse/Discourse-Sidekiq.container +5 -0
  28. data/Plugins/Apps/Discourse/Discourse.conf.erb +1 -4
  29. data/Plugins/Apps/Discourse/Discourse.container +4 -0
  30. data/Plugins/Apps/Discourse/Discourse.lmm.rb +116 -55
  31. data/Plugins/Apps/Dovecot/Dovecot.lmm.rb +74 -62
  32. data/Plugins/Apps/ERPNext/ERPNext-Frontend.container +6 -1
  33. data/Plugins/Apps/ERPNext/ERPNext-Queue.container +5 -0
  34. data/Plugins/Apps/ERPNext/ERPNext-Scheduler.container +5 -0
  35. data/Plugins/Apps/ERPNext/ERPNext-Websocket.container +6 -1
  36. data/Plugins/Apps/ERPNext/ERPNext.container +6 -1
  37. data/Plugins/Apps/ERPNext/ERPNext.lmm.rb +138 -127
  38. data/Plugins/Apps/GitLab/GitLab.container +6 -0
  39. data/Plugins/Apps/GitLab/GitLab.lmm.rb +43 -49
  40. data/Plugins/Apps/Homepage/Homepage.conf.erb +86 -0
  41. data/Plugins/Apps/Homepage/Homepage.container +19 -0
  42. data/Plugins/Apps/Homepage/Homepage.lmm.rb +54 -0
  43. data/Plugins/Apps/IPFS/IPFS.conf.erb +0 -3
  44. data/Plugins/Apps/IPFS/IPFS.lmm.rb +0 -1
  45. data/Plugins/Apps/InfluxDB/InfluxDB.conf.erb +0 -3
  46. data/Plugins/Apps/InfluxDB/InfluxDB.lmm.rb +0 -1
  47. data/Plugins/Apps/Jackett/Jackett.conf.erb +0 -3
  48. data/Plugins/Apps/Jackett/Jackett.lmm.rb +0 -1
  49. data/Plugins/Apps/Jellyfin/Jellyfin.conf.erb +0 -3
  50. data/Plugins/Apps/Jellyfin/Jellyfin.lmm.rb +0 -1
  51. data/Plugins/Apps/LetsEncrypt/LetsEncrypt.lmm.rb +49 -28
  52. data/Plugins/Apps/LibreTranslate/LibreTranslate.container +21 -0
  53. data/Plugins/Apps/LibreTranslate/LibreTranslate.lmm.rb +34 -0
  54. data/Plugins/Apps/Lobsters/Containerfile +81 -0
  55. data/Plugins/Apps/Lobsters/Lobsters-Tasks.container +26 -0
  56. data/Plugins/Apps/Lobsters/Lobsters.conf.erb +99 -0
  57. data/Plugins/Apps/Lobsters/Lobsters.container +27 -0
  58. data/Plugins/Apps/Lobsters/Lobsters.lmm.rb +196 -0
  59. data/Plugins/Apps/Lobsters/crontab +3 -0
  60. data/Plugins/Apps/Lobsters/database.yml +26 -0
  61. data/Plugins/Apps/Lobsters/entrypoint.sh +30 -0
  62. data/Plugins/Apps/Lobsters/generateCredentials.rb +19 -0
  63. data/Plugins/Apps/Lobsters/lobsters-cron.sh +25 -0
  64. data/Plugins/Apps/Lobsters/lobsters-daily.sh +23 -0
  65. data/Plugins/Apps/Lobsters/puma.rb +49 -0
  66. data/Plugins/Apps/MariaDB/Connection.rb +55 -0
  67. data/Plugins/Apps/MariaDB/MariaDB.lmm.rb +60 -53
  68. data/Plugins/Apps/Mastodon/Mastodon-Sidekiq.container +22 -0
  69. data/Plugins/Apps/Mastodon/Mastodon-Streaming.container +20 -0
  70. data/Plugins/Apps/Mastodon/Mastodon.conf.erb +34 -45
  71. data/Plugins/Apps/Mastodon/Mastodon.container +28 -0
  72. data/Plugins/Apps/Mastodon/Mastodon.lmm.rb +240 -5
  73. data/Plugins/Apps/Mastodon/configlmm.rake +30 -0
  74. data/Plugins/Apps/Mastodon/entrypoint.sh +16 -0
  75. data/Plugins/Apps/Matrix/Element.container +5 -0
  76. data/Plugins/Apps/Matrix/Matrix.conf.erb +2 -8
  77. data/Plugins/Apps/Matrix/Matrix.lmm.rb +100 -71
  78. data/Plugins/Apps/Matrix/Synapse.container +5 -0
  79. data/Plugins/Apps/Netdata/Netdata.conf.erb +0 -3
  80. data/Plugins/Apps/Netdata/Netdata.lmm.rb +0 -1
  81. data/Plugins/Apps/Nextcloud/Nextcloud.conf.erb +3 -4
  82. data/Plugins/Apps/Nextcloud/Nextcloud.lmm.rb +150 -68
  83. data/Plugins/Apps/Nextcloud/autoconfig.php +13 -0
  84. data/Plugins/Apps/Nextcloud/config.php +10 -1
  85. data/Plugins/Apps/Nextcloud/nextcloudcron.service +8 -0
  86. data/Plugins/Apps/Nextcloud/nextcloudcron.timer +10 -0
  87. data/Plugins/Apps/Nginx/Connection.rb +93 -0
  88. data/Plugins/Apps/Nginx/conf.d/configlmm.conf +50 -9
  89. data/Plugins/Apps/Nginx/conf.d/languages.conf +21 -0
  90. data/Plugins/Apps/Nginx/config-lmm/errors.conf +25 -20
  91. data/Plugins/Apps/Nginx/config-lmm/gateway-errors.conf +20 -0
  92. data/Plugins/Apps/Nginx/config-lmm/proxy.conf +1 -1
  93. data/Plugins/Apps/Nginx/main.conf.erb +7 -3
  94. data/Plugins/Apps/Nginx/nginx.conf +2 -2
  95. data/Plugins/Apps/Nginx/nginx.lmm.rb +99 -81
  96. data/Plugins/Apps/Nginx/proxy.conf.erb +11 -3
  97. data/Plugins/Apps/Odoo/Odoo.conf.erb +0 -3
  98. data/Plugins/Apps/Odoo/Odoo.container +5 -0
  99. data/Plugins/Apps/Odoo/Odoo.lmm.rb +4 -5
  100. data/Plugins/Apps/Ollama/Ollama.container +26 -0
  101. data/Plugins/Apps/Ollama/Ollama.lmm.rb +73 -0
  102. data/Plugins/Apps/OpenTelemetry/Config/config.yaml +704 -0
  103. data/Plugins/Apps/OpenTelemetry/OpenTelemetry.lmm.rb +154 -0
  104. data/Plugins/Apps/OpenVidu/Ingress.container +5 -0
  105. data/Plugins/Apps/OpenVidu/OpenVidu.conf.erb +0 -3
  106. data/Plugins/Apps/OpenVidu/OpenVidu.container +5 -0
  107. data/Plugins/Apps/OpenVidu/OpenVidu.lmm.rb +7 -3
  108. data/Plugins/Apps/OpenVidu/OpenViduCall.conf.erb +0 -3
  109. data/Plugins/Apps/OpenVidu/OpenViduCall.container +5 -0
  110. data/Plugins/Apps/PHP-FPM/Connection.rb +91 -0
  111. data/Plugins/Apps/PHP-FPM/PHP-FPM.lmm.rb +31 -4
  112. data/Plugins/Apps/Peppermint/Peppermint.conf.erb +2 -5
  113. data/Plugins/Apps/Peppermint/Peppermint.container +5 -0
  114. data/Plugins/Apps/Peppermint/Peppermint.lmm.rb +29 -33
  115. data/Plugins/Apps/Perplexica/Perplexica.container +25 -0
  116. data/Plugins/Apps/Perplexica/Perplexica.lmm.rb +92 -0
  117. data/Plugins/Apps/Perplexica/config.toml +26 -0
  118. data/Plugins/Apps/Podman/Connection.rb +24 -0
  119. data/Plugins/Apps/Podman/Podman.lmm.rb +80 -0
  120. data/Plugins/Apps/Podman/storage.conf +6 -0
  121. data/Plugins/Apps/Postfix/Postfix.lmm.rb +242 -164
  122. data/Plugins/Apps/PostgreSQL/Connection.rb +97 -0
  123. data/Plugins/Apps/PostgreSQL/PostgreSQL.lmm.rb +184 -148
  124. data/Plugins/Apps/Pterodactyl/Pterodactyl.conf.erb +0 -3
  125. data/Plugins/Apps/Pterodactyl/Pterodactyl.lmm.rb +0 -2
  126. data/Plugins/Apps/Pterodactyl/Wings.conf.erb +0 -3
  127. data/Plugins/Apps/RVM/RVM.lmm.rb +57 -0
  128. data/Plugins/Apps/Roundcube/Roundcube.conf.erb +0 -3
  129. data/Plugins/Apps/Roundcube/Roundcube.lmm.rb +15 -19
  130. data/Plugins/Apps/SSH/SSH.lmm.rb +9 -15
  131. data/Plugins/Apps/SearXNG/SearXNG.container +22 -0
  132. data/Plugins/Apps/SearXNG/SearXNG.lmm.rb +79 -0
  133. data/Plugins/Apps/SearXNG/limiter.toml +40 -0
  134. data/Plugins/Apps/SearXNG/settings.yml +2 -0
  135. data/Plugins/Apps/SigNoz/Config/alerts.yml +11 -0
  136. data/Plugins/Apps/SigNoz/Config/otel-collector-config.yaml +110 -0
  137. data/Plugins/Apps/SigNoz/Config/otel-collector-opamp-config.yaml +1 -0
  138. data/Plugins/Apps/SigNoz/Config/prometheus.yml +18 -0
  139. data/Plugins/Apps/SigNoz/SigNoz-Collector.container +23 -0
  140. data/Plugins/Apps/SigNoz/SigNoz-Migrator.container +17 -0
  141. data/Plugins/Apps/SigNoz/SigNoz.conf.erb +61 -0
  142. data/Plugins/Apps/SigNoz/SigNoz.container +26 -0
  143. data/Plugins/Apps/SigNoz/SigNoz.lmm.rb +319 -0
  144. data/Plugins/Apps/Solr/log4j2.xml +89 -0
  145. data/Plugins/Apps/Solr/solr.lmm.rb +82 -0
  146. data/Plugins/Apps/Sunshine/Sunshine.conf.erb +0 -3
  147. data/Plugins/Apps/Sunshine/Sunshine.lmm.rb +0 -1
  148. data/Plugins/Apps/Tunnel/tunnel.lmm.rb +33 -37
  149. data/Plugins/Apps/UVdesk/UVdesk.conf.erb +0 -3
  150. data/Plugins/Apps/Umami/Umami.container +19 -0
  151. data/Plugins/Apps/Umami/Umami.lmm.rb +108 -0
  152. data/Plugins/Apps/Valkey/Valkey.lmm.rb +54 -42
  153. data/Plugins/Apps/Vaultwarden/Vaultwarden.conf.erb +9 -6
  154. data/Plugins/Apps/Vaultwarden/Vaultwarden.container +7 -1
  155. data/Plugins/Apps/Vaultwarden/Vaultwarden.lmm.rb +64 -29
  156. data/Plugins/Apps/Wiki.js/Wiki.js.conf.erb +1 -4
  157. data/Plugins/Apps/Wiki.js/Wiki.js.container +5 -0
  158. data/Plugins/Apps/Wiki.js/Wiki.js.lmm.rb +31 -37
  159. data/Plugins/Apps/YaCy/YaCy.conf.erb +93 -0
  160. data/Plugins/Apps/YaCy/YaCy.container +21 -0
  161. data/Plugins/Apps/YaCy/YaCy.lmm.rb +160 -0
  162. data/Plugins/Apps/ZooKeeper/ZooKeeper.container +24 -0
  163. data/Plugins/Apps/ZooKeeper/ZooKeeper.lmm.rb +68 -0
  164. data/Plugins/Apps/bitmagnet/bitmagnet.conf.erb +0 -3
  165. data/Plugins/Apps/bitmagnet/bitmagnet.lmm.rb +0 -1
  166. data/Plugins/Apps/gollum/gollum.conf.erb +2 -4
  167. data/Plugins/Apps/gollum/gollum.container +6 -0
  168. data/Plugins/Apps/gollum/gollum.lmm.rb +51 -50
  169. data/Plugins/Apps/llama.cpp/llama.cpp.container +28 -0
  170. data/Plugins/Apps/llama.cpp/llama.cpp.lmm.rb +90 -0
  171. data/Plugins/Apps/vLLM/vLLM.container +32 -0
  172. data/Plugins/Apps/vLLM/vLLM.lmm.rb +89 -0
  173. data/Plugins/OS/General/Utils.lmm.rb +26 -0
  174. data/Plugins/OS/Linux/Connection.rb +472 -0
  175. data/Plugins/OS/Linux/Debian/preseed.cfg.erb +25 -6
  176. data/Plugins/OS/Linux/Flavours.yaml +13 -0
  177. data/Plugins/OS/Linux/Grub/grub.cfg +10 -0
  178. data/Plugins/OS/Linux/HTTP.rb +32 -0
  179. data/Plugins/OS/Linux/Linux.lmm.rb +533 -187
  180. data/Plugins/OS/Linux/Packages.yaml +20 -1
  181. data/Plugins/OS/Linux/Services.yaml +8 -0
  182. data/Plugins/OS/Linux/Shell.rb +70 -0
  183. data/Plugins/OS/Linux/Syslinux/default +8 -0
  184. data/Plugins/OS/Linux/WireGuard/WireGuard.lmm.rb +83 -59
  185. data/Plugins/OS/Linux/WireGuard/wg0.conf.erb +3 -0
  186. data/Plugins/OS/Linux/openSUSE/autoinst.xml.erb +29 -3
  187. data/Plugins/OS/Linux/systemd/systemd.lmm.rb +13 -11
  188. data/Plugins/OS/Routers/Aruba/ArubaInstant.lmm.rb +6 -5
  189. data/Plugins/Platforms/GitHub.lmm.rb +73 -28
  190. data/Plugins/Platforms/GoDaddy/GoDaddy.lmm.rb +9 -6
  191. data/Plugins/Platforms/Proxmox/Proxmox.lmm.rb +402 -0
  192. data/Plugins/Platforms/Proxmox/XTerm.rb +321 -0
  193. data/Plugins/Platforms/libvirt/libvirt.lmm.rb +38 -13
  194. data/Plugins/Platforms/porkbun.lmm.rb +12 -2
  195. data/Plugins/Platforms/porkbun_spec.rb +2 -2
  196. data/Plugins/Services/DNS/AmberBit.lmm.rb +1 -1
  197. data/Plugins/Services/DNS/ArubaItDNS.lmm.rb +1 -1
  198. data/Plugins/Services/DNS/NICLV.lmm.rb +1 -1
  199. data/Plugins/Services/DNS/PowerDNS.lmm.rb +70 -68
  200. data/Plugins/Services/DNS/tonic.lmm.rb +22 -12
  201. data/lib/ConfigLMM/Framework/plugins/dns.rb +4 -3
  202. data/lib/ConfigLMM/Framework/plugins/linuxApp.rb +145 -184
  203. data/lib/ConfigLMM/Framework/plugins/nginxApp.rb +34 -17
  204. data/lib/ConfigLMM/Framework/plugins/plugin.rb +53 -181
  205. data/lib/ConfigLMM/Framework/plugins/store.rb +4 -4
  206. data/lib/ConfigLMM/Framework/variables.rb +75 -0
  207. data/lib/ConfigLMM/Framework.rb +1 -0
  208. data/lib/ConfigLMM/cli.rb +12 -6
  209. data/lib/ConfigLMM/commands/configsCommand.rb +37 -6
  210. data/lib/ConfigLMM/commands/diff.rb +33 -9
  211. data/lib/ConfigLMM/context.rb +22 -3
  212. data/lib/ConfigLMM/io/configList.rb +82 -6
  213. data/lib/ConfigLMM/io/connection.rb +143 -0
  214. data/lib/ConfigLMM/io/dhcp.rb +330 -0
  215. data/lib/ConfigLMM/io/http.rb +78 -0
  216. data/lib/ConfigLMM/io/local.rb +207 -0
  217. data/lib/ConfigLMM/io/pxe.rb +92 -0
  218. data/lib/ConfigLMM/io/ssh.rb +156 -0
  219. data/lib/ConfigLMM/io/tftp.rb +105 -0
  220. data/lib/ConfigLMM/io.rb +2 -0
  221. data/lib/ConfigLMM/secrets/envStore.rb +39 -0
  222. data/lib/ConfigLMM/secrets/fileStore.rb +43 -0
  223. data/lib/ConfigLMM/state.rb +2 -1
  224. data/lib/ConfigLMM/version.rb +2 -1
  225. data/lib/ConfigLMM.rb +1 -0
  226. data/{Examples → scripts}/configlmmAuth.sh +7 -5
  227. metadata +205 -8
@@ -1,4 +1,6 @@
1
1
 
2
+ require 'addressable/idna'
3
+
2
4
  module ConfigLMM
3
5
  module LMM
4
6
  class Postfix < Framework::Plugin
@@ -7,203 +9,279 @@ module ConfigLMM
7
9
  MASTER_FILE = 'master.cf'
8
10
  MAIN_FILE = 'main.cf'
9
11
  TRANSPORT_FILE = 'transport'
12
+ PASSWORD_FILE = 'sasl_passwd'
10
13
 
11
14
  def actionPostfixDeploy(id, target, activeState, context, options)
12
- plugins[:Linux].ensurePackages([PACKAGE_NAME, 'CyrusSASL'], target['Location'])
13
- plugins[:Linux].ensureServiceAutoStart(SERVICE_NAME, target['Location'])
14
-
15
- activeState['Instance'] = target['Instance']
16
- activeState['AlternativePort'] = target['AlternativePort']
17
- deploySettings(target, target['Location'], options)
15
+ self.withConnection(target['Location'], target) do |connection|
16
+ Linux.withConnection(connection) do |linuxConnection|
17
+ linuxConnection.ensurePackages([PACKAGE_NAME, 'CyrusSASL'], options)
18
+ linuxConnection.ensureServiceAutoStart(SERVICE_NAME, options)
18
19
 
19
- plugins[:Linux].startService(SERVICE_NAME, target['Location'])
20
+ deploySettings(target, linuxConnection, context, options)
21
+ deployAccounts(target, linuxConnection, context, options)
20
22
 
21
- activeState['Status'] = State::STATUS_DEPLOYED
23
+ linuxConnection.restartService(SERVICE_NAME, options)
24
+ end
25
+ end
22
26
  end
23
27
 
24
- def deploySettings(target, location, options)
28
+ def deploySettings(target, linuxConnection, context, options)
25
29
  postfixDirName = 'postfix'
26
30
  postfixDirName = 'postfix-' + target['Instance'] if target['Instance']
27
31
  postfixDir = '/etc/' + postfixDirName + '/'
28
- if location && location != '@me'
29
- self.class.sshStart(location) do |ssh|
30
- if target['Instance']
31
- self.class.sshExec!(ssh, "postmulti -e init")
32
- self.class.sshExec!(ssh, "postmulti -I #{postfixDirName} -e create", true)
33
- self.class.sshExec!(ssh, "sed -i 's|^master_service_disable|#master_service_disable|' #{postfixDir + MAIN_FILE}")
34
- end
35
- self.class.sshExec!(ssh, "sed -i 's|^tlsmgr|#tlsmgr|' #{postfixDir + MASTER_FILE}")
36
- if target.key?('SMTP')
37
- if !target['SMTP'] || target['SMTP'] == 'unix'
38
- self.class.sshExec!(ssh, "sed -i 's|^smtp|#smtp|' #{postfixDir + MASTER_FILE}")
39
- end
40
- end
41
- updateRemoteFile(ssh, postfixDir + MASTER_FILE, options, true) do |fileLines|
42
- if target['AlternativePort']
43
- fileLines << "#{target['AlternativePort']} inet n - n - - smtpd\n"
44
- fileLines << "tlsmgr unix - - n 1000? 1 tlsmgr\n"
45
- else
46
- if !target.key?('Submission') || (target.key?('Submission') && target['Submission'])
47
- fileLines << "submissions inet n - n - - smtpd\n"
48
- fileLines << " -o syslog_name=postfix/submissions\n"
49
- fileLines << " -o smtpd_tls_wrappermode=yes\n"
50
- fileLines << " -o smtpd_tls_security_level=encrypt\n"
51
- fileLines << " -o smtpd_sasl_auth_enable=yes\n"
52
- fileLines << " -o cleanup_service_name=header_cleanup\n"
53
- fileLines << "header_cleanup unix n - - - 0 cleanup\n"
54
- fileLines << " -o header_checks=regexp:/etc/postfix/header_cleanup\n"
55
-
56
- self.class.sshExec!(ssh, "echo '/^Received:/ IGNORE' > /etc/postfix/header_cleanup")
57
- self.class.sshExec!(ssh, "echo '/^User-Agent:/ IGNORE' >> /etc/postfix/header_cleanup")
58
- end
59
- fileLines << "tlsmgr unix - - n 1000? 1 tlsmgr\n"
60
- if target['SMTP'] == 'unix'
61
- fileLines << "smtp unix - - n - - smtp\n"
62
- end
63
- end
64
- fileLines
65
- end
66
32
 
67
- domain = target['Domain']
68
- domain = self.class.sshExec!(ssh, "hostname --fqdn").strip unless domain
69
- command = "sed -i 's|^myhostname = .*|myhostname = #{domain}|' #{postfixDir + MAIN_FILE}"
70
- self.class.sshExec!(ssh, command)
71
- command = "sed -i 's|^#myhostname = virtual.domain.tld|myhostname = #{domain}|' #{postfixDir + MAIN_FILE}"
72
- self.class.sshExec!(ssh, command)
73
-
74
- # Fix config bug
75
- command = "sed -i 's|^alias_maps = :/etc/aliases|alias_maps = lmdb:/etc/aliases|' #{postfixDir + MAIN_FILE}"
76
- self.class.sshExec!(ssh, command)
77
- command = "sed -i 's|^canonical_maps = :/etc/postfix/canonical|canonical_maps = lmdb:/etc/postfix/canonical|' #{postfixDir + MAIN_FILE}"
78
- self.class.sshExec!(ssh, command)
79
- command = "sed -i 's|^relocated_maps = :/etc/postfix/relocated|relocated_maps = lmdb:/etc/postfix/relocated|' #{postfixDir + MAIN_FILE}"
80
- self.class.sshExec!(ssh, command)
81
- command = "sed -i 's|^sender_canonical_maps = :/etc/postfix/sender_canonical|sender_canonical_maps = lmdb:/etc/postfix/sender_canonical|' #{postfixDir + MAIN_FILE}"
82
- self.class.sshExec!(ssh, command)
83
- command = "sed -i 's|^transport_maps = :/etc/postfix/transport|transport_maps = lmdb:/etc/postfix/transport|' #{postfixDir + MAIN_FILE}"
84
- self.class.sshExec!(ssh, command)
85
- command = "sed -i 's|^smtpd_sender_restrictions = :/etc/postfix/access|smtpd_sender_restrictions = lmdb:/etc/postfix/access|' #{postfixDir + MAIN_FILE}"
86
- self.class.sshExec!(ssh, command)
87
- command = "sed -i 's|^virtual_alias_maps = :/etc/postfix/virtual|virtual_alias_maps = lmdb:/etc/postfix/virtual|' #{postfixDir + MAIN_FILE}"
88
- self.class.sshExec!(ssh, command)
89
- command = "sed -i 's|^relay_domains = $mydestination :/etc/postfix/relay|relay_domains = $mydestination lmdb:/etc/postfix/relay|' #{postfixDir + MAIN_FILE}"
90
- self.class.sshExec!(ssh, command)
91
- command = "sed -i 's|^relay_recipient_maps = :/etc/postfix/relay_recipients|relay_recipient_maps = lmdb:/etc/postfix/relay_recipients|' #{postfixDir + MAIN_FILE}"
92
- self.class.sshExec!(ssh, command)
93
- command = "sed -i 's|^virtual_mailbox_maps =.*|virtual_mailbox_maps = lmdb:/etc/postfix/mailboxes|' #{postfixDir + MAIN_FILE}"
94
- self.class.sshExec!(ssh, command)
95
-
96
-
97
- if target['AlternativePort']
98
- Framework::LinuxApp.firewallAddPort("#{target['AlternativePort']}/tcp", ssh)
99
- else
100
- Framework::LinuxApp.firewallAddService('smtp', ssh)
101
- end
102
- Framework::LinuxApp.firewallAddService('smtps', ssh)
103
-
104
- ssh.scp.upload!(__dir__ + '/smtpd.conf', '/etc/sasl2/smtpd.conf')
105
- self.class.sshExec!(ssh, "touch /etc/sasldb2")
106
- self.class.sshExec!(ssh, "chown postfix:postfix /etc/sasldb2")
107
- self.class.sshExec!(ssh, "touch #{postfixDir}access")
108
- self.class.sshExec!(ssh, "postmap #{postfixDir}access")
109
- self.class.sshExec!(ssh, "touch #{postfixDir}sender_login")
110
- self.class.sshExec!(ssh, "postmap #{postfixDir}sender_login")
111
-
112
- certDir = Framework::LinuxApp.createCertificateOverSSH(ssh)
113
- target['Settings'] ||= []
114
- target['Settings']['smtpd_sender_login_maps'] = "lmdb:#{postfixDir}sender_login" unless target['Settings']['smtpd_sender_login_maps']
115
- target['Settings']['smtpd_sender_restrictions'] = "reject_sender_login_mismatch, lmdb:#{postfixDir}access" unless target['Settings']['smtpd_sender_restrictions']
116
- target['Settings']['smtp_tls_security_level'] = 'may' unless target['Settings']['smtp_tls_security_level']
117
- target['Settings']['smtpd_tls_mandatory_protocols'] = '>=TLSv1.2' unless target['Settings']['smtpd_tls_mandatory_protocols']
118
- target['Settings']['smtpd_tls_auth_only'] = 'yes' unless target['Settings']['smtpd_tls_auth_only']
119
- target['Settings']['smtpd_tls_security_level'] = 'may' unless target['Settings']['smtpd_tls_security_level']
120
- target['Settings']['smtpd_tls_cert_file'] = certDir + 'fullchain.pem' unless target['Settings']['smtpd_tls_cert_file']
121
- target['Settings']['smtpd_tls_key_file'] = certDir + 'privkey.pem' unless target['Settings']['smtpd_tls_key_file']
122
- target['Settings']['tls_preempt_cipherlist'] = 'yes' unless target['Settings']['tls_preempt_cipherlist']
123
- target['Settings']['tls_ssl_options'] = 'NO_RENEGOTIATION' unless target['Settings']['tls_ssl_options']
124
-
125
-
126
- target['Settings'].each do |name, value|
127
- command = "sed -i 's|^#{name} =.*|##{name} = #{value}|' #{postfixDir + MAIN_FILE}"
128
- self.class.sshExec!(ssh, command)
129
- end
130
- updateRemoteFile(ssh, postfixDir + MAIN_FILE, options) do |fileLines|
131
- target['Settings'].each do |name, value|
132
- value = 'yes' if value == true
133
- value = 'no' if value == false
134
- fileLines << "#{name} = #{value}\n"
135
- end
136
- fileLines
137
- end
33
+ if target['Instance']
34
+ linuxConnection.exec("postmulti -e init", false, options)
35
+ linuxConnection.exec("postmulti -I #{postfixDirName} -e create", true, options)
36
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^master_service_disable', '#master_service_disable', options)
37
+ end
138
38
 
139
- if target['ForwardDovecot']
140
- command = "sed -i 's|^#virtual_transport =.*|virtual_transport = lmtp:unix:/run/dovecot/lmtp|' #{postfixDir + MAIN_FILE}"
141
- self.class.sshExec!(ssh, command)
142
- end
39
+ linuxConnection.fileReplace("#{postfixDir + MASTER_FILE}", '^tlsmgr', '#tlsmgr', options)
40
+ if target.key?('SMTP')
41
+ if !target['SMTP'] || target['SMTP'] == 'unix'
42
+ linuxConnection.fileReplace("#{postfixDir + MASTER_FILE}", '^smtp', '#smtp', options)
43
+ end
44
+ end
143
45
 
144
- if target['ForwardAll']
145
- command = "sed -i 's|^transport_maps =.*|transport_maps = lmdb:#{postfixDir}transport|' #{postfixDir + MAIN_FILE}"
146
- self.class.sshExec!(ssh, command)
147
- updateRemoteFile(ssh, postfixDir + TRANSPORT_FILE, options, true) do |fileLines|
148
- hostname, port = target['ForwardAll'].split(':')
149
- hostname = '[' + hostname + ']'
150
- line = '* smtp:' + hostname
151
- line += ':' + port if port
152
- fileLines << line + "\n"
153
- end
154
- self.class.sshExec!(ssh, "postmap #{postfixDir + TRANSPORT_FILE}")
46
+ linuxConnection.updateFile(postfixDir + MASTER_FILE, options, true) do |fileLines|
47
+ if target['AlternativePort']
48
+ fileLines << "#{target['AlternativePort']} inet n - n - - smtpd\n"
49
+ fileLines << "tlsmgr unix - - n 1000? 1 tlsmgr\n"
50
+ else
51
+ fileLines << "tlsmgr unix - - n 1000? 1 tlsmgr\n"
52
+ if target['SMTP'] == 'unix'
53
+ fileLines << "smtp unix - - n - - smtp\n"
155
54
  end
55
+ end
56
+ if target['Submission']
57
+ fileLines << "submissions inet n - n - - smtpd\n"
58
+ fileLines << " -o syslog_name=postfix/submissions\n"
59
+ fileLines << " -o smtpd_tls_wrappermode=yes\n"
60
+ fileLines << " -o smtpd_tls_security_level=encrypt\n"
61
+ fileLines << " -o smtpd_sasl_auth_enable=yes\n"
62
+ fileLines << " -o smtpd_client_restrictions=permit_sasl_authenticated,reject\n"
63
+ fileLines << " -o smtpd_sender_restrictions=reject_sender_login_mismatch,lmdb:#{postfixDir}access\n"
64
+ fileLines << " -o cleanup_service_name=header_cleanup\n"
65
+ fileLines << "header_cleanup unix n - - - 0 cleanup\n"
66
+ fileLines << " -o header_checks=regexp:/etc/postfix/header_cleanup\n"
156
67
 
157
- if target['Instance']
158
- self.class.sshExec!(ssh, "postmulti -i #{postfixDirName} -e enable")
159
- self.class.sshExec!(ssh, "postmulti -i #{postfixDirName} -p start", true)
160
- end
68
+ linuxConnection.fileWrite("/etc/postfix/header_cleanup", '/^Received:/ IGNORE', options)
69
+ linuxConnection.fileAppend("/etc/postfix/header_cleanup", '/^User-Agent:/ IGNORE', options)
161
70
  end
71
+ fileLines
72
+ end
73
+
74
+ domain = target['Domain']
75
+ domain = linuxConnection.exec("hostname --fqdn", false, { **options, 'dry' => false }).strip unless domain
76
+
77
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^myhostname = .*', "myhostname = #{domain}", options)
78
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^#myhostname = virtual.domain.tld', "myhostname = #{domain}", options)
79
+
80
+ # Fix config bug
81
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^alias_maps = :/etc/aliases', 'alias_maps = lmdb:/etc/aliases', options)
82
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^canonical_maps = :/etc/postfix/canonical', 'canonical_maps = lmdb:/etc/postfix/canonical', options)
83
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^relocated_maps = :/etc/postfix/relocated', 'relocated_maps = lmdb:/etc/postfix/relocated', options)
84
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^sender_canonical_maps = :/etc/postfix/sender_canonical', 'sender_canonical_maps = lmdb:/etc/postfix/sender_canonical', options)
85
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^transport_maps = :/etc/postfix/transport', 'transport_maps = lmdb:/etc/postfix/transport', options)
86
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^smtpd_sender_restrictions = :/etc/postfix/access', 'smtpd_sender_restrictions = lmdb:/etc/postfix/access', options)
87
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^virtual_alias_maps = :/etc/postfix/virtual', 'virtual_alias_maps = lmdb:/etc/postfix/virtual', options)
88
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^relay_domains = $mydestination :/etc/postfix/relay', 'relay_domains = $mydestination lmdb:/etc/postfix/relay', options)
89
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^relay_recipient_maps = :/etc/postfix/relay_recipients', 'relay_recipient_maps = lmdb:/etc/postfix/relay_recipients', options)
90
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^virtual_mailbox_maps =.*', 'virtual_mailbox_maps = lmdb:/etc/postfix/mailboxes', options)
91
+
92
+ if target['AlternativePort']
93
+ linuxConnection.firewallAddPort("#{target['AlternativePort']}/tcp", options)
162
94
  else
163
- if target['AlternativePort']
164
- updateLocalFile(postfixDir + MASTER_FILE, options, true) do |fileLines|
165
- fileLines << "#{target['AlternativePort']} inet n - n - - smtpd\n"
95
+ linuxConnection.firewallAddService('smtp', options)
96
+ end
97
+ linuxConnection.firewallAddService('smtps', options)
98
+
99
+ linuxConnection.createDirs(options, '/etc/sasl2')
100
+ linuxConnection.upload(__dir__ + '/smtpd.conf', '/etc/sasl2/smtpd.conf', options)
101
+ linuxConnection.ensureFile('/etc/sasldb2', options)
102
+ linuxConnection.setUserGroup('/etc/sasldb2', 'postfix', 'postfix', options)
103
+ linuxConnection.ensureFile("#{postfixDir}access", options)
104
+ linuxConnection.exec("postmap lmdb:#{postfixDir}access", false, options)
105
+ linuxConnection.ensureFile("#{postfixDir}sender_login", options)
106
+ linuxConnection.exec("postmap lmdb:#{postfixDir}sender_login", false, options)
107
+ linuxConnection.ensureFile("/etc/postfix/#{PASSWORD_FILE}", options)
108
+
109
+ if !target['Aliases'].to_h.empty?
110
+ if !linuxConnection.filePresent?('/etc/aliases', options)
111
+ linuxConnection.exec('cp /etc/postfix/aliases /etc/', false, options)
112
+ linuxConnection.rm("/etc/postfix/aliases", false, options[:dry])
113
+ end
114
+ target['Aliases'].each do |name, destination|
115
+ linuxConnection.fileReplace('/etc/aliases', '^'+name + ':', '#' + name + ':', options)
116
+ end
117
+ linuxConnection.updateFile('/etc/aliases', options, true) do |fileLines|
118
+ target['Aliases'].each do |name, destination|
119
+ fileLines << "#{(name.to_s + ':').ljust(16)}#{self.class.punnycodeEMail(destination)}\n"
120
+ end
121
+ fileLines
122
+ end
123
+ linuxConnection.exec('postalias lmdb:/etc/aliases', false, options)
124
+ end
125
+
126
+ certDir = linuxConnection.createWildecardCertificate(options)
127
+ target['Settings'] ||= {}
128
+ target['Settings']['alias_maps'] = 'lmdb:/etc/aliases'
129
+ target['Settings']['default_database_type'] = 'lmdb'
130
+ target['Settings']['smtp_tls_security_level'] = 'may' unless target['Settings']['smtp_tls_security_level']
131
+ target['Settings']['smtp_sasl_password_maps'] = 'lmdb:/etc/postfix/' + PASSWORD_FILE
132
+ target['Settings']['smtp_sasl_security_options'] = 'noanonymous'
133
+ target['Settings']['smtpd_sender_login_maps'] = "lmdb:#{postfixDir}sender_login" unless target['Settings']['smtpd_sender_login_maps']
134
+ target['Settings']['smtpd_sender_restrictions'] = "lmdb:#{postfixDir}access" unless target['Settings']['smtpd_sender_restrictions']
135
+ target['Settings']['smtpd_tls_mandatory_protocols'] = '>=TLSv1.2' unless target['Settings']['smtpd_tls_mandatory_protocols']
136
+ target['Settings']['smtpd_tls_auth_only'] = 'yes' unless target['Settings']['smtpd_tls_auth_only']
137
+ target['Settings']['smtpd_tls_security_level'] = 'may' unless target['Settings']['smtpd_tls_security_level']
138
+ target['Settings']['smtpd_tls_cert_file'] = certDir + 'fullchain.pem' unless target['Settings']['smtpd_tls_cert_file']
139
+ target['Settings']['smtpd_tls_key_file'] = certDir + 'privkey.pem' unless target['Settings']['smtpd_tls_key_file']
140
+ target['Settings']['tls_preempt_cipherlist'] = 'yes' unless target['Settings']['tls_preempt_cipherlist']
141
+ target['Settings']['tls_ssl_options'] = 'NO_RENEGOTIATION' unless target['Settings']['tls_ssl_options']
142
+
143
+ if target['Relay']
144
+ port = target['Relay']['Port'].to_s
145
+ port = '587' if port.empty?
146
+ target['Settings']['relayhost'] = "#{target['Relay']['Host']}:#{port}" unless target['Settings']['relayhost']
147
+ target['Settings']['smtp_sasl_auth_enable'] = 'yes'
148
+ if port == '465'
149
+ target['Settings']['smtp_tls_security_level'] = 'encrypt'
150
+ target['Settings']['smtp_tls_wrappermode'] = 'yes'
151
+ end
152
+ if target['Relay']['SecretId']
153
+ username = target['Relay']['Username'].to_s
154
+ password = context.secrets.load(target['Relay']['SecretId'], username.upcase + '_PASSWORD')
155
+ linuxConnection.updateFile('/etc/postfix/' + PASSWORD_FILE, options) do |fileLines|
156
+ fileLines << "#{target['Settings']['relayhost']} #{username}:#{password}\n"
166
157
  end
167
158
  end
168
- if target['Settings']
169
- target['Settings'].each do |name, value|
170
- `sed -i 's|^#{name} =.*|#{name} = #{value}|' #{postfixDir + MAIN_FILE}`
159
+ end
160
+ linuxConnection.exec("postmap lmdb:/etc/postfix/#{PASSWORD_FILE}", false, options)
161
+
162
+ target['Settings'].each do |name, value|
163
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, "^#{name}[[:blank:]]*=[[:blank:]]*", "##{name} = ", options)
164
+ end
165
+ linuxConnection.updateFile(postfixDir + MAIN_FILE, options) do |fileLines|
166
+ target['Settings'].each do |name, value|
167
+ value = 'yes' if value == true
168
+ value = 'no' if value == false
169
+ fileLines << "#{name} = #{value}\n"
170
+ end
171
+ fileLines
172
+ end
173
+
174
+ if target['ForwardDovecot']
175
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^#virtual_transport =.*', 'virtual_transport = lmtp:unix:/run/dovecot/lmtp', options)
176
+ end
177
+
178
+ if target['ForwardAll']
179
+ linuxConnection.fileReplace(postfixDir + MAIN_FILE, '^transport_maps =.*', "transport_maps = lmdb:#{postfixDir}transport", options)
180
+
181
+ linuxConnection.updateFile(postfixDir + TRANSPORT_FILE, options, true) do |fileLines|
182
+ hostname, port = target['ForwardAll'].split(':')
183
+ hostname = '[' + hostname + ']'
184
+ line = '* smtp:' + hostname
185
+ line += ':' + port if port
186
+ fileLines << line + "\n"
187
+ end
188
+ linuxConnection.exec("postmap lmdb:#{postfixDir + TRANSPORT_FILE}", false, options)
189
+ end
190
+
191
+ if target['Instance']
192
+ linuxConnection.exec("postmulti -i #{postfixDirName} -e enable", false, options)
193
+ linuxConnection.exec("postmulti -i #{postfixDirName} -p start", true, options)
194
+ end
195
+ end
196
+
197
+ def deployAccounts(target, linuxConnection, context, options)
198
+ if target['Accounts']
199
+ postfixDirName = 'postfix'
200
+ postfixDirName = 'postfix-' + target['Instance'] if target['Instance']
201
+ postfixDir = '/etc/' + postfixDirName + '/'
202
+
203
+ linuxConnection.ensureFile("#{postfixDir}sender_login", { **options, 'dry' => false })
204
+ linuxConnection.download("#{postfixDir}sender_login", options['output'], { **options, 'dry' => false })
205
+ senderLoginFile = options['output'] + '/sender_login'
206
+ accountData = File.read(senderLoginFile)
207
+ addAccounts = []
208
+ accountPasswords = linuxConnection.exec('sasldblistusers2', false, { **options, 'dry' => false })
209
+ target['Accounts'].each do |account, emails|
210
+ accountLine = "#{account} #{account}"
211
+ if !accountData.include?(accountLine)
212
+ addAccounts << accountLine
213
+ end
214
+ if !accountPasswords.include?(account + ':')
215
+ passwordName = account.upcase + '_PASSWORD'
216
+ password = context.secrets.load(target['SecretId'], passwordName)
217
+ if password.nil?
218
+ password = SecureRandom.urlsafe_base64(20)
219
+ context.secrets.store(target['SecretId'], passwordName, password) unless options['dry']
220
+ end
221
+ linuxConnection.exec("echo '#{password}' | saslpasswd2 -p #{account}", false, { **options, hide: true })
222
+ end
223
+ if emails.is_a?(String)
224
+ accountLine = "#{emails} #{account}"
225
+ if !accountData.include?(accountLine)
226
+ addAccounts << accountLine
227
+ end
228
+ elsif emails.is_a?(Array)
229
+ emails.each do |email|
230
+ accountLine = "#{email} #{account}"
231
+ if !accountData.include?(accountLine)
232
+ addAccounts << accountLine
233
+ end
234
+ end
171
235
  end
172
236
  end
173
- if target['ForwardAll']
174
- updateLocalFile(postfixDir + TRANSPORT_FILE, options, true) do |fileLines|
175
- fileLines << '* smtp:[' + target['ForwardAll'] + "]\n"
237
+ if !addAccounts.empty?
238
+ addAccounts.each do |accountLine|
239
+ accountData << "\n" + accountLine
176
240
  end
177
- `postmap #{postfixDir + TRANSPORT_FILE}`
241
+ accountData << "\n"
242
+ File.write(senderLoginFile, accountData)
243
+ linuxConnection.upload(senderLoginFile, "#{postfixDir}sender_login", options)
244
+ linuxConnection.exec("postmap lmdb:#{postfixDir}sender_login", false, options)
178
245
  end
179
246
  end
180
247
  end
181
248
 
182
249
  def cleanup(configs, state, context, options)
183
- cleanupType(:Postfix, configs, state, context, options) do |item, id, state, context, options, ssh|
184
- instances = self.class.exec('postmulti -l | wc -l', ssh, true).strip.to_i
185
- if instances <= 1
186
- Framework::LinuxApp.stopService(SERVICE_NAME, ssh, options[:dry])
187
- Framework::LinuxApp.firewallRemoveService('smtps', ssh, options[:dry])
188
- if item['AlternativePort']
189
- Framework::LinuxApp.firewallRemovePort("#{item['AlternativePort']}/tcp", ssh, options[:dry])
190
- else
191
- Framework::LinuxApp.firewallRemoveService('smtp', ssh, options[:dry])
192
- end
193
- Framework::LinuxApp.removePackage(PACKAGE_NAME, ssh, options[:dry])
250
+ cleanupType(:Postfix, configs, state, context, options) do |item, id, state, context, options, connection|
251
+ Linux.withConnection(connection) do |linuxConnection|
252
+ instances = linuxConnection.exec('postmulti -l | wc -l', true).strip.to_i
253
+ if instances <= 1
254
+ linuxConnection.stopService(SERVICE_NAME, options)
255
+ linuxConnection.firewallRemoveService('smtps', options)
256
+ if item['AlternativePort']
257
+ linuxConnection.firewallRemovePort("#{item['AlternativePort']}/tcp", options)
258
+ else
259
+ linuxConnection.firewallRemoveService('smtp', options)
260
+ end
261
+ linuxConnection.removePackage(PACKAGE_NAME, options)
194
262
 
195
- state.item(id)['Status'] = State::STATUS_DELETED unless options[:dry]
263
+ state.item(id)['Status'] = State::STATUS_DELETED unless options[:dry]
196
264
 
197
- if options[:destroy]
198
- rm('/etc/postfix', options[:dry], ssh)
265
+ if options[:destroy]
266
+ linuxConnection.rm('/etc/postfix', options[:dry])
199
267
 
200
- state.item(id)['Status'] = State::STATUS_DESTROYED unless options[:dry]
268
+ state.item(id)['Status'] = State::STATUS_DESTROYED unless options[:dry]
269
+ end
270
+ else
271
+ prompt.say('Postfix multiple instance cleanup not implemented!', :color => :red)
201
272
  end
202
- else
203
- prompt.say('Postfix multiple instance cleanup not implemented!', :color => :red)
204
273
  end
205
274
  end
206
275
  end
276
+
277
+ def self.punnycodeEMail(email)
278
+ emailLocal, emailDomain = email.to_s.split('@')
279
+ if emailLocal && emailDomain && emailLocal.ascii_only? && !emailDomain.ascii_only?
280
+ email = emailLocal + '@' + Addressable::IDNA.to_ascii(emailDomain)
281
+ end
282
+ email
283
+ end
284
+
207
285
  end
208
286
 
209
287
  end
@@ -0,0 +1,97 @@
1
+
2
+ module ConfigLMM
3
+ module LMM
4
+ class PostgreSQLConnection
5
+
6
+ attr_reader :connection
7
+
8
+ def initialize(connection, settings)
9
+ @connection = connection
10
+ @settings = settings
11
+ @pgsqlDir = nil
12
+ end
13
+
14
+ def exec(sql, db, allowFailure = false, queryOptions = [], options = {})
15
+ db = 'postgres' unless db
16
+ cmd = "psql #{queryOptions.join(' ')} --dbname=#{db} --command=#{sql.shellescape}"
17
+ if options[:hide]
18
+ cmd = ' ' + cmd
19
+ end
20
+ connection.exec(cmd, allowFailure, options)
21
+ end
22
+
23
+ def createUser(user, password = nil, options = {})
24
+ connection.exec("createuser #{user.shellescape}", true)
25
+ if !password.nil?
26
+ sql = " ALTER USER #{user} WITH PASSWORD '#{LinuxShell.escapeSingleQuotes(password)}'"
27
+ exec(sql, nil, false, [], { **options, hide: true })
28
+ end
29
+ end
30
+
31
+ def createDB(db, owner = nil, options = {})
32
+ ownerSQL = owner ? "--owner=#{owner.shellescape}" : ''
33
+ connection.exec("createdb --locale=C --template=template0 #{ownerSQL} #{db.shellescape}", true, options)
34
+ end
35
+
36
+ def createUserAndDB(user, password, options = {})
37
+ createUser(user, password, options)
38
+ createDB(user, user, options)
39
+ end
40
+
41
+ def dropUserAndDB(user, options = {})
42
+ connection.exec("dropdb #{user.shellescape}", true, options)
43
+ connection.exec("dropuser #{user.shellescape}", true, options)
44
+ end
45
+
46
+ def grantReplication(user, options = {})
47
+ exec("ALTER USER #{user} REPLICATION", nil, false, [], options)
48
+ exec("GRANT pg_read_all_data TO #{user}", nil, false, [], options)
49
+ end
50
+
51
+ def createExtensions(db, extensions, options)
52
+ extensions.each do |extension|
53
+ exec("CREATE EXTENSION #{extension}", db, true, [], options)
54
+ end
55
+ end
56
+
57
+ def importSQL(owner, db, sqlFile, options = {})
58
+ cmd = "psql #{db} < #{sqlFile}"
59
+ output = connection.exec(cmd, false, options)
60
+ raise output if output.include?('ERROR:') && !output.include?('already exists')
61
+ end
62
+
63
+ def updateOwner(db, owner, options = {})
64
+ sql = "SELECT tablename FROM pg_tables WHERE NOT schemaname IN ('pg_catalog', 'information_schema')"
65
+ tables = self.exec(sql, db, false, ['--csv', '--tuples-only']).strip.lines
66
+ tables.each do |table|
67
+ self.exec("ALTER TABLE public.#{table} OWNER TO #{owner};", db, false, [], options)
68
+ end
69
+
70
+ sql = "SELECT sequence_name FROM information_schema.sequences WHERE NOT sequence_schema IN ('pg_catalog', 'information_schema')"
71
+ sequences = self.exec(sql, db, false, ['--csv', '--tuples-only']).strip.lines
72
+ sequences.each do |sequence|
73
+ self.exec("ALTER SEQUENCE public.#{sequence} OWNER TO #{owner};", db, false, [], options)
74
+ end
75
+
76
+ sql = "SELECT table_name FROM information_schema.views WHERE NOT table_schema IN ('pg_catalog', 'information_schema')"
77
+ views = self.exec(sql, db, false, ['--csv', '--tuples-only']).strip.lines
78
+ views.each do |view|
79
+ self.exec("ALTER VIEW public.#{view} OWNER TO #{owner};", db, false, [], options)
80
+ end
81
+ end
82
+
83
+ def pgsqlDir
84
+ return @pgsqlDir if @pgsqlDir
85
+ distroID = connection.distroID
86
+ if distroID == 'opensuse-leap'
87
+ @pgsqlDir = '/var/lib/pgsql/'
88
+ elsif distroID == 'arch'
89
+ @pgsqlDir = '/var/lib/postgres/'
90
+ else
91
+ raise Framework::PluginProcessError.new("Unsupported Linux Distro: #{distroID}!")
92
+ end
93
+ @pgsqlDir
94
+ end
95
+ end
96
+ end
97
+ end