1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas;
import org.apache.atlas.model.metrics.AtlasMetrics;
import org.apache.atlas.type.AtlasType;
import org.apache.atlas.utils.AuthenticationUtil;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.configuration.Configuration;
import java.util.Arrays;
/**
* An application that allows users to run admin commands against an Atlas server.
*
* The application uses {@link AtlasClient} to send REST requests to the Atlas server. The details of connections
* and other configuration is specified in the Atlas properties file.
* Exit status of the application will be as follows:
* <li>0: successful execution</li>
* <li>1: error in options used for the application</li>
* <li>-1/255: application error</li>
*/
public class AtlasAdminClient {
private static final Option STATUS = new Option("status", false, "Get the status of an atlas instance");
private static final Option STATS = new Option("stats", false, "Get the metrics of an atlas instance");
private static final Options OPTIONS = new Options();
private static final int INVALID_OPTIONS_STATUS = 1;
private static final int PROGRAM_ERROR_STATUS = -1;
static {
OPTIONS.addOption(STATUS);
OPTIONS.addOption(STATS);
}
public static void main(String[] args) throws AtlasException, ParseException {
AtlasAdminClient atlasAdminClient = new AtlasAdminClient();
int result = atlasAdminClient.run(args);
System.exit(result);
}
private int run(String[] args) throws AtlasException {
CommandLine commandLine = parseCommandLineOptions(args);
Configuration configuration = ApplicationProperties.get();
String[] atlasServerUri = configuration.getStringArray(AtlasConstants.ATLAS_REST_ADDRESS_KEY);
if (atlasServerUri == null || atlasServerUri.length == 0) {
atlasServerUri = new String[] { AtlasConstants.DEFAULT_ATLAS_REST_ADDRESS };
}
AtlasClient atlasClient = null;
if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput();
atlasClient = new AtlasClient(atlasServerUri, basicAuthUsernamePassword);
} else {
atlasClient = new AtlasClient(atlasServerUri, null);
}
return handleCommand(commandLine, atlasServerUri, atlasClient);
}
private int handleCommand(CommandLine commandLine, String[] atlasServerUri, AtlasClient atlasClient) {
int cmdStatus = PROGRAM_ERROR_STATUS;
if (commandLine.hasOption(STATUS.getOpt())) {
try {
System.out.println(atlasClient.getAdminStatus());
cmdStatus = 0;
} catch (AtlasServiceException e) {
System.err.println("Could not retrieve status of the server at " + Arrays.toString(atlasServerUri));
printStandardHttpErrorDetails(e);
}
} else if (commandLine.hasOption(STATS.getOpt())) {
try {
AtlasMetrics atlasMetrics = atlasClient.getAtlasMetrics();
String json = AtlasType.toJson(atlasMetrics);
System.out.println(json);
cmdStatus = 0;
} catch (AtlasServiceException e) {
System.err.println("Could not retrieve metrics of the server at " + Arrays.toString(atlasServerUri));
printStandardHttpErrorDetails(e);
}
} else {
System.err.println("Unsupported option. Refer to usage for valid options.");
printUsage(INVALID_OPTIONS_STATUS);
}
return cmdStatus;
}
private void printStandardHttpErrorDetails(AtlasServiceException e) {
System.err.println("Error details: ");
System.err.println("HTTP Status: " + e.getStatus().getStatusCode() + ","
+ e.getStatus().getReasonPhrase());
System.err.println("Exception message: " + e.getMessage());
}
private CommandLine parseCommandLineOptions(String[] args) {
if (args.length == 0) {
printUsage(INVALID_OPTIONS_STATUS);
}
CommandLineParser parser = new GnuParser();
CommandLine commandLine = null;
try {
commandLine = parser.parse(OPTIONS, args);
} catch (ParseException e) {
System.err.println("Could not parse command line options.");
printUsage(INVALID_OPTIONS_STATUS);
}
return commandLine;
}
private void printUsage(int statusCode) {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp("atlas_admin.py", OPTIONS);
System.exit(statusCode);
}
}