lib/ssh.rb in forj-1.0.3 vs lib/ssh.rb in forj-1.0.4

- old
+ new

@@ -13,99 +13,107 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -#~ require 'rubygems' - +# ~ require 'rubygems' +require 'lorj' require 'highline/import' +require 'cloud_connection.rb' -#~ require 'security.rb' -#~ include SecurityGroup +# ~ require 'security.rb' +# ~ include SecurityGroup # # ssh module # module Forj - module Ssh + # This module provides the behavior to create a ssh connection + module Ssh + def self.connect(name, account) + o_cloud = Forj::CloudConnection.connect(account) - def Ssh.connect(name, oConfig) - oForjAccount = Lorj::Account.new(oConfig) + PrcLib.state(format("Getting information about forge '%s'", name)) - oForjAccount.ac_load() + o_forge = o_cloud.get(:forge, name) - aProcesses = [] + if o_forge[:servers].count > 0 + if account[:box_ssh] + o_server = validate_server_name(name, account, o_forge) - # Defines how to manage Maestro and forges - # create a maestro box. Identify a forge instance, delete it,... - aProcesses << File.join($LIB_PATH, 'forj', 'ForjCore.rb') + if !o_server.nil? + ssh_connection(account, o_cloud, name, o_server[:id]) + else + PrcLib.debug("server '%s.%s' was not found", + account[:box_ssh], name) + PrcLib.high_level_msg("server '%s.%s' was not found.\n", + account[:box_ssh], name) + end + else + o_server_number = select_forge_server(o_forge) - # Defines how cli will control FORJ features - # boot/down/ssh/... - aProcesses << File.join($LIB_PATH, 'forj', 'ForjCli.rb') + ssh_connection( + account, + o_cloud, + name, + o_forge[:servers][o_server_number][:id] + ) + end + else + PrcLib.high_level_msg("No server(s) found for instance name '%s' \n", + name) + end + end - oCloud = Lorj::CloudCore.new(oForjAccount, oConfig[:account_name], aProcesses) + def self.select_forge_server(o_forge) + # Ask the user to get server(s) to create ssh connection + server_list = [] + index = 0 + s_default = nil + o_forge[:servers].each do |server| + server_list[index] = server[:name] + s_default = server[:name] if server[:name].include? 'maestro' + index += 1 + end - PrcLib.state("Getting information about forge '%s'" % name) + say(format( + 'Select box for ssh connection %s', + ((s_default.nil?) ? '' : format( + 'Default: ' + "|%s|\n", s_default + )) + ) + ) + value = choose do |q| + q.choices(*server_list) + q.default = s_default unless s_default.nil? + end - oForge = oCloud.Get(:forge, name) + o_server_number = server_list.index(value) + o_server_number + end - if oForge[:servers].count > 0 - if oConfig[:box_ssh] - box_ssh = oConfig[:box_ssh] + def self.ssh_connection(account, o_cloud, name, server_id) + # Property for :forge + account.set(:instance_name, name) + # Property for :ssh + account.set(:forge_server, server_id) - oServer = nil - regex = Regexp.new('%s\.%s$' % [box_ssh, name]) + o_cloud.create(:ssh) + end - oForge[:servers].each { |server| - oName = server[:name] - next if (regex =~ oName) == nil - oServer = server - break - } + def self.validate_server_name(name, account, o_forge) + box_ssh = account[:box_ssh] - if oServer != nil - #Property for :forge - oConfig.set(:instance_name, name) - #Property for :ssh - oConfig.set(:forge_server, oServer[:id]) + o_server = nil + regex = Regexp.new(format('%s\.%s$', box_ssh, name)) - oCloud.Create(:ssh) - else - PrcLib.debug("server '%s.%s' was not found" % [oConfig[:box_ssh], name] ) - PrcLib.high_level_msg("server '%s.%s' was not found.\n" % [oConfig[:box_ssh], name] ) - end - - else - #Ask the user to get server(s) to create ssh connection - serverList = [] - index = 0 - sDefault = nil - oForge[:servers].each{ |server| - serverList[index] = server[:name] - sDefault = server[:name] if server[:name].include? "maestro" - index = index + 1 - } - - say("Select box for ssh connection %s" % ((sDefault.nil?)? "" : "Default: " + "|%s|\n" % sDefault)) - value = choose { | q | - q.choices(*serverList) - q.default = sDefault if not sDefault.nil? - } - - oServerNumber = serverList.index(value) - - #Property for :forge - oConfig.set(:instance_name, name) - #Property for :ssh - oConfig.set(:forge_server, oForge[:servers][oServerNumber][:id]) - - oCloud.Create(:ssh) - - end - else - PrcLib.high_level_msg("No server(s) found for instance name '%s' \n" % name ) - end - + o_forge[:servers].each do |server| + o_name = server[:name] + next if (regex =~ o_name).nil? + o_server = server + break end - end + + o_server + end + end end