Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions proto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ CPP_FILES := $(CPP_SOURCE) $(CPP_HDR)
DOCKER ?= docker
CWD := $(shell pwd)

default: example
default: example_cpp
./example_cpp

all: go py c cpp

Expand All @@ -42,6 +43,10 @@ $(CPP_DIR)/%.pb.cc: %.proto
@mkdir -p $(CPP_DIR)
protoc --cpp_out=$(CPP_DIR)/ $^

%_cpp: %.cc $(CPP_FILES)
pkg-config --cflags protobuf # fails if protobuf is not installed
c++ $< $(filter %.cc, $(CPP_FILES)) -o $@ $$(pkg-config --cflags --libs protobuf) -I $(CPP_DIR)

py: $(PY_FILES)

$(PY_DIR)/%_pb2.py: %.proto
Expand All @@ -52,5 +57,4 @@ proto3: Dockerfile run.sh
$(DOCKER) build -t $@ . && $(DOCKER) run -it -v $(CWD):/proto:ro -v $(CWD)/output:/output:rw $@

clean:
rm -rf *~ $(GO_FILES) $(C_FILES) $(PY_FILES) $(CPP_FILES) $(CWD)/output

rm -rf *~ *_cpp $(GO_FILES) $(C_FILES) $(PY_FILES) $(CPP_FILES) $(CWD)/output
35 changes: 35 additions & 0 deletions proto/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.1.0",
"platform": {
"os": "linux",
"arch": "amd64"
},
"process": {
"terminal": true,
"user": {
"@type": "type.googleapis.com/oci.LinuxUser",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is type added by the json generation? Or is this something we have in the spec now? (I don't see it)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Wed, Sep 30, 2015 at 10:29:33AM -0700, Timothy Chen wrote:

  •  "@type": "type.googleapis.com/oci.LinuxUser",
    

Is type added by the json generation?

It's added by protobuf so it can figure out which message type to use
for deserializing Any fields. See 1.

"uid": 1,
"gid": 1,
"additionalGids": [
5,
6
]
},
"args": [
"sh"
],
"env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TERM=linux"
],
"cwd": "/root"
},
"hostname": "mrsdalloway",
"linuxx": {
"capabilities": [
"CAP_AUDIT_WRITE",
"CAP_KILL",
"CAP_NET_BIND_SERVICE"
]
}
}
64 changes: 23 additions & 41 deletions proto/config.proto
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
syntax = "proto3";

package oci;

import "google/protobuf/any.proto";

// Spec is the base configuration for the container. It specifies platform
// independent configuration.
message Spec {
// Version is the version of the specification that is supported.
optional string version = 1;
string version = 1;
// Platform is the host information for OS and Arch.
optional Platform platform = 2;
Platform platform = 2;
// Process is the container's main process.
optional Process process = 3;
Process process = 3;
// Root is the root information for the container's filesystem.
optional Root root = 4;
Root root = 4;
// Hostname is the container's host name.
optional string hostname = 5;
string hostname = 5;
// Mounts profile configuration for adding mounts to the container's
// filesystem.
repeated MountPoint mounts = 6;
}


// LinuxSpec is the full specification for linux containers.
message LinuxSpec {
optional Spec spec = 1;
// LinuxConfig is platform specific configuration for linux based
// containers.
optional LinuxConfig linux_config = 2;
// LinuxConfig is the Linux-specific, host-independent configuration.
LinuxConfig linuxx = 7;
}

