index.ts
Overview
This file serves as the **Pulumi entry point** for deploying the Bitcoin coinstack within the Unchained infrastructure automation system. Its primary responsibility is to:
Load environment and configuration files.
Prepare service-specific deployment arguments for blockchain node components (daemon and indexer).
Invoke the core deployment function
deployCoinstackto orchestrate Kubernetes resource creation for this coinstack.
It integrates blockchain-specific parameters with the generic deployment automation framework, enabling consistent, configurable, and repeatable deployment of Bitcoin node services on Kubernetes clusters.
Detailed Explanation
Module Imports
import { readFileSync } from 'fs'
import { deployCoinstack } from '../../../../pulumi/src/coinstack'
import { CoinServiceArgs, Outputs, getConfig } from '../../../../pulumi/src'
import { defaultBlockbookServiceArgs } from '../../../packages/blockbook/src/constants'
readFileSync: Node.js filesystem API to synchronously read files.deployCoinstack: Core function that manages deployment of coinstack services on Kubernetes.CoinServiceArgs,Outputs,getConfig: Types and utilities for configuring services and retrieving Kubernetes config.defaultBlockbookServiceArgs: Default configuration constants for the indexer service (Blockbook).
Exported Async Function (Default Pulumi Entry Point)
export = async (): Promise<Outputs> => { ... }
This anonymous async function is the Pulumi program’s entry point, following Pulumi’s JavaScript/TypeScript pattern.
Returns: A
Promiseresolving toOutputs— the deployment outputs such as service URLs, resource names, and status.
Variables and Configuration
const appName = 'unchained'
const coinstack = 'bitcoin'
const sampleEnv = readFileSync('../sample.env')
const { kubeconfig, config, namespace } = await getConfig()
appName: Application identifier.coinstack: Specifies the blockchain type — here, Bitcoin.sampleEnv: Contents of the sample environment file used to create Kubernetes Secrets.getConfig(): Async function that retrieves the Kubernetes configuration (kubeconfig), deployment configuration (config), and target namespace.
Preparing coinServiceArgs
const coinServiceArgs = config.statefulService?.services?.map((service): CoinServiceArgs => {
switch (service.name) {
case 'daemon':
return {
...service,
ports: { 'daemon-rpc': { port: 8332 } },
env: {
NETWORK: config.network,
},
readinessProbe: { periodSeconds: 30, failureThreshold: 10 },
}
case 'indexer':
return {
...service,
...defaultBlockbookServiceArgs,
configMapData: { 'indexer-config.json': readFileSync('../indexer/config.json').toString() },
}
default:
throw new Error(`no support for coin service: ${service.name}`)
}
})
Iterates over each service defined in the stateful service configuration.
For each service:
daemonservice:Adds the Bitcoin RPC port 8332.
Sets environment variable
NETWORKfrom the config.Defines a readiness probe to check service health.
indexerservice:Applies default Blockbook service arguments (e.g., ports, volumes, probes).
Injects the indexer configuration JSON as a ConfigMap.
Throws an error if an unsupported service name is encountered.
**Parameters:**
service: Each service object fromconfig.statefulService.services.
**Returns:**
An array of
CoinServiceArgsobjects used bydeployCoinstackto configure Kubernetes StatefulSets.
Deploying the Coinstack
return deployCoinstack({
appName,
coinServiceArgs,
coinstack,
coinstackType: 'node',
config,
kubeconfig,
namespace,
sampleEnv,
})
Calls the core deployment orchestration function with all necessary parameters:
appName: Identifier for the deployment.coinServiceArgs: Prepared service arguments containing ports, env variables, ConfigMaps, probes.coinstack: Blockchain coinstack type ('bitcoin').coinstackType: Specifies deployment type; here, 'node' indicating a node-based coinstack.config: Deployment configuration object.kubeconfig: Kubernetes cluster access credentials.namespace: Kubernetes namespace for resource creation.sampleEnv: Raw contents of the environment file, used to create Kubernetes Secrets.
Usage Example
This file is executed automatically by Pulumi CLI as the deployment entry point. To deploy the Bitcoin coinstack:
pulumi up --cwd node/coinstacks/bitcoin/pulumi
Pulumi will run this async function, which:
Reads the environment and configuration.
Prepares service deployment specs for daemon and indexer.
Calls
deployCoinstackto create all Kubernetes resources (Secrets, StatefulSets, Services, ConfigMaps, Ingress, etc.) for the Bitcoin node infrastructure.
Important Implementation Details
Service Configuration Mapping: The mapping of services from
config.statefulService.servicestoCoinServiceArgsallows dynamic configuration based on external YAML/JSON configs, promoting flexibility.Error Handling: Throws an error if an unsupported service name is encountered, preventing invalid deployments early.
Synchronous File Reading: Reads environment and indexer config files synchronously during deployment initialization for simplicity and reliability.
Integration with Pulumi Config: Uses
getConfig()to abstract cluster access and deployment settings, supporting multi-cluster and multi-environment workflows.
Interaction with Other System Components
deployCoinstackFunction: This file callsdeployCoinstackfrom the Pulumi coinstack core, which manages the complex Kubernetes resource creation and orchestration for the coinstack.Configuration Files: Reads local files (
sample.env,../indexer/config.json) which are injected into the deployment as ConfigMaps and Secrets.Blockbook Constants: Uses shared default constants for the Blockbook indexer service to maintain consistency between coinstacks.
Pulumi Stack and Kubernetes Cluster: This file runs within a Pulumi stack context and interacts with the target Kubernetes cluster via the kubeconfig.
Stateful Services: The services defined here correspond to StatefulSets deployed to Kubernetes, representing the persistent blockchain node components.
Visual Diagram: File Structure and Workflow
flowchart TD
A[Start] --> B[Read sample.env file]
B --> C[Retrieve Kubernetes config via getConfig()]
C --> D[Map config.statefulService.services to coinServiceArgs]
D -->|For each service| E{Service name}
E -->|daemon| F[Add ports, env, readinessProbe]
E -->|indexer| G[Merge defaultBlockbookServiceArgs + configMapData]
E -->|other| H[Throw Error: unsupported service]
F & G --> I[Collect CoinServiceArgs array]
I --> J[Call deployCoinstack with deployment parameters]
J --> K[Deploy Kubernetes resources for coinstack]
K --> L[End]
Summary
The **index.ts** file is the Bitcoin coinstack Pulumi deployment entry point. It prepares blockchain node service configurations and invokes the deployment orchestration function to deploy the daemon and indexer as Kubernetes StatefulSets with appropriate environment variables, ports, and health checks. This file acts as the critical bridge between blockchain-specific deployment parameters and the generic Pulumi-based deployment automation framework.