This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / FS / part_export / grandstream.pm
1 package FS::part_export::grandstream;
2
3 use base 'FS::part_export';
4 use vars qw($DEBUG $me %info $GAPSLITE_HOME $JAVA_HOME);
5 use URI;
6 use MIME::Base64;
7 use Tie::IxHash;
8 use IPC::Run qw(run);
9 use FS::CGI qw(rooturl);
10
11 $DEBUG = 0;
12
13 $me = '[' . __PACKAGE__ . ']';
14 $GAPSLITE_HOME = '/usr/local/src/GS_CFG_GEN/';
15
16 my @java = qw( /usr/lib/jvm/default-java/ /usr/java/default/
17                /usr/lib/jvm/java-6-sun/
18                /usr/lib/jvm/java-1.4.2-gcj-4.1-1.4.2.0/
19              ); #add more common places distros and people put their JREs
20
21 $JAVA_HOME = (grep { -e $_ } @java)[0];
22
23 tie my %options, 'Tie::IxHash',
24   'upload'          => { label=>'Enable upload to TFTP server via SSH',
25                          type=>'checkbox',
26                        },
27   'user'            => { label=>'User name for SSH to TFTP server' },
28   'tftproot'        => { label=>'Directory in which to upload configuration' },
29   'java_home'       => { label=>'Path to java to be used',
30                          default=>$JAVA_HOME,
31                        },
32   'gapslite_home'   => { label=>'Path to grandstream configuration tool',
33                          default=>$GAPSLITE_HOME,
34                        },
35   'template'        => { label=>'Configuration template',
36                          type=>'textarea',
37                          notes=>'Type or paste the configuration template here',
38                        },
39 ;
40
41 %info = (
42   'svc'      => [ qw( part_device ) ], # svc_phone
43   'desc'     => 'Provision phone numbers to Grandstream Networks phones/ATAs',
44   'options'  => \%options,
45   'notes'    => 'Provision phone numbers to Grandstream Networks phones/ATAs.  Requires a Java runtime environment and the Grandstream configuration tool to be installed.',
46 );
47
48 sub rebless { shift; }
49
50 sub gs_create_config {
51   my($self, $mac, %opt) = (@_);
52
53   eval "use Net::SCP;";
54   die $@ if $@;
55
56   warn "gs_create_config called with mac of $mac\n" if $DEBUG;
57   $mac = sprintf('%012s', lc($mac));
58   my $dir = '%%%FREESIDE_CONF%%%/cache.'. $FS::UID::datasrc;
59
60   my $fh = new File::Temp(
61     TEMPLATE => "grandstream.$mac.XXXXXXXX",
62     DIR      => $dir,
63     UNLINK   => 0,
64   );
65
66   my $filename = $fh->filename;
67
68   #my $template = new Text::Template (
69   #  TYPE       => 'ARRAY',
70   #  SOURCE     => $self->option('template'),
71   #  DELIMITERS => $delimiters,
72   #  OUTPUT     => $fh,
73   #);
74
75   #$template->compile or die "Can't compile template: $Text::Template::ERROR\n";
76
77   #my $config = $template->fill_in( HASH => { mac_addr => $mac } );
78
79   print $fh $self->option('template') or die "print failed: $!";
80   close $fh;
81
82   #system( "export GAPSLITE_HOME=$GAPSLITE_HOME; export JAVA_HOME=$JAVA_HOME; ".
83   #        "cd $dir; $GAPSLITE_HOME/bin/encode.sh $mac $filename $dir/cfg$mac"
84   #      ) == 0
85   #  or die "grandstream encode failed: $!";
86   my $out_and_err = '';
87   my @cmd = ( "$JAVA_HOME/bin/java",
88               '-classpath', "$GAPSLITE_HOME/lib/gapslite.jar:$GAPSLITE_HOME/lib/bcprov-jdk14-124.jar:$GAPSLITE_HOME/config",
89               'com.grandstream.cmd.TextEncoder',
90               $mac, $filename, "$dir/cfg$mac",
91             );
92   run \@cmd, '>&', \$out_and_err
93     or die "grandstream encode failed: $out_and_err";
94
95   unlink $filename;
96
97   open my $encoded, "$dir/cfg$mac"  or die "open cfg$mac failed: $!";
98   
99   my $content;
100
101   if ($opt{upload}) {
102     if ($self->option('upload')) {
103       my $scp = new Net::SCP ( {
104         'host' => $self->machine,
105         'user' => $self->option('user'),
106         'cwd'  => $self->option('tftproot'),
107       } );
108
109       $scp->put( "$dir/cfg$mac" ) or die "upload failed: ". $scp->errstr;
110     }
111   } else {
112     local $/;
113     $content = <$encoded>;
114   }
115
116   close $encoded;
117   unlink "$dir/cfg$mac";
118
119   $content;
120 }
121
122 sub gs_create {
123   my($self, $mac) = (shift, shift);
124
125   return unless $mac;  # be more alarmed?  Or check upstream?
126
127   $self->gs_create_config($mac, 'upload' => 1);
128   '';
129 }
130
131 sub gs_delete {
132   my($self, $mac) = (shift, shift);
133
134   $mac = sprintf('%012s', lc($mac));
135
136   ssh_cmd( user => $self->option('user'),
137            host => $self->machine,
138            command => 'rm',
139            args    => [ '-f', $self->option('tftproot'). "/cfg$mac" ],
140          );
141   '';
142
143 }
144
145 sub ssh_cmd { #subroutine, not method
146   use Net::SSH '0.08';
147   &Net::SSH::ssh_cmd( { @_ } );
148 }
149
150 sub _export_insert {
151 #  my( $self, $svc_phone ) = (shift, shift);
152 #  $self->gs_create($svc_phone->mac_addr);
153   '';
154 }
155
156 sub _export_replace {
157 #  my( $self, $new_svc, $old_svc ) = (shift, shift, shift);
158 #  $self->gs_delete($old_svc->mac_addr);
159 #  $self->gs_create($new_svc->mac_addr);
160   '';
161 }
162
163 sub _export_delete {
164 #  my( $self, $svc_phone ) = (shift, shift);
165 #  $self->gs_delete($svc_phone->mac_addr);
166   '';
167 }
168
169 sub _export_suspend {
170   '';
171 }
172
173 sub _export_unsuspend {
174   '';
175 }
176
177 sub export_device_insert {
178   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
179   $self->gs_create($phone_device->mac_addr);
180   '';
181 }
182
183 sub export_device_delete {
184   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
185   $self->gs_delete($phone_device->mac_addr);
186   '';
187 }
188
189 sub export_device_config {
190   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
191
192   my $mac;
193 #  if ($phone_device) {
194     $mac = $phone_device->mac_addr;
195 #  } else {
196 #    $mac = $svc_phone->mac_addr;
197 #  }
198
199   return '' unless $mac;  # be more alarmed?  Or check upstream?
200
201   $self->gs_create_config($mac);
202 }
203
204
205 sub export_device_replace {
206   my( $self, $svc_phone, $new_svc_or_device, $old_svc_or_device ) =
207     (shift, shift, shift, shift);
208
209   $self->gs_delete($old_svc_or_device->mac_addr);
210   $self->gs_create($new_svc_or_device->mac_addr);
211   '';
212 }
213
214 # bad overloading?
215 sub export_links {
216   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
217
218   return;  # remove if we actually support being an export for svc_phone;
219
220   my @deviceparts = map { $_->devicepart } $self->export_device;
221   my @devices = grep { my $part = $_->devicepart;
222                        scalar( grep { $_ == $part } @deviceparts );
223                      } $svc_phone->phone_device;
224
225   my $export = $self->exportnum;
226   my $fsurl = rooturl();
227   if (@devices) {
228     foreach my $device ( @devices ) {
229       next unless $device->mac_addr;
230       my $num = $device->devicenum;
231       push @$arrayref,
232         qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;devicenum=$num">!.
233         qq! Phone config </A>!;
234       }
235   } elsif ($svc_phone->mac_addr) {
236     my $num = $svc_phone->svcnum;
237     push @$arrayref,
238       qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;svcnum=$num">!.
239       qq! Phone config </A>!;
240   } #else
241   '';
242 }
243
244 sub export_device_links {
245   my($self, $svc_phone, $device, $arrayref) = (shift, shift, shift, shift);
246   warn "export_device_links $self $svc_phone $device $arrayref\n" if $DEBUG;
247   return unless $device && $device->mac_addr;
248   my $export = $self->exportnum;
249   my $fsurl = rooturl();
250   my $num = $device->devicenum;
251   push @$arrayref,
252     qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;devicenum=$num">!.
253     qq! Phone config </A>!;
254   '';
255 }
256
257 1;