简体   繁体   English

使用SSH-NET的Ruby脚本中的文件路径问题

[英]Filepaths issue in ruby script using ssh-net

I am currently writing a ruby script to allow me to upload .war files into a VPC. 我目前正在编写一个ruby脚本,以允许我将.war文件上传到VPC。 To update a java service in our cloud I simply build with maven locally and replace the .war file in the VPC with the file I have created via the build. 要在我们的云中更新Java服务,我只需在本地使用maven进行构建,然后将VPC中的.war文件替换为通过构建所创建的文件即可。 My problem lies in my file paths in my script. 我的问题出在脚本中的文件路径中。 It seems that when even trying to run simple bash commands such as 'mv', or 'rm' I am presented with the following error - 似乎即使尝试运行简单的bash命令(例如“ mv”或“ rm”),也会出现以下错误-

mv: missing destination file operand after `/tmp/asset-manager-service-1.0.2-SNAPSHOT.war'
Try `mv --help' for more information.
bash: line 1: /srv/asset-manager-service/war/: Is a directory

I thought that maybe this may be a problem with the fact I have a variable just before the second mv directory? 我认为这可能与我在第二个mv目录之前有一个变量的事实有关吗? The same error applies when I try to use 'rm' as well. 当我尝试使用“ rm”时,也会出现相同的错误。 I am new to ruby, so this may not be pretty, but here is some of the code in question. 我是红宝石的新手,所以这可能并不漂亮,但这是一些有问题的代码。

#handles the old war file and pushes up the new one.
  def handle_war(value)
    war_file_name = File.basename("#$warfile")
    local_war_path = File.absolute_path("#$warfile")
    puts File.exist?(local_war_path)
    remote_war_path = "/tmp/#{war_file_name}"

    puts "IP is " + value
    puts "CONNECTING to ubuntu@#{value}.."
    Net::SSH.start("#{value}", "ubuntu") do |ssh|
      puts 'Connected.'
      puts "SENDING #{local_war_path} to VPC /tmp folder - #{remote_war_path}"
      ssh.sftp.upload!('asset-manager-service-1.0.2-SNAPSHOT.war',
                       remote_war_path)

      print "SENT - New .war created at /tmp/#{war_file_name}"

      old_war_file = ssh.exec!("ls /srv/#{$service_dir}/war/*war")
      old_war_file_name = File.basename(old_war_file)
      puts 'What do you want to do with this old war file? - (delete/backup)'
      puts old_war_file
      input = gets.chomp
      if input=='delete'
        puts ssh.exec!("sudo rm #{old_war_file}")
        puts 'If no errors thrown - old .war file deleted successfully.'
      elsif input == 'backup'
        puts ssh.exec!("sudo mv #{old_war_file} srv/#$service_dir/war_backup/#{old_war_file_name}")
        puts 'If no errors thrown - old .war file backed up successfully at'
        puts "srv/#$service_dir/war_backup/#{old_war_file_name}"
      else
        puts 'no valid option chosen. Leaving .war file as is. You will have to delete or backup the .war manually.'
      end

      puts "MOVING the new war file from tmp into #{$service_dir}/war directory"
      puts ssh.exec!("sudo mv /tmp/#{war_file_name} /srv/#{$service_dir}/war/")
      puts 'done!'
    end
  end

The main issue here I think is that there seems to be some funny business when I use variables as part of the filepaths.. I am unsure as to why this should make any difference though.I understand this question may be a bit hard to grasp out of context but I am really banging my head here. 我认为这里的主要问题是,当我将变量用作文件路径的一部分时,似乎有些事可笑。.我不确定为什么这会有所不同。我知道这个问题可能很难理解脱离上下文,但我真的在这里敲头。 Long story short - 长话短说 -

  1. Do filepaths change when using File.basename? 使用File.basename时文件路径会更改吗?
  2. Is there a difference from using a variable in a filepath as opposed to a hard coded string? 与在文件路径中使用变量而不是在硬编码字符串上有区别吗?
  3. Are there any glaring mistakes in my code that could be the problem? 我的代码中是否有任何明显的错误可能是问题所在?

It would appear that war_file_name has a newline at the end of it. 似乎war_file_name有换行符。

That would cause the shell to see 那会导致外壳看到

sudo mv /tmp/#{war_file_name} /srv/#{$service_dir}/war/

as

sudo mv /tmp/#{war_file_name}
/srv/#{$service_dir}/war/

which would give both the mv missing destination error and the "is a directory" error from the shell trying to execute a directory as a command. 这会同时导致mv缺少目标错误和shell尝试将目录作为命令执行时出现的“是目录”错误。

(In general you don't want to use the output from ls for any scripting work as it is unreliable and unsafe in many cases. In this case the output from echo would work just as well for example, though would still have many/all of the safety problems with "odd" filenames. Working around those issues would require some more scripting but is doable.) (通常,您不希望将ls的输出用于任何脚本工作,因为在许多情况下它是不可靠和不安全的。在这种情况下, echo的输出同样会很好,例如,尽管仍然有很多/全部文件名“奇数”的安全性问题。解决这些问题将需要更多脚本,但可行。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM