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 8 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
)
3 changes: 3 additions & 0 deletions grid-compose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin/*
.pre-commit-config.yaml
out
9 changes: 9 additions & 0 deletions grid-compose/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BIN_PATH = bin/main

.PHONY: build run

build:
@go build -o $(BIN_PATH) ./main.go

run:
@go run main.go up -f ./grid-compose.yaml
22 changes: 22 additions & 0 deletions grid-compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Grid-Compose

is a tool for running multi-vm applications on TFGrid defined using a Yaml formatted file.

## Usage

`REQUIRED` EnvVars:

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

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

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

COMMANDS:
- version: shows the project version
- up: deploy the app
- down: cancel all deployments
```
16 changes: 16 additions & 0 deletions grid-compose/cmd/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var downCmd = &cobra.Command{
Use: "down",
Short: "cancel your project on the grid",
Run: func(cmd *cobra.Command, args []string) {
if err := app.Down(); err != nil {
log.Fatal().Err(err).Send()
}
},
}
18 changes: 18 additions & 0 deletions grid-compose/cmd/ps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var psCmd = &cobra.Command{
Use: "ps",
Short: "list containers",
Run: func(cmd *cobra.Command, args []string) {
flags := cmd.Flags()

if err := app.Ps(cmd.Context(), flags); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

invoke the flags here, and pass to the method only the values it needs

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 (
"os"

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

var (
app *internal.App
configPath string
Copy link
Contributor

Choose a reason for hiding this comment

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

to avoid the global variable, we could add the app to the context. it will shared between the commands

network string
mnemonic string
)

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

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) {
var err error
app, err = internal.NewApp(network, mnemonic, configPath)
if err != nil {
log.Fatal().Err(err).Send()
}
},
}

func init() {
network = os.Getenv("NETWORK")
Copy link
Contributor

Choose a reason for hiding this comment

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

move getting the env to the Run in rootCmd

mnemonic = os.Getenv("MNEMONIC")
rootCmd.PersistentFlags().StringVarP(&configPath, "file", "f", "./grid-compose.yaml", "the grid-compose configuration file")
psCmd.PersistentFlags().BoolP("verbose", "v", false, "all information about deployed services")
psCmd.PersistentFlags().StringP("output", "o", "", "output result to a file")

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(upCmd)
rootCmd.AddCommand(psCmd)
rootCmd.AddCommand(downCmd)
}
20 changes: 20 additions & 0 deletions grid-compose/cmd/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

// load config from file + validate
// parse environment variables
// deploy networks + volumes
// deploy services
var upCmd = &cobra.Command{
Use: "up",
Short: "deploy application on the grid",
Run: func(cmd *cobra.Command, args []string) {
if err := app.Up(cmd.Context()); err != nil {
log.Fatal().Err(err).Send()
}
},
}
17 changes: 17 additions & 0 deletions grid-compose/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var version = "v0.0.1"
Copy link
Contributor

Choose a reason for hiding this comment

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


var versionCmd = &cobra.Command{
Use: "version",
Short: "get current version number",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}
58 changes: 58 additions & 0 deletions grid-compose/example/full_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# not all features here are implemented yet
version: '1.0.0'

# cannot figure out how to run mycelium always error deployment not configured
networks:
net1:
type: 'wg'
net2:
type: 'myc'
net3:
type: 'ygg'
net4:
type: 'ip4'
net5:
type: 'ip6'

services:
web:
flist: 'https://hub.grid.tf/tf-official-apps/nginx-latest.flist'
environment:
- ENV_VAR_NAME=value
volumes:
- web-data:/data
networks:
- net1
resources:
cpu: 2
memory: 2
depends_on:
- database
database:
flist: 'https://hub.grid.tf/tf-official-apps/postgresql-latest.flist'
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
volumes:
- db-data:/var/lib/postgresql/data
networks:
- net1
- net2
- net3
resources:
cpu: 4
memory: 2
healthcheck:
test: ['CMD', 'curl', 'http://{myceliumip}']
interval: 1m30s
timeout: 10s
retries: 3
# ask omar about the types zmount and all that
storage:
web-data:
type: 'zmount'
size: 10GB
db-data:
type: 'zmount'
size: 10GB
57 changes: 57 additions & 0 deletions grid-compose/example/full_example_v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: '1.0.0'

networks:
net1:
name: 'nettyy'
nodes:
- 11
- 14
- 144
range:
ip:
type: ip4
ip: 10.20.0.0
mask:
type: cidr
mask: 16/32
wg: true

project_name: 'example_project'
services:
web5:
flist: 'https://hub.grid.tf/tf-official-vms/ubuntu-24.04-full.flist'
networks:
- net1
network_types:
- ip4
- ygg
node_id: 11
entrypoint: '/sbin/zinit init'
volumes:
- webdata:/data
resources:
cpu: 1
memory: 2048
disk: 25600
database:
flist: 'https://hub.grid.tf/tf-official-vms/ubuntu-24.04-full.flist'
networks:
- net1
network_types:
- ygg
volumes:
- dbdata:/var/lib/postgresql/data
node_id: 11
entrypoint: '/sbin/zinit init'
resources:
cpu: 1
memory: 2048
disk: 25600

storage:
webdata:
type: 'zmount'
size: 10GB
dbdata:
type: 'zmount'
size: 10GB
50 changes: 50 additions & 0 deletions grid-compose/example/full_example_v3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: '1.0.0'

networks:
net1:
type: 'wg'
net2:
type: 'myc'
net3:
type: 'ygg'
net4:
type: 'ip4'
net5:
type: 'ip6'

services:
web5:
flist: 'https://hub.grid.tf/tf-official-vms/ubuntu-24.04-full.flist'
networks:
- net3
- net4
- net5
node_id: 144
entrypoint: '/sbin/zinit init'
volumes:
- webdata:/data
resources:
cpu: 1
memory: 2048
disk: 25600
database:
flist: 'https://hub.grid.tf/tf-official-vms/ubuntu-24.04-full.flist'
networks:
- net3
- net1
volumes:
- dbdata:/var/lib/postgresql/data
node_id: 14
entrypoint: '/sbin/zinit init'
resources:
cpu: 1
memory: 2048
disk: 25600

storage:
webdata:
type: 'zmount'
size: 10GB
dbdata:
type: 'zmount'
size: 10GB
14 changes: 14 additions & 0 deletions grid-compose/example/single_vm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '1.0.0'

services:
new_single_vm:
flist: 'https://hub.grid.tf/omarabdulaziz.3bot/ubuntu-jammy.flist'
entrypoint: '/sbin/init'
environment:
- SSH_KEY=...
resources:
cpu: 1
memory: 512
node_id: 14
networks:
- yggdrasil
16 changes: 16 additions & 0 deletions grid-compose/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/threefoldtech/tfgrid-sdk-go/grid-compose

go 1.21.0

require (
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.12.0 // indirect
)
Loading
Loading