class Mixlib::Install::ScriptGenerator

Constants

VALID_INSTALL_OPTS

Attributes

endpoint[RW]
http_proxy[RW]
https_proxy[RW]
install_flags[RW]
install_msi_url[RW]
nightlies[RW]
omnibus_url[RW]
powershell[RW]
prerelease[RW]
root[RW]
sudo_command[R]
use_sudo[RW]
version[RW]

Public Class Methods

new(version, powershell = false, opts = {}) click to toggle source
# File lib/mixlib/install/script_generator.rb, line 73
def initialize(version, powershell = false, opts = {})
  @version = (version || "latest").to_s.downcase
  @powershell = powershell
  @http_proxy = nil
  @https_proxy = nil
  @install_flags = nil
  @prerelease = false
  @nightlies = false
  @endpoint = "metadata"
  @omnibus_url = "https://www.chef.io/chef/install.sh"
  @use_sudo = true
  @sudo_command = "sudo -E"

  @root = if powershell
            "$env:systemdrive\\#{Mixlib::Install::Dist::WINDOWS_INSTALL_DIR}\\#{Mixlib::Install::Dist::DEFAULT_PRODUCT}"
          else
            "#{Mixlib::Install::Dist::LINUX_INSTALL_DIR}/#{Mixlib::Install::Dist::DEFAULT_PRODUCT}"
          end

  parse_opts(opts)
end

Public Instance Methods

install_command() click to toggle source
# File lib/mixlib/install/script_generator.rb, line 95
def install_command
  vars = if powershell
           install_command_vars_for_powershell
         else
           install_command_vars_for_bourne
         end
  shell_code_from_file(vars)
end
sudo_command=(cmd) click to toggle source
# File lib/mixlib/install/script_generator.rb, line 46
def sudo_command=(cmd)
  if cmd.nil?
    @use_sudo = false
  else
    @sudo_command = cmd
  end
end

Private Instance Methods

install_command_vars_for_bourne() click to toggle source

Generates the install command variables for Bourne shell-based platforms.

@return [String] shell variable lines @api private

# File lib/mixlib/install/script_generator.rb, line 111
def install_command_vars_for_bourne
  flags = %w{latest true nightlies}.include?(version) ? "" : "-v #{CGI.escape(version)}"
  flags << " " << "-n" if nightlies
  flags << " " << "-p" if prerelease
  flags << " " << install_flags if install_flags

  [
    shell_var("chef_omnibus_root", root),
    shell_var("chef_omnibus_url", omnibus_url),
    shell_var("install_flags", flags.strip),
    shell_var("pretty_version", Util.pretty_version(version)),
    shell_var("sudo_sh", sudo("sh")),
    shell_var("version", version),
  ].join("\n")
end
install_command_vars_for_powershell() click to toggle source

Generates the install command variables for PowerShell-based platforms.

@param version [String] version string @param metadata_url [String] The metadata URL for the Chef Omnitruck API server @param omnibus_root [String] The base directory the project is installed to @return [String] shell variable lines @api private

# File lib/mixlib/install/script_generator.rb, line 134
def install_command_vars_for_powershell
  d_flag = install_flags.nil? ? nil : install_flags.match(/-download_directory (\S+)/)
  download_directory = d_flag.nil? ? "$env:TEMP" : d_flag[1]
  [
    shell_var("chef_omnibus_root", root),
    shell_var("msi", "#{download_directory}\\chef-#{version}.msi"),
    shell_var("download_directory", download_directory),
  ].tap do |vars|
    if install_msi_url
      vars << shell_var("chef_msi_url", install_msi_url)
    else
      vars << shell_var("chef_metadata_url", windows_metadata_url)
      vars << shell_var("pretty_version", Util.pretty_version(version))
      vars << shell_var("version", version)
    end
  end.join("\n")
end
metadata_endpoint_from_project(project = nil) click to toggle source

@return the correct Chef Omnitruck API metadata endpoint, based on project

# File lib/mixlib/install/script_generator.rb, line 206
def metadata_endpoint_from_project(project = nil)
  if project.nil? || project.casecmp("chef") == 0
    "metadata"
  else
    "metadata-#{project.downcase}"
  end
end
parse_opts(opts) click to toggle source
# File lib/mixlib/install/script_generator.rb, line 158
def parse_opts(opts)
  opts.each do |opt, setting|
    validate_opts!(opt)
    case opt.to_s
    when "project", "endpoint"
      self.endpoint = metadata_endpoint_from_project(setting)
    else
      send("#{opt.to_sym}=", setting)
    end
  end
end
powershell_prefix() click to toggle source

Prefixes the PowerShell install script with helpers and shell vars to detect the platform version and architecture.

@return [String] PowerShell helpers and shell vars for platform info

# File lib/mixlib/install/script_generator.rb, line 187
def powershell_prefix
  [
    Mixlib::Install::Generator::PowerShell.get_script("helpers.ps1"),
    "$platform_architecture = Get-PlatformArchitecture",
    "$platform_version = Get-PlatformVersion",
  ].join("\n")
end
shell_code_from_file(vars) click to toggle source
# File lib/mixlib/install/script_generator.rb, line 170
def shell_code_from_file(vars)
  fn = File.join(
    File.dirname(__FILE__),
    %w{.. .. .. support},
    "install_command"
  )
  code = Util.shell_code_from_file(
    vars, fn, powershell,
    http_proxy: http_proxy, https_proxy: https_proxy
  )
  powershell ? powershell_prefix.concat(code) : code
end
shell_var(name, value) click to toggle source

Builds a shell variable assignment string for the required shell type.

@param name [String] variable name @param value [String] variable value @return [String] shell variable assignment @api private

# File lib/mixlib/install/script_generator.rb, line 201
def shell_var(name, value)
  Util.shell_var(name, value, powershell)
end
sudo(script) click to toggle source

Conditionally prefixes a command with a sudo command.

@param command [String] command to be prefixed @return [String] the command, conditionally prefixed with sudo @api private

# File lib/mixlib/install/script_generator.rb, line 232
def sudo(script)
  use_sudo ? "#{sudo_command} #{script}" : script
end
validate_opts!(opt) click to toggle source
# File lib/mixlib/install/script_generator.rb, line 152
def validate_opts!(opt)
  err_msg = ["#{opt} is not a valid option",
             "valid options are #{VALID_INSTALL_OPTS.join(" ")}"].join(",")
  raise ArgumentError, err_msg unless VALID_INSTALL_OPTS.include?(opt.to_s)
end
windows_metadata_url() click to toggle source
# File lib/mixlib/install/script_generator.rb, line 214
def windows_metadata_url
  base = if omnibus_url =~ %r{/install.sh$}
           "#{File.dirname(omnibus_url)}/"
         end

  url = "#{base}#{endpoint}"
  url << "?p=windows&m=$platform_architecture&pv=$platform_version"
  url << "&v=#{CGI.escape(version)}" unless %w{latest true nightlies}.include?(version)
  url << "&prerelease=true" if prerelease
  url << "&nightlies=true" if nightlies
  url
end