-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.pl
executable file
·218 lines (165 loc) · 4.13 KB
/
run.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env perl
use YAML;
use Data::Dumper;
use Rex -base;
use Rex::Commands::Virtualization;
use Rex::Commands::SimpleCheck;
use Cwd 'getcwd';
use Mojo::UserAgent;
set virtualization => "LibVirt";
our $RETVAL = 0;
my $ua = Mojo::UserAgent->new;
$ua->request_timeout(300);
$ua->inactivity_timeout(300);
$ua->connect_timeout(300);
$::QUIET = 1;
my $starttime;
my $phase;
our $branch = $ENV{REX_BRANCH} || "master";
our $git_repo = $ENV{GIT_REPO} || "git\@github.com:RexOps/Rex.git";
start_phase('Initializing');
my $yaml =
eval { local ( @ARGV, $/ ) = ( $ENV{HOME} . "/.build_config" ); <>; };
$yaml .= "\n";
our $config = Load($yaml);
my $base_vm = $ARGV[0];
my $time = time;
my $new_vm = "${base_vm}-test-$time-$$";
$new_vm =~ s/:/_/gms;
#Rex::connect( %{$config} );
&end_phase;
my $vm_info;
start_phase('Creating test VM');
my $con_str =
"http://$config->{jobcontrol}->{user}:$config->{jobcontrol}->{password}\@"
. "$config->{jobcontrol}->{host}:$config->{jobcontrol}->{port}"
. "/api/1.0/project/5bde00a59817c6e3e6e79cc4ad8a514a/node";
our ($vm_id, $ip) = create_vm($new_vm, $base_vm);
&end_phase;
if ( !$ip ) {
die "Error couldn't get IP address.";
}
start_phase("Waiting for VM SSH port wakeup on $ip");
while ( !is_port_open( $ip, 22 ) ) {
sleep 1;
}
&end_phase;
our ( $user, $pass );
if ( exists $ENV{use_sudo} ) {
$user = $config->{box}->{sudo}->{user};
$pass = $config->{box}->{sudo}->{password};
}
else {
$user = $config->{box}->{default}->{user};
$pass = $config->{box}->{default}->{password};
}
eval {
do "run.tests.pl";
1;
} or do {
$RETVAL = 1;
};
start_phase('Cleaning up VM');
#$ua->delete("$con_str/$vm_id");
remove_vm($vm_id);
#vm destroy => $new_vm;
#vm delete => $new_vm;
#rm "/ram/$new_vm.img";
# fix for #6
#run "virsh vol-delete --pool default $new_vm.img";
&end_phase;
exit $RETVAL;
sub get_random {
my $count = shift;
my @chars = @_;
srand();
my $ret = "";
for ( 1 .. $count ) {
$ret .= $chars[ int( rand( scalar(@chars) - 1 ) ) ];
}
return $ret;
}
sub start_phase {
$phase = shift;
$starttime = time;
local $| = 1;
printf '%-70s', $phase;
}
sub end_phase {
printf "%4u s\n", scalar( time - $starttime );
}
sub create_vm {
my ($new_vm, $base_vm) = @_;
my ($ip, $vm_id);
if ( $ENV{use_docker} ) {
my $tx = $ua->post(
$con_str,
json => {
name => $new_vm,
type => "docker",
parent => "3a7f1fc9e58a8492fc625d8a16e85e76_c5fd214cdd0d2b3b4272e73b022ba5c2",
data => {
image => $base_vm,
host => "3a7f1fc9e58a8492fc625d8a16e85e76_1b21b0d71706897b69f108572c444d40_b0da275520918e23dd615e2a747528f1",
command => "/usr/sbin/sshd -D",
}
}
);
if ( $tx->success ) {
my $ref = $tx->res->json;
$vm_id = $ref->{id};
if ( !$vm_id ) {
die "Error creating test VM";
}
my $qtx = $ua->get("$con_str/$vm_id");
if ( $qtx->success ) {
my $qref = $qtx->res->json;
$ip = $qref->{provisioner}->[0]->{NetworkSettings}->{IPAddress};
}
else {
die "Error getting info of test VM";
}
}
else {
die "Error creating test VM";
}
}
else {
my $tx = $ua->post(
$con_str,
json => {
name => $new_vm,
type => "kvm",
parent => "3a7f1fc9e58a8492fc625d8a16e85e76_c5fd214cdd0d2b3b4272e73b022ba5c2",
data => {
image => $base_vm,
host => "3a7f1fc9e58a8492fc625d8a16e85e76_1b21b0d71706897b69f108572c444d40_b0da275520918e23dd615e2a747528f1",
}
}
);
if ( $tx->success ) {
my $ref = $tx->res->json;
$vm_id = $ref->{id};
if ( !$vm_id ) {
die "Error creating test VM";
}
my $qtx = $ua->get("$con_str/$vm_id");
if ( $qtx->success ) {
my $qref = $qtx->res->json;
$ip = $qref->{provisioner}->{network}->[0]->{ip};
}
else {
print STDERR Dumper $qtx;
die "Error getting info of test VM";
}
}
else {
die "Error creating test VM";
}
}
return ($vm_id, $ip);
}
sub remove_vm {
my ($vm_id) = @_;
$ua->delete("$con_str/$vm_id");
}