Juju

 view release on metacpan or  search on metacpan

lib/Juju/Environment.pm  view on Meta::CPAN

package Juju::Environment;
BEGIN {
  $Juju::Environment::AUTHORITY = 'cpan:ADAMJS';
}
$Juju::Environment::VERSION = '2.002';
# ABSTRACT: Exposed juju api environment


use Moose;
use JSON::PP;
use YAML::Tiny qw(Dump);
use Function::Parameters;
use Juju::Util;
use Juju::Error::Environment;
use namespace::autoclean;
with 'Juju::RPC';


has password         => (is => 'ro', isa => 'Str', required => 1);
has is_authenticated => (is => 'rw', isa => 'Int', default  => 0);
has endpoint         => (
    is       => 'ro',
    isa      => 'Str',
    default  => 'wss://localhost:17070',
    required => 1
);
has username => (is => 'ro', isa => 'Str', default => 'user-admin');
has Jobs => (
    is      => 'ro',
    isa     => 'HashRef',
    builder => '_build_Jobs',
    lazy    => 1
);

method _build_Jobs {
    return {
        HostUnits     => 'JobHostUnits',
        ManageEnviron => 'JobManageEnviron',
        ManageState   => 'JobManageSate'
    };
}

has util =>
  (is => 'ro', isa => 'Juju::Util', lazy => 1, builder => '_build_util');

method _build_util { Juju::Util->new };



method _prepare_constraints (HashRef $constraints) {
    foreach my $key (keys %{$constraints}) {
        if ($key =~ /^(cpu-cores|cpu-power|mem|root-disk)/) {
            $constraints->{k} = int($constraints->{k});
        }
    }
    return $constraints;
}


method login {
    my $params = {
        "Type"      => "Admin",
        "Request"   => "Login",
        "RequestId" => $self->request_id,
        "Params"    => {
            "AuthTag"  => $self->username,
            "Password" => $self->password
        }
    };

    # block
    my $res = $self->call($params);
    if (defined($res->{Error})) {
        Juju::Error::Environment->throw(
            error_message => 'Failed to login: ' . $res->{Error},
            method_name   => 'login'
        );
    }
    $self->is_authenticated(1)
      unless !defined($res->{Response}->{EnvironTag});
}

lib/Juju/Environment.pm  view on Meta::CPAN


    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);

}


method destroy_machines (ArrayRef $machine_ids, $force = 0, $cb = undef) {
    my $params = {
        "Type"    => "Client",
        "Request" => "DestroyMachines",
        "Params"  => {"MachineNames" => $machine_ids}
    };

    if ($force) {
        $params->{Params}->{Force} = 1;
    }

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);

}



method provisioning_script($cb = undef) {
    my $params = {
        "Type"    => "Client",
        "Request" => "ProvisioningScript"
    };

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);
}


method retry_provisioning (ArrayRef $machines, $cb = undef) {
    my $params = {
        "Type"    => "Client",
        "Request" => "RetryProvisioning",
        "Params"  => @{$machines}
    };

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);
}


method add_relation (Str $endpoint_a, Str $endpoint_b, $cb = undef) {
    my $params = {
        'Type'    => 'Client',
        'Request' => 'AddRelation',
        'Params'  => {'Endpoints' => [$endpoint_a, $endpoint_b]}
    };

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);
}


method destroy_relation (Str $endpoint_a, Str $endpoint_b, $cb = undef) {
    my $params = {
        'Type'    => 'Client',
        'Request' => 'DestroyRelation',
        'Params'  => {'Endpoints' => [$endpoint_a, $endpoint_b]}
    };

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);
}


method deploy (Str $charm, Str $service_name, Int $num_units = 1, Str $config_yaml = "", HashRef $constraints = "", Str $machine_spec = "", $cb = undef) {
    my $params = {
        Type    => "Client",
        Request => "ServiceDeploy",
        Params  => {ServiceName => $service_name}
    };

    # Check for series format
    my (@charm_args) = $charm =~ /(\w+)\/(\w+)/i;
    my $_charm_url = undef;
    if (scalar @charm_args == 2) {
        $_charm_url = $self->util->query_cs($charm_args[1], $charm_args[0]);
    }
    else {
        $_charm_url = $self->util->query_cs($charm);
    }

    $params->{Params}->{CharmUrl}   = $_charm_url->{charm}->{url};
    $params->{Params}->{NumUnits}   = $num_units;
    $params->{Params}->{ConfigYAML} = $config_yaml;
    $params->{Params}->{Constraints} =
      $self->_prepare_constraints($constraints);
    $params->{Params}->{ToMachineSpec} = "$machine_spec";

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);
}


