Skip to content

Commit

Permalink
Write operation id to build manifest file instead of a separate file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kichalla authored May 31, 2019
1 parent bddf7a9 commit 9c7c2fd
Show file tree
Hide file tree
Showing 31 changed files with 170 additions and 138 deletions.
2 changes: 1 addition & 1 deletion build/build-constants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
file-name-prefix: __
- name: file-paths
constants:
build-id-file-name: .oryx_build_id
build-manifest-file-name: oryx-manifest.toml
outputs:
- type: csharp
directory: src/Common
Expand Down
5 changes: 5 additions & 0 deletions src/BuildScriptGenerator/BuildScriptGeneratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace Microsoft.Oryx.BuildScriptGenerator
/// </summary>
public partial class BuildScriptGeneratorContext
{
/// <summary>
/// Gets or sets the information which is used to correlate log messages.
/// </summary>
public string OperationId { get; set; }

public ISourceRepo SourceRepo { get; set; }

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/BuildScriptGenerator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public static class Constants
{
public const string OryxEnvironmentSettingNamePrefix = "ORYX_";
public const string BuildEnvironmentFileName = "build.env";
public const string ManifestFileName = "oryx-manifest.toml";
public const string AppInsightsKey = "APPINSIGHTS_INSTRUMENTATIONKEY";
public const string ZipAllOutputBuildPropertyKey = "zip_all_output";
public const string ZipAllOutputBuildPropertyKeyDocumentation =
Expand Down
2 changes: 1 addition & 1 deletion src/BuildScriptGenerator/DefaultBuildScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private string BuildScriptFromSnippets(
PostBuildCommand = postBuildCommand,
DirectoriesToExcludeFromCopyToIntermediateDir = directoriesToExcludeFromCopyToIntermediateDir,
DirectoriesToExcludeFromCopyToBuildOutputDir = directoriesToExcludeFromCopyToBuildOutputDir,
ManifestFileName = Constants.ManifestFileName,
ManifestFileName = FilePaths.BuildManifestFileName,
BuildProperties = buildProperties
};

Expand Down
34 changes: 12 additions & 22 deletions src/BuildScriptGenerator/DotNetCore/DotnetCorePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Oryx.Common;

namespace Microsoft.Oryx.BuildScriptGenerator.DotNetCore
{
Expand Down Expand Up @@ -52,6 +53,8 @@ public LanguageDetectorResult Detect(ISourceRepo sourceRepo)
public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
{
var buildProperties = new Dictionary<string, string>();
buildProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;

(string projectFile, string publishDir) = GetProjectFileAndPublishDir(context.SourceRepo);
if (string.IsNullOrEmpty(projectFile) || string.IsNullOrEmpty(publishDir))
{
Expand All @@ -77,7 +80,7 @@ public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorCon
context),
PreBuildCommand = preBuildCommand,
PostBuildCommand = postBuildCommand,
ManifestFileName = Constants.ManifestFileName,
ManifestFileName = FilePaths.BuildManifestFileName,
ZipAllOutput = zipAllOutput,
Configuration = GetBuildConfiguration()
};
Expand Down Expand Up @@ -148,6 +151,14 @@ public IEnumerable<string> GetDirectoriesToExcludeFromCopyToIntermediateDir(
return dirs;
}

private static bool ShouldZipAllOutput(BuildScriptGeneratorContext context)
{
return BuildPropertiesHelper.IsTrue(
Constants.ZipAllOutputBuildPropertyKey,
context,
valueIsRequired: false);
}

private string GetBuildConfiguration()
{
var configuration = _options.MSBuildConfiguration;
Expand All @@ -159,14 +170,6 @@ private string GetBuildConfiguration()
return configuration;
}

private static bool ShouldZipAllOutput(BuildScriptGeneratorContext context)
{
return BuildPropertiesHelper.IsTrue(
Constants.ZipAllOutputBuildPropertyKey,
context,
valueIsRequired: false);
}

private (string projFile, string publishDir) GetProjectFileAndPublishDir(ISourceRepo repo)
{
var projectFile = _aspNetCoreWebAppProjectFileProvider.GetRelativePathToProjectFile(repo);
Expand All @@ -178,18 +181,5 @@ private static bool ShouldZipAllOutput(BuildScriptGeneratorContext context)
var publishDir = Path.Combine(repo.RootPath, DotnetCoreConstants.OryxOutputPublishDirectory);
return (projectFile, publishDir);
}

private string GetCommandOrScript(string commandOrScript)
{
if (!string.IsNullOrEmpty(commandOrScript))
{
if (File.Exists(commandOrScript))
{
return $"\"{commandOrScript}\"";
}
}

return commandOrScript;
}
}
}
4 changes: 3 additions & 1 deletion src/BuildScriptGenerator/ManifestFilePropertyKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Microsoft.Oryx.BuildScriptGenerator
{
public static class ManifestFilePropertyKeys
{
public const string ZipAllOutput = "zipAllOutput";
public const string ZipAllOutput = nameof(ZipAllOutput);

public const string OperationId = nameof(OperationId);
}
}
2 changes: 2 additions & 0 deletions src/BuildScriptGenerator/Node/NodePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public LanguageDetectorResult Detect(ISourceRepo sourceRepo)
public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
{
var buildProperties = new Dictionary<string, string>();
buildProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;

var packageJson = GetPackageJsonObject(context.SourceRepo, _logger);
string runBuildCommand = null;
string runBuildAzureCommand = null;
Expand Down
5 changes: 4 additions & 1 deletion src/BuildScriptGenerator/Php/PhpPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public LanguageDetectorResult Detect(ISourceRepo sourceRepo)

public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext ctx)
{
var buildProperties = new Dictionary<string, string>();
buildProperties[ManifestFilePropertyKeys.OperationId] = ctx.OperationId;

_logger.LogDebug("Selected PHP version: {phpVer}", ctx.PhpVersion);
bool composerFileExists = false;

Expand Down Expand Up @@ -70,7 +73,7 @@ public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorCon

var props = new PhpBashBuildSnippetProperties { ComposerFileExists = composerFileExists };
string snippet = TemplateHelpers.Render(TemplateHelpers.TemplateResource.PhpBuildSnippet, props, _logger);
return new BuildScriptSnippet { BashBuildScriptSnippet = snippet };
return new BuildScriptSnippet { BashBuildScriptSnippet = snippet, BuildProperties = buildProperties };
}

public bool IsEnabled(BuildScriptGeneratorContext ctx)
Expand Down
5 changes: 4 additions & 1 deletion src/BuildScriptGenerator/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.Oryx.BuildScriptGenerator.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c96528184668742ee5bc6a1f8b4447fdc42d482d7c339989f050d7858ea7277df2f86c7ad0b8afa733987baaf6adb2858b170995d03ba3ad612bbd0b5a389e1f6392cc5ad158f726d018d9aa75c362e0350da1c6f92b75ca449591be97fc08e68c576d95aff6cfc9e58a4653e4b6e87a1d83e3060f98a6214eacfd62a45cc8bf")]
[assembly: InternalsVisibleTo("BuildScriptGeneratorCli.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c96528184668742ee5bc6a1f8b4447fdc42d482d7c339989f050d7858ea7277df2f86c7ad0b8afa733987baaf6adb2858b170995d03ba3ad612bbd0b5a389e1f6392cc5ad158f726d018d9aa75c362e0350da1c6f92b75ca449591be97fc08e68c576d95aff6cfc9e58a4653e4b6e87a1d83e3060f98a6214eacfd62a45cc8bf")]
[assembly: InternalsVisibleTo("BuildScriptGeneratorCli.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c96528184668742ee5bc6a1f8b4447fdc42d482d7c339989f050d7858ea7277df2f86c7ad0b8afa733987baaf6adb2858b170995d03ba3ad612bbd0b5a389e1f6392cc5ad158f726d018d9aa75c362e0350da1c6f92b75ca449591be97fc08e68c576d95aff6cfc9e58a4653e4b6e87a1d83e3060f98a6214eacfd62a45cc8bf")]
[assembly: InternalsVisibleTo("Oryx.Integration.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c96528184668742ee5bc6a1f8b4447fdc42d482d7c339989f050d7858ea7277df2f86c7ad0b8afa733987baaf6adb2858b170995d03ba3ad612bbd0b5a389e1f6392cc5ad158f726d018d9aa75c362e0350da1c6f92b75ca449591be97fc08e68c576d95aff6cfc9e58a4653e4b6e87a1d83e3060f98a6214eacfd62a45cc8bf")]
[assembly: InternalsVisibleTo("Oryx.BuildImage.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c96528184668742ee5bc6a1f8b4447fdc42d482d7c339989f050d7858ea7277df2f86c7ad0b8afa733987baaf6adb2858b170995d03ba3ad612bbd0b5a389e1f6392cc5ad158f726d018d9aa75c362e0350da1c6f92b75ca449591be97fc08e68c576d95aff6cfc9e58a4653e4b6e87a1d83e3060f98a6214eacfd62a45cc8bf")]
[assembly: InternalsVisibleTo("Oryx.RuntimeImage.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c96528184668742ee5bc6a1f8b4447fdc42d482d7c339989f050d7858ea7277df2f86c7ad0b8afa733987baaf6adb2858b170995d03ba3ad612bbd0b5a389e1f6392cc5ad158f726d018d9aa75c362e0350da1c6f92b75ca449591be97fc08e68c576d95aff6cfc9e58a4653e4b6e87a1d83e3060f98a6214eacfd62a45cc8bf")]
8 changes: 4 additions & 4 deletions src/BuildScriptGenerator/Python/PythonPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public LanguageDetectorResult Detect(ISourceRepo sourceRepo)
public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
{
var buildProperties = new Dictionary<string, string>();
buildProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;

var packageDir = GetPackageDirectory(context);
var virtualEnvName = GetVirtualEnvironmentName(context);
if (string.IsNullOrEmpty(packageDir))
Expand Down Expand Up @@ -105,13 +107,11 @@ public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorCon

bool enableCollectStatic = IsCollectStaticEnabled();

string compressVirtualEnvCommand = null;
string compressedVirtualEnvFileName = null;
GetVirtualEnvPackOptions(
context,
virtualEnvName,
out compressVirtualEnvCommand,
out compressedVirtualEnvFileName);
out var compressVirtualEnvCommand,
out var compressedVirtualEnvFileName);

if (!string.IsNullOrWhiteSpace(compressedVirtualEnvFileName))
{
Expand Down
11 changes: 8 additions & 3 deletions src/BuildScriptGeneratorCli/BuildScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,30 @@ internal class BuildScriptGenerator
private readonly IConsole _console;
private readonly List<ICheckerMessage> _checkerMessageSink;
private readonly ILogger<BuildScriptGenerator> _logger;
private readonly string _operationId;

public BuildScriptGenerator(
IServiceProvider serviceProvider,
IConsole console,
List<ICheckerMessage> checkerMessageSink)
List<ICheckerMessage> checkerMessageSink,
string operationId)
{
_console = console;
_serviceProvider = serviceProvider;
_checkerMessageSink = checkerMessageSink;
_logger = _serviceProvider.GetRequiredService<ILogger<BuildScriptGenerator>>();
_operationId = operationId;
}

public static BuildScriptGeneratorContext CreateContext(
BuildScriptGeneratorOptions options,
CliEnvironmentSettings envSettings,
ISourceRepo sourceRepo)
ISourceRepo sourceRepo,
string operationId)
{
return new BuildScriptGeneratorContext
{
OperationId = operationId,
SourceRepo = sourceRepo,
Language = options.Language,
LanguageVersion = options.LanguageVersion,
Expand All @@ -63,7 +68,7 @@ public bool TryGenerateScript(out string generatedScript)
var sourceRepoProvider = _serviceProvider.GetRequiredService<ISourceRepoProvider>();
var environment = _serviceProvider.GetRequiredService<CliEnvironmentSettings>();
var sourceRepo = sourceRepoProvider.GetSourceRepo();
var scriptGenCtx = CreateContext(options, environment, sourceRepo);
var scriptGenCtx = CreateContext(options, environment, sourceRepo, _operationId);

scriptGen.GenerateBashScript(scriptGenCtx, out generatedScript, _checkerMessageSink);
return true;
Expand Down
23 changes: 4 additions & 19 deletions src/BuildScriptGeneratorCli/Commands/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using McMaster.Extensions.CommandLineUtils;
Expand Down Expand Up @@ -96,7 +95,7 @@ internal int Execute(
DataReceivedEventHandler stdErrHandler)
{
var logger = serviceProvider.GetRequiredService<ILogger<BuildCommand>>();
var buildOpId = logger.StartOperation(
var buildOperationId = logger.StartOperation(
BuildOperationName(serviceProvider.GetRequiredService<IEnvironment>()));

var options = serviceProvider.GetRequiredService<IOptions<BuildScriptGeneratorOptions>>().Value;
Expand All @@ -107,7 +106,7 @@ internal int Execute(

var buildInfo = new DefinitionListFormatter();
buildInfo.AddDefinition("Oryx Version", $"{Program.GetVersion()}, Commit: {Program.GetCommit()}");
buildInfo.AddDefinition("Build Operation ID", buildOpId);
buildInfo.AddDefinition("Build Operation ID", buildOperationId);

var sourceRepo = serviceProvider.GetRequiredService<ISourceRepoProvider>().GetSourceRepo();
var commitId = GetSourceRepoCommitId(
Expand All @@ -121,21 +120,6 @@ internal int Execute(

console.WriteLine(buildInfo.ToString());

// Try writing the ID to a file in the source directory
try
{
using (logger.LogTimedEvent("WriteBuildIdFile"))
using (var idFileWriter = new StreamWriter(
Path.Combine(sourceRepo.RootPath, Common.FilePaths.BuildIdFileName)))
{
idFileWriter.Write(buildOpId);
}
}
catch (Exception exc)
{
logger.LogError(exc, "Exception caught while trying to write build ID file");
}

var environmentSettingsProvider = serviceProvider.GetRequiredService<IEnvironmentSettingsProvider>();
if (!environmentSettingsProvider.TryGetAndLoadSettings(out var environmentSettings))
{
Expand All @@ -147,7 +131,8 @@ internal int Execute(
using (var stopwatch = logger.LogTimedEvent("GenerateBuildScript"))
{
var checkerMessages = new List<ICheckerMessage>();
var scriptGenerator = new BuildScriptGenerator(serviceProvider, console, checkerMessages);
var scriptGenerator = new BuildScriptGenerator(
serviceProvider, console, checkerMessages, buildOperationId);

var generated = scriptGenerator.TryGenerateScript(out scriptContent);
stopwatch.AddProperty("generateSucceeded", generated.ToString());
Expand Down
7 changes: 5 additions & 2 deletions src/BuildScriptGeneratorCli/Commands/BuildScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// --------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -40,7 +39,11 @@ internal class BuildScriptCommand : CommandBase

internal override int Execute(IServiceProvider serviceProvider, IConsole console)
{
var scriptGenerator = new BuildScriptGenerator(serviceProvider, console, checkerMessageSink: null);
var scriptGenerator = new BuildScriptGenerator(
serviceProvider,
console,
checkerMessageSink: null,
operationId: null);

if (!scriptGenerator.TryGenerateScript(out var generatedScript))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal override int Execute(IServiceProvider serviceProvider, IConsole console
var env = serviceProvider.GetRequiredService<CliEnvironmentSettings>();
var repo = serviceProvider.GetRequiredService<ISourceRepoProvider>().GetSourceRepo();

var ctx = BuildScriptGenerator.CreateContext(options, env, repo);
var ctx = BuildScriptGenerator.CreateContext(options, env, repo, operationId: null);
var compatPlats = generator.GetCompatiblePlatforms(ctx);

if (compatPlats != null && compatPlats.Any())
Expand Down
2 changes: 1 addition & 1 deletion src/Common/FilePaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Microsoft.Oryx.Common
{
public static class FilePaths
{
public const string BuildIdFileName = ".oryx_build_id";
public const string BuildManifestFileName = "oryx-manifest.toml";
}
}
21 changes: 14 additions & 7 deletions src/startupscriptgenerator/common/buildManifestHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
package common

import (
"startupscriptgenerator/common/consts"
"github.com/BurntSushi/toml"
"log"
"path/filepath"
)

type BuildManifest struct {
StartupFileName string
ZipAllOutput string
StartupFileName string
ZipAllOutput string
OperationId string
}

const ManifestFileName = "oryx-manifest.toml"
var _buildManifest = BuildManifest{}
var _readManifest = false

func DeserializeBuildManifest(manifestFile string) BuildManifest {
var manifest BuildManifest
Expand All @@ -27,11 +30,15 @@ func DeserializeBuildManifest(manifestFile string) BuildManifest {
}

func GetBuildManifest(appPath string) BuildManifest {
buildManifest := BuildManifest{}
if _readManifest {
return _buildManifest
}

tomlFile := filepath.Join(appPath, ManifestFileName)
tomlFile := filepath.Join(appPath, consts.BuildManifestFileName)
if FileExists(tomlFile) {
buildManifest = DeserializeBuildManifest(tomlFile)
_buildManifest = DeserializeBuildManifest(tomlFile)
_readManifest = true
}
return buildManifest

return _buildManifest
}
2 changes: 1 addition & 1 deletion src/startupscriptgenerator/common/consts/file_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package consts

const BuildIdFileName string = ".oryx_build_id"
const BuildManifestFileName string = "oryx-manifest.toml"
Loading

0 comments on commit 9c7c2fd

Please sign in to comment.