Skip to content

Commit

Permalink
fix: version is now correctly set for plugin modules (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed May 12, 2018
1 parent 66c8ff1 commit 27e9e2f
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gardenignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
examples/
test/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build/
coverage/
dist/
examples/**/app.yaml
*.js
gulpfile.js
*.map
src/**/*.js
src/**/*.map
Expand Down
7 changes: 7 additions & 0 deletions garden.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
project:
name: garden-framework
environments:
local:
providers:
container: {}
local-kubernetes: {}
40 changes: 35 additions & 5 deletions gulpfile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { spawn as _spawn, ChildProcess } from "child_process"
import { join } from "path"
import {
spawn as _spawn,
ChildProcess,
} from "child_process"
import { writeFileSync } from "fs"
import {
join,
relative,
} from "path"

const gulp = require("gulp")
const cached = require("gulp-cached")
const excludeGitIgnore = require("gulp-exclude-gitignore")
// const debug = require("gulp-debug")
// const exec = require("gulp-exec")
const pegjs = require("gulp-pegjs")
Expand Down Expand Up @@ -47,7 +55,6 @@ function spawn(cmd, args, cb) {
child.stderr.on("data", (data) => output.push(data.toString()))

child.on("exit", (code) => {
console.log(code)
if (code !== 0) {
console.log(output.join(""))
die()
Expand All @@ -65,6 +72,28 @@ function die() {
process.exit(1)
}

gulp.task("add-version-files", (cb) => {
const gardenBinPath = join(destDir, "src", "bin", "garden.js")
const proc = _spawn("node", [gardenBinPath, "scan", "--output=json"])

proc.on("error", err => cb(err))

let output = ""
proc.stdout.on("data", d => output += d)

proc.on("close", () => {
const results = JSON.parse(output)

for (const module of <any>Object.values(results.result)) {
const relPath = relative(__dirname, module.path)
const versionFilePath = join(__dirname, destDir, relPath, ".garden-version")
writeFileSync(versionFilePath, JSON.stringify(module.version))
}

cb()
})
})

gulp.task("check-licenses", (cb) =>
spawn("./bin/check-licenses", [], cb),
)
Expand All @@ -85,12 +114,13 @@ gulp.task("pegjs-watch", () =>

gulp.task("static", () => {
return gulp.src(staticFiles, { base: "./" })
.pipe(excludeGitIgnore())
.pipe(cached("static"))
.pipe(gulp.dest(destDir))
})

gulp.task("static-watch", () => {
return gulp.watch(staticFiles, gulp.parallel("static"))
return gulp.watch(staticFiles, gulp.series("static", "add-version-files"))
})

gulp.task("tsc", () =>
Expand Down Expand Up @@ -154,7 +184,7 @@ gulp.task("tslint-watch", () =>
)

gulp.task("lint", gulp.parallel("check-licenses", "tslint", "tslint-tests", "tsfmt"))
gulp.task("build", gulp.parallel("pegjs", "static", "tsc"))
gulp.task("build", gulp.series(gulp.parallel("pegjs", "static", "tsc"), "add-version-files"))
gulp.task("dist", gulp.series((done) => { setDestDir("dist"); done() }, "lint", "build"))
gulp.task("test", gulp.parallel("build", "lint", "mocha"))
gulp.task("watch",
Expand Down
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"gulp": "^4.0.0",
"gulp-cached": "^1.1.1",
"gulp-debug": "^3.2.0",
"gulp-exclude-gitignore": "^1.2.0",
"gulp-exec": "^3.0.1",
"gulp-pegjs": "^0.1.0",
"gulp-sourcemaps": "^2.6.4",
Expand Down
10 changes: 9 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import * as sywac from "sywac"
import chalk from "chalk"
import { ScanCommand } from "./commands/scan"
import { DeepPrimitiveMap } from "./types/common"
import { enumToArray, shutdown } from "./util"
import {
enumToArray,
shutdown,
sleep,
} from "./util"
import { merge, intersection, reduce } from "lodash"
import {
BooleanParameter,
Expand Down Expand Up @@ -314,6 +318,8 @@ export class GardenCli {
if (output && result !== undefined) {
const renderer = OUTPUT_RENDERERS[output]
console.log(renderer({ success: true, result }))
// Note: this circumvents an issue where the process exits before the output is fully flushed
await sleep(100)
}

return result
Expand All @@ -325,6 +331,8 @@ export class GardenCli {
if (output) {
const renderer = OUTPUT_RENDERERS[output]
console.error(renderer(result))
// Note: this circumvents an issue where the process exits before the output is fully flushed
await sleep(100)
}

return result
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { resolve } from "path"
export const MODULE_CONFIG_FILENAME = "garden.yml"
export const STATIC_DIR = resolve(__dirname, "..", "static")
export const GARDEN_DIR_NAME = ".garden"
export const GARDEN_VERSIONFILE_NAME = ".garden-version"
export const DEFAULT_NAMESPACE = "default"
export const DEFAULT_PORT_PROTOCOL = "TCP"

Expand Down
5 changes: 0 additions & 5 deletions src/plugins/kubernetes/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@ export async function createDeployment(service: ContainerService, serviceContext
const env: KubeEnvVar[] = toPairs(envVars).map(([name, value]) => ({ name, value: value + "" }))

// expose some metadata to the container
env.push({
name: "GARDEN_VERSION",
value: versionString,
})

env.push({
name: "POD_NAME",
valueFrom: { fieldRef: { fieldPath: "metadata.name" } },
Expand Down
42 changes: 41 additions & 1 deletion src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import {
existsSync,
readFileSync,
} from "fs"
import * as Joi from "joi"
import { GARDEN_VERSIONFILE_NAME } from "../constants"
import { ServiceMap } from "../garden"
import { PluginContext } from "../plugin-context"
import { DeployTask } from "../tasks/deploy"
import { TestTask } from "../tasks/test"
import { identifierRegex, joiIdentifier, joiVariables, PrimitiveMap } from "./common"
import {
identifierRegex,
joiIdentifier,
joiVariables,
PrimitiveMap,
validate,
} from "./common"
import { ConfigurationError } from "../exceptions"
import Bluebird = require("bluebird")
import {
Expand All @@ -27,6 +38,7 @@ import {
import { resolveTemplateStrings, TemplateStringContext } from "../template-string"
import { Memoize } from "typescript-memoize"
import { TreeVersion } from "../vcs/base"
import { join } from "path"

export interface BuildCopySpec {
source: string
Expand Down Expand Up @@ -62,6 +74,12 @@ export interface TestConfig {
[group: string]: TestSpec
}

const versionFileSchema = Joi.object().keys({
versionString: Joi.string().required(),
latestCommit: Joi.string().required(),
dirtyTimestamp: Joi.number().allow(null).required(),
})

export interface ModuleConfig<T extends ServiceConfig = ServiceConfig> {
allowPush: boolean
build: BuildConfig
Expand Down Expand Up @@ -109,6 +127,28 @@ export class Module<T extends ModuleConfig = ModuleConfig> {
}

async getVersion(): Promise<TreeVersion> {
const versionFilePath = join(this.path, GARDEN_VERSIONFILE_NAME)

if (existsSync(versionFilePath)) {
// this is used internally to specify version outside of source control
const versionFileContents = readFileSync(versionFilePath).toString().trim()

if (!!versionFileContents) {
try {
return validate(JSON.parse(versionFileContents), versionFileSchema)
} catch (err) {
throw new ConfigurationError(
`Unable to parse ${GARDEN_VERSIONFILE_NAME} as valid version file in module directory ${this.path}`,
{
modulePath: this.path,
versionFilePath,
versionFileContents,
},
)
}
}
}

const treeVersion = await this.ctx.vcs.getTreeVersion([this.path])

const versionChain: TreeVersion[] = await Bluebird.map(
Expand Down

0 comments on commit 27e9e2f

Please sign in to comment.