// LinuxConfig contains platform specific configuration for linux based
Expand All @@ -38,69 +35,54 @@ message LinuxConfig {
// container is created for.
message Platform {
// OS is the operating system.
optional string os = 1;
string os = 1;
// Arch is the architecture
optional string arch = 2;
string arch = 2;
}

// Process contains information to start a specific application inside the
// container.
message Process {
// Terminal creates an interactive terminal for the container.
optional bool terminal = 1;
bool terminal = 1;
// User specifies user information for the process.
optional User user = 2;
google.protobuf.Any user = 2;
// Args specifies the binary and arguments for the application to
// execute.
repeated string args = 3;
// Env populates the process environment for the process.
repeated string env = 4;
// Cwd is the current working directory for the process and must be
// relative to the container's root.
optional string cwd = 5;
}

enum PlatformType {
UNKNOWN = 0;
LINUX = 1;
}

// User specifies user information for the process.
message User {
// Type so that receivers of this message can `switch` for the fields
// expected
optional PlatformType type = 1;

//optional LinuxUser linux_type = 2;
extensions 100 to 499;
string cwd = 5;
}

// LinuxUser specifies linux specific user and group information for the
// container's main process.
extend User {
message LinuxUser {
// Uid is the user id.
optional int32 uid = 101;
int32 uid = 101;
// Gid is the group id.
optional int32 gid = 102;
int32 gid = 102;
repeated int32 additional_gids = 103;
}

// Root contains information about the container's root filesystem on the host.
message Root {
// Path is the absolute path to the container's root filesystem.
optional string path = 1;
string path = 1;
// Readonly makes the root filesystem for the container readonly before
// the process is executed.
optional bool readonly = 2;
bool readonly = 2;
}

// MountPoint describes a directory that may be fullfilled by a mount in the
// runtime.json.
message MountPoint {
// Name is a unique descriptive identifier for this mount point.
optional string name = 1;
string name = 1;
// Path specifies the path of the mount. The path and child directories
// MUST exist, a runtime MUST NOT create directories automatically to a
// mount point.
optional string path = 2;
string path = 2;
}
104 changes: 104 additions & 0 deletions proto/example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// See README.txt for information and build instructions.

#include <fstream>
#include <iostream>
#include <string>

#include <google/protobuf/descriptor.h>
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/util/type_resolver.h>
#include <google/protobuf/util/type_resolver_util.h>

#include "config.pb.h"
#include "runtime_config.pb.h"

using namespace std;

static const char kTypeUrlPrefix[] = "type.googleapis.com";

static string GetTypeUrl(const google::protobuf::Descriptor* message) {
return string(kTypeUrlPrefix) + "/" + message->full_name();
}

static bool ReadMessage(string path, google::protobuf::Message *message) {
string binary;
ifstream input(path.c_str(), ios::in | ios::binary);
if (!input) {
cout << path << ": File not found." << endl;
return false;
}
string json( (istreambuf_iterator<char>(input)),
istreambuf_iterator<char>() );
google::protobuf::scoped_ptr<google::protobuf::util::TypeResolver> resolver;
resolver.reset(google::protobuf::util::NewTypeResolverForDescriptorPool(
kTypeUrlPrefix,
google::protobuf::DescriptorPool::generated_pool()));
GOOGLE_CHECK_OK(google::protobuf::util::JsonToBinaryString(
resolver.get(),
GetTypeUrl(message->GetDescriptor()),
json,
&binary));
return message->ParseFromString(binary);
}

static bool WriteMessage(const google::protobuf::Message& message) {
string json;
google::protobuf::util::JsonOptions options;
google::protobuf::scoped_ptr<google::protobuf::util::TypeResolver> resolver;
resolver.reset(google::protobuf::util::NewTypeResolverForDescriptorPool(
kTypeUrlPrefix,
google::protobuf::DescriptorPool::generated_pool()));
options.add_whitespace = true;
GOOGLE_CHECK_OK(google::protobuf::util::BinaryToJsonString(
resolver.get(),
GetTypeUrl(message.GetDescriptor()),
message.SerializeAsString(),
&json,
options));
cout << json;
return true;
}

// Main function: Reads the config from a file and writes it to stdout.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;

oci::Spec config;
oci::RuntimeSpec runtime;

if (!ReadMessage("config.json", &config)) {
cerr << "config.json: Failed to load." << endl;
return -1;
}

/*
oci::LinuxUser user;
user.set_uid(1);
user.set_gid(1);
user.add_additional_gids(5);
user.add_additional_gids(6);
config.mutable_process()->mutable_user()->PackFrom(user);
*/

if (!WriteMessage(config)) {
cerr << "config.json: Failed to write to stdout." << endl;
return -1;
}

if (!ReadMessage("runtime.json", &runtime)) {
cerr << "runtime.json: Failed to load." << endl;
return -1;
}

if (!WriteMessage(runtime)) {
cerr << "runtime.json: Failed to write to stdout." << endl;
return -1;
}

// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();

return 0;
}
Loading