class Kitchen::Provisioner::HabitatConfig

Public Instance Methods

create_sandbox() click to toggle source
Calls superclass method
# File lib/kitchen/provisioner/habitat_config.rb, line 99
def create_sandbox
  super
  copy_results_to_sandbox
  copy_user_toml_to_sandbox
  copy_package_config_from_override_to_sandbox
end
finalize_config!(instance) click to toggle source
Calls superclass method
# File lib/kitchen/provisioner/habitat_config.rb, line 53
def finalize_config!(instance)
  # Check to see if a package ident was specified for package name and be helpful
  unless config[:package_name].nil? || (config[:package_name] =~ /\//).nil?
    config[:package_origin], config[:package_name], config[:package_version], config[:package_release] = config[:package_name].split('/')
  end

  unless config[:hab_sup_artifact_name].nil?
    ident = artifact_name_to_package_ident_regex.match(config[:hab_sup_artifact_name])
    config[:hab_sup_origin] = ident['origin']
    config[:hab_sup_name] = ident['name']
    config[:hab_sup_version] = ident['version']
    config[:hab_sup_release] = ident['release']
  end

  unless config[:artifact_name].nil?
    ident = artifact_name_to_package_ident_regex.match(config[:artifact_name])
    config[:package_origin] = ident['origin']
    config[:package_name] = ident['name']
    config[:package_version] = ident['version']
    config[:package_release] = ident['release']
  end
  super(instance)
end
init_command() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 90
      def init_command
        wrap_shell_code <<-EOH
          id -u hab >/dev/null 2>&1 || sudo useradd hab >/dev/null 2>&1
          rm -rf /tmp/kitchen
          mkdir -p /tmp/kitchen/results
          #{'mkdir -p /tmp/kitchen/config' unless config[:override_package_config]}
        EOH
      end
install_command() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 77
      def install_command
        raise "Need to fill in some implementation here." if instance.platform == "windows"
        wrap_shell_code <<-BASH
        #{export_hab_origin}
        if command -v hab >/dev/null 2>&1
        then
          echo "Habitat CLI already installed."
        else
          curl 'https://raw.githubusercontent.com/habitat-sh/habitat/master/components/hab/install.sh' | sudo bash
        fi
        BASH
      end
prepare_command() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 106
      def prepare_command
        wrap_shell_code <<-EOH
          #{export_hab_origin}
          #{install_supervisor_command}
          #{binlink_supervisor_command}
          #{install_service_package}
          #{remove_previous_user_toml}
          #{copy_user_toml_to_service_directory}
          EOH
      end
run_command() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 117
      def run_command
        run = <<-RUN
        #{export_hab_origin}
        #{clean_up_screen_sessions}
        #{clean_up_previous_supervisor}
        echo "Running #{package_ident}."
        sudo hab pkg install #{package_ident}
        #{run_package_in_background}
        RUN

        wrap_shell_code run
      end

Private Instance Methods

artifact_name_to_package_ident_regex() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 288
def artifact_name_to_package_ident_regex
  /(?<origin>\w+)-(?<name>.*)-(?<version>(\d+)?(\.\d+)?(\.\d+)?(\.\d+)?)-(?<release>\d+)-(?<target>.*)\.hart$/
end
clean_package_name() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 308
def clean_package_name
  @clean_name ||= "#{config[:package_origin]}-#{config[:package_name]}"
end
clean_up_previous_supervisor() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 145
      def clean_up_previous_supervisor
        return if config[:use_screen]
        <<-EOH
        [ -f ./run.pid ] && echo "Removing previous supervisor. "
        [ -f ./run.pid ] && sudo kill $(cat run.pid)
        [ -f ./run.pid ] && sleep 5
        EOH
      end
clean_up_screen_sessions() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 132
      def clean_up_screen_sessions
        return unless config[:use_screen]
        <<-CLEAN
        if sudo screen -ls | grep -q #{clean_package_name}
          then
            echo "Killing previous supervisor session."
            sudo screen -S \"#{clean_package_name}\" -X quit > /dev/null
            echo "Removing dead session."
            sudo screen -wipe > /dev/null
        fi
        CLEAN
      end
copy_package_config_from_override_to_sandbox() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 196
def copy_package_config_from_override_to_sandbox
  return if config[:config_directory].nil?
  return unless config[:override_package_config]
  local_config_dir = File.join(config[:kitchen_root], config[:config_directory])
  return unless Dir.exist?(local_config_dir)

  sandbox_config_dir = File.join(sandbox_path, "config")
  FileUtils.copy_entry(local_config_dir, sandbox_config_dir)
end
copy_results_to_sandbox() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 206
def copy_results_to_sandbox
  return if config[:artifact_name].nil? && !config[:install_latest_artifact]
  results_dir = resolve_results_directory
  return if results_dir.nil?
  FileUtils.mkdir_p(File.join(sandbox_path, "results"))
  FileUtils.cp(
    File.join(results_dir, config[:install_latest_artifact] ? latest_artifact_name : config[:artifact_name]),
    File.join(sandbox_path, "results"),
    preserve: true
  )
end
copy_user_toml_to_sandbox() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 226
def copy_user_toml_to_sandbox
  return if config[:config_directory].nil?
  return unless File.exist?(full_user_toml_path)
  FileUtils.mkdir_p(File.join(sandbox_path, "config"))
  debug("Copying user.toml from #{full_user_toml_path} to #{sandbox_user_toml_path}")
  FileUtils.cp(full_user_toml_path, sandbox_user_toml_path)
end
copy_user_toml_to_service_directory() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 259
      def copy_user_toml_to_service_directory
        return unless !config[:config_directory].nil? && File.exist?(full_user_toml_path)
        <<-EOH
          sudo mkdir -p /hab/svc/#{config[:package_name]}
          sudo cp #{File.join(File.join(config[:root_path], 'config'), 'user.toml')} /hab/svc/#{config[:package_name]}/user.toml
        EOH
      end
export_hab_origin() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 275
def export_hab_origin
  return if config[:depot_url].nil?
  "export HAB_ORIGIN=#{config[:depot_url]}"
end
full_user_toml_path() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 218
def full_user_toml_path
  File.join(File.join(config[:kitchen_root], config[:config_directory]), config[:user_toml_name])
end
hab_sup_ident() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 292
def hab_sup_ident
  ident = "#{config[:hab_sup_origin]}/" \
          "#{config[:hab_sup_name]}/" \
          "#{config[:hab_sup_version]}/" \
          "#{config[:hab_sup_release]}".chomp("/").chomp("/")
  @sup_ident ||= ident
end
install_service_package() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 234
def install_service_package
  return unless config[:install_latest_artifact] || !config[:artifact_name].nil?

  artifact_name = ""
  if config[:install_latest_artifact]
    artifact_name = latest_artifact_name
  elsif !config[:install_latest_artifact] && !config[:artifact_name].nil?
    artifact_name = config[:artifact_name]
  else
    return
  end

  artifact_path = File.join(File.join(config[:root_path], "results"), artifact_name)
  "sudo hab pkg install #{artifact_path}"
end
install_supervisor_command() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 280
def install_supervisor_command
  "sudo hab pkg install #{hab_sup_ident}"
end
latest_artifact_name() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 250
def latest_artifact_name
  results_dir = resolve_results_directory
  return if results_dir.nil?

  artifact_path = Dir.glob(File.join(results_dir, "#{config[:package_origin]}-#{config[:package_name]}-*.hart")).max_by { |f| File.mtime(f) }

  File.basename(artifact_path)
end
package_ident() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 300
def package_ident
  ident = "#{config[:package_origin]}/" \
          "#{config[:package_name]}/" \
          "#{config[:package_version]}/" \
          "#{config[:package_release]}".chomp("/").chomp("/")
  @pkg_ident ||= ident
end
remove_previous_user_toml() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 267
      def remove_previous_user_toml
        <<-REMOVE
        if [ -d "/hab/svc/#{config[:package_name]}" ]; then
          sudo find /hab/svc/#{config[:package_name]} -name user.toml -delete
        fi
        REMOVE
      end
resolve_results_directory() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 180
def resolve_results_directory
  return config[:results_directory] unless config[:results_directory].nil?

  results_in_current = File.join(config[:kitchen_root], "results")
  results_in_parent = File.join(config[:kitchen_root], "../results")
  results_in_grandparent = File.join(config[:kitchen_root], "../../results")

  if Dir.exist?(results_in_current)
    results_in_current
  elsif Dir.exist?(results_in_parent)
    results_in_parent
  elsif Dir.exist?(results_in_grandparent)
    results_in_grandparent
  end
end
run_package_in_background() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 154
      def run_package_in_background
        if config[:use_screen]
          "sudo screen -mdS \"#{clean_package_name}\" hab start #{package_ident} #{supervisor_options}"
        else
          <<-RUN
          [ -f ./run.pid ] && rm -f run.pid
          [ -f ./nohup.out ] && rm -f nohup.out
          nohup sudo hab sup run #{sup_run_options} & echo $! > run.pid

          until sudo hab svc status
          do
            sleep 1
          done

          sudo hab svc load #{package_ident} #{supervisor_options}

          until sudo hab svc status | grep #{package_ident}
          do
            sleep 1
          done

          [ -f ./nohup.out ] && cat nohup.out || (echo "Failed to start the supervisor." && exit 1)
          RUN
        end
      end
sandbox_user_toml_path() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 222
def sandbox_user_toml_path
  File.join(File.join(sandbox_path, "config"), "user.toml")
end
sup_run_options() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 312
def sup_run_options
  options = ''
  options += " --listen-gossip #{config[:hab_sup_listen_gossip]}" unless config[:hab_sup_listen_gossip].nil?
  options += " --config-from #{File.join(config[:root_path], 'config/')}" if config[:override_package_config]
  options += config[:hab_sup_bind].map { |b| " --bind #{b}" }.join(" ") if config[:hab_sup_bind].any?
  options += config[:hab_sup_peer].map { |p| " --peer #{p}" }.join(" ") if config[:hab_sup_peer].any?

  options
end
supervisor_options() click to toggle source
# File lib/kitchen/provisioner/habitat_config.rb, line 322
def supervisor_options
  options = ''
  options += " --listen-gossip #{config[:hab_sup_listen_gossip]}" unless config[:hab_sup_listen_gossip].nil?
  options += " --config-from #{File.join(config[:root_path], 'config/')}" if config[:override_package_config]
  options += config[:hab_sup_bind].map { |b| " --bind #{b}" }.join(" ") if config[:hab_sup_bind].any?
  # options += config[:hab_sup_peer].map { |p| " --peer #{p}" }.join(" ") if config[:hab_sup_peer].any?
  options += " --group #{config[:hab_sup_group]}" unless config[:hab_sup_group].nil?
  options += " --topology #{config[:service_topology]}" unless config[:service_topology].nil?
  options += " --strategy #{config[:service_update_strategy]}" unless config[:service_update_strategy].nil?
  options += " --channel #{config[:channel]}" unless config[:channel].nil?

  options
end