From c76221182ca98ed804cc0c5259982250fa45f67c Mon Sep 17 00:00:00 2001
From: elijah <elijah@riseup.net>
Date: Wed, 28 Nov 2012 01:40:20 -0800
Subject: give the user a nice error if 'init-node' has not yet been run (or if
 there are required packages that are missing).

---
 lib/leap_cli/commands/deploy.rb | 19 ++++---------------
 lib/leap_cli/log.rb             | 40 +++++++++++++++++++++++-----------------
 lib/leap_cli/remote/plugin.rb   | 39 +++++++++++++++++++++++++++++++++++----
 lib/leap_cli/remote/tasks.rb    | 17 +++++------------
 4 files changed, 67 insertions(+), 48 deletions(-)

(limited to 'lib')

diff --git a/lib/leap_cli/commands/deploy.rb b/lib/leap_cli/commands/deploy.rb
index 63e6c73..bee09a0 100644
--- a/lib/leap_cli/commands/deploy.rb
+++ b/lib/leap_cli/commands/deploy.rb
@@ -2,8 +2,8 @@ module LeapCli
   module Commands
 
     desc 'Apply recipes to a node or set of nodes'
-    long_desc 'The node filter can be the name of a node, service, or tag.'
-    arg_name '<node filter>'
+    long_desc 'The node-filter can be the name of a node, service, or tag.'
+    arg_name 'node-filter'
     command :deploy do |c|
       c.action do |global_options,options,args|
         init_submodules
@@ -17,11 +17,7 @@ module LeapCli
         end
 
         ssh_connect(nodes) do |ssh|
-          # directory setup
-          ssh.leap.mkdir("/etc/leap")
-          ssh.leap.mkdir("/srv/leap")
-          ssh.leap.chown_root("/etc/leap")
-          ssh.leap.chown_root("/srv/leap")
+          ssh.leap.assert_initialized
 
           # sync hiera conf
           ssh.leap.log :updating, "hiera.yaml" do
@@ -31,14 +27,7 @@ module LeapCli
             end
           end
 
-          # sync puppet
-          #
-          # what we want:
-          #     puppet apply --confdir /srv/leap/puppet /srv/leap/puppet/manifests/site.pp | grep -v 'warning:.*is deprecated'
-          #
-          # what we get currently:
-          #
-          #
+          # sync puppet manifests and apply them
           ssh.set :puppet_source, [Path.platform, 'puppet'].join('/')
           ssh.set :puppet_destination, '/srv/leap'
           ssh.set :puppet_command, '/usr/bin/puppet apply --color=false'
diff --git a/lib/leap_cli/log.rb b/lib/leap_cli/log.rb
index 42a886e..4b0bbe2 100644
--- a/lib/leap_cli/log.rb
+++ b/lib/leap_cli/log.rb
@@ -59,27 +59,33 @@ module LeapCli
         end
         if title
           prefix = case title
-            when :error     then Paint['error', :red, :bold]
-            when :warning   then Paint['warning', :yellow, :bold]
-            when :info      then Paint['info', :cyan, :bold]
-            when :updated   then Paint['updated', :cyan, :bold]
-            when :updating  then Paint['updating', :cyan, :bold]
-            when :created   then Paint['created', :green, :bold]
-            when :removed   then Paint['removed', :red, :bold]
-            when :nochange  then Paint['no change', :magenta]
-            when :loading   then Paint['loading', :magenta]
-            when :missing   then Paint['missing', :yellow, :bold]
-            when :run       then Paint['run', :magenta]
-            when :failed    then Paint['FAILED', :red, :bold]
-            when :completed then Paint['completed', :green, :bold]
-            when :ran       then Paint['ran', :green, :bold]
-            when :bail      then Paint['bailing out', :red, :bold]
-            else Paint[title.to_s, :cyan, :bold]
+            when :error     then ['error', :red, :bold]
+            when :warning   then ['warning', :yellow, :bold]
+            when :info      then ['info', :cyan, :bold]
+            when :updated   then ['updated', :cyan, :bold]
+            when :updating  then ['updating', :cyan, :bold]
+            when :created   then ['created', :green, :bold]
+            when :removed   then ['removed', :red, :bold]
+            when :nochange  then ['no change', :magenta]
+            when :loading   then ['loading', :magenta]
+            when :missing   then ['missing', :yellow, :bold]
+            when :run       then ['run', :magenta]
+            when :failed    then ['FAILED', :red, :bold]
+            when :completed then ['completed', :green, :bold]
+            when :ran       then ['ran', :green, :bold]
+            when :bail      then ['bailing out', :red, :bold]
+            else [title.to_s, :cyan, :bold]
+          end
+          if options[:host]
+            print "[%s] %s " % [Paint[options[:host], prefix[1], prefix[2]], prefix[0]]
+          else
+            print "%s " % Paint[prefix[0], prefix[1], prefix[2]]
           end
