Class: Cloudboot::Generator

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

Class Method Summary collapse

Class Method Details

.bootstrap_scriptObject



6
7
8
9
10
11
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
# File 'lib/cloudboot/generator.rb', line 6

def self.bootstrap_script
  <<-EOF
#!/usr/bin/ruby

require 'rubygems'
require 'cloudboot'

module Cloudboot
  class Callback

def self.failure(options)
  puts "cloudboot failure"
  puts options[:error].inspect if options[:error]
end

def self.success(options)
  
  hostname = options[:hostname]
  domain = options[:domain]
  
  puts "setup - hostname: \#{hostname}, domain: \#{domain}"
  
  `echo "127.0.0.1  \#{hostname}.\#{domain} localhost \#{hostname}" > /etc/hosts`
  `sed -i 's/HOSTNAME=.*/HOSTNAME=\"\#{hostname}\"/g' /etc/rc.conf`
  
  `hostname \#{hostname}`
end
  end
end

Cloudboot::Bootstrap.new.bootstrap
EOF
end

.server_script(options) ⇒ Object



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

def self.server_script(options)
  
  database = options["database"]
  server = options["server"]
  
  <<-EOF
#!/usr/bin/ruby

require 'rubygems'
require 'activerecord'
require 'sinatra'
require 'cloudboot'

ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Base.establish_connection({
  :adapter    => '#{database["adapter"]}',
  :encoding   => '#{database["encoding"]}',
  :database   => '#{database["database"]}',
  :host       => '#{database["host"]}',
  :port       => #{database["port"]},
  :username   => '#{database["username"]}',
  :password   => '#{database["password"]}'
})

Cloudboot::Generator.setup_tables

Cloudboot::Domain.ensure("#{server["domain"]}", "srv", 3, 120)

set :server, ["webrick"]
set :environment, :production
set :lock, true

post "/register" do

  public_ip   = request.params['public_ip']
  private_ip  = request.params['private_ip']
  domain      = request.params['domain']
  hostname    = request.params['host']

  instance = Cloudboot::Domain.register(domain, hostname, public_ip, private_ip)
  instance.name
end
  EOF
end

.setup_tablesObject



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

def self.setup_tables

  unless table_exist?("domains")
    ActiveRecord::Base.connection.execute <<-SQL
      create
        table domains
        (
          id bigint not null AUTO_INCREMENT,
          name varchar(255) not null,
          refresh_timeout bigint not null,
          instance_prefix varchar(255) not null,
          primary key (id)
        )
        ENGINE=InnoDB default CHARSET=utf8
    SQL
  end

  unless table_exist?("instances")
    ActiveRecord::Base.connection.execute <<-SQL
      create
        table instances
        (
          id bigint not null AUTO_INCREMENT,
          name varchar(255) not null,
          domain_id bigint not null,
          public_ip varchar(15) not null,
          private_ip varchar(15) not null,
          refresh datetime not null,
          created_at datetime not null,
          primary key (id)
        )
        ENGINE=InnoDB default CHARSET=utf8
    SQL
  end
  
end

.table_exist?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/cloudboot/generator.rb', line 86

def self.table_exist?(table_name)
  ActiveRecord::Base.connection.select_value("show tables like '#{table_name}'") != nil
end