Class: VCAP::Services::Base::NodeBin

Inherits:
Object
  • Object
show all
Defined in:
lib/base/node_bin.rb

Defined Under Namespace

Modules: Boolean

Instance Method Summary collapse

Instance Method Details

#parse_property(hash, key, type, options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/base/node_bin.rb', line 139

def parse_property(hash, key, type, options = {})
  obj = hash[key]
  if obj.nil?
    raise "Missing required option: #{key}" unless options[:optional]
    options[:default]
  elsif type == Range
    raise "Invalid Range object: #{obj}" unless obj.kind_of?(Hash)
    first, last = obj["first"], obj["last"]
    raise "Invalid Range object: #{obj}" unless first.kind_of?(Integer) and last.kind_of?(Integer)
    Range.new(first, last)
  else
    raise "Invalid #{type} object: #{obj}" unless obj.kind_of?(type)
    obj
  end
end

#shutdown(node) ⇒ Object



132
133
134
135
136
137
# File 'lib/base/node_bin.rb', line 132

def shutdown(node)
  @logger.info("Begin to shutdown node")
  node.shutdown
  @logger.info("End to shutdown node")
  EM.stop
end

#startObject



31
32
33
34
35
36
37
38
39
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
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
125
126
127
128
129
130
# File 'lib/base/node_bin.rb', line 31

def start
  config_file = default_config_file

  OptionParser.new do |opts|
    opts.banner = "Usage: #{$0.split(/\//)[-1]} [options]"
    opts.on("-c", "--config [ARG]", "Configuration File") do |opt|
      config_file = opt
    end
    opts.on("-h", "--help", "Help") do
      puts opts
      exit
    end
  end.parse!

  begin
    config = YAML.load_file(config_file)
  rescue => e
    puts "Could not read configuration file:  #{e}"
    exit
  end

  options = {
    :index => parse_property(config, "index", Integer, :optional => true),
    :plan => parse_property(config, "plan", String, :optional => true, :default => "free"),
    :capacity => parse_property(config, "capacity", Integer, :optional => true, :default => 200),
    :ip_route => parse_property(config, "ip_route", String, :optional => true),
    :node_id => parse_property(config, "node_id", String),
    :z_interval => parse_property(config, "z_interval", Integer, :optional => true),
    :mbus => parse_property(config, "mbus", String),
    :local_db => parse_property(config, "local_db", String),
    :migration_nfs => parse_property(config, "migration_nfs", String, :optional => true),
    :max_nats_payload => parse_property(config, "max_nats_payload", Integer, :optional => true),
    :fqdn_hosts => parse_property(config, "fqdn_hosts", Boolean, :optional => true, :default => false),
    :op_time_limit => parse_property(config, "op_time_limit", Integer, :optional => true, :default => 6),
    :supported_versions => parse_property(config, "supported_versions", Array),
    :default_version => parse_property(config, "default_version", String),
    :max_clients => parse_property(config, "max_clients", Integer, :optional => true),
    :database_lock_file => parse_property(config, "database_lock_file", String, :optional => true, :default => "/var/vcap/sys/run/LOCK"),
    :disabled_file => parse_property(config, "disabled_file", String, :optional => true, :default => "/var/vcap/store/DISABLED"),
    # Wardenized service configuration
    :base_dir => parse_property(config, "base_dir", String, :optional => true),
    :service_log_dir => parse_property(config, "service_log_dir", String, :optional => true),
    :service_common_dir => parse_property(config, "service_common_dir", String, :optional => true, :default => "/var/vcap/store/common"),
    :service_bin_dir => parse_property(config, "service_bin_dir", Hash, :optional => true),
    :image_dir => parse_property(config, "image_dir", String, :optional => true),
    :port_range => parse_property(config, "port_range", Range, :optional => true),
    :filesystem_quota => parse_property(config, "filesystem_quota", Boolean, :optional => true, :default => false),
    :service_start_timeout => parse_property(config, "service_start_timeout", Integer, :optional => true, :default => 3),
    :service_status_timeout => parse_property(config, "service_status_timeout", Integer, :optional => true, :default => 3),
    :max_memory => parse_property(config, "max_memory", Numeric, :optional => true),
    :memory_overhead => parse_property(config, "memory_overhead", Numeric, :optional => true, :default => 0.0),
    :max_disk => parse_property(config, "max_disk", Numeric, :optional => true, :default => 128.0),
    :disk_overhead => parse_property(config, "disk_overhead", Numeric, :optional => true, :default => 0.0),
    :m_interval => parse_property(config, "m_interval", Integer, :optional => true, :default => 10),
    :m_actions => parse_property(config, "m_actions", Array, :optional => true, :default => []),
    :m_failed_times => parse_property(config, "m_failed_times", Integer, :optional => true, :default => 3),
    :warden_socket_path => parse_property(config, "warden_socket_path", String, :optional => true),
  }
  # Workaround for services that support running the service both inside and outside warden
  use_warden = parse_property(config, "use_warden", Boolean, :optional => true, :default => false)
  if use_warden
    warden_config = parse_property(config, "warden", Hash, :optional => true)
    options[:service_log_dir] = parse_property(warden_config, "service_log_dir", String)
    options[:service_common_dir] = parse_property(warden_config, "service_common_dir", String, :optional => true, :default => "/var/vcap/store/common")
    options[:service_bin_dir] = parse_property(warden_config, "service_bin_dir", Hash, :optional => true)
    options[:port_range] = parse_property(warden_config, "port_range", Range)
    options[:image_dir] = parse_property(warden_config, "image_dir", String)
    options[:filesystem_quota] = parse_property(warden_config, "filesystem_quota", Boolean, :optional => true, :default => false)
    options[:service_start_timeout] = parse_property(warden_config, "service_start_timeout", Integer, :optional => true, :default => 3)
    options[:service_status_timeout] = parse_property(warden_config, "service_status_timeout", Integer, :optional => true, :default => 3)
  end

  logging_config = Steno::Config.from_hash(config["logging"])
  Steno.init(logging_config)
  # Use the node id for logger identity name.
  options[:logger] = Steno.logger(options[:node_id])
  @logger = options[:logger]

  options = additional_config(options, config)

  EM.error_handler do |ex|
    @logger.fatal("#{ex} #{ex.backtrace.join("|")}")
    exit
  end

  pid_file = parse_property(config, "pid", String)
  begin
    FileUtils.mkdir_p(File.dirname(pid_file))
  rescue => e
    @logger.fatal "Can't create pid directory, exiting: #{e}"
    exit
  end
  File.open(pid_file, 'w') { |f| f.puts "#{Process.pid}" }

  EM.run do
    node = node_class.new(options)
    trap("INT") {shutdown(node)}
    trap("TERM") {shutdown(node)}
  end
end