Skip to content

Commit

Permalink
fix(openfaas): fix issues with openfaas builds
Browse files Browse the repository at this point in the history
Closes #298
  • Loading branch information
edvald committed Oct 8, 2018
1 parent ad0e708 commit f62db2f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 28 deletions.
24 changes: 24 additions & 0 deletions garden-service/src/plugins/openfaas/faas-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { BinaryCmd } from "../../util/ext-tools"

export const faasCli = new BinaryCmd({
name: "faas-cli",
specs: {
darwin: {
url: "https://github.com/openfaas/faas-cli/releases/download/0.7.3/faas-cli-darwin",
},
linux: {
url: "https://github.com/openfaas/faas-cli/releases/download/0.7.3/faas-cli",
},
win32: {
url: "https://github.com/openfaas/faas-cli/releases/download/0.7.3/faas-cli.exe",
},
},
})
41 changes: 21 additions & 20 deletions garden-service/src/plugins/openfaas/openfaas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
Service,
} from "../../types/service"
import {
buildGenericModule,
GenericModuleSpec,
genericModuleSpecSchema,
GenericTestSpec,
Expand All @@ -45,18 +44,18 @@ import {
} from "../../types/plugin/params"
import { every, values } from "lodash"
import { dumpYaml, findByName } from "../../util/util"
import * as execa from "execa"
import { KubeApi } from "../kubernetes/api"
import { waitForObjects, checkDeploymentStatus } from "../kubernetes/status"
import { systemSymbol } from "../kubernetes/system"
import { BaseServiceSpec } from "../../config/service"
import { GardenPlugin } from "../../types/plugin/plugin"
import { Provider, providerConfigBaseSchema } from "../../config/project"
import { faasCli } from "./faas-cli"
import { CleanupEnvironmentParams } from "../../types/plugin/params"
import dedent = require("dedent")

const systemProjectPath = join(STATIC_DIR, "openfaas", "system")
export const stackFilename = "stack.yml"
export const FAAS_CLI_IMAGE_ID = "openfaas/faas-cli:0.7.3"

export interface OpenFaasModuleSpec extends GenericModuleSpec {
handler: string
Expand Down Expand Up @@ -128,7 +127,7 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug

await ofGarden.actions.prepareEnvironment({ force })

const results = await ofGarden.actions.deployServices({})
const results = await ofGarden.actions.deployServices({ force })
const failed = values(results.taskResults).filter(r => !!r.error).length

if (failed) {
Expand All @@ -140,9 +139,8 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
return {}
},

async cleanupEnvironment({ ctx }) {
async cleanupEnvironment({ ctx }: CleanupEnvironmentParams) {
const ofGarden = await getOpenFaasGarden(ctx)

return ofGarden.actions.cleanupEnvironment({})
},
},
Expand All @@ -155,17 +153,11 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
{ context: `module ${moduleConfig.name}` },
)

moduleConfig.build.command = [
"faas-cli",
"build",
"-f", stackFilename,
]

moduleConfig.build.dependencies.push({
name: "builder",
plugin: "openfaas",
copy: [{
source: "*",
source: "templates/template",
target: ".",
}],
})
Expand Down Expand Up @@ -193,13 +185,16 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug

getBuildStatus: getGenericModuleBuildStatus,

async build(params: BuildModuleParams<OpenFaasModule>) {
const { ctx, module } = params

async build({ ctx, module }: BuildModuleParams<OpenFaasModule>) {
// prepare the stack.yml file, before handing off the build to the generic handler
await writeStackFile(ctx, module, {})

return buildGenericModule(params)
const buildLog = await faasCli.stdout({
cwd: module.buildPath,
args: ["build", "-f", stackFilename],
})

return { fresh: true, buildLog }
},

// TODO: design and implement a proper test flow for openfaas functions
Expand All @@ -214,13 +209,16 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
},

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

// write the stack file again with environment variables
await writeStackFile(ctx, module, runtimeContext.envVars)

// use faas-cli to do the deployment
await execa("faas-cli", ["deploy", "-f", stackFilename], { cwd: module.buildPath })
await faasCli.stdout({
cwd: module.buildPath,
args: ["deploy", "-f", stackFilename],
})

// wait until deployment is ready
const k8sProvider = getK8sProvider(ctx)
Expand Down Expand Up @@ -251,7 +249,10 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug

found = !!status.state

await execa("faas-cli", ["remove", "-f", stackFilename], { cwd: service.module.buildPath })
await faasCli.stdout({
cwd: service.module.buildPath,
args: ["remove", "-f", stackFilename],
})

} catch (err) {
found = false
Expand Down
21 changes: 15 additions & 6 deletions garden-service/src/util/ext-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import { platform, homedir } from "os"
import { pathExists, createWriteStream, ensureDir } from "fs-extra"
import { pathExists, createWriteStream, ensureDir, chmod, remove } from "fs-extra"
import { ConfigurationError, ParameterError, GardenBaseError } from "../exceptions"
import { join } from "path"
import { join, dirname } from "path"
import { hashString } from "./util"
import Axios from "axios"
import * as execa from "execa"
Expand Down Expand Up @@ -69,6 +69,7 @@ export class BinaryCmd extends Cmd {
private targetFilename: string
private downloadPath: string
private executablePath: string
private defaultCwd: string

constructor(spec: BinaryCmdSpec) {
super()
Expand All @@ -93,6 +94,7 @@ export class BinaryCmd extends Cmd {
? this.spec.extract.executablePath
: [this.name]
this.executablePath = join(this.downloadPath, ...executableSubpath)
this.defaultCwd = dirname(this.executablePath)
}

private async download(logEntry?: LogEntry) {
Expand Down Expand Up @@ -174,17 +176,24 @@ export class BinaryCmd extends Cmd {
))
}

debug && debug.setSuccess("Done")
logEntry && logEntry.setSuccess(`Fetched ${this.name}`)
resolve()
chmod(this.executablePath, 0o755, (chmodErr) => {
if (chmodErr) {
remove(this.downloadPath, () => reject(chmodErr))
return
}

debug && debug.setSuccess("Done")
logEntry && logEntry.setSuccess(`Fetched ${this.name}`)
resolve()
})
})
})
})
}

async exec({ cwd, args, logEntry }: ExecParams) {
await this.download(logEntry)
return execa(this.executablePath, args || [], { cwd })
return execa(this.executablePath, args || [], { cwd: cwd || this.defaultCwd })
}

async stdout(params: ExecParams) {
Expand Down
12 changes: 10 additions & 2 deletions garden-service/static/openfaas/builder/garden.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
module:
description: Base image used for building openfaas modules
description: Used for pre-fetching templates, before building containers
name: builder
type: generic
build:
command: ["faas-cli", "template", "pull"]
command: [
"rm", "-rf", "templates",
"&&",
"git", "clone", "https://github.com/openfaas/templates.git",
"&&",
"cd templates",
"&&",
"git", "checkout", "85fca87",
]

0 comments on commit f62db2f

Please sign in to comment.