Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) spin up audiusd container without audius-docker-compose #10441

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ build-push-wrapper:

.PHONY: build-audiusd-local build-push-audiusd
build-audiusd-local:
docker build -t audius/audiusd:$(AD_TAG) -f ./cmd/audiusd/Dockerfile ./
DOCKER_DEFAULT_PLATFORM=linux/amd64 docker build -t audius/audiusd:$(AD_TAG) -f ./cmd/audiusd/Dockerfile ./

build-push-audiusd:
DOCKER_DEFAULT_PLATFORM=linux/amd64 docker build --push -t audius/audiusd:$(AD_TAG) -f ./cmd/audiusd/Dockerfile ./
Expand Down
12 changes: 9 additions & 3 deletions cmd/audiusd/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
FROM debian:bookworm AS cpp-builder
FROM debian:bullseye AS cpp-builder

RUN apt-get update && \
apt-get install -y curl gnupg2 lsb-release && \
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg && \
echo "deb [signed-by=/usr/share/keyrings/postgresql-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/postgresql.list

RUN apt-get update && apt-get install -y \
build-essential \
Expand All @@ -19,6 +24,7 @@ RUN apt-get update && apt-get install -y \
libavformat-dev \
libavutil-dev \
libswresample-dev \
libavresample-dev \
libsamplerate0-dev \
libtag1-dev \
libchromaprint-dev \
Expand Down Expand Up @@ -73,11 +79,11 @@ RUN g++ -o /bin/analyze-key /app/cpp/keyfinder.cpp \
RUN g++ -o /bin/analyze-bpm /app/cpp/bpm-analyzer.cpp \
-I/usr/include/eigen3 -I/usr/local/include/essentia -I/usr/local/include \
-L/usr/local/lib \
-lessentia -ltag -lyaml -lfftw3 -lfftw3f -lavcodec -lavformat -lavutil -lavfilter -lsamplerate -lswresample -lpthread -lz -lchromaprint && \
-lessentia -ltag -lyaml -lfftw3 -lfftw3f -lavcodec -lavformat -lavutil -lavfilter -lsamplerate -lavresample -lpthread -lz -lchromaprint && \
chmod +x /bin/analyze-bpm


FROM golang:1.22-bookworm AS go-builder
FROM golang:1.22-bullseye AS go-builder

WORKDIR /app

Expand Down
11 changes: 8 additions & 3 deletions cmd/audiusd/entrypoint.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ source_env_file() {
[[ -z "$key" ]] && continue
# only set variables that are not already defined (prioritize docker-passed env)
if [ -z "${!key}" ]; then
export "$key"="$value"
# strip quotations
val="${value%\"}"
val="${val#\"}"
export "$key"="$val"
fi
done < "$file"
else
Expand All @@ -29,18 +32,20 @@ source_env_file "$OVERRIDE_ENV_FILE"
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres}
POSTGRES_DB=${POSTGRES_DB:-audiusd}
POSTGRES_DATA_DIR=${POSTGRES_DATA_DIR:-/var/lib/postgresql/data}
export dbUrl=${dbUrl:-postgresql://postgres:postgres@localhost:5432/audius_creator_node?sslmode=disable}
export dbUrl=${dbUrl:-postgresql://postgres:postgres@localhost:5432/audiusd?sslmode=disable}
export uptimeDataDir=${uptimeDataDir:-/data/bolt}
export audius_core_root_dir=${audius_core_root_dir:-/data/audiusd}
export creatorNodeEndpoint=${creatorNodeEndpoint:-http://localhost}

if [ ! -d "$POSTGRES_DATA_DIR" ]; then
if [ ! -d "$POSTGRES_DATA_DIR" ] || [ -z "$(ls -A "$POSTGRES_DATA_DIR" 2>/dev/null)" ]; then
echo "Initializing PostgreSQL data directory at $POSTGRES_DATA_DIR..."
su - postgres -c "/usr/lib/postgresql/*/bin/initdb -D $POSTGRES_DATA_DIR"

echo "Updating PostgreSQL configuration for password authentication..."
sed -i "s/peer/trust/g" "$POSTGRES_DATA_DIR/pg_hba.conf"
sed -i "s/md5/trust/g" "$POSTGRES_DATA_DIR/pg_hba.conf"
else
echo "PostgreSQL data directory already initialized at $POSTGRES_DATA_DIR."
fi

chown -R postgres:postgres "$POSTGRES_DATA_DIR"
Expand Down
1 change: 1 addition & 0 deletions dev-tools/templates/devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ nodes:
httpPort: 4000
httpsPort: 4001
version: prerelease
isLocalhost: true
privateKey: 21118f9a6de181061a2abd549511105adb4877cf9026f271092e6813b7cf58ab
wallet: 0x0D38e653eC28bdea5A2296fD5940aaB2D0B8875c
rewardsWallet: 0xb3c66e682Bf9a85F6800c769AC5A05c18C3F331d
Expand Down
232 changes: 224 additions & 8 deletions pkg/orchestration/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ var (
},
}

devnetHosts = []string{
"creator-1.devnet.audius-d:172.100.0.1",
"discovery-1.devnet.audius-d:172.100.0.1",
"identity.devnet.audius-d:172.100.0.1",
"eth-ganache.devnet.audius-d:172.100.0.1",
"acdc-ganache.devnet.audius-d:172.100.0.1",
"solana-test-validator.devnet.audius-d:172.100.0.1",
}

semverRegex = regexp.MustCompile(`\d+\.\d+\.\d+`)
)

Expand Down Expand Up @@ -169,14 +178,7 @@ func runNode(

if nconf.DeployOn == conf.Devnet {
hostConfig.NetworkMode = "deployments_devnet"
hostConfig.ExtraHosts = []string{
"creator-1.devnet.audius-d:172.100.0.1",
"discovery-1.devnet.audius-d:172.100.0.1",
"identity.devnet.audius-d:172.100.0.1",
"eth-ganache.devnet.audius-d:172.100.0.1",
"acdc-ganache.devnet.audius-d:172.100.0.1",
"solana-test-validator.devnet.audius-d:172.100.0.1",
}
hostConfig.ExtraHosts = devnetHosts
containerConfig.Env = []string{"HOST_DOCKER_INTERNAL=172.100.0.1"}
}

Expand Down Expand Up @@ -365,6 +367,220 @@ func runNode(
return nil
}

func runNodeWrapperless(
host string,
config conf.NodeConfig,
nconf conf.NetworkConfig,
) error {
if config.Type != conf.Content {
return logger.Errorf("Unsupported node type %s", config.Type)
}

execHost := host
if config.IsLocalhost {
execHost = "localhost"
}
containerName := host
if host == "localhost" {
containerName = "audiusd"
}

dockerClient, err := getDockerClient(execHost)
if err != nil {
return logger.Error(err)
}
defer dockerClient.Close()

if isContainerRunning(dockerClient, containerName) {
logger.Infof("audiusd container already running for %s", host)
logger.Infof("Use 'audius-ctl restart %s' for a clean restart", host)
return nil
} else if isContainerNameInUse(dockerClient, containerName) {
logger.Infof("stopped audiusd container '%s' exists on %s, removing and starting with current config", containerName, host)
if err := removeContainerByName(dockerClient, containerName); err != nil {
return logger.Error(err)
}
}

logger.Infof("\nStarting %s\n", host)

tag := config.Version
if tag == "" {
tag = "current"
}

containerConfig := &container.Config{
Image: fmt.Sprintf("audius/audiusd:%s", tag),
}
hostConfig := &container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: "/var/k8s/mediorum",
Target: "/tmp/mediorum",
},
{
Type: mount.TypeBind,
Source: "/var/k8s/creator-node-backend",
Target: "/file_storage",
},
{
Type: mount.TypeBind,
Source: "/var/k8s/creator-node-db-15",
Target: "/var/lib/postgresql/data",
},
{
Type: mount.TypeBind,
Source: "/var/k8s/bolt",
Target: "/bolt",
},
{
Type: mount.TypeBind,
Source: "/var/k8s/bolt",
Target: "/audius-core",
},
{
Type: mount.TypeBind,
Source: "/var/k8s/env",
Target: "/env",
},
// allow me to work on entrypoint without building the image
{
Type: mount.TypeBind,
Source: "/private/var/orion/audius/audius-protocol/cmd/audiusd/entrypoint.sh",
Target: "/bin/entrypoint.sh",
},
},
}

