Class: Ridley::Bootstrapper::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ridley/bootstrapper/context.rb

Overview

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ Context

Returns a new instance of Context.

Parameters:

  • host (String)

    name of the node as identified in Chef

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :validator_path (String)

    filepath to the validator used to bootstrap the node (required)

  • :node_name (String)
  • :server_url (String)
  • :validator_client (String)
  • :bootstrap_proxy (String)

    URL to a proxy server to bootstrap through (default: nil)

  • :encrypted_data_bag_secret_path (String)

    filepath on your host machine to your organizations encrypted data bag secret (default: nil)

  • :hints (Hash)

    a hash of Ohai hints to place on the bootstrapped node (default: Hash.new)

  • :attributes (Hash)

    a hash of attributes to use in the first Chef run (default: Hash.new)

  • :run_list (Array)

    an initial run list to bootstrap with (default: Array.new)

  • :chef_version (String)

    version of Chef to install on the node (default: CHEF_VERSION)

  • :environment (String)

    environment to join the node to (default: ‘_default’)

  • :sudo (Boolean)

    bootstrap with sudo (default: true)

  • :template (String)

    bootstrap template to use (default: omnibus)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ridley/bootstrapper/context.rb', line 79

def initialize(host, options = {})
  options = self.class.default_options.merge(options)
  self.class.validate_options(options)

  @host                           = host
  @server_url                     = options[:server_url]
  @validator_path                 = options[:validator_path]
  @node_name                      = options[:node_name]
  @validator_client               = options[:validator_client]
  @bootstrap_proxy                = options[:bootstrap_proxy]
  @encrypted_data_bag_secret_path = options[:encrypted_data_bag_secret_path]
  @hints                          = options[:hints]
  @attributes                     = options[:attributes]
  @run_list                       = options[:run_list]
  @chef_version                   = options[:chef_version]
  @environment                    = options[:environment]
  @sudo                           = options[:sudo]
  @template_file                  = options[:template]
end

Instance Attribute Details

#bootstrap_proxyString (readonly)

Returns:

  • (String)


46
47
48
# File 'lib/ridley/bootstrapper/context.rb', line 46

def bootstrap_proxy
  @bootstrap_proxy
end

#chef_versionString (readonly)

Returns:

  • (String)


50
51
52
# File 'lib/ridley/bootstrapper/context.rb', line 50

def chef_version
  @chef_version
end

#environmentString (readonly)

Returns:

  • (String)


52
53
54
# File 'lib/ridley/bootstrapper/context.rb', line 52

def environment
  @environment
end

#hintsHash (readonly)

Returns:

  • (Hash)


48
49
50
# File 'lib/ridley/bootstrapper/context.rb', line 48

def hints
  @hints
end

#hostString (readonly)

Returns:

  • (String)


36
37
38
# File 'lib/ridley/bootstrapper/context.rb', line 36

def host
  @host
end

#node_nameString (readonly)

Returns:

  • (String)


38
39
40
# File 'lib/ridley/bootstrapper/context.rb', line 38

def node_name
  @node_name
end

#server_urlString (readonly)

Returns:

  • (String)


40
41
42
# File 'lib/ridley/bootstrapper/context.rb', line 40

def server_url
  @server_url
end

#validator_clientString (readonly)

Returns:

  • (String)


42
43
44
# File 'lib/ridley/bootstrapper/context.rb', line 42

def validator_client
  @validator_client
end

Class Method Details

.default_optionsHash

A hash of default options to be used in the Context initializer

Returns:

  • (Hash)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ridley/bootstrapper/context.rb', line 21

def default_options
  @default_options ||= {
    validator_client: "chef-validator",
    hints: Hash.new,
    attributes: Hash.new,
    run_list: Array.new,
    chef_version: Ridley::CHEF_VERSION,
    environment: "_default",
    sudo: true,
    template: Bootstrapper.default_template
  }
end

.validate_options(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/ridley/bootstrapper/context.rb', line 8

def validate_options(options = {})
  if options[:server_url].nil?
    raise Errors::ArgumentError, "A server_url is required for bootstrapping"
  end

  if options[:validator_path].nil?
    raise Errors::ArgumentError, "A path to a validator is required for bootstrapping"
  end
end

Instance Method Details

#boot_commandString

Returns:

  • (String)


100
101
102
103
104
105
106
107
108
# File 'lib/ridley/bootstrapper/context.rb', line 100

def boot_command
  cmd = template.evaluate(self)

  if sudo
    cmd = "sudo #{cmd}"
  end

  cmd
end

#chef_configString

Returns:

  • (String)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ridley/bootstrapper/context.rb', line 121

def chef_config
  body = <<-CONFIG
log_level        :info
log_location     STDOUT
chef_server_url  "#{server_url}"
validation_client_name "#{validator_client}"
CONFIG

  if node_name.present?
    body << %Q{node_name "#{node_name}"\n}
  else
    body << "# Using default node name (fqdn)\n"
  end

  if bootstrap_proxy.present?
    body << %Q{http_proxy        "#{bootstrap_proxy}"\n}
    body << %Q{https_proxy       "#{bootstrap_proxy}"\n}
  end

  if encrypted_data_bag_secret.present?
    body << %Q{encrypted_data_bag_secret "/etc/chef/encrypted_data_bag_secret"\n}
  end

  body
end

#chef_runString

Returns:

  • (String)


116
117
118
# File 'lib/ridley/bootstrapper/context.rb', line 116

def chef_run
  "chef-client -j /etc/chef/first-boot.json -E #{environment}"
end

#clean_commandString

Returns:

  • (String)


111
112
113
# File 'lib/ridley/bootstrapper/context.rb', line 111

def clean_command
  "rm /etc/chef/first-boot.json; rm /etc/chef/validation.pem"
end

#encrypted_data_bag_secretString?

Returns:

  • (String, nil)

Raises:



166
167
168
169
170
171
172
# File 'lib/ridley/bootstrapper/context.rb', line 166

def encrypted_data_bag_secret
  return nil if encrypted_data_bag_secret_path.nil?

  IO.read(encrypted_data_bag_secret_path).chomp
rescue Errno::ENOENT => encrypted_data_bag_secret
  raise Errors::EncryptedDataBagSecretNotFound, "Error bootstrapping: Encrypted data bag secret provided but not found at '#{encrypted_data_bag_secret_path}'"
end

#first_bootString

Returns:

  • (String)


148
149
150
# File 'lib/ridley/bootstrapper/context.rb', line 148

def first_boot
  attributes.merge(run_list: run_list).to_json
end

#validation_keyString

The validation key to create a new client for the node

Returns:

  • (String)

Raises:



157
158
159
160
161
# File 'lib/ridley/bootstrapper/context.rb', line 157

def validation_key
  IO.read(validator_path).chomp
rescue Errno::ENOENT
  raise Errors::ValidatorNotFound, "Error bootstrapping: Validator not found at '#{validator_path}'"
end