Class: Cloudspin::Stack::Rake::InspecTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/cloudspin/stack/rake/inspec_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack_instance:, inspec_folder: './inspec', work_folder: nil, inspec_target: nil, inspec_parameters: {}) ⇒ InspecTask

Returns a new instance of InspecTask.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 13

def initialize(stack_instance:,
               inspec_folder: './inspec',
               work_folder: nil,
               inspec_target: nil,
               inspec_parameters: {})
  @stack_instance = stack_instance
  @stack_instance_id = stack_instance.id
  @inspec_target = inspec_target
  @inspec_parameters = inspec_parameters

  @work_folder = work_folder || @stack_instance.working_folder
  @inspec_folder = inspec_folder
  if Dir.exists?(inspec_folder)
    define
  else
    puts "No directory found: #{inspec_folder}"
  end
end

Instance Attribute Details

#inspec_folderObject (readonly)

Returns the value of attribute inspec_folder.



9
10
11
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 9

def inspec_folder
  @inspec_folder
end

#inspec_parametersObject (readonly)

Returns the value of attribute inspec_parameters.



11
12
13
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 11

def inspec_parameters
  @inspec_parameters
end

#inspec_targetObject (readonly)

Returns the value of attribute inspec_target.



10
11
12
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 10

def inspec_target
  @inspec_target
end

#stack_instance_idObject (readonly)

Returns the value of attribute stack_instance_id.



7
8
9
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 7

def stack_instance_id
  @stack_instance_id
end

#work_folderObject (readonly)

Returns the value of attribute work_folder.



8
9
10
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 8

def work_folder
  @work_folder
end

Instance Method Details

#build_attributes_fileObject



40
41
42
43
44
45
46
47
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 40

def build_attributes_file
  ensure_path(inspec_attributes_file)
  File.open(inspec_attributes_file, 'w') {|f|
    f.write(inspec_parameters.merge(
      { 'stack_instance_id' => stack_instance_id }
    ).to_yaml)
  }
end

#defineObject



32
33
34
35
36
37
38
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 32

def define
  desc 'Run inspec tests'
  task :inspec do |t, args|
    build_attributes_file
    run_inspec_profile
  end
end

#ensure_path(file_path) ⇒ Object



53
54
55
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 53

def ensure_path(file_path)
  mkpath(File.dirname(file_path))
end

#inspec_attributes_fileObject



49
50
51
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 49

def inspec_attributes_file
  "#{work_folder}/inspec/attributes-for-stack-#{stack_instance_id}.yml"
end

#inspec_command(inspec_profile_subfolder) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 68

def inspec_command(inspec_profile_subfolder)
  command_parts = [
    'inspec',
    'exec',
    "#{@inspec_folder}/#{inspec_profile_subfolder}",
    '--attrs',
    inspec_attributes_file,
    '--reporter',
    "json-rspec:#{inspec_profile_results_file(inspec_profile_name(inspec_profile_subfolder))}",
    'cli']

  command_parts << ['-t', inspec_target] if inspec_target
  command_parts.join(' ')
end

#inspec_profile_name(subfolder_name) ⇒ Object



87
88
89
90
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 87

def inspec_profile_name(subfolder_name)
  profile_spec = YAML.load_file("#{@inspec_folder}/#{subfolder_name}/inspec.yml") || {}
  profile_spec['name'] || 'default'
end

#inspec_profile_results_file(profile_name) ⇒ Object



83
84
85
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 83

def inspec_profile_results_file(profile_name)
  "#{work_folder}/inspec/results-for-stack-#{stack_instance_id}-profile-#{profile_name}.json"
end

#inspec_profiles_in(folder) ⇒ Object



92
93
94
95
96
97
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 92

def inspec_profiles_in(folder)
  Dir.entries(folder).select { |possible_profile|
    possible_profile != '..' &&
      File.exists?("#{folder}/#{possible_profile}/inspec.yml")
  }
end

#run_inspec_profileObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/cloudspin/stack/rake/inspec_task.rb', line 57

def run_inspec_profile
  puts "Run inspec"
  inspec_profiles_in(@inspec_folder).each { |inspec_profile_subfolder|
    cmd = inspec_command(inspec_profile_subfolder)
    puts cmd
    return if system(cmd)
    $stderr.puts "#{cmd} failed"
    exit $?.exitstatus || 1
  }
end