Skip to content

Commit

Permalink
feat(VsockFirecrackerClient): Add client to talk to FC microVMs
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Oct 16, 2019
1 parent 90f4686 commit 8c28b84
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/base/Transports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum Transport {
direct = 'direct',
stdio = 'stdio',
uds = 'uds',
vsock = 'vsock',
tcp = 'tcp',
http = 'http',
Expand All @@ -19,13 +20,49 @@ export interface StdioAddress {
cwd?: string
}

/**
* An address in the Unix Domain Socket (UDS) address family
*
* @see https://en.wikipedia.org/wiki/Unix_domain_socket
*/
export class UdsAddress {
public readonly type: Transport.uds = Transport.uds

/**
* The file system path to the socket
*/
public readonly path: string

public constructor (path: string) {
this.path = path
}
}

/**
* An address int the Linux VSOCK address family
*
* @see http://man7.org/linux/man-pages/man7/vsock.7.html
*/
export class VsockAddress {
public readonly type: Transport.vsock = Transport.vsock

/**
* The port number
*/
public readonly port: number

public constructor (port: number = 6000) {
/**
* The file system path to the socket
*
* Although VSOCK addresses do not include a path,
* Firecracker uses a UDS on the host. This allows
* for that use case.
*/
public readonly path?: string

public constructor (port: number = 6000, path?: string) {
this.port = port
this.path = path
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/vsock/VsockFirecrackerClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getLogger } from '@stencila/logga';
import net from 'net';
import { VsockAddress } from '../base/Transports';
import StreamClient from '../stream/StreamClient';

const log = getLogger('executa:vsock:client')

export default class VsockFirecrackerClient extends StreamClient {
private socket: net.Socket

public constructor(address: VsockAddress = new VsockAddress()) {
const { path, port} = address
if (path === undefined) throw (`Path is required`)

const socket = net.connect(path)
socket.on('connect', () => {
log.debug(`${path}:${port}: connecting`)
socket.write(`CONNECT ${port}\n`)
})
socket.on('error', error => {
log.error(`${path}:${port}: ${error}`)
})
socket.on('close', () => {
// If there is no server listening in the VM then the connection will be closed.
log.debug(`${path}:${port}: connection-closed`)
})

super(socket, socket)
this.socket = socket
}

public stop() {
this.socket.destroy()
}
}

0 comments on commit 8c28b84

Please sign in to comment.