knife-solo 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,9 +2,11 @@ require 'pathname'
2
2
 
3
3
  require 'chef/knife'
4
4
  require 'chef/config'
5
+ require 'chef/cookbook/chefignore'
5
6
 
6
7
  require 'knife-solo/ssh_command'
7
8
  require 'knife-solo/kitchen_command'
9
+ require 'knife-solo/tools'
8
10
 
9
11
  class Chef
10
12
  class Knife
@@ -13,6 +15,7 @@ class Chef
13
15
  class Cook < Knife
14
16
  include KnifeSolo::SshCommand
15
17
  include KnifeSolo::KitchenCommand
18
+ include KnifeSolo::Tools
16
19
 
17
20
  banner "knife cook [user@]hostname [json] (options)"
18
21
 
@@ -21,24 +24,34 @@ class Chef
21
24
  :boolean => true,
22
25
  :description => "Skip the version check on the Chef gem"
23
26
 
27
+ option :sync_only,
28
+ :long => '--sync-only',
29
+ :boolean => false,
30
+ :description => "Only sync the cookbook - do not run Chef"
31
+
32
+ option :skip_syntax_check,
33
+ :long => '--skip-syntax-check',
34
+ :boolean => true,
35
+ :description => "Skip Ruby syntax checks"
36
+
24
37
  def run
25
38
  super
26
- check_syntax
39
+ check_syntax unless config[:skip_syntax_check]
27
40
  Chef::Config.from_file('solo.rb')
28
41
  check_chef_version unless config[:skip_chef_check]
29
42
  rsync_kitchen
30
43
  add_patches
31
- cook
44
+ cook unless config[:sync_only]
32
45
  end
33
46
 
34
47
  def check_syntax
35
48
  ui.msg('Checking cookbook syntax...')
36
- Dir["**/*.rb"].each do |recipe|
49
+ chefignore.remove_ignores_from(Dir["**/*.rb"]).each do |recipe|
37
50
  ok = system "ruby -c #{recipe} >/dev/null 2>&1"
38
51
  raise "Syntax error in #{recipe}" if not ok
39
52
  end
40
53
 
41
- Dir["**/*.json"].each do |json|
54
+ chefignore.remove_ignores_from(Dir["**/*.json"]).each do |json|
42
55
  begin
43
56
  require 'json'
44
57
  # parse without instantiating Chef classes
@@ -57,31 +70,55 @@ class Chef
57
70
  Chef::Config.file_cache_path
58
71
  end
59
72
 
73
+ def chefignore
74
+ @chefignore ||= ::Chef::Cookbook::Chefignore.new("./")
75
+ end
76
+
77
+ # cygwin rsync path must be adjusted to work
78
+ def adjust_rsync_path(path)
79
+ return path unless windows_node?
80
+ path.gsub(/^(\w):/) { "/cygdrive/#{$1}" }
81
+ end
82
+
60
83
  def patch_path
61
84
  Array(Chef::Config.cookbook_path).first + "/chef_solo_patches/libraries"
62
85
  end
63
86
 
87
+ def rsync_exclude
88
+ (%w{revision-deploys tmp '.*'} + chefignore.ignores).uniq
89
+ end
90
+
64
91
  def rsync_kitchen
