Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement grid-compose #1130

Open
wants to merge 22 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7503ba9
init grid-compose
Omarabdul3ziz Jun 25, 2024
84cbd10
add network support
Omarabdul3ziz Jun 25, 2024
7f0d28b
use yaml.v3 to fix the custom unmarshal
Omarabdul3ziz Jun 26, 2024
9e70865
feat: implement init logic for ps command
eyad-hussein Jul 24, 2024
7d3cff0
enhance: implement logic to parse env vars and disks for up command
eyad-hussein Aug 3, 2024
905394b
refactor: refactor ps command implementation
eyad-hussein Aug 3, 2024
40e67a2
refactor: refactor up and ps commands
eyad-hussein Aug 4, 2024
9fcedd2
feat: implement deployment of two or more services on same network
eyad-hussein Aug 4, 2024
09d55e2
exmple: add more example files to cover test cases
eyad-hussein Aug 6, 2024
3b095a7
refactor: refactor logic for all commands to comply with cases and co…
eyad-hussein Aug 13, 2024
9e227e1
docs: edit readme file
eyad-hussein Aug 13, 2024
b0f3b22
docs: add supported cases for deployment, update readme
eyad-hussein Aug 13, 2024
4bf7aaf
feat: add support for depends_on optoin(dependency resolution)
eyad-hussein Aug 17, 2024
352ef37
feat: add support for health check option
eyad-hussein Aug 18, 2024
de9f63a
docs: add future work file
eyad-hussein Aug 18, 2024
07c5da9
refactor: refactor project structure and code
eyad-hussein Aug 21, 2024
124fe56
fix: add check for app type assertion, refactor: remove deploy packag…
eyad-hussein Aug 22, 2024
bd2aa64
Merge pull request #1155 from threefoldtech/development_compose_init_…
Omarabdul3ziz Aug 22, 2024
8ddab7b
Merge branch 'development' of github.com:threefoldtech/tfgrid-sdk-go …
eyad-hussein Aug 22, 2024
deb6275
feat: add support for different storage units for service resources
eyad-hussein Aug 24, 2024
f21cf97
fix: fix storage parser
eyad-hussein Aug 26, 2024
bf550b0
fix: change project name so type becomes Micro Virtual Machine in das…
eyad-hussein Aug 31, 2024
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
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.21
go 1.21.0

