Class: SmartMachine::Grids::Emailer

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_machine/grids/emailer.rb

Instance Method Summary collapse

Methods inherited from Base

#machine_has_engine_installed?, #platform_on_machine?, #user_bash

Methods included from Logger

configure_logger_for, included, #logger, logger_for

Constructor Details

#initialize(name:) ⇒ Emailer

Returns a new instance of Emailer.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/smart_machine/grids/emailer.rb', line 4

def initialize(name:)
  config = SmartMachine.config.grids.emailer.dig(name.to_sym)
  raise "emailer config for #{name} not found." unless config

  @image = "smartmachine/emailer:#{SmartMachine.version}"
  @fqdn = config.dig(:fqdn)
  @mailname = config.dig(:mailname)
  @sysadmin_email = config.dig(:sysadmin_email)
  @networks = config.dig(:networks)
  @mysql_host = config.dig(:mysql_host)
  @mysql_port = config.dig(:mysql_port)
  @mysql_user = config.dig(:mysql_user)
  @mysql_password = config.dig(:mysql_password)
  @mysql_database_name = config.dig(:mysql_database_name)
  @monit_smtp_email_name = config.dig(:monit_smtp_email_name)
  @monit_smtp_email_address = config.dig(:monit_smtp_email_address)
  @monit_smtp_host = config.dig(:monit_smtp_host)
  @monit_smtp_port = config.dig(:monit_smtp_port)
  @monit_smtp_username = config.dig(:monit_smtp_username)
  @monit_smtp_password = config.dig(:monit_smtp_password)
  @oracle_ips_allowed = config.dig(:oracle_ips_allowed)
  @oracle_deflect_url = config.dig(:oracle_deflect_url)

  @name = name.to_s
  @home_dir = File.expand_path('~')
end

Instance Method Details

#downerObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/smart_machine/grids/emailer.rb', line 141

def downer
  # Disconnecting networks
  @networks.reverse.each do |network|
    system("docker network disconnect #{network} #{@name}")
  end

  # Stopping & Removing containers - in reverse order
  print "-----> Stopping container #{@name} ... "
  if system("docker stop '#{@name}'", out: File::NULL)
    puts "done"
    print "-----> Removing container #{@name} ... "
    if system("docker rm '#{@name}'", out: File::NULL)
      puts "done"
    end
  end
end

#installerObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/smart_machine/grids/emailer.rb', line 31

def installer
  unless system("docker image inspect #{@image}", [:out, :err] => File::NULL)
    puts "-----> Creating image #{@image} ... "
    command = [
      "docker image build -t #{@image}",
      "--build-arg SMARTMACHINE_VERSION=#{SmartMachine.version}",
      "-f- #{SmartMachine.config.gem_dir}/lib/smart_machine/grids/emailer",
      "<<'EOF'\n#{dockerfile}EOF"
    ]
    if system(command.join(" "), out: File::NULL)
      puts "done"
    else
      raise "Error: Could not install image: #{@image}"
    end
  else
    raise "Error: Image already installed: #{@image}. Please uninstall using 'smartmachine grids emailer uninstall' and try installing again."
  end
end

#uninstallerObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/smart_machine/grids/emailer.rb', line 50

def uninstaller
  unless system("docker inspect -f '{{.State.Running}}' '#{@name}'", [:out, :err] => File::NULL)
    if system("docker image inspect #{@image}", [:out, :err] => File::NULL)
      puts "-----> Removing image #{@image} ... "
      if system("docker image rm #{@image}", out: File::NULL)
        puts "done"
      end
    else
      raise "Error: Emailer already uninstalled. Please install using 'smartmachine grids emailer install' and try uninstalling again."
    end
  else
    raise "Error: Emailer is currently running. Please stop the emailer using 'smartmachine grids emailer down' and try uninstalling again."
  end
end

#uperObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/smart_machine/grids/emailer.rb', line 65

