You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ya...@apache.org on 2015/08/14 00:18:45 UTC

[1/5] mesos git commit: Added AppcImageManifest protobuf.

Repository: mesos
Updated Branches:
  refs/heads/master b62c6e6be -> dedd1ed69


Added AppcImageManifest protobuf.

Review: https://reviews.apache.org/r/37308


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/28d3041e
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/28d3041e
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/28d3041e

Branch: refs/heads/master
Commit: 28d3041e6f31d55c321917a44d7f500469513282
Parents: e90c448
Author: Jiang Yan Xu <ya...@jxu.me>
Authored: Mon Aug 10 10:51:44 2015 -0700
Committer: Jiang Yan Xu <ya...@jxu.me>
Committed: Thu Aug 13 15:15:19 2015 -0700

----------------------------------------------------------------------
 include/mesos/mesos.proto | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/28d3041e/include/mesos/mesos.proto
----------------------------------------------------------------------
diff --git a/include/mesos/mesos.proto b/include/mesos/mesos.proto
index 7e2d66c..62bb3e7 100644
--- a/include/mesos/mesos.proto
+++ b/include/mesos/mesos.proto
@@ -1387,3 +1387,36 @@ message DiscoveryInfo {
   optional Ports ports = 6;
   optional Labels labels = 7;
 }
+
+
+/**
+* Protobuf for the Appc image manifest JSON schema:
+* https://github.com/appc/spec/blob/master/spec/aci.md#image-manifest-schema
+* Where possible, any field required in the schema is required in the protobuf
+* but some cannot be expressed, e.g., a repeated string that has at least one
+* element. Further validation should be performed after parsing the JSON into
+* the protobuf.
+* This version of Appc protobuf is based on Appc spec version 0.6.1.
+* TODO(xujyan): This protobuf currently defines a subset of fields in the spec
+* that Mesos makes use of to avoid confusion. New fields are going to be added
+* when Mesos starts to support them.
+*/
+message AppcImageManifest {
+  required string acKind = 1;
+  required string acVersion = 2;
+  required string name = 3;
+
+  message Label {
+    required string name = 1;
+    required string value = 2;
+  }
+
+  repeated Label labels = 4;
+
+  message Annotation {
+    required string name = 1;
+    required string value = 2;
+  }
+
+  repeated Annotation annotations = 5;
+}


[4/5] mesos git commit: Added Appc spec validation utility.

Posted by ya...@apache.org.
Added Appc spec validation utility.

Review: https://reviews.apache.org/r/37310


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

Branch: refs/heads/master
Commit: ab75d1d662e1506550f277e6403d1bd25aa060d4
Parents: a070de3
Author: Jiang Yan Xu <ya...@jxu.me>
Authored: Mon Aug 10 11:15:46 2015 -0700
Committer: Jiang Yan Xu <ya...@jxu.me>
Committed: Thu Aug 13 15:16:22 2015 -0700

----------------------------------------------------------------------
 src/Makefile.am                                 |   3 +
 .../containerizer/provisioners/appc/spec.cpp    | 104 +++++++++++++++++
 .../containerizer/provisioners/appc/spec.hpp    |  54 +++++++++
 .../containerizer/appc_provisioner_tests.cpp    | 114 +++++++++++++++++++
 4 files changed, 275 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/ab75d1d6/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index f8e54d2..d664df6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -458,6 +458,7 @@ libmesos_no_3rdparty_la_SOURCES =					\
 	slave/containerizer/mesos/launch.cpp				\
 	slave/containerizer/provisioner.cpp				\
 	slave/containerizer/provisioners/appc/paths.cpp			\
+	slave/containerizer/provisioners/appc/spec.cpp			\
 	slave/resource_estimators/noop.cpp				\
 	usage/usage.cpp							\
 	v1/attributes.cpp						\
@@ -713,6 +714,7 @@ libmesos_no_3rdparty_la_SOURCES +=					\
 	slave/containerizer/linux_launcher.hpp				\
 	slave/containerizer/provisioner.hpp				\
 	slave/containerizer/provisioners/appc/paths.hpp			\
+	slave/containerizer/provisioners/appc/spec.hpp			\
 	slave/containerizer/isolators/posix.hpp				\
 	slave/containerizer/isolators/posix/disk.hpp			\
 	slave/containerizer/isolators/cgroups/constants.hpp		\
@@ -1619,6 +1621,7 @@ mesos_tests_SOURCES =						\
   tests/zookeeper_url_tests.cpp					\
   tests/common/http_tests.cpp					\
   tests/common/recordio_tests.cpp				\
+  tests/containerizer/appc_provisioner_tests.cpp		\
   tests/containerizer/composing_containerizer_tests.cpp		\
   tests/containerizer/docker_containerizer_tests.cpp		\
   tests/containerizer/docker_tests.cpp				\

http://git-wip-us.apache.org/repos/asf/mesos/blob/ab75d1d6/src/slave/containerizer/provisioners/appc/spec.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/provisioners/appc/spec.cpp b/src/slave/containerizer/provisioners/appc/spec.cpp
new file mode 100644
index 0000000..15a3257
--- /dev/null
+++ b/src/slave/containerizer/provisioners/appc/spec.cpp
@@ -0,0 +1,104 @@
+/**
+ * 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.
+ */
+
+#include <stout/os/stat.hpp>
+#include <stout/protobuf.hpp>
+#include <stout/strings.hpp>
+
+#include "slave/containerizer/provisioners/appc/paths.hpp"
+#include "slave/containerizer/provisioners/appc/spec.hpp"
+
+using std::string;
+
+namespace mesos {
+namespace internal {
+namespace slave {
+namespace appc {
+namespace spec {
+
+Option<Error> validateManifest(const AppcImageManifest& manifest)
+{
+  // TODO(idownes): Validate that required fields are present when
+  // this cannot be expressed in the protobuf specification, e.g.,
+  // repeated fields with >= 1.
+  // TODO(xujyan): More thorough type validation:
+  // https://github.com/appc/spec/blob/master/spec/types.md
+  if (manifest.ackind() != "ImageManifest") {
+    return Error("Incorrect acKind field: " + manifest.ackind());
+  }
+
+  return None();
+}
+
+
+Option<Error> validateImageID(const string& imageId)
+{
+  if (!strings::startsWith(imageId, "sha512-")) {
+    return Error("Image ID needs to start with sha512-");
+  }
+
+  string hash = strings::remove(imageId, "sha512-", strings::PREFIX);
+  if (hash.length() != 128) {
+    return Error("Invalid hash length for: " + hash);
+  }
+
+  return None();
+}
+
+
+Option<Error> validateLayout(const string& imagePath)
+{
+  if (!os::stat::isdir(paths::getImageRootfsPath(imagePath))) {
+    return Error("No rootfs directory found in image layout");
+  }
+
+  if (!os::stat::isfile(paths::getImageManifestPath(imagePath))) {
+    return Error("No manifest found in image layout");
+  }
+
+  return None();
+}
+
+
+Try<AppcImageManifest> parse(const string& value)
+{
+  Try<JSON::Object> json = JSON::parse<JSON::Object>(value);
+  if (json.isError()) {
+    return Error("JSON parse failed: " + json.error());
+  }
+
+  Try<AppcImageManifest> manifest =
+    protobuf::parse<AppcImageManifest>(json.get());
+
+  if (manifest.isError()) {
+    return Error("Protobuf parse failed: " + manifest.error());
+  }
+
+  Option<Error> error = validateManifest(manifest.get());
+  if (error.isSome()) {
+    return Error("Schema validation failed: " + error.get().message);
+  }
+
+  return manifest.get();
+}
+
+} // namespace spec {
+} // namespace appc {
+} // namespace slave {
+} // namespace internal {
+} // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/ab75d1d6/src/slave/containerizer/provisioners/appc/spec.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/provisioners/appc/spec.hpp b/src/slave/containerizer/provisioners/appc/spec.hpp
new file mode 100644
index 0000000..5e4308a
--- /dev/null
+++ b/src/slave/containerizer/provisioners/appc/spec.hpp
@@ -0,0 +1,54 @@
+/**
+ * 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.
+ */
+
+#ifndef __MESOS_APPC_SPEC__
+#define __MESOS_APPC_SPEC__
+
+#include <string>
+
+#include <stout/error.hpp>
+#include <stout/option.hpp>
+
+#include <mesos/mesos.hpp>
+
+namespace mesos {
+namespace internal {
+namespace slave {
+namespace appc {
+namespace spec {
+
+// Validate if the specified image manifest conforms to the Appc spec.
+Option<Error> validateManifest(const AppcImageManifest& manifest);
+
+// Validate if the specified image ID conforms to the Appc spec.
+Option<Error> validateImageID(const std::string& imageId);
+
+// Validate if the specified image has the disk layout that conforms
+// to the Appc spec.
+Option<Error> validateLayout(const std::string& imagePath);
+
+// Parse the AppcImageManifest in the specified JSON string.
+Try<AppcImageManifest> parse(const std::string& value);
+
+} // namespace spec {
+} // namespace appc {
+} // namespace slave {
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __MESOS_APPC_SPEC__

http://git-wip-us.apache.org/repos/asf/mesos/blob/ab75d1d6/src/tests/containerizer/appc_provisioner_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/appc_provisioner_tests.cpp b/src/tests/containerizer/appc_provisioner_tests.cpp
new file mode 100644
index 0000000..1335cfd
--- /dev/null
+++ b/src/tests/containerizer/appc_provisioner_tests.cpp
@@ -0,0 +1,114 @@
+/**
+ * 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.
+ */
+
+#include <string>
+
+#include <process/gtest.hpp>
+
+#include <stout/gtest.hpp>
+#include <stout/json.hpp>
+#include <stout/os.hpp>
+#include <stout/path.hpp>
+#include <stout/stringify.hpp>
+
+#include "slave/containerizer/provisioners/appc/spec.hpp"
+
+#include "tests/utils.hpp"
+
+using std::string;
+using std::vector;
+
+using namespace process;
+
+using namespace mesos::internal::slave::appc;
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+class AppcProvisionerTest : public TemporaryDirectoryTest {};
+
+
+TEST_F(AppcProvisionerTest, ValidateImageManifest)
+{
+  JSON::Value manifest = JSON::parse(
+      "{"
+      "  \"acKind\": \"ImageManifest\","
+      "  \"acVersion\": \"0.6.1\","
+      "  \"name\": \"foo.com/bar\","
+      "  \"labels\": ["
+      "    {"
+      "      \"name\": \"version\","
+      "      \"value\": \"1.0.0\""
+      "    },"
+      "    {"
+      "      \"name\": \"arch\","
+      "      \"value\": \"amd64\""
+      "    },"
+      "    {"
+      "      \"name\": \"os\","
+      "      \"value\": \"linux\""
+      "    }"
+      "  ],"
+      "  \"annotations\": ["
+      "    {"
+      "      \"name\": \"created\","
+      "      \"value\": \"1438983392\""
+      "    }"
+      "  ]"
+      "}").get();
+
+  EXPECT_SOME(spec::parse(stringify(manifest)));
+
+  // Incorrect acKind for image manifest.
+  manifest = JSON::parse(
+      "{"
+      "  \"acKind\": \"PodManifest\","
+      "  \"acVersion\": \"0.6.1\","
+      "  \"name\": \"foo.com/bar\""
+      "}").get();
+
+  EXPECT_ERROR(spec::parse(stringify(manifest)));
+}
+
+
+TEST_F(AppcProvisionerTest, ValidateLayout)
+{
+  string image = os::getcwd();
+
+  JSON::Value manifest = JSON::parse(
+      "{"
+      "  \"acKind\": \"ImageManifest\","
+      "  \"acVersion\": \"0.6.1\","
+      "  \"name\": \"foo.com/bar\""
+      "}").get();
+
+  ASSERT_SOME(os::write(path::join(image, "manifest"), stringify(manifest)));
+
+  // Missing rootfs.
+  EXPECT_SOME(spec::validateLayout(image));
+
+  ASSERT_SOME(os::mkdir(path::join(image, "rootfs", "tmp")));
+  ASSERT_SOME(os::write(path::join(image, "rootfs", "tmp", "test"), "test"));
+
+  EXPECT_NONE(spec::validateLayout(image));
+}
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {


[2/5] mesos git commit: Changed Image::AppC::id from required to optional.

Posted by ya...@apache.org.
Changed Image::AppC::id from required to optional.

Review: https://reviews.apache.org/r/37307


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

Branch: refs/heads/master
Commit: e90c4485746c0641c6ecf34d26011c79998840a0
Parents: b62c6e6
Author: Jiang Yan Xu <ya...@jxu.me>
Authored: Mon Aug 10 10:51:10 2015 -0700
Committer: Jiang Yan Xu <ya...@jxu.me>
Committed: Thu Aug 13 15:15:19 2015 -0700

----------------------------------------------------------------------
 include/mesos/mesos.proto | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e90c4485/include/mesos/mesos.proto
----------------------------------------------------------------------
diff --git a/include/mesos/mesos.proto b/include/mesos/mesos.proto
index a4a9c6f..7e2d66c 100644
--- a/include/mesos/mesos.proto
+++ b/include/mesos/mesos.proto
@@ -1211,7 +1211,7 @@ message Image {
     // "hash" is the hash algorithm used and "value" is the hex
     // encoded string of the digest. Currently the only permitted
     // hash algorithm is sha512.
-    required string id = 2;
+    optional string id = 2;
 
     // Optional labels. Suggested labels: "version", "os", and "arch".
     optional Labels labels = 3;


[5/5] mesos git commit: Implemented a 'read-only' Appc image store.

Posted by ya...@apache.org.
Implemented a 'read-only' Appc image store.

Review: https://reviews.apache.org/r/37311


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

Branch: refs/heads/master
Commit: dedd1ed69611e143fefd6a246cc85943457efc39
Parents: ab75d1d
Author: Jiang Yan Xu <ya...@jxu.me>
Authored: Thu Aug 13 13:57:15 2015 -0700
Committer: Jiang Yan Xu <ya...@jxu.me>
Committed: Thu Aug 13 15:16:23 2015 -0700

----------------------------------------------------------------------
 src/Makefile.am                                 |   2 +
 .../containerizer/provisioners/appc/store.cpp   | 198 +++++++++++++++++++
 .../containerizer/provisioners/appc/store.hpp   |  94 +++++++++
 src/slave/flags.cpp                             |   5 +
 src/slave/flags.hpp                             |   3 +
 .../containerizer/appc_provisioner_tests.cpp    |  72 +++++++
 6 files changed, 374 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/dedd1ed6/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index d664df6..b5dbdc3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -459,6 +459,7 @@ libmesos_no_3rdparty_la_SOURCES =					\
 	slave/containerizer/provisioner.cpp				\
 	slave/containerizer/provisioners/appc/paths.cpp			\
 	slave/containerizer/provisioners/appc/spec.cpp			\
+	slave/containerizer/provisioners/appc/store.cpp			\
 	slave/resource_estimators/noop.cpp				\
 	usage/usage.cpp							\
 	v1/attributes.cpp						\
@@ -715,6 +716,7 @@ libmesos_no_3rdparty_la_SOURCES +=					\
 	slave/containerizer/provisioner.hpp				\
 	slave/containerizer/provisioners/appc/paths.hpp			\
 	slave/containerizer/provisioners/appc/spec.hpp			\
+	slave/containerizer/provisioners/appc/store.hpp			\
 	slave/containerizer/isolators/posix.hpp				\
 	slave/containerizer/isolators/posix/disk.hpp			\
 	slave/containerizer/isolators/cgroups/constants.hpp		\

http://git-wip-us.apache.org/repos/asf/mesos/blob/dedd1ed6/src/slave/containerizer/provisioners/appc/store.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/provisioners/appc/store.cpp b/src/slave/containerizer/provisioners/appc/store.cpp
new file mode 100644
index 0000000..fbd1c53
--- /dev/null
+++ b/src/slave/containerizer/provisioners/appc/store.cpp
@@ -0,0 +1,198 @@
+/**
+ * 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.
+ */
+
+#include <list>
+
+#include <glog/logging.h>
+
+#include <process/defer.hpp>
+#include <process/dispatch.hpp>
+
+#include <stout/check.hpp>
+#include <stout/hashmap.hpp>
+#include <stout/os.hpp>
+#include <stout/path.hpp>
+
+#include "slave/containerizer/provisioners/appc/paths.hpp"
+#include "slave/containerizer/provisioners/appc/spec.hpp"
+#include "slave/containerizer/provisioners/appc/store.hpp"
+
+using namespace process;
+
+using std::list;
+using std::string;
+using std::vector;
+
+namespace mesos {
+namespace internal {
+namespace slave {
+namespace appc {
+
+class StoreProcess : public Process<StoreProcess>
+{
+public:
+  StoreProcess(const string& root);
+
+  ~StoreProcess() {}
+
+  Future<Nothing> recover();
+
+  Future<std::vector<Store::Image>> get(const string& name);
+
+private:
+  // Absolute path to the root directory of the store as defined by
+  // --appc_store_dir.
+  const string root;
+
+  // Mappings: name -> id -> image.
+  hashmap<string, hashmap<string, Store::Image>> images;
+};
+
+
+Try<Owned<Store>> Store::create(const Flags& flags)
+{
+  Try<Nothing> mkdir = os::mkdir(paths::getImagesDir(flags.appc_store_dir));
+  if (mkdir.isError()) {
+    return Error("Failed to create the images directory: " + mkdir.error());
+  }
+
+  // Make sure the root path is canonical so all image paths derived
+  // from it are canonical too.
+  Result<string> root = os::realpath(flags.appc_store_dir);
+  if (!root.isSome()) {
+    // The above mkdir call recursively creates the store directory
+    // if necessary so it cannot be None here.
+    CHECK_ERROR(root);
+    return Error(
+        "Failed to get the realpath of the store directory: " + root.error());
+  }
+
+  return Owned<Store>(new Store(
+      Owned<StoreProcess>(new StoreProcess(root.get()))));
+}
+
+
+Store::Store(Owned<StoreProcess> _process)
+  : process(_process)
+{
+  spawn(CHECK_NOTNULL(process.get()));
+}
+
+
+Store::~Store()
+{
+  terminate(process.get());
+  wait(process.get());
+}
+
+
+Future<Nothing> Store::recover()
+{
+  return dispatch(process.get(), &StoreProcess::recover);
+}
+
+
+Future<vector<Store::Image>> Store::get(const string& name)
+{
+  return dispatch(process.get(), &StoreProcess::get, name);
+}
+
+
+StoreProcess::StoreProcess(const string& _root) : root(_root) {}
+
+
+// Implemented as a helper function because it's going to be used by 'put()'.
+static Try<Store::Image> createImage(const string& imagePath)
+{
+  Option<Error> error = spec::validateLayout(imagePath);
+  if (error.isSome()) {
+    return Error("Invalid image layout: " + error.get().message);
+  }
+
+  string imageId = Path(imagePath).basename();
+
+  error = spec::validateImageID(imageId);
+  if (error.isSome()) {
+    return Error("Invalid image ID: " + error.get().message);
+  }
+
+  Try<string> read = os::read(paths::getImageManifestPath(imagePath));
+  if (read.isError()) {
+    return Error("Failed to read manifest: " + read.error());
+  }
+
+  Try<AppcImageManifest> manifest = spec::parse(read.get());
+  if (manifest.isError()) {
+    return Error("Failed to parse manifest: " + manifest.error());
+  }
+
+  return Store::Image(manifest.get(), imageId, imagePath);
+}
+
+
+Future<vector<Store::Image>> StoreProcess::get(const string& name)
+{
+  if (!images.contains(name)) {
+    return vector<Store::Image>();
+  }
+
+  vector<Store::Image> result;
+  foreach (const Store::Image& image, images[name].values()) {
+    result.push_back(image);
+  }
+
+  return result;
+}
+
+
+Future<Nothing> StoreProcess::recover()
+{
+  // Recover everything in the store.
+  Try<list<string>> imageIds = os::ls(paths::getImagesDir(root));
+  if (imageIds.isError()) {
+    return Failure(
+        "Failed to list images under '" +
+        paths::getImagesDir(root) + "': " +
+        imageIds.error());
+  }
+
+  foreach (const string& imageId, imageIds.get()) {
+    string path = paths::getImagePath(root, imageId);
+    if (!os::stat::isdir(path)) {
+      LOG(WARNING) << "Unexpected entry in storage: " << imageId;
+      continue;
+    }
+
+    Try<Store::Image> image = createImage(path);
+    if (image.isError()) {
+      LOG(WARNING) << "Unexpected entry in storage: " << image.error();
+      continue;
+    }
+
+    LOG(INFO) << "Restored image '" << image.get().manifest.name() << "'";
+
+    images[image.get().manifest.name()].put(image.get().id, image.get());
+  }
+
+  return Nothing();
+}
+
+} // namespace appc {
+} // namespace slave {
+} // namespace internal {
+} // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/dedd1ed6/src/slave/containerizer/provisioners/appc/store.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/provisioners/appc/store.hpp b/src/slave/containerizer/provisioners/appc/store.hpp
new file mode 100644
index 0000000..e48d91b
--- /dev/null
+++ b/src/slave/containerizer/provisioners/appc/store.hpp
@@ -0,0 +1,94 @@
+/**
+ * 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.
+ */
+
+#ifndef __MESOS_APPC_STORE__
+#define __MESOS_APPC_STORE__
+
+#include <string>
+#include <vector>
+
+#include <mesos/mesos.hpp>
+
+#include <process/future.hpp>
+#include <process/owned.hpp>
+
+#include <stout/try.hpp>
+
+#include "slave/flags.hpp"
+
+namespace mesos {
+namespace internal {
+namespace slave {
+namespace appc {
+
+// Forward declaration.
+class StoreProcess;
+
+
+// Provides the provisioner with access to locally stored / cached Appc images.
+class Store
+{
+public:
+  // Defines an image in the store (which has passed validation).
+  struct Image
+  {
+    Image(
+        const AppcImageManifest& _manifest,
+        const std::string& _id,
+        const std::string& _path)
+      : manifest(_manifest), id(_id), path(_path) {}
+
+    const AppcImageManifest manifest;
+
+    // Image ID of the format "sha512-value" where "value" is the hex
+    // encoded string of the sha512 digest of the uncompressed tar file
+    // of the image.
+    const std::string id;
+
+    // Absolute path of the extracted image.
+    const std::string path;
+  };
+
+  static Try<process::Owned<Store>> create(const Flags& flags);
+
+  ~Store();
+
+  process::Future<Nothing> recover();
+
+  // Get all images matched by name.
+  process::Future<std::vector<Image>> get(const std::string& name);
+
+  // TODO(xujyan): Implement a put() method that fetches an image into
+  // the store. i.e.,
+  // process::Future<StoredImage> put(const std::string& uri);
+
+private:
+  Store(process::Owned<StoreProcess> process);
+
+  Store(const Store&); // Not copyable.
+  Store& operator=(const Store&); // Not assignable.
+
+  process::Owned<StoreProcess> process;
+};
+
+} // namespace appc {
+} // namespace slave {
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __MESOS_APPC_STORE__

http://git-wip-us.apache.org/repos/asf/mesos/blob/dedd1ed6/src/slave/flags.cpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.cpp b/src/slave/flags.cpp
index 7aece2d..e43dd1c 100644
--- a/src/slave/flags.cpp
+++ b/src/slave/flags.cpp
@@ -64,6 +64,11 @@ mesos::internal::slave::Flags::Flags()
       "Comma separated list of image rootfs provisioners,\n"
       "e.g., appc,docker");
 
+  add(&Flags::appc_store_dir,
+      "appc_store_dir",
+      "Directory the appc provisioner will store images in",
+      "/tmp/mesos/store/appc");
+
   add(&Flags::default_role,
       "default_role",
       "Any resources in the --resources flag that\n"

http://git-wip-us.apache.org/repos/asf/mesos/blob/dedd1ed6/src/slave/flags.hpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.hpp b/src/slave/flags.hpp
index 881d494..e56738e 100644
--- a/src/slave/flags.hpp
+++ b/src/slave/flags.hpp
@@ -46,7 +46,10 @@ public:
   Option<std::string> hostname;
   Option<std::string> resources;
   std::string isolation;
+
   Option<std::string> provisioners;
+  std::string appc_store_dir;
+
   std::string default_role;
   Option<std::string> attributes;
   Bytes fetcher_cache_size;

http://git-wip-us.apache.org/repos/asf/mesos/blob/dedd1ed6/src/tests/containerizer/appc_provisioner_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/appc_provisioner_tests.cpp b/src/tests/containerizer/appc_provisioner_tests.cpp
index 1335cfd..47b66b9 100644
--- a/src/tests/containerizer/appc_provisioner_tests.cpp
+++ b/src/tests/containerizer/appc_provisioner_tests.cpp
@@ -27,6 +27,7 @@
 #include <stout/stringify.hpp>
 
 #include "slave/containerizer/provisioners/appc/spec.hpp"
+#include "slave/containerizer/provisioners/appc/store.hpp"
 
 #include "tests/utils.hpp"
 
@@ -109,6 +110,77 @@ TEST_F(AppcProvisionerTest, ValidateLayout)
   EXPECT_NONE(spec::validateLayout(image));
 }
 
+
+TEST_F(AppcProvisionerTest, StoreRecover)
+{
+  // Create store.
+  slave::Flags flags;
+  flags.appc_store_dir = path::join(os::getcwd(), "store");
+  Try<Owned<Store>> store = Store::create(flags);
+  ASSERT_SOME(store);
+
+  // Create a simple image in the store:
+  // <store>
+  // |--images
+  //    |--<id>
+  //       |--manifest
+  //       |--rootfs/tmp/test
+  JSON::Value manifest = JSON::parse(
+      "{"
+      "  \"acKind\": \"ImageManifest\","
+      "  \"acVersion\": \"0.6.1\","
+      "  \"name\": \"foo.com/bar\","
+      "  \"labels\": ["
+      "    {"
+      "      \"name\": \"version\","
+      "      \"value\": \"1.0.0\""
+      "    },"
+      "    {"
+      "      \"name\": \"arch\","
+      "      \"value\": \"amd64\""
+      "    },"
+      "    {"
+      "      \"name\": \"os\","
+      "      \"value\": \"linux\""
+      "    }"
+      "  ],"
+      "  \"annotations\": ["
+      "    {"
+      "      \"name\": \"created\","
+      "      \"value\": \"1438983392\""
+      "    }"
+      "  ]"
+      "}").get();
+
+  // The 'imageId' below has the correct format but it's not computed by
+  // hashing the tarball of the image. It's OK here as we assume
+  // the images under 'images' have passed such check when they are
+  // downloaded and validated.
+  string imageId =
+    "sha512-e77d96aa0240eedf134b8c90baeaf76dca8e78691836301d7498c84020446042e"
+    "797b296d6ab296e0954c2626bfb264322ebeb8f447dac4fac6511ea06bc61f0";
+
+  string imagePath = path::join(flags.appc_store_dir, "images", imageId);
+
+  ASSERT_SOME(os::mkdir(path::join(imagePath, "rootfs", "tmp")));
+  ASSERT_SOME(
+      os::write(path::join(imagePath, "rootfs", "tmp", "test"), "test"));
+  ASSERT_SOME(
+      os::write(path::join(imagePath, "manifest"), stringify(manifest)));
+
+  // Recover the image from disk.
+  AWAIT_READY(store.get()->recover());
+
+  Future<vector<Store::Image>> _images = store.get()->get("foo.com/bar");
+  AWAIT_READY(_images);
+
+  vector<Store::Image> images = _images.get();
+
+  EXPECT_EQ(1u, images.size());
+  ASSERT_SOME(os::realpath(imagePath));
+  EXPECT_EQ(os::realpath(imagePath).get(), images.front().path);
+}
+
 } // namespace tests {
 } // namespace internal {
 } // namespace mesos {


[3/5] mesos git commit: Added app::paths which handles Appc related path manipulation.

Posted by ya...@apache.org.
Added app::paths which handles Appc related path manipulation.

- Akin to slave::paths.

Review: https://reviews.apache.org/r/37309


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

Branch: refs/heads/master
Commit: a070de312ee0e6902c34407e306ccdc8eaa3d8e9
Parents: 28d3041
Author: Jiang Yan Xu <ya...@jxu.me>
Authored: Mon Aug 10 11:12:31 2015 -0700
Committer: Jiang Yan Xu <ya...@jxu.me>
Committed: Thu Aug 13 15:15:20 2015 -0700

----------------------------------------------------------------------
 src/Makefile.am                                 |  2 +
 .../containerizer/provisioners/appc/paths.cpp   | 80 +++++++++++++++++++
 .../containerizer/provisioners/appc/paths.hpp   | 81 ++++++++++++++++++++
 3 files changed, 163 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/a070de31/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index 111aed9..f8e54d2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -457,6 +457,7 @@ libmesos_no_3rdparty_la_SOURCES =					\
 	slave/containerizer/mesos/containerizer.cpp			\
 	slave/containerizer/mesos/launch.cpp				\
 	slave/containerizer/provisioner.cpp				\
+	slave/containerizer/provisioners/appc/paths.cpp			\
 	slave/resource_estimators/noop.cpp				\
 	usage/usage.cpp							\
 	v1/attributes.cpp						\
@@ -711,6 +712,7 @@ libmesos_no_3rdparty_la_SOURCES +=					\
 	slave/containerizer/launcher.hpp				\
 	slave/containerizer/linux_launcher.hpp				\
 	slave/containerizer/provisioner.hpp				\
+	slave/containerizer/provisioners/appc/paths.hpp			\
 	slave/containerizer/isolators/posix.hpp				\
 	slave/containerizer/isolators/posix/disk.hpp			\
 	slave/containerizer/isolators/cgroups/constants.hpp		\

http://git-wip-us.apache.org/repos/asf/mesos/blob/a070de31/src/slave/containerizer/provisioners/appc/paths.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/provisioners/appc/paths.cpp b/src/slave/containerizer/provisioners/appc/paths.cpp
new file mode 100644
index 0000000..a244d9a
--- /dev/null
+++ b/src/slave/containerizer/provisioners/appc/paths.cpp
@@ -0,0 +1,80 @@
+/**
+ * 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.
+ */
+
+#include <stout/path.hpp>
+
+#include "slave/containerizer/provisioners/appc/paths.hpp"
+
+using std::string;
+
+namespace mesos {
+namespace internal {
+namespace slave {
+namespace appc {
+namespace paths {
+
+string getStagingDir(const string& storeDir)
+{
+  return path::join(storeDir, "staging");
+}
+
+
+string getImagesDir(const string& storeDir)
+{
+  return path::join(storeDir, "images");
+}
+
+
+string getImagePath(const string& storeDir, const string& imageId)
+{
+  return path::join(getImagesDir(storeDir), imageId);
+}
+
+
+string getImageRootfsPath(
+    const string& storeDir,
+    const string& imageId)
+{
+  return path::join(getImagePath(storeDir, imageId), "rootfs");
+}
+
+
+string getImageRootfsPath(const std::string& imagePath)
+{
+  return path::join(imagePath, "rootfs");
+}
+
+
+string getImageManifestPath(
+    const string& storeDir,
+    const string& imageId)
+{
+  return path::join(getImagePath(storeDir, imageId), "manifest");
+}
+
+
+string getImageManifestPath(const std::string& imagePath)
+{
+  return path::join(imagePath, "manifest");
+}
+
+} // namespace paths {
+} // namespace appc {
+} // namespace slave {
+} // namespace internal {
+} // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/a070de31/src/slave/containerizer/provisioners/appc/paths.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/provisioners/appc/paths.hpp b/src/slave/containerizer/provisioners/appc/paths.hpp
new file mode 100644
index 0000000..e358051
--- /dev/null
+++ b/src/slave/containerizer/provisioners/appc/paths.hpp
@@ -0,0 +1,81 @@
+/**
+ * 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.
+ */
+
+#ifndef __MESOS_APPC_PATHS__
+#define __MESOS_APPC_PATHS__
+
+#include <list>
+#include <string>
+
+#include <mesos/mesos.hpp>
+
+namespace mesos {
+namespace internal {
+namespace slave {
+namespace appc {
+namespace paths {
+
+// The appc store file system layout is as follows:
+//
+// <root> ('--appc_store_dir' flag)
+// |--staging (contains temp directories for staging downloads)
+// |
+// |--images (stores validated images)
+//    |--<image_id> (in the form of "sha512-<128_character_hash_sum>")
+//       |--manifest
+//       |--rootfs
+//          |--... (according to the ACI spec)
+//
+// TODO(xujyan): The staging directory is unused for now (it's
+// externally managed) but implemented to illustrate the need for a
+// separate 'images' directory. Complete the layout diagram when the
+// staging directory is utilized by the provisioner.
+
+std::string getStagingDir(const std::string& storeDir);
+
+
+std::string getImagesDir(const std::string& storeDir);
+
+
+std::string getImagePath(
+    const std::string& storeDir,
+    const std::string& imageId);
+
+
+std::string getImageRootfsPath(
+    const std::string& storeDir,
+    const std::string& imageId);
+
+
+std::string getImageRootfsPath(const std::string& imagePath);
+
+
+std::string getImageManifestPath(
+    const std::string& storeDir,
+    const std::string& imageId);
+
+
+std::string getImageManifestPath(const std::string& imagePath);
+
+} // namespace paths {
+} // namespace appc {
+} // namespace slave {
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __MESOS_APPC_PATHS__