65
- system %Q{rsync -rlP --rsh="ssh #{ssh_args}" --delete --exclude '.*' ./ :#{chef_path}}
92
+ system! %Q{rsync -rl --rsh="ssh #{ssh_args}" --delete #{rsync_exclude.collect{ |ignore| "--exclude #{ignore} " }.join} ./ :#{adjust_rsync_path(chef_path)}}
66
93
  end
67
94
 
68
95
  def add_patches
69
- run_command "mkdir -p #{patch_path}"
96
+ run_portable_mkdir_p(patch_path)
70
97
  Dir[Pathname.new(__FILE__).dirname.join("patches", "*.rb")].each do |patch|
71
- system %Q{rsync -rlP --rsh="ssh #{ssh_args}" #{patch} :#{patch_path}}
98
+ system! %Q{rsync -rl --rsh="ssh #{ssh_args}" #{patch} :#{adjust_rsync_path(patch_path)}}
72
99
  end
73
100
  end
74
101
 
75
102
  def check_chef_version
76
103
  constraint = "~>0.10.4"
77
104
  result = run_command <<-BASH
78
- ruby -rubygems -e "gem 'chef', '#{constraint}'"
105
+ opscode_ruby="/opt/opscode/embedded/bin/ruby"
106
+
107
+ if command -v $opscode_ruby &>/dev/null
108
+ then
109
+ ruby_bin=$opscode_ruby
110
+ else
111
+ ruby_bin="ruby"
112
+ fi
113
+
114
+ $ruby_bin -rubygems -e "gem 'chef', '#{constraint}'"
79
115
  BASH
80
116
  raise "The chef gem on #{host} is out of date. Please run `#{$0} prepare #{ssh_args}` to upgrade Chef to #{constraint}." unless result.success?
81
117
  end
82
118
 
83
119
  def cook
84
120
  logging_arg = "-l debug" if config[:verbosity] > 0
121
+
85
122
  stream_command <<-BASH
86
123
  sudo chef-solo -c #{chef_path}/solo.rb \
87
124
  -j #{chef_path}/#{node_config} \
@@ -2,7 +2,7 @@ class OperatingSystemNotSupportedError < StandardError ; end
2
2
 
3
3
  module KnifeSolo
4
4
  module Bootstraps
5
- class OperatingSystemNotImplementedError < StandardError
5
+ class OperatingSystemNotImplementedError < StandardError
6
6
  end
7
7
 
8
8
  def self.class_exists_for?(os_name)
@@ -55,6 +55,11 @@ module KnifeSolo
55
55
  "wget #{url}"
56
56
  end
57
57
 
58
+ def omnibus_install
59
+ run_command(http_client_get_url("http://opscode.com/chef/install.sh"))
60
+ run_command("sudo bash install.sh")
61
+ end
62
+
58
63
  def gem_install
59
64
  ui.msg "Installing rubygems from source..."
60
65
  release = "rubygems-1.8.10"
@@ -81,7 +86,7 @@ module KnifeSolo
81
86
  # run right before we run #{distro[:type]}_install method
82
87
  # barf out here if need be
83
88
  end
84
-
89
+
85
90
  end # Bootstraps
86
91
  end
87
92
 
@@ -2,7 +2,7 @@ module KnifeSolo::Bootstraps
2
2
  class Linux < Base
3
3
 
4
4
  def issue
5
- prepare.run_command("cat /etc/issue").stdout.strip
5
+ prepare.run_command("cat /etc/issue").stdout.strip || perepare.run_command("lsb_release -d -s").stdout.strip
6
6
  end
7
7
 
8
8
  def package_list
@@ -26,6 +26,7 @@ module KnifeSolo::Bootstraps
26
26
  def emerge_gem_install
27
27
  ui.msg("Installing required packages...")
28
28
  run_command("sudo USE='-test' ACCEPT_KEYWORDS='~amd64' emerge -u chef")
29
+ gem_install
29
30
  end
30
31
 
31
32
  def add_yum_repos(repo_path)
@@ -71,32 +72,35 @@ module KnifeSolo::Bootstraps
71
72
  return @distro if @distro
72
73
  @distro = case issue
73
74
  when %r{Debian GNU/Linux 5}
74
- {:type => "debian_gem", :version => "lenny"}
75
+ {:type => "omnibus", :version => "lenny"}
75
76
  when %r{Debian GNU/Linux 6}
76
- {:type => "debian_gem", :version => "squeeze"}
77
+ {:type => "omnibus", :version => "squeeze"}
77
78
  when %r{Debian GNU/Linux wheezy}
78
79
  {:type => "debian_gem", :version => "wheezy"}
79
80
  when %r{Ubuntu}
80
81
  version = run_command("lsb_release -cs").stdout.strip
81
82
  {:type => "debian_gem", :version => version}
83
+ when %r{Linaro}
84
+ version = run_command("lsb_release -cs").stdout.strip
85
+ {:type => "debian_gem", :version => version}
82
86
  when %r{CentOS.*? 5}
83
- {:type => "yum", :version => "RHEL5"}
87
+ {:type => "omnibus", :version => "RHEL5"}
84
88
  when %r{CentOS.*? 6}
85
- {:type => "yum", :version => "RHEL6"}
89
+ {:type => "omnibus", :version => "RHEL6"}
86
90
  when %r{Red Hat Enterprise.*? 5}
87
- {:type => "yum", :version => "RHEL5"}
91
+ {:type => "omnibus", :version => "RHEL5"}
88
92
  when %r{Red Hat Enterprise.*? 6}
89
- {:type => "yum", :version => "RHEL6"}
93
+ {:type => "omnibus", :version => "RHEL6"}
90
94
  when %r{Scientific Linux.*? 5}
91
- {:type => "yum", :version => "RHEL5"}
95
+ {:type => "omnibus", :version => "RHEL5"}
92
96
  when %r{Scientific Linux.*? 6}
93
- {:type => "yum", :version => "RHEL6"}
97
+ {:type => "omnibus", :version => "RHEL6"}
94
98
  when %r{SUSE Linux Enterprise Server 11 SP1}
95
99
  {:type => "zypper_gem", :version => "SLES11"}
96
100
  when %r{openSUSE 11.4}
97
101
  {:type => "zypper_gem", :version => "openSUSE"}
98
- when %r{This is \\n\.\\O (\\s \\m \\r) \\t}
99
- {:type => "gentoo_gem", :version => "Gentoo"}
102
+ when %r{This is \\n\.\\O \(\\s \\m \\r\) \\t}
103
+ {:type => "emerge_gem", :version => "Gentoo"}
100
104
  else
101
105
  raise "Distro not recognized from looking at /etc/issue. Please fork https://github.com/matschaffer/knife-solo and add support for your distro."
102
106
  end
@@ -1,5 +1,5 @@
1
1
  module KnifeSolo
2
2
  def self.version
3
- '0.0.8'
3
+ '0.0.9'
4
4
  end
5
5
  end
@@ -9,8 +9,8 @@ module KnifeSolo
9
9
  end
10
10
 
11
11
  option :ssh_config,
12
- :short => "-F configfile",
13
- :long => "--ssh-config-file configfile",
12
+ :short => "-F CONFIG_FILE",
13
+ :long => "--ssh-config-file CONFIG_FILE",
14
14
  :description => "Alternate location for ssh config file"
15
15
 
16
16
  option :ssh_password,
@@ -24,8 +24,8 @@ module KnifeSolo
24
24
  :description => "The ssh identity file"
25
25
 
26
26
  option :ssh_port,
27
- :short => "-p FILE",
28
- :long => "--ssh-port FILE",
27
+ :short => "-p PORT",
28
+ :long => "--ssh-port PORT",
29
29
  :description => "The ssh port"
30
30
 
31
31
  option :startup_script,
@@ -134,6 +134,13 @@ module KnifeSolo
134
134
  end
135
135
  end
136
136
 
137
+ def windows_node?
138
+ return @windows_node unless @windows_node.nil?
139
+ @windows_node = run_command('ver', :process_sudo => false).stdout =~ /Windows/i
140
+ Chef::Log.debug("Windows node detected") if @windows_node
141
+ @windows_node
142
+ end
143
+
137
144
  def sudo_available?
138
145
  return @sudo_available unless @sudo_available.nil?
139
146
  @sudo_available = run_command('sudo -V', :process_sudo => false).success?
@@ -209,5 +216,18 @@ module KnifeSolo
209
216
  end
210
217
  result
211
218
  end
219
+
220
+ # TODO:
221
+ # - move this to a dedicated "portability" module?
222
+ # - use ruby in all cases instead?
223
+ def run_portable_mkdir_p(folder)
224
+ if windows_node?
225
+ # no mkdir -p on windows - fake it
226
+ run_command %Q{ruby -e "require 'fileutils'; FileUtils.mkdir_p('#{folder}')"}
227
+ else
228
+ run_command "mkdir -p #{folder}"
229
+ end
230
+ end
231
+
212
232
  end
213
233
  end
@@ -0,0 +1,7 @@
1
+ module KnifeSolo
2
+ module Tools
3
+ def system!(command)
4
+ raise "Failed to launch command #{command}" unless system(command)
5
+ end
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-solo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-10 00:00:00.000000000Z
12
+ date: 2012-05-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70146380288440 !ruby/object:Gem::Requirement
16
+ requirement: &70342129968000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70146380288440
24
+ version_requirements: *70342129968000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &70146380288020 !ruby/object:Gem::Requirement
27
+ requirement: &70342129967580 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,21 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70146380288020
36
- - !ruby/object:Gem::Dependency
37
- name: virtualbox
38
- requirement: &70146380287600 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
44
- type: :development
45
- prerelease: false
46
- version_requirements: *70146380287600
35
+ version_requirements: *70342129967580
47
36
  - !ruby/object:Gem::Dependency
48
37
  name: chef
49
- requirement: &70146380287100 !ruby/object:Gem::Requirement
38
+ requirement: &70342129967080 !ruby/object:Gem::Requirement
50
39
  none: false
51
40
  requirements:
52
41
  - - ~>
@@ -54,18 +43,21 @@ dependencies:
54
43
  version: 0.10.0
55
44
  type: :runtime
56
45
  prerelease: false
57
- version_requirements: *70146380287100
46
+ version_requirements: *70342129967080
58
47
  - !ruby/object:Gem::Dependency
59
48
  name: net-ssh
60
- requirement: &70146380286600 !ruby/object:Gem::Requirement
49
+ requirement: &70342129966560 !ruby/object:Gem::Requirement
61
50
  none: false
62
51
  requirements:
63
- - - ~>
52
+ - - ! '>='
64
53
  - !ruby/object:Gem::Version
65
54
  version: 2.1.3
55
+ - - <
56
+ - !ruby/object:Gem::Version
57
+ version: 2.3.0
66
58
  type: :runtime
67
59
  prerelease: false
68
- version_requirements: *70146380286600
60
+ version_requirements: *70342129966560
69
61
  description: Handles bootstrapping, running chef solo, rsyncing cookbooks etc
70
62
  email: mat@schaffer.me
71
63
  executables: []
@@ -83,6 +75,7 @@ files:
83
75
  - lib/knife-solo/info.rb
84
76
  - lib/knife-solo/kitchen_command.rb
85
77
  - lib/knife-solo/ssh_command.rb
78
+ - lib/knife-solo/tools.rb
86
79
  - lib/knife-solo.rb
87
80
  homepage: https://github.com/matschaffer/knife-solo
88
81
  licenses: []
@@ -104,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
97
  version: '0'
105
98
  requirements: []
106
99
  rubyforge_project: nowarning
107
- rubygems_version: 1.8.15
100
+ rubygems_version: 1.8.17
108
101
  signing_key:
109
102
  specification_version: 3
110
103
  summary: A collection of knife plugins for dealing with chef solo