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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#!/usr/bin/perl
# The next line is here to help me find the directory of the script
# if you have a better method, let me know.
use FindBin qw($RealBin);
# Next line adds the script directory to the lib path
use lib "$RealBin";
use strict;
use warnings;
use Proc::Spawn;
use Functions::Shell qw(check_daemon check_command start_daemon get_daemon_output exit_shell);
use Functions::ShellSocket qw(connect_shell_socket receive_shell_msg send_shell_msg close_shell_socket term_shell_ctxt bind_shell_socket);
use Getopt::Long;
my $command = undef;
my $wait_daemon = undef;
my $setup = undef;
my $help_message = undef;
my $interactive = 1;
# Variables to store daemon ip and port on distributed test
my $daemon_ip = "127.0.0.1";
my $daemon_port = "6650";
my $daemon_path = "$daemon_ip:$daemon_port";
# Next variables is used to control when to skip a loop cicle
my $continue = undef;
# Variables for socket managing
my $socket = undef;
my $ctxt = undef;
my $shell_socket = undef;
my $shell_ctxt = undef;
my $ret = GetOptions ( "c|command=s" => \$command,
"wait-daemon" => \$wait_daemon,
"setup" => \$setup,
"h|help" => \$help_message );
if (defined($help_message)) {
my $help = <<'END';
Usage: cvmfs-test [--i] [--setup] [--wait-daemon] [--c command]
-h|--help Print this help and exit.
--i Start the interactive shell. Default.
--setup Setup the environment.
--wait-daemon Wait for the daemon to send its ip.
--c command Executes command and exit.
END
print $help;
exit_shell($socket, $ctxt);
}
if (defined($setup)) {
check_command(undef, undef, undef, 'setup');
check_command(undef, undef, undef, 'fixperm');
exit_shell($socket, $ctxt);
}
if (defined($wait_daemon)) {
# Opening the socket to wait for the daemon to send its ip
($shell_socket, $shell_ctxt) = bind_shell_socket();
# Wait fo the daemon message
my $answer = receive_shell_msg($shell_socket);
# Splitting the message and assigning it to variables
my @ip_port = split /:/, $answer;
($daemon_ip, $daemon_port) = ($ip_port[0], $ip_port[1]);
$daemon_path = "$daemon_ip:$daemon_port";
# Closing the socket. The same socket will be used to receive tests output.
$socket = close_shell_socket($socket);
$ctxt = term_shell_ctxt($ctxt);
}
if (defined($command)) {
if (!check_daemon()) {
($socket, $ctxt) = start_daemon($daemon_path);
}
else {
($socket, $ctxt) = connect_shell_socket($daemon_path);
}
my ($continue, $socket, $ctxt) = check_command($socket, $ctxt, $daemon_path, $command);
unless ($continue) {
send_shell_msg($socket, $command);
get_daemon_output($socket, $ctxt);
}
exit_shell($socket, $ctxt, 1);
}
if ($interactive) {
# A simple welcome.
print '#'x80 . "\n";
print "Welcome in the interactive shell of the CernVM-FS testing system.\n";
print "Type 'help' for a list of available commands.\n";
print '#'x80 . "\n";
# If the daemon is not running, the shell will ask the use if run it
if (!check_daemon()) {
print 'The daemon is not running. Would you like to run it now? [Y/n]';
my $answer = <STDIN>;
if($answer eq "\n" or $answer eq "Y\n" or $answer eq "y\n"){
($socket, $ctxt) = start_daemon($daemon_path);
}
}
else {
# Starting the socket to communicate with the server
($socket, $ctxt) = connect_shell_socket($daemon_path);
}
# Infinite loop for the shell. It will switch between two shells: the first one
# is the one used when the shell is connected to the daemon. The second one is used
# when the daemon is not running and will have less options available.
# Both shell terminates on exit.
while(1){
# This is the first shell, the one used to communicate with the daemon
while(check_daemon()){
print '-> ';
# Reading an input line.
my $line = STDIN->getline;
chomp($line);
# Checking again if the daemon is running, maybe something killed it.
unless (check_daemon()) {
print "Daemon isn't running anymore. Check logs.\n";
$socket = close_shell_socket($socket);
$ctxt = term_shell_ctxt($ctxt);
next;
}
# Checking if the command refer to the shell and not to the daemon
($continue, $socket, $ctxt) = check_command($socket, $ctxt, $daemon_path, $line);
# If the command was already executed, passing to the next while cicle
next if $continue;
# Send the command through the socket
send_shell_msg($socket, $line);
# Get answer from the daemon
get_daemon_output($socket, $ctxt);
}
# This is the second shell, use when the daemon is closed
while(!check_daemon()){
print '(Daemon not running) -> ';
# Reading an input line.
my $line = STDIN->getline;
chomp($line);
# Launching the command
($continue, $socket, $ctxt) = check_command($socket, $ctxt, $daemon_path, $line);
}
}
}
|