aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/node.rb
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2012-12-08 20:02:27 -0800
committerelijah <elijah@riseup.net>2012-12-08 20:02:27 -0800
commit8572dfd59c21d2032b030adc9dc9a973c6e1c3f5 (patch)
tree3d3fb221339ddf4fa0037cbd88133431307f2e4e /lib/leap_cli/commands/node.rb
parent155d744f6c6f94709925f0674f510b3064b6608e (diff)
downloadleap_cli-8572dfd59c21d2032b030adc9dc9a973c6e1c3f5.tar.gz
leap_cli-8572dfd59c21d2032b030adc9dc9a973c6e1c3f5.tar.bz2
added commands 'node add' 'node rm' and 'node mv'
Diffstat (limited to 'lib/leap_cli/commands/node.rb')
-rw-r--r--lib/leap_cli/commands/node.rb74
1 files changed, 67 insertions, 7 deletions
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