Skip to content

Commit

Permalink
add a flag to generate a network configuration with defaults
Browse files Browse the repository at this point in the history
the cli for generating network configurations is using the survey
library for generating the configurations. However, this library does
not support stdin as IO as described in
https://github.com/go-survey/survey#what-kinds-of-io-are-supported-by-survey.

This is a problem for automating the generation of configuration for
creating a docker based setup. We would rather not use static keys as
done in https://github.com/SamBouwer/any-docker because if by any bad
luck the local setup is broken and starts to sync data on the public
backup node instead of the local one, this could lead to serious
troubles for the data, since the keys are public.

To avoid that situation, the cli should rather have a way to generate
the configuration with default network values mainly for generating the
security keys and leave it to the user to then further update the
configurations as necessary. This only works for one node but in the
case of self-hosted I think it is good enough for now.

Also, an alternative solution would have been to replace the survey
library to use something able to read from stdin but this would be more
intrusive and would have more potential for bugs after the change hence
my proposal to introduce a simple --defaults flag to the command.
  • Loading branch information
clems4ever committed Aug 4, 2023
1 parent 15ddf55 commit 82b5a0b
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 36 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
on:
push: []

name: build_and_test

jobs:
build:
name: build release
runs-on: ubuntu-22.04
strategy:
matrix:
arch:
- amd64
- arm64
os:
- linux
- windows
- darwin
go-version:
- 1.19.5
include:
- arch: amd64
rpm_arch: x86_64
- arch: arm64
rpm_arch: aarch64
env:
GOPRIVATE: github.com/anyproto
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '${{ matrix.go-version }}'
- name: git config
run: git config --global url.https://${{ secrets.ANYTYPE_PAT }}@github.com/.insteadOf https://github.com/
# cache {{
- id: go-cache-paths
run: |
echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT
echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
with:
path: |
${{ steps.go-cache-paths.outputs.GOCACHE }}
${{ steps.go-cache-paths.outputs.GOMODCACHE }}
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ matrix.os }}-${{ matrix.arch }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{ matrix.go-version }}-${{ matrix.os }}-${{ matrix.arch }}-
# }}

# build {{
- name: deps
run: make deps

#- name: unit tests
# run: make test
- name: test default config generation
run: go run any-sync-network/main.go create --defaults

- name: build
run: make build BUILD_GOOS=${{ matrix.os}} BUILD_GOARCH=${{ matrix.arch }}
# }}
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ jobs:
run: make build BUILD_GOOS=${{ matrix.os}} BUILD_GOARCH=${{ matrix.arch }}
# }}

- name: test default config generation
run: ./bin/any-sync-network create --defaults

