Class: Chef::Knife::BaremetalcloudServerCreate

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/baremetalcloud_server_create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bmcObject

Returns the value of attribute bmc.



16
17
18
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 16

def bmc
  @bmc
end

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay.



16
17
18
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 16

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#addNode(config, options = {}) ⇒ Object

Method to add baremetalcloud’s node and configure



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 149

def addNode(config, options = {})
  
  # Get the API response
  response = @bmc.add_server_by_configuration(config,options).body
  
  # Error handling
  errorHandling(response)
  
  # get Server id
  serverId = response[:server][:id]
  
  # Loop while server's status is not "Active"
  print "Waiting for server#{serverId} to be ready"
  print(".") until isNodeReady(serverId){
    print "done\n"
  }
  
  # Loop while SSH is not available and sleep @initial_sleep_delay seconds
  print "Connecting to server#{serverId}" 
  print(".") until testSSH(response[:server][:ip][:address]) {
    print "done\n"
  }

end

#bootstrapNodeObject

Method to configure Knife Bootstrap object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 125

def bootstrapNode()
  puts "Bootstrapping node id #{locateConfigValue(:chef_node_name)}"
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = locateConfigValue(:chef_node_name)
  bootstrap.config[:run_list] = locateConfigValue(:run_list)
  bootstrap.config[:identity_file] = locateConfigValue(:identity_file)
  bootstrap.config[:ssh_user] = locateConfigValue(:ssh_user)
  bootstrap.config[:ssh_password] = locateConfigValue(:ssh_password)
  bootstrap.config[:use_sudo] = true unless locateConfigValue(:ssh_user) == 'root'
  bootstrap.config[:environment] = config[:environment]
  bootstrap
end

#errorHandling(response) ⇒ Object

Method to handle baremetalcloud errors



139
140
141
142
143
144
145
146
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 139

def errorHandling(response)
  # Error handling
  unless response[:error].nil?
    puts "#{ui.color("ERROR:", :bold)} #{response[:error][:name]}"
    puts "Description: #{response[:error][:description]}"
    exit 1 
  end
end

#isNodeReady(serverId) ⇒ Object

Method to check when “system” credentials are available Node’s IP Address is updated



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 52

def isNodeReady(serverId)
  
  # Sleep @initial_sleep_delay seconds
  sleep @initial_sleep_delay
  
  # Get server's status
  response = @bmc.get_server(serverId).body 
  
  # 1855 and 1955 have only "system" credentials
  if ( response[:server][:login].class == Hash )
    if ( response[:server][:login][:name] == "system" )
      config[:ssh_user] = response[:server][:login][:username]
      config[:ssh_password] = response[:server][:login][:password]
      config[:chef_node_name] =  response[:server][:ip][:address]
      return true
    end
  elsif ( response[:server][:login].class == Array )  # M600 and M610 have iDRAC credentials
    response[:server][:login].each do |r|
      if ( r[:name] == "system")
        config[:ssh_user] = r[:username]
        config[:ssh_password] = r[:password]
        config[:chef_node_name] =  response[:server][:ip][:address]
        return true
      end
    end
  end
  false
end

#locateConfigValue(key) ⇒ Object

Method to lacate configuration variables



119
120
121
122
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 119

def locateConfigValue(key)
  key = key.to_sym
  config[key] || Chef::Config[:knife][key]
end

#runObject

Plugin method called by Knife



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 200

def run
  
  # sleeptime for testing ssh connectivity
  @initial_sleep_delay = 3

  # Verify mandatory arguments
  verifyArguments
  
  # Configure the API abstraction @bmc
  @bmc = Fog::Compute.new({
    :bare_metal_cloud_username => locateConfigValue(:baremetalcloud_username),
    :bare_metal_cloud_password => locateConfigValue(:baremetalcloud_password),
    :provider => 'BareMetalCloud'
  })
  
  # Options
  options = {
    :imageName => locateConfigValue(:imageName),
    :name => locateConfigValue(:name)
  }
  
  # Add server method
  addNode(locateConfigValue(:config), options)
  
  # Configure bootstrap and trigger "run" to start up
  bootstrapNode().run
  
end

#testSSH(hostname) ⇒ Object

Method to test if SSH service is running on a node



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 82

def testSSH(hostname)
  sleep @initial_sleep_delay
  tcp_socket = TCPSocket.new(hostname, "22")
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
    yield
    true
  else
    false
  end
  rescue SocketError
    sleep 2
    false
  # Connection timed out
  rescue Errno::ETIMEDOUT
    false
  # Operation not permitted
  rescue Errno::EPERM
    false
  # Connection refused  
  rescue Errno::ECONNREFUSED
    sleep 2
    false
  # No route to host
  rescue Errno::EHOSTUNREACH
    sleep 2
    false
  # Network is unreachable
  rescue Errno::ENETUNREACH
    sleep 2
    false
  ensure
    tcp_socket && tcp_socket.close
end

#verifyArgumentsObject

Method to verify mandatory arguments



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/chef/knife/baremetalcloud_server_create.rb', line 175

def verifyArguments
  # Parameters :baremetalcloud_username and :baremetalcloud_password are mandatory
  unless config[:baremetalcloud_username]
    ui.error("--username is a mandatory parameter")
    exit 1
  end
  
  unless config[:baremetalcloud_password]
    ui.error("--password is a mandatory parameter")
    exit 1
  end
  
  unless config[:config]
    ui.error("--configuration is a mandatory parameter")
    exit 1
  end
  
  unless config[:imageName]
    ui.error("--image is a mandatory parameter")
    exit 1
  end

end