예제 #1
0
        private void AddHooks(IService container)
        {
            // Copy files just before starting
            if (null != _config.CpToOnStart)
            {
                container.AddHook(ServiceRunningState.Starting,
                                  service =>
                {
                    foreach (var copy in _config.CpToOnStart)
                    {
                        ((IContainerService)service).CopyTo(copy.Item2, copy.Item1);
                    }
                });
            }

            // Wait for port when started
            if (null != _config.WaitForPort)
            {
                container.AddHook(ServiceRunningState.Running,
                                  service =>
                {
                    ((IContainerService)service).WaitForPort(_config.WaitForPort.Item1, _config.WaitForPort.Item2);
                });
            }

            // Wait for process when started
            if (null != _config.WaitForProcess)
            {
                container.AddHook(ServiceRunningState.Running,
                                  service =>
                {
                    ((IContainerService)service).WaitForProcess(_config.WaitForProcess.Item1, _config.WaitForProcess.Item2);
                });
            }

            // docker execute on running
            if (null != _config.ExecuteOnRunningArguments && _config.ExecuteOnRunningArguments.Count > 0)
            {
                container.AddHook(ServiceRunningState.Running, service =>
                {
                    var svc = (IContainerService)service;
                    foreach (var binaryAndArguments in _config.ExecuteOnRunningArguments)
                    {
                        var result = svc.DockerHost.Execute(svc.Id, binaryAndArguments, svc.Certificates);
                        if (!result.Success)
                        {
                            throw new FluentDockerException($"Failed to execute {binaryAndArguments} error: {result.Error}");
                        }
                    }
                });
            }

            // Copy files / folders on dispose
            if (null != _config.CpFromOnDispose && 0 != _config.CpFromOnDispose.Count)
            {
                container.AddHook(ServiceRunningState.Removing, service =>
                {
                    foreach (var copy in _config.CpFromOnDispose)
                    {
                        ((IContainerService)service).CopyFrom(copy.Item2, copy.Item1);
                    }
                });
            }

            // docker execute when disposing
            if (null != _config.ExecuteOnDisposingArguments && _config.ExecuteOnDisposingArguments.Count > 0)
            {
                container.AddHook(ServiceRunningState.Removing, service =>
                {
                    var svc = (IContainerService)service;
                    foreach (var binaryAndArguments in _config.ExecuteOnDisposingArguments)
                    {
                        var result = svc.DockerHost.Execute(svc.Id, binaryAndArguments, svc.Certificates);
                        if (!result.Success)
                        {
                            throw new FluentDockerException($"Failed to execute {binaryAndArguments} error: {result.Error}");
                        }
                    }
                });
            }

            // Export container on dispose
            if (null != _config.ExportOnDispose)
            {
                container.AddHook(ServiceRunningState.Removing, service =>
                {
                    var svc = (IContainerService)service;
                    if (_config.ExportOnDispose.Item3(svc))
                    {
                        svc.Export(_config.ExportOnDispose.Item1,
                                   _config.ExportOnDispose.Item2);
                    }
                });
            }
        }