diff --git a/.env_example b/.env_example deleted file mode 100644 index 24ebff01f..000000000 --- a/.env_example +++ /dev/null @@ -1,2 +0,0 @@ -MYSTERIUM_API_URL=http://127.0.0.1:8001/v1 -NATS_SERVER_IP=127.0.0.1 diff --git a/.travis.yml b/.travis.yml index 837662acd..a6427f1aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,11 +18,6 @@ cache: directories: - "$CACHE_ROOT_DIR" -before_install: - - rm -f .env - - echo "MYSTERIUM_API_URL=$MYSTERIUM_API_URL" >> .env - - echo "NATS_SERVER_IP=$NATS_SERVER_IP" >> .env - install: - source bin/travis_scripts/setup_tools_dir.sh $BUILD_TOOLS_PATH - source bin/travis_scripts/ensure_glide.sh $BUILD_TOOLS_PATH "v0.13.1" diff --git a/bin/client_build b/bin/client_build index 23ee79a12..5ddcbad85 100755 --- a/bin/client_build +++ b/bin/client_build @@ -17,7 +17,6 @@ #> readelf -d build/client/mysterium_client source bin/helpers/functions.sh -source bin/helpers/load_environment.sh export GOOS=${GOOS:=`go env GOHOSTOS`} export GOARCH=${GOARCH:=`go env GOHOSTARCH`} diff --git a/bin/client_run b/bin/client_run index 1a3e0c57b..ac1d139dc 100755 --- a/bin/client_run +++ b/bin/client_run @@ -1,9 +1,7 @@ #!/bin/bash -source bin/helpers/load_run_environment.sh - sudo ./build/client/mysterium_client \ --config-dir=bin/client_package/config \ --runtime-dir=build/client \ - $DISCOVERY_OPTION\ + --localnet \ $@ diff --git a/bin/helpers/functions.sh b/bin/helpers/functions.sh index 2eb6867f5..20fa4461b 100644 --- a/bin/helpers/functions.sh +++ b/bin/helpers/functions.sh @@ -2,8 +2,6 @@ # Map environment variables to flags for Golang linker's -ldflags usage function get_linker_ldflags { - echo -n "-X 'github.com/mysterium/node/cmd.MysteriumAPIURL=${MYSTERIUM_API_URL}' " - echo -n "-X 'github.com/mysterium/node/cmd/commands/server.natsServerIP=${NATS_SERVER_IP}' " [ -n "$BRANCH" ] && echo -n "-X 'github.com/mysterium/node/version.BuildBranch=${BRANCH}' " [ -n "$COMMIT" ] && echo -n "-X 'github.com/mysterium/node/version.BuildCommit=${COMMIT}' " [ -n "$BUILD_NUMBER" ] && echo -n "-X 'github.com/mysterium/node/version.BuildNumber=${BUILD_NUMBER}' " diff --git a/bin/helpers/load_environment.sh b/bin/helpers/load_environment.sh deleted file mode 100644 index 8963c236c..000000000 --- a/bin/helpers/load_environment.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -if [ ! -f .env ]; then - printf "\e[0;31m%s\e[0m\n" "Environment file must be set!" - exit 1 -fi -source .env diff --git a/bin/helpers/load_run_environment.sh b/bin/helpers/load_run_environment.sh deleted file mode 100644 index 2f47e20fc..000000000 --- a/bin/helpers/load_run_environment.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -if [ -f .env ]; then - source .env - - [ -n "$MYSTERIUM_API_URL" ] && DISCOVERY_OPTION="--discovery-address=$MYSTERIUM_API_URL" - [ -n "$NATS_SERVER_IP" ] && BROKER_OPTION="--broker-address=$NATS_SERVER_IP" -fi diff --git a/bin/localnet/docker-compose.yml b/bin/localnet/docker-compose.yml new file mode 100644 index 000000000..703baa97b --- /dev/null +++ b/bin/localnet/docker-compose.yml @@ -0,0 +1,33 @@ +version: '3' +services: + + broker: + image: nats + expose: + - 4222 + - 8222 + + #infrastructure - centralized api and db + db: + image: percona:5.7 + restart: always + expose: + - 3306 + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: myst_api + MYSQL_USER: myst_api + MYSQL_PASSWORD: myst_api + + discovery: + image: mysteriumnetwork/mysterium-api:0.1.12 + expose: + - 80 + environment: + APP_PORT: 80 + DB_HOST: db + DB_NAME: myst_api + DB_USER: myst_api + DB_PASSWORD: myst_api + depends_on: + - db diff --git a/bin/localnet/functions.sh b/bin/localnet/functions.sh new file mode 100644 index 000000000..d2ca3cec6 --- /dev/null +++ b/bin/localnet/functions.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +source bin/helpers/output.sh + + +PROJECT_FILE="bin/localnet/docker-compose.yml" + +setupDockerComposeCmd() { + projectName=$1; shift; + + projectFiles=("-f ${PROJECT_FILE}") + + for extensionFile in "$@"; do + projectFiles=("${projectFiles[@]}" "-f ${extensionFile}") + done + + dockerComposeCmd="docker-compose ${projectFiles[@]} -p $projectName" +} + +setup () { + + setupDockerComposeCmd "$@" + echo "Setting up: $projectName" + + ${dockerComposeCmd} up -d db # start database first - it takes about 10 sec untils db startsup, and otherwise db migration fails + if [ ! $? -eq 0 ]; then + print_error "Db startup failed" + cleanup "$@" + exit 1 + fi + + echo "Waiting for db to become up" + while ! ${dockerComposeCmd} exec db mysqladmin ping --protocol=TCP --silent; do + echo -n "." + sleep 1 + done + sleep 2 #even after successful TCP connection we still hit db not ready yet sometimes + echo "Database is up" + + ${dockerComposeCmd} run --entrypoint bin/db-upgrade discovery + if [ ! $? -eq 0 ]; then + print_error "Db migration failed" + cleanup "$@" + exit 1 + fi + + ${dockerComposeCmd} up -d broker discovery + if [ ! $? -eq 0 ]; then + print_error "Starting built docker images failed" + cleanup "$@" + exit 1 + fi +} + +cleanup () { + setupDockerComposeCmd "$@" + + echo "Cleaning up: $projectName" + ${dockerComposeCmd} down --remove-orphans +} \ No newline at end of file diff --git a/bin/localnet/publish-ports.yml b/bin/localnet/publish-ports.yml new file mode 100644 index 000000000..45269468e --- /dev/null +++ b/bin/localnet/publish-ports.yml @@ -0,0 +1,11 @@ +version: '3' +services: + + broker: + ports: + - 4222:4222 + - 8222:4222 + + discovery: + ports: + - 80:80 diff --git a/bin/localnet/setup.sh b/bin/localnet/setup.sh new file mode 100755 index 000000000..2034e5ee6 --- /dev/null +++ b/bin/localnet/setup.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +source bin/localnet/functions.sh + +setup "localnet" "bin/localnet/publish-ports.yml" \ No newline at end of file diff --git a/bin/localnet/teardown.sh b/bin/localnet/teardown.sh new file mode 100755 index 000000000..c7d0d88be --- /dev/null +++ b/bin/localnet/teardown.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +source bin/localnet/functions.sh + +cleanup "localnet" \ No newline at end of file diff --git a/bin/server_build b/bin/server_build index 3c0381ad8..f8f1ecbc9 100755 --- a/bin/server_build +++ b/bin/server_build @@ -17,7 +17,6 @@ #> readelf -d build/server/mysterium_server source bin/helpers/functions.sh -source bin/helpers/load_environment.sh export GOOS=${GOOS:=`go env GOHOSTOS`} export GOARCH=${GOARCH:=`go env GOHOSTARCH`} diff --git a/bin/server_run b/bin/server_run index bc0a23f24..edaaa79c4 100755 --- a/bin/server_run +++ b/bin/server_run @@ -1,10 +1,7 @@ #!/bin/bash -source bin/helpers/load_run_environment.sh - sudo ./build/server/mysterium_server \ --config-dir=bin/server_package/config \ --runtime-dir=build/server \ - $DISCOVERY_OPTION \ - $BROKER_OPTION \ + --localnet \ $@ diff --git a/bin/test_e2e b/bin/test_e2e index f6b2e7a51..4fed94be4 100755 --- a/bin/test_e2e +++ b/bin/test_e2e @@ -1,66 +1,30 @@ #!/usr/bin/env bash -source bin/helpers/output.sh +source bin/localnet/functions.sh -PROJECT_NAME="node_e2e_test" -PROJECT_FILE="e2e/docker-compose.yml" +projectName="node_e2e_test" -dockerComposeCmd="docker-compose -f $PROJECT_FILE -p $PROJECT_NAME" +setup $projectName "e2e/docker-compose.yml" -setup () { - ${dockerComposeCmd} up -d db # start database first - it takes about 10 sec untils db startsup, and otherwise db migration fails - if [ ! $? -eq 0 ]; then - print_error "Db startup failed" - cleanup - exit 1 - fi - - ${dockerComposeCmd} build - if [ ! $? -eq 0 ]; then - print_error "Building docker images failed" - cleanup - exit 1 - fi - - echo "Waiting for db to become up" - while ! ${dockerComposeCmd} exec db mysqladmin ping --protocol=TCP --silent; do - echo -n "." - sleep 1 - done - echo "Database is up" - - ${dockerComposeCmd} run --entrypoint bin/db-upgrade discovery - if [ ! $? -eq 0 ]; then - print_error "Db migration failed" - cleanup - exit 1 - fi - - ${dockerComposeCmd} up -d - if [ ! $? -eq 0 ]; then - print_error "Starting built docker images failed" - cleanup - exit 1 - fi - ${dockerComposeCmd} logs -f > e2e_tests.log & -} - -cleanup () { - echo "Cleaning up" - ${dockerComposeCmd} down -} +$dockerComposeCmd build && $dockerComposeCmd up -d +if [ ! $? -eq 0 ] +then + print_error "Image building failed" + cleanup $projectName + exit 1 +fi -setup +$dockerComposeCmd logs -f > e2e_tests.log & go test -v ./e2e/... -args --tequila.host=localhost --tequila.port=4052 if [ ! $? -eq 0 ] then print_error "Tests failed" - cleanup + cleanup $projectName exit 1 fi print_success "Tests passed" -cleanup +cleanup $projectName exit 0 diff --git a/cmd/commands/client/command_client.go b/cmd/commands/client/command_client.go index a8ef953f5..bfb1d3ad9 100644 --- a/cmd/commands/client/command_client.go +++ b/cmd/commands/client/command_client.go @@ -41,9 +41,10 @@ import ( // NewCommand function creates new client command by given options func NewCommand(options CommandOptions) *Command { + networkDefinition := getNetworkDefinition(options) return NewCommandWith( options, - server.NewClient(options.DiscoveryAPIAddress), + server.NewClient(networkDefinition.DiscoveryAPIAddress), ) } @@ -176,3 +177,20 @@ func (cmd *Command) Kill() error { return nil } + +// TODO this function can be aligned with server function when client and server options will merge into +func getNetworkDefinition(options CommandOptions) metadata.NetworkDefinition { + network := metadata.DefaultNetwork + + switch { + case options.Localnet: + network = metadata.LocalnetDefinition + } + + //override defined values one by one from options + if options.DiscoveryAPIAddress != metadata.DefaultNetwork.DiscoveryAPIAddress { + network.DiscoveryAPIAddress = options.DiscoveryAPIAddress + } + + return network +} diff --git a/cmd/commands/client/options.go b/cmd/commands/client/options.go index 045cfb5f9..5dce98e41 100644 --- a/cmd/commands/client/options.go +++ b/cmd/commands/client/options.go @@ -20,6 +20,7 @@ package client import ( "flag" "github.com/mysterium/node/cmd" + "github.com/mysterium/node/metadata" "path/filepath" ) @@ -39,10 +40,10 @@ type CommandOptions struct { LicenseConditions bool DiscoveryAPIAddress string - BrokerAddress string IpifyUrl string LocationDatabase string + Localnet bool } // ParseArguments parses CLI flags and adds to CommandOptions structure @@ -114,7 +115,7 @@ func ParseArguments(args []string) (options CommandOptions, err error) { flags.StringVar( &options.DiscoveryAPIAddress, "discovery-address", - cmd.MysteriumAPIURL, + metadata.DefaultNetwork.DiscoveryAPIAddress, "Address (URL form) of discovery service", ) @@ -132,6 +133,13 @@ func ParseArguments(args []string) (options CommandOptions, err error) { "Service location autodetect database of GeoLite2 format e.g. http://dev.maxmind.com/geoip/geoip2/geolite2/", ) + flags.BoolVar( + &options.Localnet, + "localnet", + false, + "Defines network configuration which expects localy deployed broker and discovery services", + ) + err = flags.Parse(args[1:]) if err != nil { return diff --git a/cmd/commands/server/factory.go b/cmd/commands/server/factory.go index de4ed124e..b68313501 100644 --- a/cmd/commands/server/factory.go +++ b/cmd/commands/server/factory.go @@ -26,6 +26,7 @@ import ( "github.com/mysterium/node/identity" "github.com/mysterium/node/ip" "github.com/mysterium/node/location" + "github.com/mysterium/node/metadata" "github.com/mysterium/node/nat" "github.com/mysterium/node/openvpn" "github.com/mysterium/node/openvpn/middlewares/server/auth" @@ -40,9 +41,11 @@ import ( // NewCommand function creates new server command by given options func NewCommand(options CommandOptions) *Command { + networkDefinition := getNetworkDefinition(options) return NewCommandWith( options, - server.NewClient(options.DiscoveryAPIAddress), + networkDefinition, + server.NewClient(networkDefinition.DiscoveryAPIAddress), ip.NewResolver(options.IpifyUrl), nat.NewService(), ) @@ -51,6 +54,7 @@ func NewCommand(options CommandOptions) *Command { // NewCommandWith function creates new client command by given options + injects given dependencies func NewCommandWith( options CommandOptions, + networkDefinition metadata.NetworkDefinition, mysteriumClient server.Client, ipResolver ip.Resolver, natService nat.NATService, @@ -91,7 +95,7 @@ func NewCommandWith( natService: natService, dialogWaiterFactory: func(myID identity.Identity) communication.DialogWaiter { return nats_dialog.NewDialogWaiter( - nats_discovery.NewAddressGenerate(options.BrokerAddress, myID), + nats_discovery.NewAddressGenerate(networkDefinition.BrokerAddress, myID), identity.NewSigner(keystoreInstance, myID), ) }, @@ -136,3 +140,23 @@ func NewCommandWith( WaitUnregister: &sync.WaitGroup{}, } } + +// TODO this function can be aligned with client function when client and server options will merge into +func getNetworkDefinition(options CommandOptions) metadata.NetworkDefinition { + network := metadata.DefaultNetwork + + switch { + case options.Localnet: + network = metadata.LocalnetDefinition + } + + //override defined values one by one from options + if options.DiscoveryAPIAddress != metadata.DefaultNetwork.DiscoveryAPIAddress { + network.DiscoveryAPIAddress = options.DiscoveryAPIAddress + } + + if options.BrokerAddress != metadata.DefaultNetwork.BrokerAddress { + network.BrokerAddress = options.BrokerAddress + } + return network +} diff --git a/cmd/commands/server/options.go b/cmd/commands/server/options.go index dd27d2d0b..b91c347f7 100644 --- a/cmd/commands/server/options.go +++ b/cmd/commands/server/options.go @@ -20,6 +20,7 @@ package server import ( "flag" "github.com/mysterium/node/cmd" + "github.com/mysterium/node/metadata" "path/filepath" ) @@ -47,13 +48,11 @@ type CommandOptions struct { Protocol string OpenvpnPort int + Localnet bool } const defaultLocationDatabase = "GeoLite2-Country.mmdb" -// TODO: rename to brokerAddress -var natsServerIP string - // ParseArguments parses CLI flags and adds to CommandOptions structure func ParseArguments(args []string) (options CommandOptions, err error) { flags := flag.NewFlagSet(args[0], flag.ContinueOnError) @@ -125,13 +124,14 @@ func ParseArguments(args []string) (options CommandOptions, err error) { flags.StringVar( &options.DiscoveryAPIAddress, "discovery-address", - cmd.MysteriumAPIURL, + metadata.DefaultNetwork.DiscoveryAPIAddress, "Address (URL form) of discovery service", ) + flags.StringVar( &options.BrokerAddress, "broker-address", - natsServerIP, + metadata.DefaultNetwork.BrokerAddress, "Address (IP or domain name) of message broker", ) @@ -161,6 +161,13 @@ func ParseArguments(args []string) (options CommandOptions, err error) { "Address (URL form) of ipify service", ) + flags.BoolVar( + &options.Localnet, + "localnet", + false, + "Defines network configuration which expects localy deployed broker and discovery services", + ) + err = flags.Parse(args[1:]) if err != nil { return diff --git a/cmd/config.go b/cmd/config.go deleted file mode 100644 index be15adaa4..000000000 --- a/cmd/config.go +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2017 The "MysteriumNetwork/node" Authors. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package cmd - -// MysteriumAPIURL stores MYSTERIUM_API_URL env variable that is complied in -var MysteriumAPIURL string diff --git a/e2e/docker-compose.yml b/e2e/docker-compose.yml index c55abcf52..f5315e4b5 100644 --- a/e2e/docker-compose.yml +++ b/e2e/docker-compose.yml @@ -1,15 +1,9 @@ version: '3' services: - broker: - image: nats - expose: - - 4222 - - 8222 - node: build: - context: .. + context: ../.. dockerfile: bin/server_docker/alpine/Dockerfile depends_on: - broker @@ -28,7 +22,7 @@ services: client: build: - context: .. + context: ../.. dockerfile: bin/client_docker/alpine/Dockerfile depends_on: - broker @@ -41,30 +35,6 @@ services: - 4052:4050 command: "--ipify-url=http://ipify:3000 --discovery-address=http://discovery/v1" - #infrastructure - centralized api and db - db: - image: percona:5.7 - restart: always - expose: - - 3306 - environment: - MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: myst_api - MYSQL_USER: myst_api - MYSQL_PASSWORD: myst_api - - discovery: - image: mysteriumnetwork/mysterium-api:0.1.12 - expose: - - 80 - environment: - APP_PORT: 80 - DB_HOST: db - DB_NAME: myst_api - DB_USER: myst_api - DB_PASSWORD: myst_api - depends_on: - - db #'external' IP detection ipify: image: owlab/ipify diff --git a/metadata/network.go b/metadata/network.go new file mode 100644 index 000000000..f474d436e --- /dev/null +++ b/metadata/network.go @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2017 The "MysteriumNetwork/node" Authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package metadata + +// NetworkDefinition structure holds all parameters which describe particular network +type NetworkDefinition struct { + DiscoveryAPIAddress string + BrokerAddress string +} + +// TestnetDefinition defines parameters for test network (currently default network) +var TestnetDefinition = NetworkDefinition{ + "https://testnet-api.mysterium.network/v1", + "testnet-broker.mysterium.network", +} + +// LocalnetDefinition defines parameters for local network (expects discovery and broker services on localhost) +var LocalnetDefinition = NetworkDefinition{ + "http://localhost/v1", + "localhost", +} + +// DefaultNetwork defines default network values when no runtime parameters are given +var DefaultNetwork = TestnetDefinition