Class: Hazetug::Tug::KnifeBase

Inherits:
Hazetug::Tug show all
Defined in:
lib/hazetug/tug/knife_base.rb

Direct Known Subclasses

Knife, Solo

Constant Summary

Constants inherited from Hazetug::Tug

LOGDIR

Constants included from NetSSH::Mixin

NetSSH::Mixin::NET_SSH_OPTIONS

Instance Attribute Summary

Attributes inherited from Hazetug::Tug

#config, #options

Instance Method Summary collapse

Methods inherited from Hazetug::Tug

[], #bootstrap, #tug_name

Methods included from UI::Mixin

included

Constructor Details

#initialize(config = {}) ⇒ KnifeBase

Returns a new instance of KnifeBase.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hazetug/tug/knife_base.rb', line 50

def initialize(config={})
  super
  @bootstrap_option_list = [
    :template_file,
    :identity_file,
    :ssh_user,
    :ssh_password,
    :host_key_verify
  ]
  @chef_option_list = [ :environment ]
end

Instance Method Details

#bootstrap_cleanupObject

After knife bootstrap hook.



107
108
# File 'lib/hazetug/tug/knife_base.rb', line 107

def bootstrap_cleanup
end

#bootstrap_configObject

Configures knife bootstrap by setting the necessary options.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hazetug/tug/knife_base.rb', line 76

def bootstrap_config
  knife.add_variable_to_bootstrap_context(:@hazetug, config)

  @bootstrap_option_list.each do |opt|
    knife.config[opt] = bootstrap_options[opt]
  end

  @chef_option_list.each do |opt|
    Chef::Config[opt] = bootstrap_options[opt]
  end

  # Check ssh identity
  cred = [
    hazetug_identity, bootstrap_options[:identity_file],
    bootstrap_options[:ssh_password]
  ]

  if cred.all?(&:nil?)
    msg = "No identity (inc. ssh_password) found. Check ssh_password," <<
      " identity_file options or #{config[:compute_name]}_ssh_keys " <<
      "hazetug parameter."

    raise Hazetug::Exception, msg
  end
end

#bootstrap_initObject

Pre knife bootstrap hook.



103
104
# File 'lib/hazetug/tug/knife_base.rb', line 103

def bootstrap_init
end

#bootstrap_optionsObject

Extracts bootstrap options from the hazetug configuration.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/hazetug/tug/knife_base.rb', line 128

def bootstrap_options
  @bootstrap_options ||= begin
    opts = {}

    opts[:ssh_user] = config[:ssh_user] || 'root'
    opts[:ssh_password] = config[:ssh_password]
    if opts[:ssh_password].nil?
      opts[:identity_file] = config[:identity_file] || hazetug_identity
    end

    template = options[:opts][:bootstrap] || 'bootstrap.erb'
    validation = config[:chef_validation_key] || 'validation.pem'

    opts[:validation_key] = File.expand_path(validation)
    opts[:template_file]  = File.expand_path(template)
    opts[:environment]  =  config[:chef_environment]
    opts[:host_key_verify] = config[:host_key_verify] || false
    opts[:chef_server_url] = config[:chef_server_url]
    opts
  end
end

#bootstrap_runObject

Initiates knife bootstrap run.



111
112
113
114
# File 'lib/hazetug/tug/knife_base.rb', line 111

def bootstrap_run
  knife.name_args = [config[:public_ip_address]]
  knife.run
end

#bootstrap_serverObject

Bootstraps the remote server using knife bootstrap.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hazetug/tug/knife_base.rb', line 63

def bootstrap_server
  bootstrap_config

  bootstrap_init
  bootstrap_run
  bootstrap_cleanup
rescue Hazetug::Exception => e
  ui.error(e.message); 1
ensure  
  knife and knife.ui.stdout.close
end

#check_bootstrap_files!(*list_of_opts) ⇒ Object

Check if files exist otherwise fail.



160
161
162
163
164
165
166
# File 'lib/hazetug/tug/knife_base.rb', line 160

def check_bootstrap_files!(*list_of_opts)
  list = list_of_opts.map {|k| bootstrap_options[k]}
  files = list.map {|f| File.expand_path(f.to_s)}
  notfound = files.select {|f| !File.exist?(f)}
  notfound.empty? or 
    raise Hazetug::Exception, "File(s) not found: #{notfound.join(', ')}"
end

#hazetug_identityObject

Lookup ssh identity key(s) in the hazetug configuration.



151
152
153
154
155
156
157
# File 'lib/hazetug/tug/knife_base.rb', line 151

def hazetug_identity
  @hazetug_identity ||= begin
    compute = config[:compute_name]
    key_path = (Hazetug::Config["#{compute}_ssh_keys"] || []).first
    key_path and File.expand_path(key_path)       
  end
end

#knifeObject

Initializes knife bootstrap, default output is redirected into file.



117
118
119
120
121
122
123
124
125
# File 'lib/hazetug/tug/knife_base.rb', line 117

def knife
  @knife ||= begin
    lf = create_log_file
    Chef::Knife::Bootstrap.load_deps
    kb = Chef::Knife::Bootstrap.new
    kb.ui = Chef::Knife::UI.new(lf, lf, lf, {verbosity: 2})
    kb
  end
end