User Tools

Site Tools


#!/usr/bin/env perl
 
package aAgentI;
 
use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::JSON qw(encode_json decode_json);
use Mojo::Util qw(dumper);
 
sub new {
    my ($class, $host, $login, $password) = @_;
    my $ua = Mojo::UserAgent->new;
    my $self = {
        host => $host,
        login => $login,
        password => $password,
        port => '8184',
        ua => $ua
    };
    bless $self, $class;
    return $self;
}
 
sub ua {
    my ($self, $ua) = @_;
    return $self->{ua} unless $ua;
    $self->{ua} = $ua;
    $self;
}
 
sub host {
    my ($self, $host) = @_;
    return $self->{host} unless $host;
    $self->{host} = $host;
    $self;
}
 
sub login {
    my ($self, $login) = @_;
    return $self->{login} unless $login;
    $self->{login} = $login;
    $self;
}
 
sub password {
    my ($self, $password) = @_;
    return $self->{password} unless $password;
    $self->{password} = $password;
    $self;
}
 
sub port {
    my ($self, $port) = @_;
    return $self->{port} unless $port;
    $self->{port} = $port;
    $self;
}
 
 
sub rpc {
    my ($self,  $call, %args) = @_;
    return undef unless $call;
    return undef unless $call =~ /^\//;
 
    my $host = $self->host;
    my $login = $self->login;
    my $password = $self->password;
    my $port = $self->port;
 
    my $url = "https://$login:$password\@$host:$port$call";
    $url .= "?" if %args;
    foreach my $key (sort keys %args) {
        my $value = $args{$key};
        next unless $value;
        $url .= "&$key=$value";
    }
 
    $url =~ s/\?&/\?/;
    my $res;
    eval {
        my $tx = $self->ua->get($url);
        $res = $tx->result->body;
    };
    return undef if $@;
    my $j = decode_json($res);
    return $j if $j;
    undef;
}
 
 
sub alive {
    my $self = shift;
    my $res = $self->rpc('/hello');
    return undef unless $res;
    return 1 if  $res->{'message'} eq 'hello';
    return undef;
}
 
sub db_list {
    my $self = shift;
    $self->rpc('/db/list');
}
 
sub db_profile {
    my ($self, $name) = @_;
    $self->rpc('/db/profile', name => $name);
}
 
sub db_create {
    my ($self, $name) = @_;
    $self->rpc('/db/create', name => $name);
}
 
sub db_copy {
    my ($self, $name, $new_name) = @_;
    $self->rpc('/db/copy', name => $name, new_name => $new_name);
}
 
sub db_drop {
    my ($self, $name) = @_;
    $self->rpc('/db/drop', name => $name);
}
 
sub db_rename {
    my ($self, $name, $new_name) = @_;
    $self->rpc('/db/rename', name => $name, new_name => $new_name);
}
 
sub db_owner {
    my ($self, $name) = @_;
    $self->rpc('/db/drop', name => $name);
}
 
 
sub user_list {
    my $self = shift;
    $self->rpc('/user/list');
}
 
sub user_profile {
    my ($self, $name) = @_;
    $self->rpc('/user/profile', name => $name);
}
 
sub user_create {
    my ($self, $name, $password) = @_;
    $self->rpc('/user/create', name => $name, password => $password);
}
 
sub user_drop {
    my ($self, $name) = @_;
    $self->rpc('/user/drop', name => $name);
}
 
sub user_rename {
    my ($self, $name, $new_name) = @_;
    $self->rpc('/user/rename', name => $name, new_name => $new_name);
}
 
sub user_password {
    my ($self, $name, $password) = @_;
    $self->rpc('/user/password', name => $name, password => $password);
}
 
sub db_dump {
    my ($self, $name, $store, $login, $password, $cb, $job_id, $magic) = @_;
    $self->rpc('/db/dump',
                        name => $name,
                        store => $store,
                        login => $login,
                        password => $password,
                        cb => $cb,
                        job_id => $job_id,
                        magic => $magic);
}
 
sub db_restore {
    my ($self, $store, $login, $password, $file, $new_name, $cb, $job_id, $magic) = @_;
    $self->rpc('/db/restore',
                        store => $store,
                        login => $login,
                        password => $password,
                        file => $file,
                        new_name => $new_name,
                        cb => $cb,
                        job_id => $job_id,
                        magic => $magic);
}
 
 
1;
 
