diff --git a/cli/app/app.go b/cli/app/app.go index aca55167..55c98d26 100644 --- a/cli/app/app.go +++ b/cli/app/app.go @@ -27,6 +27,8 @@ import ( "github.com/urfave/cli" "github.com/docker/docker/api/client/bundlefile" + "github.com/docker/libcompose/docker" + "github.com/docker/libcompose/project" "encoding/json" "io/ioutil" @@ -41,6 +43,7 @@ import ( "github.com/fatih/structs" "github.com/ghodss/yaml" + ) const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789" @@ -619,6 +622,37 @@ func loadBundlesFile(file string) KomposeObject { return komposeObject } +// Load compose file version 1 into KomposeObject +func loadComposeFile(file string, c *cli.Context) KomposeObject { + komposeObject := KomposeObject{ + ServiceConfigs: make(map[string]ServiceConfig), + } + context := &docker.Context{} + if file == "" { + file = "docker-compose.yml" + } + context.ComposeFiles = []string{file} + + // load compose file into composeObject + composeObject := project.NewProject(&context.Context, nil, nil) + err := composeObject.Parse() + if err != nil { + logrus.Fatalf("Failed to load compose file", err) + } + + // transform composeObject into komposeObject + composeServiceNames := composeObject.ServiceConfigs.Keys() + for _, name := range composeServiceNames { + if composeServiceConfig, ok := composeObject.ServiceConfigs.Get(name); ok { + // TODO: mapping composeObject config to komposeObject config + serviceConfig := ServiceConfig{} + serviceConfig.Image = composeServiceConfig.Image + komposeObject.ServiceConfigs[name] = serviceConfig + } + } + return komposeObject +} + // Convert komposeObject to K8S controllers func komposeConvert(komposeObject KomposeObject, toStdout, createD, createRS, createDS, createChart, generateYaml bool, replicas int, inputFile string, outFile string, f *os.File) { mServices := make(map[string][]byte) @@ -839,6 +873,8 @@ func Convert(c *cli.Context) { // Parse DAB file into komposeObject if fromBundles { komposeObject = loadBundlesFile(inputFile) + } else { + komposeObject = loadComposeFile(inputFile, c) } // Convert komposeObject to K8S controllers diff --git a/cli/command/command.go b/cli/command/command.go index a348f063..0df48166 100644 --- a/cli/command/command.go +++ b/cli/command/command.go @@ -75,6 +75,10 @@ func ConvertCommand() cli.Command { Name: "from-bundles", Usage: "Getting input from docker DAB file", }, + cli.BoolFlag{ + Name: "from-compose-v2", + Usage: "Getting input from docker compose file version 2", + }, }, } } diff --git a/examples/docker-voting.yml b/examples/docker-voting.yml new file mode 100644 index 00000000..289b9d24 --- /dev/null +++ b/examples/docker-voting.yml @@ -0,0 +1,29 @@ +version: "2" + +services: + vote: + build: ./vote + command: python app.py + volumes: + - ./vote:/app + ports: + - "5000:80" + + redis: + image: redis:alpine + ports: ["6379"] + + worker: + build: ./worker + + db: + image: postgres:9.4 + + result: + build: ./result + command: nodemon --debug server.js + volumes: + - ./result:/app + ports: + - "5001:80" + - "5858:5858"