Class: AWSMine::AWSMine

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_minecraft.rb,
lib/aws_minecraft/version.rb

Overview

Main class for AWS Minecraft

Constant Summary collapse

MINECRAFT_SESSION_NAME =
'minecraft'.freeze
VERSION =
'0.2.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAWSMine

Returns a new instance of AWSMine.



14
15
16
17
18
19
20
21
# File 'lib/aws_minecraft.rb', line 14

def initialize
  @aws_helper = AWSHelper.new
  @db_helper = DBHelper.new
  @upload_helper = UploadHelper.new
  @ssh_helper = SSHHelper.new
  @logger = Logger.new($stdout)
  @logger.level = Logger.const_get(MineConfig.new.loglevel)
end

Instance Attribute Details

#aws_helperObject

Returns the value of attribute aws_helper.



12
13
14
# File 'lib/aws_minecraft.rb', line 12

def aws_helper
  @aws_helper
end

#db_helperObject

Returns the value of attribute db_helper.



12
13
14
# File 'lib/aws_minecraft.rb', line 12

def db_helper
  @db_helper
end

#ssh_helperObject

Returns the value of attribute ssh_helper.



12
13
14
# File 'lib/aws_minecraft.rb', line 12

def ssh_helper
  @ssh_helper
end

#upload_helperObject

Returns the value of attribute upload_helper.



12
13
14
# File 'lib/aws_minecraft.rb', line 12

def upload_helper
  @upload_helper
end

Instance Method Details

#attach_to_serverObject



89
90
91
92
93
# File 'lib/aws_minecraft.rb', line 89

def attach_to_server
  @logger.info("Attaching to server: #{MINECRAFT_SESSION_NAME}.")
  ip, = @db_helper.instance_details
  @ssh_helper.attach_to_server(ip)
end

#create_instanceObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aws_minecraft.rb', line 23

def create_instance
  if @db_helper.instance_exists?
    ip, id = @db_helper.instance_details
    @logger.info 'Instance already exists.'
    state = @aws_helper.state(id)
    @logger.info "State is: #{state}"
    @logger.info "Public ip | id: #{ip} | #{id}"
    return
  end
  ip, id = @aws_helper.create_ec2
  @db_helper.store_instance(ip, id)
end

#init_dbObject



95
96
97
98
99
# File 'lib/aws_minecraft.rb', line 95

def init_db
  @logger.info 'Creating db.'
  @db_helper.init_db
  @logger.info 'Done.'
end

#remote_exec(cmd) ⇒ Object



109
110
111
112
# File 'lib/aws_minecraft.rb', line 109

def remote_exec(cmd)
  ip, = @db_helper.instance_details
  @ssh_helper.remote_exec(ip, cmd)
end

#sshObject



114
115
116
117
# File 'lib/aws_minecraft.rb', line 114

def ssh
  ip, = @db_helper.instance_details
  @ssh_helper.ssh(ip)
end

#start_instanceObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/aws_minecraft.rb', line 36

def start_instance
  unless @db_helper.instance_exists?
    @logger.info 'No instances found. Nothing to do.'
    return
  end
  ip, id = @db_helper.instance_details
  @logger.info("Starting instance #{ip} | #{id}.")
  new_ip = @aws_helper.start_ec2(id)
  @db_helper.update_instance(new_ip, id)
end

#start_server(name) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/aws_minecraft.rb', line 73

def start_server(name)
  @logger.info("Starting server: #{name}.")
  cmd = "cd /home/ec2-user/data && ../tmux-2.2/tmux new -d -s #{MINECRAFT_SESSION_NAME} " \
        "'echo eula=true > eula.txt && java -jar #{name} nogui'"
  @logger.info("Running command: '#{cmd}'")
  remote_exec(cmd)
  ip, = @db_helper.instance_details
  @logger.info("Server URL is: #{ip}:25565")
end

#stop_instanceObject



47
48
49
50
51
52
53
54
55
# File 'lib/aws_minecraft.rb', line 47

def stop_instance
  unless @db_helper.instance_exists?
    @logger.info 'No running instances found. Nothing to do.'
    return
  end
  ip, id = @db_helper.instance_details
  @logger.info("Stopping instance #{ip} | #{id}.")
  @aws_helper.stop_ec2(id)
end

#stop_serverObject



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

def stop_server
  @logger.info('Stopping server')
  ip, = @db_helper.instance_details
  @ssh_helper.stop_server(ip)
end

#terminate_instanceObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aws_minecraft.rb', line 57

def terminate_instance
  unless @db_helper.instance_exists?
    @logger.info 'No running instances found. Nothing to do.'
    return
  end
  @logger.info('WARNING! Terminating an instance will result in dataloss. Make ' \
               'sure everything is backed up. Do you want to continue? (Y/n):')
  answer = $stdin.gets.chomp
  return unless answer == 'Y'

  ip, id = @db_helper.instance_details
  @logger.info("Terminating instance #{ip} | #{id}.")
  @aws_helper.terminate_ec2(id)
  @db_helper.remove_instance
end

#upload_filesObject



104
105
106
107
# File 'lib/aws_minecraft.rb', line 104

def upload_files
  ip, = @db_helper.instance_details
  @upload_helper.upload_files(ip)
end

#upload_worldObject



101
102
# File 'lib/aws_minecraft.rb', line 101

def upload_world
end