syntax cleanup

This commit is contained in:
dima 2013-09-24 20:32:03 +02:00
parent 14c508c4da
commit 40d862459d
8 changed files with 113 additions and 113 deletions

View File

@ -249,40 +249,38 @@ module VagrantPlugins
end end
end end
action_root = Pathname.new(File.expand_path('../action', __FILE__))
action_root = Pathname.new(File.expand_path("../action", __FILE__)) autoload :ConnectLibvirt, action_root.join('connect_libvirt')
autoload :ConnectLibvirt, action_root.join("connect_libvirt") autoload :IsCreated, action_root.join('is_created')
autoload :IsCreated, action_root.join("is_created") autoload :IsRunning, action_root.join('is_running')
autoload :IsRunning, action_root.join("is_running") autoload :IsSuspended, action_root.join('is_suspended')
autoload :IsSuspended, action_root.join("is_suspended") autoload :MessageAlreadyCreated, action_root.join('message_already_created')
autoload :MessageAlreadyCreated, action_root.join("message_already_created") autoload :MessageNotCreated, action_root.join('message_not_created')
autoload :MessageNotCreated, action_root.join("message_not_created") autoload :MessageNotRunning, action_root.join('message_not_running')
autoload :MessageNotRunning, action_root.join("message_not_running") autoload :MessageNotSuspended, action_root.join('message_not_suspended')
autoload :MessageNotSuspended, action_root.join("message_not_suspended") autoload :HandleStoragePool, action_root.join('handle_storage_pool')
autoload :HandleStoragePool, action_root.join("handle_storage_pool") autoload :HandleBoxUrl, 'vagrant/action/builtin/handle_box_url'
autoload :HandleBoxUrl, "vagrant/action/builtin/handle_box_url" autoload :HandleBoxImage, action_root.join('handle_box_image')
autoload :HandleBoxImage, action_root.join("handle_box_image") autoload :SetNameOfDomain, action_root.join('set_name_of_domain')
autoload :SetNameOfDomain, action_root.join("set_name_of_domain") autoload :CreateDomainVolume, action_root.join('create_domain_volume')
autoload :CreateDomainVolume, action_root.join("create_domain_volume") autoload :CreateDomain, action_root.join('create_domain')
autoload :CreateDomain, action_root.join("create_domain") autoload :CreateNetworks, action_root.join('create_networks')
autoload :CreateNetworks, action_root.join("create_networks") autoload :CreateNetworkInterfaces, action_root.join('create_network_interfaces')
autoload :CreateNetworkInterfaces, action_root.join("create_network_interfaces") autoload :DestroyDomain, action_root.join('destroy_domain')
autoload :DestroyDomain, action_root.join("destroy_domain") autoload :DestroyNetworks, action_root.join('destroy_networks')
autoload :DestroyNetworks, action_root.join("destroy_networks") autoload :StartDomain, action_root.join('start_domain')
autoload :StartDomain, action_root.join("start_domain") autoload :HaltDomain, action_root.join('halt_domain')
autoload :HaltDomain, action_root.join("halt_domain") autoload :SuspendDomain, action_root.join('suspend_domain')
autoload :SuspendDomain, action_root.join("suspend_domain") autoload :ResumeDomain, action_root.join('resume_domain')
autoload :ResumeDomain, action_root.join("resume_domain") autoload :ReadState, action_root.join('read_state')
autoload :ReadState, action_root.join("read_state") autoload :ReadSSHInfo, action_root.join('read_ssh_info')
autoload :ReadSSHInfo, action_root.join("read_ssh_info") autoload :TimedProvision, action_root.join('timed_provision')
autoload :TimedProvision, action_root.join("timed_provision") autoload :WaitTillUp, action_root.join('wait_till_up')
autoload :WaitTillUp, action_root.join("wait_till_up") autoload :SyncFolders, action_root.join('sync_folders')
autoload :SyncFolders, action_root.join("sync_folders") autoload :SSHRun, 'vagrant/action/builtin/ssh_run'
autoload :SSHRun, "vagrant/action/builtin/ssh_run" autoload :PrepareNFSSettings, action_root.join('prepare_nfs_settings')
autoload :PrepareNFSSettings, action_root.join("prepare_nfs_settings") autoload :PruneNFSExports, action_root.join('prune_nfs_exports')
autoload :PruneNFSExports, action_root.join("prune_nfs_exports") autoload :ShareFolders, action_root.join('share_folders')
autoload :ShareFolders, action_root.join("share_folders")
end end
end end
end end

