2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-29 17:23:08 +00:00

add using pipework to config ip for containers are start

This commit is contained in:
ertaozh 2016-03-05 08:12:05 -05:00
parent 86e335114d
commit 4a1d58ffe7
3 changed files with 130 additions and 17 deletions

View File

@ -2101,8 +2101,70 @@ sub getNodeNetworkCfg
return ($ip, $node, $gateway, xCAT::NetworkUtils::formatNetmask($mask,0,0));
}
#-------------------------------------------------------------------------------
=head3 getNodesNetworkCfg
Description:
Get network configuration (ip,netmask,gateway) for a group of nodes
Arguments:
nodes: the group of nodes
Returns:
If failed: (1, error_msg)
If success: (0, the hash variable store network configuration info for nodes that get matching network entry)
Error:
none
Example:
my ($ret, $hash) = xCAT::NetworkUtils::getNodesNetworkCfg($noderange);
Comments:
=cut
#-------------------------------------------------------------------------------
sub getNodesNetworkCfg
{
my $nodes = shift;
if ($nodes =~ /xCAT::NetworkUtils/) {
$nodes = shift;
}
my @nets = ();
my $nettab = xCAT::Table->new("networks");
if($nettab) {
my @error_net = ();
my @all_nets = $nettab->getAllAttribs('net','mask','gateway');
foreach my $net (@all_nets) {
my $gateway = $net->{gateway};
if (defined($gateway) and ($gateway eq '<xcatmaster>')) {
my @gatewayd = xCAT::NetworkUtils->my_ip_facing($net->{'net'});
unless ($gatewayd[0]) {
$gateway = $gatewayd[1];
}
}
push @nets, {net=>$net->{net}, mask=>$net->{mask}, gateway=>$gateway};
}
$nettab->close;
}
else {
return (1, "Open \"networks\" table failed");
}
if (!scalar(@nets)) {
return (1, "No entry find in \"networks\" table");
}
my %rethash = ();
foreach my $node (@$nodes) {
my $ip = xCAT::NetworkUtils->getipaddr($node);
foreach my $net (@nets) {
if (xCAT::NetworkUtils::isInSameSubnet( $net->{'net'}, $ip, $net->{'mask'}, 0)) {
$rethash{$node}->{ip} = $ip;
$rethash{$node}->{mask} = $net->{'mask'};
$rethash{$node}->{gateway} = $net->{'gateway'};
last;
}
}
}
return (0, \%rethash);
}
#-------------------------------------------------------------------------------

View File

