Class: SmartMachine::Grids::Nginx

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_machine/grids/nginx.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

#initializeNginx

Returns a new instance of Nginx.



8
9
10
# File 'lib/smart_machine/grids/nginx.rb', line 8

def initialize
  @home_dir = File.expand_path('~')
end

Instance Method Details

#create_htpasswd_filesObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/smart_machine/grids/nginx.rb', line 151

def create_htpasswd_files
  htpasswd_dir = "#{Dir.pwd}/grids/nginx/htpasswd"

  # Remove existing htpasswd_dir and recreate it.
  FileUtils.rm_r htpasswd_dir if Dir.exist?(htpasswd_dir)
  FileUtils.mkdir htpasswd_dir
  FileUtils.touch "#{htpasswd_dir}/.keep"

  # Add hostfiles to htpasswd_dir
  get_users_from_file.each do |domain_name, users|
    next unless users

    file_data = ""
    users.each do |user, password|
      file_data += "#{user}:#{BCrypt::Password.create(password)}\n"
    end
    File.open("#{Dir.pwd}/grids/nginx/htpasswd/#{domain_name}", "w") { |file| file.write(file_data) }
  end
end

#downerObject

Stopping & Removing containers - in reverse order



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/smart_machine/grids/nginx.rb', line 116

def downer
  puts "-----> Stopping container nginx-letsencrypt ... "
  if system("docker stop 'nginx-letsencrypt'", out: File::NULL)
    puts "done"
    puts "-----> Removing container nginx-letsencrypt ... "
    if system("docker rm 'nginx-letsencrypt'", out: File::NULL)
      puts "done"
    end
  end

  puts "-----> Stopping container nginx-gen ... "
  if system("docker stop 'nginx-gen'", out: File::NULL)
    puts "done"
    puts "-----> Removing container nginx-gen ... "
    if system("docker rm 'nginx-gen'", out: File::NULL)
      puts "done"
    end
  end

  puts "-----> Stopping container nginx ... "
  if system("docker stop 'nginx'", out: File::NULL)
    puts "done"
    puts "-----> Removing container nginx ... "
    if system("docker rm 'nginx'", out: File::NULL)
      puts "done"
    end
  end

  # Removing networks
  puts "-----> Removing network nginx-network ... "
  if system("docker network rm nginx-network", out: File::NULL)
    puts "done"
  end
end

#uperObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/smart_machine/grids/nginx.rb', line 12

def uper
  # Creating volumes
  puts "-----> Creating volume nginx-confd ... "
  if system("docker volume create nginx-confd", out: File::NULL)
    puts "done"
  end

  puts "-----> Creating volume nginx-vhost ... "
  if system("docker volume create nginx-vhost", out: File::NULL)
    puts "done"
  end

  puts "-----> Creating volume nginx-shtml ... "
  if system("docker volume create nginx-shtml", out: File::NULL)
    puts "done"
  end

  # Creating networks
  unless system("docker network inspect nginx-network", [:out, :err] => File::NULL)
    puts "-----> Creating network nginx-network ... "
    if system("docker network create nginx-network", out: File::NULL)
      puts "done"
    end
  end

  # Creating & Starting containers
  puts "-----> Creating container nginx ... "
  command = [
    "docker create",
    "--name='nginx'",
    "--publish='80:80' --publish='443:443'",
    "--volume='nginx-confd:/etc/nginx/conf.d/'",
    "--volume='nginx-vhost:/etc/nginx/vhost.d/'",
    "--volume='nginx-shtml:/usr/share/nginx/html'",
    "--volume='#{@home_dir}/smartmachine/grids/nginx/certificates:/etc/nginx/certs'",
    "--volume='#{@home_dir}/smartmachine/grids/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf:ro'",
    "--volume='#{@home_dir}/smartmachine/grids/nginx/htpasswd:/etc/nginx/htpasswd:ro'",
    "--restart='always'",
    "--network='nginx-network'",
    "nginx:alpine"
  ]
  if system(command.compact.join(" "), out: File::NULL)
    puts "done"
    puts "-----> Starting container nginx ... "
    if system("docker start nginx", out: File::NULL)
      puts "done"
    else
      raise "Error: Could not start the created nginx container."
    end
  else
    raise "Error: Could not create nginx container."
  end

  puts "-----> Creating container nginx-gen ... "
  command = [
    "docker create",
    "--name='nginx-gen'",
    "--volumes-from nginx",
    "--volume='#{@home_dir}/smartmachine/grids/nginx/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro'",
    "--volume='/var/run/docker.sock:/tmp/docker.sock:ro'",
    "--restart='always'",
    "--network='nginx-network'",
    "jwilder/docker-gen",
    "-notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf"
  ]
  if system(command.compact.join(" "), out: File::NULL)
    puts "done"
    puts "-----> Starting container nginx-gen ... "
    if system("docker start nginx-gen", out: File::NULL)
      puts "done"
    else
      raise "Error: Could not start the created nginx-gen container."
    end
  else
    raise "Error: Could not create nginx-gen container."
  end

  puts "-----> Creating container nginx-letsencrypt ... "
  command = [
    "docker create",
    "--name='nginx-letsencrypt'",
    "--env NGINX_PROXY_CONTAINER=nginx",
    "--env NGINX_DOCKER_GEN_CONTAINER=nginx-gen",
    "--env DEFAULT_EMAIL=#{SmartMachine.config.sysadmin_email}",
    "--volumes-from nginx",
    "--volume='/var/run/docker.sock:/var/run/docker.sock:ro'",
    "--restart='always'",
    "--network='nginx-network'",
    "jrcs/letsencrypt-nginx-proxy-companion"
  ]
  if system(command.compact.join(" "), out: File::NULL)
    puts "done"
    puts "-----> Starting container nginx-letsencrypt ... "
    if system("docker start nginx-letsencrypt", out: File::NULL)
      puts "done"
    else
      raise "Error: Could not start the created nginx-letsencrypt container."
    end
  else
    raise "Error: Could not create nginx-letsencrypt container."
  end
end