var port uint = 80
var tlsPort uint = 443
if config.HttpPort != 0 {
port = config.HttpPort
}
if config.HttpsPort != 0 {
tlsPort = config.HttpsPort
}
httpPorts := fmt.Sprintf("%d:%d", port, port)
httpsPorts := fmt.Sprintf("%d:%d", tlsPort, tlsPort)
allPorts := []string{httpPorts, httpsPorts}
if config.HostPorts != "" {
allPorts = append(allPorts, strings.Split(config.HostPorts, ",")...)
}

if config.CorePortP2P == 0 {
config.CorePortP2P = 26656
}

if config.CorePortRPC == 0 {
config.CorePortRPC = 26657
}

allPorts = append(allPorts, fmt.Sprintf("%d:26656", config.CorePortP2P), fmt.Sprintf("%d:26657", config.CorePortRPC))

portSet, portBindings, err := nat.ParsePortSpecs(allPorts)
if err != nil {
return logger.Error(err)
}
containerConfig.ExposedPorts = portSet
hostConfig.PortBindings = portBindings

if nconf.DeployOn == conf.Devnet {
hostConfig.NetworkMode = "deployments_devnet"
hostConfig.ExtraHosts = devnetHosts
containerConfig.Env = []string{"HOST_DOCKER_INTERNAL=172.100.0.1"}
}

logger.Info("Generating configuration...")

privateKey, err := NormalizedPrivateKey(execHost, config.PrivateKey)
if err != nil {
return logger.Error(err)
}
config.PrivateKey = privateKey

override := config.ToOverrideEnv(host, nconf)

// generate the override.env file locally
localOverrideFile, err := os.CreateTemp(".", fmt.Sprintf("%s*.env", host))
if err != nil {
return logger.Error("Could not create local temp file:", err)
}
localOverridePath := localOverrideFile.Name()
localOverrideFile.Close()

if err := appendRemoteConfig(execHost, override, config.RemoteConfigFile); err != nil {
return logger.Error(err)
}
if err := godotenv.Write(override, localOverridePath); err != nil {
return logger.Error(err)
}
if err := copyFileToHost(execHost, localOverridePath, "/var/k8s/env/override.env"); err != nil {
return logger.Errorf("Could not copy override config to host: %v", err)
}
if err := os.Remove(localOverridePath); err != nil {
return logger.Error(err)
}

logger.Info("Pulling audiusd image...")
logger.Debugf("Using image %s", containerConfig.Image)
pullResp, err := dockerClient.ImagePull(context.Background(), containerConfig.Image, types.ImagePullOptions{})
if err != nil {
logger.Warnf("Failed to pull image: %v", err)
logger.Warnf("Continuing with local image repository")
} else {
defer pullResp.Close()
if err := readAndLogCommandOutput(bufio.NewReader(pullResp)); err != nil {
return logger.Error("Error reading ImagePull output:", err)
}
}

// create audiusd container
createResponse, err := dockerClient.ContainerCreate(
context.Background(),
containerConfig,
hostConfig,
nil,
nil,
containerName,
)
if err != nil {
return logger.Error("Failed to create container:", err)
}
if err := dockerClient.ContainerStart(
context.Background(),
createResponse.ID,
container.StartOptions{},
); err != nil {
return logger.Error("Failed to start container:", err)
}

// Wait for audius-d wrapper to be ready
ready := false
timeout := time.After(30 * time.Second)
for !ready {
select {
case <-timeout:
return logger.Error("Timeout waiting for audiusd container to start")
default:
inspect, err := dockerClient.ContainerInspect(context.Background(), createResponse.ID)
if err != nil {
return logger.Error("Could not get status of audiusd container:", err)
}
time.Sleep(3 * time.Second)
ready = inspect.State.Running
logger.Debugf("audiusd container status: %s", inspect.State.Status)
}
}

// logger.Info("Creating auto-update cron job")
// if err := setAutoUpdateCron(host, config.Version, nconf.DeployOn); err != nil {
// return logger.Error("Failed to add auto-update cron job:", err)
// }

return nil
}

func isContainerRunning(dockerClient *client.Client, containerName string) bool {
containers, err := dockerClient.ContainerList(context.Background(), container.ListOptions{})
if err != nil {
Expand Down
Loading