Skip to content

Commit

Permalink
refactor: add buildDependencies key to plugin module action params
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Oct 8, 2018
1 parent bb59304 commit b24c6a9
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 28 deletions.
23 changes: 17 additions & 6 deletions garden-service/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Bluebird = require("bluebird")
import chalk from "chalk"
import { Garden } from "./garden"
import { PrimitiveMap } from "./config/common"
import { Module } from "./types/module"
import { Module, ModuleMap } from "./types/module"
import { ModuleActions, ServiceActions, PluginActions } from "./types/plugin/plugin"
import {
BuildResult,
Expand Down Expand Up @@ -90,10 +90,10 @@ export interface DeployServicesParams {
type ActionHelperParams<T extends PluginActionParamsBase> =
Omit<T, keyof PluginActionContextParams> & { pluginName?: string }
type ModuleActionHelperParams<T extends PluginModuleActionParamsBase> =
Omit<T, keyof PluginActionContextParams> & { pluginName?: string }
Omit<T, "buildDependencies" | keyof PluginActionContextParams> & { pluginName?: string }
// additionally make runtimeContext param optional
type ServiceActionHelperParams<T extends PluginServiceActionParamsBase> =
Omit<T, "module" | "runtimeContext" | keyof PluginActionContextParams>
Omit<T, "module" | "buildDependencies" | "runtimeContext" | keyof PluginActionContextParams>
& { runtimeContext?: RuntimeContext, pluginName?: string }

type RequirePluginName<T> = T & { pluginName: string }
Expand Down Expand Up @@ -292,13 +292,18 @@ export class ActionHelper implements TypeGuard {
//region Helper Methods
//===========================================================================

private async getBuildDependencies(module: Module): Promise<ModuleMap> {
const dependencies = await this.garden.resolveModuleDependencies(module.build.dependencies, [])
return keyBy(dependencies, "name")
}

async getStatus(): Promise<ContextStatus> {
const envStatus: EnvironmentStatusMap = await this.getEnvironmentStatus({})
const services = keyBy(await this.garden.getServices(), "name")

const serviceStatus = await Bluebird.props(mapValues(services, async (service: Service) => {
const dependencies = await this.garden.getServices(service.config.dependencies)
const runtimeContext = await prepareRuntimeContext(this.garden, service.module, dependencies)
const serviceDependencies = await this.garden.getServices(service.config.dependencies)
const runtimeContext = await prepareRuntimeContext(this.garden, service.module, serviceDependencies)
return this.getServiceStatus({ service, runtimeContext })
}))

Expand Down Expand Up @@ -372,10 +377,14 @@ export class ActionHelper implements TypeGuard {
pluginName,
defaultHandler,
})

const buildDependencies = await this.getBuildDependencies(module)

const handlerParams: any = {
...this.commonParams(handler),
...omit(<object>params, ["module"]),
...<object>params,
module: omit(module, ["_ConfigType"]),
buildDependencies,
}
// TODO: figure out why this doesn't compile without the function cast
return (<Function>handler)(handlerParams)
Expand All @@ -396,6 +405,7 @@ export class ActionHelper implements TypeGuard {
})

// TODO: figure out why this doesn't compile without the casts
const buildDependencies = await this.getBuildDependencies(module)
const deps = await this.garden.getServices(service.config.dependencies)
const runtimeContext = ((<any>params).runtimeContext || await prepareRuntimeContext(this.garden, module, deps))

Expand All @@ -404,6 +414,7 @@ export class ActionHelper implements TypeGuard {
...<object>params,
module,
runtimeContext,
buildDependencies,
}

return (<Function>handler)(handlerParams)
Expand Down
4 changes: 2 additions & 2 deletions garden-service/src/plugins/google/google-cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const gardenPlugin = (): GardenPlugin => ({
validate: parseGcfModule,

async deployService(
{ ctx, module, service, runtimeContext, logEntry }: DeployServiceParams<GcfModule>,
{ ctx, module, service, runtimeContext, logEntry, buildDependencies }: DeployServiceParams<GcfModule>,
) {
// TODO: provide env vars somehow to function
const project = getProject(service, ctx.provider)
Expand All @@ -126,7 +126,7 @@ export const gardenPlugin = (): GardenPlugin => ({
"--trigger-http",
])

return getServiceStatus({ ctx, module, service, runtimeContext, logEntry })
return getServiceStatus({ ctx, module, service, runtimeContext, logEntry, buildDependencies })
},

async getServiceOutputs({ ctx, service }: GetServiceOutputsParams<GcfModule>) {
Expand Down
6 changes: 4 additions & 2 deletions garden-service/src/plugins/kubernetes/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export async function runModule(
}

export async function runService(
{ ctx, service, interactive, runtimeContext, silent, timeout, logEntry }:
{ ctx, service, interactive, runtimeContext, silent, timeout, logEntry, buildDependencies }:
RunServiceParams<ContainerModule>,
) {
return runModule({
Expand All @@ -187,11 +187,12 @@ export async function runService(
silent,
timeout,
logEntry,
buildDependencies,
})
}

export async function testModule(
{ ctx, interactive, module, runtimeContext, silent, testConfig, logEntry }:
{ ctx, interactive, module, runtimeContext, silent, testConfig, logEntry, buildDependencies }:
TestModuleParams<ContainerModule>,
): Promise<TestResult> {
const testName = testConfig.name
Expand All @@ -208,6 +209,7 @@ export async function testModule(
silent,
timeout,
logEntry,
buildDependencies,
})

const api = new KubeApi(ctx.provider)
Expand Down
6 changes: 3 additions & 3 deletions garden-service/src/plugins/kubernetes/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@ async function getChartObjects(ctx: PluginContext, service: Service, logEntry?:
}

async function getServiceStatus(
{ ctx, service, module, logEntry }: GetServiceStatusParams<HelmModule>,
{ ctx, service, module, logEntry, buildDependencies }: GetServiceStatusParams<HelmModule>,
): Promise<ServiceStatus> {
// need to build to be able to check the status
const buildStatus = await getGenericModuleBuildStatus({ ctx, module, logEntry })
const buildStatus = await getGenericModuleBuildStatus({ ctx, module, logEntry, buildDependencies })
if (!buildStatus.ready) {
await build({ ctx, module, logEntry })
await build({ ctx, module, logEntry, buildDependencies })
}

// first check if the installed objects on the cluster match the current code
Expand Down
16 changes: 12 additions & 4 deletions garden-service/src/plugins/local/local-docker-swarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const gardenPlugin = (): GardenPlugin => ({
getServiceStatus,

async deployService(
{ ctx, module, service, runtimeContext, logEntry }: DeployServiceParams<ContainerModule>,
{ ctx, module, service, runtimeContext, logEntry, buildDependencies }: DeployServiceParams<ContainerModule>,
) {
// TODO: split this method up and test
const { versionString } = service.module.version
Expand Down Expand Up @@ -116,7 +116,14 @@ export const gardenPlugin = (): GardenPlugin => ({
}

const docker = getDocker()
const serviceStatus = await getServiceStatus({ ctx, service, module, runtimeContext, logEntry })
const serviceStatus = await getServiceStatus({
ctx,
service,
module,
runtimeContext,
logEntry,
buildDependencies,
})
let swarmServiceStatus
let serviceId

Expand Down Expand Up @@ -172,7 +179,7 @@ export const gardenPlugin = (): GardenPlugin => ({
msg: `Ready`,
})

return getServiceStatus({ ctx, module, service, runtimeContext, logEntry })
return getServiceStatus({ ctx, module, service, runtimeContext, logEntry, buildDependencies })
},

async getServiceOutputs({ ctx, service }: GetServiceOutputsParams<ContainerModule>) {
Expand All @@ -182,14 +189,15 @@ export const gardenPlugin = (): GardenPlugin => ({
},

async execInService(
{ ctx, service, command, runtimeContext, logEntry }: ExecInServiceParams<ContainerModule>,
{ ctx, service, command, runtimeContext, logEntry, buildDependencies }: ExecInServiceParams<ContainerModule>,
) {
const status = await getServiceStatus({
ctx,
service,
module: service.module,
runtimeContext,
logEntry,
buildDependencies,
})

if (!status.state || status.state !== "ready") {
Expand Down
5 changes: 3 additions & 2 deletions garden-service/src/plugins/openfaas/openfaas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
},

async deployService(params: DeployServiceParams<OpenFaasModule>): Promise<ServiceStatus> {
const { ctx, module, service, logEntry, runtimeContext } = params
const { ctx, module, service, logEntry, runtimeContext, buildDependencies } = params

// write the stack file again with environment variables
await writeStackFile(ctx, module, runtimeContext.envVars)
Expand All @@ -236,7 +236,7 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
},

async deleteService(params: DeleteServiceParams<OpenFaasModule>): Promise<ServiceStatus> {
const { ctx, logEntry, service, runtimeContext } = params
const { ctx, logEntry, service, runtimeContext, buildDependencies } = params
let status
let found = true

Expand All @@ -245,6 +245,7 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
ctx,
service,
runtimeContext,
buildDependencies,
module: service.module,
})

Expand Down
5 changes: 4 additions & 1 deletion garden-service/src/types/plugin/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Stream from "ts-stream"
import { LogEntry } from "../../logger/log-entry"
import { PluginContext, pluginContextSchema } from "../../plugin-context"
import { ModuleVersion, moduleVersionSchema } from "../../vcs/base"
import { Primitive, joiPrimitive, joiArray } from "../../config/common"
import { Primitive, joiPrimitive, joiArray, joiIdentifierMap } from "../../config/common"
import { Module, moduleSchema } from "../module"
import { RuntimeContext, Service, serviceSchema, runtimeContextSchema } from "../service"
import { EnvironmentStatus, ServiceLogEntry, environmentStatusSchema } from "./outputs"
Expand Down Expand Up @@ -39,10 +39,13 @@ const actionParamsSchema = Joi.object()

export interface PluginModuleActionParamsBase<T extends Module = Module> extends PluginActionParamsBase {
module: T
buildDependencies: { [name: string]: Module }
}
const moduleActionParamsSchema = actionParamsSchema
.keys({
module: moduleSchema,
buildDependencies: joiIdentifierMap(moduleSchema)
.description("All build dependencies of this module, keyed by name."),
})

export interface PluginServiceActionParamsBase<T extends Module = Module> extends PluginModuleActionParamsBase<T> {
Expand Down
5 changes: 4 additions & 1 deletion garden-service/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export const testPlugin: PluginFactory = (): GardenPlugin => {
build: buildGenericModule,
runModule,

async runService({ ctx, service, interactive, runtimeContext, silent, timeout }: RunServiceParams) {
async runService(
{ ctx, service, interactive, runtimeContext, silent, timeout, buildDependencies }: RunServiceParams,
) {
return runModule({
ctx,
module: service.module,
Expand All @@ -147,6 +149,7 @@ export const testPlugin: PluginFactory = (): GardenPlugin => {
runtimeContext,
silent,
timeout,
buildDependencies,
})
},

Expand Down
14 changes: 7 additions & 7 deletions garden-service/test/src/plugins/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ describe("plugins.container", () => {

td.replace(helpers, "imageExistsLocally", async () => true)

const result = await getBuildStatus({ ctx, module })
const result = await getBuildStatus({ ctx, module, buildDependencies: {} })
expect(result).to.eql({ ready: true })
})

Expand Down Expand Up @@ -514,7 +514,7 @@ describe("plugins.container", () => {

td.replace(helpers, "imageExistsLocally", async () => false)

const result = await getBuildStatus({ ctx, module })
const result = await getBuildStatus({ ctx, module, buildDependencies: {} })
expect(result).to.eql({ ready: false })
})
})
Expand Down Expand Up @@ -547,7 +547,7 @@ describe("plugins.container", () => {
td.replace(helpers, "pullImage", async () => null)
td.replace(helpers, "imageExistsLocally", async () => false)

const result = await build({ ctx, module })
const result = await build({ ctx, module, buildDependencies: {} })

expect(result).to.eql({ fetched: true })
})
Expand Down Expand Up @@ -581,7 +581,7 @@ describe("plugins.container", () => {

const dockerCli = td.replace(helpers, "dockerCli")

const result = await build({ ctx, module })
const result = await build({ ctx, module, buildDependencies: {} })

expect(result).to.eql({
fresh: true,
Expand Down Expand Up @@ -618,7 +618,7 @@ describe("plugins.container", () => {

td.replace(helpers, "hasDockerfile", async () => false)

const result = await publishModule({ ctx, module })
const result = await publishModule({ ctx, module, buildDependencies: {} })
expect(result).to.eql({ published: false })
})

Expand Down Expand Up @@ -651,7 +651,7 @@ describe("plugins.container", () => {

const dockerCli = td.replace(helpers, "dockerCli")

const result = await publishModule({ ctx, module })
const result = await publishModule({ ctx, module, buildDependencies: {} })
expect(result).to.eql({ message: "Published some/image:12345", published: true })

td.verify(dockerCli(module, "tag some/image:12345 some/image:12345"), { times: 0 })
Expand Down Expand Up @@ -687,7 +687,7 @@ describe("plugins.container", () => {

const dockerCli = td.replace(helpers, "dockerCli")

const result = await publishModule({ ctx, module })
const result = await publishModule({ ctx, module, buildDependencies: {} })
expect(result).to.eql({ message: "Published some/image:1.1", published: true })

td.verify(dockerCli(module, "tag some/image:12345 some/image:1.1"))
Expand Down

0 comments on commit b24c6a9

Please sign in to comment.