Module: Proxyneitor::Builders

Included in:
Base
Defined in:
lib/proxyneitor/builders.rb

Instance Method Summary collapse

Instance Method Details

#app_dirObject



69
70
71
# File 'lib/proxyneitor/builders.rb', line 69

def app_dir
  File.join(@root, @app)
end

#app_file_url(file) ⇒ Object



81
82
83
# File 'lib/proxyneitor/builders.rb', line 81

def app_file_url(file)
  File.join(app_dir, file)
end

#create(port) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/proxyneitor/builders.rb', line 3

def create(port)
  root = @root
  app = @app

  ## Create ~/Proxies/APP_NAME folder if dont exist
  Dir.mkdir(app_dir) unless File.directory?(app_dir)

  ssl = ssl_folder
  use_ssl = use_ssl?
  # write app/nginx.conf
  puts "Building nginx conf for #{app_dir}"

  render_template('nginx.conf.erb', app_file_url('nginx.conf'), false) do |file, template|
    file.write ERB.new(template).result(binding)
  end

  restart_nginx

end

#install(force = false) ⇒ Object



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
# File 'lib/proxyneitor/builders.rb', line 32

def install(force=false)
  root = @root
  os = @os

  puts "Instaling nginx"
  nginx_folder = load_nginx_folder
  puts "Instalation force" if force == true

  conf_file = File.join(nginx_folder, 'nginx.conf')
  render_template('base_nginx.conf.erb', conf_file, !force) do |file, template|
    puts "Loading new nginx.conf file"
    file.write(ERB.new(template).result(binding))
  end

  puts "This configuration asume that you have this settings in #{nginx_folder}:"
  puts "        include /etc/nginx/conf.d/*.conf;"
  puts "        include /etc/nginx/sites-enabled/*;"

  puts "Setting site-enabled folder and default"
  sites_enabled = File.join(nginx_folder, 'sites-enabled')
  Dir.mkdir(sites_enabled) unless File.directory?(sites_enabled)

  render_template('default', File.join(sites_enabled, 'default'), !force) do |file, template|
    file.write(template)
  end

  puts "Setting conf.d folder and proxyneitor"
  conf_d = File.join(nginx_folder, 'conf.d')
  Dir.mkdir(conf_d) unless File.directory?(conf_d)

  render_template('proxyneitor_site', File.join(conf_d, 'proxyneitor.conf'), !force) do |file, template|
    file.write ERB.new(template).result(binding)
  end

  restart_nginx
end

#listObject



117
118
119
# File 'lib/proxyneitor/builders.rb', line 117

def list
  Dir.glob(File.join(@root, '*')).each { |f| puts f.split('/')[-1] }
end

#load_nginx_folderObject



107
108
109
110
111
112
113
114
115
# File 'lib/proxyneitor/builders.rb', line 107

def load_nginx_folder
  if @os == 'darwin'
    system('brew install nginx')
    nginx_folder = '/usr/local/etc/nginx/'
  else
    system('sudo apt-get install nginx')
    nginx_folder = '/etc/nginx/'
  end
end

#removeObject



23
24
25
26
27
28
29
30
# File 'lib/proxyneitor/builders.rb', line 23

def remove
  app = @app
  root = @root
  directory = app_dir

  remove_folder directory
  restart_nginx
end

#remove_folder(directory) ⇒ Object



102
103
104
105
# File 'lib/proxyneitor/builders.rb', line 102

def remove_folder(directory)
  puts "Removing app from #{app_dir}"
  FileUtils.rm_rf(directory)
end

#render_template(template_url, target, optional = true, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/proxyneitor/builders.rb', line 85

def render_template(template_url, target , optional = true, &block)
  unless File.exists?(target) && optional == true

    puts "Rendering #{target}"
    template = File.open(File.join(Pathname.new(__FILE__).dirname, 'templates', template_url), 'rb').read

    File.open(target, 'w') do |file|
      block.call(file, template)
    end
  end
end

#restart_nginxObject



97
98
99
100
# File 'lib/proxyneitor/builders.rb', line 97

def restart_nginx
  puts "Restarting nginx"
  system('sudo nginx -s reload')
end

#ssl_folderObject



73
74
75
# File 'lib/proxyneitor/builders.rb', line 73

def ssl_folder
  @ssl ||= File.exists?(File.join(@root, @app, 'ssl')) ? File.join(@root, @app, 'ssl') : File.join(@root, @app, 'tls')
end

#use_ssl?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/proxyneitor/builders.rb', line 77

def use_ssl?
  @user_ssl ||= File.exists?(File.join(@ssl, 'server.crt')) && File.exists?(File.join(@ssl, 'server.key'))
end