PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.0.0-rc0/src/java/org/apache/hcatalog/cli/HCatDriver.java

#
Java | 129 lines | 91 code | 15 blank | 23 comment | 21 complexity | 1fbcf18b6842dbeb8cf4d803477f7bc4 MD5 | raw file
  1/*
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.apache.hcatalog.cli;
 19
 20import org.apache.hadoop.conf.Configuration;
 21import org.apache.hadoop.fs.FileSystem;
 22import org.apache.hadoop.fs.Path;
 23import org.apache.hadoop.fs.permission.FsPermission;
 24import org.apache.hadoop.hive.metastore.MetaStoreUtils;
 25import org.apache.hadoop.hive.metastore.Warehouse;
 26import org.apache.hadoop.hive.ql.Driver;
 27import org.apache.hadoop.hive.ql.metadata.Hive;
 28import org.apache.hadoop.hive.ql.metadata.HiveException;
 29import org.apache.hadoop.hive.ql.metadata.Table;
 30import org.apache.hadoop.hive.ql.processors.CommandProcessorResponse;
 31import org.apache.hadoop.hive.ql.session.SessionState;
 32import org.apache.hcatalog.common.HCatConstants;
 33
 34public class HCatDriver extends Driver {
 35
 36  @Override
 37  public CommandProcessorResponse run(String command) {
 38
 39    int ret = super.run(command).getResponseCode();
 40
 41    SessionState ss = SessionState.get();
 42
 43    if (ret == 0){
 44      // Only attempt to do this, if cmd was successful.
 45      ret = setFSPermsNGrp(ss);
 46    }
 47    // reset conf vars
 48    ss.getConf().set(HCatConstants.HCAT_CREATE_DB_NAME, "");
 49    ss.getConf().set(HCatConstants.HCAT_CREATE_TBL_NAME, "");
 50
 51    return new CommandProcessorResponse(ret);
 52  }
 53
 54  private int setFSPermsNGrp(SessionState ss) {
 55
 56    Configuration conf =ss.getConf();
 57
 58    String tblName = conf.get(HCatConstants.HCAT_CREATE_TBL_NAME,"");
 59    String dbName = conf.get(HCatConstants.HCAT_CREATE_DB_NAME, "");
 60    String grp = conf.get(HCatConstants.HCAT_GROUP,null);
 61    String permsStr = conf.get(HCatConstants.HCAT_PERMS,null);
 62
 63    if(tblName.isEmpty() && dbName.isEmpty()){
 64      // it wasn't create db/table
 65      return 0;
 66    }
 67
 68    if(null == grp && null == permsStr) {
 69      // there were no grp and perms to begin with.
 70      return 0;
 71    }
 72
 73    FsPermission perms = FsPermission.valueOf(permsStr);
 74    if(!tblName.isEmpty()){
 75      Hive db = null;
 76      try{
 77        db = Hive.get();
 78        Table tbl =  db.getTable(tblName);
 79        Path tblPath = tbl.getPath();
 80
 81        FileSystem fs = tblPath.getFileSystem(conf);
 82        if(null != perms){
 83          fs.setPermission(tblPath, perms);
 84        }
 85        if(null != grp){
 86          fs.setOwner(tblPath, null, grp);
 87        }
 88        return 0;
 89
 90      } catch (Exception e){
 91          ss.err.println(String.format("Failed to set permissions/groups on TABLE: <%s> %s",tblName,e.getMessage()));
 92          try {  // We need to drop the table.
 93            if(null != db){ db.dropTable(tblName); }
 94          } catch (HiveException he) {
 95            ss.err.println(String.format("Failed to drop TABLE <%s> after failing to set permissions/groups on it. %s",tblName,e.getMessage()));
 96          }
 97          return 1;
 98      }
 99    }
100    else{
101      // looks like a db operation
102      if (dbName.isEmpty() || dbName.equals(MetaStoreUtils.DEFAULT_DATABASE_NAME)){
103        // We dont set perms or groups for default dir.
104        return 0;
105      }
106      else{
107        try{
108          Path dbPath = new Warehouse(conf).getDefaultDatabasePath(dbName);
109          FileSystem fs = dbPath.getFileSystem(conf);
110          if(perms != null){
111            fs.setPermission(dbPath, perms);
112          }
113          if(null != grp){
114            fs.setOwner(dbPath, null, grp);
115          }
116          return 0;
117        } catch (Exception e){
118          ss.err.println(String.format("Failed to set permissions and/or group on DB: <%s> %s", dbName, e.getMessage()));
119          try {
120            Hive.get().dropDatabase(dbName);
121          } catch (Exception e1) {
122            ss.err.println(String.format("Failed to drop DB <%s> after failing to set permissions/group on it. %s", dbName, e1.getMessage()));
123          }
124          return 1;
125        }
126      }
127    }
128  }
129}