lib/ssh.rb in forj-1.0.1 vs lib/ssh.rb in forj-1.0.2

- old
+ new

@@ -13,38 +13,99 @@ # 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 'security.rb' -include SecurityGroup +require 'highline/import' + +#~ require 'security.rb' +#~ include SecurityGroup + # # ssh module # -module Ssh - def connect(name, server, oConfig) - # Following line to remove as soon as ssh function is implemented with forj-lib framework - Logging.warning("This function may not work appropriately. Currenty under development. Thank you for your understanding.") +module Forj + module Ssh - msg = 'logging into %s : %s' % [name, server] - Logging.info(msg) + def Ssh.connect(name, oConfig) + oForjAccount = Lorj::Account.new(oConfig) - oForjAccount = ForjAccount.new(oConfig) + oForjAccount.ac_load() - oForjAccount.ac_load() + aProcesses = [] - oKey = SecurityGroup.keypair_detect(oForjAccount.get(:keypair_name), oForjAccount.get(:keypair_path)) + # 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') - update = '%s/ssh.sh -u %s' % [ $LIB_PATH, oConfig.get(:account_name)] - connection = '%s/ssh.sh %s %s %s' % [$LIB_PATH, name, server, File.join(oKey[:keypair_path],oKey[:private_key_name]) ] + # Defines how cli will control FORJ features + # boot/down/ssh/... + aProcesses << File.join($LIB_PATH, 'forj', 'ForjCli.rb') - # update the list of servers - Logging.debug("Executing '%s'" % update) - Kernel.system(update) + oCloud = Lorj::CloudCore.new(oForjAccount, oConfig[:account_name], aProcesses) - # connect to the server - Logging.debug("Executing '%s'" % connection) - Kernel.system(connection) + PrcLib.state("Getting information about forge '%s'" % name) + + oForge = oCloud.Get(:forge, name) + + if oForge[:servers].count > 0 + if oConfig[:box_ssh] + box_ssh = oConfig[:box_ssh] + + oServer = nil + regex = Regexp.new('%s\.%s$' % [box_ssh, name]) + + oForge[:servers].each { |server| + oName = server[:name] + next if (regex =~ oName) == nil + oServer = server + break + } + + if oServer != nil + #Property for :forge + oConfig.set(:instance_name, name) + #Property for :ssh + oConfig.set(:forge_server, oServer[:id]) + + 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 + + end end end