use strict;
use warnings;
use Mojo::Util qw(dumper);
use Scalar::Util 'blessed';
 
my $a = aAgentI->new("pgdbxxxx.xxxxx.us", "xxxxxx", "xxxxxxxxxxx");
#print ref $a;
 
 
#no strict 'refs';
#print dumper @{'aAgentI' . '::'};
 
 
#my $a = aAgentI->new("thx.xxxxxx.org", "master", "password");
 
print dumper $a->rpc('/hello');
 
#print dumper $a->db_list;
#print dumper $a->db_create('asterisk_copy');
#print dumper $a->db_profile('asterisk_copy');
 
##print dumper $a->db_profile('kpi_xxx1')->{profile};
 
#print dumper $a->db_drop('kpi_xxxx_copy');
#print dumper $a->db_copy('kpixxx', 'kpi_xxxx_copy');
#print dumper $a->db_copy('kpixxx', 'kpi_xxx_copy');
 
#print dumper $a->db_profile('kpi_xxx');
#print dumper $a->db_profile('kpi_xxxx_copy');
#print dumper $a->db_drop('kpi_xxx_copy');
#print dumper $a->db_drop('asterisk_copy');
#print dumper $a->db_drop('asterisk_copy_2');
 
#print dumper $a->db_create('asterisk_copy');
#print dumper $a->db_copy('asterisk_copy', 'asterisk_copy_2');
#print dumper $a->db_profile('asterisk_copy_2');
#print dumper $a->db_drop('asterisk_copy_2');
#print dumper $a->db_profile('asterisk_copy_2');
 
#print dumper $a->user_profile('asterisk');
#print dumper $a->user_create('asterisk_2', '123456');
#print dumper $a->user_profile('asterisk_2');
#print dumper $a->user_rename('asterisk_2', 'asterisk_222');
#print dumper $a->user_profile('asterisk_222');
#print dumper $a->user_drop('asterisk_222');
#print dumper $a->user_drop('asterisk_2');
 
#print dumper $a->db_profile('asterisk_copy');
#print dumper $a->db_dump('asterisk', 'th.xxxx.org', 'master', 'password', 'th.xxxx.org', '123', 'magic');
 
#print dumper $a->db_restore( 'thx.xxxxx.org', 'master', 'password',
#                                     'asterisk_copy--20171221-181222-MSK--127.0.0.1.sqlz',
#                                      'asterisk_copy333' , 'thx.xxxxx.org', '123', 'magic');
 
 
#my $list = $a->db_list->{list};
 
#foreach my $pro (@$list) {
#    my $name = $pro->{name};
#    print dumper $a->db_dump($name, 'thx.xxxxx.org', 'login', 'password', 'thx.xxxx.org', '123', 'magic');
#}
 
#print dumper $a->db_dump('asterisk_copy', 'thx.xxxx.org', 'login', 'password', 'thx.xxxx.org', '123', 'magic');
 
#my $a = aAgentI->new("pgdb-xxxx.lazurit.us", "master", "password");
#if ($a->alive) {
#    my $list = $a->db_list->{list};
#    foreach my $db (@$list) {
#        my $name = $db->{name};
#        my $size = $db->{size};
#        my $owner = $db->{owner};
#        my $nb = $db->{numbackends};
#        print "$name $size $owner $nb\n";
#    }
#}
 
#EOF
astorei.pl
#!/usr/bin/env perl
 
#--------------
#--- STOREI ---
#--------------
 
package aStoreI;
 
use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::JSON qw(encode_json decode_json);
use Mojo::Util qw(dumper);
 
sub new {
    my ($class, $host, $login, $password) = @_;
    my $ua = Mojo::UserAgent->new;
 
    $ua->max_response_size(10*1024*1024*1024);
    $ua->inactivity_timeout(60);
    $ua->connect_timeout(60);
    $ua->request_timeout(2*60*60);
 
    my $self = {
        host => $host,
        login => $login,
        password => $password,
        port => '8184',
        ua => $ua
    };
    bless $self, $class;
    return $self;
}
 
