oraivisor

package module
v0.0.0-...-c29cd6b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

README

oraivisor Quick Start

oraivisor is a small process manager around Oraichain binaries that monitors the governance module via stdout to see if there's a chain upgrade proposal coming in. If it see a proposal that gets approved it can be run manually or automatically to download the new code, stop the node, run the migration script, replace the node binary, and start with the new genesis file.

Installation

Run:

make build

Command Line Arguments And Environment Variables

All arguments passed to the oraivisor program will be passed to the current daemon binary (as a subprocess). It will return /dev/stdout and /dev/stderr of the subprocess as its own. Because of that, it cannot accept any command line arguments, nor print anything to output (unless it terminates unexpectedly before executing a binary).

oraivisor reads its configuration from environment variables:

  • DAEMON_HOME is the location where upgrade binaries should be kept (e.g. $HOME/.oraid).
  • DAEMON_NAME is the name of the binary itself (eg. oraid, etc).
  • DAEMON_ALLOW_DOWNLOAD_BINARIES (optional) if set to true will enable auto-downloading of new binaries (for security reasons, this is intended for full nodes rather than validators).
  • DAEMON_RESTART_AFTER_UPGRADE (optional) if set to true it will restart the sub-process with the same command line arguments and flags (but new binary) after a successful upgrade. By default, oraivisor dies afterwards and allows the supervisor to restart it if needed. Note that this will not auto-restart the child if there was an error.

Data Folder Layout

$DAEMON_HOME/oraivisor is expected to belong completely to oraivisor and subprocesses that are controlled by it. The folder content is organised as follows:

.
├── current -> genesis or upgrades/<name>
├── genesis
│   └── bin
│       └── $DAEMON_NAME
└── upgrades
    └── <name>
        └── bin
            └── $DAEMON_NAME

Each version of the Oraichain application is stored under either genesis or upgrades/<name>, which holds bin/$DAEMON_NAME along with any other needed files such as auxiliary client programs or libraries. current is a symbolic link to the currently active folder (so current/bin/$DAEMON_NAME is the currently active binary).

Note: the name variable in upgrades/<name> holds the URI-encoded name of the upgrade as specified in the upgrade module plan.

Please note that $DAEMON_HOME/oraivisor just stores the binaries and associated program code. The oraivisor binary can be stored in any typical location (eg /usr/local/bin). The actual blockchain program will store it's data under their default data directory (e.g. $HOME/.oraid) which is independent of the $DAEMON_HOME. You can choose to set $DAEMON_HOME to the actual binary's home directory and then end up with a configuation like the following, but this is left as a choice to the system admininstrator for best directory layout:

.oraid
├── config
├── data
└── oraivisor

Usage

The system administrator admin is responsible for:

  • installing the oraivisor binary and configure the host's init system (e.g. systemd, launchd, etc) along with the environmental variables appropriately;
  • installing the genesis folder manually;
  • installing the upgrades/<name> folders manually.

oraivisor will set the current link to point to genesis at first start (when no current link exists) and handles binaries switch overs at the correct points in time, so that the system administrator can prepare days in advance and relax at upgrade time.

Note that blockchain applications that wish to support upgrades may package up a genesis oraivisor tarball with this information, just as they prepare the genesis binary tarball. In fact, they may offer a tarball will all upgrades up to current point for easy download for those who wish to sync a fullnode from start.

The DAEMON specific code and operations (e.g. tendermint config, the application db, syncing blocks, etc) are performed as normal. Application binaries' directives such as command-line flags and environment variables work normally.

Example: oraid

The following instructions provide a demonstration of oraivisor's integration with the oraid application shipped along the Oraichain's source code.

First compile oraid:

cd /workspace
make build

Set the required environment variables:

export DAEMON_NAME=oraid         # binary name
export DAEMON_HOME=$HOME/.oraid  # daemon's home directory

Create the oraivisor’s genesis folders and deploy the binary:

mkdir -p $DAEMON_HOME/oraivisor/genesis/bin
cp ./build/oraid $DAEMON_HOME/oraivisor/genesis/bin

Create a new key and setup the oraid node:

./scripts/setup_oraid.sh 12345678

For the sake of this demonstration, we would amend voting_params.voting_period in .oraid/config/genesis.json to a reduced time ~1 minutes (60s) and eventually launch oraivisor:

sed -i 's/voting_period" *: *".*"/voting_period": "60s"/g' .oraid/config/genesis.json

Now oraivisor is a replacement for oraid

oraivisor start

For the sake of this demonstration, we will hardcode a modification in oraid to simulate a code change. In oraid/app.go, find the line containing the upgrade Keeper initialisation, it should look like app.upgradekeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, ...). After that line, add the following snippet:

app.upgradekeeper.SetUpgradeHandler("ai-oracle", func(ctx sdk.Context, plan upgradetypes.Plan) {
    // Add modification logic
})

then rebuild it with make build

Submit a software upgrade proposal:

# check orai.env for allowing auto download and upgrade form a URL
# DAEMON_ALLOW_DOWNLOAD_BINARIES=true
# DAEMON_RESTART_AFTER_UPGRADE=true

# using s3 to store build file
aws s3 mb s3://orai
aws s3 cp build/oraid s3://orai --acl public-read
echo '{"binaries":{"linux/amd64":"https://orai.s3.amazonaws.com/oraid?versionId=new_oraid_version"}}' > build/manifest.json
aws s3 cp build/manifest.json s3://orai --acl public-read

