Module: LeapCli::Commands

Extended by:
Commands, Util, Util::RemoteCommand
Included in:
Commands
Defined in:
lib/leap_cli.rb,
lib/leap_cli/commands/ca.rb,
lib/leap_cli/commands/db.rb,
lib/leap_cli/commands/env.rb,
lib/leap_cli/commands/new.rb,
lib/leap_cli/commands/pre.rb,
lib/leap_cli/commands/ssh.rb,
lib/leap_cli/commands/list.rb,
lib/leap_cli/commands/node.rb,
lib/leap_cli/commands/test.rb,
lib/leap_cli/commands/user.rb,
lib/leap_cli/commands/util.rb,
lib/leap_cli/commands/clean.rb,
lib/leap_cli/commands/facts.rb,
lib/leap_cli/commands/deploy.rb,
lib/leap_cli/commands/compile.rb,
lib/leap_cli/commands/inspect.rb,
lib/leap_cli/commands/vagrant.rb,
lib/leap_cli/commands/node_init.rb

Overview

for commands in leap_cli/commands

Defined Under Namespace

Classes: NodeTable, TagTable

Instance Method Summary collapse

Methods included from Util

assert!, assert_bin!, assert_config!, assert_files_exist!, assert_files_missing!, assert_run!, bail!, cmd_exists?, current_git_branch, current_git_commit, dir_exists?, ensure_dir, erb_eval, exit_status, file_content_equals?, file_exists?, help!, is_git_directory?, long_running, pty_run, quit!, read_file, read_file!, relative_symlink, remove_directory!, remove_file!, rename_file!, replace_file!, write_file!

Methods included from Util::RemoteCommand

ssh_connect

Instance Method Details

#assert_valid_node_name!(name, local = false) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/leap_cli/commands/node.rb', line 156

def assert_valid_node_name!(name, local=false)
  assert! name, 'No <node-name> specified.'
  if local
    assert! name =~ /^[0-9a-z]+$/, "illegal characters used in node name '#{name}' (note: Vagrant does not allow hyphens or underscores)"
  else
    assert! name =~ /^[0-9a-z-]+$/, "illegal characters used in node name '#{name}' (note: Linux does not allow underscores)"
  end
end

#format_seed_value(v) ⇒ Object

conversions:

"x,y,z" => ["x","y","z"]

"22" => 22

"5.1" => 5.1


129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/leap_cli/commands/node.rb', line 129

def format_seed_value(v)
  if v =~ /,/
    v = v.split(',')
    v.map! do |i|
      i = i.to_i if i.to_i.to_s == i
      i = i.to_f if i.to_f.to_s == i
      i
    end
  else
    v = v.to_i if v.to_i.to_s == v
    v = v.to_f if v.to_f.to_s == v
  end
  return v
end

#get_node_from_args(args, options = {}) ⇒ Object

PUBLIC HELPERS



84
85
86
87
88
89
90
91
92
# File 'lib/leap_cli/commands/node.rb', line 84

def get_node_from_args(args, options={})
  node_name = args.first
  node = manager.node(node_name)
  if node.nil? && options[:include_disabled]
    node = manager.disabled_node(node_name)
  end
  assert!(node, "Node '#{node_name}' not found.")
  node
end

#numbered_choice_menu(msg, items, &block) ⇒ Object

keeps prompting the user for a numbered choice, until they pick a good one or bail out.

block is yielded and is responsible for rendering the choices.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/leap_cli/commands/util.rb', line 16

def numbered_choice_menu(msg, items, &block)
  while true
    say("\n" + msg + ':')
    items.each_with_index &block
    say("q. quit")
    index = ask("number 1-#{items.length}> ")
    if index.empty?
      next
    elsif index =~ /q/
      bail!
    else
      i = index.to_i - 1
      if i < 0 || i >= items.length
        bail!
      else
        return i
      end
    end
  end
end

#parse_node_list(nodes) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/leap_cli/commands/util.rb', line 38

def parse_node_list(nodes)
  if nodes.is_a? Config::Object
    Config::ObjectList.new(nodes)
  elsif nodes.is_a? Config::ObjectList
    nodes
  elsif nodes.is_a? String
    manager.filter!(nodes)
  else
    bail! "argument error"
  end
