trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject [trafficserver] branch master updated: Update runroot to use yaml-cpp library
Date Tue, 26 Jun 2018 15:37:14 GMT
This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new dc7e2ba  Update runroot to use yaml-cpp library
dc7e2ba is described below

commit dc7e2babe0692b97cb3d5a81bd4628017a16f192
Author: Xavier Chi <chitianhaoxp@gmail.com>
AuthorDate: Fri Jun 22 13:48:56 2018 -0500

    Update runroot to use yaml-cpp library
---
 .vscode/c_cpp_properties.json                  |  1 +
 lib/ts/Makefile.am                             |  6 +++--
 lib/ts/runroot.cc                              | 33 +++++++++++---------------
 src/traffic_layout/Makefile.inc                |  6 +++--
 src/traffic_layout/engine.cc                   | 18 ++++++++------
 tests/gold_tests/runroot/runroot_error.test.py |  4 ++--
 6 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
index 1a3185f..bf61087 100644
--- a/.vscode/c_cpp_properties.json
+++ b/.vscode/c_cpp_properties.json
@@ -23,6 +23,7 @@
                 "${workspaceFolder}/lib/cppapi/include",
                 "${workspaceFolder}/lib/records",
                 "${workspaceFolder}/lib/ts",
+                "${workspaceFolder}/lib/yamlcpp/include",
                 "${workspaceFolder}/mgmt",
                 "${workspaceFolder}/mgmt/api",
                 "${workspaceFolder}/mgmt/api/include",
diff --git a/lib/ts/Makefile.am b/lib/ts/Makefile.am
index f87b7b1..797c4e3 100644
--- a/lib/ts/Makefile.am
+++ b/lib/ts/Makefile.am
@@ -35,9 +35,10 @@ AM_CPPFLAGS += \
 	$(iocore_include_dirs) \
 	-I$(abs_top_srcdir)/lib \
 	-I$(abs_top_srcdir)/lib/records \
-	$(TS_INCLUDES)
+	$(TS_INCLUDES) \
+	@YAMLCPP_INCLUDES@
 
-libtsutil_la_LDFLAGS = -no-undefined -version-info @TS_LIBTOOL_VERSION@
+libtsutil_la_LDFLAGS = -no-undefined -version-info @TS_LIBTOOL_VERSION@ @YAMLCPP_LDFLAGS@
 libtsutil_la_LIBADD = \
 	@HWLOC_LIBS@ \
 	@LIBOBJS@ \
@@ -46,6 +47,7 @@ libtsutil_la_LIBADD = \
 	@LIBTCL@ \
 	@LIBRESOLV@ \
 	@LIBCAP@ \
+	@YAMLCPP_LIBS@ \
 	-lc
 
 libtsutil_la_SOURCES = \
diff --git a/lib/ts/runroot.cc b/lib/ts/runroot.cc
index 9624b08..119cd56 100644
--- a/lib/ts/runroot.cc
+++ b/lib/ts/runroot.cc
@@ -45,6 +45,7 @@ datadir, libexecdir, libdir, runtimedir, cachedir.
 #include <fstream>
 #include <set>
 #include <unistd.h>
+#include <yaml-cpp/yaml.h>
 
 static std::string using_runroot = {};
 
