Class: InspecPlugins::Habitat::Profile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Profile.



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

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.



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

def logger
  @logger
end

Instance Method Details

#createObject



19
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
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 19

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 55

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)

  run_hook_file = File.join(path, 'habitat', 'hooks', 'run')
  logger.info("Generating a Habitat run hook at #{run_hook_file}...")
  create_file_from_template(run_hook_file, 'hooks/run.erb')

  default_toml = File.join(path, 'habitat', 'default.toml')
  logger.info("Generating a Habitat default.toml at #{default_toml}...")
  create_file_from_template(default_toml, 'default.toml.erb')

  config = File.join(path, 'habitat', 'config', 'inspec_exec_config.json')
  logger.info("Generating #{config} for `inspec exec`...")
  create_file_from_template(config, 'config/inspec_exec_config.json.erb')
end

#uploadObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/plugins/inspec-habitat/lib/inspec-habitat/profile.rb', line 80

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