Class: Tupperware::VagrantVirtualBox

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, kitchen_config = Kitchen::Config.new) ⇒ VagrantVirtualBox

Returns a new instance of VagrantVirtualBox.



122
123
124
125
126
# File 'lib/tupperware.rb', line 122

def initialize(options={}, kitchen_config=Kitchen::Config.new)
  @options = options
  @kitchen_config = kitchen_config
  @kitchen_root = @kitchen_config.kitchen_root
end

Instance Attribute Details

#kitchen_configObject (readonly)

Returns the value of attribute kitchen_config.



120
121
122
# File 'lib/tupperware.rb', line 120

def kitchen_config
  @kitchen_config
end

#kitchen_rootObject (readonly)

Returns the value of attribute kitchen_root.



120
121
122
# File 'lib/tupperware.rb', line 120

def kitchen_root
  @kitchen_root
end

#optionsObject (readonly)

Returns the value of attribute options.



120
121
122
# File 'lib/tupperware.rb', line 120

def options
  @options
end

Instance Method Details

#get_id(instance_name) ⇒ Object



135
136
137
138
139
140
# File 'lib/tupperware.rb', line 135

def get_id(instance_name)
  v_root = vagrant_root(instance_name)
  id_file = "#{v_root}/.vagrant/machines/default/virtualbox/id"
  Utils.check_path(id_file)
  IO.read(id_file)
end

#halt(instance_name) ⇒ Object



188
189
190
191
192
# File 'lib/tupperware.rb', line 188

def halt(instance_name)
  setup_vagrant_env(instance_name) do 
    Utils.run_cmd('vagrant halt')
  end
end

#package(instance_name) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/tupperware.rb', line 194

def package(instance_name)
  setup_vagrant_env(instance_name) do
    if @options[:pre_package]
      Utils.check_path()
      load(@options[:pre_package])
    end
    id = get_id(instance_name)
    puts "\n*** Packaging #{instance_name} ***\n"
    Utils.run_cmd("vagrant package --base #{id} --output #{instance_name}.box")
    current_path = File.absolute_path("#{instance_name}.box")
    desired_path = File.join(@kitchen_root, "#{instance_name}.box")
    FileUtils.mv(current_path, desired_path)
  end
end

#provision_local_box(instance) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/tupperware.rb', line 142

def provision_local_box(instance)
  if @options[:provision]
    Utils.check_path(@options[:provision])
    script = IO.read(@options[:provision])
  else
    script = "    #!/bin/bash\n    echo -n \"\\n*** Deploying the pulibic key ***\\n\"\n    echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key' | sudo tee -a /home/vagrant/.ssh/authorized_keys\n    echo \"\\n*** Setting up passwordless SSH for the vagrant user ***\\n\"\n    echo 'vagrant ALL=(ALL) NOPASSWD: ALL' | sudo tee -a /etc/sudoers.conf\n    echo \"\\n*** Removing locale files ***\\n\"\n    sudo find /usr/share/locale/ -mindepth 1 ! -iregex \"/.*en/.*\" -type d -exec rm -rf {} +\n    echo \"\\n*** Cleaning logs ***\\n\"\n    sudo find /var/log -type f | while read f; do echo -n '' > $f; done;\n    echo \"\\n*** Cleaning history ***\\n\"\n    sudo rm -f /root/.bash_history\n    sudo rm -f /home/vagrant/.bash_history\n    SCRIPT\n  end\n  bin = File.join(vagrant_root(instance.name.to_s), 'kitchen-provision')\n  File.open(bin, 'w') {|f| f.write(script)}\n  state = Kitchen::StateFile.new(@kitchen_root, instance.name.to_s)\n  state_hash = state.read\n  options = {\n    keys: [state_hash[:ssh_key]], \n    port: state_hash[:port], \n    username: state_hash[:username], \n    hostname: state_hash[:hostname]\n  }\n  Kitchen::Transport::Ssh::Connection.new(options) do |c|\n    c.upload(bin, '/tmp/kitchen-provision')\n  end\n  instance.remote_exec('sudo chmod +x /tmp/kitchen-provision')\n  instance.remote_exec('sudo /tmp/kitchen-provision')\n  instance.remote_exec('sudo rm -f /tmp/kitchen-provision')\n  File.delete(bin)\nend\n"

#setup_vagrant_env(instance_name) ⇒ Object



181
182
183
184
185
186
# File 'lib/tupperware.rb', line 181

def setup_vagrant_env(instance_name)
  current_dir = ENV['PWD']
  Dir.chdir(vagrant_root(instance_name))
  yield
  Dir.chdir(current_dir)
end

#vagrant_root(instance_name) ⇒ Object



128
129
130
131
132
133
# File 'lib/tupperware.rb', line 128

def vagrant_root(instance_name)
  File.join(
    @kitchen_root, %w[.kitchen kitchen-vagrant],
    "kitchen-#{File.basename(@kitchen_root)}-#{instance_name}"
  )
end