本文介绍了通过带有备用端口的 sftp/scp 进行指南针/sass 远程主题化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让指南针/sass 观察我本地计算机上的更改,并使用自定义 config.rb 脚本远程反映这些更改.net::sftp 有效,但我的服务器需要自定义 ssh 端口.我找不到任何使 sftp 与备用端口一起工作的 mod,所以我现在尝试 net:scp,问题是我不知道使用 net:scp 上传的正确命令结构,想看看如果有人可以帮助我.这是我的代码:

I am trying to get compass/sass to watch changes on my local computer and reflect those changes remotely using a custom config.rb script. net::sftp works but my server requires a custom ssh port. I couldn't find any mods to make sftp work with an alternate port so im trying net:scp now, the problem is I dont know the proper command structure to upload using net:scp and wanted to see if someone can help me. Here is my code:

  # Require any additional compass plugins here.
require 'net/ssh'
require 'net/scp'

# SFTP Connection Details - Does not support alternate ports os SSHKeys, but could with mods
remote_theme_dir_absolute = '/home2/trinsic/public_html/scottrlarson.com/sites/all/themes/ gateway_symbology_zen/css'
sftp_host = 'xxx.xxx.xxx.xxx' # Can be an IP
sftp_user = 'user' # SFTP Username
sftp_pass = 'password' # SFTP Password

# Callback to be used when a file change is written. This will upload to a remote WP install
on_stylesheet_saved do |filename|
  $local_path_to_css_file = css_dir + '/' + File.basename(filename)

  Net::SSH.start( sftp_host, sftp_user, {:password => sftp_pass,:port => 2222} ) do ssh.scp.upload! $local_path_to_css_file, remote_theme_dir_absolute + '/' + File.basename(filename)
    end
  puts ">>>> Compass is polling for changes. Press Ctrl-C to Stop"
end

#
# This file is only needed for Compass/Sass integration. If you are not using
# Compass, you may safely ignore or delete this file.
#
# If you'd like to learn more about Sass and Compass, see the sass/README.txt
# file for more information.
#


# Change this to :production when ready to deploy the CSS to the live server.
environment = :development
#environment = :production

# In development, we can turn on the FireSass-compatible debug_info.
firesass = false
#firesass = true


# Location of the theme's resources.
css_dir         = "css"
sass_dir        = "sass"
extensions_dir  = "sass-extensions"
images_dir      = "images"
javascripts_dir = "js"


# Require any additional compass plugins installed on your system.
#require 'ninesixty'
#require 'zen-grids'

# Assuming this theme is in sites/*/themes/THEMENAME, you can add the partials
# included with a module by uncommenting and modifying one of the lines below:
#add_import_path "../../../default/modules/FOO"
#add_import_path "../../../all/modules/FOO"
#add_import_path "../../../../modules/FOO"


##
## You probably don't need to edit anything below this.
##

# You can select your preferred output style here (can be overridden via the command line):
# output_style = :expanded or :nested or :compact or :compressed
output_style = (environment == :development) ? :expanded : :compressed

# To enable relative paths to assets via compass helper functions. Since Drupal
# themes can be installed in multiple locations, we don't need to worry about
# the absolute path to the theme from the server root.
relative_assets = true

# To disable debugging comments that display the original location of your selectors. Uncomment:
# line_comments = false

# Pass options to sass. For development, we turn on the FireSass-compatible
# debug_info if the firesass config variable above is true.
sass_options = (environment == :development && firesass == true) ? {:debug_info => true} : {}

运行命令时出现错误:compass watch:

I get an error when I run the command: compass watch:

NoMethodError on line ["17"] of K: undefined method `upload!' for #<Net::SSH::Co
nnection::Session:0x000000036bb220>
Run with --trace to see the full backtrace

推荐答案

我也需要一个解决方案,但没有在任何地方找到任何令人满意的答案.在阅读了 Ruby Net::ssh 文档和 Compass 的一些来源后,这是我将 CSS 和源映射上传到具有非标准端口和强制公钥授权的远程 SSH 服务器的解决方案:

I needed a solution for this too but did not find any satisfying answer anywhere.After reading the Ruby Net::ssh documentation and some source of Compass, this is my solution to upload CSS and sourcemap to a remote SSH server with non-standard port and forced public-key authorisation:

首先确保您安装了所需的 gems

First make sure you have the required gems installed

sudo gem install net-ssh net-sftp

然后将其添加到您的 config.rb

then add this to your config.rb

# Add this to the first lines of your config.rb
require 'net/ssh'
require 'net/sftp'

...
# Your normal compass config comes here
...

# At the end of your config.rb add the config for the upload code
remote_theme_dir_absolute = '/path/to/my/remote/stylesheets'

sftp_host = 'ssh_host' # Can be an IP
sftp_user = 'ssh_user' # SFTP Username

on_stylesheet_saved do |filename|

  # You can use the ssh-agent for authorisation.
  # In this case you can remove the :passphrase from the config and set :use_agent => true.

  Net::SFTP.start(

    sftp_host,
    sftp_user ,
    :port => 10022,
    :keys_only => true,
    :keys => ['/path/to/my/private/id_rsa'],
    :auth_methods => ['publickey'],
    :passphrase => 'my_secret_passphrase',
    :use_agent => false,
    :verbose => :warn

  ) do |sftp|

      puts sftp.upload! css_dir + '/app.css', remote_theme_dir_absolute + '/' + 'app.css'

  end

end

on_sourcemap_saved do |filename|

  # You can use the ssh-agent for authorisation.
  # In this case you can remove the :passphrase from the config and set :use_agent true.

  Net::SFTP.start(
    sftp_host,
    sftp_user ,
    :port => 10022,
    :keys_only => true,
    :keys => ['/path/to/my/private/id_rsa'],
    :auth_methods => ['publickey'],
    :passphrase => 'my_secret_passphrase',
    :use_agent => false,
    :verbose => :warn
  ) do |sftp|

    puts sftp.upload! css_dir + '/app.css.map', remote_theme_dir_absolute + '/' + 'app.css.map'

  end

end

在这对我有用之前,这是相当多的反复试验.一些失败点是:

It was quite some trial and error until this worked for me.Some points of failure were:

  • 如果没有可用的 ssh-agent 连接将失败,直到您设置 :ssh_agent =>;false 明确
  • 如果您不使用 :keys 限制可用密钥,则将一个接一个地尝试所有可用密钥.如果您使用 ssh-agent 并安装了 3 个以上的密钥,则如果您尝试过多对当前连接的服务器无效的密钥,远程服务器将关闭连接.
  • 在任何连接问题上将详细级别设置为 :verbose =>:debug 看看发生了什么.请记住停止 compass watch 并重新启动以确保配置更改适用.
  • If no ssh-agent is available connection will fail until you set :ssh_agent => false explicitly
  • If you do not limit the available keys with :keys all available keys will be tried one after another. If you use the ssh-agent and have more than 3 keys installed chanches are high that the remote server will close the connection if you try too much keys that are not valid for the server you currently connect.
  • On any connection issue set verbosity level to :verbose => :debug to see what is going on. Remember to stop the compass watch and restart to ensure configuration changes apply.

这篇关于通过带有备用端口的 sftp/scp 进行指南针/sass 远程主题化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 16:44