Class: Hazetug::Tug::KnifeBase
- Inherits:
-
Hazetug::Tug
- Object
- Hazetug::Tug
- Hazetug::Tug::KnifeBase
- Defined in:
- lib/hazetug/tug/knife_base.rb
Constant Summary
Constants inherited from Hazetug::Tug
Constants included from NetSSH::Mixin
NetSSH::Mixin::NET_SSH_OPTIONS
Instance Attribute Summary
Attributes inherited from Hazetug::Tug
Instance Method Summary collapse
-
#bootstrap_cleanup ⇒ Object
After knife bootstrap hook.
-
#bootstrap_config ⇒ Object
Configures knife bootstrap by setting the necessary options.
-
#bootstrap_init ⇒ Object
Pre knife bootstrap hook.
-
#bootstrap_options ⇒ Object
Extracts bootstrap options from the hazetug configuration.
-
#bootstrap_run ⇒ Object
Initiates knife bootstrap run.
-
#bootstrap_server ⇒ Object
Bootstraps the remote server using knife bootstrap.
-
#check_bootstrap_files!(*list_of_opts) ⇒ Object
Check if files exist otherwise fail.
-
#hazetug_identity ⇒ Object
Lookup ssh identity key(s) in the hazetug configuration.
-
#initialize(config = {}) ⇒ KnifeBase
constructor
A new instance of KnifeBase.
-
#knife ⇒ Object
Initializes knife bootstrap, default output is redirected into file.
Methods inherited from Hazetug::Tug
Methods included from UI::Mixin
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_cleanup ⇒ Object
After knife bootstrap hook.
107 108 |
# File 'lib/hazetug/tug/knife_base.rb', line 107 def bootstrap_cleanup end |
#bootstrap_config ⇒ Object
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] = [opt] end @chef_option_list.each do |opt| Chef::Config[opt] = [opt] end # Check ssh identity cred = [ hazetug_identity, [:identity_file], [: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_init ⇒ Object
Pre knife bootstrap hook.
103 104 |
# File 'lib/hazetug/tug/knife_base.rb', line 103 def bootstrap_init end |
#bootstrap_options ⇒ Object
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 ||= 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 = [:opts][:bootstrap] || 'bootstrap.erb' validation = config[:chef_validation_key] || 'validation.pem' opts[:validation_key] = File.(validation) opts[:template_file] = File.(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_run ⇒ Object
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_server ⇒ Object
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.); 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| [k]} files = list.map {|f| File.(f.to_s)} notfound = files.select {|f| !File.exist?(f)} notfound.empty? or raise Hazetug::Exception, "File(s) not found: #{notfound.join(', ')}" end |
#hazetug_identity ⇒ Object
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.(key_path) end end |
#knife ⇒ Object
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 |