You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ao...@apache.org on 2017/09/22 11:04:59 UTC

ambari git commit: AMBARI-22040. configs.py does not work properly when dealing with files (aonishuk)

Repository: ambari
Updated Branches:
  refs/heads/branch-2.6 b2afb01fd -> 29e6213d1


AMBARI-22040. configs.py does not work properly when dealing with files (aonishuk)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/29e6213d
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/29e6213d
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/29e6213d

Branch: refs/heads/branch-2.6
Commit: 29e6213d12facc22045f7c01db8d743440431d47
Parents: b2afb01
Author: Andrew Onishuk <ao...@hortonworks.com>
Authored: Fri Sep 22 14:04:52 2017 +0300
Committer: Andrew Onishuk <ao...@hortonworks.com>
Committed: Fri Sep 22 14:04:52 2017 +0300

----------------------------------------------------------------------
 .../src/main/resources/scripts/configs.py       |  18 +-
 .../src/main/resources/scripts/configs.sh       | 272 +------------------
 2 files changed, 5 insertions(+), 285 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/29e6213d/ambari-server/src/main/resources/scripts/configs.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/scripts/configs.py b/ambari-server/src/main/resources/scripts/configs.py
index 639f28e..8d4de1c 100644
--- a/ambari-server/src/main/resources/scripts/configs.py
+++ b/ambari-server/src/main/resources/scripts/configs.py
@@ -181,7 +181,7 @@ def update_from_file(config_file):
     except Exception as e:
       raise Exception('Cannot find file "{0}" to PUT'.format(config_file))
     try:
-      file_properties = json.loads('{' + file_content + '}')
+      file_properties = json.loads(file_content)
     except Exception as e:
       raise Exception('File "{0}" should be in the following JSON format ("properties_attributes" is optional):\n{1}'.format(config_file, FILE_FORMAT))
     new_properties = file_properties.get(PROPERTIES, {})
@@ -199,26 +199,14 @@ def delete_specific_property(config_name):
     return properties, attributes
   return update
 
-def format_json(dictionary, tab_level=0):
-  output = ''
-  tab = ' ' * 2 * tab_level
-  for key, value in dictionary.iteritems():
-    output += ',\n{0}"{1}": '.format(tab, key)
-    if isinstance(value, dict):
-      output += '{\n' + format_json(value, tab_level + 1) + tab + '}'
-    else:
-      output += '"{0}"'.format(value)
-  output += '\n'
-  return output[2:]
-
 def output_to_file(filename):
   def output(config):
     with open(filename, 'w') as out_file:
-      out_file.write(format_json(config))
+      json.dump(config, out_file, indent=2)
   return output
 
 def output_to_console(config):
-  print format_json(config)
+  print json.dumps(config, indent=2)
 
 def get_config(cluster, config_type, accessor, output):
   properties, attributes = get_current_config(cluster, config_type, accessor)

http://git-wip-us.apache.org/repos/asf/ambari/blob/29e6213d/ambari-server/src/main/resources/scripts/configs.sh
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/scripts/configs.sh b/ambari-server/src/main/resources/scripts/configs.sh
index 5fc96fd..4141fed 100755
--- a/ambari-server/src/main/resources/scripts/configs.sh
+++ b/ambari-server/src/main/resources/scripts/configs.sh
@@ -18,273 +18,5 @@
 # under the License.
 #
 
