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.



98
99
100
101
102
# File 'lib/tupperware.rb', line 98

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.



96
97
98
# File 'lib/tupperware.rb', line 96

def kitchen_config
  @kitchen_config
end

#kitchen_rootObject (readonly)

Returns the value of attribute kitchen_root.



96
97
98
# File 'lib/tupperware.rb', line 96

def kitchen_root
  @kitchen_root
end

#optionsObject (readonly)

Returns the value of attribute options.



96
97
98
# File 'lib/tupperware.rb', line 96

def options
  @options
end

Instance Method Details

#get_id(instance_name) ⇒ Object



111
112
113
114
115
116
# File 'lib/tupperware.rb', line 111

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("#{v_root}/.vagrant/machines/default/virtualbox/id")
end

#halt(instance_name) ⇒ Object



164
165
166
167
168
# File 'lib/tupperware.rb', line 164

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

#package(instance_name) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tupperware.rb', line 170

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



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
# File 'lib/tupperware.rb', line 118

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' >> /home/vagrant/.ssh/authorized_keys\n    echo \"\\n*** Setting up passwordless SSH for the vagrant user ***\\n\"\n    echo 'vagrant ALL=(ALL) NOPASSWD: ALL' >> /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



157
158
159
160
161
162
# File 'lib/tupperware.rb', line 157

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



104
105
106
107
108
109
# File 'lib/tupperware.rb', line 104

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