@ -2582,6 +2582,7 @@ my @nodeattrs = (
# dockercpus --> vm.cpus
# dockermemory --> vm.memory
# dockerflag --> vm.othersettings
# dockernics --> vm.nics
{attr_name => 'dockerhost',
only_if => 'mgt=docker',
tabentry => 'vm.host',
@ -2602,7 +2603,11 @@ my @nodeattrs = (
tabentry => 'vm.othersettings',
access_tabentry => 'vm.node=attr:node',
},
{attr_name => 'dockernics',
only_if => 'mgt=docker',
tabentry => 'vm.nics',
access_tabentry => 'vm.node=attr:node',
},
######################
# hypervisor table #
######################

View File

@ -39,6 +39,7 @@ use JSON;
my $verbose;
my $global_callback;
my $subreq;
my $async;
@ -64,6 +65,7 @@ my %http_session_variable = ();
image=>$nodetype.provmethod,
cmd=>$nodetype.provmethod,
ip=>$host.ip,
nics=>$vm.vmnics,
mac=>$mac.mac,
cpu=>$vm.cpus
memory=>$vm.memory
@ -419,6 +421,16 @@ sub single_state_engine {
return;
}
}
elsif ($curr_state eq 'INIT_TO_WAIT_FOR_START_DONE') {
if ($msg[0]->[0] eq 0 and $data->code ne '304') {
if (defined($node_hash->{ip})) {
my $ret = xCAT::Utils->runxcmd({ command => ["xdsh"], node => [$node_hash->{hostinfo}->{name}], arg =>['-e','/usr/bin/pipework',"$node_hash->{nics}","$node","$node_hash->{ip}/$node_hash->{netmask}\@$node_hash->{gateway}"]},$subreq, 0, 1);
if ($::RUNCMD_RC != 0) {
$msg[0] = [1, $ret];
}
}
}
}
foreach my $tmp (@msg) {
if ($tmp->[0]) {
@ -713,7 +725,7 @@ sub preprocess_request {
sub process_request {
my $req = shift;
my $callback = shift;
$subreq = shift;
my $noderange = $req->{node};
my $command = $req->{command}->[0];
my $args = $req->{arg};
@ -779,17 +791,19 @@ sub process_request {
# The dockerhost is mapped to vm.host, so open vm table here
my $vmtab = xCAT::Table->new('vm');
if ($vmtab) {
my $vmhashs = $vmtab->getNodesAttribs($noderange, ['host']);
my $vmhashs = $vmtab->getNodesAttribs($noderange, ['host','nics']);
if ($vmhashs) {
my @errornodes = ();
foreach my $node (@$noderange) {
my $vmhash = $vmhashs->{$node}->[0];
if (!defined($vmhash) or !defined($vmhash->{host})) {
delete $node_hash_variable{$node};
push @errornodes, $node;
next;
}
my ($host, $port) = split /:/,$vmhash->{host};
if (!defined($host)) {
delete $node_hash_variable{$node};
push @errornodes, $node;
next;
}
@ -804,6 +818,11 @@ sub process_request {
$node_hash_variable{$node}->{node_app_state} = $init_state;
$node_hash_variable{$node}->{state_machine_engine} = $state_machine_engine;
$node_hash_variable{$node}->{genreq_ptr} = $genreq_ptr;
if (defined($vmhash->{nics})) {
$node_hash_variable{$node}->{nics} = $vmhash->{nics};
} else {
$node_hash_variable{$node}->{nics} = "mydocker0";
}
}
if (scalar(@errornodes)) {
$callback->({error=>["Docker host not set correct for @errornodes"], errorcode=>1});
@ -815,6 +834,41 @@ sub process_request {
$callback->({error=>["Open table 'vm' failed"], errorcode=>1});
return;
}
#parse ip for rpower docker start
if ($command eq 'rpower' and $args->[0] eq 'start') {
if (! -e "/usr/bin/pipework") {
$callback->({error=>["Cann't find tool \"pipework\", please make sure it have been downloaded and copied to the right place"], errorcode=>1});
return;
}
my ($ret, $hash) = xCAT::NetworkUtils->getNodesNetworkCfg($noderange);
if ($ret) {
$callback->({error=>[$hash], errorcode=>1});
return;
}
my @error_nodes = ();
foreach my $node (@$noderange) {
if (!defined($hash->{$node})) {
delete $node_hash_variable{$node};
push @error_nodes, $node;
}
else {
if (!defined($hash->{$node}->{'gateway'})) {
push @error_nodes, $node;
}
else {
$node_hash_variable{$node}->{ip} = $hash->{$node}->{'ip'};
$node_hash_variable{$node}->{netmask} = $hash->{$node}->{mask};
$node_hash_variable{$node}->{gateway} = $hash->{$node}->{gateway};
}
}
}
if (scalar(@error_nodes)) {
$callback->({error=>["Can not get network information for :". join(',',@error_nodes)], errorcode=>1});
}
@$noderange = keys %node_hash_variable;
}
#parse parameters for mkdocker
if ($command eq 'mkdocker') {
my ($imagearg, $cmdarg, $flagarg);
@ -828,24 +882,18 @@ sub process_request {
elsif (/dockerflag=(.*)$/) {
$flagarg = $1;
}
}
}
my $nodetypetab = xCAT::Table->new('nodetype');
if (!defined($nodetypetab)) {
$callback->({error=>["Open table 'nodetype' failed"], errorcode=>1});
return;
}
my $hosttab = xCAT::Table->new('hosts');
if (!defined($hosttab)) {
$callback->({error=>["Open table 'hosts' failed"], errorcode=>1});
return;
}
my $mactab = xCAT::Table->new('mac');
if (!defined($mactab)) {
$callback->({error=>["Open table 'mac' failed"], errorcode=>1});
return;
}
my $nodetypehash = $nodetypetab->getNodesAttribs($noderange, ['provmethod']);
my $hosthash = $hosttab->getNodesAttribs($noderange, ['ip']);
my $machash = $mactab->getNodesAttribs($noderange, ['mac']);
my $vmhash = $vmtab->getNodesAttribs($noderange, ['cpus', 'memory', 'othersettings']);
my @errornodes = ();
@ -862,12 +910,14 @@ sub process_request {
}
else {
if (!defined($nodetypehash->{$node}->[0]->{provmethod})) {
delete $node_hash_variable{$node};
push @errornodes, $node;
next;
}
else {
my ($tmp_img,$tmp_cmd) = split /!/, $nodetypehash->{$node}->[0]->{provmethod};
if (!defined($tmp_img)) {
delete $node_hash_variable{$node};
push @errornodes, $node;
next;
}
@ -879,9 +929,6 @@ sub process_request {
$node_hash_variable{$node}->{flag} = $flagarg;
$vmtab->setNodeAttribs($node,{othersettings=>$flagarg});
}
if (defined($hosthash->{$node}->[0]->{ip})) {
$node_hash_variable{$node}->{ip} = $hosthash->{$node}->[0]->{ip};
}
if (defined($machash->{$node}->[0]->{mac})) {
$node_hash_variable{$node}->{mac} = $machash->{$node}->[0]->{mac};
}
@ -892,19 +939,17 @@ sub process_request {
}
if (defined($vmnodehash->{memory})) {
$node_hash_variable{$node}->{memory} = $vmnodehash->{memory};
}
}
if (!defined($flagarg) and defined($vmnodehash->{othersettings})) {
$node_hash_variable{$node}->{flag} = $vmnodehash->{othersettings};
}
}
}
}
$nodetypetab->close;
$hosttab->close;
$mactab->close;
if (scalar(@errornodes)) {
$callback->({error=>["Docker image not set correct for @errornodes"], errorcode=>1});
return;
}
}
$vmtab->close;
@ -1039,6 +1084,7 @@ sub genreq_for_mkdocker {
#$info_hash{name} = '/'.$node;
#$info_hash{Hostname} = '';
#$info_hash{Domainname} = '';
$info_hash{NetworkDisabled} = JSON::true;
$info_hash{Image} = "$dockerinfo->{image}";
@{$info_hash{Cmd}} = split/,/, $dockerinfo->{cmd};
$info_hash{Memory} = $dockerinfo->{mem};