Class: KnifePlugin::Check

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/knife-check.rb

Instance Method Summary collapse

Instance Method Details

#check_syntax(dirpath, dir = nil, type) ⇒ Object

Common method to test file syntax



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/knife-check.rb', line 91

def check_syntax(dirpath, dir = nil, type)
  files = Dir.glob("#{dirpath}").select { |f| File.file?(f) }
  files.each do |file|
    fname = Pathname.new(file).basename
    if ("#{fname}".end_with?('.json'))
      ui.msg("Testing file #{file}")
      json = File.new(file, 'r')
      parser = Yajl::Parser.new
      hash = parser.parse(json)
    elsif("#{fname}".end_with?('.rb'))
      ui.msg("Testing file #{file}")
      result = `ruby -wc #{file}`
    end 
  end 
end

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef/knife/knife-check.rb', line 45

def run 
  if config[:roles] 
    test_object("roles/*", "role")
  elsif config[:environments]
    test_object("environments/*", "environment")
  elsif config[:nodes]
    test_object("nodes/*", "node")
  elsif config[:databags]
    test_databag("data_bags/*", "data bag")
  elsif config[:cookbooks]
    test_cookbooks("cookbooks")
  elsif config[:all]
    test_object("roles/*", "role")
    test_object("environments/*", "environment")
    test_object("nodes/*", "node")
    test_databag("data_bags/*", "data bag")
    test_cookbooks("cookbooks")
  else
    ui.msg("Usage: knife check --help")
  end 
end

#test_cookbooks(path) ⇒ Object

Test all cookbooks



68
69
70
71
72
# File 'lib/chef/knife/knife-check.rb', line 68

def test_cookbooks(path)
  ui.msg("Testing all cookbooks...")
  result = `knife cookbook test -a -o #{path}`
  ui.msg(result)
end

#test_databag(dirname, type) ⇒ Object

Test databag syntax



81
82
83
84
85
86
87
88
# File 'lib/chef/knife/knife-check.rb', line 81

def test_databag(dirname, type)
  ui.msg("Finding type #{type} to test from #{dirname}")
  dirs = Dir.glob(dirname).select { |d| File.directory?(d) }
  dirs.each do |directory|
    dir = Pathname.new(directory).basename
    check_syntax("#{directory}/*", dir, type)
  end 
end

#test_object(dirname, type) ⇒ Object

Test object syntax



75
76
77
78
# File 'lib/chef/knife/knife-check.rb', line 75

def test_object(dirname,type)
  ui.msg("Finding type #{type} to test from #{dirname}")
  check_syntax(dirname,nil,type)
end