Skip to content

Commit

Permalink
Byte to stream on docker api
Browse files Browse the repository at this point in the history
  • Loading branch information
ragoso committed Mar 25, 2021
1 parent 298783b commit c62494f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 39 deletions.
15 changes: 8 additions & 7 deletions Console/Console.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\GRPC\GRPC.csproj" />
<ProjectReference Include="..\Core\Core.csproj"/>
<ProjectReference Include="..\GRPC\GRPC.csproj"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="YamlDotNet" Version="9.1.4"/>
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<Target Name="SpicNSpan" AfterTargets="Clean">
<RemoveDir Directories="$(BaseIntermediateOutputPath)" />
<RemoveDir Directories="$(BaseOutputPath)" />
<Target Name="SpicNSpan" AfterTargets="Clean">
<RemoveDir Directories="$(BaseIntermediateOutputPath)"/>
<RemoveDir Directories="$(BaseOutputPath)"/>
</Target>
</Project>
119 changes: 95 additions & 24 deletions Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Mono.Options;
using Grpc.Core;
using GRPC;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using Core;
using System.Linq;

namespace Console
{
Expand All @@ -28,7 +29,7 @@ class Program
private static string file = string.Empty;
private static Actions action = Actions.Non;
private static string token = string.Empty;
private static string tag = "latest";
private static string tag = string.Empty;
private static IList<string> buildParam = new List<string>();
private static IServiceHandle _serviceHandle;
private static IImageHandle _imageHandle;
Expand Down Expand Up @@ -70,6 +71,11 @@ private static void BuildImage()
{
throw new FileNotFoundException();
}

if (string.IsNullOrEmpty(tag))
{
throw new ArgumentNullException("tag");
}

_imageHandle = new ImageClientHandle(GetImageClient(url), token);

Expand All @@ -82,49 +88,64 @@ private static void BuildImage()
}
private static void Update()
{
var service = ReadMyServiceJson(file);
if (string.IsNullOrEmpty(tag))
{
throw new ArgumentNullException("tag");
}

var services = ReadMyServiceFile();

services.FirstOrDefault().Image = tag;

_serviceHandle = new ServiceClientHandle(GetServiceClient(url), token);

var reply = _serviceHandle.UpdateService(service).Result;
services.ToList().ForEach(x => {

var reply = _serviceHandle.UpdateService(x).Result;

System.Console.WriteLine(reply);
System.Console.WriteLine(reply);
});
}

private static void Remove()
{
var service = ReadMyServiceJson(file);
var services = ReadMyServiceFile();

_serviceHandle = new ServiceClientHandle(GetServiceClient(url), token);

var reply = _serviceHandle.RemoveService(service.Id ?? service.Name).Result;

System.Console.WriteLine(reply);
services.ToList().ForEach(x =>
{
var reply = _serviceHandle.RemoveService(x.Id ?? x.Name).Result;
System.Console.WriteLine(reply);
});
}

private static void Create()
{
var service = ReadMyServiceJson(file);
var services = ReadMyServiceFile();

_serviceHandle = new ServiceClientHandle(GetServiceClient(url), token);

var response = _serviceHandle.CreateService(service, true).Result;
services.ToList().ForEach(x => {

System.Console.WriteLine(response);
var response = _serviceHandle.CreateService(x, true).Result;

System.Console.WriteLine(response);
});
}

private static void ParserArgs(IEnumerable<string> args)
{
var showHelp = false;
var writeJson = false;
var writeYaml = false;
var opt = new OptionSet()
{
{"u|url=", "The url of grpc endpoints", u => url = u },
{"f|file=", "The json of service or tar of image to be handled", j => file = j},
{"t|tag=", "The image tag to build. If not passed, latest be default.", t => tag = t },
{"f|file=", "The yaml or json of service or tar of image to be handled", j => file = j},
{"t|tag=", "The image tag to build and update.", t => tag = t },
{"p|param=", "The param to build. Ex: foo=bar", p => buildParam.Add(p) },
{"a|action=", "The action to perform (create,update,remove)", a => action = (Actions)Enum.Parse(typeof(Actions), a)},
{"w|write", "Write json example", w => writeJson = w != null},
{"w|write", "Write yaml example", w => writeYaml = w != null},
{"h|help", "Print help", h => showHelp = h != null}
};

Expand All @@ -145,9 +166,9 @@ private static void ParserArgs(IEnumerable<string> args)
Environment.Exit(0);
}

if (writeJson)
if (writeYaml)
{
PrintJsonExample();
PrintYamlExample();
Environment.Exit(0);
}

Expand All @@ -169,7 +190,25 @@ private static void ShowHelp(OptionSet opt)

