Class: Buildhosts::Manginx

Inherits:
Object
  • Object
show all
Defined in:
lib/buildhosts/Manginx.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Manginx



6
7
8
9
# File 'lib/buildhosts/Manginx.rb', line 6

def initialize config
    # Set up teh parser and parse with it
    @conf = Buildhosts::ConfigParser.parse config
end

Instance Method Details

#configuring(what) ⇒ Object

Helper methods to print common messages



12
13
14
# File 'lib/buildhosts/Manginx.rb', line 12

def configuring what
    puts "Configuring: #{what}..."
end

#done(next_step = '') ⇒ Object



15
16
17
# File 'lib/buildhosts/Manginx.rb', line 15

def done next_step=''
    puts "Done! #{next_step}"
end

#runObject



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
# File 'lib/buildhosts/Manginx.rb', line 19

def run
    # If you don't have any IP addresses in your config, what are you doing here?
    if @conf['ips'].nil? || @conf['ips'].empty?
        puts 'What\'s the point?'
        exit
    end
    system('mkdir -p /usr/local/etc/nginx/xip')
    # Let's find out what nginx is expecting from us...
    expected = Array.new
    n = File.open('/usr/local/etc/nginx/nginx.conf', 'r')
    n.each_line do |line|
        # So basically, if the line goes "include xip/derp.local;", we want to
        # set up a file with that name with the appropriate server_name directives.
        match = line.match(/include\s+xip\/([\w\.]+);/) 
        if !match.nil? # We don't want to try to convert nil to an array...
           expected << match.to_a[1..-1][0]
        end
    end

    # This is where the files will be stored.
    base_path = '/usr/local/etc/nginx/xip'
    `rm -f #{base_path}/*`

    # Le bread and buttere
    expected.each do |file|
        configuring file
        out = File.open("#{base_path}/#{file}", 'w+')
        @conf['ips'].each do |ip|
            # Simple. Just put a line in there for each IP in our config file.
            out.write "server_name #{file}.#{ip}.xip.io;\n"
        end
        out.close
    end

    # Now we just reload nginx and apologize if something goes rotten.
    done 'Reloading nginx...'
    if system('sudo nginx -s reload')
        done
    else
        puts "Oops!\nSomething went wrong... Sorry."
    end
end