-          print "#{prefix} "
           if FILE_TITLES.include?(title) && message =~ /^\//
             message = LeapCli::Path.relative_path(message)
           end
+        elsif options[:host]
+          print "[%s] " % options[:host]
         end
         puts "#{message}"
         if block_given?
diff --git a/lib/leap_cli/remote/plugin.rb b/lib/leap_cli/remote/plugin.rb
index a69cca4..6dafbd8 100644
--- a/lib/leap_cli/remote/plugin.rb
+++ b/lib/leap_cli/remote/plugin.rb
@@ -4,18 +4,49 @@
 
 module LeapCli; module Remote; module Plugin
 
+  def required_packages
+    "puppet ruby-hiera-puppet rsync lsb-release"
+  end
+
   def log(*args, &block)
     LeapCli::Util::log(*args, &block)
   end
 
-  def mkdir(dir)
-    run "mkdir -p #{dir}"
+  #
+  # creates directories that are owned by root and 700 permissions
+  #
+  def mkdirs(*dirs)
+    raise ArgumentError.new('illegal dir name') if dirs.grep(/[\' ]/).any?
+    run dirs.collect{|dir| "mkdir -m 700 -p #{dir}; "}.join
   end
 
-  def chown_root(dir)
-    run "chown root -R #{dir} && chmod -R ag-rwx,u+rwX #{dir}"
+  def assert_initialized
+
+    begin
+      test_initialized_file = "test -f /srv/leap/initialized"
+      check_required_packages = "! dpkg-query -W --showformat='${Status}\n' #{required_packages} 2>&1 | grep -q -E '(deinstall|no packages)'"
+      run "#{test_initialized_file} && #{check_required_packages}"
+    rescue Capistrano::CommandError => exc
+      LeapCli::Util.bail! do
+        exc.hosts.each do |host|
+          LeapCli::Util.log :error, "running deploy: node not initialized. Run 'leap init-node #{host}'", :host => host
+        end
+      end
+    end
   end
 
+  def mark_initialized
+    run "touch /srv/leap/initialized"
+  end
+
+  #def mkdir(dir)
+  #  run "mkdir -p #{dir}"
+  #end
+
+  #def chown_root(dir)
+  #  run "chown root -R #{dir} && chmod -R ag-rwx,u+rwX #{dir}"
+  #end
+
   #
   # takes a block, yielded a server, that should return {:source => '', :dest => ''}
   #
diff --git a/lib/leap_cli/remote/tasks.rb b/lib/leap_cli/remote/tasks.rb
index 4a29517..ef41cb0 100644
--- a/lib/leap_cli/remote/tasks.rb
+++ b/lib/leap_cli/remote/tasks.rb
@@ -9,30 +9,23 @@ MAX_HOSTS = 10
 
 task :install_authorized_keys, :max_hosts => MAX_HOSTS do
   leap.log :updating, "authorized_keys" do
-    run 'mkdir -p /root/.ssh && chmod 700 /root/.ssh'
+    leap.mkdirs '/root/.ssh'
     upload LeapCli::Path.named_path(:authorized_keys), '/root/.ssh/authorized_keys', :mode => '600'
   end
 end
 
 task :install_prerequisites, :max_hosts => MAX_HOSTS do
-  packages = "puppet ruby-hiera-puppet rsync lsb-release"
-  run "mkdir -p #{puppet_destination}"
+  leap.mkdirs puppet_destination
   leap.log :updating, "package list" do
     run "apt-get update"
   end
   leap.log :installing, "required packages" do
-    run "DEBIAN_FRONTEND=noninteractive apt-get -q -y -o DPkg::Options::=--force-confold install #{packages}"
+    run "DEBIAN_FRONTEND=noninteractive apt-get -q -y -o DPkg::Options::=--force-confold install #{leap.required_packages}"
   end
+  leap.mkdirs("/etc/leap", "/srv/leap")
+  leap.mark_initialized
 end
 
-#task :update_platform, :max_hosts => MAX_HOSTS do
-#  puppet.update_code
-#end
-
-#task :mk_leap_dir, :max_hosts => MAX_HOSTS do
-#  run 'mkdir -p /root/leap/config && chown -R root /root/leap && chmod -R ag-rwx,u+rwX /root/leap'
-#end
-
 task :apply_puppet, :max_hosts => MAX_HOSTS do
   raise "now such directory #{puppet_source}" unless File.directory?(puppet_source)
   leap.log :applying, "puppet" do
-- 
cgit v1.2.3