Skip to content

Commit

Permalink
work on go migration
Browse files Browse the repository at this point in the history
  • Loading branch information
ufasoli committed Jun 21, 2020
1 parent 16f78f0 commit 33115a7
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/inspectionProfiles/profiles_settings.xml

This file was deleted.

9 changes: 9 additions & 0 deletions .idea/platys.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added bin/cobra
Binary file not shown.
22 changes: 22 additions & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(genCmd)
}

var genCmd = &cobra.Command{
Use: "gen",
Short: "Generates all the needed artifacts for the docker-based modern (data) platform",
Long: `Generates all the needed artifacts for the docker-based modern (data) platform.
The stack configuration can either be passed as a local file (using the --config-filename option or using the default name 'config.yml')
or as an URL
referencing a file on the Internet (using the --config-url option).`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Platys v 1.0")
},
}
36 changes: 36 additions & 0 deletions cmd/list_services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (

"github.com/spf13/cobra"

)

func init() {
rootCmd.AddCommand(listServicesCmd)
}

var listServicesCmd = &cobra.Command{
Use: "list_services",
Short: "List the services",
Long: `List the services contained in the given version of the platy`,
Run: func(cmd *cobra.Command, args []string) {
pullConfig()

/* tar_config = pull_config(Stack, Version)
// extract the config file from the tar in to the current folder
tar_file = tarfile.open(tar_config)
tar_file.extractall(path=tempfile.gettempdir())
tar_file.close()
yaml = ruamel.yaml.YAML()
with open(rf'{tempfile.gettempdir()}/config.yml') as file:
config_yml = yaml.load(file)
for c in config_yml:
service = re.search("([A-Z0-9_-]+)_enable", str(c)) // if variable follows regex it's considered a service and will be printed
if service is not None:
print(service.group(1))*/
},
}


143 changes: 143 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package cmd

import (
"archive/tar"
"bytes"
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"log"

"github.com/spf13/cobra"
"io"
"os"
)

var Stack string
var Version string

var rootCmd = &cobra.Command{
Use: "platys",
Short: "Platys platform generator",
Long: `Platys modern data platform generator
Complete documentation is available at https://github.com/TrivadisPF/platys`,
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
},
}

func init() {
rootCmd.PersistentFlags().StringVarP(&Stack, "stack", "s", "trivadis/platys-modern-data-platform", "stack version to employ")
rootCmd.PersistentFlags().StringVarP(&Version, "stack-version", "w", "latest", "version of the stack to employ")
}

func er(msg interface{}) {
fmt.Println("Error:", msg)
os.Exit(1)
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func pullConfig(){
// init and start docker container
/*client = Docker.from_env()
dp_container = client.containers.run(image = fmt.Sprintf("%s:%s", Stack, ), detach = True, auto_remove = True)
// copy default config file (with default values to the current folder
tar_config = tempfile.gettempdir() + '/config.tar'
f = open(tar_config, 'wb')
bits, stats = dp_container.get_archive('/opt/mdps-gen/vars/config.yml')
for chunk in bits:
f.write(chunk)
f.close()*/

//return tar_config


ctx := context.Background()

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}

reader, err := cli.ImagePull(ctx, Stack, types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, reader)

resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: Stack,

Tty: true,

}, nil, nil, "platys")
if err != nil {
panic(err)
}

if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}


statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}

out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}


reader, _, err = cli.CopyFromContainer(ctx, resp.ID,"/opt/mdps-gen/vars/config.yml")
if err != nil{
log.Println(err.Error())
}
tr := tar.NewReader(reader)

for {
// hdr gives you the header of the tar file
hdr, err := tr.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil {
log.Fatalln(err)
}
buf := new(bytes.Buffer)
buf.ReadFrom(tr)

// You can use this wholeContent to create new file
wholeContent := buf.String()

fmt.Println("Whole of the string of ", hdr.Name ," is ",wholeContent)

}

stdcopy.StdCopy(os.Stdout, os.Stderr, out)
cli.ContainerRemove(ctx, resp.ID,types.ContainerRemoveOptions{
RemoveVolumes: true,
RemoveLinks: true,
Force: true,
})
}


19 changes: 19 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of platys",
Long: `Print the version number of platys`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Platys v 1.0")
},
}
6 changes: 6 additions & 0 deletions go-dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#### Installing docker dependency

provide this exact version
```
go get github.com/docker/docker@v19.03.9
```
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module trivadis.com/platys

go 1.14

require (
github.com/containerd/containerd v1.3.4 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v17.12.0-ce-rc1.0.20200514230353-811a247d06e8+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/spf13/cobra v1.0.0
)
Loading

0 comments on commit 33115a7

Please sign in to comment.