Clone via CLI?

Hello, everybody! I'm trying to clone on the command-line and written
this little rake task to do so:
namespace :hosts do
task :clone => :environment do
host_a = Host.find_by_name(ENV['HOSTA'])
host_b = host_a.clone
host_b['name'] = ENV['HOSTB']
host_b['mac'] = ENV['MAC']
host_b['ip'] = ENV['IP']
host_b.save
host_b.setBuild
end

calling rake like so seems to do the trick:
sudo -u foreman rake hosts:clone HOSTA=test1.collmedia.net
HOSTB=clone.collmedia.net MAC=08002732E78E IP=192.168.8.31

Again, this seems to work, but I'm skeptical . . . Do any of you
people know if anything is missing here, or if this will actually
produce a clone, just like the UI?

Oh! And does anyone know how I can not require the IP on the
command-line, but get it via proxy instead?

Thanks a lot,
Guy Matz

I was able to get something working by adding this as a task in my
hosts.rake. Suggestions are appreciated . . .

desc "Clone a machine. See source for instructions"

Clone a to b and get an IP from Foreman proxy

#sudo -u foreman RAILS_ENV=production rake hosts:clone
HOSTA=a.example.netHOSTB=
b.example.net MAC=08002732E78F

CLone a to b, specifying an IP

#sudo -u foreman RAILS_ENV=production rake hosts:clone
HOSTA=a.example.netHOSTB=
b.example.net MAC=08002732E78F IP=192.168.8.55
task :clone => :environment do
a = ENV['HOSTA']
b = ENV['HOSTB']
mac = ENV['MAC']
ip = ENV['IP'] || nil
# find host a . . .
host_a = Host.find_by_name(a)
if host_a.nil?
puts "Error cloning #{ENV['HOSTA']}: Can't find #{a}"
exit 1
end
# Clone a to b . . .
begin
host_b = host_a.clone
host_b['name'] = b
host_b['mac'] = mac
# Give host b an IP address (either from CLI or looked up)
unless host_b['ip'] = ip
s = Subnet.find_by_id(host_b[:subnet_id])
host_b['ip'] = s.unused_ip
end
# Save the new host and set it as "buildable"
host_b.save
host_b.setBuild
rescue => e
puts "Error cloning #{a}: #{e.message}"
exit 1
else
puts "#{b} created with IP #{host_b['ip']}"
end
end

··· On Thu, Jun 13, 2013 at 5:28 PM, Guy Matz wrote:

Hello, everybody! I’m trying to clone on the command-line and written
this little rake task to do so:
namespace :hosts do
task :clone => :environment do
host_a = Host.find_by_name(ENV[‘HOSTA’])
host_b = host_a.clone
host_b[‘name’] = ENV[‘HOSTB’]
host_b[‘mac’] = ENV[‘MAC’]
host_b[‘ip’] = ENV[‘IP’]
host_b.save
host_b.setBuild
end

calling rake like so seems to do the trick:
sudo -u foreman rake hosts:clone HOSTA=test1.collmedia.net
HOSTB=clone.collmedia.net MAC=08002732E78E IP=192.168.8.31

Again, this seems to work, but I’m skeptical . . . Do any of you
people know if anything is missing here, or if this will actually
produce a clone, just like the UI?

Oh! And does anyone know how I can not require the IP on the
command-line, but get it via proxy instead?

Thanks a lot,
Guy Matz


You received this message because you are subscribed to the Google Groups
“Foreman users” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.