@@ -210,27 +211,21 @@ runroot_map_default()
 RunrootMapType
 runroot_map(const std::string &prefix)
 {
-  std::string yaml_path = Layout::relative_to(prefix, "runroot_path.yml");
-  std::ifstream file;
-  file.open(yaml_path);
-  if (!file.good()) {
-    ink_warning("Bad path '%s', continue with default value", prefix.c_str());
-    return RunrootMapType{};
-  }
-
-  std::ifstream yamlfile(yaml_path);
   RunrootMapType map;
-  std::string str;
-  while (std::getline(yamlfile, str)) {
-    int pos                 = str.find(':');
-    map[str.substr(0, pos)] = str.substr(pos + 2);
-  }
-
-  // change it to absolute path in the map
-  for (auto it : map) {
-    if (it.second[0] != '/') {
-      map[it.first] = Layout::relative_to(prefix, it.second);
+  try {
+    YAML::Node yamlfile = YAML::LoadFile(Layout::relative_to(prefix, "runroot_path.yml"));
+    for (auto it : yamlfile) {
+      // key value pairs of dirs
+      std::string value = it.second.as<std::string>();
+      if (value[0] != '/') {
+        value = Layout::relative_to(prefix, value);
+      }
+      map[it.first.as<std::string>()] = value;
     }
+  } catch (YAML::Exception &e) {
+    ink_warning("Unable to read runroot_path.yml from '%s': %s", prefix.c_str(), e.what());
+    ink_notice("Continuing with default value");
+    return RunrootMapType{};
   }
   return map;
 }
diff --git a/src/traffic_layout/Makefile.inc b/src/traffic_layout/Makefile.inc
index eaeacf9..c48b62d 100644
--- a/src/traffic_layout/Makefile.inc
+++ b/src/traffic_layout/Makefile.inc
@@ -24,11 +24,13 @@ traffic_layout_traffic_layout_CPPFLAGS = \
     -I$(abs_top_srcdir)/lib/records \
     -I$(abs_top_srcdir)/mgmt \
     -I$(abs_top_srcdir)/mgmt/utils \
+    @YAMLCPP_INCLUDES@ \
     $(TS_INCLUDES)
 
 traffic_layout_traffic_layout_LDFLAGS =	\
     $(AM_LDFLAGS) \
-    @OPENSSL_LDFLAGS@
+    @OPENSSL_LDFLAGS@ \
+    @YAMLCPP_LDFLAGS@
 
 traffic_layout_traffic_layout_SOURCES = \
 	traffic_layout/traffic_layout.cc \
@@ -45,4 +47,4 @@ traffic_layout_traffic_layout_LDADD = \
 	$(top_builddir)/proxy/shared/libUglyLogStubs.a \
 	$(top_builddir)/iocore/eventsystem/libinkevent.a \
 	$(top_builddir)/lib/ts/libtsutil.la \
-	@LIBTCL@ @HWLOC_LIBS@
+	@LIBTCL@ @HWLOC_LIBS@ @YAMLCPP_LIBS@
diff --git a/src/traffic_layout/engine.cc b/src/traffic_layout/engine.cc
index 1d95d46..0e9a142 100644
--- a/src/traffic_layout/engine.cc
+++ b/src/traffic_layout/engine.cc
@@ -40,6 +40,7 @@
 #include <iostream>
 #include <ftw.h>
 #include <pwd.h>
+#include <yaml-cpp/yaml.h>
 
 // for nftw check_directory
 std::string directory_check = "";
@@ -402,15 +403,18 @@ RunrootEngine::create_runroot()
   // create new root & copy from original to new runroot. then fill in the map
   copy_runroot(original_root, ts_runroot);
 
-  // create and emit to yaml file the key value pairs of path
-  std::ofstream yamlfile;
-  std::string yaml_path = Layout::relative_to(ts_runroot, "runroot_path.yml");
-  yamlfile.open(yaml_path);
-
+  YAML::Emitter yamlfile;
+  // emit key value pairs to the yaml file
+  yamlfile << YAML::BeginMap;
   for (uint i = 0; i < dir_vector.size(); i++) {
-    // out put key value pairs of path
-    yamlfile << dir_vector[i] << ": " << path_map[dir_vector[i]] <<
std::endl;
+    yamlfile << YAML::Key << dir_vector[i];
+    yamlfile << YAML::Value << path_map[dir_vector[i]];
   }
+  yamlfile << YAML::EndMap;
+
+  // create the file
+  std::ofstream f(Layout::relative_to(ts_runroot, "runroot_path.yml"));
+  f << yamlfile.c_str();
 }
 
 // copy the stuff from original_root to ts_runroot
diff --git a/tests/gold_tests/runroot/runroot_error.test.py b/tests/gold_tests/runroot/runroot_error.test.py
index bbd4209..2776243 100644
--- a/tests/gold_tests/runroot/runroot_error.test.py
+++ b/tests/gold_tests/runroot/runroot_error.test.py
@@ -62,9 +62,9 @@ f.Exists = False
 path_invalid = os.path.join(ts_root, "tmp")
 tr = Test.AddTestRun("remove invalid runroot")
 tr.Processes.Default.Command = "$ATS_BIN/traffic_layout remove --path " + path_invalid
-tr.Processes.Default.Streams.All = Testers.ContainsExpression("Bad path", "init incorrect
usage")
+tr.Processes.Default.Streams.All = Testers.ContainsExpression("Unable to read runroot_path.yml",
"remove incorrect usage")
 
 # verify invalid runroot
 tr = Test.AddTestRun("verify invalid runroot")
 tr.Processes.Default.Command = "$ATS_BIN/traffic_layout verify --path " + path_invalid
-tr.Processes.Default.Streams.All = Testers.ContainsExpression("Bad path", "init incorrect
usage")
+tr.Processes.Default.Streams.All = Testers.ContainsExpression("Unable to read runroot_path.yml",
"verify incorrect usage")


Mime
View raw message