lisausa-knife-plugins 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,16 +2,25 @@ require_relative '../../core_ext'
2
2
  require 'chef/knife'
3
3
 
4
4
  class LisausaKnifePlugins::SetupSsh < ::Chef::Knife
5
- banner 'knife setup ssh'
5
+ banner 'knife setup ssh (options)'
6
6
 
7
7
  deps do
8
8
  require 'chef/shef/ext'
9
9
  end
10
10
 
11
+ option :write, long: '--write-file', boolean: true,
12
+ description: 'Write the config to your ~/.ssh/config (CAREFUL!)'
13
+
11
14
  # This method will be executed when you run this knife command.
12
15
  def run
13
16
  Shef::Extensions.extend_context_object(self)
14
- config = []
17
+ ssh_config = []
18
+
19
+ ssh_config << "### BEGIN KNIFE BLOCK ###"
20
+ ssh_config << "## This was generated by `knife setup ssh`:"
21
+
22
+ STDOUT.sync = true
23
+
15
24
  nodes.all do |n|
16
25
  next if /vagrant/.match(n.name)
17
26
  name = n.name
@@ -19,17 +28,46 @@ class LisausaKnifePlugins::SetupSsh < ::Chef::Knife
19
28
 
20
29
  begin
21
30
  hostname = n.ipaddress
22
- rescue
23
- hostname = nil
31
+ rescue => ex
32
+ ui.warn("Error (#{ex.inspect}) while getting #ipaddress for #{n.name}")
33
+ next
24
34
  end
25
- config << <<-END.strip_heredoc
26
- Host #{name}
27
- HostName #{hostname}
28
- END
35
+
36
+ ssh_config << [
37
+ "Host #{name}",
38
+ " HostName #{hostname}",
39
+ " HostKeyAlias #{[name,hostname,n.macaddress].join('-')}"
40
+ ]
29
41
  end
30
42
 
31
- ui.msg "Copy and paste the following into your ~/.ssh/config file:"
32
- puts "## This is generated by `knife setup ssh`:\n\n#{config.join("\n")}"
43
+ if (c = Chef::Config.knife).keys.grep(/identity_file|ssh_user/).any?
44
+ ssh_config.push [
45
+ "Host *.lisausa.net",
46
+ " IdentitiesOnly yes",
47
+ " PasswordAuthentication no"
48
+ ]
49
+ ssh_config.push " IdentityFile #{c[:identity_file]}" if c[:identity_file]
50
+ ssh_config.push " User #{c[:ssh_user]}" if c[:ssh_user]
51
+ end
52
+
53
+ ssh_config << "### END KNIFE BLOCK ###"
54
+ ssh_config = ssh_config.flatten.join("\n")
55
+
56
+ file_path = File.join(ENV['HOME'], '.ssh', 'config')
57
+ if config[:write] or ui.ask_question("Write config to #{file_path} (Y/N)?", default: 'N').downcase == 'y'
58
+ FileUtils.copy_file(file_path, "#{file_path}~")
59
+ File.open(file_path, File::RDWR|File::CREAT) do |f|
60
+ f.flock(File::LOCK_EX)
61
+ contents = f.read.gsub(/### BEGIN KNIFE BLOCK ###.+?(### END KNIFE BLOCK ###|\Z)/m, ssh_config)
62
+ f.rewind
63
+ f.truncate(0)
64
+ f.write contents
65
+ end
66
+ ui.msg "Wrote to #{file_path}. Previous contents were backed up to #{file_path}~"
67
+ else
68
+ ui.msg "Copy and paste the following into your #{file_path} file:"
69
+ ui.msg ssh_config
70
+ end
33
71
  end
34
72
 
35
73
  end
@@ -1,3 +1,3 @@
1
1
  module LisausaKnifePlugins
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -25,4 +25,5 @@ Gem::Specification.new do |gem|
25
25
  gem.add_dependency 'chef'
26
26
 
27
27
  gem.add_development_dependency 'rake'
28
+ gem.add_development_dependency 'pry'
28
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lisausa-knife-plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-07 00:00:00.000000000 Z
12
+ date: 2013-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: pry
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  description: ! ' These are plugins for the Chef utility `knife`, for use by LISAUSA
47
63
  employees '
48
64
  email:
@@ -74,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
90
  version: '0'
75
91
  segments:
76
92
  - 0
77
- hash: -4271793852436981644
93
+ hash: -1816767679144316471
78
94
  required_rubygems_version: !ruby/object:Gem::Requirement
79
95
  none: false
80
96
  requirements:
@@ -83,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
99
  version: '0'
84
100
  segments:
85
101
  - 0
86
- hash: -4271793852436981644
102
+ hash: -1816767679144316471
87
103
  requirements: []
88
104
  rubyforge_project:
89
105
  rubygems_version: 1.8.23