浏览代码

AMBARI-246. Add support for parsing yum repo files to understand how to bootstrap nodes for local yum mirror

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/branches/ambari-186@1339733 13f79535-47bb-0310-9956-ffa450edef68
Ramya Sunil 13 年之前
父节点
当前提交
e807fa1edd
共有 2 个文件被更改,包括 125 次插入0 次删除
  1. 3 0
      CHANGES.txt
  2. 122 0
      hmc/php/util/YumRepoConfigParser.php

+ 3 - 0
CHANGES.txt

@@ -2,6 +2,9 @@ Ambari Change log
 
 Release 0.x.x - unreleased
 
+  AMBARI-246. Add support for parsing yum repo files to understand how to
+  bootstrap nodes for local yum mirror (Hitesh via ramya)
+
   AMBARI-218. Install Combo-Handler On HMC Webserver To Drastically Speed Up
   Page Load Times (Varun Kapoor via ramya)
 

+ 122 - 0
hmc/php/util/YumRepoConfigParser.php

@@ -0,0 +1,122 @@
+<?php
+
+/*
+ * 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.
+ */
+
+function getEnabledGpgKeyLocations($repoFile) {
+  $logger = new HMCLogger("YumRepoConfigParser");
+
+  $logger->log_info("Parsing gpg key info from " . $repoFile);
+  if (!file_exists($repoFile)) {
+    $logger->log_error("Invalid repo file provided, file=" . $repoFile);
+    return FALSE;
+  }
+
+  $fileContents = file($repoFile,
+      FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
+
+  $logger->log_debug("Repo file contents, file=" . $repoFile
+      . ", contents=" . print_r($fileContents, true));
+
+  $response = array();
+
+  $currentRepoId = "";
+  $currentRepoEnabled = 1;
+  $currentGpgCheck = -1;
+  $currentGpgLocation = "";
+  $globalGpgCheck = 0;
+
+  foreach ($fileContents as $fLine) {
+    $line = trim($fLine);
+    if (substr($line, 0, 1) == "#") {
+      continue;
+    }
+
+    $matches = array();
+    $logger->log_debug("Parsing repo file, file=" . $repoFile
+        . ", line=" . $line);
+    $logger->log_debug("Current: "
+        . ", repoId=" . $currentRepoId
+        . ", repoEnabled=" . $currentRepoEnabled
+        . ", gpgCheckEnabled=" . $currentGpgCheck
+        . ", gpgCheckLocation=" . $currentGpgLocation);
+
+    if (preg_match("/\[(.*)\]/", $line, $matches) > 0) {
+      $newRepoId = $matches[1];
+      $logger->log_debug("Found new repo id in repo file, file=" . $repoFile
+          . ", repoId=" . $newRepoId);
+      if ($currentRepoEnabled == 1
+          && (($currentGpgCheck == -1 && $globalGpgCheck == 1)
+              || ($currentGpgCheck == 1))) {
+        if ($currentGpgLocation != "") {
+          $response[$currentRepoId] = array ( "gpgkey" => $currentGpgLocation);
+        }
+      } else if ($currentRepoId != ""
+          && $currentRepoId != "main") {
+        $logger->log_debug("Skipping repo as repo/check not enabled"
+            .", id=" . $currentRepoId);
+      }
+      $currentRepoId = $newRepoId;
+      $currentGpgLocation = "";
+      $currentGpgCheck = -1;
+      $currentRepoEnabled = -1;
+      continue;
+    }
+
+    $eIdx = strpos($line, "=");
+    if ($eIdx === FALSE) {
+      $logger->log_warn("Invalid line when parsing repo file, file=" . $repoFile
+          . ", line=" . $line);
+      continue;
+    }
+
+    $key = trim(substr($line, 0, $eIdx));
+    $val = trim(substr($line, $eIdx+1));
+
+    $logger->log_debug("Parsed line, key=" . $key . ", val=" . $val);
+
+    if ($key == "gpgcheck") {
+      if ($currentRepoId == "main") {
+        $globalGpgCheck = intval($val);
+      } else {
+        $currentGpgCheck = intval($val);
+      }
+    } else if ($key == "enabled") {
+      $currentRepoEnabled = intval($val);
+    } else if ($key == "gpgkey") {
+      $currentGpgLocation = $val;
+    }
+  }
+
+  if ($currentRepoEnabled == 1
+      && (($currentGpgCheck == -1 && $globalGpgCheck == 1)
+           || ($currentGpgCheck == 1))) {
+    if ($currentGpgLocation != "") {
+      $response[$currentRepoId] = array ( "gpgkey" => $currentGpgLocation);
+    }
+  } else if ($currentRepoId != ""
+      && $currentRepoId != "main") {
+    $logger->log_debug("Skipping repo as repo/check not enabled"
+        .", id=" . $currentRepoId);
+  }
+
+  return $response;
+}
+
+
+?>