Skip to content

Commit

Permalink
Add json flag for json input and output (#692)
Browse files Browse the repository at this point in the history
* Add json flag for json input and output

* Add output flag

* Remove false config check

* Add error check

* Remove error check
  • Loading branch information
AbdelrahmanElawady authored Jan 30, 2024
1 parent 0babd00 commit eae1b0f
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 91 deletions.
2 changes: 1 addition & 1 deletion mass-deployer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tfrobot is tool designed to automate mass deployment of groups of VMs on ThreeFo

- **Mass Deployment:** Deploy groups of vms on ThreeFold Grid simultaneously.
- **Mass Cancelation:** cancel all vms on ThreeFold Grid defined in configuration file simultaneously.
- **Customizable Configurations:** Define Node groups, VMs groups and other configurations through YAML file.
- **Customizable Configurations:** Define Node groups, VMs groups and other configurations through YAML or JSON file.

## Download

Expand Down
4 changes: 3 additions & 1 deletion mass-deployer/cmd/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd

import (
"os"
"path/filepath"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -24,8 +25,9 @@ var cancelCmd = &cobra.Command{
log.Fatal().Err(err).Msgf("failed to open config file: %s", configPath)
}
defer configFile.Close()
jsonFmt := filepath.Ext(configPath) == ".json"

cfg, err := parser.ParseConfig(configFile)
cfg, err := parser.ParseConfig(configFile, jsonFmt)
if err != nil {
log.Fatal().Err(err).Msgf("failed to parse config file: %s", configPath)
}
Expand Down
11 changes: 9 additions & 2 deletions mass-deployer/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd
import (
"context"
"os"
"path/filepath"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -19,20 +20,25 @@ var deployCmd = &cobra.Command{
if err != nil || configPath == "" {
log.Fatal().Err(err).Msg("error in config file")
}
output, err := cmd.Flags().GetString("output")
if err != nil {
log.Fatal().Err(err).Msg("error in output file")
}

configFile, err := os.Open(configPath)
if err != nil {
log.Fatal().Err(err).Msgf("failed to open config file: %s", configPath)
}
defer configFile.Close()
jsonFmt := filepath.Ext(configPath) == ".json"

cfg, err := parser.ParseConfig(configFile)
cfg, err := parser.ParseConfig(configFile, jsonFmt)
if err != nil {
log.Fatal().Err(err).Msgf("failed to parse config file: %s", configPath)
}

ctx := context.Background()
err = deployer.RunDeployer(cfg, ctx)
err = deployer.RunDeployer(cfg, ctx, output)
if err != nil {
log.Fatal().Err(err).Msg("failed to run the deployer")
}
Expand All @@ -41,5 +47,6 @@ var deployCmd = &cobra.Command{

func init() {
deployCmd.Flags().StringP("config", "c", "", "path to config file")
deployCmd.Flags().StringP("output", "o", "", "output file")
rootCmd.AddCommand(deployCmd)
}
43 changes: 43 additions & 0 deletions mass-deployer/example/conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"node_groups": [
{
"name": "group_a",
"nodes_count": 3,
"free_cpu": 2,
"free_mru": 16384,
"free_ssd": 100,
"free_hdd": 50,
"dedicated": false,
"public_ip4": false,
"public_ip6": false,
"certified": false,
"region": "europe"
}
],
"vms": [
{
"name": "examplevm123",
"vms_count": 1,
"node_group": "group_a",
"cpu": 1,
"mem": 256,
"ssd": [
{
"size": 15,
"mount_point": "/mnt/ssd"
}
],
"public_ip4": false,
"public_ip6": false,
"flist": "https://hub.grid.tf/tf-official-apps/base:latest.flist",
"entry_point": "/sbin/zinit init",
"root_size": 0,
"ssh_key": "example1"
}
],
"ssh_keys": {
"example1": "ssh_key1"
},
"mnemonic": "example-mnemonic",
"network": "dev"
}
10 changes: 7 additions & 3 deletions mass-deployer/internal/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"encoding/json"
"fmt"
"io"
"os"
Expand All @@ -17,16 +18,19 @@ const (
networkKey = "NETWORK"
)

func ParseConfig(file io.Reader) (deployer.Config, error) {
func ParseConfig(file io.Reader, jsonFmt bool) (deployer.Config, error) {
conf := deployer.Config{}
nodeGroupsNames := []string{}

configFile, err := io.ReadAll(file)
if err != nil {
return deployer.Config{}, fmt.Errorf("failed to read the config file %+w", err)
}

err = yaml.Unmarshal(configFile, &conf)
if jsonFmt {
err = json.Unmarshal(configFile, &conf)
} else {
err = yaml.Unmarshal(configFile, &conf)
}
if err != nil {
return deployer.Config{}, err
}
Expand Down
47 changes: 30 additions & 17 deletions mass-deployer/internal/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"encoding/json"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestParseConfig(t *testing.T) {
} `
configFile := strings.NewReader(conf)

_, err := ParseConfig(configFile)
_, err := ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -70,7 +71,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -83,7 +84,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -96,7 +97,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -109,7 +110,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -122,7 +123,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -137,7 +138,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -152,7 +153,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -167,7 +168,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -182,7 +183,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -197,7 +198,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -212,7 +213,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -227,7 +228,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -242,7 +243,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -257,7 +258,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -272,7 +273,7 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.Error(t, err)
})

Expand All @@ -284,7 +285,19 @@ func TestParseConfig(t *testing.T) {

configFile := strings.NewReader(string(data))

_, err = ParseConfig(configFile)
_, err = ParseConfig(configFile, false)
assert.NoError(t, err)
})
t.Run("valid json config", func(t *testing.T) {
conf := confStruct

data, err := json.Marshal(conf)
assert.NoError(t, err)

configFile := strings.NewReader(string(data))

parsedConf, err := ParseConfig(configFile, true)
assert.NoError(t, err)
assert.Equal(t, conf, parsedConf)
})
}
Loading

0 comments on commit eae1b0f

Please sign in to comment.