Skip to content

Commit

Permalink
Merge pull request #109 from snprajwal/fix-proto-gen
Browse files Browse the repository at this point in the history
crit: fix proto imports for library
  • Loading branch information
adrianreber committed May 20, 2023
2 parents 45baa8a + e69417e commit 748b90b
Show file tree
Hide file tree
Showing 99 changed files with 1,396 additions and 1,198 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ jobs:
run: sudo -E make test

- name: Test magicgen script
run: sudo -E make -C scripts test
run: sudo -E make -C scripts/magic-gen test

- name: Test CRIT
run: |
if [ "${{ matrix.criu_branch }}" = "criu-dev" ]; then
# We need to use the protobuf definitions from the criu-dev
# branch as it might have changed.
make -C crit update-proto GIT_BRANCH=${{ matrix.criu_branch }}
# First update protobuf. It is too old in the Ubuntu image.
curl -Lo protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.20.3/protoc-3.20.3-linux-x86_64.zip
curl -Lo protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v22.2/protoc-22.2-linux-x86_64.zip
sudo unzip -o protoc.zip -d /usr
sudo -E make -C crit clean
make -C crit gen-proto
sudo -E make -C crit all
# We need to use the protobuf definitions from the criu-dev
# branch as it might have changed.
sudo -E make -C scripts/proto-gen proto-update GIT_BRANCH=${{ matrix.criu_branch }}
# Generate the bindings
sudo -E make -C scripts/proto-gen
# Build the CRIT binary
sudo -E make -C crit clean bin/crit
fi
sudo -E make -C crit unit-test
sudo -E make -C test crit-test
Expand Down
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ test/crit/test-imgs
test/crit/crit-test.coverage
test/.coverage/
image
scripts/*.h
scripts/expected.go
scripts/output.go
scripts/magic-gen/*.h
scripts/magic-gen/expected.go
scripts/magic-gen/output.go
crit/bin
crit/test-imgs/
42 changes: 3 additions & 39 deletions crit/Makefile
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
GO ?= go
CRIU ?= criu

# The import path that protoc will use if a proto file imports another one
import_path := github.com/checkpoint-restore/go-criu/crit/images
# Path to .proto source files
proto_path := ./images
# Generate string of all .proto filenames
proto_files := $(sort $(subst $(proto_path)/,,$(wildcard $(proto_path)/*.proto)))
# Generate M flag to specify import path for all .proto files
# and replace all spaces with commas to use with go_opt flag
comma := ,
proto_opts := $(subst $() $(),$(comma),$(patsubst %,M%=$(import_path),$(proto_files)))
GIT_BRANCH := master

all: gen-proto bin/crit

update-proto:
rm ./images/*.proto || true
git clone --depth 1 --branch $(GIT_BRANCH) https://github.com/checkpoint-restore/criu criu-temp
cp criu-temp/images/*.proto ./images/
# rpc.proto is not an image and it is used only to communicate criu-service and swrk.
rm -rf criu-temp images/rpc.proto
# To prevent namespace conflict with proto files
# in github.com/letsencrypt/boulder, we prepend
# a prefix to the filenames.
mv ./images/sa.proto ./images/criu-sa.proto
sed -i 's/sa\.proto/criu-sa\.proto/g' images/*.proto
mv ./images/core.proto ./images/criu-core.proto
sed -i 's/core\.proto/criu-core\.proto/g' images/*.proto

gen-proto:
rm -f ./images/*.pb.go
@protoc \
--proto_path=$(proto_path) \
--go_out=$(proto_path) \
--go_opt=paths=source_relative,$(proto_opts) \
$(proto_files)

bin/crit: cmd/cli.go
bin/crit: cmd/main.go
$(GO) build ${GOFLAGS} -o $@ $^

../test/loop/loop:
Expand All @@ -52,10 +16,10 @@ test-imgs: ../test/loop/loop

unit-test: test-imgs
$(eval GOFLAGS ?= -cover)
go test ${GOFLAGS} -v ./...
$(GO) test ${GOFLAGS} -v ./...

clean:
@rm -f bin/crit
@rm -rf test-imgs

.PHONY: all gen-proto update-proto unit-test clean
.PHONY: unit-test clean
132 changes: 105 additions & 27 deletions crit/cmd/cli.go → crit/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"encoding/json"
Expand All @@ -13,7 +13,7 @@ import (

var (
// The crit service used to invoke all commands
c crit.CritSvc
c crit.Critter

// All members needed for crit struct
inputFilePath string
Expand All @@ -40,11 +40,29 @@ var decodeCmd = &cobra.Command{
Long: `Convert the input binary image to JSON and write it to a file.
If no output file is provided, the JSON is printed to stdout.`,
Run: func(cmd *cobra.Command, args []string) {
c = crit.NewCli(inputFilePath, outputFilePath,
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}
defer inputFile.Close()
}

c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
img, err := c.Decode()
entryType, err := GetEntryTypeFromImg(inputFile)
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}
img, err := c.Decode(entryType)
if err != nil {
log.Fatal(fmt.Errorf("error decoding image: %w", err))
}

var jsonData []byte
Expand All @@ -54,7 +72,7 @@ If no output file is provided, the JSON is printed to stdout.`,
jsonData, err = json.Marshal(img)
}
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error processing data into JSON: ", err)))
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))
}
// If no output file, print to stdout
if outputFilePath == "" {
Expand All @@ -64,12 +82,13 @@ If no output file is provided, the JSON is printed to stdout.`,
// Write to output file
jsonFile, err := os.Create(outputFilePath)
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error opening destination file: ", err)))
log.Fatal(fmt.Errorf("error opening destination file: %w", err))
}
defer jsonFile.Close()

_, err = jsonFile.Write(jsonData)
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error writing JSON data: ", err)))
log.Fatal(fmt.Errorf("error writing JSON data: %w", err))
}
},
}
Expand All @@ -80,16 +99,43 @@ var encodeCmd = &cobra.Command{
Short: "Convert JSON to binary image file",
Long: "Convert the input JSON to a CRIU image file.",
Run: func(cmd *cobra.Command, args []string) {
c = crit.NewCli(inputFilePath, outputFilePath,
var (
inputFile, outputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}
defer inputFile.Close()
}
if outputFilePath == "" {
outputFile = os.Stdout
} else {
outputFile, err = os.Create(outputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening output file: %w", err))
}
defer outputFile.Close()
}

c = crit.New(inputFile, outputFile,
inputDirPath, pretty, noPayload)
entryType, err := GetEntryTypeFromJSON(inputFile)
if err != nil {
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}
// Convert JSON to Go struct
img, err := c.Parse()
img, err := c.Parse(entryType)
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error parsing JSON: %w", err))
}
// Write Go struct to binary image file
if err := c.Encode(img); err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error writing to file: %w", err))
}
},
}
Expand All @@ -103,16 +149,34 @@ var showCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
inputFilePath = args[0]
pretty = true
c = crit.NewCli(inputFilePath, outputFilePath,
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}
defer inputFile.Close()
}

c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
img, err := c.Decode()
entryType, err := GetEntryTypeFromImg(inputFile)
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}
img, err := c.Decode(entryType)
if err != nil {
log.Fatal(fmt.Errorf("error decoding image: %w", err))
}

jsonData, err := json.MarshalIndent(img, "", " ")
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error processing data into JSON: ", err)))
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))
}
fmt.Println(string(jsonData))
},
Expand All @@ -125,16 +189,30 @@ var infoCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inputFilePath = args[0]
c = crit.NewCli(inputFilePath, outputFilePath,
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}
defer inputFile.Close()
}

c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
img, err := c.Info()
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error decoding image: %w", err))
}

jsonData, err := json.MarshalIndent(img, "", " ")
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error processing data into JSON: ", err)))
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))
}
fmt.Println(string(jsonData))
},
Expand All @@ -155,10 +233,10 @@ var xCmd = &cobra.Command{
// returned object since we don't really care
// about the data itself, as long as we can
// marshal it into JSON and display it.
var xData interface{}
var xData any
var err error

c = crit.NewCli(inputFilePath, outputFilePath,
c = crit.New(nil, nil,
inputDirPath, pretty, noPayload)
// Switch the explore type and call the handler.
switch args[1] {
Expand All @@ -171,22 +249,22 @@ var xCmd = &cobra.Command{
case "rss":
xData, err = c.ExploreRss()
default:
err = errors.New("error exploring directory: Invalid explore type")
err = errors.New("error exploring directory: invalid explore type")
}
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error exploring directory: %w", err))
}

jsonData, err := json.MarshalIndent(xData, "", " ")
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error processing data into JSON: ", err)))
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))
}
fmt.Println(string(jsonData))
},
}

// Add all commands to the root command and configure flags
func init() {
func Init() {
// Disable completion generation
rootCmd.CompletionOptions.DisableDefaultCmd = true
// Decode options
Expand All @@ -212,8 +290,8 @@ func init() {
rootCmd.AddCommand(xCmd)
}

func main() {
func Run() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error running CLI: %w", err))
}
}

0 comments on commit 748b90b

Please sign in to comment.