use (
./activation-service
Expand All @@ -12,4 +12,5 @@ use (
./tfrobot
./tools/relay-cache-warmer
./user-contracts-mon
./grid-compose
)
6 changes: 6 additions & 0 deletions grid-compose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin/*
.pre-commit-config.yaml
out
full_example.yml
invalid
grid-compose.yml
21 changes: 21 additions & 0 deletions grid-compose/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test:
@echo "Running Tests"
go test -v ./...

clean:
rm ./bin -rf

getverifiers:
@echo "Installing golangci-lint" && go install github.com/golangci/golangci-lint/cmd/golangci-lint
go mod tidy

lint:
@echo "Running $@"
golangci-lint run -c ../.golangci.yml

build:
@echo "Running $@"
@go build -ldflags=\
"-X 'github.com/threefoldtech/tfgrid-sdk-go/grid-compose/cmd.commit=$(shell git rev-parse HEAD)'\
-X 'github.com/threefoldtech/tfgrid-sdk-go/grid-compose/cmd.version=$(shell git tag --sort=-version:refname | head -n 1)'"\
-o bin/grid-compose main.go
163 changes: 163 additions & 0 deletions grid-compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Grid-Compose

is a tool similar to docker-compose created for running multi-vm applications on TFGrid defined using a Yaml formatted file.

The yaml file's structure is defined in [docs/config](docs/config.md).

## Usage

`REQUIRED` EnvVars:

- `MNEMONIC`: your secret words
- `NETWORK`: one of (dev, qa, test, main)

```bash
grid-compose [OPTIONS] [COMMAND]

OPTIONS:
-f, --file: path to yaml file, default is ./grid-compose.yml

COMMANDS:
- version: shows the project version
- up: deploy the app
- down: cancel all deployments
- ps: list deployments on the grid
OPTIONS:
- -v, --verbose: show full details of each deployment
```

Export env vars using:

```bash
export MNEMONIC=your_mnemonics
export NETWORK=working_network
```

Run:

```bash
make build
```

To use any of the commands, run:

```bash
./bin/grid-compose [COMMAND]
```

For example:

```bash
./bin/grid-compose ps -f example/multiple_services_diff_network_3.yml
```

## Usage For Each Command

### up

The up command deploys the services defined in the yaml file to the grid.

Refer to the [cases](docs/cases.md) for more information on the cases supported.

Refer to examples in the [examples](examples) directory to have a look at different possible configurations.

```bash
./bin/grid-compose up [OPTIONS]
```

OPTIONS:

- `-f, --file`: path to the yaml file, default is `./grid-compose.yml`

### Example

```bash
./bin/grid-compose up
```

output:

```bash
3:40AM INF starting peer session=tf-848216 twin=8658
3:40AM INF deploying network... name=miaminet node_id=14
3:41AM INF deployed successfully
3:41AM INF deploying vm... name=database node_id=14
3:41AM INF deployed successfully
3:41AM INF deploying network... name=miaminet node_id=14
3:41AM INF deployed successfully
3:41AM INF deploying vm... name=server node_id=14
3:41AM INF deployed successfully
3:41AM INF all deployments deployed successfully
```

### down

The down command cancels all deployments on the grid.

```bash
./bin/grid-compose down [OPTIONS]
```

OPTIONS:

- `-f, --file`: path to the yaml file, default is `./grid-compose.yml`

### Example

```bash
./bin/grid-compose down
```

output:

```bash
3:45AM INF starting peer session=tf-854215 twin=8658
3:45AM INF canceling deployments projectName=vm/compose/8658/net1
3:45AM INF canceling contracts project name=vm/compose/8658/net1
3:45AM INF project is canceled project name=vm/compose/8658/net1
```

### ps

The ps command lists all deployments on the grid.

```bash
./bin/grid-compose ps [FLAGS] [OPTIONS]
```

OPTIONS:

- `-f, --file`: path to the yaml file, default is `./grid-compose.yml`

### Example

```bash
./bin/grid-compose ps
```

output:

```bash
3:43AM INF starting peer session=tf-851312 twin=8658

Deployment Name | Node ID | Network | Services | Storage | State | IP Address
------------------------------------------------------------------------------------------------------------------------------------------------------
dl_database | 14 | miaminet | database | dbdata | ok | wireguard: 10.20.2.2
dl_server | 14 | miaminet | server | webdata | ok | wireguard: 10.20.2.3
```

FLAGS:

- `-v, --verbose`: show full details of each deployment

### version

The version command shows the project's current version.

```bash
./bin/grid-compose version
```

## Future Work

Refer to [docs/future_work.md](docs/future_work.md) for more information on the future work that is to be done on the grid-compose project.
22 changes: 22 additions & 0 deletions grid-compose/cmd/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/threefoldtech/tfgrid-sdk-go/grid-compose/internal/app"
)

var downCmd = &cobra.Command{
Use: "down",
Short: "cancel your project on the grid",
Run: func(cmd *cobra.Command, args []string) {
app, ok := cmd.Context().Value("app").(*app.App)
if !ok {
log.Fatal().Msg("app not found in context")
}

if err := app.Down(); err != nil {
log.Fatal().Err(err).Send()
}
},
}
27 changes: 27 additions & 0 deletions grid-compose/cmd/ps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/threefoldtech/tfgrid-sdk-go/grid-compose/internal/app"
)

var psCmd = &cobra.Command{
Use: "ps",
Short: "list deployments on the grid",
Run: func(cmd *cobra.Command, args []string) {
verbose, err := cmd.Flags().GetBool("verbose")
if err != nil {
log.Fatal().Err(err).Send()
}

app, ok := cmd.Context().Value("app").(*app.App)
if !ok {
log.Fatal().Msg("app not found in context")
}

if err := app.Ps(cmd.Context(), verbose); err != nil {
log.Fatal().Err(err).Send()
}
},
}
50 changes: 50 additions & 0 deletions grid-compose/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"context"
"os"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/threefoldtech/tfgrid-sdk-go/grid-compose/internal/app"
)

func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal().Err(err).Send()
}
}

// TODO: validate command line arguments
var rootCmd = &cobra.Command{
Use: "grid-compose",
Short: "Grid-Compose is a tool for running multi-vm applications on TFGrid defined using a Yaml formatted file.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
network := os.Getenv("NETWORK")
mnemonic := os.Getenv("MNEMONIC")
configPath, _ := cmd.Flags().GetString("file")

app, err := app.NewApp(network, mnemonic, configPath)

if err != nil {
log.Fatal().Err(err).Send()
}

ctx := context.WithValue(cmd.Context(), "app", app)
cmd.SetContext(ctx)
},
}

func init() {
rootCmd.PersistentFlags().StringP("file", "f", "./grid-compose.yml", "the grid-compose configuration file")

rootCmd.AddCommand(downCmd)
rootCmd.AddCommand(upCmd)
rootCmd.AddCommand(versionCmd)

psCmd.PersistentFlags().BoolP("verbose", "v", false, "all information about deployed services")
rootCmd.AddCommand(psCmd)

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
22 changes: 22 additions & 0 deletions grid-compose/cmd/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/threefoldtech/tfgrid-sdk-go/grid-compose/internal/app"
)

var upCmd = &cobra.Command{
Use: "up",
Short: "deploy application on the grid",
Run: func(cmd *cobra.Command, args []string) {
app, ok := cmd.Context().Value("app").(*app.App)
if !ok {
log.Fatal().Msg("app not found in context")
}

if err := app.Up(cmd.Context()); err != nil {
log.Fatal().Err(err).Send()
}
},
}
22 changes: 22 additions & 0 deletions grid-compose/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Package cmd for parsing command line arguments
package cmd

import (
"log"

"github.com/spf13/cobra"
)

// set at build time
var commit string
var version string

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Get latest build tag",
Run: func(cmd *cobra.Command, args []string) {
log.Println(version)
log.Println(commit)
},
}
Loading
Loading