github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/localnet/localnet.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package localnet
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/magefile/mage/sh"
    24  	"github.com/mysteriumnetwork/node/logconfig"
    25  	"github.com/pkg/errors"
    26  	"github.com/rs/zerolog/log"
    27  )
    28  
    29  const composeFile = "./docker-compose.localnet.yml"
    30  
    31  // LocalnetUp starts local environment
    32  func LocalnetUp() error {
    33  	logconfig.Bootstrap()
    34  	runner := newRunner(composeFile)
    35  	if err := runner.Up(); err != nil {
    36  		log.Err(err).Msg("Failed to start local environment")
    37  		if err := runner.Down(); err != nil {
    38  			log.Err(err).Msg("Failed to cleanup environment")
    39  		}
    40  		return err
    41  	}
    42  	return nil
    43  }
    44  
    45  // LocalnetDown stops local environment
    46  func LocalnetDown() error {
    47  	logconfig.Bootstrap()
    48  	runner := newRunner(composeFile)
    49  	return runner.Down()
    50  }
    51  
    52  // NewRunner returns e2e test runners instance
    53  func newRunner(composeFiles ...string) *runner {
    54  	fileArgs := make([]string, 0)
    55  	for _, f := range composeFiles {
    56  		fileArgs = append(fileArgs, "-f", f)
    57  	}
    58  	envName := "localnet"
    59  	var args []string
    60  	args = append(args, fileArgs...)
    61  	args = append(args, "-p", envName)
    62  
    63  	return &runner{
    64  		compose: sh.RunCmd("docker-compose", args...),
    65  		envName: envName,
    66  	}
    67  }
    68  
    69  type runner struct {
    70  	compose func(args ...string) error
    71  	envName string
    72  }
    73  
    74  // Up starts containers.
    75  func (r *runner) Up() error {
    76  	if err := r.startAppContainers(); err != nil {
    77  		return errors.Wrap(err, "could not start app containers")
    78  	}
    79  
    80  	if err := r.startProviderConsumerNodes(); err != nil {
    81  		return errors.Wrap(err, "could not start provider consumer nodes")
    82  	}
    83  	return nil
    84  }
    85  
    86  // Down stops containers.
    87  func (r *runner) Down() error {
    88  	if err := r.compose("down", "--remove-orphans", "--timeout", "30"); err != nil {
    89  		return errors.Wrap(err, "could not stop environment")
    90  	}
    91  	return nil
    92  }
    93  
    94  func (r *runner) startAppContainers() error {
    95  	log.Info().Msg("Starting other services")
    96  	if err := r.compose("pull"); err != nil {
    97  		return errors.Wrap(err, "could not pull images")
    98  	}
    99  	if err := r.compose("up", "-d", "broker", "ganache", "ipify", "morqa"); err != nil {
   100  		return errors.Wrap(err, "starting other services failed!")
   101  	}
   102  	log.Info().Msg("Starting DB")
   103  	if err := r.compose("up", "-d", "db"); err != nil {
   104  		return errors.Wrap(err, "starting DB failed!")
   105  	}
   106  
   107  	dbUp := false
   108  	for start := time.Now(); !dbUp && time.Since(start) < 60*time.Second; {
   109  		err := r.compose("exec", "-T", "db", "mysqladmin", "ping", "--protocol=TCP", "--silent")
   110  		if err != nil {
   111  			log.Info().Msg("Waiting...")
   112  		} else {
   113  			log.Info().Msg("DB is up")
   114  			dbUp = true
   115  			break
   116  		}
   117  	}
   118  	if !dbUp {
   119  		return errors.New("starting DB timed out")
   120  	}
   121  
   122  	log.Info().Msg("Starting transactor")
   123  	if err := r.compose("up", "-d", "transactor"); err != nil {
   124  		return errors.Wrap(err, "starting transactor failed!")
   125  	}
   126  
   127  	log.Info().Msg("Migrating DB")
   128  	if err := r.compose("run", "--entrypoint", "bin/db-upgrade", "mysterium-api"); err != nil {
   129  		return errors.Wrap(err, "migrating DB failed!")
   130  	}
   131  
   132  	log.Info().Msg("Starting mysterium-api")
   133  	if err := r.compose("up", "-d", "mysterium-api"); err != nil {
   134  		return errors.Wrap(err, "starting mysterium-api failed!")
   135  	}
   136  
   137  	log.Info().Msg("Deploying contracts")
   138  	err := r.compose("run", "go-runner",
   139  		"go", "run", "./e2e/blockchain/deployer.go",
   140  		"--keystore.directory=./e2e/blockchain/keystore",
   141  		"--ether.address=0x354Bd098B4eF8c9E70B7F21BE2d455DF559705d7")
   142  	if err != nil {
   143  		return errors.Wrap(err, "failed to deploy contracts!")
   144  	}
   145  
   146  	log.Info().Msg("starting hermes")
   147  	if err := r.compose("up", "-d", "hermes"); err != nil {
   148  		return errors.Wrap(err, "starting hermes failed!")
   149  	}
   150  
   151  	return nil
   152  }
   153  
   154  func (r *runner) startProviderConsumerNodes() error {
   155  	log.Info().Msg("Building app images")
   156  	if err := r.compose("build"); err != nil {
   157  		return errors.Wrap(err, "building app images failed!")
   158  	}
   159  
   160  	log.Info().Msg("Starting app containers")
   161  	if err := r.compose("up", "-d", "myst-provider", "myst-consumer"); err != nil {
   162  		return errors.Wrap(err, "starting app containers failed!")
   163  	}
   164  	return nil
   165  }