Class: VMTools::VM

Inherits:
Object
  • Object
show all
Defined in:
lib/vmtools.rb

Instance Method Summary collapse

Constructor Details

#initializeVM

Returns a new instance of VM.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vmtools.rb', line 81

def initialize
  # Get vmtools gem path
  spec = Gem::Specification.find_by_name("vmtools")
  @gem_root = spec.gem_dir
  # Read config.
  # First searching for config in current dir and then in ruby gems dir
  begin
    VMConfig.from_file('vmtconfig.rb')
  rescue Errno::ENOENT
    puts "Warring: There's no vmtconfig.rb in current dir. Using default vmtconfig.rb"
    VMConfig.from_file("#{@gem_root}/config/vmtconfig.rb")
  end

  # Now only vagrant type of VMs is supported.
  if VMConfig.vm_type != 'vagrant'
    puts "Well, this's embarassing, but only vagrant VMs are supported now"
    puts "Your specified #{VMConfig.vm_type} VM in config."
    exit(0)
  end
  # Assign class instance variables
  ## Chef stuff
  @orgname = VMConfig.orgname
  @knife_config = VMConfig.knife_config

  ## Common stuff
  @testbox_name = VMConfig.testbox_name

  ## Vagrant stuff
  @vtemplate = VMConfig.vtemplate
  @vconfig = VMConfig.vconfig
  @homedir = VMConfig.homedir
  @vmemory = VMConfig.vmemory
  @vforward_ports = VMConfig.vforward_ports
  @vbox_name = VMConfig.vbox_name
  @vbox_url = VMConfig.vbox_url
end

Instance Method Details

#createObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/vmtools.rb', line 163

def create
  # Read template content
  # First searching for config in current dir and then in ruby gems dir
  begin
    template_content = IO.read(@vtemplate)
  rescue Errno::ENOENT
    puts "Warring: There's no Vagrantfile.erb in current dir. Using default Vagrantfile.erb"
    template_content = IO.read("#{@gem_root}/templates/Vagrantfile.erb")
  end

  erb = ERB.new(template_content)

  env = Vagrant::Environment.new
  # Clean out old vagrant config before writing a new one
  begin
    File.delete(@vconfig)
  rescue Errno::ENOENT
    puts "Warring: There's no #{@vconfig} in this dir. Nothing to delete"
  end
  # Generate new config from erb template
  IO.write(@vconfig,erb.result(self.get_binding))
  env.cli("up")
end

#deleteObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/vmtools.rb', line 187

def delete
  # Delete chef client/node
  ENV['ORGNAME'] = "#{@orgname}"
  system("knife node delete #{@testbox} -c #{@knife_config} -y")
  system("knife client delete #{@testbox} -c #{@knife_config} -y")
  # Delete VM  
  env = Vagrant::Environment.new
  begin
    File.delete(@vconfig)
  rescue Errno::ENOENT
    puts "Warring: There's no #{@vconfig} in this dir. Nothing to delete"
  end
  env.cli("destroy","--force")
end

#get_bindingObject

this is only a helper method to access the objects



77
78
79
80
# File 'lib/vmtools.rb', line 77

def get_binding # this is only a helper method to access the objects
# binding method
  binding
end

#runObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vmtools.rb', line 118

def run
  # Proccess command line parameters
  ## If no arguments - then let's print help
  if ARGV.empty?
    puts "Well, this's embarassing, but you need to specify an option! Please read help:"
    ARGV << "-h"
  end

  cli = VMCLI.new
  banner = <<-eos
Vmtools is util for create/delete/recreate test VM
VM configuration stored in vmtconfig.rb file

Usage:
     vmtools --create|--delete|--recreate

Options:
eos
  cli.banner=banner
  ## Parse options
  begin
    cli.parse_options
  rescue OptionParser::InvalidOption
    # if we have wrong option, print help.
    puts "Well, this's embarassing, but wrong option! Please read help:"
    ARGV.clear << "-h"
    retry
  end
  cli.config.each do |opt,value|
    if value
      case opt
        when :create_vm
          self.create
        when :delete_vm
          self.delete
        when :recreate_vm
          self.delete
          self.create
        when :version
        puts "vmtools #{VMTools::VERSION} (c) 2012 Atalanta Systems"
      end
    end
  end
end