View File

@ -15,7 +15,7 @@ module VagrantPlugins
include Vagrant::Util::ScopedHashOverride include Vagrant::Util::ScopedHashOverride
def initialize(app, env) def initialize(app, env)
@logger = Log4r::Logger.new("vagrant_libvirt::action::create_network_interfaces") @logger = Log4r::Logger.new('vagrant_libvirt::action::create_network_interfaces')
@app = app @app = app
end end
@ -91,7 +91,7 @@ module VagrantPlugins
end end
end end
# Continue the middleware chain. # Continue the middleware chain.
@app.call(env) @app.call(env)
# Configure interfaces that user requested. Machine should be up and # Configure interfaces that user requested. Machine should be up and
@ -122,15 +122,15 @@ module VagrantPlugins
networks_to_configure << network networks_to_configure << network
end end
env[:ui].info I18n.t("vagrant.actions.vm.network.configuring") env[:ui].info I18n.t('vagrant.actions.vm.network.configuring')
env[:machine].guest.capability( env[:machine].guest.capability(
:configure_networks, networks_to_configure) :configure_networks, networks_to_configure)
end end
private private
def find_empty(array, start=0, stop=8) def find_empty(array, start=0, stop=8)
for i in start..stop (start..stop).each do |i|
return i if !array[i] return i if !array[i]
end end
return nil return nil

View File