private static void PrintJsonExample()
{
var service = new MyService("test", "redis", "backend", "db_net")
MyService service = MountObjExample();

System.Console.WriteLine(JsonConvert.SerializeObject(service));
}

private static void PrintYamlExample()
{
var service = MountObjExample();

var serializer = new SerializerBuilder()
//.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();

System.Console.WriteLine(serializer.Serialize(new List<MyService>() { service }));
}

private static MyService MountObjExample()
{
return new MyService("test", "redis", "backend", "db_net")
{
Id = Guid.NewGuid().ToString(),
Labels = new Dictionary<string, string>()
Expand All @@ -185,8 +224,6 @@ private static void PrintJsonExample()
new Core.DTO.Port(80, "tcp")
}
};

System.Console.WriteLine(JsonConvert.SerializeObject(service));
}

private static MyContainerService.MyContainerServiceClient GetServiceClient(string url)
Expand Down Expand Up @@ -216,11 +253,45 @@ private static MyContainerImage.MyContainerImageClient GetImageClient(string url
return client;
}

private static MyService ReadMyServiceJson(string jsonPath)
private static IList<MyService> ReadMyServiceFile()
{
var fileInfo = new FileInfo(file);

if (!fileInfo.Exists)
{
throw new FileNotFoundException();
}

if (fileInfo.Extension.Equals(".json"))
{
return ReadMyServiceJson(file);
}

if (fileInfo.Extension.Equals(".yaml"))
{
return ReadMyServiceAsYaml(file);
}

throw new FileLoadException();
}

private static IList<MyService> ReadMyServiceAsYaml(string yamlPath)
{
var yamlContent = File.ReadAllText(yamlPath);

var deserializer = new DeserializerBuilder()
// .WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();

return deserializer.Deserialize<IList<MyService>>(yamlContent);

}

private static IList<MyService> ReadMyServiceJson(string jsonPath)
{
var json = File.ReadAllText(jsonPath);

return JsonConvert.DeserializeObject<MyService>(json);
return JsonConvert.DeserializeObject<IList<MyService>>(json);
}
}
}
8 changes: 4 additions & 4 deletions Console/Services/myService.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
[{
"Name": "mcs_MCS",
"Image": "mcs:latest",
"Networks": [
Expand All @@ -16,9 +16,9 @@
],
"Ports": [
{
"InternalPort": 5000,
"ExternalPort": 80,
"InternalPort": 80,
"ExternalPort": 5000,
"Protocol": "tcp"
}
]
}
}]
15 changes: 15 additions & 0 deletions Console/Services/myServices.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- id: ''
name: mcs_MCS
image: mcs:latest
networks:
- mcs_default
labels:
createdByMyService: true
volumes:
- ReadOnly: true
source: /var/run/docker.sock
target: /var/run/docker.sock
ports:
- internal_port: 80
external_port: 5000
protocol: tcp
Binary file removed Console/img.tar
Binary file not shown.
2 changes: 1 addition & 1 deletion Core/DTO/MyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MyService()

public string Id { get; set; }
public string Name { get; init; }
public string Image { get; init; }
public string Image { get; set; }
public int Version { get; set; }
public IEnumerable<string> Networks { get; init; }
public IDictionary<string, string> Labels { get; set; }
Expand Down
9 changes: 7 additions & 2 deletions Docker/DockerImageHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class DockerImageHandle : IImageHandle
public DockerImageHandle(HttpClient httpClient)
{
_httpClient = httpClient;

_httpClient.Timeout = new TimeSpan(0, 15, 0);
}
public async Task<string> BuildImage(byte[] imageFile, IEnumerable<string> param, string tag)
{
Expand All @@ -28,9 +30,12 @@ public async Task<string> BuildImage(byte[] imageFile, IEnumerable<string> param

var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{BUILD_URI}?{query.ToString()}");

var tarAsString = Encoding.UTF8.GetString(imageFile);
//var tarAsString = Encoding.UTF8.GetString(imageFile);

using var memStream = new MemoryStream(imageFile);

requestMessage.Content = new StringContent(tarAsString, Encoding.UTF8, "application/x-tar");
requestMessage.Content = new StreamContent(memStream);
//requestMessage.Content = new StringContent(tarAsString, Encoding.UTF8, "application/x-tar");

var response = await _httpClient.SendAsync(requestMessage);

Expand Down
2 changes: 1 addition & 1 deletion HttpSocket/UnixHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class UnixHttpClient
private static async ValueTask<Stream> SocketConnectionAsync(SocketsHttpConnectionContext socketsHttpConnectionContext, CancellationToken cancellationToken)
{
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);

await socket.ConnectAsync(_endPoint);

return new NetworkStream(socket);;
Expand Down

0 comments on commit c62494f

Please sign in to comment.