sub ua {
    my ($self, $ua) = @_;
    return $self->{ua} unless $ua;
    $self->{ua} = $ua;
    $self;
}
 
sub host {
    my ($self, $host) = @_;
    return $self->{host} unless $host;
    $self->{host} = $host;
    $self;
}
 
sub login {
    my ($self, $login) = @_;
    return $self->{login} unless $login;
    $self->{login} = $login;
    $self;
}
 
sub password {
    my ($self, $password) = @_;
    return $self->{password} unless $password;
    $self->{password} = $password;
    $self;
}
 
sub port {
    my ($self, $port) = @_;
    return $self->{port} unless $port;
    $self->{port} = $port;
    $self;
}
 
 
sub rpc {
    my ($self,  $call, %args) = @_;
    return undef unless $call;
    return undef unless $call =~ /^\//;
 
    my $host = $self->host;
    my $login = $self->login;
    my $password = $self->password;
    my $port = $self->port;
 
    my $url = "https://$login:$password\@$host:$port$call";
    $url .= "?" if %args;
    foreach my $key (sort keys %args) {
        my $value = $args{$key};
        next unless $value;
        $url .= "&$key=$value";
    }
 
    $url =~ s/\?&/\?/;
    my $res;
    eval {
        my $tx = $self->ua->get($url);
        $res = $tx->result->body;
    };
    return undef if $@;
    my $j = decode_json($res);
}
 
 
sub alive {
    my $self = shift;
    my $res = $self->rpc('/hello');
    return 1 if  $res->{'message'} eq 'hello';
    return undef;
}
 
sub data_list {
    my $self = shift;
    $self->rpc('/data/list');
}
 
sub data_profile {
    my ($self, $name) = @_;
    $self->rpc('/data/profile', name => $name);
}
 
sub data_delete {
    my ($self, $name) = @_;
    $self->rpc('/data/delete', name => $name);
}
 
sub store_profile {
    my ($self) = @_;
    $self->rpc('/store/profile');
}
 
 
sub data_get {
    my ($self, $name, $dir) = @_;
 
    return undef unless $dir;
    return undef unless -w $dir;
 
    my $host = $self->host;
    my $login = $self->login;
    my $password = $self->password;
    my $port = $self->port;
 
    $ENV{MOJO_TMPDIR} = $dir;
 
    my $tx = $self->ua->get("https://$login:$password\@$host:$port/data/get?name=$name");
    my $res = $tx->result;
 
    my $type = $res->headers->content_type || '';
    my $disp = $res->headers->content_disposition || '';
    my $file = "$dir/$name";
 
    if ($type =~ /name=/ or $disp =~ /filename=/) {
        my ($filename) = $disp =~ /filename=\"(.*)\"/;
        rename $file, "$file.bak" if -r $file;
        $res->content->asset->move_to($file);
    }
    return undef unless -r $file;
    $file;
}
 
sub data_put {
    my ($self, $file) = @_;
 
    return undef unless $file;
    return undef unless -r $file;
 
    my $host = $self->host;
    my $login = $self->login;
    my $password = $self->password;
    my $port = $self->port;
 
    my $url = "https://$login:$password\@$host:$port/data/put";
    my $res;
    eval {
        my $tx = $self->ua->post($url => form => {data => { file => $file } });
        $res = $tx->result->body;
    };
    return undef if $@;
    my $j = decode_json($res);
}
 
sub dump_clean {
    my ($self, $pattern, $remain) = @_;
    $self->rpc('/dump/clean', pattern => $pattern, remain => $remain);
}
 
1;
 
use strict;
use warnings;
use Mojo::Util qw(dumper);
 
 
my $s = aStoreI->new("dbstorexx.xxxx.us", "xxxxxx", "xxxxxxxx");
print dumper $s->data_list;
#print dumper $s->dump_clean('hrm_auto_new--*', 1);
#print dumper $s->data_put('data.bin');
#print dumper $s->data_profile('data.bin');
print dumper $s->data_get('_dummy--20180901-001004-MSK--local.sqlz', '.');
#print dumper $s->data_delete('data.bin');
#print dumper $s->data_profile('data.bin');
#print dumper $s->store_profile;
 
#EOF