6
6
* to you under the Apache License, Version 2.0 (the
7
7
* "License"); you may not use this file except in compliance
8
8
* with the License. You may obtain a copy of the License at
9
- *
10
- * http://www.apache.org/licenses/LICENSE-2.0
11
- *
9
+ * <p>
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ * <p>
12
12
* Unless required by applicable law or agreed to in writing, software
13
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
32
33
33
import java .util .Arrays ;
34
34
35
-
36
35
/**
37
36
* An application that allows users to run admin commands against an Atlas server.
38
37
*
44
43
* <li>-1/255: application error</li>
45
44
*/
46
45
public class AtlasAdminClient {
47
-
48
46
private static final Option STATUS = new Option ("status" , false , "Get the status of an atlas instance" );
49
47
private static final Option STATS = new Option ("stats" , false , "Get the metrics of an atlas instance" );
50
48
private static final Option CREDENTIALS = new Option ("u" , true , "Authorized atlas user credentials (<user>:<password>)" );
51
49
52
50
private static final Options OPTIONS = new Options ();
53
51
54
52
private static final int INVALID_OPTIONS_STATUS = 1 ;
55
- private static final int PROGRAM_ERROR_STATUS = -1 ;
56
-
57
- static {
58
- OPTIONS .addOption (STATUS );
59
- OPTIONS .addOption (STATS );
60
- OPTIONS .addOption (CREDENTIALS );
61
- }
53
+ private static final int PROGRAM_ERROR_STATUS = -1 ;
62
54
63
55
public static void main (String [] args ) throws AtlasException , ParseException {
64
56
AtlasAdminClient atlasAdminClient = new AtlasAdminClient ();
65
- int result = atlasAdminClient .run (args );
57
+ int result = atlasAdminClient .run (args );
58
+
66
59
System .exit (result );
67
60
}
68
61
69
62
private int run (String [] args ) throws AtlasException {
70
- CommandLine commandLine = parseCommandLineOptions (args );
71
- Configuration configuration = ApplicationProperties .get ();
72
- String [] atlasServerUri = configuration .getStringArray (AtlasConstants .ATLAS_REST_ADDRESS_KEY );
63
+ CommandLine commandLine = parseCommandLineOptions (args );
64
+ Configuration configuration = ApplicationProperties .get ();
65
+ String [] atlasServerUri = configuration .getStringArray (AtlasConstants .ATLAS_REST_ADDRESS_KEY );
73
66
74
67
if (atlasServerUri == null || atlasServerUri .length == 0 ) {
75
- atlasServerUri = new String [] { AtlasConstants .DEFAULT_ATLAS_REST_ADDRESS };
68
+ atlasServerUri = new String [] {AtlasConstants .DEFAULT_ATLAS_REST_ADDRESS };
76
69
}
77
70
78
71
return handleCommand (commandLine , atlasServerUri );
79
72
}
80
73
81
74
private int handleCommand (CommandLine commandLine , String [] atlasServerUri ) throws AtlasException {
82
- AtlasClient atlasClient ;
83
-
84
75
String [] providedUserPassword = getUserPassword (commandLine );
76
+ int cmdStatus = PROGRAM_ERROR_STATUS ;
85
77
86
- int cmdStatus = PROGRAM_ERROR_STATUS ;
87
78
if (commandLine .hasOption (STATUS .getOpt ())) {
88
- atlasClient = initAtlasClient (atlasServerUri , providedUserPassword ); // Status is open API, no auth needed
79
+ AtlasClient atlasClient = initAtlasClient (atlasServerUri , providedUserPassword ); // Status is open API, no auth needed
80
+
89
81
try {
90
82
System .out .println (atlasClient .getAdminStatus ());
83
+
91
84
cmdStatus = 0 ;
92
85
} catch (AtlasServiceException e ) {
93
86
System .err .println ("Could not retrieve status of the server at " + Arrays .toString (atlasServerUri ));
87
+
94
88
printStandardHttpErrorDetails (e );
95
89
}
96
90
} else if (commandLine .hasOption (STATS .getOpt ())) {
97
- atlasClient = initAtlasClient (atlasServerUri , providedUserPassword ); // Stats/metrics is open API, no auth needed
91
+ AtlasClient atlasClient = initAtlasClient (atlasServerUri , providedUserPassword ); // Stats/metrics is open API, no auth needed
92
+
98
93
try {
99
94
AtlasMetrics atlasMetrics = atlasClient .getAtlasMetrics ();
100
- String json = AtlasType .toJson (atlasMetrics );
95
+ String json = AtlasType .toJson (atlasMetrics );
96
+
101
97
System .out .println (json );
98
+
102
99
cmdStatus = 0 ;
103
100
} catch (AtlasServiceException e ) {
104
101
System .err .println ("Could not retrieve metrics of the server at " + Arrays .toString (atlasServerUri ));
102
+
105
103
printStandardHttpErrorDetails (e );
106
104
}
107
105
} else {
108
106
System .err .println ("Unsupported option. Refer to usage for valid options." );
107
+
109
108
printUsage ();
110
109
}
111
110
@@ -118,18 +117,21 @@ private String[] getUserPassword(CommandLine commandLine) {
118
117
// Parse the provided username password
119
118
if (commandLine .hasOption (CREDENTIALS .getOpt ())) {
120
119
String value = commandLine .getOptionValue (CREDENTIALS .getOpt ());
120
+
121
121
if (value != null ) {
122
122
basicAuthUsernamePassword = value .split (":" );
123
123
}
124
124
}
125
+
125
126
if (basicAuthUsernamePassword == null || basicAuthUsernamePassword .length != 2 ) {
126
127
System .err .println ("Invalid credentials. Format: <user>:<password>" );
127
128
}
129
+
128
130
return basicAuthUsernamePassword ;
129
131
}
130
132
131
133
private AtlasClient initAtlasClient (final String [] atlasServerUri , final String [] providedUserNamePassword ) throws AtlasException {
132
- AtlasClient atlasClient ;
134
+ final AtlasClient atlasClient ;
133
135
134
136
if (!AuthenticationUtil .isKerberosAuthenticationEnabled ()) {
135
137
if (providedUserNamePassword == null || providedUserNamePassword .length < 2 ) {
@@ -140,35 +142,46 @@ private AtlasClient initAtlasClient(final String[] atlasServerUri, final String[
140
142
} else {
141
143
atlasClient = new AtlasClient (atlasServerUri );
142
144
}
145
+
143
146
return atlasClient ;
144
147
}
145
148
146
149
private void printStandardHttpErrorDetails (AtlasServiceException e ) {
147
150
System .err .println ("Error details: " );
148
- System .err .println ("HTTP Status: " + e .getStatus ().getStatusCode () + ","
149
- + e .getStatus ().getReasonPhrase ());
151
+ System .err .println ("HTTP Status: " + e .getStatus ().getStatusCode () + "," + e .getStatus ().getReasonPhrase ());
150
152
System .err .println ("Exception message: " + e .getMessage ());
151
153
}
152
154
153
155
private CommandLine parseCommandLineOptions (String [] args ) {
154
156
if (args .length == 0 ) {
155
157
printUsage ();
156
158
}
157
- CommandLineParser parser = new GnuParser ();
158
- CommandLine commandLine = null ;
159
+
160
+ CommandLineParser parser = new GnuParser ();
161
+ CommandLine commandLine = null ;
162
+
159
163
try {
160
164
commandLine = parser .parse (OPTIONS , args );
161
165
} catch (ParseException e ) {
162
166
System .err .println ("Could not parse command line options. " + e .getMessage ());
167
+
163
168
printUsage ();
164
169
}
170
+
165
171
return commandLine ;
166
172
}
167
173
168
174
private void printUsage () {
169
175
HelpFormatter helpFormatter = new HelpFormatter ();
176
+
170
177
helpFormatter .printHelp ("atlas_admin.py" , OPTIONS );
178
+
171
179
System .exit (AtlasAdminClient .INVALID_OPTIONS_STATUS );
172
180
}
173
181
182
+ static {
183
+ OPTIONS .addOption (STATUS );
184
+ OPTIONS .addOption (STATS );
185
+ OPTIONS .addOption (CREDENTIALS );
186
+ }
174
187
}
0 commit comments