Class: Xanthus::VirtualMachine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVirtualMachine

Returns a new instance of VirtualMachine.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/xanthus/virtual_machine.rb', line 23

def initialize
  @name = :default
  @box = 'jhcook/fedora27'
  @version = '4.13.12.300'
  @ip = '192.168.33.8'
  @memory = 4096
  @cpus = 2
  @cpu_cap = 70
  @gui = false
  @boxing = nil
  @ssh_username = nil
  @ssh_key_path = nil
end

Instance Attribute Details

#aws_amiObject

Returns the value of attribute aws_ami.



19
20
21
# File 'lib/xanthus/virtual_machine.rb', line 19

def aws_ami
  @aws_ami
end

#aws_env_key_idObject

Returns the value of attribute aws_env_key_id.



15
16
17
# File 'lib/xanthus/virtual_machine.rb', line 15

def aws_env_key_id
  @aws_env_key_id
end

#aws_env_key_secretObject

Returns the value of attribute aws_env_key_secret.



16
17
18
# File 'lib/xanthus/virtual_machine.rb', line 16

def aws_env_key_secret
  @aws_env_key_secret
end

#aws_instance_typeObject

Returns the value of attribute aws_instance_type.



20
21
22
# File 'lib/xanthus/virtual_machine.rb', line 20

def aws_instance_type
  @aws_instance_type
end

#aws_key_pair_nameObject

Returns the value of attribute aws_key_pair_name.



17
18
19
# File 'lib/xanthus/virtual_machine.rb', line 17

def aws_key_pair_name
  @aws_key_pair_name
end

#aws_regionObject

Returns the value of attribute aws_region.



18
19
20
# File 'lib/xanthus/virtual_machine.rb', line 18

def aws_region
  @aws_region
end

#aws_security_groupObject

Returns the value of attribute aws_security_group.



21
22
23
# File 'lib/xanthus/virtual_machine.rb', line 21

def aws_security_group
  @aws_security_group
end

#boxObject

Returns the value of attribute box.



4
5
6
# File 'lib/xanthus/virtual_machine.rb', line 4

def box
  @box
end

#boxingObject

Returns the value of attribute boxing.



11
12
13
# File 'lib/xanthus/virtual_machine.rb', line 11

def boxing
  @boxing
end

#cpu_capObject

Returns the value of attribute cpu_cap.



7
8
9
# File 'lib/xanthus/virtual_machine.rb', line 7

def cpu_cap
  @cpu_cap
end

#cpusObject

Returns the value of attribute cpus.



6
7
8
# File 'lib/xanthus/virtual_machine.rb', line 6

def cpus
  @cpus
end

#guiObject

Returns the value of attribute gui.



10
11
12
# File 'lib/xanthus/virtual_machine.rb', line 10

def gui
  @gui
end

#ipObject

Returns the value of attribute ip.



9
10
11
# File 'lib/xanthus/virtual_machine.rb', line 9

def ip
  @ip
end

#memoryObject

Returns the value of attribute memory.



8
9
10
# File 'lib/xanthus/virtual_machine.rb', line 8

def memory
  @memory
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/xanthus/virtual_machine.rb', line 3

def name
  @name
end

#on_awsObject

Returns the value of attribute on_aws.



14
15
16
# File 'lib/xanthus/virtual_machine.rb', line 14

def on_aws
  @on_aws
end

#ssh_key_pathObject

Returns the value of attribute ssh_key_path.



13
14
15
# File 'lib/xanthus/virtual_machine.rb', line 13

def ssh_key_path
  @ssh_key_path
end

#ssh_usernameObject

Returns the value of attribute ssh_username.



12
13
14
# File 'lib/xanthus/virtual_machine.rb', line 12

def ssh_username
  @ssh_username
end

#versionObject

Returns the value of attribute version.



5
6
7
# File 'lib/xanthus/virtual_machine.rb', line 5

def version
  @version
end

Instance Method Details

#generate_box(config) ⇒ Object



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
# File 'lib/xanthus/virtual_machine.rb', line 90

def generate_box config
  return unless !boxing.nil?
  puts 'Generating box...'

  FileUtils.mkdir_p 'boxing'
  Dir.chdir 'boxing' do
    File.open('Vagrantfile', 'w+') do |f|
      f.write(self.to_vagrant)
    end

    script =  Script.new(boxing, config).to_s
    File.open('provision.sh', 'w+') do |f|
      f.write(script)
    end

    system('vagrant', 'up')
    system('vagrant', 'halt')
    system('vagrant', 'package', '--output', "#{name}.box")
    puts "#{name}.box created."
    system('vagrant', 'box', 'add', '--force', "local/#{name}", "#{name}.box")
    system('vagrant', 'destroy', '-f')
    @box = "local/#{name}"
    @version = '0'
  end
end

#to_vagrantObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/xanthus/virtual_machine.rb', line 37

def to_vagrant
script = %Q{
Vagrant.configure(2) do |config|
  config.vm.box = "#{@box}"
  config.vm.box_version = "#{@version}"
  config.vm.network "private_network", ip: "#{@ip}"
  config.vm.synced_folder ".", "/vagrant", create: true, owner: 'vagrant', disabled: false, type: 'virtualbox'
}
script += %Q{
if Vagrant.has_plugin?("vagrant-vbguest")
  config.vbguest.auto_update = false
end
}
script += %Q{
  config.vm.synced_folder ".", "/vagrant", disabled: false, type: 'rsync'
} unless !@on_aws
script += %Q{
  config.ssh.username = "#{@ssh_username}"
} unless ssh_username.nil?
script += %Q{
  config.ssh.private_key_path = "#{@ssh_key_path}"
} unless ssh_key_path.nil?
script += %Q{
  config.vm.provider "virtualbox" do |vb, override|
   vb.gui = #{@gui}
   vb.memory = #{@memory}
   vb.customize ["modifyvm", :id, "--cpuexecutioncap", "#{@cpu_cap}"]
   vb.cpus = #{@cpus}
   vb.name = "#{@name}"
  end
} unless @on_aws
script += %Q{
  config.vm.provider "aws" do |aws, override|
   aws.access_key_id = ENV['#{@aws_env_key_id}']
   aws.secret_access_key = ENV['#{@aws_env_key_secret}']
   aws.keypair_name = '#{@aws_key_pair_name}'
   aws.region = '#{@aws_region}'
   aws.ami =  '#{@aws_ami}'
   aws.instance_type = '#{@aws_instance_type}'
   aws.security_groups = ['#{@aws_security_group}']
  end
} unless !@on_aws
script += %Q{  config.vm.provision "shell", path: "provision.sh"

  config.trigger.before :halt do |trigger|
trigger.info = "Retrieving data before halt..."
trigger.run = {path: "before_halt.sh"}
  end
end
}
  return script
end