| 1 | package Service::Engine::API; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Service::Engine; | ||
| 8 | use Data::Dumper; | ||
| 9 | use Module::Runtime qw(require_module); | ||
| 10 | use Service::Engine::Admin::Server; | ||
| 11 | use threads; | ||
| 12 | use threads::shared; | ||
| 13 | |||
| 14 | our $Config; | ||
| 15 | our $Log; | ||
| 16 | our $EngineName; | ||
| 17 | our $Commands = {}; | ||
| 18 | our $CommandList = ''; | ||
| 19 | our $Server; | ||
| 20 | our $Threads; | ||
| 21 | our $Engine; | ||
| 22 | our $Health; | ||
| 23 | |||
| 24 | share($Health); | ||
| 25 | share($Commands); | ||
| 26 | share($CommandList); | ||
| 27 | |||
| 28 | # load desired classes | ||
| 29 | # Service::Engine::API:* | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | |||
| 33 | my ($class,$options) = @_; | ||
| 34 | |||
| 35 | # set some defaults | ||
| 36 | my $attributes = {'methods'=>{}}; | ||
| 37 | |||
| 38 | # load options | ||
| 39 | if (ref($options) eq 'HASH') { | ||
| 40 | foreach my $option (keys %{$options}) { | ||
| 41 | $attributes->{$option} = $options->{$option}; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | # pull in some Service::Engine globals | ||
| 46 | $Config = $Service::Engine::Config; | ||
| 47 | $Log = $Service::Engine::Log; | ||
| 48 | $EngineName = $Service::Engine::EngineName; | ||
| 49 | $Threads = $Service::Engine::Threads; | ||
| 50 | $Health = $Service::Engine::Health; | ||
| 51 | $Engine = $Service::Engine::Engine; | ||
| 52 | |||
| 53 | my $self = bless $attributes, $class; | ||
| 54 | |||
| 55 | # create our admin methods | ||
| 56 | # these will be autoloaded from Service::Engine like this my $method = $Admin->methodname(); | ||
| 57 | # this works by adding the connection methods to $attributes->{'methods'}->{methodname} | ||
| 58 | # save the method label to $Methods->{$label} = $type; | ||
| 59 | |||
| 60 | if (ref($Config->get_config('admin')) eq 'HASH') { | ||
| 61 | |||
| 62 | if ($Config->get_config('admin')->{'enabled'}) { | ||
| 63 | foreach my $service (keys $Config->get_config('admin')->{'modules'}) { | ||
| 64 | |||
| 65 | my $service_conf = $Config->get_config('admin')->{'modules'}->{$service}; | ||
| 66 | |||
| 67 | $Log->log({msg=>Dumper($service_conf),level=>3}); | ||
| 68 | |||
| 69 | if ($service_conf->{'enabled'}) { | ||
| 70 | $Log->log({msg=>"loading $service",level=>2}); | ||
| 71 | my $package = 'Service::Engine::Admin::' . ucfirst lc $service; | ||
| 72 | |||
| 73 | if ($service_conf->{'module_name'}) { | ||
| 74 | $package = $EngineName . '::Modules::' . $service_conf->{'module_name'}; | ||
| 75 | } | ||
| 76 | my $res = eval { require_module($package) }; | ||
| 77 | if ($@) { | ||
| 78 | $Log->log({msg=>"error loading $service: $@",level=>1}); | ||
| 79 | } else { | ||
| 80 | my $obj = $package->new($self,$service_conf->{'options'}); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | # set up commands | ||
| 87 | $self->add_command({'module'=>$Threads, 'method'=>'add_thread', 'label'=>'AddThread'}); | ||
| 88 | $self->add_command({'module'=>$self, 'method'=>'stop_engine', 'label'=>'StopEngine'}); | ||
| 89 | $self->add_command({'module'=>$Engine, 'method'=>'start_selector', 'label'=>'StartEngine'}); | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | } | ||
| 94 | |||
| 95 | return $self; | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | sub start_api_server { | ||
| 100 | |||
| 101 | my $self = shift; | ||
| 102 | |||
| 103 | if ($Config->get_config('admin')->{'enabled'}) { | ||
| 104 | my $host_ip = $Config->get_config('api')->{'host_ip'}; | ||
| 105 | my $host_port = $Config->get_config('api')->{'host_port'}; | ||
| 106 | $host_ip ||= '127.0.0.1'; | ||
| 107 | $host_port ||= '42180'; | ||
| 108 | my $key_file = $Config->get_config('api')->{'ssl'}->{'SSL_key_file'}; | ||
| 109 | my $cert_file = $Config->get_config('api')->{'ssl'}->{'SSL_cert_file'}; | ||
| 110 | |||
| 111 | $Server = Service::Engine::API::Server->new( | ||
| 112 | 'host' => $host_ip, | ||
| 113 | 'port' => $host_port, | ||
| 114 | 'server_type' => ['Net::Server::Multiplex'], | ||
| 115 | 'SSL_key_file' => $key_file, | ||
| 116 | 'SSL_cert_file' => $cert_file, | ||
| 117 | ); | ||
| 118 | $Server->run; | ||
| 119 | } | ||
| 120 | |||
| 121 | } | ||
| 122 | |||
| 123 | sub expand { | ||
| 124 | |||
| 125 | my $self = shift; | ||
| 126 | my $stub = shift; | ||
| 127 | |||
| 128 | my $commands = $CommandList; | ||
| 129 | my @commands = split /\n/, $commands; | ||
| 130 | |||
| 131 | foreach my $command (@commands) { | ||
| 132 | return "!$command" unless $command !~ /^$stub/i; | ||
| 133 | } | ||
| 134 | |||
| 135 | return $stub; | ||
| 136 | |||
| 137 | } | ||
| 138 | |||
| 139 | sub command_list { | ||
| 140 | my $self = shift; | ||
| 141 | return $CommandList; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub add_command { | ||
| 145 | my ($self,$command) = @_; | ||
| 146 | |||
| 147 | if (ref($command) ne 'HASH') { | ||
| 148 | $Log->log({msg=>"invalid command format - must be a hash:" . Dumper($command),level=>2}); | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | if (!$command->{'module'} || !$command->{'method'} || !$command->{'label'}) { | ||
| 153 | $Log->log({msg=>"invalid command format - missing module, method or label:" . Dumper($command),level=>2}); | ||
| 154 | return; | ||
| 155 | } | ||
| 156 | |||
| 157 | warn($command->{'label'} . ':' . Dumper($command->{'module'})); | ||
| 158 | |||
| 159 | $Commands->{$command->{'label'}} = shared_clone({'module'=>$command->{'module'}, 'method'=>$command->{'method'}}); | ||
| 160 | my @commands = split /\n/, $CommandList; | ||
| 161 | push @commands, $command->{'label'}; | ||
| 162 | $CommandList = join "\n", sort @commands; | ||
| 163 | |||
| 164 | return; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub stop_engine { | ||
| 168 | # we do this here because it blocks while it waits; that would be bad in the Engine | ||
| 169 | my ($self,$fh) = @_; | ||
| 170 | |||
| 171 | $fh->write("shutting down\n") unless !$fh; | ||
| 172 | my $stop = $Engine->stop_selector($fh); | ||
| 173 | $fh->write("waiting for the backlog to finish processing\n") unless !$fh; | ||
| 174 | |||
| 175 | # wait for it | ||
| 176 | ###>>>TO DO - we need to do this in a separate thread so it doesn't block admin entirely | ||
| 177 | while ($Threads->get_queue_count()){}; | ||
| 178 | |||
| 179 | $fh->write("All done.") unless !$fh; | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | our $AUTOLOAD; | ||
| 184 | sub AUTOLOAD { | ||
| 185 | my $self = shift; | ||
| 186 | # Remove qualifier from original method name... | ||
| 187 | my $called = $AUTOLOAD =~ s/.*:://r; | ||
| 188 | # Is there an attribute of that name? | ||
| 189 | # check for an expansion | ||
| 190 | $called = $self->expand($called); | ||
| 191 | |||
| 192 | if ($called =~ /^\!/) { # this is a command | ||
| 193 | $called =~ s/^\!//; | ||
| 194 | return $Commands->{$called}; | ||
| 195 | } else { | ||
| 196 | print "Hmm. $called? Available options are: \n$CommandList\n> " | ||
| 197 | unless exists $self->{'methods'}->{$called}; | ||
| 198 | # If so, return it... | ||
| 199 | return $self->{'methods'}->{$called}; | ||
| 200 | } | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | 1; |