将 ssh 连接存储在 Rails 中
我有一个 Rails 应用程序,需要通过 ssh 与几个服务器进行通信。我正在使用 Net::SSH 库,它运行得很好。然而,我希望能够在请求之间以某种方式缓存/存储 ssh 连接(类似于 OpenSSH 多路复用)。
因此,我无法将它们存储在像 Memcached 或 Redis 这样的键值存储中(因为 ssh 连接不可序列化)。
我不想将它们存储在会话中,因为它们是供所有用户使用的(此外我认为它也需要可序列化)。
我设法使用类变量和初始化器常量来实现这一点。我知道类变量不会在服务器之间复制(在生产中),而且我很确定初始化器常量也不会复制。像这样的东西:
initializer:
SSH = {}
model:
class Server
def connection
require 'net/ssh'
SSH[name] ||= Net::SSH.start(ip, "root", :password => password)
end
end
OpenSSH 多路复用会很棒,但我不确定是否可以通过 Net::SSH ruby 库来做到这一点(我又回到将主连接存储在某处) 。
还有其他解决方案吗?或者如果不是,哪一个是其中最不邪恶的?
I have a rails app that needs to communicate with a couple of servers through ssh. I'm using the Net::SSH library and it works great. I would like however to be able to cache/store the ssh connections somehow between requests (something like OpenSSH multiplexing).
So, i can't store them in a key-value store like Memcached or Redis (because ssh connections are not serializable).
I don't want to store them in a session because they are meant to be used by all users (and besides i think it needs to be serializable also).
I managed to get this working with class variables and initiliazer constants. I know that class variables don't replicate between servers (in production), and i'm pretty certain initializer constants also don't. Something like:
initializer:
SSH = {}
model:
class Server
def connection
require 'net/ssh'
SSH[name] ||= Net::SSH.start(ip, "root", :password => password)
end
end
OpenSSH multiplexing would be great but i'm not sure if i could do that through the Net::SSH ruby library (i'm back to storing the master connection somewhere).
Are there any other solutions? Or if not, which one is the least evil of them all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以委托给某种后台处理器,而不是尝试在请求之间共享套接字(这必然会导致痛苦和痛苦)?您可以设置 ssh 隧道并使用 DRb 进行通信,就像它只是一个本地网络守护进程或任何大量联网异步作业处理守护进程一样。
http://ruby-toolbox.com/categories/queueing.html
Perhaps rather than trying to share sockets across requests which is bound to end up causing pain and suffering you could delegate to a background processor of some kind? You could set up an ssh tunnel and use DRb to talk across it as if it was just a local network daemon, or any of the large number of networked asynchronous job handling daemons.
http://ruby-toolbox.com/categories/queueing.html
为了在请求之间保持 SSH 连接正常,您需要启动一个后台进程。后台进程可以打开管道或某种其他类型的进程间通信方法,句柄您可以以可序列化的方式存储。
请注意,这是一个不平凡的练习,这就是为什么我只对其进行了高级细节的描述。
To keep the SSH connection up between requests, you'll need to spawn off a background process. The background process can open up a pipe or some other sort of interprocess communication method, the handle to which you can store in a serializable way.
Note that this is a non-trivial exercise, which is why I've only described it at high-level detail.