Skip to content

Commit

Permalink
refactor(oci): namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Jan 18, 2024
1 parent 5bab464 commit b3a7ce2
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 48 deletions.
4 changes: 2 additions & 2 deletions compile/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import assert, { deepEqual, throws } from 'node:assert'
import { dirname } from 'node:path'
import { DotGit } from '@hackbg/repo'
import { Program, Deploy } from '@fadroma/agent'
import { OCIConnection, OCIImage } from '@fadroma/oci'
import * as OCI from '@fadroma/oci'
import { getCompiler, RawLocalRustCompiler, ContainerizedLocalRustCompiler } from './compile'
import { packageRoot } from './package'

Expand Down Expand Up @@ -50,7 +50,7 @@ export async function testBuildContainer () {
})
new ContainerizedLocalRustCompiler({
//@ts-ignore
dockerImage: new OCIImage({ engine: null, name: 'test' }) as any
dockerImage: new OCI.Image({ engine: null, name: 'test' }) as any
})
}

Expand Down
12 changes: 6 additions & 6 deletions compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,12 @@ const DEFAULT_ENGINE_SOCKET = '/var/run/docker.sock'
/** Runs the build script in a container. */
export class ContainerizedLocalRustCompiler extends LocalRustCompiler {
/** Used to launch build container. */
engine: OCI.OCIConnection
engine: OCI.Connection
/** Path to Docker API endpoint. */
engineSocket: string =
this.config.getString('FADROMA_DOCKER', ()=>DEFAULT_ENGINE_SOCKET)
/** Tag of the docker image for the build container. */
buildImage: OCI.OCIImage
buildImage: OCI.Image
/** Docker image to use for dockerized builds. */
buildImageTag: string =
this.config.getString('FADROMA_BUILD_IMAGE', ()=>'ghcr.io/hackbg/fadroma:master')
Expand All @@ -377,14 +377,14 @@ export class ContainerizedLocalRustCompiler extends LocalRustCompiler {
super(options as Partial<LocalRustCompiler>)
// Set up Docker API handle
if (options?.engineSocket) {
this.engine = new OCI.OCIConnection({ url: options.engineSocket })
this.engine = new OCI.Connection({ url: options.engineSocket })
} else if (options?.engine) {
this.engine = options.engine
} else {
this.engine = new OCI.OCIConnection()
this.engine = new OCI.Connection()
}
if ((options?.buildImageTag as unknown) instanceof OCI.OCIImage) {
this.buildImage = options?.buildImageTag as unknown as OCI.OCIImage
if ((options?.buildImageTag as unknown) instanceof OCI.Image) {
this.buildImage = options?.buildImageTag as unknown as OCI.Image
} else if (options?.buildImageTag) {
this.buildImage = this.engine.image(options.buildImageTag)
} else {
Expand Down
6 changes: 3 additions & 3 deletions devnet/devnet-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { packageRoot } from './package'
import type * as Devnets from './devnet'
import Container from './devnet-base'
import { Chain, Token } from '@fadroma/agent'
import { OCIContainer, OCIImage } from '@fadroma/oci'
import * as OCI from '@fadroma/oci'
import { ok, equal, deepEqual, throws, rejects } from 'node:assert'
import { getuid, getgid } from 'node:process'
import { resolve } from 'node:path'
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function testDevnetPlatform <
ok(devnet.gasToken)
ok(devnet.gasToken instanceof Token.Fungible)
ok(typeof devnet.gasToken.denom === 'string')
ok((await devnet.container.image) instanceof OCIImage)
ok((await devnet.container.image) instanceof OCI.Image)

const spawnEnv = Impl.containerEnvironment(devnet)
deepEqual(spawnEnv.DAEMON, daemon)
Expand All @@ -62,7 +62,7 @@ export async function testDevnetPlatform <

equal(await devnet.created, devnet)
equal(devnet.url.toString(), `http://${devnet.nodeHost}:${devnet.nodePort}/`)
ok(devnet.container instanceof OCIContainer)
ok(devnet.container instanceof OCI.Container)
equal(devnet.container.name, devnet.chainId)

equal(await devnet.started, devnet)
Expand Down
4 changes: 2 additions & 2 deletions devnet/devnet-base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import portManager, { waitPort } from '@hackbg/port'
import { Path, SyncFS, FileFormat } from '@hackbg/file'
import { OCIConnection, OCIImage, OCIContainer } from '@fadroma/oci'
import * as OCI from '@fadroma/oci'
import { Core, Program, Chain } from '@fadroma/agent'
const { assign, bold, colors, randomBase16, randomColor, } = Core
import type { Address, CodeId, Uint128 } from '@fadroma/agent'
Expand All @@ -27,7 +27,7 @@ export default abstract class DevnetContainer extends Chain.Backend {
/** Version of devnet platform. */
platformVersion: string
/** Container instance of devnet. */
container: OCIContainer = new OCIContainer()
container: OCI.Container = new OCI.Container()
/** The protocol of the API URL without the trailing colon. */
nodeProtocol: string = 'http'
/** The hostname of the API URL. */
Expand Down
26 changes: 13 additions & 13 deletions devnet/devnet-impl.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ok, equal, throws } from 'node:assert'
import { OCIConnection, OCIImage, OCIContainer, Mock } from '@fadroma/oci'
import { Core } from '@fadroma/agent'
import * as OCI from '@fadroma/oci'
import * as Impl from './devnet-impl'
const { Console } = Core

Expand Down Expand Up @@ -66,10 +66,10 @@ export default async () => {
platformName: undefined,
platformVersion: undefined,
genesisAccounts: undefined,
container: Object.assign(new OCIContainer({
container: Object.assign(new OCI.Container({
id: 'mock-create',
image: new OCIImage({
engine: OCIConnection.mock(),
image: new OCI.Image({
engine: OCI.Connection.mock(),
name: 'mock'
}),
}), {
Expand All @@ -96,10 +96,10 @@ export default async () => {
initScript: undefined,
stateDir: undefined,
stateFile: { save (_) {} },
container: Object.defineProperties(new OCIContainer({
container: Object.defineProperties(new OCI.Container({
id: 'mock-start',
image: new OCIImage({
engine: OCIConnection.mock(),
image: new OCI.Image({
engine: OCI.Connection.mock(),
name: 'mock'
}),
}), {
Expand Down Expand Up @@ -131,10 +131,10 @@ export default async () => {
await Impl.pauseDevnetContainer({
log: new Console('pauseDevnetContainer'),
running: undefined,
container: new OCIContainer({
container: new OCI.Container({
id: 'mock-pause',
image: Object.assign(new OCIImage({
engine: OCIConnection.mock(),
image: Object.assign(new OCI.Image({
engine: OCI.Connection.mock(),
name: 'mock'
})),
}),
Expand All @@ -144,10 +144,10 @@ export default async () => {
log: new Console('deleteDevnetContainer'),
stateDir: undefined,
paused: undefined,
container: Object.defineProperties(new OCIContainer({
container: Object.defineProperties(new OCI.Container({
id: 'mock-delete',
image: new OCIImage({
engine: OCIConnection.mock(),
image: new OCI.Image({
engine: OCI.Connection.mock(),
name: 'mock'
}),
}), {
Expand Down
6 changes: 3 additions & 3 deletions devnet/devnet-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { onExit } from 'gracy'
import portManager from '@hackbg/port'
import { Path, SyncFS, FileFormat, XDG } from '@hackbg/file'
import { Core, Chain } from '@fadroma/agent'
import { OCIImage, OCIConnection } from '@fadroma/oci'
import * as OCI from '@fadroma/oci'
import type { default as DevnetContainer } from './devnet-base'
import type { APIMode } from './devnet-base'
const { bold } = Core
Expand All @@ -26,11 +26,11 @@ export function initPort (devnet: $D<'nodePortMode'|'nodePort'>) {
export function initContainer (devnet: $D<'log'|'container'>) {
devnet.container.log.label = devnet.log.label
if (!devnet.container.image) {
devnet.container.image = new OCIImage()
devnet.container.image = new OCI.Image()
}
devnet.container.image.log.label = devnet.log.label
if (!devnet.container.image.engine) {
devnet.container.image.engine = new OCIConnection()
devnet.container.image.engine = new OCI.Connection()
}
return devnet
}
Expand Down
6 changes: 3 additions & 3 deletions devnet/devnet-okp4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import DevnetContainer from './devnet-base'
import { connect } from './devnet-impl'
import { Core, Token } from '@fadroma/agent'
import { OKP4Connection, OKP4MnemonicIdentity } from '@fadroma/cw'
import { OCIContainer, OCIImage } from '@fadroma/oci'
import * as OCI from '@fadroma/oci'
import { Path } from '@hackbg/file'

const { Error } = Core
Expand Down Expand Up @@ -55,8 +55,8 @@ export function okp4Version (v: OKP4Version): Partial<OKP4Container<typeof v>> {
nodePortMode: 'rpc' as APIMode,
platform: `okp4_${w}`,
waitString: 'indexed block',
container: new OCIContainer({
image: new OCIImage({
container: new OCI.Container({
image: new OCI.Image({
name: `ghcr.io/hackbg/fadroma-devnet-okp4-${v}:master`,
dockerfile: new Path(packageRoot, `okp4_${w}.Dockerfile`).absolute
})
Expand Down
6 changes: 3 additions & 3 deletions devnet/devnet-scrt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { APIMode } from './devnet-base'
import { connect } from './devnet-impl'
import DevnetContainer from './devnet-base'
import { Token } from '@fadroma/agent'
import { OCIContainer, OCIImage } from '@fadroma/oci'
import * as OCI from '@fadroma/oci'
import { ScrtConnection, ScrtMnemonicIdentity } from '@fadroma/scrt'
import { Core } from '@fadroma/agent'
import { Path } from '@hackbg/file'
Expand Down Expand Up @@ -101,8 +101,8 @@ function scrtVersion (v: ScrtVersion): Partial<ScrtContainer<typeof v>> {
waitString,
nodeBinary: 'secretd',
platform: `scrt_${w}`,
container: new OCIContainer({
image: new OCIImage({
container: new OCI.Container({
image: new OCI.Image({
name: `ghcr.io/hackbg/fadroma-devnet-scrt-${v}:master`,
dockerfile: new Path(packageRoot, `scrt_${w}.Dockerfile`).absolute
})
Expand Down
6 changes: 3 additions & 3 deletions devnet/devnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Path, FileFormat, SyncFS, XDG } from '@hackbg/file'
import type { CodeId, ChainId, Address, Uint128 } from '@fadroma/agent'
import { Core, Program } from '@fadroma/agent'
import CLI from '@hackbg/cmds'
import { OCIConnection, OCIContainer, OCIImage } from '@fadroma/oci'
import * as OCI from '@fadroma/oci'
import { packageName, packageVersion } from './package'
import ScrtContainer from './devnet-scrt'
import OKP4Container from './devnet-okp4'
Expand Down Expand Up @@ -64,7 +64,7 @@ export default class DevnetCLI extends CLI {
info: 'list existing devnets',
args: ''
}, async () => {
const engine = new OCIConnection()
const engine = new OCI.Connection()
const devnetsDir = new SyncFS.Directory(
XDG({ expanded: true, subdir: 'fadroma' }).data.home,
'devnets'
Expand Down Expand Up @@ -337,7 +337,7 @@ export default class DevnetCLI extends CLI {
info: 'delete broken devnets',
args: ''
}, async () => {
const engine = new OCIConnection()
const engine = new OCI.Connection()
const dataDir = XDG({ expanded: true, subdir: 'fadroma' }).data.home
const devnetsDir = new SyncFS.Directory(dataDir, 'devnets')
const devnets = devnetsDir.list()
Expand Down
4 changes: 2 additions & 2 deletions oci/oci-impl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Docker from 'dockerode'
import type { OCIContainer } from './oci'
import { Container as OCIContainer } from './oci'
import { OCIConsole as Console, OCIError, bold } from './oci-base'

export function toDockerodeOptions ({
Expand Down Expand Up @@ -40,7 +40,7 @@ export function toDockerodeOptions ({

exposed.forEach(containerPort=>config.ExposedPorts[containerPort] = {})

Object.entries(mapped).forEach(([containerPort, hostPort])=>
Object.entries(mapped).forEach(([containerPort, hostPort]: [string, string])=>
config.HostConfig.PortBindings[containerPort] = [{ HostPort: hostPort }])

Object.entries(readonly).forEach(([hostPath, containerPath])=>
Expand Down
8 changes: 4 additions & 4 deletions oci/oci.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Suite } from '@hackbg/ensuite'
import { OCIConnection, OCIImage, OCIContainer, defaultSocketPath } from './oci'
import * as OCI from './oci'
import { Core } from '@fadroma/agent'
import * as assert from 'node:assert'
import { resolve, dirname } from 'node:path'
Expand All @@ -10,9 +10,9 @@ export default new Suite([
])

export async function testContainerEngine () {
const engine = new OCIConnection()
const engine = new OCI.Connection()
const image = engine.image('hello-world')
assert.ok(image instanceof OCIImage)
assert.ok(image instanceof OCI.Image)
assert.equal(image.engine, engine)
console.log('Pull or build...')
await image.pullOrBuild()
Expand All @@ -24,7 +24,7 @@ export async function testContainerEngine () {
console.log('Build...')
await image.build()
const container = image.container(`test-hello-${Core.randomBase16()}`)
assert.ok(container instanceof OCIContainer)
assert.ok(container instanceof OCI.Container)
assert.equal(container.image, image)
assert.equal(await container.exists(), false)
console.log('Create container...')
Expand Down
12 changes: 9 additions & 3 deletions oci/oci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const defaultSocketPath = process.env.DOCKER_HOST || '/var/run/docker.soc

export const console = new Console('@fadroma/oci')

export class OCIConnection extends Chain.Connection {
class OCIConnection extends Chain.Connection {
static mock (callback?: Function) {
return new this({ api: Mock.mockDockerode(callback) })
}
Expand Down Expand Up @@ -114,7 +114,7 @@ export class OCIConnection extends Chain.Connection {
}
}

export class OCIImage extends Deploy.ContractTemplate {
class OCIImage extends Deploy.ContractTemplate {

constructor (properties: Partial<OCIImage> = {}) {
super(properties)
Expand Down Expand Up @@ -284,7 +284,7 @@ export class OCIImage extends Deploy.ContractTemplate {
}

/** Interface to a Docker container. */
export class OCIContainer extends Deploy.ContractInstance {
class OCIContainer extends Deploy.ContractInstance {

constructor (properties: Partial<OCIContainer> = {}) {
super(properties)
Expand Down Expand Up @@ -555,3 +555,9 @@ export class LineTransformStream extends Transform {
callback(null, output)
}
}

export {
OCIConnection as Connection,
OCIImage as Image,
OCIContainer as Container,
}
2 changes: 1 addition & 1 deletion toolbox

0 comments on commit b3a7ce2

Please sign in to comment.