Class: Domctl::Config
- Inherits:
-
Object
- Object
- Domctl::Config
- Defined in:
- lib/domctl/config.rb
Class Method Summary collapse
- .cluster_nodes ⇒ Object
- .config_file ⇒ Object
- .create_sample_config ⇒ Object
- .each_host ⇒ Object
- .exit_if_not_defined(node) ⇒ Object
- .node_defined?(node) ⇒ Boolean
- .validate ⇒ Object
Class Method Details
.cluster_nodes ⇒ Object
3 4 5 6 |
# File 'lib/domctl/config.rb', line 3 def self.cluster_nodes yaml = YAML.load_file(self.config_file) yaml['cluster'] end |
.config_file ⇒ Object
73 74 75 |
# File 'lib/domctl/config.rb', line 73 def self.config_file "#{ENV['HOME']}/.domctlrc" end |
.create_sample_config ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/domctl/config.rb', line 26 def self.create_sample_config if not File.exist?(self.config_file) File.open(self.config_file, 'w') do |f| cfg = { 'cluster' => { 'xen0' => { 'url' => 'http://xen0.example.net:9363', 'username' => 'foo', 'password' => 'bar' }, 'xen1' => { 'url' => 'http://xen1.example.net:9363', 'username' => 'foo', 'password' => 'bar' } } } YAML.dump(cfg, f) end end end |
.each_host ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/domctl/config.rb', line 47 def self.each_host cluster_nodes.sort.each do |node, settings| begin h = Pangea::Host.connect(settings['url'], settings['username'], settings['password']) yield h rescue Pangea::LinkConnectError => e puts "Error connecting to host #{node}. Skipping." end end end |
.exit_if_not_defined(node) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/domctl/config.rb', line 58 def self.exit_if_not_defined(node) settings = Domctl::Config.cluster_nodes[node] if settings.nil? $stderr.puts "ERROR: Xen host #{node} not defined in #{Domctl::Config.config_file}" exit 1 end end |
.node_defined?(node) ⇒ Boolean
65 66 67 68 69 70 71 |
# File 'lib/domctl/config.rb', line 65 def self.node_defined?(node) settings = Domctl::Config.cluster_nodes[node] if settings.nil? return false end true end |
.validate ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/domctl/config.rb', line 8 def self.validate if not File.exist?(self.config_file) self.create_sample_config $stderr.puts $stderr.puts "domctl config file does not exist.\n" $stderr.puts "I have created an example one for you (#{config_file}). Configure it first." $stderr.puts exit 1 end yaml = YAML.load_file(self.config_file) if not yaml or yaml['cluster'].nil? or not yaml['cluster'].is_a?(Hash) $stderr.puts $stderr.puts "Invalid config file found.\n" $stderr.puts exit 1 end end |