From 0259db6a5f67fa8837c18bbaed3e424cccf7a3e0 Mon Sep 17 00:00:00 2001 From: levinse Date: Sat, 22 Jan 2011 02:45:27 +0000 Subject: [PATCH] add svc_broadband shellcommands export, RT11250 --- FS/FS/Schema.pm | 1 + FS/FS/part_export/broadband_shellcommands.pm | 109 +++++++++++++++++++++++++++ FS/FS/svc_broadband.pm | 11 +++ httemplate/edit/svc_broadband.cgi | 17 +---- httemplate/view/svc_broadband.cgi | 6 ++ 5 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 FS/FS/part_export/broadband_shellcommands.pm diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index 30fb006d6..fe5f96a77 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -2353,6 +2353,7 @@ sub tables_hashref { 'altitude', 'decimal', 'NULL', '', '', '', 'vlan_profile', 'varchar', 'NULL', $char_d, '', '', 'performance_profile', 'varchar', 'NULL', $char_d, '', '', + 'plan_id', 'varchar', 'NULL', $char_d, '', '', ], 'primary_key' => 'svcnum', 'unique' => [ [ 'mac_addr' ] ], diff --git a/FS/FS/part_export/broadband_shellcommands.pm b/FS/FS/part_export/broadband_shellcommands.pm new file mode 100644 index 000000000..c7f0fbb33 --- /dev/null +++ b/FS/FS/part_export/broadband_shellcommands.pm @@ -0,0 +1,109 @@ +package FS::part_export::broadband_shellcommands; + +use strict; +use vars qw(@ISA %info); +use Tie::IxHash; +use FS::part_export; + +@ISA = qw(FS::part_export); + +tie my %options, 'Tie::IxHash', + 'user' => { label=>'Remote username', default=>'freeside' }, + 'insert' => { label=>'Insert command', + default=>'php provision.php --mac=$mac_addr --plan=$plan_id --account=active', + }, + 'delete' => { label=>'Delete command', + default=>'', + }, + 'suspend' => { label=>'Suspension command', + default=>'php provision.php --mac=$mac_addr --plan=$plan_id --account=suspend', + }, + 'unsuspend'=> { label=>'Unsuspension command', + default=>'', + }, + 'uppercase_mac' => { label => 'Force MACs to uppercase', + type => 'checkbox', } +; + +%info = ( + 'svc' => 'svc_broadband', + 'desc' => 'Run remote commands via SSH, for svc_broadband services', + 'options' => \%options, + 'notes' => <<'END' +END +); + + +sub rebless { shift; } + +sub _export_insert { + my($self) = shift; + $self->_export_command('insert', @_); +} + +sub _export_delete { + my($self) = shift; + $self->_export_command('delete', @_); +} + +sub _export_suspend { + my($self) = shift; + $self->_export_command('suspend', @_); +} + +sub _export_unsuspend { + my($self) = shift; + $self->_export_command('unsuspend', @_); +} + +sub _export_command { + my ( $self, $action, $svc_broadband) = (shift, shift, shift); + my $command = $self->option($action); + return '' if $command =~ /^\s*$/; + + #set variable for the command + no strict 'vars'; + { + no strict 'refs'; + ${$_} = $svc_broadband->getfield($_) foreach $svc_broadband->fields; + } + + if ( $self->option('uppercase_mac') ) { + $mac_addr = uc $mac_addr; + } + + #done setting variables for the command + + $self->shellcommands_queue( $svc_broadband->svcnum, + user => $self->option('user')||'root', + host => $self->machine, + command => eval(qq("$command")), + ); +} + +sub _export_replace { + ''; +} + +#a good idea to queue anything that could fail or take any time +sub shellcommands_queue { + my( $self, $svcnum ) = (shift, shift); + my $queue = new FS::queue { + 'svcnum' => $svcnum, + 'job' => "FS::part_export::broadband_shellcommands::ssh_cmd", + }; + $queue->insert( @_ ); +} + +sub ssh_cmd { #subroutine, not method + use Net::OpenSSH; + my $opt = { @_ }; + my $ssh = Net::OpenSSH->new($opt->{'user'}.'@'.$opt->{'host'}); + die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error; + my ($output, $errput) = $ssh->capture2($opt->{'command'}); + die "Error running SSH command: ". $ssh->error if $ssh->error; + die $errput if $errput; + die $output if $output; + ''; +} + diff --git a/FS/FS/svc_broadband.pm b/FS/FS/svc_broadband.pm index 5ffe0e452..2b794aa4b 100755 --- a/FS/FS/svc_broadband.pm +++ b/FS/FS/svc_broadband.pm @@ -70,6 +70,8 @@ customer's router will have the same address for both its internal and external interfaces thus saving address space. This has been found to work on most NAT routers available. +=item plan_id + =back =head1 METHODS @@ -105,6 +107,14 @@ sub table_info { 'select_label' => 'cidr', 'disable_inventory' => 1, }, + 'plan_id' => 'Service Plan Id', + 'performance_profile' => 'Peformance Profile', + 'authkey' => 'Authentication key', + 'mac_addr' => 'MAC address', + 'latitude' => 'Latitude', + 'longitude' => 'Longitude', + 'altitude' => 'Altitude', + 'vlan_profile' => 'VLAN profile', }, }; } @@ -337,6 +347,7 @@ sub check { || $self->ut_coordn('longitude', -180, 180) || $self->ut_sfloatn('altitude') || $self->ut_textn('vlan_profile') + || $self->ut_textn('plan_id') ; return $error if $error; diff --git a/httemplate/edit/svc_broadband.cgi b/httemplate/edit/svc_broadband.cgi index b11fec7a7..ae7f50fca 100644 --- a/httemplate/edit/svc_broadband.cgi +++ b/httemplate/edit/svc_broadband.cgi @@ -2,21 +2,6 @@ 'post_url' => popurl(1). 'process/svc_broadband.cgi', 'name' => 'broadband service', 'table' => 'svc_broadband', - 'labels' => { 'svcnum' => 'Service', - 'description' => 'Description', - 'ip_addr' => 'IP address', - 'speed_down' => 'Download speed', - 'speed_up' => 'Upload speed', - 'blocknum' => 'Router/Block', - 'block_label' => 'Router/Block', - 'mac_addr' => 'MAC address', - 'latitude' => 'Latitude', - 'longitude' => 'Longitude', - 'altitude' => 'Altitude', - 'vlan_profile' => 'VLAN profile', - 'performance_profile' => 'Performance profile', - 'authkey' => 'Authentication key', - }, 'fields' => \@fields, 'field_callback' => $callback, 'dummy' => $cgi->query_string, @@ -35,7 +20,7 @@ my $conf = new FS::Conf; my @fields = ( qw( description ip_addr speed_down speed_up blocknum ), { field=>'block_label', type=>'fixed' }, - qw( mac_addr latitude longitude altitude vlan_profile performance_profile authkey ) + qw( mac_addr latitude longitude altitude vlan_profile performance_profile authkey plan_id ) ); my $fixedblock = ''; diff --git a/httemplate/view/svc_broadband.cgi b/httemplate/view/svc_broadband.cgi index f552e9bc7..12e5f0f5d 100644 --- a/httemplate/view/svc_broadband.cgi +++ b/httemplate/view/svc_broadband.cgi @@ -83,6 +83,10 @@ Authentication Key <%$auth_key%> + + Service Plan Id + <%$plan_id%> + % %foreach (sort { $a cmp $b } $svc_broadband->virtual_fields) { @@ -203,6 +207,7 @@ my ( $vlan_profile, $auth_key, $description, + $plan_id, ) = ( $svc_broadband->getfield('speed_down'), $svc_broadband->getfield('speed_up'), @@ -214,6 +219,7 @@ my ( $svc_broadband->vlan_profile, $svc_broadband->auth_key, $svc_broadband->description, + $svc_broadband->plan_id, ); -- 2.11.0