diff options
author | elijah <elijah@riseup.net> | 2012-12-08 20:02:27 -0800 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2012-12-08 20:02:27 -0800 |
commit | 8572dfd59c21d2032b030adc9dc9a973c6e1c3f5 (patch) | |
tree | 3d3fb221339ddf4fa0037cbd88133431307f2e4e /lib/leap_cli/commands | |
parent | 155d744f6c6f94709925f0674f510b3064b6608e (diff) |
added commands 'node add' 'node rm' and 'node mv'
Diffstat (limited to 'lib/leap_cli/commands')
-rw-r--r-- | lib/leap_cli/commands/deploy.rb | 4 | ||||
-rw-r--r-- | lib/leap_cli/commands/node.rb | 74 | ||||
-rw-r--r-- | lib/leap_cli/commands/pre.rb | 5 | ||||
-rw-r--r-- | lib/leap_cli/commands/vagrant.rb | 39 |
4 files changed, 100 insertions, 22 deletions
diff --git a/lib/leap_cli/commands/deploy.rb b/lib/leap_cli/commands/deploy.rb index bee09a0..f94465f 100644 --- a/lib/leap_cli/commands/deploy.rb +++ b/lib/leap_cli/commands/deploy.rb @@ -16,6 +16,10 @@ module LeapCli end end + nodes.each_node do |node| + assert_files_exist! Path.named_path([:hiera, node.name]), :msg => 'try running `leap compile`' + end + ssh_connect(nodes) do |ssh| ssh.leap.assert_initialized diff --git a/lib/leap_cli/commands/node.rb b/lib/leap_cli/commands/node.rb index 9bf27e2..f208f87 100644 --- a/lib/leap_cli/commands/node.rb +++ b/lib/leap_cli/commands/node.rb @@ -10,16 +10,41 @@ module LeapCli; module Commands desc 'Node management' command :node do |node| node.desc 'Create a new configuration file for a node' + node.long_desc ["If specified, the optional argument seed-options can be used to seed values in the node configuration file.", + "The format is property_name:value.", + "For example: `leap node add web1 ip_address:1.2.3.4 services:webapp`.", + "To set nested properties, property name can contain '.', like so: `leap node add web1 ssh.port:44`", + "To set multiple values for a single property, use ',', like so: `leap node add mynode services:webapp,dns`"].join("\n\n") + node.arg_name '<node-name> [seed-options]' # , :optional => false, :multiple => false node.command :add do |add| + add.switch :local, :desc => 'Make a local testing node (by automatically assigning the next available local IP address). Local nodes are run as virtual machines on your computer.', :negatable => false add.action do |global_options,options,args| - log 'not yet implemented' + # argument sanity checks + name = args.first + assert! name, 'No <node-name> specified.' + assert! name =~ /^[0-9a-z_-]+$/, "illegal characters used in node name '#{name}'" + assert_files_missing! [:node_config, node.name] + + # create and seed new node + node = Config::Object.new + if options[:local] + node['ip_address'] = pick_next_vagrant_ip_address + end + seed_node_data(node, args[1..-1]) + + # write the file + write_file! [:node_config, name], node.dump_json + "\n" end end - node.desc 'Bootstraps a node, setting up ssh keys and installing prerequisites' - node.arg_name 'node-name', :optional => false, :multiple => false + node.desc 'Bootstraps a node, setting up SSH keys and installing prerequisite packages' + node.long_desc "This command prepares a server to be used with the LEAP Platform by saving the server's SSH host key, " + + "copying the authorized_keys file, and installing packages that are required for deploying. " + + "Node init must be run before deploying to a server, and the server must be running and available via the network. " + + "This command only needs to be run once, but there is no harm in running it multiple times." + node.arg_name '<node-name>' #, :optional => false, :multiple => false node.command :init do |init| - init.switch 'echo', :desc => 'if set, passwords are visible as you type them (default is hidden)', :negatable => false + init.switch 'echo', :desc => 'If set, passwords are visible as you type them (default is hidden)', :negatable => false init.action do |global_options,options,args| node = get_node_from_args(args) ping_node(node) @@ -34,17 +59,30 @@ module LeapCli; module Commands end node.desc 'Renames a node file, and all its related files' + node.arg_name '<old-name> <new-name>' node.command :mv do |mv| mv.action do |global_options,options,args| - log 'not yet implemented' + node = get_node_from_args(args) + new_name = args.last + ensure_dir [:node_files_dir, new_name] + Path::NODE_PATHS.each do |path| + rename_file! [path, node.name], [path, new_name] + end + remove_directory! [:node_files_dir, node.name] end end node.desc 'Removes a node file, and all its related files' - node.arg_name '<node-name>', :optional => false, :multiple => false + node.arg_name '<node-name>' #:optional => false #, :multiple => false node.command :rm do |rm| rm.action do |global_options,options,args| - log 'not yet implemented' + node = get_node_from_args(args) + (Path::NODE_PATHS + [:node_files_dir]).each do |path| + remove_file! [path, node.name] + end + if node.vagrant? + vagrant_command("destroy --force", [node.name]) + end end end end @@ -135,4 +173,26 @@ module LeapCli; module Commands assert_run!("ping -W 1 -c 1 #{node.ip_address}", "Could not ping #{node.name} (address #{node.ip_address}). Try again, we only send a single ping.") end + def seed_node_data(node, args) + args.each do |seed| + key, value = seed.split(':') + if value =~ /,/ + value = value.split(',') + end + assert! key =~ /^[0-9a-z\._]+$/, "illegal characters used in property '#{key}'" + if key =~ /\./ + key_parts = key.split('.') + final_key = key_parts.pop + current_object = node + key_parts.each do |key_part| + current_object[key_part] = Config::Object.new + current_object = current_object[key_part] + end + current_object[final_key] = value + else + node[key] = value + end + end + end + end; end
\ No newline at end of file diff --git a/lib/leap_cli/commands/pre.rb b/lib/leap_cli/commands/pre.rb index cae787e..346814b 100644 --- a/lib/leap_cli/commands/pre.rb +++ b/lib/leap_cli/commands/pre.rb @@ -40,6 +40,11 @@ module LeapCli end # + # load all the nodes everything + # + manager + + # # check requirements # REQUIREMENTS.each do |key| diff --git a/lib/leap_cli/commands/vagrant.rb b/lib/leap_cli/commands/vagrant.rb index cd3e71b..a9c2928 100644 --- a/lib/leap_cli/commands/vagrant.rb +++ b/lib/leap_cli/commands/vagrant.rb @@ -10,7 +10,6 @@ module LeapCli; module Commands local.arg_name 'node-filter', :optional => true #, :multiple => false local.command :start do |start| start.action do |global_options,options,args| - vagrant_setup vagrant_command(["up", "sandbox on"], args) end end @@ -19,7 +18,6 @@ module LeapCli; module Commands local.arg_name 'node-filter', :optional => true #, :multiple => false local.command :stop do |stop| stop.action do |global_options,options,args| - vagrant_setup vagrant_command("halt", args) end end @@ -28,7 +26,6 @@ module LeapCli; module Commands local.arg_name 'node-filter', :optional => true #, :multiple => false local.command :reset do |reset| reset.action do |global_options,options,args| - vagrant_setup vagrant_command("sandbox rollback", args) end end @@ -37,7 +34,6 @@ module LeapCli; module Commands local.arg_name 'node-filter', :optional => true #, :multiple => false local.command :destroy do |destroy| destroy.action do |global_options,options,args| - vagrant_setup vagrant_command("destroy", args) end end @@ -46,7 +42,6 @@ module LeapCli; module Commands local.arg_name 'node-filter', :optional => true #, :multiple => false local.command :status do |status| status.action do |global_options,options,args| - vagrant_setup vagrant_command("status", args) end end @@ -66,18 +61,10 @@ module LeapCli; module Commands return file_path end - private - - def vagrant_setup - assert_bin! 'vagrant', 'run "sudo gem install vagrant"' - unless `vagrant gem which sahara`.chars.any? - log :installing, "vagrant plugin 'sahara'" - assert_run! 'vagrant gem install sahara' - end - create_vagrant_file - end + protected def vagrant_command(cmds, args) + vagrant_setup cmds = cmds.to_a assert_config! 'provider.vagrant.network' if args.empty? @@ -99,6 +86,17 @@ module LeapCli; module Commands end end + private + + def vagrant_setup + assert_bin! 'vagrant', 'run "sudo gem install vagrant"' + unless `vagrant gem which sahara`.chars.any? + log :installing, "vagrant plugin 'sahara'" + assert_run! 'vagrant gem install sahara' + end + create_vagrant_file + end + def execute(cmd) log 2, :run, cmd exec cmd @@ -123,4 +121,15 @@ module LeapCli; module Commands write_file! :vagrantfile, lines.join("\n") end + def pick_next_vagrant_ip_address + taken_ips = manager.nodes[:local => true].field(:ip_address) + if taken_ips.any? + highest_ip = taken_ips.map{|ip| IPAddr.new(ip)}.max + new_ip = highest_ip.succ + else + new_ip = IPAddr.new(manager.provider.vagrant.network).succ.succ + end + return new_ip.to_s + end + end; end
\ No newline at end of file |