1
- #
1
+ #
2
2
# Licensed under the Apache License, Version 2.0 (the "License");
3
3
# you may not use this file except in compliance with the License.
4
4
# You may obtain a copy of the License at
5
- #
5
+ #
6
6
# http://www.apache.org/licenses/LICENSE-2.0
7
- #
7
+ #
8
8
# Unless required by applicable law or agreed to in writing, software
9
9
# distributed under the License is distributed on an "AS IS" BASIS,
10
10
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
11
# See the License for the specific language governing permissions and
12
12
# limitations under the License.
13
13
#
14
- # Copyright (c) 2015,2016 by Delphix. All rights reserved.
14
+ # Copyright (c) 2015,2020 by Delphix. All rights reserved.
15
15
#
16
16
# Program Name : dx_config.pl
17
17
# Description : Convert dxtools.conf file from and to csv
18
18
# Author : Marcin Przepiorowski
19
19
# Created : 22 Apr 2015 (v2.0.0)
20
20
#
21
-
21
+
22
22
23
23
use strict;
24
24
use warnings;
41
41
42
42
43
43
GetOptions(
44
- ' help|?' => \( my $help ),
45
- ' debug' => \(my $debug ),
44
+ ' help|?' => \( my $help ),
45
+ ' debug' => \(my $debug ),
46
46
' convert=s' => \(my $convert ),
47
47
' csvfile|f=s' => \(my $csvfile ),
48
+ ' text|c=s' => \(my $conf_param_file ),
48
49
' configfile|c=s' => \(my $configfile ),
49
- ' version|v' => \(my $print_version )
50
+ ' version|v' => \(my $print_version )
50
51
) or pod2usage(-verbose => 1, -output => \*STDERR );
51
52
52
-
53
53
pod2usage(-verbose => 2, -output => \*STDERR ) && exit if $help ;
54
- die " $version \n " if $print_version ;
54
+ die " $version \n " if $print_version ;
55
55
56
- if (! ( defined ($convert ) && defined ($csvfile ) && defined ($configfile ) ) ) {
56
+ if (! ( defined ($convert ) && ( defined ($csvfile ) || defined ( $conf_param_file ) ) && defined ($configfile ) ) ) {
57
57
print " Parameter convert is required.\n " ;
58
58
pod2usage(-verbose => 1, -output => \*STDERR );
59
59
exit ;
66
66
67
67
if ( $convert eq ' tocsv' ) {
68
68
convert_tocsv($csvfile , $configfile );
69
- }
69
+ }
70
70
71
- if ( $convert eq ' todxconf' ) {
71
+ if ( ( $convert eq ' todxconf' ) && ( defined (( $csvfile )) ) ) {
72
72
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
+
74
79
75
80
76
81
# ###########################################################################
77
82
78
83
sub convert_todxconf {
79
- my $csvfile = shift ;
84
+ my $csvfile = shift ;
80
85
my $configfile = shift ;
81
86
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 ;
83
90
84
- my @engine_list ;
91
+ while ( my $line = < $FD >) {
85
92
86
- while ( my $line = < $FD >) {
93
+ chomp $line ;
87
94
88
- chomp $line ;
89
-
90
95
if ( ! ($line =~ m / ^\# / g ) ) {
91
96
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 );
93
98
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 ) )) {
95
101
print " There is a problem with line $line \n " ;
96
102
print " Not all fields defined. Exiting\n " ;
97
- exit ;
103
+ exit 1 ;
98
104
}
99
105
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
+ }
109
135
110
136
111
137
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;
112
174
}
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 );
113
207
}
114
208
115
209
210
+
116
211
my $time = strftime(' %Y-%m-%d-%H-%M-%S' ,localtime );
117
212
118
213
if ( -e $configfile ) {
@@ -133,7 +228,7 @@ sub convert_todxconf {
133
228
134
229
135
230
sub convert_tocsv {
136
- my $csvfile = shift ;
231
+ my $csvfile = shift ;
137
232
my $configfile = shift ;
138
233
139
234
@@ -142,12 +237,20 @@ sub convert_tocsv {
142
237
143
238
open (my $FD , " >" , $csvfile ) || die " Can't open file: $csvfile for write \n " ;
144
239
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 " ;
146
241
147
242
148
243
for my $engine_name ( $engine_obj -> getAllEngines() ) {
149
244
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 ;
151
254
152
255
}
153
256
@@ -162,12 +265,12 @@ sub convert_tocsv {
162
265
163
266
=head1 SYNOPSIS
164
267
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]
166
269
167
270
168
271
=head1 DESCRIPTION
169
272
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.
171
274
Existing configuration file will be copy into backup file.
172
275
173
276
ex.
@@ -178,13 +281,21 @@ =head1 ARGUMENTS
178
281
179
282
=over 3
180
283
181
- =item B<-convert >
284
+ =item B<-convert todxconf|tocsv >
182
285
Specify a conversion direction
183
286
184
- =item B<-csvfile >
287
+ =item B<-csvfile file.csv >
185
288
CSV file name
186
289
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 >
188
299
config file name
189
300
190
301
=back
@@ -194,7 +305,7 @@ =head1 OPTIONS
194
305
195
306
=over 2
196
307
197
- =item B<-help >
308
+ =item B<-help >
198
309
Print this screen
199
310
200
311
=item B<-debug >
@@ -206,12 +317,18 @@ =head1 EXAMPLES
206
317
207
318
Create CSV from dxtools.conf
208
319
209
- dx_config -convert tocsv -csvfile new.csv -configfile dxtools.conf
320
+ dx_config -convert tocsv -csvfile new.csv -configfile dxtools.conf
210
321
New csv file new.csv created.
211
-
212
- Create dxtools.conf from CSV file
322
+
323
+ Create dxtools.conf from CSV file
213
324
214
325
dx_config -convert todxconf -csvfile new.csv -configfile dxtools.conf
215
326
New config file dxtools.conf created.
216
327
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
+
217
334
=cut
0 commit comments