Created
December 12, 2014 14:57
-
-
Save s-wool/2a05c65bb8bc3e2b8700 to your computer and use it in GitHub Desktop.
Cloudera Managerでrack awarenessを戻すスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use File::Basename; | |
| use LWP::UserAgent; | |
| use JSON::XS; | |
| use Data::Dumper; | |
| my $topology_file_path = shift @ARGV; | |
| die 'topology file is not found!' unless (defined $topology_file_path && -f $topology_file_path); | |
| open my $in, $topology_file_path or die "can not open $topology_file_path : $!"; | |
| my $topology = {}; | |
| while (my $line = <$in>) { | |
| chomp $line; | |
| my ($ip, $rack) = split /\s+/, $line, 2; | |
| warn $ip, $rack; | |
| $topology->{$ip} = $rack; | |
| } | |
| my $ua = LWP::UserAgent->new(agent => basename(__FILE__)); | |
| my $endpoint = 'http://yourhostname:7180/api/v6'; | |
| my $user = 'admin'; | |
| my $pass = 'yourpassword'; | |
| my $req = HTTP::Request->new( | |
| GET => "$endpoint/hosts" | |
| ); | |
| $req->authorization_basic($user, $pass); | |
| my $res = $ua->request($req); | |
| my $json = JSON::XS->new->utf8->decode($res->content); | |
| for my $host ( @{$json->{items}} ) { | |
| if ( defined $topology->{$host->{ipAddress}} ) { | |
| my $url = $endpoint . "/hosts/" . $host->{hostId}; | |
| my $content = JSON::XS->new->utf8->encode({ rackId => $topology->{$host->{ipAddress}} }); | |
| $req = HTTP::Request->new( | |
| PUT => $url, | |
| [ 'Content-Type' => 'application/json' ], | |
| $content | |
| ); | |
| $req->authorization_basic($user, $pass); | |
| $res = $ua->request($req); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment