1- #
1+ #
22# Licensed under the Apache License, Version 2.0 (the "License");
33# you may not use this file except in compliance with the License.
44# You may obtain a copy of the License at
5- #
5+ #
66# http://www.apache.org/licenses/LICENSE-2.0
7- #
7+ #
88# Unless required by applicable law or agreed to in writing, software
99# distributed under the License is distributed on an "AS IS" BASIS,
1010# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111# See the License for the specific language governing permissions and
1212# limitations under the License.
1313#
14- # Copyright (c) 2015,2016 by Delphix. All rights reserved.
14+ # Copyright (c) 2015,2020 by Delphix. All rights reserved.
1515#
1616# Program Name : dx_config.pl
1717# Description : Convert dxtools.conf file from and to csv
1818# Author : Marcin Przepiorowski
1919# Created : 22 Apr 2015 (v2.0.0)
2020#
21-
21+
2222
2323use strict;
2424use warnings;
4141
4242
4343GetOptions(
44- ' help|?' => \( my $help ),
45- ' debug' => \(my $debug ),
44+ ' help|?' => \( my $help ),
45+ ' debug' => \(my $debug ),
4646' convert=s' => \(my $convert ),
4747' csvfile|f=s' => \(my $csvfile ),
48+ ' text|c=s' => \(my $conf_param_file ),
4849' configfile|c=s' => \(my $configfile ),
49- ' version|v' => \(my $print_version )
50+ ' version|v' => \(my $print_version )
5051) or pod2usage(-verbose => 1, -output => \*STDERR );
5152
52-
5353pod2usage(-verbose => 2, -output => \*STDERR ) && exit if $help ;
54- die " $version \n " if $print_version ;
54+ die " $version \n " if $print_version ;
5555
56- if (! ( defined ($convert ) && defined ($csvfile ) && defined ($configfile ) ) ) {
56+ if (! ( defined ($convert ) && ( defined ($csvfile ) || defined ( $conf_param_file ) ) && defined ($configfile ) ) ) {
5757 print " Parameter convert is required.\n " ;
5858 pod2usage(-verbose => 1, -output => \*STDERR );
5959 exit ;
6666
6767if ( $convert eq ' tocsv' ) {
6868 convert_tocsv($csvfile , $configfile );
69- }
69+ }
7070
71- if ( $convert eq ' todxconf' ) {
71+ if ( ( $convert eq ' todxconf' ) && ( defined (( $csvfile )) ) ) {
7272 convert_todxconf($csvfile , $configfile );
73- }
73+ }
74+
75+ if ( ($convert eq ' todxconf' ) && (defined ($conf_param_file ))) {
76+ convert_text_todxconf($conf_param_file , $configfile );
77+ }
78+
7479
7580
7681# ###########################################################################
7782
7883sub convert_todxconf {
79- my $csvfile = shift ;
84+ my $csvfile = shift ;
8085 my $configfile = shift ;
8186
82- open (my $FD ,$csvfile ) || die " Can't open file: $csvfile \n " ;
87+ open (my $FD ,$csvfile ) || die " Can't open file: $csvfile \n " ;
88+
89+ my @engine_list ;
8390
84- my @engine_list ;
91+ while ( my $line = < $FD >) {
8592
86- while ( my $line = < $FD >) {
93+ chomp $line ;
8794
88- chomp $line ;
89-
9095 if ( ! ($line =~ m / ^\# / g ) ) {
9196
92- my ( $hostname , $ip_address , $port , $username , $password , $default , $protocol ) = split (' ,' ,$line );
97+ my ( $hostname , $ip_address , $port , $username , $password , $default , $protocol , $clientid , $clientsecret ) = split (' ,' ,$line );
9398
94- if ( ! ( defined ($hostname ) && defined ($ip_address ) && defined ($port ) && defined ($username ) && defined ($password ) && defined ($default ) )) {
99+ if ( ! ( defined ($hostname ) && defined ($ip_address ) && defined ($port ) && defined ($username ) && defined ($password ) && ((defined ($username ) && defined ($password )) || (defined ($clientid ) && defined ($clientsecret )) )
100+ && defined ($default ) )) {
95101 print " There is a problem with line $line \n " ;
96102 print " Not all fields defined. Exiting\n " ;
97- exit ;
103+ exit 1 ;
98104 }
99105
100- my %engine = (
101- hostname => $hostname ,
102- username => $username ,
103- ip_address => $ip_address ,
104- password => $password ,
105- port => $port ,
106- default => $default ,
107- protocol => $protocol
108- );
106+ if (($username ne ' ' ) && (defined ($clientid )) && ($clientid ne ' ' )) {
107+ print " There is a problem with line $line \n " ;
108+ print " username and clientid are mutually exclusive\n " ;
109+ exit 1;
110+ }
111+
112+ my %engine ;
113+
114+ if ($username ne ' ' ) {
115+ %engine = (
116+ hostname => $hostname ,
117+ username => $username ,
118+ ip_address => $ip_address ,
119+ password => $password ,
120+ port => $port ,
121+ default => $default ,
122+ protocol => $protocol
123+ );
124+ } else {
125+ %engine = (
126+ hostname => $hostname ,
127+ clientid => $clientid ,
128+ ip_address => $ip_address ,
129+ clientsecret => $clientsecret ,
130+ port => $port ,
131+ default => $default ,
132+ protocol => $protocol
133+ );
134+ }
109135
110136
111137 push (@engine_list , \%engine );
138+ }
139+ }
140+
141+
142+ my $time = strftime(' %Y-%m-%d-%H-%M-%S' ,localtime );
143+
144+ if ( -e $configfile ) {
145+ my $backupfile = $configfile . " ." . $time ;
146+ copy ( $configfile , $backupfile ) or die (" Can't generate backup file $backupfile " );
147+ print " Old config file backup file name is $backupfile \n " ;
148+ }
149+
150+ my %engine_json = (
151+ data => \@engine_list
152+ );
153+
154+ open (my $fh , " >" , $configfile ) or die (" Can't open new config file $configfile for write" );
155+ print $fh to_json(\%engine_json , {pretty => 1});
156+ close $fh ;
157+ print " New config file $configfile created.\n " ;
158+ }
159+
160+ sub convert_text_todxconf {
161+ my $conf_param_file = shift ;
162+ my $configfile = shift ;
163+ my @engine_list ;
164+ chomp $conf_param_file ;
165+
166+
167+ if ( ! ($conf_param_file =~ m / ^\# / g ) ) {
168+ my ( $hostname , $ip_address , $port , $username , $password , $default , $protocol , $clientid , $clientsecret ) = split (' ,' ,$conf_param_file );
169+ if ( ! ( defined ($hostname ) && defined ($ip_address ) && defined ($port ) && ((defined ($username ) && defined ($password )) || (defined ($clientid ) && defined ($clientsecret )) )
170+ && defined ($default ) )) {
171+ print " There is a problem with line $conf_param_file \n " ;
172+ print " Not all fields defined. Exiting\n " ;
173+ exit 1;
112174 }
175+
176+ if (($username ne ' ' ) && (defined ($clientid )) && ($clientid ne ' ' )) {
177+ print " There is a problem with line $conf_param_file \n " ;
178+ print " username and clientid are mutually exclusive\n " ;
179+ exit 1;
180+ }
181+
182+ my %engine ;
183+
184+ if ($username ne ' ' ) {
185+ %engine = (
186+ hostname => $hostname ,
187+ username => $username ,
188+ ip_address => $ip_address ,
189+ password => $password ,
190+ port => $port ,
191+ default => $default ,
192+ protocol => $protocol
193+ );
194+ } else {
195+ %engine = (
196+ hostname => $hostname ,
197+ clientid => $clientid ,
198+ ip_address => $ip_address ,
199+ clientsecret => $clientsecret ,
200+ port => $port ,
201+ default => $default ,
202+ protocol => $protocol
203+ );
204+ }
205+
206+ push (@engine_list , \%engine );
113207 }
114208
115209
210+
116211 my $time = strftime(' %Y-%m-%d-%H-%M-%S' ,localtime );
117212
118213 if ( -e $configfile ) {
@@ -133,7 +228,7 @@ sub convert_todxconf {
133228
134229
135230sub convert_tocsv {
136- my $csvfile = shift ;
231+ my $csvfile = shift ;
137232 my $configfile = shift ;
138233
139234
@@ -142,12 +237,20 @@ sub convert_tocsv {
142237
143238 open (my $FD , " >" , $csvfile ) || die " Can't open file: $csvfile for write \n " ;
144239
145- print $FD " # engine nick name, engine ip/hostname, port, username, password, default, protocol \n " ;
240+ print $FD " # engine nick name, engine ip/hostname, port, username, password, default, protocol, clientid, clientsecret \n " ;
146241
147242
148243 for my $engine_name ( $engine_obj -> getAllEngines() ) {
149244 my $engine = $engine_obj -> getEngine($engine_name );
150- print $FD $engine_name . " ," . $engine -> {ip_address } . " ," . $engine -> {port } . " ," . $engine -> {username } . " ," . $engine -> {password } . " ," . $engine -> {default } . " ," . $engine -> {protocol } . " \n " ;
245+
246+ my $line ;
247+ if (defined ($engine -> {clientid })) {
248+ $line = $engine_name . " ," . $engine -> {ip_address } . " ," . $engine -> {port } . " ,,," . $engine -> {default } . " ," . $engine -> {protocol } . " ," . $engine -> {clientid } . " ," . $engine -> {clientsecret } ." \n " ;
249+ } else {
250+ $line = $engine_name . " ," . $engine -> {ip_address } . " ," . $engine -> {port } . " ," . $engine -> {username } . " ," . $engine -> {password } . " ," . $engine -> {default } . " ," . $engine -> {protocol } . " ,," . " \n " ;
251+ }
252+
253+ print $FD $line ;
151254
152255 }
153256
@@ -162,12 +265,12 @@ sub convert_tocsv {
162265
163266=head1 SYNOPSIS
164267
165- dx_config -convert todxconf|tocsv -csvfile file.csv -configfile dxtools.conf [-help] [-version]
268+ dx_config -convert todxconf|tocsv -csvfile file.csv|-text params_list -configfile dxtools.conf [-help] [-version]
166269
167270
168271=head1 DESCRIPTION
169272
170- Convert a csv file into DXTOOLKIT configuration file (dxtools.conf) or convert configuration file into csv file.
273+ Convert a csv file or list of parameters into DXTOOLKIT configuration file (dxtools.conf) or convert configuration file into csv file.
171274Existing configuration file will be copy into backup file.
172275
173276ex.
@@ -178,13 +281,21 @@ =head1 ARGUMENTS
178281
179282=over 3
180283
181- =item B<-convert >
284+ =item B<-convert todxconf|tocsv >
182285Specify a conversion direction
183286
184- =item B<-csvfile >
287+ =item B<-csvfile file.csv >
185288CSV file name
186289
187- =item B<-configfile >
290+ =item B<-text parameter_list >
291+ Create a new config file from input. Parameter list is comma separated list of values in the following order:
292+
293+
294+ hostname, ip_address, port, username, password, default, protocol, clientid, clientsecret
295+
296+ Only one pair is required: username and password or clientid and clientsecret
297+
298+ =item B<-configfile dxtools.conf >
188299config file name
189300
190301=back
@@ -194,7 +305,7 @@ =head1 OPTIONS
194305
195306=over 2
196307
197- =item B<-help >
308+ =item B<-help >
198309Print this screen
199310
200311=item B<-debug >
@@ -206,12 +317,18 @@ =head1 EXAMPLES
206317
207318Create CSV from dxtools.conf
208319
209- dx_config -convert tocsv -csvfile new.csv -configfile dxtools.conf
320+ dx_config -convert tocsv -csvfile new.csv -configfile dxtools.conf
210321 New csv file new.csv created.
211-
212- Create dxtools.conf from CSV file
322+
323+ Create dxtools.conf from CSV file
213324
214325 dx_config -convert todxconf -csvfile new.csv -configfile dxtools.conf
215326 New config file dxtools.conf created.
216327
328+ Create dxtools.conf from command line
329+
330+ dx_config -convert todxconf -text hostname,192.168.1.100,443,username,password,N,https -configfile new.conf
331+ Old config file backup file name is new.conf.2020-05-01-10-28-07
332+ New config file new.conf created.
333+
217334=cut
0 commit comments