首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过连接到Rails应用程序来更新主机名?

通过连接到Rails应用程序来更新主机名?
EN

Stack Overflow用户
提问于 2014-10-30 17:34:51
回答 1查看 255关注 0票数 0

设想情况:

我希望在启动时有一个Linux盒,连接到Rails网站,该网站将主机名列表添加到一个模型中。Linux框从列表中的下一个可用主机名记录中更新自己的主机名,并在rails站点中记录它的MAC地址和IP地址。很难吧?

Rails应用程序

Host.rb (模型) ip_address:string,mac_address:string,主机名:string,available:boolean

艰难的部分:

我正在努力弄清楚如何让Linux盒连接RoRs站点来查看我的主机记录,这些记录将列出所有主机名。Ie:

代码语言:javascript
复制
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框“下载”之后,记录的主机名如下所示:

代码语言:javascript
复制
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中放置了一个新文件

代码语言:javascript
复制
#! /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台这样的机器,这样就可以消除很多麻烦了。

有人有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-30 18:06:14

在Rails应用程序上创建一个具有以下方法的控制器:

代码语言:javascript
复制
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-请求的返回值来检查主机名耗尽情况。您可以使用curl或另一个http客户端请求必要的信息,但我认为这是问题的范围之外。

发送MAC和IP地址可以通过将它们附加为GET查询参数来完成,如下所示:

lwp-request http://examplewebsite.com/hostname?mac=$mac&ip=$ip > /etc/hostname

如果您已经在相应的变量中收集了MAC和IP地址。

然后,在rails方面,您可以这样记录它们:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26659701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档