Commit b852bfbb authored by Francesc Guasch's avatar Francesc Guasch
Browse files

fix(backend): deal with conflicting ports

This problem raised when creating multiple clones
and starting them all at once.
parent 96a41f54
...@@ -2365,31 +2365,46 @@ sub _add_expose($self, $internal_port, $name, $restricted) { ...@@ -2365,31 +2365,46 @@ sub _add_expose($self, $internal_port, $name, $restricted) {
return $public_port; return $public_port;
} }
sub _open_exposed_port($self, $internal_port, $name, $restricted) { sub _set_public_port($self, $id_port, $internal_port, $name, $restricted) {
my $sth = $$CONNECTOR->dbh->prepare("SELECT id,public_port FROM domain_ports" my $public_port = $self->_vm->_new_free_port();
." WHERE id_domain=? AND internal_port=?" for (;;) {
);
$sth->execute($self->id, $internal_port);
my ($id_port, $public_port) = $sth->fetchrow();
if (!$public_port) {
$public_port = $self->_vm->_new_free_port();
if ($id_port) { if ($id_port) {
my $sth = $$CONNECTOR->dbh->prepare("UPDATE domain_ports set public_port=?" my $sth = $$CONNECTOR->dbh->prepare("UPDATE domain_ports set public_port=?"
." WHERE id_domain=? AND internal_port=?" ." WHERE id_domain=? AND internal_port=?"
); );
$sth->execute($public_port, $self->id, $internal_port); eval {
$sth->execute($public_port, $self->id, $internal_port);
};
die $@ if $@ && $@ !~ /uplicate entry/;
return $public_port if !$@;
} else { } else {
my $sth = $$CONNECTOR->dbh->prepare("INSERT INTO domain_ports " my $sth = $$CONNECTOR->dbh->prepare("INSERT INTO domain_ports "
."(id_domain, public_port, internal_port, name, restricted)" ."(id_domain, public_port, internal_port, name, restricted)"
." VALUES(?,?,?,?,?) " ." VALUES(?,?,?,?,?) "
);
$sth->execute( $self->id
,$public_port, $internal_port
,( $name or undef )
,$restricted
); );
eval {
$sth->execute( $self->id
,$public_port, $internal_port
,( $name or undef )
,$restricted
);
};
die $@ if $@ && $@ !~ /uplicate entry/;
return $public_port if !$@;
} }
$public_port += int(rand(10))+1;
} }
}
sub _open_exposed_port($self, $internal_port, $name, $restricted) {
my $sth = $$CONNECTOR->dbh->prepare("SELECT id,public_port FROM domain_ports"
." WHERE id_domain=? AND internal_port=?"
);
$sth->execute($self->id, $internal_port);
my ($id_port, $public_port) = $sth->fetchrow();
$public_port = $self->_set_public_port($id_port, $internal_port, $name, $restricted)
if !$public_port;
my $local_ip = $self->_vm->ip; my $local_ip = $self->_vm->ip;
my $internal_ip = $self->ip; my $internal_ip = $self->ip;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment