Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chefstyle -a #478

Merged
merged 3 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ group :test do
gem "coveralls", require: false
gem "minitest", "~> 5.8"
gem "rake", "~> 10"
gem "chefstyle"
gem "chefstyle", "0.13.2"
gem "simplecov", "~> 0.10"
gem "concurrent-ruby", "~> 1.0"
gem "pry-byebug"
Expand All @@ -19,7 +19,7 @@ group :test do
end

group :integration do
gem "berkshelf", ">= 6.3.4"
gem "berkshelf", "~> 6.3.0"
gem "test-kitchen", ">= 1.2.4"
gem "kitchen-vagrant"
gem "ed25519" # ed25519 ssh key support
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RuboCop::RakeTask.new(:lint) do |task|
end
#
# run tests
task default: [:test, :lint]
task default: %i{test lint}

Rake::TestTask.new do |t|
t.libs << "test"
Expand Down Expand Up @@ -59,7 +59,7 @@ namespace :test do

sh_cmd = "cd #{path} && target=#{args[:target]} key_files=#{key_files}"

sh_cmd += " debug=#{ENV['debug']}" if ENV["debug"]
sh_cmd += " debug=#{ENV["debug"]}" if ENV["debug"]
sh_cmd += " ruby -I ../../lib test_ssh.rb tests/"
sh_cmd += ENV["test"] || "*"

Expand Down
4 changes: 2 additions & 2 deletions contrib/fixup_requiretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

log "Warning: a sudoers configuration for user #{user} already exists, "\
"doing nothing (override with TRAIN_SUDO_VERY_MUCH=yes)" do
only_if "test -f #{sudoer} || grep #{user} /etc/sudoers"
end
only_if "test -f #{sudoer} || grep #{user} /etc/sudoers"
end

file sudoer do
content "#{user} ALL=(root) NOPASSWD:ALL\n"\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
end

# Since this is a Local-type connection, we MUST implement these three.
[
:file_via_connection,
:run_command_via_connection,
].each do |method_name|
%i{
file_via_connection
run_command_via_connection
}.each do |method_name|
it "should provide a #{method_name}() method" do
# false passed to instance_methods says 'don't use inheritance'
connection_class.instance_methods(false).must_include(method_name)
Expand Down
8 changes: 5 additions & 3 deletions lib/train.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def self.target_config(config = nil)

group_keys_and_keyfiles(conf) # TODO: move logic into SSH plugin
return conf if conf[:target].to_s.empty?

unpack_target_from_uri(conf[:target], conf).merge(conf)
end

Expand Down Expand Up @@ -146,6 +147,7 @@ def self.parse_uri(string)
# TODO: this actually does no validation of the credential options whatsoever
def self.validate_backend(credentials, default_transport_name = "local")
return default_transport_name if credentials.nil?

transport_name = credentials[:backend]

# TODO: Determine if it is ever possible (or supported) for transport_name to be 'localhost'
Expand All @@ -155,16 +157,16 @@ def self.validate_backend(credentials, default_transport_name = "local")
"To run this locally with elevated privileges, run the command with `sudo ...`."
end

return transport_name if !transport_name.nil?
return transport_name unless transport_name.nil?

if !credentials[:target].nil?
unless credentials[:target].nil?
# We should not get here, because if target_uri unpacking was successful,
# it would have set credentials[:backend]
raise Train::UserError, "Cannot determine backend from target "\
"configuration #{credentials[:target]}. Valid example: ssh://192.168.0.1"
end

if !credentials[:host].nil?
unless credentials[:host].nil?
raise Train::UserError, "Host configured, but no backend was provided. Please "\
"specify how you want to connect. Valid example: ssh://192.168.0.1"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/train/extras/command_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def initialize(backend, options)
def verify
res = @backend.run_command(run("echo"))
return nil if res.exit_status == 0

rawerr = res.stdout + " " + res.stderr

{
Expand Down Expand Up @@ -164,6 +165,7 @@ class CommandWrapper
def self.load(transport, options)
if transport.platform.unix?
return nil unless LinuxCommand.active?(options)

res = LinuxCommand.new(transport, options)
verification_res = res.verify
if verification_res
Expand Down
6 changes: 5 additions & 1 deletion lib/train/extras/stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def self.stat(shell_escaped_path, backend, follow_symlink)
return bsd_stat(shell_escaped_path, backend, follow_symlink) if backend.os.bsd?
# linux,solaris 11 and esx will use standard linux stats
return linux_stat(shell_escaped_path, backend, follow_symlink) if backend.os.unix? || backend.os.esx?

# all other cases we don't handle
# TODO: print an error if we get here, as it shouldn't be invoked
# on non-unix
Expand Down Expand Up @@ -77,7 +78,8 @@ def self.bsd_stat(shell_escaped_path, backend, follow_symlink)
lstat = follow_symlink ? " -L" : ""
res = backend.run_command(
"stat#{lstat} -f '%z\n%p\n%Su\n%u\n%Sg\n%g\n%a\n%m' "\
"#{shell_escaped_path}")
"#{shell_escaped_path}"
)

return {} if res.exit_status != 0

Expand Down Expand Up @@ -113,8 +115,10 @@ def self.aix_stat(shell_escaped_path, backend, follow_symlink)

res = backend.run_command(stat_cmd)
return {} if res.exit_status != 0

fields = res.stdout.split("\n")
return {} if fields.length != 7

tmask = fields[0].to_i(8)
{
type: find_type(tmask),
Expand Down
2 changes: 2 additions & 0 deletions lib/train/file/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def content

def link_path
return nil unless symlink?

begin
@link_path ||= ::File.realpath(@path)
rescue Errno::ELOOP => _
Expand All @@ -29,6 +30,7 @@ def link_path

def shallow_link_path
return nil unless symlink?

@link_path ||= ::File.readlink(@path)
end

Expand Down
11 changes: 8 additions & 3 deletions lib/train/file/local/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,29 @@ class Windows < Train::File::Local
# @see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions
def sanitize_filename(path)
return if path.nil?

# we do not filter :, backslash and forward slash, since they are part of the path
@spath = path.gsub(/[<>"|?*]/, "")
end

def product_version
@product_version ||= @backend.run_command(
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").ProductVersion").stdout.chomp
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").ProductVersion"
).stdout.chomp
end

def file_version
@file_version ||= @backend.run_command(
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").FileVersion").stdout.chomp
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").FileVersion"
).stdout.chomp
end

def owner
owner = @backend.run_command(
"Get-Acl \"#{@spath}\" | select -expand Owner").stdout.strip
"Get-Acl \"#{@spath}\" | select -expand Owner"
).stdout.strip
return if owner.empty?

owner
end

Expand Down
4 changes: 4 additions & 0 deletions lib/train/file/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ class File
class Remote < Train::File
def basename(suffix = nil, sep = "/")
raise "Not yet supported: Suffix in file.basename" unless suffix.nil?

@basename ||= detect_filename(path, sep || "/")
end

def stat
return @stat if defined?(@stat)

@stat = Train::Extras::Stat.stat(@spath, @backend, @follow_symlink)
end

Expand All @@ -19,8 +21,10 @@ def stat
def detect_filename(path, sep)
idx = path.rindex(sep)
return path if idx.nil?

idx += 1
return detect_filename(path[0..-2], sep) if idx == path.length

path[idx..-1]
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/train/file/remote/aix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class Remote
class Aix < Train::File::Remote::Unix
def link_path
return nil unless symlink?

@link_path ||=
@backend.run_command("perl -e 'print readlink shift' #{@spath}").stdout.chomp
end

def shallow_link_path
return nil unless symlink?

@shallow_link_path ||=
@backend.run_command("perl -e 'print readlink shift' #{@spath}").stdout.chomp
end
Expand Down
2 changes: 2 additions & 0 deletions lib/train/file/remote/linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class Remote
class Linux < Train::File::Remote::Unix
def content
return @content if defined?(@content)

@content = @backend.run_command("cat #{@spath} || echo -n").stdout
return @content unless @content.empty?

@content = nil if directory? || size.nil? || (size > 0)
@content
end
Expand Down
4 changes: 3 additions & 1 deletion lib/train/file/remote/unix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def exist?
@exist ||= begin
f = @follow_symlink ? "" : " || test -L #{@spath}"
@backend.run_command("test -e #{@spath}" + f)
.exit_status == 0
.exit_status == 0
end
end

Expand Down Expand Up @@ -58,6 +58,7 @@ def link_path

def shallow_link_path
return nil unless symlink?

@shallow_link_path ||=
@backend.run_command("readlink #{@spath}").stdout.chomp
end
Expand All @@ -74,6 +75,7 @@ def unix_mode_mask(owner, type)

def path
return @path unless @follow_symlink && symlink?

@link_path ||= read_target_path
end

Expand Down
22 changes: 17 additions & 5 deletions lib/train/file/remote/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Windows < Train::File::Remote
# @see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions
def sanitize_filename(path)
return if path.nil?

# we do not filter :, backslash and forward slash, since they are part of the path
@spath = path.gsub(/[<>"|?*]/, "")
end
Expand All @@ -19,22 +20,28 @@ def basename(suffix = nil, sep = '\\')

def content
return @content if defined?(@content)

@content = @backend.run_command("Get-Content(\"#{@spath}\") | Out-String").stdout
return @content unless @content.empty?

@content = nil if directory? # or size.nil? or size > 0
@content
end

def exist?
return @exist if defined?(@exist)

@exist = @backend.run_command(
"(Test-Path -Path \"#{@spath}\").ToString()").stdout.chomp == "True"
"(Test-Path -Path \"#{@spath}\").ToString()"
).stdout.chomp == "True"
end

def owner
owner = @backend.run_command(
"Get-Acl \"#{@spath}\" | select -expand Owner").stdout.strip
"Get-Acl \"#{@spath}\" | select -expand Owner"
).stdout.strip
return if owner.empty?

owner
end

Expand All @@ -46,6 +53,7 @@ def type
elsif attributes.include?("Directory")
return :directory
end

:unknown
end

Expand All @@ -57,12 +65,14 @@ def size

def product_version
@product_version ||= @backend.run_command(
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").ProductVersion").stdout.chomp
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").ProductVersion"
).stdout.chomp
end

def file_version
@file_version ||= @backend.run_command(
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").FileVersion").stdout.chomp
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").FileVersion"
).stdout.chomp
end

%w{
Expand All @@ -89,8 +99,10 @@ def mounted

def attributes
return @attributes if defined?(@attributes)

@attributes = @backend.run_command(
"(Get-ItemProperty -Path \"#{@spath}\").attributes.ToString()").stdout.chomp.split(/\s*,\s*/)
"(Get-ItemProperty -Path \"#{@spath}\").attributes.ToString()"
).stdout.chomp.split(/\s*,\s*/)
end
end
end
Expand Down
11 changes: 6 additions & 5 deletions lib/train/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def option(name, conf = nil, &block)
d = conf || {}
unless d.is_a? Hash
raise Train::ClientError,
"The transport plugin #{self} declared an option #{name} "\
"and didn't provide a valid configuration hash."
"The transport plugin #{self} declared an option #{name} "\
"and didn't provide a valid configuration hash."
end

if !conf.nil? && !conf[:default].nil? && block_given?
raise Train::ClientError,
"The transport plugin #{self} declared an option #{name} "\
"with both a default value and block. Only use one of these."
"The transport plugin #{self} declared an option #{name} "\
"with both a default value and block. Only use one of these."
end

d[:default] = block if block_given?
Expand Down Expand Up @@ -56,6 +56,7 @@ def merge_options(base, opts)
res = base.merge(opts || {})
default_options.each do |field, hm|
next unless res[field].nil? && hm.key?(:default)

default = hm[:default]
if default.is_a? Proc
res[field] = default.call(res)
Expand All @@ -70,7 +71,7 @@ def validate_options(opts)
default_options.each do |field, hm|
if opts[field].nil? && hm[:required]
raise Train::ClientError,
"You must provide a value for #{field.to_s.inspect}."
"You must provide a value for #{field.to_s.inspect}."
end
end
opts
Expand Down
2 changes: 1 addition & 1 deletion lib/train/platforms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def self.list_all
def self.print_children(parent, pad = 2)
parent.children.each do |key, value|
obj = key
puts "#{' ' * pad}-> #{obj.title}#{value unless value.empty?}"
puts "#{" " * pad}-> #{obj.title}#{value unless value.empty?}"
print_children(obj, pad + 2) if defined?(obj.children) && !obj.children.nil?
end
end
Expand Down
Loading