-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.pl
executable file
·140 lines (125 loc) · 3.64 KB
/
manage.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use IPC::System::Simple qw(capture);
###### Settings ######
my $game = "css";
my $path_servers = "/srv/gs/$game/active";
my $start_file = "run.sh";
my $pidfile = "cstrike/srcds.pid";
my @servers = glob "$path_servers/*";
my @servers_names = map(basename($_), @servers);
my @allowed_options = ("start", "stop", "restart", "status");
sub main
{
if ($#ARGV != 1 or !(grep $_ eq $ARGV[0], @allowed_options)) {
&usage();
} else {
if ($ARGV[0] eq "start") {
if ($ARGV[1] eq "all") {
&start($ARGV[1]);
#I have read that this is highly bad practice.
} elsif (grep $_ eq $ARGV[1], @servers_names) {
&start($ARGV[1]);
} else {
print "You gave me a wrong name you dick\n";
&usage();
}
}
if ($ARGV[0] eq "stop") {
if ($ARGV[1] eq "all") {
&stop($ARGV[1]);
#I have read that this is highly bad practice.
} elsif (grep $_ eq $ARGV[1], @servers_names) {
&stop($ARGV[1]);
} else {
print "You gave me a wrong name you dick\n";
&usage();
}
}
if ($ARGV[0] eq "restart") {
if ($ARGV[1] eq "all") {
&restart($ARGV[1]);
#I have read that this is highly bad practice.
} elsif (grep $_ eq $ARGV[1], @servers_names) {
&restart($ARGV[1]);
} else {
print "You gave me a wrong name you dick\n";
&usage();
}
}
if ($ARGV[0] eq "status") {
if ($ARGV[1] eq "all") {
&status($ARGV[1]);
#I have read that this is highly bad practice.
} elsif (grep $_ eq $ARGV[1], @servers_names) {
&status($ARGV[1]);
} else {
print "You gave me a wrong name you dick\n";
&usage();
}
}
}
}
sub start
{
my $arg = shift;
if ($arg eq "all") {
print "Make initial tmux session for servers\n";
system("tmux new-session -s $game -d");
sleep(2);
foreach my $server (@servers) {
my $name = basename($server);
print "Starting $name\n";
system("tmux new-window -a -n $name -t $game -P \"cd $server; ./$start_file\"");
}
print "All servers are started\n";
} else {
print "Starting $arg\n";
system("tmux new-window -a -n $arg -t $game -P \"cd $path_servers/$arg; ./$start_file\"");
print "Started $arg\n";
}
}
sub stop
{
my $arg = shift;
if ($arg eq "all") {
print "Killing all servers!\n";
system("tmux kill-session -t $game");
} else {
print "Stoping $arg\n";
open my $pfile, "<$path_servers/$arg/$pidfile" or die "PID file not found";
my $pid = <$pfile>;
chomp $pid;
close $pfile;
my $ppid = capture("ps -p $pid -o ppid=");
system("kill $ppid");
print "Stopped $arg\n";
}
}
sub restart
{
my $arg = shift;
if ($arg eq "all") {
print "Restarting all servers!\n";
&stop($arg);
&start($arg);
print "All servers are now restarted\n";
} else {
print "Restarting $arg\n";
&stop($arg);
&start($arg);
print "Restarted $arg\n";
}
}
sub status
{
#implement smart cool shit
print "NOT YET IMPLEMENTED EXCEPTION LOL\n";
}
sub usage
{
print "Usage: manage.pl [start | stop | restart | status] [all | servername]\n"
}
&main()