Class: Habitat::Profile

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

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Profile.



27
28
29
30
31
32
33
# File 'lib/bundles/inspec-habitat/profile.rb', line 27

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

  log_level = options.fetch('log_level', 'info')
  Habitat::Log.level(log_level.to_sym)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/bundles/inspec-habitat/profile.rb', line 10

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/bundles/inspec-habitat/profile.rb', line 10

def path
  @path
end

#profileObject (readonly)

Returns the value of attribute profile.



10
11
12
# File 'lib/bundles/inspec-habitat/profile.rb', line 10

def profile
  @profile
end

Class Method Details

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



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

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

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



20
21
22
23
24
25
# File 'lib/bundles/inspec-habitat/profile.rb', line 20

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

Instance Method Details

#copy(hart_file) ⇒ Object



58
59
60
61
62
63
# File 'lib/bundles/inspec-habitat/profile.rb', line 58

def copy(hart_file)
  validate_output_dir

  Habitat::Log.info("Copying artifact to #{output_dir}...")
  copy_hart(hart_file)
end

#createObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bundles/inspec-habitat/profile.rb', line 35

def create
  Habitat::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_plan
  create_run_hook
  create_default_config

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

#delete_work_dirObject



77
78
79
80
# File 'lib/bundles/inspec-habitat/profile.rb', line 77

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

#uploadObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bundles/inspec-habitat/profile.rb', line 65

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