- name: get release version
id: release-version
run: |
Expand Down
107 changes: 71 additions & 36 deletions any-sync-network/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,32 @@ var create = &cobra.Command{
// Create coordinator node
fmt.Println("\nCreating coordinator node...")

var defaultCoordinatorAddress = "127.0.0.1:4830"
var defaultMongoAddress = "mongodb://localhost:27017"
var defaultMongoDatabase = "coordinator"

var coordinatorQs = []*survey.Question{
{
Name: "address",
Prompt: &survey.Input{
Message: "Any-Sync Coordinator Node address",
Default: "127.0.0.1:4830",
Default: defaultCoordinatorAddress,
},
Validate: survey.Required,
},
{
Name: "mongoConnect",
Prompt: &survey.Input{
Message: "Mongo connect URI",
Default: "mongodb://localhost:27017",
Default: defaultMongoAddress,
},
Validate: survey.Required,
},
{
Name: "mongoDB",
Prompt: &survey.Input{
Message: "Mongo database name",
Default: "coordinator",
Default: defaultMongoDatabase,
},
Validate: survey.Required,
},
Expand All @@ -152,12 +156,18 @@ var create = &cobra.Command{
Address string
MongoConnect string
MongoDB string
}{}
}{
Address: defaultCoordinatorAddress,
MongoConnect: defaultMongoAddress,
MongoDB: defaultMongoDatabase,
}

err := survey.Ask(coordinatorQs, &answers)
if err != nil {
fmt.Println(err.Error())
return
if !defaultsFlag {
err := survey.Ask(coordinatorQs, &answers)
if err != nil {
fmt.Println(err.Error())
return
}
}

coordinatorNode := defaultCoordinatorNode()
Expand Down Expand Up @@ -212,25 +222,31 @@ var syncNodes = []SyncNodeConfig{}
func createSyncNode() {
fmt.Println("\nCreating sync node...")

var defaultSyncNodeAddress = "127.0.0.1:" + syncNodePort

var syncQs = []*survey.Question{
{
Name: "address",
Prompt: &survey.Input{
Message: "Any-Sync Node address",
Default: "127.0.0.1:" + syncNodePort,
Default: defaultSyncNodeAddress,
},
Validate: survey.Required,
},
}

answers := struct {
Address string
}{}
}{
defaultSyncNodeAddress,
}

err := survey.Ask(syncQs, &answers)
if err != nil {
fmt.Println(err.Error())
return
if !defaultsFlag {
err := survey.Ask(syncQs, &answers)
if err != nil {
fmt.Println(err.Error())
return
}
}

syncNode := defaultSyncNode()
Expand All @@ -252,12 +268,19 @@ var fileNodes = []FileNodeConfig{}
func createFileNode() {
fmt.Println("\nCreating file node...")

var defaultFileNodeAddress = "127.0.0.1:" + fileNodePort
var defaultS3Region = "eu-central-1"
var defaultS3Profile = "default"
var defaultS3Bucket = "any-sync-files"
var defaultRedisURL = "redis://127.0.0.1:6379/?dial_timeout=3&db=1&read_timeout=6s&max_retries=2"
var defaultRedisCluster = "false"

var fileQs = []*survey.Question{
{
Name: "address",
Prompt: &survey.Input{
Message: "Any-Sync File Node address",
Default: "127.0.0.1:" + fileNodePort,
Default: defaultFileNodeAddress,
},
Validate: survey.Required,
},
Expand All @@ -273,31 +296,31 @@ func createFileNode() {
Name: "s3Region",
Prompt: &survey.Input{
Message: "S3 Region",
Default: "eu-central-1",
Default: defaultS3Region,
},
Validate: survey.Required,
},
{
Name: "s3Profile",
Prompt: &survey.Input{
Message: "S3 Profile",
Default: "default",
Default: defaultS3Profile,
},
Validate: survey.Required,
},
{
Name: "s3Bucket",
Prompt: &survey.Input{
Message: "S3 Bucket",
Default: "any-sync-files",
Default: defaultS3Bucket,
},
Validate: survey.Required,
},
{
Name: "redisURL",
Prompt: &survey.Input{
Message: "Redis URL",
Default: "redis://127.0.0.1:6379/?dial_timeout=3&db=1&read_timeout=6s&max_retries=2",
Default: defaultRedisURL,
},
Validate: survey.Required,
},
Expand All @@ -306,7 +329,7 @@ func createFileNode() {
Prompt: &survey.Select{
Message: "Is your redis installation a cluster?",
Options: []string{"true", "false"},
Default: "false",
Default: defaultRedisCluster,
},
Validate: survey.Required,
},
Expand All @@ -320,12 +343,22 @@ func createFileNode() {
S3Bucket string
RedisURL string
RedisCluster string
}{}
}{
Address: defaultFileNodeAddress,
S3Endpoint: "",
S3Region: defaultS3Region,
S3Profile: defaultS3Profile,
S3Bucket: defaultS3Bucket,
RedisURL: defaultRedisURL,
RedisCluster: defaultRedisCluster,
}

err := survey.Ask(fileQs, &answers)
if err != nil {
fmt.Println(err.Error())
return
if !defaultsFlag {
err := survey.Ask(fileQs, &answers)
if err != nil {
fmt.Println(err.Error())
return
}
}

fileNode := defaultFileNode()
Expand Down Expand Up @@ -355,17 +388,19 @@ func lastStepOptions() {
Default: "No, generate configs",
}

option := ""
survey.AskOne(prompt, &option, survey.WithValidator(survey.Required))
switch option {
case "Add sync-node":
createSyncNode()
lastStepOptions()
case "Add file-node":
createFileNode()
lastStepOptions()
default:
return
if !defaultsFlag {
option := ""
survey.AskOne(prompt, &option, survey.WithValidator(survey.Required))
switch option {
case "Add sync-node":
createSyncNode()
lastStepOptions()
case "Add file-node":
createFileNode()
lastStepOptions()
default:
return
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions any-sync-network/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/spf13/cobra"
)

var defaultsFlag bool

var rootCmd = &cobra.Command{
Use: "anyconf",
Short: "Configuration builder for Any-Sync nodes.",
Expand All @@ -19,5 +21,6 @@ func Execute() {
}

func init() {
create.Flags().BoolVar(&defaultsFlag, "defaults", false, "generate configuration files using default parameters")
rootCmd.AddCommand(create)
}

0 comments on commit 82b5a0b

Please sign in to comment.