source: genesis/tools/genesis-to-yaml.pl@ 9112

Last change on this file since 9112 was 9095, checked in by rick, 14 years ago

Make optional module import more robust.

  • Property svn:executable set to *
File size: 4.5 KB
RevLine 
[8266]1#!/usr/bin/env perl
[8621]2use File::Basename;
[9095]3use Module::Load::Conditional qw[check_install];
[8266]4
[9095]5if (check_install(module => 'Geo::Coordinates::RDNAP')) {
[8266]6 require Geo::Coordinates::RDNAP;
7 Geo::Coordinates::RDNAP->import(qw/from_rd to_rd dms/);
8} else {
9 sub from_rd {
10 return (0,0,0);
11 }
12}
13
14# RD X,Y -> RD Noorderbreedte, Oosterbreedte
15# http://web.inter.nl.net/users/F.Kissels/gps/conversie.html
16
17# Little perl to convert genesis files into python config files,
18# Which gonna be used for more logic stuff ;-)
19
20$argc = $#ARGV + 1;
21if ($argc == 0) {
[8870]22 print STDERR "Usage $ENV{_} <wleiden.conf> ...\n";
23 @ARGV=`ls \$(dirname $ENV{_})/../nodes/*/wleiden.conf`;
24}
25
26if ($argc != 1) {
27 # Hack to easy support multiple files without remembering any variable in between
[8266]28 foreach $file (@ARGV) {
[8870]29 chomp($file);
[8294]30 $output = $file;
31 $output =~ s/conf$/yaml/;
[8870]32 print "# Processing $file -> $output\n";
[8621]33 $CMD=dirname(__FILE__) . "/genesis-to-yaml.pl";
34 `$CMD $file > $output`;
[8266]35 }
36 exit 0;
37}
38
39
40do($ARGV[0]) || die;
41
42my %status_labels = ( up => 'up', down => 'dw', planned => 'pl' );
43
44$interfaces = [];
45foreach $if (keys %config) {
46 push(@interfaces, $if) if ($if =~ m/^[a-z0-9]+$/g);
47}
48$interfaces = join(",",@interfaces);
49
50if ( ($X =~ /\d+/) and ($Y =~ /\d+/) and ($X > 10) and ($Y > 10)) {
51 ($lat, $lon, $h) = from_rd( $X, $Y, 0);
52} else {
53 $lat = $lon = $h = 0;
54}
55
56$status = $status_labels{$status} || 'up'; # ensure reporting a correct status
57$comment = $comment || 'None';
58
59print <<EOF;
60# Genesis config yaml style
61# vim:ts=2:et:sw=2:ai
62#
63comment : $comment
64interfaces: $interfaces
65latitude : $lat
[8871]66location : '$location'
[8266]67longitude : $lon
68masterip : $master_ip
[8644]69release : $release
[8266]70nodename : $nodename
71nodetype : $nodetype
72rdnap_x : $X
73rdnap_y : $Y
74status : $status
75EOF
76
[8644]77if ($ileiden =~ /yes/) {
78 print "ileiden : yes\n";
79} else {
80 print "ileiden : no\n";
81}
[8266]82
83foreach $if (keys %config) {
84 ($iflabel, $alias) = split(':',$if);
[8270]85 $iflabel .= "_alias${alias}" if ($alias =~ m/[0-9]/);
[8266]86
87 print <<EOF;
88iface_$iflabel:
89EOF
90 %tmp = (
91 'dhcp' => 'False',
[8273]92 'compass' => 'None',
[8266]93 'interface' => $if,
94 );
95 foreach $line (split('\n',$config{$if})) {
96 ($key, $value) = split(/=/,$line);
[8300]97 # Remove trailing and leading white spaces
98 $value =~ s/^\s+//;
99 $value =~ s/\s+$//;
[8266]100 if ($key and (lc($key) =~ /^[a-z].*/)) {
101 #print "$key : $value\n";
102 $value = lc($value) if not lc($key) =~ m/(desc|comment)/;
103 if ($key =~ /^type$/i) {
104 if ($value eq 'ethernet') {
105 $value = 'eth';
106 } else {
107 $value = '11b';
108 }
[8644]109 } elsif ($key =~ /^status$/i) {
110 if ($value eq 'up') {
111 $value = 'up';
112 } else {
113 $value = 'down';
114 }
[8266]115 } elsif ($key =~ /^mode$/i) {
116 if ($value eq 'master') {
[8273]117 $value = 'ap-wds';
[8266]118 } else { # managed
[8273]119 $value = 'station-wds';
[8266]120 }
121 } elsif ($key =~ /^polar$/i) {
122 if ($value eq 'hor') {
123 $value = 'hr';
124 } else {
125 $value = 'vr';
126 }
127 } elsif ($key =~ /^essid$/i) {
128 $key = 'ssid';
129
130 # Dirty hack to fetch compass direction of essid
131 $value =~ /[a-z]+-([a-z]+)\..*/;
132 $direction = $1;
133 # Translate into English
134 if ($direction ne "omni") {
135 $direction =~ tr/oz/es/;
136 }
137 # node AMwtrt1 and 'test' directions
138 if ($direction eq "wtrtr" or $direction eq "test") {
139 $direction = 'None';
140 }
[8586]141 if ($direction) {
142 $tmp{'compass'} = $direction;
143 } else {
144 $tmp{'compass'} = 'None';
145 }
[8266]146 } elsif ($key =~ /^gain$/i) {
147 $value =~ s/dbi//;
148 }
[8644]149 # Als key niet bestaat, Dan key toevoegen
[8266]150 $tmp{lc($key)} = $value;
151 }
152 }
[8644]153# Status
154 if ( ! $tmp{status} ) {
155 $tmp{status} = 'up';
156 }
[8266]157# ethernet2wifi bridges
158 if ( $tmp{extra_type} eq 'eth2wifibridge' ) {
[8273]159 $tmp{type} = 'eth';
[8266]160 }
161# 11a
162 if ( $tmp{type} eq '11b' && $tmp{channel} > 13 ) {
163 $tmp{type} = '11a';
164 }
165
166 foreach $key (sort keys %tmp) {
[8270]167 printf " %-11s: '$tmp{$key}'\n", $key;
[8266]168 }
169 print "\n";
170}
171
Note: See TracBrowser for help on using the repository browser.