From b9a20186350a0315ee7159f2df2b55a47e9f90a4 Mon Sep 17 00:00:00 2001 From: elijah Date: Mon, 1 Apr 2013 00:04:54 -0700 Subject: remove supply_drop, add support for puppet_command. --- lib/leap_cli/remote/leap_plugin.rb | 67 ++++++++++++++++++++++++++ lib/leap_cli/remote/plugin.rb | 93 ------------------------------------ lib/leap_cli/remote/puppet_plugin.rb | 66 +++++++++++++++++++++++++ lib/leap_cli/remote/rsync_plugin.rb | 35 ++++++++++++++ lib/leap_cli/remote/tasks.rb | 22 +++------ 5 files changed, 175 insertions(+), 108 deletions(-) create mode 100644 lib/leap_cli/remote/leap_plugin.rb delete mode 100644 lib/leap_cli/remote/plugin.rb create mode 100644 lib/leap_cli/remote/puppet_plugin.rb create mode 100644 lib/leap_cli/remote/rsync_plugin.rb (limited to 'lib/leap_cli/remote') diff --git a/lib/leap_cli/remote/leap_plugin.rb b/lib/leap_cli/remote/leap_plugin.rb new file mode 100644 index 0000000..2c427e9 --- /dev/null +++ b/lib/leap_cli/remote/leap_plugin.rb @@ -0,0 +1,67 @@ +# +# these methods are made available in capistrano tasks as 'leap.method_name' +# (see RemoteCommand::new_capistrano) +# + +module LeapCli; module Remote; module LeapPlugin + + def required_packages + "puppet ruby-hiera-puppet rsync lsb-release" + end + + def log(*args, &block) + LeapCli::Util::log(*args, &block) + end + + # + # 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 assert_initialized + begin + test_initialized_file = "test -f #{INITIALIZED_FILE}" + 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 node init #{host}'", :host => host + end + end + end + end + + def mark_initialized + run "touch #{INITIALIZED_FILE}" + 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 + + #def logrun(cmd) + # @streamer ||= LeapCli::Remote::LogStreamer.new + # run cmd do |channel, stream, data| + # @streamer.collect_output(channel[:host], data) + # end + #end + +# return_code = nil +# run "something; echo return code: $?" do |channel, stream, data| +# if data =~ /return code: (\d+)/ +# return_code = $1.to_i +# else +# Capistrano::Configuration.default_io_proc.call(channel, stream, data) +# end +# end +# puts "finished with return code: #{return_code}" + +end; end; end \ No newline at end of file diff --git a/lib/leap_cli/remote/plugin.rb b/lib/leap_cli/remote/plugin.rb deleted file mode 100644 index 4824858..0000000 --- a/lib/leap_cli/remote/plugin.rb +++ /dev/null @@ -1,93 +0,0 @@ -# -# these methods are made available in capistrano tasks as 'leap.method_name' -# - -require 'rsync_command' - -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 - - # - # 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 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 node init #{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 a hash with various rsync options. - # supported options include: - # - # {:source => '', :dest => '', :flags => '', :includes => [], :excludes => []} - # - def rsync_update - rsync = RsyncCommand.new(:logger => logger, :flags => '-a') - rsync.asynchronously(find_servers) do |server| - options = yield server - next unless options - remote_user = server.user || fetch(:user, ENV['USER']) - src = options[:source] - dest = {:user => remote_user, :host => server.host, :path => options[:dest]} - options[:ssh] = ssh_options.merge(server.options[:ssh_options]||{}) - options[:chdir] ||= Path.provider - rsync.exec(src, dest, options) - end - if rsync.failed? - LeapCli::Util.bail! do - LeapCli::Util.log :failed, "to rsync to #{rsync.failures.map{|f|f[:dest][:host]}.join(' ')}" - end - end - end - - #def logrun(cmd) - # @streamer ||= LeapCli::Remote::LogStreamer.new - # run cmd do |channel, stream, data| - # @streamer.collect_output(channel[:host], data) - # end - #end - -# return_code = nil -# run "something; echo return code: $?" do |channel, stream, data| -# if data =~ /return code: (\d+)/ -# return_code = $1.to_i -# else -# Capistrano::Configuration.default_io_proc.call(channel, stream, data) -# end -# end -# puts "finished with return code: #{return_code}" - -end; end; end \ No newline at end of file diff --git a/lib/leap_cli/remote/puppet_plugin.rb b/lib/leap_cli/remote/puppet_plugin.rb new file mode 100644 index 0000000..9c41380 --- /dev/null +++ b/lib/leap_cli/remote/puppet_plugin.rb @@ -0,0 +1,66 @@ +# +# these methods are made available in capistrano tasks as 'puppet.method_name' +# (see RemoteCommand::new_capistrano) +# + +module LeapCli; module Remote; module PuppetPlugin + + def apply(options) + run "#{PUPPET_DESTINATION}/bin/puppet_command set_hostname apply #{flagize(options)}" + end + + private + + def flagize(hsh) + hsh.inject([]) {|str, item| + if item[1] === false + str + elsif item[1] === true + str << "--" + item[0].to_s + else + str << "--" + item[0].to_s + " " + item[1].to_s + end + }.join(' ') + end + +end; end; end + + + # def puppet(command = :noop) + # #puppet_cmd = "cd #{puppet_destination} && #{sudo_cmd} #{puppet_command} --modulepath=#{puppet_lib} #{puppet_parameters}" + # puppet_cmd = "cd #{puppet_destination} && #{sudo_cmd} #{puppet_command} #{puppet_parameters}" + # flag = command == :noop ? '--noop' : '' + + # writer = if puppet_stream_output + # SupplyDrop::Writer::Streaming.new(logger) + # else + # SupplyDrop::Writer::Batched.new(logger) + # end + + # writer = SupplyDrop::Writer::File.new(writer, puppet_write_to_file) unless puppet_write_to_file.nil? + + # begin + # exitcode = nil + # run "#{puppet_cmd} #{flag}; echo exitcode:$?" do |channel, stream, data| + # if data =~ /exitcode:(\d+)/ + # exitcode = $1 + # writer.collect_output(channel[:host], "Puppet #{command} complete (#{exitcode_description(exitcode)}).\n") + # else + # writer.collect_output(channel[:host], data) + # end + # end + # ensure + # writer.all_output_collected + # end + # end + + # def exitcode_description(code) + # case code + # when "0" then "no changes" + # when "2" then "changes made" + # when "4" then "failed" + # when "6" then "changes and failures" + # else code + # end + # end + diff --git a/lib/leap_cli/remote/rsync_plugin.rb b/lib/leap_cli/remote/rsync_plugin.rb new file mode 100644 index 0000000..2c89f26 --- /dev/null +++ b/lib/leap_cli/remote/rsync_plugin.rb @@ -0,0 +1,35 @@ +# +# these methods are made available in capistrano tasks as 'rsync.method_name' +# (see RemoteCommand::new_capistrano) +# + +require 'rsync_command' + +module LeapCli; module Remote; module RsyncPlugin + + # + # takes a block, yielded a server, that should return a hash with various rsync options. + # supported options include: + # + # {:source => '', :dest => '', :flags => '', :includes => [], :excludes => []} + # + def update + rsync = RsyncCommand.new(:logger => logger, :flags => '-a') + rsync.asynchronously(find_servers) do |server| + options = yield server + next unless options + remote_user = server.user || fetch(:user, ENV['USER']) + src = options[:source] + dest = {:user => remote_user, :host => server.host, :path => options[:dest]} + options[:ssh] = ssh_options.merge(server.options[:ssh_options]||{}) + options[:chdir] ||= Path.provider + rsync.exec(src, dest, options) + end + if rsync.failed? + LeapCli::Util.bail! do + LeapCli::Util.log :failed, "to rsync to #{rsync.failures.map{|f|f[:dest][:host]}.join(' ')}" + end + end + end + +end; end; end diff --git a/lib/leap_cli/remote/tasks.rb b/lib/leap_cli/remote/tasks.rb index 35349ad..b515650 100644 --- a/lib/leap_cli/remote/tasks.rb +++ b/lib/leap_cli/remote/tasks.rb @@ -3,8 +3,6 @@ # For DSL manual, see https://github.com/capistrano/capistrano/wiki # -require 'supply_drop' - MAX_HOSTS = 10 task :install_authorized_keys, :max_hosts => MAX_HOSTS do @@ -14,14 +12,8 @@ task :install_authorized_keys, :max_hosts => MAX_HOSTS do end end -task :set_hostname, :max_hosts => MAX_HOSTS do - leap.log :setting, "hostname" do - run "hostname $CAPISTRANO:HOST$" - end -end - task :install_prerequisites, :max_hosts => MAX_HOSTS do - leap.mkdirs puppet_destination + leap.mkdirs LeapCli::PUPPET_DESTINATION run "locale-gen" leap.log :updating, "package list" do run "apt-get update" @@ -33,9 +25,9 @@ task :install_prerequisites, :max_hosts => MAX_HOSTS do leap.mark_initialized 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 - puppet.apply - end -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 +# puppet.apply +# end +#end -- cgit v1.2.3