FEATURE: Allow multiple directories to be watched.

FEATURE: Allow multiple directories to be watched.

diff --git a/README.md b/README.md
index 5840d18..7102e88 100644
--- a/README.md
+++ b/README.md
@@ -9,17 +9,13 @@ provided as environment variables, and probably `docker.sock` as a bind mount:
 
 `‍``
 docker run -v /var/run/docker.sock:/var/run/docker.sock \
-    -e NEFARIO_CONFIG_DIRECTORY=/nefario \
+    -e NEFARIO_CONFIG_DIRECTORIES=/nefario \
     discourse/nefario:latest
 `‍``
 
 The available configuration environment variables are listed in the "[Configuration](#configuration)" section, below.
 
-Once Nefario is up and running, it will keep an eye on the directory specified by
-`NEFARIO_CONFIG_DIRECTORY`, and any time a file is created or modified in that
-directory that has a `.yaml` extension, it will treat it as a
-[`moby-derp`](https://github.com/mpalmer/moby-derp) pod configuration file, and
-trigger a `moby-derp` run with that configuration.
+Once Nefario is up and running, it will keep an eye on the directoies specified by `NEFARIO_CONFIG_DIRECTORIES`, and any time a file is created or modified in the watched directories that has a `.yaml` extension, it will treat it as a [`moby-derp`](https://github.com/mpalmer/moby-derp) pod configuration file, and trigger a `moby-derp` run with that configuration.
 
 
 ## Configuration
@@ -28,8 +24,7 @@ As per the principles of the [12-factor app](https://12factor.net), Nefario is
 configured entirely via environment variables.  Only the `NEFARIO_CONFIG_DIRECTORY`
 variable is required.
 
-* **`NEFARIO_CONFIG_DIRECTORY`**: the directory to watch for modified
-  configuration files.  This directory must exist before Nefario is started.
+* **`NEFARIO_CONFIG_DIRECTORIES`**: comma seperated string of directories to watch for modified configuration files.  The directories must exist before Nefario is started.
 
 * **`NEFARIO_MOUNT_ROOT`**: (default: `/srv/docker`) The filesystem path where
   all pod volumes should be created.
diff --git a/Rakefile b/Rakefile
index ebf04c8..0d991d6 100644
--- a/Rakefile
+++ b/Rakefile
@@ -25,7 +25,7 @@ end
 desc "Run sample local nefario"
 task :run_local do
   $LOAD_PATH.unshift(File.expand_path(".", "lib"))
-  ENV["NEFARIO_CONFIG_DIRECTORY"] = File.expand_path(".", "sample/pods")
+  ENV["NEFARIO_CONFIG_DIRECTORIES"] = "#{File.expand_path(".", "sample/pods")},#{File.expand_path(".", "sample/pods_2")}"
   ENV["NEFARIO_METRICS_PORT"] = "9408"
   load(File.expand_path(".", "bin/nefario"))
 end
@@ -56,4 +56,3 @@ namespace :docker do
     end
   end
 end
-
diff --git a/lib/nefario.rb b/lib/nefario.rb
index a4cce8a..74aeee5 100644
--- a/lib/nefario.rb
+++ b/lib/nefario.rb
@@ -6,7 +6,7 @@ class Nefario
 
   url :DOCKER_HOST, default: "unix:///var/run/docker.sock"
   string :DOCKER_CONFIG, default: "~/.docker"
-  string :NEFARIO_CONFIG_DIRECTORY
+  string :NEFARIO_CONFIG_DIRECTORIES
   string :NEFARIO_MOUNT_ROOT, default: "/srv/docker"
   string :NEFARIO_HOST_HOSTNAME, default: Socket.gethostname
   integer :NEFARIO_IMAGE_REFRESH_INTERVAL, default: 15 * 60
diff --git a/lib/nefario/config_dir_watcher.rb b/lib/nefario/config_dir_watcher.rb
index f212d36..ef65011 100644
--- a/lib/nefario/config_dir_watcher.rb
+++ b/lib/nefario/config_dir_watcher.rb
@@ -16,18 +16,23 @@ class Nefario::ConfigDirWatcher
 
   def run
     notifier = INotify::Notifier.new
-    notifier.watch(@config.config_directory, :move, :close_write, :delete) do |event|
-      @metrics.pending_events_total.set(@inotify_queue.length)
-      @inotify_queue << event
+    config_directories = @config.config_directories.split(",")
+
+    config_directories.each do |config_directory|
+      notifier.watch(config_directory, :move, :close_write, :delete) do |event|
+        @metrics.pending_events_total.set(@inotify_queue.length)
+        @inotify_queue << event
+      end
     end
 
 
     logger.info(logloc) { "Refreshing all pods" }
-    Pathname.new(@config.config_directory).each_child do |f|
-      next unless f.basename.to_s =~ /(\A[^.]|\.yaml\z)/
 
-      pod_name = f.basename(".yaml").to_s
-      @ultravisor[:moby_derp_runner].call.refresh_pod(pod_name)
+    config_directories.each do |config_directory|
+      Pathname.new(config_directory).each_child do |f|
+        next unless f.basename.to_s =~ /(\A[^.]|\.yaml\z)/
+        @ultravisor[:moby_derp_runner].call.refresh_pod(f)
+      end
     end
 
     loop do
@@ -87,7 +92,7 @@ class Nefario::ConfigDirWatcher
     if event.flags.include?(:delete) || event.flags.include?(:moved_from)
       pod_deleted(File.basename(event.name, ".yaml"))
     elsif event.flags.include?(:moved_to) || event.flags.include?(:close_write)
-      pod_changed(File.basename(event.name, ".yaml"))
+      pod_changed(Pathname.new(event.absolute_name))
     else
       logger.debug(logloc) { "Ignoring event; no interesting looking flags" }
     end
@@ -105,11 +110,11 @@ class Nefario::ConfigDirWatcher
       .each { |c| logger.debug(logloc) { "Deleting container #{c.id}" }; c.stop; c.delete }
   end
 
-  def pod_changed(pod_name)
-    logger.info(logloc) { "Triggering refresh on #{pod_name}" }
+  def pod_changed(path)
+    logger.info(logloc) { "Triggering refresh on #{path.basename(".yaml")}" }
 
     begin
-      @ultravisor[:moby_derp_runner].call.refresh_pod(pod_name)
+      @ultravisor[:moby_derp_runner].call.refresh_pod(path)
     rescue Ultravisor::ChildExitedError
       retry
     end
diff --git a/lib/nefario/image_watcher.rb b/lib/nefario/image_watcher.rb
index 5f27374..9d30a72 100644
--- a/lib/nefario/image_watcher.rb
+++ b/lib/nefario/image_watcher.rb
@@ -41,13 +41,18 @@ class Nefario::ImageWatcher
   def refresh_all_images
     image_map = Hash.new { |h, k| h[k] = ImageInfo.new(k, @docker_config, logger) }
 
-    Pathname.new(@config.config_directory).each_child do |podfile|
-      next unless podfile.basename.to_s =~ /(\A[^.]|\.yaml\z)/
+    @config.config_directories.split(",").each do |config_directory|
+      Pathname.new(config_directory).each_child do |podfile|
+        next unless podfile.basename.to_s =~ /(\A[^.]|\.yaml\z)/
 
-      pod_name = podfile.basename(".yaml").to_s
-      cfg = YAML.safe_load(podfile.read)
-      logger.debug(logloc) { "Pod configuration for #{pod_name} is #{cfg.inspect}" }
-      cfg["containers"].values.each { |c| image_map[c["image"]] << pod_name }
+        cfg = YAML.safe_load(podfile.read)
+        logger.debug(logloc) { "Pod configuration for #{podfile} is #{cfg.inspect}" }
+        pod_name = podfile.basename(".yaml").to_s
+
+        cfg["containers"].values.each do |c|
+          image_map[c["image"]] << podfile
+        end
+      end
     end
 
     changed_pods = []
diff --git a/lib/nefario/moby_derp_runner.rb b/lib/nefario/moby_derp_runner.rb
index e89a74e..1db06c6 100644
--- a/lib/nefario/moby_derp_runner.rb
+++ b/lib/nefario/moby_derp_runner.rb
@@ -16,13 +16,13 @@ class Nefario::MobyDerpRunner
     process_castcall_loop
   end
 
-  def refresh_pod(name)
+  def refresh_pod(path)
+    name = path.basename(".yaml").to_s
     logger.debug(logloc) { "Received request to refresh pod #{name.inspect}" }
-
     validate_pod_name(name)
 
     begin
-      MobyDerp::Pod.new(pod_config(name)).run
+      MobyDerp::Pod.new(pod_config(path)).run
     rescue MobyDerp::Error => ex
       log_exception(ex) { "Exception while derping moby pod #{name}" }
     else
@@ -41,8 +41,8 @@ class Nefario::MobyDerpRunner
     end
   end
 
-  def pod_config(name)
-    MobyDerp::PodConfig.new(Pathname.new(@config.config_directory).join("#{name}.yaml"), system_config)
+  def pod_config(path)
+    MobyDerp::PodConfig.new(path, system_config)
   end
 
   def system_config
diff --git a/sample/pods_2/sample_container_2.yaml b/sample/pods_2/sample_container_2.yaml
new file mode 100644
index 0000000..3249895
--- /dev/null
+++ b/sample/pods_2/sample_container_2.yaml
@@ -0,0 +1,15 @@
+hostname: testing
+root_labels:
+  test.label: '100'
+expose:
+  - 9998
+  - 9409
+containers:
+  green:
+    image: "debian:latest"
+    command: "/bin/sleep 102"
+    restart: always

[... diff too long, it was truncated ...]

GitHub sha: f5416d9d

This commit appears in #2 which was merged by tgxworld.