情景:
我希望在启动时有一个Linux盒连接到Ruby on Rails网站,该网站上有一个主机名列表添加到模型中。 Linux框从列表中的下一个可用主机名记录更新它自己的主机名,并在rails站点中记录它的MAC地址和IP地址。对不对?
Ruby on Rails应用程序
Host.rb(model)ip_address:string,mac_address:string,hostname:string,available:boolean
艰难的一部分:
我正在试图弄清楚如何让Linux盒连接RoRs站点以查看我的所有主机记录,这些记录将列出所有主机名。即:
id:1 hostname: "hostname-1", ip_address: nil, mac_address: nil, available: true
id:2 hostname: "hostname-2", ip_address: nil, mac_address: nil, available: true
id:3 hostname: "hostname-3", ip_address: nil, mac_address: nil, available: true
..然后查看下一个可用的主机名,并在将mac_address和ip_address记录到该记录时将自身更新为该主机名。
在Linux框“下载”主机名之后,记录将如下所示:
id:1 hostname: "hostname-1", ip_address: "192.168.1.10", mac_address: "00:13:EF:35:GH:00":, available: false
id:2 hostname: "hostname-2", ip_address: nil, mac_address: nil, available: true
id:3 hostname: "hostname-3", ip_address: nil, mac_address: nil, available: true
我做了什么:
我在/etc/init.d/selfconfig
中放置了一个新文件#! /bin/sh
# /etc/init.d/selfconfig
USER=root
HOME=/root
export USER HOME
# download remote /zip file with hostname inside
/usr/bin/wget -o /home/admin/selfconfig.zip http://examplewebsite.com/selfconfig.zip
# unzip the .zip file
/usr/bin/unzip -o /home/admin/selfconfig.zip /home/admin
# copy the hostname file to the primary hostname location
cp /home/admin/hostname /etc/hostname
exit 0
这完全符合我希望它在重新启动后更新主机名的方式,但主机名文件位于静态.zip文件中。我需要能够动态更新.zip文件,或者以某种方式将Linux盒连接到站点本身并拉出json列表或其他东西。这就是我被困住的地方。
推理:
我将设置约150台这样的机器,这将消除这么多的麻烦。
有人有什么想法吗?
答案 0 :(得分:1)
在Rails应用程序上创建一个具有方法的控制器:
def hostname
host = Host.where(available: true).order(:id).first
if host.present?
render :text => host.hostname
else
render :text => 'No hostname available'
end
end
我们假设您已将路由配置为http://examplewebsite.com/hostname
。您可以通过以下方式从init shell脚本获取主机名:
lwp-request http://examplewebsite.com/hostname > /etc/hostname
当然,您应该通过检查lwp-request的返回值来检查主机名耗尽。您可以使用curl或其他http客户端来请求必要的信息,但我认为这超出了问题的范围。
发送MAC和IP地址可以通过将它们附加为GET查询参数来完成,如下所示:
lwp-request http://examplewebsite.com/hostname?mac=$mac&ip=$ip > /etc/hostname
前提是您已在相应的变量中收集了MAC和IP地址。
然后,在导轨方面,您可以像这样记录它们:
def hostname
host = Host.where(available: true).order(:id).first
if host.present?
host.update_attributes({
ip_address: params[:ip],
mac_address: params[:mac]
})
render :text => host.hostname
else
render :text => 'No hostname available'
end
end