Class: InspecPlugins::Habitat::Profile

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

Constant Summary

Constants included from Inspec::Dist

Inspec::Dist::AUTOMATE_PRODUCT_NAME, Inspec::Dist::COMPLIANCE_PRODUCT_NAME, Inspec::Dist::EXEC_NAME, Inspec::Dist::PRODUCT_NAME, Inspec::Dist::SERVER_PRODUCT_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Profile.



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

def initialize(path, options = {})
  @path    = path
  @options = options
  @logger  = Inspec::Log
  logger.level(options.fetch(:log_level, "info").to_sym)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 20

def create
  logger.info("Creating a Habitat artifact for '#{@path}'...")

  # Need to create working directory first so `ensure` doesn't error
  working_dir = create_working_dir

  habitat_config = read_habitat_config
  verify_habitat_setup(habitat_config)

  output_dir = @options[:output_dir] || Dir.pwd
  unless File.directory?(output_dir)
    exit_with_error("Output directory #{output_dir} is not a directory " \
                    "or does not exist.")
  end

  duplicated_profile = duplicate_profile(@path, working_dir)
  prepare_profile!(duplicated_profile)

  hart_file = build_hart(working_dir, habitat_config)

  logger.debug("Copying artifact to #{output_dir}...")
  destination = File.join(output_dir, File.basename(hart_file))
  FileUtils.cp(hart_file, destination)

  logger.info("Habitat artifact '#{@destination}' created.")
  destination
rescue => e
  logger.debug(e.backtrace.join("\n"))
  exit_with_error("Unable to create Habitat artifact.")
ensure
  if Dir.exist?(working_dir)
    logger.debug("Deleting working directory #{working_dir}")
    FileUtils.rm_rf(working_dir)
  end
end

#setup(profile = profile_from_path(@path)) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 56

def setup(profile = profile_from_path(@path))
  path = profile.root_path
  logger.debug("Setting up #{path} for Habitat...")

  plan_file = File.join(path, "habitat", "plan.sh")
  logger.info("Generating Habitat plan at #{plan_file}...")
  vars = {
    profile: profile,
    habitat_origin: read_habitat_config["origin"],
  }
  create_file_from_template(plan_file, "plan.sh.erb", vars)
end

#uploadObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 69

def upload
  habitat_config = read_habitat_config

  if habitat_config["auth_token"].nil?
    exit_with_error(
      "Unable to determine Habitat auth token for uploading.",
      "Run `hab setup` or set the HAB_AUTH_TOKEN environment variable."
    )
  end

  # Run create command to create habitat artifact
  hart = create

  logger.info("Uploading Habitat artifact #{hart}...")
  upload_hart(hart, habitat_config)
  logger.info("Habitat artifact #{hart} uploaded.")
rescue => e
  logger.debug(e.backtrace.join("\n"))
  exit_with_error("Unable to upload Habitat artifact.")
end