@ -14,7 +14,8 @@ module VagrantPlugins
include VagrantPlugins::ProviderLibvirt::Util::LibvirtUtil include VagrantPlugins::ProviderLibvirt::Util::LibvirtUtil
def initialize(app, env) def initialize(app, env)
@logger = Log4r::Logger.new("vagrant_libvirt::action::create_networks") mess = 'vagrant_libvirt::action::create_networks'
@logger = Log4r::Logger.new(mess)
@app = app @app = app
@available_networks = [] @available_networks = []
@ -44,22 +45,22 @@ module VagrantPlugins
# (:libvirt__network_name => ...). # (:libvirt__network_name => ...).
@options = scoped_hash_override(options, :libvirt) @options = scoped_hash_override(options, :libvirt)
@options = { @options = {
:netmask => '255.255.255.0', netmask: '255.255.255.0',
:dhcp_enabled => true, dhcp_enabled: true,
:forward_mode => 'nat', forward_mode: 'nat',
}.merge(@options) }.merge(@options)
# Prepare a hash describing network for this specific interface. # Prepare a hash describing network for this specific interface.
@interface_network = { @interface_network = {
:name => nil, name: nil,
:ip_address => nil, ip_address: nil,
:netmask => @options[:netmask], netmask: @options[:netmask],
:network_address => nil, network_address: nil,
:bridge_name => nil, bridge_name: nil,
:created => false, created: false,
:active => false, active: false,
:autostart => false, autostart: false,
:libvirt_network => nil, libvirt_network: nil,
} }
if @options[:ip] if @options[:ip]
@ -67,16 +68,16 @@ module VagrantPlugins
elsif @options[:network_name] elsif @options[:network_name]
handle_network_name_option handle_network_name_option
else else
# TODO Should be smarter than just using fixed 'default' string. # TODO: Should be smarter than just using fixed 'default' string.
@interface_network = lookup_network_by_name('default') @interface_network = lookup_network_by_name('default')
if not @interface_network if !@interface_network
raise Errors::NetworkNotAvailableError, raise Errors::NetworkNotAvailableError,
:network_name => 'default' network_name: 'default'
end end
end end
autostart_network if not @interface_network[:autostart] autostart_network if @interface_network[:autostart].nil?
activate_network if not @interface_network[:active] activate_network if @interface_network[:active].nil?
end end
@app.call(env) @app.call(env)
@ -103,7 +104,7 @@ module VagrantPlugins
# Handle only situations, when ip is specified. Variables @options and # Handle only situations, when ip is specified. Variables @options and
# @available_networks should be filled before calling this function. # @available_networks should be filled before calling this function.
def handle_ip_option(env) def handle_ip_option(env)
return if not @options[:ip] return if !@options[:ip]
net_address = network_address(@options[:ip], @options[:netmask]) net_address = network_address(@options[:ip], @options[:netmask])
@interface_network[:network_address] = net_address @interface_network[:network_address] = net_address
@ -129,32 +130,32 @@ module VagrantPlugins
# config match together. # config match together.
if @options[:network_name] != @interface_network[:name] if @options[:network_name] != @interface_network[:name]
raise Errors::NetworkNameAndAddressMismatch, raise Errors::NetworkNameAndAddressMismatch,
:ip_address => @options[:ip], ip_address: @options[:ip],
:network_name => @options[:network_name] network_name: @options[:network_name]
end end
else else
# Network is not created, but name is set. We need to check, # Network is not created, but name is set. We need to check,
# whether network name from config doesn't already exist. # whether network name from config doesn't already exist.
if lookup_network_by_name @options[:network_name] if lookup_network_by_name @options[:network_name]
raise Errors::NetworkNameAndAddressMismatch, raise Errors::NetworkNameAndAddressMismatch,
:ip_address => @options[:ip], ip_address: @options[:ip],
:network_name => @options[:network_name] network_name: @options[:network_name]
end end
# Network with 'name' doesn't exist. Set it as name for new # Network with 'name' doesn't exist. Set it as name for new
# network. # network.
@interface_network[:name] = @options[:network_name] @interface_network[:name] = @options[:network_name]
end end
end end
# Do we need to create new network? # Do we need to create new network?
if not @interface_network[:created] if !@interface_network[:created]
# TODO stop after some loops. Don't create infinite loops. # TODO: stop after some loops. Don't create infinite loops.
# Is name for new network set? If not, generate a unique one. # Is name for new network set? If not, generate a unique one.
count = 0 count = 0
while @interface_network[:name] == nil do while @interface_network[:name].nil?
# Generate a network name. # Generate a network name.
network_name = env[:root_path].basename.to_s.dup network_name = env[:root_path].basename.to_s.dup
@ -169,7 +170,7 @@ module VagrantPlugins
# Generate a unique name for network bridge. # Generate a unique name for network bridge.
count = 0 count = 0
while @interface_network[:bridge_name] == nil do while @interface_network[:bridge_name].nil?
bridge_name = 'virbr' bridge_name = 'virbr'
bridge_name << count.to_s bridge_name << count.to_s
count += 1 count += 1
@ -188,12 +189,12 @@ module VagrantPlugins
# @options and @available_networks should be filled before calling this # @options and @available_networks should be filled before calling this
# function. # function.
def handle_network_name_option def handle_network_name_option
return if @options[:ip] or not @options[:network_name] return if @options[:ip] || !@options[:network_name]
@interface_network = lookup_network_by_name(@options[:network_name]) @interface_network = lookup_network_by_name(@options[:network_name])
if not @interface_network if !@interface_network
raise Errors::NetworkNotAvailableError, raise Errors::NetworkNotAvailableError,
:network_name => @options[:network_name] network_name: @options[:network_name]
end end
end end
@ -214,9 +215,10 @@ module VagrantPlugins
network_address << "#{@interface_network[:netmask]}" network_address << "#{@interface_network[:netmask]}"
net = IPAddr.new(network_address) net = IPAddr.new(network_address)
# First is address of network, second is gateway. Start the range two # First is address of network, second is gateway.
# Start the range two
# addresses after network address. # addresses after network address.
# TODO Detect if this IP is not set on the interface. # TODO: Detect if this IP is not set on the interface.
start_address = net.to_range.begin.succ.succ start_address = net.to_range.begin.succ.succ
# Stop address must not be broadcast. # Stop address must not be broadcast.
@ -233,13 +235,12 @@ module VagrantPlugins
@interface_network[:libvirt_network] = \ @interface_network[:libvirt_network] = \
@libvirt_client.define_network_xml(to_xml('private_network')) @libvirt_client.define_network_xml(to_xml('private_network'))
rescue => e rescue => e
raise Errors::CreateNetworkError, raise Errors::CreateNetworkError, error_message: e.message
:error_message => e.message
end end
created_networks_file = env[:machine].data_dir + 'created_networks' created_networks_file = env[:machine].data_dir + 'created_networks'
message = "Saving information about created network " message = 'Saving information about created network '
message << "#{@interface_network[:name]}, " message << "#{@interface_network[:name]}, "
message << "UUID=#{@interface_network[:libvirt_network].uuid} " message << "UUID=#{@interface_network[:libvirt_network].uuid} "
message << "to file #{created_networks_file}." message << "to file #{created_networks_file}."
@ -254,8 +255,7 @@ module VagrantPlugins
begin begin
@interface_network[:libvirt_network].autostart = true @interface_network[:libvirt_network].autostart = true
rescue => e rescue => e
raise Errors::AutostartNetworkError, raise Errors::AutostartNetworkError, error_message: e.message
:error_message => e.message
end end
end end
@ -263,8 +263,7 @@ module VagrantPlugins
begin begin
@interface_network[:libvirt_network].create @interface_network[:libvirt_network].create
rescue => e rescue => e
raise Errors::ActivateNetworkError, raise Errors::ActivateNetworkError, error_message: e.message
:error_message => e.message
end end
end end

