Skip to content

Commit

Permalink
Merge pull request #823 from garden-io/custom-garden-dir-path
Browse files Browse the repository at this point in the history
Custom garden dir path
  • Loading branch information
eysi09 authored Jun 11, 2019
2 parents ccf948c + 33019b0 commit 9d78108
Show file tree
Hide file tree
Showing 35 changed files with 225 additions and 185 deletions.
2 changes: 1 addition & 1 deletion garden-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@
},
"snyk": true,
"gitHead": "b0647221a4d2ff06952bae58000b104215aed922"
}
}
2 changes: 1 addition & 1 deletion garden-service/src/bin/add-version-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function addVersionFiles() {
const path = config.path
const versionFilePath = resolve(path, ".garden-version")

const vcsHandler = new GitHandler(path)
const vcsHandler = new GitHandler(garden.gardenDirPath)
const treeVersion = await vcsHandler.getTreeVersion(path, config.include || null)

console.log(`${config.name} -> ${relative(STATIC_DIR, versionFilePath)}`)
Expand Down
14 changes: 5 additions & 9 deletions garden-service/src/build-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
emptyDir,
ensureDir,
} from "fs-extra"
import { GARDEN_DIR_NAME } from "./constants"
import { ConfigurationError } from "./exceptions"
import {
FileCopySpec,
Expand All @@ -35,15 +34,12 @@ import { LogEntry } from "./logger/log-entry"

// Lazily construct a directory of modules inside which all build steps are performed.

const buildDirRelPath = join(GARDEN_DIR_NAME, "build")
const buildMetadataDirRelPath = join(GARDEN_DIR_NAME, "build-metadata")

export class BuildDir {
constructor(private projectRoot: string, public buildDirPath: string, public buildMetadataDirPath) { }
constructor(private projectRoot: string, public buildDirPath: string, public buildMetadataDirPath: string) { }

static async factory(projectRoot: string) {
const buildDirPath = join(projectRoot, buildDirRelPath)
const buildMetadataDirPath = join(projectRoot, buildMetadataDirRelPath)
static async factory(projectRoot: string, gardenDirPath: string) {
const buildDirPath = join(gardenDirPath, "build")
const buildMetadataDirPath = join(gardenDirPath, "build-metadata")
await ensureDir(buildDirPath)
await ensureDir(buildMetadataDirPath)
return new BuildDir(projectRoot, buildDirPath, buildMetadataDirPath)
Expand Down Expand Up @@ -131,7 +127,7 @@ export class BuildDir {
destinationPath = stripWildcard(destinationPath)

// --exclude is required for modules where the module and project are in the same directory
const syncOpts = ["-rptgo", `--exclude=${GARDEN_DIR_NAME}`]
const syncOpts = ["-rptgo", `--exclude=${this.buildDirPath}`]
if (withDelete) {
syncOpts.push("--delete")
}
Expand Down
39 changes: 19 additions & 20 deletions garden-service/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import * as sywac from "sywac"
import { intersection, merge } from "lodash"
import { resolve } from "path"
import { resolve, join } from "path"
import { safeDump } from "js-yaml"
import { coreCommands } from "../commands/commands"
import { DeepPrimitiveMap } from "../config/common"
Expand All @@ -30,7 +30,7 @@ import { LogLevel } from "../logger/log-node"
import { BasicTerminalWriter } from "../logger/writers/basic-terminal-writer"
import { FancyTerminalWriter } from "../logger/writers/fancy-terminal-writer"
import { JsonTerminalWriter } from "../logger/writers/json-terminal-writer"
import { FileWriter } from "../logger/writers/file-writer"
import { FileWriter, FileWriterConfig } from "../logger/writers/file-writer"
import { Writer } from "../logger/writers/base"

import {
Expand All @@ -48,7 +48,12 @@ import {
parseLogLevel,
} from "./helpers"
import { defaultEnvironments, ProjectConfig } from "../config/project"
import { ERROR_LOG_FILENAME, DEFAULT_API_VERSION } from "../constants"
import {
ERROR_LOG_FILENAME,
DEFAULT_API_VERSION,
DEFAULT_GARDEN_DIR_NAME,
LOGS_DIR_NAME,
} from "../constants"
import stringify = require("json-stringify-safe")
import { generateBasicDebugInfoReport } from "../commands/get/get-debug-info"

Expand All @@ -67,12 +72,6 @@ const WRITER_CLASSES = {
json: JsonTerminalWriter,
}

const FILE_WRITER_CONFIGS = [
{ filename: "development.log" },
{ filename: ERROR_LOG_FILENAME, level: LogLevel.error },
{ filename: ERROR_LOG_FILENAME, level: LogLevel.error, path: ".", truncatePrevious: true },
]

const GLOBAL_OPTIONS_GROUP_NAME = "Global options"
const DEFAULT_CLI_LOGGER_TYPE = "fancy"

Expand Down Expand Up @@ -190,18 +189,17 @@ export class GardenCli {
globalOptions.forEach(([key, opt]) => this.addGlobalOption(key, opt))
}

async initFileWriters(logger: Logger, projectRoot: string) {
async initFileWriters(logger: Logger, root: string, gardenDirPath: string) {
if (this.fileWritersInitialized) {
return
}
for (const config of FILE_WRITER_CONFIGS) {
logger.writers.push(
await FileWriter.factory({
level: logger.level,
root: projectRoot,
...config,
}),
)
const logConfigs: FileWriterConfig[] = [
{ logFilePath: join(root, ERROR_LOG_FILENAME), truncatePrevious: true, level: LogLevel.error },
{ logFilePath: join(gardenDirPath, LOGS_DIR_NAME, ERROR_LOG_FILENAME), level: LogLevel.error },
{ logFilePath: join(gardenDirPath, LOGS_DIR_NAME, "development.log"), level: logger.level },
]
for (const config of logConfigs) {
logger.writers.push(await FileWriter.factory(config))
}
this.fileWritersInitialized = true
}
Expand Down Expand Up @@ -283,7 +281,7 @@ export class GardenCli {

// Register log file writers. We need to do this after the Garden class is initialised because
// the file writers depend on the project root.
await this.initFileWriters(logger, garden.projectRoot)
await this.initFileWriters(logger, garden.projectRoot, garden.gardenDirPath)

// TODO: enforce that commands always output DeepPrimitiveMap
result = await command.action({
Expand All @@ -300,7 +298,8 @@ export class GardenCli {
// Generate a basic report in case Garden.factory(...) fails and command is "get debug-info".
// Other exceptions are handled within the implementation of "get debug-info".
if (command.name === "debug-info") {
await generateBasicDebugInfoReport(root, log)
// Use default Garden dir name as fallback since Garden class hasn't been initialised
await generateBasicDebugInfoReport(root, join(root, DEFAULT_GARDEN_DIR_NAME), log)
return
}
throw err
Expand Down
28 changes: 14 additions & 14 deletions garden-service/src/commands/get/get-debug-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { getModulesPathsFromPath } from "../../util/fs"
import {
CONFIG_FILENAME,
ERROR_LOG_FILENAME,
GARDEN_DIR_NAME,
} from "../../constants"
import dedent = require("dedent")
import { Garden } from "../../garden"
Expand All @@ -43,9 +42,10 @@ export const PROVIDER_INFO_FILENAME_NO_EXT = "info"
*
* @export
* @param {string} root Project root path
* @param {string} gardenDirPath Path to the Garden cache directory
* @param {LogEntry} log Logger
*/
export async function collectBasicDebugInfo(root: string, log: LogEntry) {
export async function collectBasicDebugInfo(root: string, gardenDirPath: string, log: LogEntry) {
// Find project definition
const config = await findProjectConfig(root, true)
if (!config) {
Expand All @@ -56,7 +56,7 @@ export async function collectBasicDebugInfo(root: string, log: LogEntry) {
}

// Create temporary folder inside .garden/ at root of project
const tempPath = join(root, GARDEN_DIR_NAME, TEMP_DEBUG_ROOT)
const tempPath = join(gardenDirPath, TEMP_DEBUG_ROOT)
await remove(tempPath)
await ensureDir(tempPath)

Expand All @@ -68,7 +68,7 @@ export async function collectBasicDebugInfo(root: string, log: LogEntry) {
}

// Find all services paths
const paths = await getModulesPathsFromPath(root)
const paths = await getModulesPathsFromPath(root, gardenDirPath)

// Copy all the service configuration files
for (const servicePath of paths) {
Expand All @@ -88,11 +88,11 @@ export async function collectBasicDebugInfo(root: string, log: LogEntry) {
* Saves all the informations as json in a temporary folder.
*
* @export
* @param {string} root Project root path
* @param {string} gardenDirPath Path to the Garden cache directory
* @param {LogEntry} log Logger
*/
export async function collectSystemDiagnostic(root: string, log: LogEntry) {
const tempPath = join(root, GARDEN_DIR_NAME, TEMP_DEBUG_ROOT)
export async function collectSystemDiagnostic(gardenDirPath: string, log: LogEntry) {
const tempPath = join(gardenDirPath, TEMP_DEBUG_ROOT)
await ensureDir(tempPath)

let dockerVersion = ""
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function collectSystemDiagnostic(root: string, log: LogEntry) {
* @param {string} format The extension format dictating the extension of the report
*/
export async function collectProviderDebugInfo(garden: Garden, log: LogEntry, format: string) {
const tempPath = join(garden.projectRoot, GARDEN_DIR_NAME, TEMP_DEBUG_ROOT)
const tempPath = join(garden.gardenDirPath, TEMP_DEBUG_ROOT)
await ensureDir(tempPath)
// Collect debug info from providers
const actions = await garden.getActionHelper()
Expand All @@ -149,17 +149,17 @@ export async function collectProviderDebugInfo(garden: Garden, log: LogEntry, fo
* @param {string} root
* @param {LogEntry} log
*/
export async function generateBasicDebugInfoReport(root: string, log: LogEntry) {
const tempPath = join(root, GARDEN_DIR_NAME, TEMP_DEBUG_ROOT)
export async function generateBasicDebugInfoReport(root: string, gardenDirPath: string, log: LogEntry) {
const tempPath = join(gardenDirPath, TEMP_DEBUG_ROOT)
const entry = log.info({ msg: "Collecting basic debug info", status: "active" })
// Collect project info
const projectEntry = entry.info({ section: "Project", msg: "collecting info", status: "active" })
await collectBasicDebugInfo(root, log)
await collectBasicDebugInfo(root, gardenDirPath, log)
projectEntry.setSuccess({ msg: chalk.green(`Done (took ${projectEntry.getDuration(1)} sec)`), append: true })

// Run system diagnostic
const systemEntry = entry.info({ section: "System", msg: "collecting info", status: "active" })
await collectSystemDiagnostic(root, log)
await collectSystemDiagnostic(gardenDirPath, log)
systemEntry.setSuccess({ msg: chalk.green(`Done (took ${systemEntry.getDuration(1)} sec)`), append: true })

// Zip report folder
Expand Down Expand Up @@ -227,13 +227,13 @@ export class GetDebugInfoCommand extends Command<Args, Opts> {
options = debugInfoOptions

async action({ garden, log, opts }: CommandParams<Args, Opts>) {
const tempPath = join(garden.projectRoot, GARDEN_DIR_NAME, TEMP_DEBUG_ROOT)
const tempPath = join(garden.gardenDirPath, TEMP_DEBUG_ROOT)

const entry = log.info({ msg: "Collecting debug info", status: "active" })

// Collect project info
const projectEntry = entry.info({ section: "Project", msg: "collecting info", status: "active" })
await collectBasicDebugInfo(garden.projectRoot, log)
await collectBasicDebugInfo(garden.projectRoot, garden.gardenDirPath, log)
projectEntry.setSuccess({ msg: chalk.green(`Done (took ${projectEntry.getDuration(1)} sec)`), append: true })

// Run system diagnostic
Expand Down
10 changes: 5 additions & 5 deletions garden-service/src/commands/update-remote/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ import { getChildDirNames } from "../../util/fs"
import {
ExternalSourceType,
getRemoteSourcesDirname,
getRemoteSourcePath,
getRemoteSourceRelPath,
} from "../../util/ext-source-util"
import { SourceConfig } from "../../config/project"

export async function pruneRemoteSources({ projectRoot, sources, type }: {
projectRoot: string,
export async function pruneRemoteSources({ gardenDirPath, sources, type }: {
gardenDirPath: string,
sources: SourceConfig[],
type: ExternalSourceType,
}) {
const remoteSourcesPath = join(projectRoot, getRemoteSourcesDirname(type))
const remoteSourcesPath = join(gardenDirPath, getRemoteSourcesDirname(type))

if (!(await pathExists(remoteSourcesPath))) {
return
}

const sourceNames = sources
.map(({ name, repositoryUrl: url }) => getRemoteSourcePath({ name, url, sourceType: type }))
.map(({ name, repositoryUrl: url }) => getRemoteSourceRelPath({ name, url, sourceType: type }))
.map(srcPath => basename(srcPath))

const currentRemoteSources = await getChildDirNames(remoteSourcesPath)
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/commands/update-remote/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class UpdateRemoteModulesCommand extends Command<Args> {
await garden.vcs.updateRemoteSource({ name, url: repositoryUrl, sourceType: "module", log })
}

await pruneRemoteSources({ projectRoot: garden.projectRoot, type: "module", sources: moduleSources })
await pruneRemoteSources({ gardenDirPath: garden.gardenDirPath, type: "module", sources: moduleSources })

return { result: moduleSources }
}
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/commands/update-remote/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class UpdateRemoteSourcesCommand extends Command<Args> {
await garden.vcs.updateRemoteSource({ name, url: repositoryUrl, sourceType: "project", log })
}

await pruneRemoteSources({ projectRoot: garden.projectRoot, type: "project", sources: projectSources })
await pruneRemoteSources({ gardenDirPath: garden.gardenDirPath, type: "project", sources: projectSources })

return { result: projectSources }
}
Expand Down
14 changes: 7 additions & 7 deletions garden-service/src/config-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import * as Joi from "joi"
import * as yaml from "js-yaml"
import { resolve } from "path"
import { join } from "path"
import { ensureFile, readFile } from "fs-extra"
import { get, isPlainObject, unset } from "lodash"

import { Primitive, validate, joiArray, joiUserIdentifier } from "./config/common"
import { LocalConfigError } from "./exceptions"
import { dumpYaml } from "./util/util"
import { GARDEN_DIR_NAME, LOCAL_CONFIG_FILENAME } from "./constants"
import { LOCAL_CONFIG_FILENAME } from "./constants"

export type ConfigValue = Primitive | Primitive[] | Object[]

Expand All @@ -25,12 +25,12 @@ export abstract class ConfigStore<T extends object = any> {
private config: null | T
protected configPath: string

constructor(projectPath: string) {
this.configPath = this.getConfigPath(projectPath)
constructor(gardenDirPath: string) {
this.configPath = this.getConfigPath(gardenDirPath)
this.config = null
}

abstract getConfigPath(projectPath: string): string
abstract getConfigPath(gardenDirPath: string): string
abstract validate(config): T

/**
Expand Down Expand Up @@ -208,8 +208,8 @@ export const configStoreSchema = Joi.object()

export class LocalConfigStore extends ConfigStore<LocalConfig> {

getConfigPath(projectPath): string {
return resolve(projectPath, GARDEN_DIR_NAME, LOCAL_CONFIG_FILENAME)
getConfigPath(gardenDirPath: string): string {
return join(gardenDirPath, LOCAL_CONFIG_FILENAME)
}

validate(config): LocalConfig {
Expand Down
8 changes: 4 additions & 4 deletions garden-service/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export const STATIC_DIR = join(GARDEN_SERVICE_ROOT, "static")
// We copy the built dashboard to the garden-service static directory (with gulp in development, otherwise in CI).
// TODO: Use env vars to detect if Garden is running in dev mode and serve straight from the dashboard directory.
export const DASHBOARD_STATIC_DIR = join(STATIC_DIR, "dashboard")
export const GARDEN_DIR_NAME = ".garden"
export const LOGS_DIR = join(GARDEN_DIR_NAME, "logs")
export const DEFAULT_GARDEN_DIR_NAME = ".garden"
export const LOGS_DIR_NAME = "logs"
export const ERROR_LOG_FILENAME = "error.log"
export const PROJECT_SOURCES_DIR_NAME = join(GARDEN_DIR_NAME, "sources", "project")
export const MODULE_SOURCES_DIR_NAME = join(GARDEN_DIR_NAME, "sources", "module")
export const PROJECT_SOURCES_DIR_NAME = join("sources", "project")
export const MODULE_SOURCES_DIR_NAME = join("sources", "module")
export const GARDEN_BUILD_VERSION_FILENAME = "garden-build-version"
export const GARDEN_VERSIONFILE_NAME = ".garden-version"
export const DEFAULT_PORT_PROTOCOL = "TCP"
Expand Down
Loading

0 comments on commit 9d78108

Please sign in to comment.