| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | ##################################################################################### | ||
| 4 | # | ||
| 5 | # Copyright (c) 2012, Alexander Todorov <atodorov()otb.bg>. See POD section. | ||
| 6 | # | ||
| 7 | ##################################################################################### | ||
| 8 | |||
| 9 | package App::Difio::OpenShift; | ||
| 10 | our $VERSION = '2.00'; | ||
| 11 | our $NAME = "difio-openshift-perl"; | ||
| 12 | |||
| 13 | use App::Difio::OpenShift::Parser; | ||
| 14 | @ISA = qw(App::Difio::OpenShift::Parser); | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | use JSON; | ||
| 20 | use LWP::UserAgent; | ||
| 21 | |||
| 22 | my $data = { | ||
| 23 | 'user_id' => $ENV{'DIFIO_USER_ID'}, | ||
| 24 | 'app_name' => $ENV{'OPENSHIFT_GEAR_NAME'}, | ||
| 25 | 'app_uuid' => $ENV{'OPENSHIFT_GEAR_UUID'}, | ||
| 26 | 'app_type' => $ENV{'OPENSHIFT_GEAR_TYPE'}, | ||
| 27 | 'app_url' => "http://$ENV{'OPENSHIFT_GEAR_DNS'}", | ||
| 28 | 'app_vendor' => 0, # Red Hat OpenShift | ||
| 29 | 'pkg_type' => 400, # Perl / CPAN | ||
| 30 | 'installed' => [], | ||
| 31 | }; | ||
| 32 | |||
| 33 | my $pod_parsed = ""; | ||
| 34 | my $parser = App::Difio::OpenShift::Parser->new(); | ||
| 35 | $parser->output_string( \$pod_parsed ); | ||
| 36 | $parser->parse_file("$ENV{'OPENSHIFT_GEAR_DIR'}/perl5lib/lib/perl5/x86_64-linux-thread-multi/perllocal.pod"); | ||
| 37 | |||
| 38 | my @installed; | ||
| 39 | foreach my $nv (split(/\n/, $pod_parsed)) { | ||
| 40 | my @name_ver = split(/ /, $nv); | ||
| 41 | push(@installed, {'n' => $name_ver[0], 'v' => $name_ver[1]}); | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | $data->{'installed'} = [ @installed ]; | ||
| 46 | |||
| 47 | my $json_data = to_json($data); # , { pretty => 1 }); | ||
| 48 | |||
| 49 | my $ua = new LWP::UserAgent(('agent' => "$NAME/$VERSION")); | ||
| 50 | |||
| 51 | # will URL Encode by default | ||
| 52 | my $response = $ua->post('https://difio-otb.rhcloud.com/application/register/', { json_data => $json_data}); | ||
| 53 | |||
| 54 | if (! $response->is_success) { | ||
| 55 | die $response->status_line; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $content = from_json($response->decoded_content); | ||
| 59 | print "Difio: $content->{'message'}\n"; | ||
| 60 | |||
| 61 | exit $content->{'exit_code'}; | ||
| 62 | |||
| 63 | |||
| 64 | 1; | ||
| 65 | __END__ |