Class: Cloudboot::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudboot/bootstrap.rb

Instance Method Summary collapse

Instance Method Details

#bootstrapObject



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
# File 'lib/cloudboot/bootstrap.rb', line 81

def bootstrap
  
  mutex "/tmp/cloudboot.lock" do

    domain    = nil
    hostname  = nil
    error     = nil

    begin
      user_data = read('http://169.254.169.254/1.0/user-data')

      if user_data
        bootstrap = YAML.load(user_data)

        domain = bootstrap['domain']

        public_ipv4 = read("http://169.254.169.254/latest/meta-data/public-ipv4")
        local_ipv4  = read("http://169.254.169.254/latest/meta-data/local-ipv4")

        puts bootstrap.inspect

        url = "http://#{bootstrap['host']}:#{bootstrap['port']}/register"

        params = "public_ip=#{public_ipv4}&private_ip=#{local_ipv4}&domain=#{domain}&host="
        params << hostname()
    
        hostname = read(url, {:params => params})
      end
  
    rescue Exception => e
      error = e
    ensure
      options = {:hostname => hostname, :domain => domain, :error => error}

      if hostname && domain 
        Callback.success(options)
      else
        Callback.failure(options)
      end
      
    end
  end

end

#failure(options) ⇒ Object



78
79
# File 'lib/cloudboot/bootstrap.rb', line 78

def failure(options)
end

#fqdnObject



56
57
58
# File 'lib/cloudboot/bootstrap.rb', line 56

def fqdn
  `hostname -f`.strip
end

#hostnameObject



52
53
54
# File 'lib/cloudboot/bootstrap.rb', line 52

def hostname
  `hostname`.strip
end

#mutex(lock_file, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cloudboot/bootstrap.rb', line 60

def mutex(lock_file, &block)
  
  unless File.exist?(lock_file)
    begin
      FileUtils.touch(lock_file)
      block.call
    ensure
      FileUtils.rm(lock_file)
    end
  else
    puts "skipping - lock_file #{lock_file} exist"
  end
  
end

#read(url, options = {}) ⇒ Object



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
# File 'lib/cloudboot/bootstrap.rb', line 16

def read(url, options={})
  
  options = {
    :params => ""
  }.merge(options)
  
  url = URI.parse(url)

  http = Net::HTTP.new(url.host, url.port)
  http.open_timeout = 10
  http.read_timeout = 10

  params = options[:params]

  puts "options: #{options.inspect}"

  res = if params.empty?
    puts "req: GET #{url}"
    http.get(url.path)
  else
    puts "req: POST #{url} - #{params}"
    http.post(url.path, params)
  end

  s = if res.is_a?(Net::HTTPOK) 
    r = res.body.strip
    r.empty? ? nil : r
  else
    nil
  end

  puts "res: #{s.nil? ? 'nil' : s}"

  s
end

#success(options) ⇒ Object



75
76
# File 'lib/cloudboot/bootstrap.rb', line 75

def success(options)
end