def uper
  if system("docker image inspect #{@image}", [:out, :err] => File::NULL)
    FileUtils.mkdir_p("#{@home_dir}/machine/grids/emailer/#{@name}/backups")
    FileUtils.mkdir_p("#{@home_dir}/machine/grids/emailer/#{@name}/data/vmail")
    FileUtils.mkdir_p("#{@home_dir}/machine/grids/emailer/#{@name}/data/opendkim")

    # Setting entrypoint permission.
    system("chmod +x #{@home_dir}/machine/config/emailer/docker/entrypoint.rb")

    # Creating & Starting containers
    print "-----> Creating container #{@name} ... "

    command = [
      "docker create",
      "--name='#{@name}'",
      "--env VIRTUAL_HOST=#{@fqdn}",
      "--env VIRTUAL_PATH='/'",
      "--env LETSENCRYPT_HOST=#{@fqdn}",
      "--env LETSENCRYPT_EMAIL=#{@sysadmin_email}",
      "--env LETSENCRYPT_TEST=false",
      "--env CONTAINER_NAME='#{@name}'",
      "--env FQDN='#{@fqdn}'",
      "--env MAILNAME='#{@mailname}'",
      "--env SYSADMIN_EMAIL='#{@sysadmin_email}'",
      "--env MYSQL_HOST='#{@mysql_host}'",
      "--env MYSQL_PORT='#{@mysql_port}'",
      "--env MYSQL_USER='#{@mysql_user}'",
      "--env MYSQL_PASSWORD='#{@mysql_password}'",
      "--env MYSQL_DATABASE_NAME='#{@mysql_database_name}'",
      "--env MONIT_SMTP_EMAIL_NAME='#{@monit_smtp_email_name}'",
      "--env MONIT_SMTP_EMAIL_ADDRESS='#{@monit_smtp_email_address}'",
      "--env MONIT_SMTP_HOST='#{@monit_smtp_host}'",
      "--env MONIT_SMTP_PORT='#{@monit_smtp_port}'",
      "--env MONIT_SMTP_USERNAME='#{@monit_smtp_username}'",
      "--env MONIT_SMTP_PASSWORD='#{@monit_smtp_password}'",
      "--env ORACLE_IPS_ALLOWED='#{@oracle_ips_allowed.join(' ')}'",
      "--env ORACLE_DEFLECT_URL='#{@oracle_deflect_url}'",
      "--expose='80'",
      "--publish='25:25'",
      # "--publish='465:465'",
      "--publish='587:587'",
      # "--publish='110:110'",
      "--publish='995:995'",
      # "--publish='143:143'",
      "--publish='993:993'",
      "--volume='#{@home_dir}/smartmachine/grids/nginx/certificates/#{@fqdn}:/etc/letsencrypt/live/#{@fqdn}:ro'",
      "--volume='#{@home_dir}/smartmachine/config/emailer:/smartmachine/config/emailer:ro'",
      "--volume='#{@home_dir}/smartmachine/grids/emailer/#{@name}/data/vmail:/var/vmail'",
      "--volume='#{@home_dir}/smartmachine/grids/emailer/#{@name}/data/opendkim:/etc/opendkim'",
      "--entrypoint='/smartmachine/config/emailer/docker/entrypoint.rb'",
      "--tmpfs /run/tmpfs",
      "--init",
      "--restart='always'",
      "--network='nginx-network'",
      "#{@image}"
    ]
    if system(command.compact.join(" "), out: File::NULL)
      @networks.each do |network|
        system("docker network connect --alias #{@fqdn} #{network} #{@name}")
      end

      puts "done"
      puts "-----> Starting container #{@name} ... "
      if system("docker start #{@name}", out: File::NULL)
        puts "done"
      else
        raise "Error: Could not start container: #{@name}"
      end
    else
      raise "Error: Could not create container: #{@name}"
    end
  else
    raise "Error: Could not find image: #{@image}"
  end
end