View File

@ -5,7 +5,7 @@ module VagrantPlugins
module Action module Action
class HandleBoxImage class HandleBoxImage
def initialize(app, env) def initialize(app, env)
@logger = Log4r::Logger.new("vagrant_libvirt::action::handle_box_image") @logger = Log4r::Logger.new('vagrant_libvirt::action::handle_box_image')
@app = app @app = app
end end
@ -30,7 +30,7 @@ module VagrantPlugins
# Get config options # Get config options
config = env[:machine].provider_config config = env[:machine].provider_config
box_image_file = env[:machine].box.directory.join("box.img").to_s box_image_file = env[:machine].box.directory.join('box.img').to_s
env[:box_volume_name] = env[:machine].box.name.to_s.dup env[:box_volume_name] = env[:machine].box.name.to_s.dup
env[:box_volume_name] << '_vagrant_box_image.img' env[:box_volume_name] << '_vagrant_box_image.img'
@ -40,7 +40,7 @@ module VagrantPlugins
# Box is not available as a storage pool volume. Create and upload # Box is not available as a storage pool volume. Create and upload
# it as a copy of local box image. # it as a copy of local box image.
env[:ui].info(I18n.t("vagrant_libvirt.uploading_volume")) env[:ui].info(I18n.t('vagrant_libvirt.uploading_volume'))
# Create new volume in storage pool # Create new volume in storage pool
box_image_size = File.size(box_image_file) # B box_image_size = File.size(box_image_file) # B
@ -49,11 +49,11 @@ module VagrantPlugins
@logger.info(message) @logger.info(message)
begin begin
fog_volume = env[:libvirt_compute].volumes.create( fog_volume = env[:libvirt_compute].volumes.create(
:name => env[:box_volume_name], name: env[:box_volume_name],
:allocation => "#{box_image_size/1024/1024}M", allocation: "#{box_image_size/1024/1024}M",
:capacity => "#{box_virtual_size}G", capacity: "#{box_virtual_size}G",
:format_type => box_format, format_type: box_format,
:pool_name => config.storage_pool_name) pool_name: config.storage_pool_name)
rescue Fog::Errors::Error => e rescue Fog::Errors::Error => e
raise Errors::FogCreateVolumeError, raise Errors::FogCreateVolumeError,
:error_message => e.message :error_message => e.message
@ -72,7 +72,7 @@ module VagrantPlugins
# If upload failed or was interrupted, remove created volume from # If upload failed or was interrupted, remove created volume from
# storage pool. # storage pool.
if env[:interrupted] or !ret if env[:interrupted] || !ret
begin begin
fog_volume.destroy fog_volume.destroy
rescue rescue
@ -99,7 +99,7 @@ module VagrantPlugins
# Exception ProviderLibvirt::RetrieveError can be raised if buffer is # Exception ProviderLibvirt::RetrieveError can be raised if buffer is
# longer than length accepted by API send function. # longer than length accepted by API send function.
# #
# TODO: How to find out if buffer is too large and what is the # TODO: How to find out if buffer is too large and what is the
# length that send function will accept? # length that send function will accept?
@ -117,8 +117,11 @@ module VagrantPlugins
:error_message => e.message :error_message => e.message
end end
return true if progress == image_size if progress == image_size
false return true
else
return false
end
end end
end end

View File

@ -4,7 +4,7 @@ module VagrantPlugins
module ProviderLibvirt module ProviderLibvirt
module Errors module Errors
class VagrantLibvirtError < Vagrant::Errors::VagrantError class VagrantLibvirtError < Vagrant::Errors::VagrantError
error_namespace("vagrant_libvirt.errors") error_namespace('vagrant_libvirt.errors')
end end
# Storage pools and volumes exceptions # Storage pools and volumes exceptions

View File

@ -1,14 +1,14 @@
begin begin
require 'vagrant' require 'vagrant'
rescue LoadError rescue LoadError
raise "The Vagrant Libvirt plugin must be run within Vagrant." raise 'The Vagrant Libvirt plugin must be run within Vagrant.'
end end
# This is a sanity check to make sure no one is attempting to install # This is a sanity check to make sure no one is attempting to install
# this into an early Vagrant version. # this into an early Vagrant version.
if Vagrant::VERSION < '1.1.0' if Vagrant::VERSION < '1.1.0'
raise "The Vagrant Libvirt plugin is only compatible with Vagrant 1.1+" raise 'The Vagrant Libvirt plugin is only compatible with Vagrant 1.1+'
end end
module VagrantPlugins module VagrantPlugins
@ -25,30 +25,30 @@ module VagrantPlugins
Config Config
end end
provider "libvirt" do provider 'libvirt' do
# Setup logging and i18n # Setup logging and i18n
setup_logging setup_logging
setup_i18n setup_i18n
require_relative "provider" require_relative 'provider'
Provider Provider
end end
# This initializes the internationalization strings. # This initializes the internationalization strings.
def self.setup_i18n def self.setup_i18n
I18n.load_path << File.expand_path("locales/en.yml", ProviderLibvirt.source_root) I18n.load_path << File.expand_path('locales/en.yml', ProviderLibvirt.source_root)
I18n.reload! I18n.reload!
end end
# This sets up our log level to be whatever VAGRANT_LOG is. # This sets up our log level to be whatever VAGRANT_LOG is.
def self.setup_logging def self.setup_logging
require "log4r" require 'log4r'
level = nil level = nil
begin begin
level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase) level = Log4r.const_get(ENV['VAGRANT_LOG'].upcase)
rescue NameError rescue NameError
# This means that the logging constant wasn't found, # This means that the logging constant wasn't found,
# which is fine. We just keep `level` as `nil`. But # which is fine. We just keep `level` as `nil`. But
@ -64,7 +64,7 @@ module VagrantPlugins
# Set the logging level on all "vagrant" namespaced # Set the logging level on all "vagrant" namespaced
# logs as long as we have a valid level. # logs as long as we have a valid level.
if level if level
logger = Log4r::Logger.new("vagrant_libvirt") logger = Log4r::Logger.new('vagrant_libvirt')
logger.outputters = Log4r::Outputter.stderr logger.outputters = Log4r::Outputter.stderr
logger.level = level logger.level = level
logger = nil logger = nil

View File

@ -8,7 +8,7 @@ module VagrantPlugins
include Vagrant::Util::NetworkIP include Vagrant::Util::NetworkIP
# Return a list of all (active and inactive) libvirt networks as a list # Return a list of all (active and inactive) libvirt networks as a list
# of hashes with their name, network address and status (active or not). # of hashes with their name, network address and status (active or not)
def libvirt_networks(libvirt_client) def libvirt_networks(libvirt_client)
libvirt_networks = [] libvirt_networks = []
@ -29,22 +29,22 @@ module VagrantPlugins
# Calculate network address of network from ip address and # Calculate network address of network from ip address and
# netmask. # netmask.
if ip and netmask if ip && netmask
network_address = network_address(ip, netmask) network_address = network_address(ip, netmask)
else else
network_address = nil network_address = nil
end end
libvirt_networks << { libvirt_networks << {
:name => network_name, name: network_name,
:ip_address => ip, ip_address: ip,
:netmask => netmask, netmask: netmask,
:network_address => network_address, network_address: network_address,
:bridge_name => libvirt_network.bridge_name, bridge_name: libvirt_network.bridge_name,
:created => true, created: true,
:active => libvirt_network.active?, active: libvirt_network.active?,
:autostart => libvirt_network.autostart?, autostart: libvirt_network.autostart?,
:libvirt_network => libvirt_network, libvirt_network: libvirt_network
} }
end end

View File

@ -1,5 +1,5 @@
module VagrantPlugins module VagrantPlugins
module ProviderLibvirt module ProviderLibvirt
VERSION = "0.0.7" VERSION = '0.0.7'
end end
end end