Class: InspecPlugins::Habitat::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Profile

Returns a new instance of Profile.



32
33
34
35
36
37
38
39
40
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 32

def initialize(path, options = {})
  @path    = path
  @options = options
  @cli_config = nil

  log_level = options.fetch('log_level', 'info')
  @log = Inspec::Log
  @log.level(log_level.to_sym)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 11

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 11

def path
  @path
end

#profileObject (readonly)

Returns the value of attribute profile.



11
12
13
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 11

def profile
  @profile
end

Class Method Details

.create(path, options = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 13

def self.create(path, options = {})
  creator = new(path, options)
  hart_file = creator.create
  creator.copy(hart_file)
ensure
  creator.delete_work_dir
end

.setup(path) ⇒ Object



21
22
23
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 21

def self.setup(path)
  new(path).setup
end

.upload(path, options = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 25

def self.upload(path, options = {})
  uploader = new(path, options)
  uploader.upload
ensure
  uploader.delete_work_dir
end

Instance Method Details

#copy(hart_file) ⇒ Object



66
67
68
69
70
71
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 66

def copy(hart_file)
  validate_output_dir

  @log.info("Copying artifact to #{output_dir}...")
  copy_hart(hart_file)
end

#createObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 42

def create
  @log.info("Creating a Habitat artifact for profile: #{path}")

  validate_habitat_installed
  validate_habitat_origin
  create_profile_object
  verify_profile
  vendor_profile_dependencies
  copy_profile_to_work_dir
  create_habitat_directories(work_dir)
  create_plan(work_dir)
  create_run_hook(work_dir)
  create_default_config(work_dir)

  # returns the path to the .hart file in the work directory
  build_hart
rescue => e
  @log.debug(e.backtrace.join("\n"))
  exit_with_error(
    'Unable to generate Habitat artifact.',
    "#{e.class} -- #{e.message}",
  )
end

#delete_work_dirObject



85
86
87
88
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 85

def delete_work_dir
  @log.debug("Deleting work directory #{work_dir}")
  FileUtils.rm_rf(work_dir) if Dir.exist?(work_dir)
end

#setupObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 90

def setup
  @log.info("Setting up profile at #{path} for Habitat...")
  create_profile_object
  verify_profile
  vendor_profile_dependencies
  create_habitat_directories(path)
  create_plan(path)
  create_run_hook(path)
  create_default_config(path)
end

#uploadObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 73

def upload
  validate_habitat_auth_token
  hart_file = create
  upload_hart(hart_file)
rescue => e
  @log.debug(e.backtrace.join("\n"))
  exit_with_error(
    'Unable to upload Habitat artifact.',
    "#{e.class} -- #{e.message}",
  )
end