Log In | Get Help   
Home My Page Projects Code Snippets Project Openings BonFIRE VCOC Demonstration Kit
Summary Activity SCM Files Wiki
[bonfiredemokit] View of /virt-cluster/build-storages.rb
[bonfiredemokit] / virt-cluster / build-storages.rb Repository:
ViewVC logotype

View of /virt-cluster/build-storages.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (download) (annotate)
Tue Oct 9 16:14:55 2012 UTC (11 years, 7 months ago) by agomez
File size: 4707 byte(s)
#
# BonFIRE Virtual Clusters on Federated Clouds Demonstration Kit
#
# Copyright (c) Fundacion Centro Tecnologico de Supercomputacion de Galicia 2012
# 
# License Apache Software
#
# The research leading to these results has received funding from 
# the European Community's Seventh Framework Programme (FP7/2007-2013) 
# under agreement number 257386
#
# This software is provided with ABSOLUTELY NO WARRANTY
# 
require 'rubygems'
require 'restfully'
require 'restfully/addons/bonfire'

##Editable parameters
#SIZES = "1,2" #Comma separated list of volume sizes in GB
SIZES = "1" #Comma separated list of volume sizes in GB
#LOCATION = "de-hlrs"
LOCATION = "fr-inria"
#LOCATION = "uk-epcc"
##

EXPERIMENT_NAME = "Build storages"
EXPERIMENT_DESCRIPTION = "Build storages test"
EXPERIMENT_WALLTIME = 2*3600 
MASTER_IMAGE_NAME = "BonFIRE Debian Squeeze 2G v3"
WAN_NAME = "BonFIRE WAN"

session = Restfully::Session.new(
  :configuration_file => "~/.restfully/api.bonfire-project.eu",
  :cache => false,
  :gateway => "ssh.fr-inria.bonfire-project.eu",
  :keys => ["~/.ssh/id_rsa"]
)
session.logger.level = Logger::INFO
#session.logger.level = Logger::DEBUG
experiment = nil
volumes = []
begin
  session.logger.info "Deploying experiment..."
  experiment = session.root.experiments.submit(
    :name => EXPERIMENT_NAME,
    :description => EXPERIMENT_DESCRIPTION + " - " + Time.now.to_s,
#    :status => "waiting",
    :walltime => EXPERIMENT_WALLTIME 
  )

  location = session.root.locations[:"#{LOCATION}"]
  session.logger.info "Chosen location is: #{location['name']}"
  fail "Can't select the machine location" if location.nil?

  def create_volume(session, location, size)
    session.logger.info "Deploying volume on #{location['name']}..."
    return location.storages.find{|storage|
      storage['name'].include?"volume-#{size}GB-empty"
  } || location.storages.submit(
      :name => "volume-#{size}GB-empty",
      :size => size*1024,
      :type => "DATABLOCK",
      :group => "private",
      :fstype => "ext3",
      :public => "NO",
      :persistent => "YES"
    )
  end

  SIZES.split(',').each{|size| volumes.push(create_volume(session,location,size.to_i))}
  volumes.each{|storage| 
	storage.update(:persistent => "YES")
	storage.reload}
  
  session.logger.info "Launching machine..."

  disks = [{:storage => location.storages.find{|s| s['name'] == MASTER_IMAGE_NAME },:type => "OS"}]
  volumes.each{|storage| disks.push({:storage => storage,:type => "DATABLOCK"})}
  #pp disks
  machine = experiment.computes.submit(
    :name => "machine-experiment#{experiment['id']}",
    :instance_type => "small",
    :disk => disks,
    :nic => [
      {:network => location.networks.find{|n| n['name'] == WAN_NAME}}
    ],
    :location => location,
    :context => {}
  )
  hostname = "#{machine['name']}-#{machine['id']}" 
  session.logger.info "Machine: #{hostname}   #{machine['nic'][0]['ip']}"

  session.logger.info "Starting experiment..."
  experiment.update(:status => "running")

  session.logger.info "Checking if machine is ready..."
  until [machine].all?{|vm|
    vm.reload['state'] == 'RUNNING' && vm.ssh.accessible?
  } do
    fail "Machine has failed" if [machine].any?{|vm| vm['state'] == 'FAILED'}
    session.logger.info "Machine is not ready. Waiting..."
    sleep 20
  end

  session.logger.info "Machine is ready"
  #Fill volumes with zeros
  machine.ssh do |ssh|
    ('e'..'i').to_a[0,volumes.size].each do |letter| 
       session.logger.info "Formating device xvd#{letter}"
       output = ssh.exec!("blockdev --getsize64 /dev/xvd#{letter}")
       session.logger.info output unless output.nil?
       output = ssh.exec!("dd if=/dev/zero of=/dev/xvd#{letter} bs=1M")
       session.logger.info output unless output.nil?
    end
  end
  session.logger.info "Devices formatted!"
 
  machine.update(:state => 'SHUTDOWN')
 
  until machine.reload['state'] == 'DONE' do
      session.logger.info "Machine is #{machine['state']}."
      sleep 20
  end
  
  session.logger.info "Disable persistence of disks"
  volumes.each{|storage| storage.update(:persistent => "NO") }

  session.logger.info "Deleting experiment"
  experiment.delete

  session.logger.info "Volumes available:"
  volumes.each{|storage| session.logger.info storage['name']}
  session.logger.warn "Experiment terminated!"

rescue Exception => e
  session.logger.error "#{e.class.name}: #{e.message}"
  session.logger.error e.backtrace.join("\n")
  
  session.logger.warn "Cleaning up in 30 seconds. Hit CTRL-C now to keep your VMs..."
  sleep 30
  experiment.delete unless experiment.nil?
  sleep 30
  volumes.each{|storage| storage.update(:persistent => "NO") }
  volumes.each{|storage| storage.delete}
end

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge