Class: Supernova::Solr::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/supernova/solr/cli.rb

Constant Summary collapse

SOLR_URL =
"http://localhost:8983/solr"
SOLR_VERSION =
"3.5.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



12
13
14
15
# File 'lib/supernova/solr/cli.rb', line 12

def initialize(args)
  self.options = {}
  opts.parse(args)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/supernova/solr/cli.rb', line 10

def options
  @options
end

Instance Method Details

#commitObject



91
92
93
# File 'lib/supernova/solr/cli.rb', line 91

def commit
  handle_response core.commit
end

#coreObject



75
76
77
# File 'lib/supernova/solr/cli.rb', line 75

def core
  Supernova::Solr::Core.new(solr_url, core_name)
end

#core_nameObject



166
167
168
# File 'lib/supernova/solr/cli.rb', line 166

def core_name
  options[:core]
end

#createObject



103
104
105
# File 'lib/supernova/solr/cli.rb', line 103

def create
  handle_response core.create(options[:data], options[:instance])
end

#executeObject



83
84
85
86
87
88
89
# File 'lib/supernova/solr/cli.rb', line 83

def execute
  if action = options[:action]
    send(action)
  else
    puts opts
  end
end

#get_solr_pidObject



147
148
149
150
151
152
# File 'lib/supernova/solr/cli.rb', line 147

def get_solr_pid
  cmd = %(ps ax | grep "\\-Dsolr" | grep -v "grep" | grep -v " cd ")
  if pid_s = `#{cmd}`[/^(\d+)/, 1]
    pid_s.to_i
  end
end

#handle_response(response) ⇒ Object



135
136
137
# File 'lib/supernova/solr/cli.rb', line 135

def handle_response(response)
  puts response.to_json
end

#listObject



107
108
109
110
111
# File 'lib/supernova/solr/cli.rb', line 107

def list
  server.core_names.sort.each do |name|
    puts name
  end
end

#optimizeObject



95
96
97
# File 'lib/supernova/solr/cli.rb', line 95

def optimize
  handle_response core.optimize
end

#optsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/supernova/solr/cli.rb', line 17

def opts
  OptionParser.new do |o|
    o.on("--core CORE", String) do |core_name|
      self.options[:core] = core_name
    end

    o.on("--server SERVER", String) do |server_name|
      self.options[:server] = server_name
    end

    o.separator ""
    o.separator "Server ACTIONS"


    o.on("--start-server", "Start SOLR server") do |solr_bin|
      self.options[:action] = :start
    end

    o.on("--server-status", "SOLR server status") do |solr_bin|
      self.options[:action] = :server_status
    end

    o.on("--stop-server", "Stop SOLR server") do |solr_bin|
      self.options[:action] = :stop
    end

    o.separator ""
    o.separator "Core ACTIONS"

    o.on("--list-cores", "List Cores") do
      self.options[:action] = :list
    end

    o.on("--create", "Create Core") do
      self.options[:action] = :create
    end

    o.on("--commit", "Commit Core") do
      self.options[:action] = :commit
    end

    o.on("--optimize", "Optimize Core") do
      self.options[:action] = :optimize
    end

    o.on("--unload", "Unload Core") do
      self.options[:action] = :unload
    end

    o.separator ""
    o.separator "OPTIONS"

    o.on("--solr-bin SOLR_BIN", "Path to SOLR jarfile") do |solr_bin|
      self.options[:solr_bin] = solr_bin
    end
  end
end

#serverObject



79
80
81
# File 'lib/supernova/solr/cli.rb', line 79

def server
  Supernova::Solr::Server.new(solr_url)
end

#server_statusObject



118
119
120
121
122
123
124
# File 'lib/supernova/solr/cli.rb', line 118

def server_status
  if solr_pid
    puts "SOLR running with pid #{solr_pid}"
  else
    puts "SOLR NOT running"
  end
end

#solr_bin_pathObject



158
159
160
# File 'lib/supernova/solr/cli.rb', line 158

def solr_bin_path
  options[:solr_bin] || "/usr/local/Cellar/solr/#{SOLR_VERSION}/libexec/example/start.jar"
end

#solr_homeObject



154
155
156
# File 'lib/supernova/solr/cli.rb', line 154

def solr_home
  "/opt/solr"
end

#solr_pidObject



143
144
145
# File 'lib/supernova/solr/cli.rb', line 143

def solr_pid
  @solr_pid ||= get_solr_pid
end

#solr_urlObject



162
163
164
# File 'lib/supernova/solr/cli.rb', line 162

def solr_url
  options[:server] || SOLR_URL
end

#startObject



113
114
115
116
# File 'lib/supernova/solr/cli.rb', line 113

def start
  puts "starting SOLR"
  exec start_solr_cmd
end

#start_solr_cmdObject



139
140
141
# File 'lib/supernova/solr/cli.rb', line 139

def start_solr_cmd
  "cd #{File.dirname(solr_bin_path)} && /usr/bin/env java -Dsolr.solr.home=#{solr_home} -jar #{File.basename(solr_bin_path)} > /opt/solr/solr.log 2>&1 &"
end

#stopObject



126
127
128
129
130
131
132
133
# File 'lib/supernova/solr/cli.rb', line 126

def stop
  if solr_pid
    system("kill #{solr_pid}")
    puts "solr killed"
  else
    puts "solr not running"
  end
end

#unloadObject



99
100
101
# File 'lib/supernova/solr/cli.rb', line 99

def unload
  handle_response core
end