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
  "#!/usr/bin/ruby\n\nrequire 'rubygems'\nrequire 'cloudboot'\n\nmodule Cloudboot\n  class Callback\n\ndef self.failure(options)\n  puts \"cloudboot failure\"\n  puts options[:error].inspect if options[:error]\nend\n\ndef self.success(options)\n  \n  hostname = options[:hostname]\n  domain = options[:domain]\n  \n  puts \"setup - hostname: \\\#{hostname}, domain: \\\#{domain}\"\n  \n  `echo \"127.0.0.1  \\\#{hostname}.\\\#{domain} localhost \\\#{hostname}\" > /etc/hosts`\n  `sed -i 's/HOSTNAME=.*/HOSTNAME=\\\"\\\#{hostname}\\\"/g' /etc/rc.conf`\n  \n  `hostname \\\#{hostname}`\nend\n  end\nend\n\nCloudboot::Bootstrap.new.bootstrap\n"
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"]
  
  "#!/usr/bin/ruby\n\nrequire 'rubygems'\nrequire 'activerecord'\nrequire 'sinatra'\nrequire 'cloudboot'\n\nActiveRecord::Base.logger = Logger.new(STDOUT)\n\nActiveRecord::Base.establish_connection({\n  :adapter    => '\#{database[\"adapter\"]}',\n  :encoding   => '\#{database[\"encoding\"]}',\n  :database   => '\#{database[\"database\"]}',\n  :host       => '\#{database[\"host\"]}',\n  :port       => \#{database[\"port\"]},\n  :username   => '\#{database[\"username\"]}',\n  :password   => '\#{database[\"password\"]}'\n})\n\nCloudboot::Generator.setup_tables\n\nCloudboot::Domain.ensure(\"\#{server[\"domain\"]}\", \"srv\", 3, 120)\n\nset :server, [\"webrick\"]\nset :environment, :production\nset :lock, true\n\npost \"/register\" do\n\n  public_ip   = request.params['public_ip']\n  private_ip  = request.params['private_ip']\n  domain      = request.params['domain']\n  hostname    = request.params['host']\n\n  instance = Cloudboot::Domain.register(domain, hostname, public_ip, private_ip)\n  instance.name\nend\n  EOF\nend\n"

.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 "      create\n        table domains\n        (\n          id bigint not null AUTO_INCREMENT,\n          name varchar(255) not null,\n          refresh_timeout bigint not null,\n          instance_prefix varchar(255) not null,\n          primary key (id)\n        )\n        ENGINE=InnoDB default CHARSET=utf8\n    SQL\n  end\n\n  unless table_exist?(\"instances\")\n    ActiveRecord::Base.connection.execute <<-SQL\n      create\n        table instances\n        (\n          id bigint not null AUTO_INCREMENT,\n          name varchar(255) not null,\n          domain_id bigint not null,\n          public_ip varchar(15) not null,\n          private_ip varchar(15) not null,\n          refresh datetime not null,\n          created_at datetime not null,\n          primary key (id)\n        )\n        ENGINE=InnoDB default CHARSET=utf8\n    SQL\n  end\n  \nend\n"

.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