Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Bug fixes and extra parameter to filter containers
Browse files Browse the repository at this point in the history
- Fixed bug where the tool throws an error for volumes that have not
been mounted on windows
- Added feature to filter the containers by a glob for which you would
like to monitor for changes
- Updated version to v0.3.0
  • Loading branch information
vandycknick committed Aug 31, 2018
1 parent 04969cc commit 9c48a36
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 15 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{yml,json,csproj}]
indent_size = 2

[Makefile]
indent_style = tab
indent_size = 4
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"processId": "${command:pickProcess}"
}
,]
}
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [v0.3.0] - 2018-08-31
### Added
- New parameter (`--container`) that accepts a glob to filter the running containers for which you would like to watch for cahnges

### Fixed
- Monitor throws an error for volumes that only have a mount point in the container, but are not mounted in windows.

## [v0.2.0] - 2018-07-26
### Added
- improved logging
Expand Down
2 changes: 1 addition & 1 deletion Version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>0.2.0</VersionPrefix>
<VersionPrefix>0.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<SourceRevisionId>$(APPVEYOR_REPO_COMMIT)</SourceRevisionId>
<PackageVersion Condition="'$(APPVEYOR_REPO_TAG)' == 'true' AND '$(VersionSuffix)' != ''">$(VersionPrefix)-$(VersionSuffix)</PackageVersion>
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.2.0.{build}
version: 0.3.0.{build}
os: Visual Studio 2017
environment:
global:
Expand Down
37 changes: 30 additions & 7 deletions src/DockerWatch/ContainerMonitorHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using DockerWatch.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

Expand All @@ -15,14 +16,20 @@ public class ContainerMonitorHost : IHostedService, IDisposable

private readonly DockerService _dockerService;
private readonly IContainerNotifierFactory _containerNotifierFactory;
private readonly ContainerMonitorHostOptions _options;
private readonly ILogger _logger;

private Dictionary<string, List<ContainerNotifier>> _containerNotifiers;
private ContainerEventsMonitor _monitor;
public ContainerMonitorHost(DockerService dockerService, IContainerNotifierFactory containerNotifierFactory, ILogger<ContainerMonitorHost> logger)
public ContainerMonitorHost(
DockerService dockerService,
IContainerNotifierFactory containerNotifierFactory,
ContainerMonitorHostOptions options,
ILogger<ContainerMonitorHost> logger)
{
_dockerService = dockerService;
_containerNotifierFactory = containerNotifierFactory;
_options = options;
_logger = logger;

_containerNotifiers = new Dictionary<string, List<ContainerNotifier>>();
Expand All @@ -38,9 +45,10 @@ private string ToWindowsPath(string dockerSourcePath)
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting monitor.");
var containers = await _dockerService.GetRunningContainers();
var result = await _dockerService.GetRunningContainers();
var containers = result.Where(c => c.Name.MatchesGlob(_options.ContainerGlob)).ToList();

_logger.LogTrace($"Found {containers.Count} running container{(containers.Count > 1 ? "s" : "")}.");
_logger.LogTrace($"Found {containers.Count} running container{(containers.Count > 1 ? "s" : "")} matching glob '{_options.ContainerGlob}'.");

foreach (var container in containers)
{
Expand Down Expand Up @@ -114,9 +122,16 @@ public void RemoveNotifiersForContainer(Container container)

public void AttachNotifiersForContainer(Container container)
{
if(!container.Name.MatchesGlob(_options.ContainerGlob))
{
_logger.LogTrace($"Container '{container.Name}' does not match '{_options.ContainerGlob}', no notifiers registered!");
return;
}

var n = new List<ContainerNotifier>();
var mounts = container.Mounts.Where(m => !String.IsNullOrEmpty(m.Source));

foreach (var mount in container.Mounts)
foreach (var mount in mounts)
{
var hostPath = ToWindowsPath(mount.Source);
try
Expand All @@ -131,13 +146,21 @@ public void AttachNotifiersForContainer(Container container)
}
}

_containerNotifiers.Add(container.ID, n);
_logger.LogInformation($"Registered notifiers for all mounted volumes in '{container.Name}'.");
if (mounts.Count() > 0)
{
_containerNotifiers.Add(container.ID, n);
_logger.LogInformation($"Registered notifiers for all mounted volumes in '{container.Name}'.");
}
else
{
_logger.LogTrace($"No windows mounted volumes found for '{container.Name}'");
}

}

public void Dispose()
{
_monitor.Dispose();
_monitor?.Dispose();
}
}
}
7 changes: 7 additions & 0 deletions src/DockerWatch/ContainerMonitorHostOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DockerWatch
{
public class ContainerMonitorHostOptions
{
public string ContainerGlob { get; set; }
}
}
1 change: 0 additions & 1 deletion src/DockerWatch/ContainerNotifier.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace DockerWatch
Expand Down
6 changes: 3 additions & 3 deletions src/DockerWatch/DockerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Dispose()
}

public class DockerService
{
{
const string DOCKER_URI = "npipe://./pipe/docker_engine";

private readonly DockerClient _Client;
Expand All @@ -66,7 +66,7 @@ public DockerService()
.CreateClient();
}

public async Task<IList<Container>> GetRunningContainers()
public async Task<IEnumerable<Container>> GetRunningContainers()
{
var running = (
from container in await GetAllContainers()
Expand All @@ -79,7 +79,7 @@ from container in await GetAllContainers()
}
);

return running.ToList();
return running;
}

private Task<IList<ContainerListResponse>> GetAllContainers()
Expand Down
22 changes: 22 additions & 0 deletions src/DockerWatch/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.RegularExpressions;

namespace DockerWatch.Extensions
{
public static class StringExtensions
{
/// <summary>
/// Compares the string against a given pattern.
/// Stolen from: https://stackoverflow.com/questions/188892/glob-pattern-matching-in-net
/// </summary>
/// <param name="str">The string.</param>
/// <param name="pattern">The pattern to match, where "*" means any sequence of characters, and "?" means any single character.</param>
/// <returns><c>true</c> if the string matches the given pattern; otherwise <c>false</c>.</returns>
public static bool MatchesGlob(this string str, string pattern)
{
return new Regex(
"^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
RegexOptions.IgnoreCase | RegexOptions.Singleline
).IsMatch(str);
}
}
}
7 changes: 6 additions & 1 deletion src/DockerWatch/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
Expand All @@ -16,6 +15,9 @@ class Program
[Option(Description = "Enable more verbose and rich logging.", ShortName = "v")]
public bool Verbose { get; set; } = false;

[Option(Description = "", ShortName = "c")]
public string Container { get; set; } = "*";

public IHostBuilder CreateContainerMonitorHostBuilder()
{
var host = new HostBuilder()
Expand All @@ -31,6 +33,9 @@ public IHostBuilder CreateContainerMonitorHostBuilder()

services.AddHostedService<ContainerMonitorHost>();

services.AddSingleton<ContainerMonitorHostOptions>(_ => new ContainerMonitorHostOptions {
ContainerGlob = Container,
});
services.AddSingleton<IContainerNotifierFactory, ContainerNotifierFactory>();
services.AddSingleton<INotifierAction, NotifierAction>();
services.AddSingleton<DockerService>();
Expand Down

0 comments on commit 9c48a36

Please sign in to comment.