Class: Mclaunch::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/mclaunch/runtime.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Runtime

Returns a new instance of Runtime.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mclaunch/runtime.rb', line 9

def initialize(options=nil)
  @options = options
  @start_cmd = "java -Xms#{@options[:server_min_heap_memory]} -Xmx#{@options[:server_max_heap_memory]} #{@options[:server_additional_options]} -jar #{@options[:server_executable]}"
  @path = options[:server_runtime_directory]
  @quiet = @options[:quiet]
  @debug = @options[:debug]
  @multiplexer = @options[:server_terminal_multiplexer]
  @multiplexer_session_name = @options[:server_terminal_multiplexer_name]
  @multiplexer_session = @options[:server_terminal_multiplexer_session]
  @tools = nil
end

Instance Method Details

#create_tmux_session(name) ⇒ Object

Create a tmux session for the server to run in



97
98
99
100
101
102
103
104
# File 'lib/mclaunch/runtime.rb', line 97

def create_tmux_session(name)
  puts "connecting to or creating a new #{@multiplexer} session now" if @debug
  `tmux -S #{@multiplexer_session} has-session -t #{name} > /dev/null 2>&1 || tmux -S #{@multiplexer_session} new-session -s #{name} -d` if @multiplexer.eql?("tmux")
  `ps ax | grep -v grep | grep SCREEN | grep craftbukkit 2>&1 || screen -dmS #{name}` if @multiplexer.eql?("screen")
  puts "multiplexer session should be started now" if @debug
  sleep 0.1
  File.chmod(01000, @multiplexer_session) if @Multiplexer.eql?("tmux")
end

#get_server_propertiesObject

Get server.properties file from server runtime directory



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mclaunch/runtime.rb', line 129

def get_server_properties
  props = Hash[File.read("#{@options[:server_runtime_directory]}server.properties").split("\n").map{|i|i.split('=') unless i =~ /^#.*$/}]
  if @debug
    puts "---"
    puts "Server properties: "
    puts
    props.each do |k,v|
      puts "\t#{k}=#{v}"
    end
    puts "---"
    puts
  end
  props
end

#haltObject

halt



57
58
59
60
61
62
63
# File 'lib/mclaunch/runtime.rb', line 57

def halt
  puts "halting server now." unless @quiet
  stop
  log_rotate
  kill_tmux_session(@multiplexer_session_name)
  puts "server haltet." unless @quiet
end

#is_running?Boolean

check is server is running

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/mclaunch/runtime.rb', line 77

def is_running?
  puts "check if server is running" if @debug
  return true if `ps ax | grep -v grep | egrep "(minecraft|craftbukkit).jar" | wc -l`.to_i > 0
end

#kill_tmux_session(name) ⇒ Object

Kill a tmux session



107
108
109
110
111
112
113
114
115
116
# File 'lib/mclaunch/runtime.rb', line 107

def kill_tmux_session(name)
  puts "killing #{@multiplexer} sessinon now" if @debug
  case @multiplexer
  when "screen"
    send_command("exit") unless is_running?
  when "tmux"
    `tmux -S #{@multiplexer_session} kill-session -t #{@multiplexer_session_name}`
  end
  halt if is_running?
end

#log_rotateObject

Rotate the server log file



119
120
121
122
123
124
125
126
# File 'lib/mclaunch/runtime.rb', line 119

def log_rotate
  puts "Rotating logfile now" if @debug
  options = {
    :gzip => true,
    :count => 5,
  }
  LogRotate.rotate_file("/home/odie/bukkit/server/server.log", options)
end

#restartObject

end halt



65
66
67
68
69
70
71
72
73
74
# File 'lib/mclaunch/runtime.rb', line 65

def restart
  puts "restarting server now." unless @quiet
  stop
  start
  if is_running?
    puts "server restart complete" unless @quiet
  else
    run
  end
end

#runObject

Run server



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mclaunch/runtime.rb', line 27

def run
  puts "starting server now." unless @quiet
  if @debug
    puts "Command: #{@start_cmd}"
    puts "Multiplexer: #{@multiplexer}"
  end
  Dir.chdir(@path)
  create_tmux_session(@multiplexer_session_name)
  start
  sleep 1
  if is_running?
    puts "server is running now." unless @quiet
  else
      run
  end
end

#send_command(cmd) ⇒ Object

Send a command to a running server



83
84
85
86
87
# File 'lib/mclaunch/runtime.rb', line 83

def send_command(cmd)
  puts "sending \"#{cmd}\" to #{@multiplexer} session \"#{@multiplexer_session_name}\"" if @debug
  `tmux -S #{@multiplexer_session} send -t #{@multiplexer_session_name} "#{cmd}" ENTER` if @multiplexer.eql?("tmux")
  `screen -p 0 -S #{@multiplexer_session_name} -X eval 'stuff \"#{cmd}\"'\15` if @multiplexer.eql?("screen")
end

#send_message(msg) ⇒ Object

Send a chat message as a server say command



90
91
92
93
94
# File 'lib/mclaunch/runtime.rb', line 90

def send_message(msg)
  msg = "say #{msg}"
  puts "sending \"#{msg}\" as server chat message" if @debug
  send_command(msg)
end

#set_options(options) ⇒ Object

setter for @options



22
23
24
# File 'lib/mclaunch/runtime.rb', line 22

def set_options(options)
  @options = options
end

#startObject

start server



45
46
47
48
# File 'lib/mclaunch/runtime.rb', line 45

def start
  @tools.worlds_create_symlinks
  send_command(@start_cmd)
end

#stopObject

stop server



51
52
53
54
# File 'lib/mclaunch/runtime.rb', line 51

def stop
  send_command("stop")
  sleep 5
end

#tools(tools = nil) ⇒ Object

end get_server_properties



144
145
146
147
# File 'lib/mclaunch/runtime.rb', line 144

def tools(tools=nil)
  @tools = tools
  @toos unless @tools
end