View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.accumulo.core.cli;
18  
19  import java.nio.charset.Charset;
20  import java.util.Iterator;
21  import java.util.Map.Entry;
22  import java.util.TreeMap;
23  import java.util.UUID;
24  
25  import org.apache.accumulo.core.Constants;
26  import org.apache.accumulo.core.client.AccumuloException;
27  import org.apache.accumulo.core.client.AccumuloSecurityException;
28  import org.apache.accumulo.core.client.Connector;
29  import org.apache.accumulo.core.client.Instance;
30  import org.apache.accumulo.core.client.ZooKeeperInstance;
31  import org.apache.accumulo.core.client.mapreduce.AccumuloInputFormat;
32  import org.apache.accumulo.core.client.mapreduce.AccumuloOutputFormat;
33  import org.apache.accumulo.core.client.mock.MockInstance;
34  import org.apache.accumulo.core.conf.AccumuloConfiguration;
35  import org.apache.accumulo.core.conf.DefaultConfiguration;
36  import org.apache.accumulo.core.conf.Property;
37  import org.apache.accumulo.core.security.Authorizations;
38  import org.apache.accumulo.core.security.ColumnVisibility;
39  import org.apache.accumulo.core.security.CredentialHelper;
40  import org.apache.accumulo.core.security.thrift.Credential;
41  import org.apache.accumulo.core.security.tokens.PasswordToken;
42  import org.apache.accumulo.core.security.tokens.SecurityToken;
43  import org.apache.accumulo.trace.instrument.Trace;
44  import org.apache.hadoop.conf.Configuration;
45  import org.apache.hadoop.fs.Path;
46  import org.apache.hadoop.mapreduce.Job;
47  import org.apache.log4j.Level;
48  import org.apache.log4j.Logger;
49  
50  import com.beust.jcommander.IStringConverter;
51  import com.beust.jcommander.Parameter;
52  
53  public class ClientOpts extends Help {
54    
55    public static class TimeConverter implements IStringConverter<Long> {
56      @Override
57      public Long convert(String value) {
58        return AccumuloConfiguration.getTimeInMillis(value);
59      }
60    }
61    
62    public static class MemoryConverter implements IStringConverter<Long> {
63      @Override
64      public Long convert(String value) {
65        return AccumuloConfiguration.getMemoryInBytes(value);
66      }
67    }
68    
69    public static class AuthConverter implements IStringConverter<Authorizations> {
70      @Override
71      public Authorizations convert(String value) {
72        return new Authorizations(value.split(","));
73      }
74    }
75    
76    public static class Password {
77      public byte[] value;
78      
79      public Password(String dfault) {
80        value = dfault.getBytes(Charset.forName("UTF-8"));
81      }
82      
83      @Override
84      public String toString() {
85        return new String(value);
86      }
87    }
88    
89    public static class PasswordConverter implements IStringConverter<Password> {
90      @Override
91      public Password convert(String value) {
92        return new Password(value);
93      }
94    }
95    
96    public static class VisibilityConverter implements IStringConverter<ColumnVisibility> {
97      @Override
98      public ColumnVisibility convert(String value) {
99        return new ColumnVisibility(value);
100     }
101   }
102   
103   @Parameter(names = {"-u", "--user"}, description = "Connection user")
104   public String principal = System.getProperty("user.name");
105   
106   @Parameter(names = "-p", converter = PasswordConverter.class, description = "Connection password")
107   public Password password = new Password("secret");
108   
109   @Parameter(names = "--password", converter = PasswordConverter.class, description = "Enter the connection password", password = true)
110   public Password securePassword = null;
111   
112   public SecurityToken getToken() {
113     PasswordToken pt = new PasswordToken();
114     if (securePassword == null) {
115       if (password.value == null)
116         return null;
117       return pt.setPassword(password.value);
118     }
119     return pt.setPassword(securePassword.value);
120   }
121   
122   @Parameter(names = {"-z", "--keepers"}, description = "Comma separated list of zookeeper hosts (host:port,host:port)")
123   public String zookeepers = "localhost:2181";
124   
125   @Parameter(names = {"-i", "--instance"}, description = "The name of the accumulo instance")
126   public String instance = null;
127   
128   @Parameter(names = {"-auths", "--auths"}, converter = AuthConverter.class, description = "the authorizations to use when reading or writing")
129   public Authorizations auths = Constants.NO_AUTHS;
130   
131   @Parameter(names = "--debug", description = "turn on TRACE-level log messages")
132   public boolean debug = false;
133   
134   @Parameter(names = {"-fake", "--mock"}, description = "Use a mock Instance")
135   public boolean mock = false;
136   
137   @Parameter(names = "--site-file", description = "Read the given accumulo site file to find the accumulo instance")
138   public String siteFile = null;
139   
140   public void startDebugLogging() {
141     if (debug)
142       Logger.getLogger(Constants.CORE_PACKAGE_NAME).setLevel(Level.TRACE);
143   }
144   
145   @Parameter(names = "--trace", description = "turn on distributed tracing")
146   public boolean trace = false;
147   
148   public void startTracing(String applicationName) {
149     if (trace) {
150       Trace.on(applicationName);
151     }
152   }
153   
154   public void stopTracing() {
155     Trace.off();
156   }
157   
158   @Override
159   public void parseArgs(String programName, String[] args, Object... others) {
160     super.parseArgs(programName, args, others);
161     startDebugLogging();
162     startTracing(programName);
163   }
164   
165   protected Instance cachedInstance = null;
166   
167   @SuppressWarnings("deprecation")
168   synchronized public Instance getInstance() {
169     if (cachedInstance != null)
170       return cachedInstance;
171     if (mock)
172       return cachedInstance = new MockInstance(instance);
173     if (siteFile != null) {
174       AccumuloConfiguration config = new AccumuloConfiguration() {
175         Configuration xml = new Configuration();
176         {
177           xml.addResource(new Path(siteFile));
178         }
179         
180         @Override
181         public Iterator<Entry<String,String>> iterator() {
182           TreeMap<String,String> map = new TreeMap<String,String>();
183           for (Entry<String,String> props : DefaultConfiguration.getInstance())
184             map.put(props.getKey(), props.getValue());
185           for (Entry<String,String> props : xml)
186             map.put(props.getKey(), props.getValue());
187           return map.entrySet().iterator();
188         }
189         
190         @Override
191         public String get(Property property) {
192           String value = xml.get(property.getKey());
193           if (value != null)
194             return value;
195           return DefaultConfiguration.getInstance().get(property);
196         }
197       };
198       this.zookeepers = config.get(Property.INSTANCE_ZK_HOST);
199       Path instanceDir = new Path(config.get(Property.INSTANCE_DFS_DIR), "instance_id");
200       return cachedInstance = new ZooKeeperInstance(UUID.fromString(ZooKeeperInstance.getInstanceIDFromHdfs(instanceDir)), zookeepers);
201     }
202     return cachedInstance = new ZooKeeperInstance(this.instance, this.zookeepers);
203   }
204   
205   public Connector getConnector() throws AccumuloException, AccumuloSecurityException {
206     return getInstance().getConnector(this.principal, this.getToken());
207   }
208   
209   public Credential getCredentials() throws AccumuloSecurityException {
210     return CredentialHelper.create(principal, getToken(), getInstance().getInstanceID());
211   }
212   
213   public void setAccumuloConfigs(Job job) throws AccumuloSecurityException {
214     AccumuloInputFormat.setZooKeeperInstance(job, instance, zookeepers);
215     AccumuloOutputFormat.setZooKeeperInstance(job, instance, zookeepers);
216   }
217   
218 }