Devin
Devin

Reputation: 1021

Update hostname by connecting to Ruby on Rails application?

SCENARIO:

I'm looking to have a Linux box on startup connect to a Ruby on Rails website that has list of hostnames added to a Model. The Linux box updates it's own hostname from the next available hostname record in the list AND logs it's MAC address and IP Address in the rails site. Tough right?

Ruby on Rails Application

Host.rb (model) ip_address:string, mac_address:string, hostname:string, available:boolean

Tough part:

I'm trying to figure out how to have the Linux box connect the the RoRs site to see all my Host records, which will list all the hostnames. Ie:

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

.. then look at the next available hostname and updates itself to that hostname while logging the mac_address and ip_address to that record.

After the Linux box "downloads" the hostname the record would look like:

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

WHAT I'VE TRIED:

I placed a new file in /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

This works exactly how I want it to updating the hostname after reboot, but the hostname file is inside a static .zip file. I need to be able to update the .zip file dynamically or somehow have the Linux box connect to the site itself and pull a json list or something. This is where I'm stuck.

REASONING:

I'm going to be setting up about 150 of these machines and this would eliminate SO much hassle.

Anyone have any ideas?

Upvotes: 0

Views: 311

Answers (1)

kroky
kroky

Reputation: 1316

Create a controller on the Rails app having a method:

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

Let's say you have configured the route to access this as http://examplewebsite.com/hostname. You can get the hostname from the init shell script this way:

lwp-request http://examplewebsite.com/hostname > /etc/hostname

Of course, you should check for hostname depletion by checking the return value of lwp-request. You can use curl or another http client to request the necessary info but I think this is out of scope of the question.

Sending the MAC and IP address can be done by appending them as GET query parameters like this:

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

provided you have gathered the MAC and IP addresses in corresponding variables.

Then, on the rails side, you can log them like this:

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

Upvotes: 1

Related Questions