method service_set (Str $service_name, HashRef $config = +{}, $cb = undef) {
    my $params = {
        "Type"    => "Client",
        "Request" => "ServiceSet",
        "Params"  => {
            "ServiceName" => $service_name,
            "Options"     => $config
        }
    };

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);
}


lib/Juju/Environment.pm  view on Meta::CPAN


    # non-block
    return $self->call($params, $cb);
}


method public_address (Str $target, $cb = undef) {
    my $params = {
        "Type"    => "Client",
        "Request" => "PublicAddress",
        "Params"  => {"Target" => $target}
    };

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);
}


method service_set_yaml (Str $service, Str $yaml, $cb = undef) {
    my $params = {
        "Type"    => "Client",
        "Request" => "ServiceSetYAML",
        "Params"  => {
            "ServiceName" => $service,
            "Config"      => Dump($yaml)
        }
    };

    # block
    return $self->call($params) unless $cb;

    # non-block
    return $self->call($params, $cb);
}

__PACKAGE__->meta->make_immutable;
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Juju::Environment - Exposed juju api environment

=head1 VERSION

version 2.002

=head1 SYNOPSIS

  use Juju;

  my $juju =
    Juju->new(endpoint => 'wss://localhost:17070', password => 's3cr3t');

=head1 ATTRIBUTES

=head2 endpoint

Websocket address

=head2 username

Juju admin user, this is a tag and should not need changing from the
default.

B<Note> This will be changing once multiple user support is released.

=head2 password

Password of juju administrator, found in your environments configuration
under B<password>

=head2 is_authenticated

Stores if user has authenticated with juju api server

=head2 Jobs

Supported juju jobs

=head2 util

L<Juju::Util> wrapper

=head1 METHODS

=head2 _prepare_constraints

Makes sure cpu-cores, cpu-power, mem are integers

B<Params>

=over 4

=item *

C<constraints>

hash of service constraints

=back

=head2 login

Login to juju, will die on a failed login attempt.

=head2 reconnect

Reconnects to API server in case of timeout, this also resets the RequestId.

=head2 environment_info

Return Juju Environment information

=head2 environment_uuid

Environment UUID from client connection

lib/Juju/Environment.pm  view on Meta::CPAN

=back

=head2 destroy_environment

Destroys Juju environment

=head2 destroy_machines

Destroy machines

B<Params>

=over 4

=item *

C<machine_ids>

List of machines

=item *

C<force>

Force destroy

=back

=head2 provisioning_script

Returns a shell script that, when run, provisions a machine agent on
the machine executing the script.

=head2 retry_provisioning

Updates the provisioning status of a machine allowing the provisioner
to retry.

B<Params>

=over 4

=item *

C<machines>

Array of machines

=back

=head2 add_relation

Sets a relation between units

B<Params>

=over 4

=item *

C<endpoint_a>

First unit endpoint

=item *

C<endpoint_b>

Second unit endpoint

=back

=head2 destroy_relation

Removes relation between endpoints

B<Params>

=over 4

=item *

C<endpoint_a>

First unit endpoint

=item *

C<endpoint_b>

Second unit endpoint

=back

=head2 deploy

Deploys a charm to service

    $juju->deploy(
        'mysql',
        'mysql',
        1,
        undef,
        undef,
        undef,
        sub {
            my $val = shift;
            print Dumper($val) if defined($val->{Error});
        }
    );

B<Params>

=over 4

=item *

C<charm>

charm to deploy, can be in the format of B<series/charm> if needing to specify a different series

=item *

C<service_name>

name of service to set. same name as charm

=item *

C<num_units>

(optional) number of service units

=item *

C<config_yaml>

(optional) A YAML formatted string of charm options

=item *

C<constraints>

(optional) Machine hardware constraints

=item *

C<machine_spec>

(optional) Machine specification



( run in 0.349 second using v1.01-cache-2.11-cpan-524268b4103 )