001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.runtime.hdinsight.client; 020 021import org.apache.reef.runtime.hdinsight.parameters.*; 022import org.apache.reef.tang.Configuration; 023import org.apache.reef.tang.formats.AvroConfigurationSerializer; 024import org.apache.reef.tang.formats.ConfigurationModule; 025import org.apache.reef.tang.formats.ConfigurationModuleBuilder; 026import org.apache.reef.tang.formats.RequiredParameter; 027 028import java.io.File; 029import java.io.IOException; 030 031/** 032 * Configuration module to setup REEF to submit jobs to HDInsight. 033 */ 034public final class HDInsightRuntimeConfiguration extends ConfigurationModuleBuilder { 035 036 /** 037 * The URL of the hdinsight web service. E.g. http://services.mycompany.com:1234/templeton/v1/ 038 */ 039 public static final RequiredParameter<String> URL = new RequiredParameter<>(); 040 041 /** 042 * The Storage account to be used by Azure. 043 */ 044 public static final RequiredParameter<String> STORAGE_ACCOUNT_NAME = new RequiredParameter<>(); 045 046 /** 047 * The Storage account key to be used by Azure. 048 */ 049 public static final RequiredParameter<String> STORAGE_ACCOUNT_KEY = new RequiredParameter<>(); 050 051 /** 052 * The Container name to be used by Azure. 053 */ 054 public static final RequiredParameter<String> CONTAINER_NAME = new RequiredParameter<>(); 055 056 /** 057 * The username to be used for connecting to hdinsight. 058 */ 059 public static final RequiredParameter<String> USER_NAME = new RequiredParameter<>(); 060 061 /** 062 * The password to be used for connecting to hdinsight. 063 */ 064 public static final RequiredParameter<String> PASSWORD = new RequiredParameter<>(); 065 066 /** 067 * The environment variable that holds the path to the default configuration file. 068 */ 069 public static final String HDINSIGHT_CONFIGURATION_FILE_ENVIRONMENT_VARIABLE = "REEF_HDI_CONF"; 070 071 public static final ConfigurationModule CONF = new HDInsightRuntimeConfiguration() 072 .merge(HDInsightRuntimeConfigurationStatic.CONF) 073 .bindNamedParameter(AzureStorageAccountName.class, STORAGE_ACCOUNT_NAME) 074 .bindNamedParameter(AzureStorageAccountKey.class, STORAGE_ACCOUNT_KEY) 075 .bindNamedParameter(AzureStorageAccountContainerName.class, CONTAINER_NAME) 076 .bindNamedParameter(HDInsightInstanceURL.class, URL) 077 .bindNamedParameter(HDInsightUsername.class, USER_NAME) 078 .bindNamedParameter(HDInsightPassword.class, PASSWORD) 079 .build(); 080 081 /** 082 * Returns a HDInsight runtime configuration from the credentials stored in the given file. 083 * 084 * @param file 085 * @return a HDInsight runtime configuration from the credentials stored in the given file. 086 * @throws IOException if the file can't be read 087 */ 088 public static Configuration fromTextFile(final File file) throws IOException { 089 return new AvroConfigurationSerializer().fromTextFile(file); 090 } 091 092 /** 093 * @return the RuntimeConfiguration that is stored in a file refered to 094 * by the environment variable HDINSIGHT_CONFIGURATION_FILE_ENVIRONMENT_VARIABLE. 095 * @throws IOException 096 * @see HDINSIGHT_CONFIGURATION_FILE_ENVIRONMENT_VARIABLE 097 */ 098 public static Configuration fromEnvironment() throws IOException { 099 100 final String configurationPath = 101 System.getenv(HDINSIGHT_CONFIGURATION_FILE_ENVIRONMENT_VARIABLE); 102 103 if (null == configurationPath) { 104 throw new IOException("Environment Variable " + 105 HDINSIGHT_CONFIGURATION_FILE_ENVIRONMENT_VARIABLE + 106 " not set."); 107 } 108 109 final File configurationFile = new File(configurationPath); 110 if (!configurationFile.canRead()) { 111 throw new IOException("Environment Variable " + 112 HDINSIGHT_CONFIGURATION_FILE_ENVIRONMENT_VARIABLE + 113 " points to a file " + configurationFile.getAbsolutePath() + 114 " which can't be read." 115 ); 116 } 117 118 return fromTextFile(configurationFile); 119 } 120}