end

#path(name) ⇒ Object



7
8
9
# File 'lib/leap_cli/commands/util.rb', line 7

def path(name)
  Path.named_path(name)
end

#pick_pgp_keyObject

let the the user choose among the gpg public keys that we encounter, or just pick the key if there is only one.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/leap_cli/commands/user.rb', line 101

def pick_pgp_key
  begin
    require 'gpgme'
  rescue LoadError
    log "Skipping OpenPGP setup because gpgme is not installed."
    return
  end

  secret_keys = GPGME::Key.find(:secret)
  if secret_keys.empty?
    log "Skipping OpenPGP setup because I could not find any OpenPGP keys for you"
    return nil
  end

  secret_keys.select!{|key| !key.expired}

  if secret_keys.length > 1
    key_index = numbered_choice_menu('Choose your OpenPGP public key', secret_keys) do |key, i|
      key_info = key.to_s.split("\n")[0..1].map{|line| line.sub(/^\s*(sec|uid)\s*/,'')}.join(' -- ')
      say("#{i+1}. #{key_info}")
    end
  else
    key_index = 0
  end

  key_id = secret_keys[key_index].sha

  # can't use this, it includes signatures:
  #puts GPGME::Key.export(key_id, :armor => true, :export_options => :export_minimal)

  # export with signatures removed:
  return `gpg --armor --export-options export-minimal --export #{key_id}`.strip
end

#pick_ssh_keyObject

let the the user choose among the ssh public keys that we encounter, or just pick the key if there is only one.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/leap_cli/commands/user.rb', line 68

def pick_ssh_key
  ssh_keys = []
  Dir.glob("#{ENV['HOME']}/.ssh/*.pub").each do |keyfile|
    ssh_keys << SshKey.load(keyfile)
  end

  if `which ssh-add`.strip.any?
    `ssh-add -L 2> /dev/null`.split("\n").compact.each do |line|
      key = SshKey.load(line)
      if key
        key.comment = 'ssh-agent'
        ssh_keys << key unless ssh_keys.include?(key)
      end
    end
  end
  ssh_keys.compact!

  assert! ssh_keys.any?, 'Sorry, could not find any SSH public key for you. Have you run ssh-keygen?'

  if ssh_keys.length > 1
    key_index = numbered_choice_menu('Choose your SSH public key', ssh_keys.collect(&:summary)) do |line, i|
      say("#{i+1}. #{line}")
    end
  else
    key_index = 0
  end

  return ssh_keys[key_index]
end

#remove_node_files(node_name) ⇒ Object



114
115
116
117
118
# File 'lib/leap_cli/commands/node.rb', line 114

def remove_node_files(node_name)
  (Leap::Platform.node_files + [:node_files_dir]).each do |path|
    remove_file! [path, node_name]
  end
end

#seed_node_data(node, args) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/leap_cli/commands/node.rb', line 94

def seed_node_data(node, args)
  args.each do |seed|
    key, value = seed.split(':')
    value = format_seed_value(value)
    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

#vagrant_ssh_key_fileObject

returns the path to a vagrant ssh key file.

if the vagrant.key file is owned by root or ourselves, then we need to make sure that it owned by us and not world readable.



74
75
76
77
78
79
80
81
82
83
# File 'lib/leap_cli/commands/vagrant.rb', line 74

def vagrant_ssh_key_file
  file_path = File.expand_path('../../../vendor/vagrant_ssh_keys/vagrant.key', File.dirname(__FILE__))
  Util.assert_files_exist! file_path
  uid = File.new(file_path).stat.uid
  if uid == 0 || uid == Process.euid
    FileUtils.install file_path, '/tmp/vagrant.key', :mode => 0600
    file_path = '/tmp/vagrant.key'
  end
  return file_path
end

#validate_ip_address(node) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/leap_cli/commands/node.rb', line 144

def validate_ip_address(node)
  IPAddr.new(node['ip_address'])
rescue ArgumentError
  bail! do
    if node['ip_address']
      log :invalid, "ip_address #{node['ip_address'].inspect}"
    else
      log :missing, "ip_address"
    end
  end
end