Class: DevLXC::ChefCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/dev-lxc/chef-cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster_config) ⇒ ChefCluster

Returns a new instance of ChefCluster.



7
8
9
10
11
12
13
14
15
16
# File 'lib/dev-lxc/chef-cluster.rb', line 7

def initialize(cluster_config)
  @cluster_config = cluster_config
  @api_fqdn = @cluster_config["api_fqdn"]
  @topology = @cluster_config["topology"]
  @servers = @cluster_config["servers"]
  if @topology == 'tier'
    @bootstrap_backend = @servers.select {|k,v| v["role"] == "backend" && v["bootstrap"] == true}.first.first
    @frontends = @servers.select {|k,v| v["role"] == "frontend"}.keys
  end
end

Instance Attribute Details

#api_fqdnObject (readonly)

Returns the value of attribute api_fqdn.



5
6
7
# File 'lib/dev-lxc/chef-cluster.rb', line 5

def api_fqdn
  @api_fqdn
end

#bootstrap_backendObject (readonly)

Returns the value of attribute bootstrap_backend.



5
6
7
# File 'lib/dev-lxc/chef-cluster.rb', line 5

def bootstrap_backend
  @bootstrap_backend
end

#frontendsObject (readonly)

Returns the value of attribute frontends.



5
6
7
# File 'lib/dev-lxc/chef-cluster.rb', line 5

def frontends
  @frontends
end

#topologyObject (readonly)

Returns the value of attribute topology.



5
6
7
# File 'lib/dev-lxc/chef-cluster.rb', line 5

def topology
  @topology
end

Instance Method Details

#abspath(rootfs_path) ⇒ Object



37
38
39
40
41
# File 'lib/dev-lxc/chef-cluster.rb', line 37

def abspath(rootfs_path)
  abspath = Array.new
  chef_servers.each { |cs| abspath << cs.abspath(rootfs_path) }
  abspath.compact
end

#chef_repoObject



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
# File 'lib/dev-lxc/chef-cluster.rb', line 43

def chef_repo
  case @topology
  when "open-source"
    puts "Unable to create a chef-repo for an Open Source Chef Server"
    exit 1
  when "standalone"
    chef_server = ChefServer.new(@servers.keys.first, @cluster_config)
  when "tier"
    chef_server = ChefServer.new(@bootstrap_backend, @cluster_config)
  end
  if ! chef_server.server.defined?
    puts "The '#{chef_server.server.name}' Chef Server does not exist. Please create it first."
    exit 1
  end
  puts "Creating chef-repo with pem files and knife.rb in the current directory"
  FileUtils.mkdir_p("./chef-repo/.chef")
  knife_rb = %Q(
current_dir = File.dirname(__FILE__)

chef_server_url "https://#{api_fqdn}/organizations/ponyville"

node_name "rainbowdash"
client_key "\#{current_dir}/rainbowdash.pem"

validation_client_name "ponyville-validator"
validation_key "\#{current_dir}/ponyville-validator.pem"

cookbook_path Dir.pwd + "/cookbooks"
knife[:chef_repo_path] = Dir.pwd
)
  IO.write("./chef-repo/.chef/knife.rb", knife_rb)
  if Dir.glob("#{chef_server.abspath('/root/chef-repo/.chef')}/*.pem").empty?
    puts "The pem files can not be copied because they do not exist in '#{chef_server.server.name}' Chef Server's `/root/chef-repo/.chef` directory"
  else
    FileUtils.cp( Dir.glob("#{chef_server.abspath('/root/chef-repo/.chef')}/*.pem"), "./chef-repo/.chef" )
  end
end

#chef_server_configObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dev-lxc/chef-cluster.rb', line 113

def chef_server_config
  chef_server_config = %Q(api_fqdn "#{@api_fqdn}"\n)
  if @topology == 'tier'
    chef_server_config += %Q(
topology "#{@topology}"

server "#{@bootstrap_backend}",
  :ipaddress => "#{@servers[@bootstrap_backend]["ipaddress"]}",
  :role => "backend",
  :bootstrap => true

backend_vip "#{@bootstrap_backend}",
  :ipaddress => "#{@servers[@bootstrap_backend]["ipaddress"]}"
)
    @frontends.each do |frontend_name|
      chef_server_config += %Q(
server "#{frontend_name}",
  :ipaddress => "#{@servers[frontend_name]["ipaddress"]}",
  :role => "frontend"
)
    end
  end
  return chef_server_config
end

#chef_serversObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dev-lxc/chef-cluster.rb', line 18

def chef_servers
  chef_servers = Array.new
  case @topology
  when "open-source", "standalone"
    chef_servers << ChefServer.new(@servers.keys.first, @cluster_config)
  when "tier"
    chef_servers << ChefServer.new(@bootstrap_backend, @cluster_config)
    @frontends.each do |frontend_name|
      chef_servers << ChefServer.new(frontend_name, @cluster_config)
    end
  end
  chef_servers
end

#destroyObject



95
96
97
98
# File 'lib/dev-lxc/chef-cluster.rb', line 95

def destroy
  puts "Destroying cluster"
  chef_servers.reverse_each { |cs| cs.destroy }
end

#destroy_container(type) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dev-lxc/chef-cluster.rb', line 100

def destroy_container(type)
  case type
  when :unique
    @servers.keys.each do |server_name|
      DevLXC::ChefServer.new(server_name, @cluster_config).destroy_container(:unique)
    end
  when :shared
    DevLXC::ChefServer.new(@servers.keys.first, @cluster_config).destroy_container(:shared)
  when :platform
    DevLXC::ChefServer.new(@servers.keys.first, @cluster_config).destroy_container(:platform)
  end
end

#run_command(command) ⇒ Object



81
82
83
# File 'lib/dev-lxc/chef-cluster.rb', line 81

def run_command(command)
  chef_servers.each { |cs| cs.run_command(command) }
end

#startObject



85
86
87
88
# File 'lib/dev-lxc/chef-cluster.rb', line 85

def start
  puts "Starting cluster"
  chef_servers.each { |cs| cs.start }
end

#statusObject



32
33
34
35
# File 'lib/dev-lxc/chef-cluster.rb', line 32

def status
  puts "Cluster is available at https://#{@api_fqdn}"
  chef_servers.each { |cs| cs.status }
end

#stopObject



90
91
92
93
# File 'lib/dev-lxc/chef-cluster.rb', line 90

def stop
  puts "Stopping cluster"
  chef_servers.reverse_each { |cs| cs.stop }
end