-usage () {
-  echo "";
-  echo "WARNING: THIS SCRIPT IS DEPRECATED AND DOESN’T SUPPORT NEW FEATURES. PLEASE USE configs.py"
-  echo "";
-  echo "Usage: configs.sh [-u userId] [-p password] [-port port] [-s] <ACTION> <AMBARI_HOST> <CLUSTER_NAME> <CONFIG_TYPE> [CONFIG_FILENAME | CONFIG_KEY [CONFIG_VALUE]]";
-  echo "";
-  echo "       [-u userId]: Optional user ID to use for authentication. Default is 'admin'.";
-  echo "       [-p password]: Optional password to use for authentication. Default is 'admin'.";
-  echo "       [-port port]: Optional port number for Ambari server. Default is '8080'. Provide empty string to not use port.";
-  echo "       [-s]: Optional support of SSL. Default is 'false'. Provide empty string to not use SSL.";
-  echo "       <ACTION>: One of 'get', 'set', 'delete'. 'Set' adds/updates as necessary.";
-  echo "       <AMBARI_HOST>: Server external host name";
-  echo "       <CLUSTER_NAME>: Name given to cluster. Ex: 'c1'"
-  echo "       <CONFIG_TYPE>: One of the various configuration types in Ambari. Ex:global, core-site, hdfs-site, mapred-queue-acls, etc.";
-  echo "       [CONFIG_FILENAME]: File where entire configurations are saved to, or read from. Only applicable to 'get' and 'set' actions";
-  echo "       [CONFIG_KEY]: Key that has to be set or deleted. Not necessary for 'get' action.";
-  echo "       [CONFIG_VALUE]: Optional value to be set. Not necessary for 'get' or 'delete' actions.";
-  exit 1;
-}
-
-USERID="admin"
-PASSWD="admin"
-PORT=":8080"
-SSL_URL_PREFIX=""
-
-if [ "$1" == "-u" ] ; then
-  USERID=$2;
-  shift 2;
-  echo "USERID=$USERID";
-fi
-
-if [ "$1" == "-p" ] ; then
-  PASSWD=$2;
-  shift 2;
-  echo "PASSWORD=$PASSWD";
-fi
-
-if [ "$1" == "-port" ] ; then
-  if [ -z $2 ]; then
-    PORT="";
-  else
-    PORT=":$2";
-  fi
-  shift 2;
-  echo "PORT=$PORT";
-fi
-
-if [ "$1" == "-s" ] ; then
-  SSL_URL_PREFIX="s"
-  shift;
-  echo "SSL is enabled";
-fi
-
-AMBARIURL="http$SSL_URL_PREFIX://$2$PORT"
-CLUSTER=$3
-SITE=$4
-SITETAG=''
-CONFIGKEY=$5
-CONFIGVALUE=$6
-
-###################
-## currentSiteTag()
-###################
-currentSiteTag () {
-  currentSiteTag=''
-  found=''
-    
-  #currentSite=`cat ds.json | grep -E "$SITE|tag"`; 
-  currentSite=`curl -k -s -u $USERID:$PASSWD "$AMBARIURL/api/v1/clusters/$CLUSTER?fields=Clusters/desired_configs" | grep -E "$SITE|tag"`;
-  for line in $currentSite; do
-    if [ $line != "{" -a $line != ":" -a $line != '"tag"' ] ; then
-      if [ -n "$found" -a -z "$currentSiteTag" ]; then
-        currentSiteTag=$line;
-      fi
-      if [ $line == "\"$SITE\"" ]; then
-        found=$SITE; 
-      fi
-    fi
-  done;
-  if [ -z $currentSiteTag ]; then
-    errOutput=`curl -k -s -u $USERID:$PASSWD "$AMBARIURL/api/v1/clusters/$CLUSTER?fields=Clusters/desired_configs"`;
-    echo "[ERROR] \"$SITE\" not found in server response.";
-    echo "[ERROR] Output of \`curl -k -s -u $USERID:$PASSWD \"$AMBARIURL/api/v1/clusters/$CLUSTER?fields=Clusters/desired_configs\"\` is:";
-    echo $errOutput | while read -r line; do
-      echo "[ERROR] $line";
-    done;
-    exit 1;
-  fi
-  currentSiteTag=`echo $currentSiteTag|cut -d \" -f 2`
-  SITETAG=$currentSiteTag;
-}
-
-#############################################
-## doConfigUpdate() 
-##  @param MODE of update. Either 'set' or 'delete'
-#############################################
-doConfigUpdate () {
-  MODE=$1
-  currentSiteTag
-  echo "########## Performing '$MODE' $CONFIGKEY:$CONFIGVALUE on (Site:$SITE, Tag:$SITETAG)";
-  propertiesStarted=0
-  attributesStarted=0
-  currentLevel=0
-  curl -k -s -u $USERID:$PASSWD "$AMBARIURL/api/v1/clusters/$CLUSTER/configurations?type=$SITE&tag=$SITETAG" | while read -r line; do
-    if [ "$propertiesStarted" -eq 0 -a "$attributesStarted" -eq 0 ]; then
-      if [ "$line" = "\"properties_attributes\" : {" ]; then
-        attributesStarted=$currentLevel
-      elif [ "$line" = "\"properties\" : {" ]; then
-        propertiesStarted=$currentLevel
-      fi
-    fi
-    if [ "$propertiesStarted" -gt 0 ]; then
-      if [ "`echo $line | grep -E "},?$"`" ]; then
-        ## Properties ended
-        ## Add property
-        propLen=${#newProperties}
-        lastChar=${newProperties:$propLen-1:1}
-        if [ "$MODE" == "delete" ]; then
-          # Remove the last ,
-          if [ "$lastChar" == "," ]; then
-            newProperties=${newProperties:0:$propLen-1}
-          fi
-        elif [ "$MODE" == "set" ]; then
-          # Add comma if required
-          if [ "$lastChar" != ","  -a "$lastChar" != "{" ]; then
-            newProperties="$newProperties,"
-          fi
-          newProperties="$newProperties \"$CONFIGKEY\" : \"$CONFIGVALUE\""
-        fi
-        newProperties=$newProperties$line
-        propertiesStarted=0
-      elif [ "`echo $line | grep "\\\"$CONFIGKEY\\\""`" ]; then
-        echo "########## Config found. Skipping origin value"
-      else
-        newProperties=$newProperties$line
-      fi
-    elif [ "$attributesStarted" -gt 0 ]; then
-      newProperties=$newProperties$line
-    fi
-    if [ "`echo $line | grep -E "{$"`" ]; then
-        currentLevel=$((currentLevel+1))
-    elif [ "`echo $line | grep -E "},?$"`" ]; then
-        currentLevel=$((currentLevel-1))
-        if [ "$currentLevel" == 1 ]; then
-          # if no properties in current config
-          if [ "$MODE" == "set" -a -z "$newProperties" ]; then
-            newProperties="\"properties\" : { \"$CONFIGKEY\" : \"$CONFIGVALUE\"}"
-          fi
-          newTag=`date "+%s%N"`
-          newTag="version${newTag}"
-          finalJson="{ \"Clusters\": { \"desired_config\": {\"type\": \"$SITE\", \"tag\":\"$newTag\", $newProperties}}}"
-          newFile="doSet_$newTag.json"
-          echo "########## PUTting json into: $newFile"
-          echo "$finalJson" > $newFile
-          curl -k -u $USERID:$PASSWD -X PUT -H "X-Requested-By: ambari" "$AMBARIURL/api/v1/clusters/$CLUSTER" --data @$newFile
-          currentSiteTag
-          echo "########## NEW Site:$SITE, Tag:$SITETAG";
-        fi
-    fi
-    if [ "$attributesStarted" -eq "$currentLevel" ]; then
-      attributesStarted=0
-    fi
-  done
-}
-
-#############################################
-## doConfigFileUpdate() 
-##  @param File name to PUT on server
-#############################################
-doConfigFileUpdate () {
-  FILENAME=$1
-  if [ -f $FILENAME ]; then
-    if [ "1" == "$(grep -En ^\"properties\" $FILENAME | cut -d : -f 1)" ]; then
-      newTag=`date "+%s%N"`
-      newTag="version${newTag}"
-      newProperties=`cat $FILENAME`;
-      finalJson="{ \"Clusters\": { \"desired_config\": {\"type\": \"$SITE\", \"tag\":\"$newTag\", $newProperties}}}"
-      newFile="doSet_$newTag.json"
-      echo "$finalJson" > $newFile
-      echo "########## PUTting file:\"$FILENAME\" into config(type:\"$SITE\", tag:$newTag) via $newFile"
-      curl -k -u $USERID:$PASSWD -X PUT -H "X-Requested-By: ambari" "$AMBARIURL/api/v1/clusters/$CLUSTER" --data @$newFile
-      currentSiteTag
-      echo "########## NEW Site:$SITE, Tag:$SITETAG";
-    else
-      echo "[ERROR] File \"$FILENAME\" should be in the following JSON format (\"properties_attributes\" is optional):";
-      echo "[ERROR]   \"properties\": {";
-      echo "[ERROR]     \"key1\": \"value1\",";
-      echo "[ERROR]     \"key2\": \"value2\",";
-      echo "[ERROR]   },";
-      echo "[ERROR]   \"properties_attributes\": {";
-      echo "[ERROR]     \"final\": {";
-      echo "[ERROR]       \"key1\": \"value1\",";
-      echo "[ERROR]       \"key2\": \"value2\",";
-      echo "[ERROR]     }";
-      echo "[ERROR]   }";
-      exit 1;
-    fi
-  else
-    echo "[ERROR] Cannot find file \"$1\"to PUT";
-    exit 1;
-  fi
-}
-
-
-#############################################
-## doGet()
-##  @param Optional filename to save to
-#############################################
-doGet () {
-  FILENAME=$1
-  if [ -n $FILENAME -a -f $FILENAME ]; then
-    rm -f $FILENAME
-  fi
-  currentSiteTag
-  echo "########## Performing 'GET' on (Site:$SITE, Tag:$SITETAG)";
-  propertiesStarted=0
-  curl -k -s -u $USERID:$PASSWD "$AMBARIURL/api/v1/clusters/$CLUSTER/configurations?type=$SITE&tag=$SITETAG" | while read -r line; do
-    # echo ">>> $line";
-    if [ "$propertiesStarted" -eq 0 ]; then
-      if [ "`echo $line | grep "\"properties\""`" -o "`echo $line | grep "\"properties_attributes\""`" ]; then
-        propertiesStarted=$currentLevel
-      fi
-    fi
-    if [ "$propertiesStarted" -gt "0" ]; then
-      if [ -z $FILENAME ]; then
-        echo "$line"
-      else
-        echo "$line" >> $FILENAME
-      fi
-    fi
-    if [ "`echo $line | grep -E "{$"`" ]; then
-        currentLevel=$((currentLevel+1))
-    elif [ "`echo $line | grep -E "},?$"`" ]; then
-        currentLevel=$((currentLevel-1))
-    fi
-    if [ "$propertiesStarted" -eq "$currentLevel" ]; then
-      propertiesStarted=0
-    fi
-  done
-}
-
-case "$1" in
-  set)
-    if (($# == 6)); then
-      doConfigUpdate "set" # Individual key
-    elif (($# == 5)); then
-      doConfigFileUpdate $5 # File based
-    else
-      usage
-    fi
-    ;;
-  get)
-    if (($# == 4)); then
-      doGet
-    elif (($# == 5)); then
-      doGet $5
-    else
-      usage
-    fi
-    ;;
-  delete)
-    if (($# != 5)); then
-      usage
-    fi
-    doConfigUpdate "delete"
-    ;;
-  *) 
-    usage
-    ;;
-esac
+echo "ERROR: THIS SCRIPT IS NO LONGER SUPPORTED. PLEASE USE configs.py INSTEAD"
+exit 1
\ No newline at end of file