World setup and different user support

This commit is contained in:
Jamie Cameron
2013-01-02 23:27:51 -08:00
parent 1cbe43e60f
commit 1def663cd0
7 changed files with 73 additions and 8 deletions

View File

@@ -9,13 +9,28 @@ our $server_jar_url;
our $progress_callback_url;
&ReadParse();
# Validate inputs
if ($in{'new'}) {
$in{'dir'} =~ /^\/\S+$/ || &error($text{'download_edir'});
getpwnam($in{'user'}) || &error($text{'download_euser'});
}
&ui_print_unbuffered_header(undef, $text{'download_title'}, "");
if ($in{'new'} && -d $config{'minecraft_dir'}) {
if ($in{'new'}) {
# Save the config
$config{'minecraft_dir'} = $in{'dir'};
$config{'unix_user'} = $in{'user'};
&save_module_config(\%config);
}
if ($in{'new'} && !-d $config{'minecraft_dir'}) {
# Create install dir
print &text('download_mkdir', $config{'minecraft_dir'}),"<p>\n";
&make_dir($config{'minecraft_dir'}, 0755) ||
&error($text{'download_emkdir'});
&set_ownership_permissions($config{'unix_user'}, undef, 0755,
$config{'minecraft_dir'});
}
# Download to temp file
@@ -35,8 +50,14 @@ if ($old_md5 eq $new_md5) {
}
else {
&copy_source_dest($temp, $jar);
&set_ownership_permissions($config{'unix_user'}, undef, undef, $jar);
print &text('download_done', $jar),"<p>\n";
if ($in{'new'}) {
print &text('download_done2', $jar),"<p>\n";
}
else {
print &text('download_done', $jar),"<p>\n";
}
if ($in{'new'}) {
print $text{'download_start'},"<p>\n";
print &ui_form_start("start.cgi");

View File

@@ -23,7 +23,7 @@ else {
}
print &ui_form_start("save_world.cgi", "form-data");
print &ui_table_start($text{'world_header'}, "width=50%", 2);
print &ui_table_start($text{'world_header'}, undef, 2);
print &ui_hidden("new", $in{'new'});
if ($in{'new'}) {

View File

@@ -22,7 +22,13 @@ elsif ($err) {
if (&has_command($config{'java_cmd'})) {
print &ui_form_start("download.cgi");
print &ui_hidden("new", 1);
print &text('index_offer', $config{'minecraft_dir'}),"<p>\n";
print $text{'index_offer'},"<p>\n";
print &ui_table_start(undef, undef, 2);
print &ui_table_row($text{'index_dir'},
&ui_textbox("dir", $config{'minecraft_dir'}, 40));
print &ui_table_row($text{'index_user'},
&ui_user_textbox("user", "nobody"));
print &ui_table_end();
print &ui_form_end([ [ undef, $text{'index_install'} ] ]);
}
else {

View File

@@ -2,7 +2,9 @@ index_title=Minecraft Server
index_cerr=An error was detected with your Minecraft server : $1. You may need to adjust the <a href='$2'>module configuration</a>.
index_cerr2=The Minecraft server does not appear to be installed on your system. If it is already running, you will need to adjust the <a href='$2'>module configuration</a> to use the correct paths.
index_nojava=Also, the Java command at <tt>$1</tt> needed to run Minecraft was not found.
index_offer=Alternatively, Webmin can automatically download and install the Minecraft server for you, into the directory <tt>$1</tt>.
index_offer=Alternatively, Webmin can automatically download and install the Minecraft server for you.
index_dir=Install into directory
index_user=Run as Unix user
index_install=Download and Install
index_stop=Stop Minecraft Server
index_stopdesc=Click this button to stop the running Minecraft server. All clients will be immediately disconnected.
@@ -243,6 +245,9 @@ download_restart=The new version will only be used when the server is restarted.
download_start=The server must be started before it can be used.
download_mkdir=Creating base directory $1 ..
download_emkdir=Directory creation failed!
download_err=Failed to setup Minecraft server
download_edir=Missing or non-absolute install directory
download_euser=User to run as does not exist
log_conf=Changed server configuration
log_stop=Stopped Minecraft server

View File

@@ -40,7 +40,9 @@ if (@worlds) {
print &ui_columns_end();
print &ui_links_row(\@links);
print &ui_form_end([ [ undef, $text{'worlds_change'} ],
[ 'apply', $text{'worlds_change2'} ] ]);
&is_minecraft_server_running() ?
( [ 'apply', $text{'worlds_change2'} ] ) :
( ) ]);
}
else {
print "<b>$text{'worlds_none'}</b><p>\n";

View File

@@ -1,8 +1,8 @@
# Functions for editing the minecraft config
#
# XXX plugins?
# XXX run as Unix user
# XXX initial setup mode
# XXX which user to write PID as?
# XXX how to edit init script java args
BEGIN { push(@INC, ".."); };
use strict;
@@ -31,6 +31,23 @@ my $jar = $config{'minecraft_jar'} ||
return undef;
}
# is_minecraft_port_in_use()
# If any server is using the default Minecraft port or looks like it is running
# minecraft_server.jar, return the PID.
sub is_minecraft_port_in_use
{
&foreign_require("proc");
my ($pid) = &proc::find_socket_processes("tcp:25565");
return $pid if ($pid);
my @procs = &proc::list_processes();
foreach my $p (@procs) {
if ($p->{'args'} =~ /^java.*minecraft_server.jar/) {
return $p->{'pid'};
}
}
return undef;
}
# is_minecraft_server_running()
# If the minecraft server is running, return the PID
sub is_minecraft_server_running
@@ -142,6 +159,9 @@ my $rv = "(test -e ".$ififo." || mkfifo ".$ififo.") ; ".
" -jar ".$jar." nogui ".
$config{'jar_args'}." ".
">> server.out 2>&1 )";
if ($config{'unix_user'} ne 'root') {
$rv = &command_as_user($config{'unix_user'}, 0, $rv);
}
return $rv;
}
@@ -174,6 +194,7 @@ my $fh = "PID";
&open_tempfile($fh, ">$pidfile");
&print_tempfile($fh, $pid."\n");
&close_tempfile($fh);
&set_ownership_permissions($config{'unix_user'}, undef, undef, $pidfile);
return undef;
}
@@ -392,6 +413,8 @@ my ($users) = @_;
my $lref = &read_file_lines(&get_whitelist_file());
@$lref = @$users;
&flush_file_lines(&get_whitelist_file());
&set_ownership_permissions($config{'unix_user'}, undef, undef,
&get_whitelist_file());
}
sub get_op_file
@@ -415,6 +438,8 @@ my ($users) = @_;
my $lref = &read_file_lines(&get_op_file());
@$lref = @$users;
&flush_file_lines(&get_op_file());
&set_ownership_permissions($config{'unix_user'}, undef, undef,
&get_op_file());
}
# list_worlds()

View File

@@ -31,6 +31,8 @@ if ($in{'new'}) {
my $fh = "EMPTY";
&open_tempfile($fh, ">$dir/level.dat", 0, 1);
&close_tempfile($fh);
&set_ownership_permissions($config{'unix_user'}, undef, 0755,
$dir, "$dir/level.dat");
}
elsif ($in{'src'} == 1) {
# Clone existing world
@@ -42,6 +44,8 @@ if ($in{'new'}) {
}
&copy_source_dest("$config{'minecraft_dir'}/$in{'world'}",
$dir);
&system_logged(
"chown -R $config{'unix_user'} ".quotemeta($dir));
if (&is_minecraft_server_running() &&
$def eq $in{'world'}) {
# Re-enable world writes
@@ -70,6 +74,8 @@ if ($in{'new'}) {
&error($text{'world_edat'});
my $copysrc = $1;
&copy_source_dest($copysrc, $dir);
&system_logged(
"chown -R $config{'unix_user'} ".quotemeta($dir));
}
&redirect("list_worlds.cgi");
}