Class: DynportTools::Services

Inherits:
Object
  • Object
show all
Defined in:
lib/dynport_tools/services.rb

Constant Summary collapse

DEFAULTS =
{
  :solr_url => "http://localhost:8983/solr/",
  :solr_data_root => "/opt/solr",
  :solr_xml => %(
    <?xml version="1.0" encoding="UTF-8" ?>
    <solr sharedLib="lib" persistent="true">
      <cores adminPath="/admin/cores">
      </cores>
    </solr>
  ).gsub(/^\s+/, "")
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#redis_config_hashObject



126
127
128
129
130
131
132
133
# File 'lib/dynport_tools/services.rb', line 126

def redis_config_hash
  { 
    :unixsocket => redis_socket_path, 
    :port => 0,
    :logfile => redis_log_path,
    :daemonize => "yes"
  }.merge(@redis_config_hash || {})
end

#redis_config_pathObject



118
119
120
# File 'lib/dynport_tools/services.rb', line 118

def redis_config_path
  "#{redis_path_prefix}.conf"
end

#redis_path_prefixObject



110
111
112
# File 'lib/dynport_tools/services.rb', line 110

def redis_path_prefix
  @redis_path_prefix or raise "redis_path_prefix not set!"
end

#solr_data_rootObject

Returns the value of attribute solr_data_root.



16
17
18
# File 'lib/dynport_tools/services.rb', line 16

def solr_data_root
  @solr_data_root
end

#solr_instance_pathObject

Returns the value of attribute solr_instance_path.



16
17
18
# File 'lib/dynport_tools/services.rb', line 16

def solr_instance_path
  @solr_instance_path
end

#solr_urlObject



70
71
72
# File 'lib/dynport_tools/services.rb', line 70

def solr_url
  @solr_url || DEFAULTS[:solr_url]
end

Instance Method Details

#bootstrap_solrObject



40
41
42
43
# File 'lib/dynport_tools/services.rb', line 40

def bootstrap_solr
  raise "#{solr_xml_path} already exists" if solr_bootstrapped?
  write_solr_xml_when_possible
end

#create_solr_core(core_name) ⇒ Object



82
83
84
85
86
# File 'lib/dynport_tools/services.rb', line 82

def create_solr_core(core_name)
  raise "please set solr_instance_path first!" if self.solr_instance_path.nil?
  raise "please set solr_data_root first!" if self.solr_data_root.nil?
  post("#{solr_url}admin/cores?action=CREATE&name=#{core_name}&instanceDir=#{solr_instance_path}&dataDir=#{solr_data_root}/#{core_name}")
end

#get(url) ⇒ Object



62
63
64
# File 'lib/dynport_tools/services.rb', line 62

def get(url)
  system_call(%(curl -s "#{url}"))
end

#head(url) ⇒ Object



56
57
58
59
60
# File 'lib/dynport_tools/services.rb', line 56

def head(url)
  if code = system_call(%(curl -s -I "#{url}" | head -n 1)).to_s.split(" ").at(1)
    code.to_i
  end
end

#post(url) ⇒ Object



66
67
68
# File 'lib/dynport_tools/services.rb', line 66

def post(url)
  system_call(%(curl -s -I -XPOST "#{url}"))
end

#redis_configObject



135
136
137
# File 'lib/dynport_tools/services.rb', line 135

def redis_config
  (redis_config_hash || {}).map { |key, value| "#{key} #{value}" if !value.nil? }.compact.join("\n")
end

#redis_log_pathObject



122
123
124
# File 'lib/dynport_tools/services.rb', line 122

def redis_log_path
  "#{redis_path_prefix}.log"
end

#redis_running?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/dynport_tools/services.rb', line 106

def redis_running?
  system_call(%(echo "info" | redis-cli -s #{redis_socket_path} 2> /dev/null | grep uptime_in_seconds)).include?("uptime_in_seconds")
end

#redis_socket_pathObject



114
115
116
# File 'lib/dynport_tools/services.rb', line 114

def redis_socket_path
  "#{redis_path_prefix}.socket"
end

#reload_all_solr_coresObject



92
93
94
95
96
# File 'lib/dynport_tools/services.rb', line 92

def reload_all_solr_cores
  solr_core_names.each do |core_name|
    reload_solr_core(core_name)
  end
end

#reload_solr_core(core_name) ⇒ Object



98
99
100
# File 'lib/dynport_tools/services.rb', line 98

def reload_solr_core(core_name)
  post("#{solr_url}admin/cores?action=RELOAD&core=#{core_name}")
end

#solr_bootstrapped?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/dynport_tools/services.rb', line 30

def solr_bootstrapped?
  File.exists?(solr_xml_path)
end

#solr_core_exists?(core_name) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/dynport_tools/services.rb', line 78

def solr_core_exists?(core_name)
  head("#{solr_url}#{core_name}/admin/") == 200
end

#solr_core_namesObject



26
27
28
# File 'lib/dynport_tools/services.rb', line 26

def solr_core_names
  get(solr_url).to_s.scan(/a href=\"(.*?)\/admin/).flatten
end

#solr_running?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/dynport_tools/services.rb', line 74

def solr_running?
  head(self.solr_url) == 200
end

#solr_xml_pathObject



22
23
24
# File 'lib/dynport_tools/services.rb', line 22

def solr_xml_path
  "#{solr_data_root}/solr.xml"
end

#start_redisObject



146
147
148
149
# File 'lib/dynport_tools/services.rb', line 146

def start_redis
  write_redis_config
  system_call("redis-server #{redis_config_path}")
end

#start_solrObject



34
35
36
37
38
# File 'lib/dynport_tools/services.rb', line 34

def start_solr
  raise "solr already running" if solr_running?
  raise "solr must be bootstrapped first" if !solr_bootstrapped?
  exec "solr #{solr_data_root} > #{solr_data_root}/solr.log 2>&1 &"
end

#system_call(cmd) ⇒ Object



151
152
153
154
# File 'lib/dynport_tools/services.rb', line 151

def system_call(cmd)
  puts "executing: #{cmd}"
  Kernel.send(:`, cmd)
end

#unload_solr_core(core_name) ⇒ Object



88
89
90
# File 'lib/dynport_tools/services.rb', line 88

def unload_solr_core(core_name)
  post("#{solr_url}admin/cores?action=UNLOAD&core=#{core_name}")
end

#write_redis_configObject



139
140
141
142
143
144
# File 'lib/dynport_tools/services.rb', line 139

def write_redis_config
  raise "please set redis_config_path first!" if redis_config_path.nil?
  File.open(redis_config_path, "w") do |f|
    f.puts(redis_config)
  end
end

#write_solr_xmlObject



50
51
52
53
54
# File 'lib/dynport_tools/services.rb', line 50

def write_solr_xml
  File.open(solr_xml_path, "w") do |f|
    f.puts(DEFAULTS[:solr_xml])
  end
end

#write_solr_xml_when_possibleObject



45
46
47
48
# File 'lib/dynport_tools/services.rb', line 45

def write_solr_xml_when_possible
  raise "please create #{solr_data_root} first" if !File.directory?(solr_data_root)
  write_solr_xml
end