You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2016/02/12 00:24:48 UTC
[3/3] mesos git commit: Added common appc spec utilities.
Added common appc spec utilities.
Review: https://reviews.apache.org/r/43198/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f89352f5
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f89352f5
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f89352f5
Branch: refs/heads/master
Commit: f89352f5384fd098e72b01f745a7f7891718ca8e
Parents: ec20fc4
Author: Jojy Varghese <jo...@mesosphere.io>
Authored: Thu Feb 11 15:12:09 2016 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Thu Feb 11 15:12:09 2016 -0800
----------------------------------------------------------------------
include/mesos/appc/spec.hpp | 12 +++++--
src/appc/spec.cpp | 75 +++++++++++++++++++++++++++++++++-------
2 files changed, 72 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/f89352f5/include/mesos/appc/spec.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/appc/spec.hpp b/include/mesos/appc/spec.hpp
index 462159d..e9430df 100644
--- a/include/mesos/appc/spec.hpp
+++ b/include/mesos/appc/spec.hpp
@@ -27,6 +27,10 @@
namespace appc {
namespace spec {
+// Parse the ImageManifest in the specified JSON string.
+Try<ImageManifest> parse(const std::string& value);
+
+
// Returns the rootfs of an image, given its path as per the spec.
std::string getImageRootfsPath(const std::string& imagePath);
@@ -35,6 +39,10 @@ std::string getImageRootfsPath(const std::string& imagePath);
std::string getImageManifestPath(const std::string& imagePath);
+// Returns ImageManifest object for an image at given path.
+Try<ImageManifest> getManifest(const std::string& imagePath);
+
+
// Validate if the specified image manifest conforms to the Appc spec.
Option<Error> validateManifest(const ImageManifest& manifest);
@@ -48,8 +56,8 @@ Option<Error> validateImageID(const std::string& imageId);
Option<Error> validateLayout(const std::string& imagePath);
-// Parse the ImageManifest in the specified JSON string.
-Try<ImageManifest> parse(const std::string& value);
+// Validates the image path if it conforms to the Appc image spec.
+Option<Error> validate(const std::string& imageDir);
} // namespace spec {
} // namespace appc {
http://git-wip-us.apache.org/repos/asf/mesos/blob/f89352f5/src/appc/spec.cpp
----------------------------------------------------------------------
diff --git a/src/appc/spec.cpp b/src/appc/spec.cpp
index fc36a0b..8471ccb 100644
--- a/src/appc/spec.cpp
+++ b/src/appc/spec.cpp
@@ -14,12 +14,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+#include <stout/os.hpp>
#include <stout/path.hpp>
#include <stout/protobuf.hpp>
#include <stout/strings.hpp>
-#include <stout/os/stat.hpp>
-
#include <mesos/appc/spec.hpp>
#include "slave/containerizer/mesos/provisioner/appc/paths.hpp"
@@ -29,6 +28,28 @@ using std::string;
namespace appc {
namespace spec {
+Try<ImageManifest> parse(const string& value)
+{
+ Try<JSON::Object> json = JSON::parse<JSON::Object>(value);
+ if (json.isError()) {
+ return Error("JSON parse failed: " + json.error());
+ }
+
+ Try<ImageManifest> manifest = protobuf::parse<ImageManifest>(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();
+}
+
+
string getImageRootfsPath(const string& imagePath)
{
return path::join(imagePath, "rootfs");
@@ -41,6 +62,22 @@ string getImageManifestPath(const string& imagePath)
}
+Try<ImageManifest> getManifest(const string& imagePath)
+{
+ Try<string> read = os::read(getImageManifestPath(imagePath));
+ if (read.isError()) {
+ return Error("Failed to read manifest file: " + read.error());
+ }
+
+ Try<ImageManifest> parseManifest = parse(read.get());
+ if (parseManifest.isError()) {
+ return Error("Failed to parse manifest: " + parseManifest.error());
+ }
+
+ return parseManifest.get();
+}
+
+
Option<Error> validateManifest(const ImageManifest& manifest)
{
// TODO(idownes): Validate that required fields are present when
@@ -85,25 +122,37 @@ Option<Error> validateLayout(const string& imagePath)
}
-Try<ImageManifest> parse(const string& value)
+Option<Error> validate(const string& imagePath)
{
- Try<JSON::Object> json = JSON::parse<JSON::Object>(value);
- if (json.isError()) {
- return Error("JSON parse failed: " + json.error());
+ Option<Error> validate = validateLayout(imagePath);
+ if (validate.isSome()) {
+ return Error(
+ "Image validation failed for image at '" + imagePath + "': " +
+ validate->message);
}
- Try<ImageManifest> manifest = protobuf::parse<ImageManifest>(json.get());
-
+ Try<ImageManifest> manifest = getManifest(imagePath);
if (manifest.isError()) {
- return Error("Protobuf parse failed: " + manifest.error());
+ return Error(
+ "Image validation failed for image at '" + imagePath + "': " +
+ manifest.error());
}
- Option<Error> error = validateManifest(manifest.get());
- if (error.isSome()) {
- return Error("Schema validation failed: " + error.get().message);
+ validate = validateManifest(manifest.get());
+ if (validate.isSome()) {
+ return Error(
+ "Image validation failed for image at '" + imagePath + "': " +
+ validate->message);
}
- return manifest.get();
+ validate = validateImageID(Path(imagePath).basename());
+ if (validate.isSome()) {
+ return Error(
+ "Image validation failed for image at '" + imagePath + "': " +
+ validate->message);
+ }
+
+ return None();
}
} // namespace spec {