From febeb64a801f3b4c72193bc93ee0400dee3a2a0a Mon Sep 17 00:00:00 2001 From: elijah Date: Fri, 9 Nov 2012 01:22:48 -0800 Subject: vagrant support --- lib/leap_cli/commands/shell.rb | 13 ++++- lib/leap_cli/commands/util.rb | 100 +-------------------------------------- lib/leap_cli/commands/vagrant.rb | 72 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 101 deletions(-) create mode 100644 lib/leap_cli/commands/vagrant.rb (limited to 'lib/leap_cli/commands') diff --git a/lib/leap_cli/commands/shell.rb b/lib/leap_cli/commands/shell.rb index a84c671..f73a706 100644 --- a/lib/leap_cli/commands/shell.rb +++ b/lib/leap_cli/commands/shell.rb @@ -2,10 +2,19 @@ module LeapCli; module Commands desc 'Log in to the specified node with an interactive shell' arg_name '', :optional => false, :multiple => false - command :shell, :ssh do |c| + command :ssh do |c| c.action do |global_options,options,args| node = get_node_from_args(args) - exec "ssh -l root -o 'HostName=#{node.ip_address}' -o 'HostKeyAlias=#{node.name}' -o 'GlobalKnownHostsFile=#{path(:known_hosts)}' -o 'StrictHostKeyChecking=yes' -p #{node.ssh.port} #{node.name}" + options = [ + "-o 'HostName=#{node.ip_address}'", + "-o 'HostKeyAlias=#{node.name}'", + "-o 'GlobalKnownHostsFile=#{path(:known_hosts)}'", + "-o 'StrictHostKeyChecking=yes'" + ] + if node.vagrant? + options << "-i #{vagrant_ssh_key_file}" + end + exec "ssh -l root -p #{node.ssh.port} #{options.join(' ')} {node.name}" end end diff --git a/lib/leap_cli/commands/util.rb b/lib/leap_cli/commands/util.rb index 164ce0d..c1da570 100644 --- a/lib/leap_cli/commands/util.rb +++ b/lib/leap_cli/commands/util.rb @@ -2,6 +2,7 @@ module LeapCli; module Commands extend self extend LeapCli::Util + extend LeapCli::Util::RemoteCommand def path(name) Path.named_path(name) @@ -33,105 +34,6 @@ module LeapCli; module Commands end end - # - # - # - # FYI - # Capistrano::Logger::IMPORTANT = 0 - # Capistrano::Logger::INFO = 1 - # Capistrano::Logger::DEBUG = 2 - # Capistrano::Logger::TRACE = 3 - # - def ssh_connect(nodes, options={}, &block) - node_list = parse_node_list(nodes) - - cap = new_capistrano - cap.logger.level = LeapCli.log_level - user = options[:user] || 'root' - cap.set :user, user - cap.set :ssh_options, ssh_options - cap.set :use_sudo, false # we may want to change this in the future - - # # supply drop options - # cap.set :puppet_source, [Path.platform, 'puppet'].join('/') - # cap.set :puppet_destination, '/root/leap' - # #cap.set :puppet_command, 'puppet apply' - # cap.set :puppet_lib, "puppet/modules" - # cap.set :puppet_parameters, '--confdir=puppet puppet/manifests/site.pp' - # #cap.set :puppet_stream_output, false - # #puppet apply --confdir=puppet puppet/manifests/site.pp | grep -v 'warning:.*is deprecated' - # #puppet_cmd = "cd #{puppet_destination} && #{sudo_cmd} #{puppet_command} --modulepath=#{puppet_lib} #{puppet_parameters}" - - # - # allow password authentication when we are bootstraping a single node. - # - if options[:bootstrap] && node_list.size == 1 - hostname = node_list.values.first.name - - # the 'password' block is only called if key auth fails - if options[:echo] - cap.set(:password) { ask "Root SSH password for #{user}@#{hostname}> " } - else - cap.set(:password) { Capistrano::CLI.password_prompt " * Typed password will be hidden (use --echo to make it visible)\nRoot SSH password for #{user}@#{hostname}> " } - end - end - - node_list.each do |name, node| - cap.server node.name, :dummy_arg, node_options(node) - end - - yield cap - end - - - private - - - # - # For available options, see http://net-ssh.github.com/net-ssh/classes/Net/SSH.html#method-c-start - # - def ssh_options - { - :config => false, - :global_known_hosts_file => path(:known_hosts), - :paranoid => true - } - end - - # - # For notes on advanced ways to set server-specific options, see - # http://railsware.com/blog/2011/11/02/advanced-server-definitions-in-capistrano/ - # - def node_options(node) - #password_proc = Proc.new {Capistrano::CLI.password_prompt "Root SSH password for #{node.name}"} # only called if needed - { - #:password => password_proc, - :ssh_options => { - :host_key_alias => node.name, - :host_name => node.ip_address, - :port => node.ssh.port - } - } - end - - def new_capistrano - # load once the library files - @capistrano_enabled ||= begin - require 'capistrano' - require 'capistrano/cli' - require 'leap_cli/remote/plugin' - Capistrano.plugin :leap, LeapCli::Remote::Plugin - true - end - - # create capistrano instance - cap = Capistrano::Configuration.new - - # add tasks to capistrano instance - cap.load File.dirname(__FILE__) + '/../remote/tasks.rb' - - return cap - end def parse_node_list(nodes) if nodes.is_a? Config::Object diff --git a/lib/leap_cli/commands/vagrant.rb b/lib/leap_cli/commands/vagrant.rb new file mode 100644 index 0000000..587e5e1 --- /dev/null +++ b/lib/leap_cli/commands/vagrant.rb @@ -0,0 +1,72 @@ +require 'ipaddr' + +module LeapCli; module Commands + + desc 'Bring up one or more local virtual machines' + arg_name '[node-filter]', :optional => true, :multiple => false + command :'local-up' do |c| + c.action do |global_options,options,args| + vagrant_command("up", args) + end + end + + desc 'Halt one or more local virtual machines' + arg_name '[node-filter]', :optional => true, :multiple => false + command :'local-down' do |c| + c.action do |global_options,options,args| + vagrant_command("halt", args) + end + end + + desc 'Destroy one or more local virtual machines' + arg_name '[node-filter]', :optional => true, :multiple => false + command :'local-reset' do |c| + c.action do |global_options,options,args| + vagrant_command("destroy", args) + end + end + + public + + def vagrant_ssh_key_file + file = File.expand_path('../../../vendor/vagrant_ssh_keys/vagrant.key', File.dirname(__FILE__)) + Util.assert_files_exist! file + return file + end + + private + + def vagrant_command(cmd, args) + create_vagrant_file + nodes = manager.filter(args)[:local => true].field(:name) + if nodes.any? + execute "cd #{File.dirname(Path.named_path(:vagrantfile))}; vagrant #{cmd} #{nodes.join(' ')}" + else + bail! "No nodes found" + end + end + + def execute(cmd) + progress2 "Running: #{cmd}" + exec cmd + end + + def create_vagrant_file + lines = [] + netmask = IPAddr.new('255.255.255.255').mask(manager.provider.vagrant.network.split('/').last).to_s + lines << %[Vagrant::Config.run do |config|] + manager.each_node do |node| + if node.vagrant? + lines << %[ config.vm.define :#{node.name} do |config|] + lines << %[ config.vm.box = "minimal-wheezy"] + lines << %[ config.vm.box_url = "http://cloud.github.com/downloads/leapcode/minimal-debian-vagrant/minimal-wheezy.box"] + lines << %[ config.vm.network :hostonly, "#{node.ip_address}", :netmask => "#{netmask}"] + lines << %[ end] + end + end + lines << %[end] + lines << "" + write_file! :vagrantfile, lines.join("\n") + end + +end; end \ No newline at end of file -- cgit v1.2.3