-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
159 lines (130 loc) · 6.79 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.Extensions.CommandLineUtils;
namespace Brthor.Dockerize
{
class Program
{
static int Main(string[] args)
{
var commandLineApplication =
new CommandLineApplication(throwOnUnexpectedArg: false);
var project = commandLineApplication.Option(
"-p |--project <project>",
"The path to the project to dockerize. Only required if the multiple projects exist " +
"in a directory or the project is not in the current working directory.",
CommandOptionType.SingleValue);
var configuration = commandLineApplication.Option(
"-c |--configuration <configuration>",
"The configuration in which to publish the project. " +
"Passed directly to 'dotnet publish -c <configuration>'. Defaults to Release.",
CommandOptionType.SingleValue);
var tag = commandLineApplication.Option(
"-t |--tag <tag>",
"The desired tag name of the created image. Will be directly passed to " +
"docker build -t, see docker build --help for more info. Defaults to the project name.",
CommandOptionType.SingleValue);
var baseRid = commandLineApplication.Option(
"-r |--runtime <RID>",
"The RID of the specified Base Docker image. Defaults to \"linux-x64\".",
CommandOptionType.SingleValue);
var baseImage = commandLineApplication.Option(
"-i |--image <image>",
"The base docker image used for the generated docker file. If you change this from the default, be sure to" +
"update BaseRid if appropriate. Defaults to \"microsoft/dotnet:2.0-runtime\".",
CommandOptionType.SingleValue);
var username = commandLineApplication.Option(
"-u |--user <username>",
"Generate a user with name <username> and set it to the current user inside the container. \n" +
"It is recommended to run production containers not as root for security.",
CommandOptionType.SingleValue);
commandLineApplication.HelpOption("-? | -h | --help");
commandLineApplication.OnExecute(() =>
{
var projectName = GetProjectName(Environment.CurrentDirectory, project);
var config = new DockerizeConfiguration(projectName, configuration.Value(), tag.Value(), baseRid.Value(), baseImage.Value(), username.Value());
return Run(config);
});
return commandLineApplication.Execute(args);
}
private static string GetProjectName(string currentDirectory, CommandOption project)
{
string projectFilePath;
if (project.HasValue())
{
projectFilePath = project.Value();
if (!File.Exists(projectFilePath))
{
throw new GracefulException(string.Format(
"The project file {0} does not exist.",
projectFilePath));
}
}
else
{
projectFilePath = GetMSBuildProjPath(currentDirectory);
}
return Path.GetFileNameWithoutExtension(projectFilePath);
}
static int Run(DockerizeConfiguration config){
var projectDirectory = Environment.CurrentDirectory;
var dockerizeBaseDir = Path.Combine(projectDirectory, "bin", "dockerize");
var publishOutDirectory = Path.Combine(dockerizeBaseDir, "publish");
var dockerfilePath = Path.Combine(dockerizeBaseDir, "Dockerfile");
var publish = Command.Create("dotnet", new[] {"publish",
"-o", publishOutDirectory,
"-r", config.BaseRid,
"-c", config.BuildConfiguration});
var publishResult = publish.WorkingDirectory(projectDirectory).ForwardStdErr().ForwardStdOut().Execute();
if (publishResult.ExitCode != 0)
{
return publishResult.ExitCode;
}
var publishOutputDepsJsons = Directory.EnumerateFiles(publishOutDirectory, "*.deps.json").ToList();
if (publishOutputDepsJsons.Count > 1)
{
throw new GracefulException(string.Format(
"Multiple output programs were found in {0}. Please try removing bin obj directories and retrying.",
publishOutDirectory));
}
if (!publishOutputDepsJsons.Any())
{
throw new GracefulException(string.Format(
"No output programs were found in {0}. Please examine 'dotnet publish' results and try again.",
publishOutDirectory));
}
var outputBinaryName =
Path.GetFileNameWithoutExtension(publishOutputDepsJsons.Single()).Replace(".deps", "");
var dockerfile = DockerfileTemplate.Generate(config, outputBinaryName);
File.WriteAllText(dockerfilePath, dockerfile);
var dockerBuild = Command.Create("docker", new[] {"build", "-t", config.GeneratedImageTag, "."}).WorkingDirectory(dockerizeBaseDir).ForwardStdErr()
.ForwardStdOut();
var dockerResult = dockerBuild.Execute();
return dockerResult.ExitCode;
}
// https://github.com/dotnet/cli/blob/444d75c0cd482f44af392d4fce8bfc081b25d2b4/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectFactory.cs#L71
// ReSharper disable once InconsistentNaming
private static string GetMSBuildProjPath(string projectDirectory)
{
IEnumerable<string> projectFiles = Directory
.GetFiles(projectDirectory, "*.*proj")
.Where(d => !d.EndsWith(".xproj")).ToList();
if (!projectFiles.Any())
{
throw new GracefulException(string.Format(
"No project files were found in {0}.",
projectDirectory));
}
else if (projectFiles.Count() > 1)
{
throw new GracefulException(string.Format(
"Multiple project files were found in {0}.",
projectDirectory));
}
return projectFiles.First();
}
}
}