# then submit proposal
oraid tx gov submit-proposal software-upgrade "v0.41.0" --title "upgrade Oraichain network to v0.41.0, patches the Dragonberry advisory with custom CosmWasm - backward compatibility for v0.13.2" --description "Please visit https://github.com/oraichain/orai to view the CHANGELOG for this upgrade" --from $USER --upgrade-height 9415363 --upgrade-info "https://orai.s3.us-east-2.amazonaws.com/v0.41.0/manifest.json" --deposit 10000000orai --chain-id Oraichain-testnet -y

Submit a Yes vote for the upgrade proposal:

oraid tx gov vote 1 yes --from $USER --chain-id $CHAIN_ID -y

Query the proposal to ensure it was correctly broadcast and added to a block:

oraid query gov proposal 1

The upgrade will occur automatically at height 20.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoUpgrade

func DoUpgrade(cfg *Config, info *UpgradeInfo) error

DoUpgrade will be called after the log message has been parsed and the process has terminated. We can now make any changes to the underlying directory without interference and leave it in a state, so we can make a proper restart

func DownloadBinary

func DownloadBinary(cfg *Config, info *UpgradeInfo) error

DownloadBinary will grab the binary and place it in the proper directory

func EnsureBinary

func EnsureBinary(path string) error

EnsureBinary ensures the file exists and is executable, or returns an error

func GetDownloadURL

func GetDownloadURL(info *UpgradeInfo) (string, error)

GetDownloadURL will check if there is an arch-dependent binary specified in Info

func LaunchProcess

func LaunchProcess(cfg *Config, args []string, stdout, stderr io.Writer) (bool, error)

LaunchProcess runs a subprocess and returns when the subprocess exits, either when it dies, or *after* a successful upgrade.

func MarkExecutable

func MarkExecutable(path string) error

MarkExecutable will try to set the executable bits if not already set Fails if file doesn't exist or we cannot set those bits

func OSArch

func OSArch() string

Types

type Config

type Config struct {
	Home                  string
	Name                  string
	AllowDownloadBinaries bool
	RestartAfterUpgrade   bool
	LogBufferSize         int
}

Config is the information passed in to control the daemon

func GetConfigFromEnv

func GetConfigFromEnv() (*Config, error)

GetConfigFromEnv will read the environmental variables into a config and then validate it is reasonable

func (*Config) CurrentBin

func (cfg *Config) CurrentBin() (string, error)

CurrentBin is the path to the currently selected binary (genesis if no link is set) This will resolve the symlink to the underlying directory to make it easier to debug

func (*Config) GenesisBin

func (cfg *Config) GenesisBin() string

GenesisBin is the path to the genesis binary - must be in place to start manager

func (*Config) Root

func (cfg *Config) Root() string

Root returns the root directory where all info lives

func (*Config) SetCurrentUpgrade

func (cfg *Config) SetCurrentUpgrade(upgradeName string) error

SetCurrentUpgrade sets the named upgrade to be the current link, returns error if this binary doesn't exist

func (*Config) SymLinkToGenesis

func (cfg *Config) SymLinkToGenesis() (string, error)

Symlink to genesis

func (*Config) UpgradeBin

func (cfg *Config) UpgradeBin(upgradeName string) string

UpgradeBin is the path to the binary for the named upgrade

func (*Config) UpgradeDir

func (cfg *Config) UpgradeDir(upgradeName string) string

UpgradeDir is the directory named upgrade

type UpgradeConfig

type UpgradeConfig struct {
	Binaries map[string]string `json:"binaries"`
}

UpgradeConfig is expected format for the info field to allow auto-download

type UpgradeInfo

type UpgradeInfo struct {
	Name string
	Info string
}

UpgradeInfo is the details from the regexp

func WaitForUpdate

func WaitForUpdate(scanner *bufio.Scanner) (*UpgradeInfo, error)

WaitForUpdate will listen to the scanner until a line matches upgradeRegexp. It returns (info, nil) on a matching line It returns (nil, err) if the input stream errored It returns (nil, nil) if the input closed without ever matching the regexp

func WaitForUpgradeOrExit

func WaitForUpgradeOrExit(cmd *exec.Cmd, scanOut, scanErr *bufio.Scanner) (*UpgradeInfo, error)

WaitForUpgradeOrExit listens to both output streams of the process, as well as the process state itself When it returns, the process is finished and all streams have closed.

It returns (info, nil) if an upgrade should be initiated (and we killed the process) It returns (nil, err) if the process died by itself, or there was an issue reading the pipes It returns (nil, nil) if the process exited normally without triggering an upgrade. This is very unlikely to happened with "start" but may happened with short-lived commands like `gaiad export ...`

type WaitResult

type WaitResult struct {
	// contains filtered or unexported fields
}

WaitResult is used to wrap feedback on cmd state with some mutex logic. This is needed as multiple go-routines can affect this - two read pipes that can trigger upgrade As well as the command, which can fail

func (*WaitResult) AsResult

func (u *WaitResult) AsResult() (*UpgradeInfo, error)

AsResult reads the data protected by mutex to avoid race conditions

func (*WaitResult) SetError

func (u *WaitResult) SetError(myErr error)

SetError will set with the first error using a mutex don't set it once info is set, that means we chose to kill the process

func (*WaitResult) SetUpgrade

func (u *WaitResult) SetUpgrade(up *UpgradeInfo)

SetUpgrade sets first non-nil upgrade info, ensure error is then nil pass in a command to shutdown on successful upgrade

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL