Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add certname option to pe_agent provisioner #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ with the global `config.pe_build` settings.
* Options: A version string, `x.y.z[-optional-stuff]`, or the string
`'current'`.
* Default: `'current'`.
* `certname`
* Description: How to determine which certname to use for the agent.
**NOTE:** This option only works on POSIX agents for now.
* Options: `vm_name`, `hostname`, or `fqdn`
* Default: `vm_name`


Commands
Expand Down
33 changes: 28 additions & 5 deletions lib/pe_build/config/pe_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,38 @@ class PEBuild::Config::PEAgent < Vagrant.plugin('2', :config)
# `current`. Defaults to `current`.
attr_accessor :version

# @!attribute certname
# @return [String] What to use as the certname. May be either fqdn
# hostname, or vm_name. The default is vm_name
attr_accessor :certname

VALID_CERTNAME_SOURCES = ['vm_name','hostname','fqdn']

def initialize
@autosign = UNSET_VALUE
@autopurge = UNSET_VALUE
@master = UNSET_VALUE
@master_vm = UNSET_VALUE
@version = UNSET_VALUE
@certname = UNSET_VALUE
end

include PEBuild::ConfigDefault

def finalize!
@master = nil if @master == UNSET_VALUE
@master_vm = nil if @master_vm == UNSET_VALUE
@autosign = (not @master_vm.nil?) if @autosign == UNSET_VALUE
@autopurge = (not @master_vm.nil?) if @autopurge == UNSET_VALUE
@version = 'current' if @version == UNSET_VALUE
set_default :@certname, 'vm_name'
set_default :@master, nil
set_default :@master_vm, nil
set_default :@autosign, (not @master_vm.nil?)
set_default :@autopurge, (not @master_vm.nil?)
set_default :@version, 'current'
end

def validate(machine)
errors = _detected_errors

validate_certname(errors, machine)

if @master.nil? && @master_vm.nil?
errors << I18n.t('pebuild.config.pe_agent.errors.no_master')
end
Expand All @@ -68,6 +81,16 @@ def validate(machine)

private

def validate_certname(errors, machine)
unless VALID_CERTNAME_SOURCES.include? @certname
errors << I18n.t(
'pebuild.config.pe_agent.errors.unknown_certname',
:certname => @certname.inspect,
:valid_certname_sources => VALID_CERTNAME_SOURCES
)
end
end

def validate_master_vm!(errors, machine)
if (not @master_vm.nil?) && (not machine.env.machine_names.include?(@master_vm.intern))
errors << I18n.t(
Expand Down
5 changes: 5 additions & 0 deletions lib/pe_build/config_builder/pe_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class PEBuild::ConfigBuilder::PEAgent < ::ConfigBuilder::Model::Base
# string of the form `x.y.x[-optional-arbitrary-stuff]` or the string
# `current`. Defaults to `current`.
def_model_attribute :version
# @!attribute certname
# @return [String] What to use as the certname. May be either fqdn,
# hostname, or vm_name. The default is vm_name
def_model_attribute :certname

def to_proc
Proc.new do |vm_config|
Expand All @@ -36,6 +40,7 @@ def to_proc
with_attr(:master) {|val| config.master = val }
with_attr(:master_vm) {|val| config.master_vm = val }
with_attr(:version) {|val| config.version = val }
with_attr(:certname) {|val| config.certname = val }
end
end
end
Expand Down
26 changes: 25 additions & 1 deletion lib/pe_build/provisioner/pe_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@ def provision_pe_repo
def provision_posix_agent
shell_config = Vagrant.plugin('2').manager.provisioner_configs[:shell].new
shell_config.privileged = true

certname_string = case config.certname
when 'hostname'
machine.config.vm.hostname
when 'fqdn'
#The installer script already defaults to FQDN
#Just let it do its thing
nil
when 'vm_name'
machine.name
else
raise "You should not be seeing this since certname config value should already have
been validated to be one of #{VALID_CERTNAME_SOURCES} and #{config.certname}
is unknown"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no default statement, so if the provided value doesn't match then this will return nil. Is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I'll update.


if certname_string.nil?
machine.ui.info "Defaulting to using FQDN for the certname"
certname_option = String.new
else
machine.ui.info "Using #{certname_string} for the certname"
certname_option = "agent:certname=#{certname_string}"
end

# Installation is split into to components running under set -e so that
# failures are detected. The curl command uses `sS` so that download
# progress is silenced, but error messages are still printed.
Expand All @@ -149,7 +173,7 @@ def provision_posix_agent
shell_config.inline = <<-EOS
set -e
curl -ksS -tlsv1 https://#{config.master}:8140/packages/current/install.bash -o pe_frictionless_installer.sh
bash pe_frictionless_installer.sh
bash pe_frictionless_installer.sh #{certname_option}
EOS
shell_config.finalize!

Expand Down
4 changes: 4 additions & 0 deletions templates/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ en:
Skipping purge of agent data.
purging_agent: |-
Purging agent data for %{certname} from %{master}.

transfer:
open_uri:
download_failed: |-
Expand Down Expand Up @@ -113,6 +114,9 @@ en:
version_too_old: |-
The agent version %{version} is too old; pe_agent can only provision versions
newer than %{minimum_version}.
unknown_certname: |-
The provided certname source %{certname} is not valid. Valid options
are %{valid_certname_sources}
cap:
run_install:
already_installed: |-
Expand Down