Class: Jupiter::VirtualMachine
- Inherits:
-
Object
- Object
- Jupiter::VirtualMachine
show all
- Defined in:
- lib/jupiter/virtualmachine.rb
Defined Under Namespace
Classes: CantFindLogicalVolPathError, InvalidCylindersError, InvalidLVMError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of VirtualMachine.
7
8
9
10
11
12
13
14
|
# File 'lib/jupiter/virtualmachine.rb', line 7
def initialize(ip, port)
@deploy_user = Jupiter.vmusers.fetch(:deploy_user)
@deploy_pass = Jupiter.vmusers.fetch(:deploy_pass)
@sudo_user = Jupiter.vmusers.fetch(:sudo_user)
@sudo_pass = Jupiter.vmusers.fetch(:sudo_pass)
self.ip = ip
self.port = port
end
|
Instance Attribute Details
#deploy_pass ⇒ Object
Returns the value of attribute deploy_pass.
4
5
6
|
# File 'lib/jupiter/virtualmachine.rb', line 4
def deploy_pass
@deploy_pass
end
|
#deploy_user ⇒ Object
Returns the value of attribute deploy_user.
4
5
6
|
# File 'lib/jupiter/virtualmachine.rb', line 4
def deploy_user
@deploy_user
end
|
#ip ⇒ Object
Returns the value of attribute ip.
5
6
7
|
# File 'lib/jupiter/virtualmachine.rb', line 5
def ip
@ip
end
|
#port ⇒ Object
Returns the value of attribute port.
5
6
7
|
# File 'lib/jupiter/virtualmachine.rb', line 5
def port
@port
end
|
#sudo_pass ⇒ Object
Returns the value of attribute sudo_pass.
4
5
6
|
# File 'lib/jupiter/virtualmachine.rb', line 4
def sudo_pass
@sudo_pass
end
|
#sudo_user ⇒ Object
Returns the value of attribute sudo_user.
4
5
6
|
# File 'lib/jupiter/virtualmachine.rb', line 4
def sudo_user
@sudo_user
end
|
Instance Method Details
#array_element(array, some_string) ⇒ Object
115
116
117
|
# File 'lib/jupiter/virtualmachine.rb', line 115
def array_element(array, some_string)
array.each { |x| return x.to_s if x.include? some_string }
end
|
#close_ssh_deploy ⇒ Object
20
21
22
23
24
|
# File 'lib/jupiter/virtualmachine.rb', line 20
def close_ssh_deploy
ssh_deploy.close
@ssh_deploy = nil
true
end
|
#detect_cylinders(fdisk_output) ⇒ Object
102
103
104
105
106
107
108
|
# File 'lib/jupiter/virtualmachine.rb', line 102
def detect_cylinders(fdisk_output)
cylinder_entry = array_element(fdisk_output, '255 heads, 63 sectors')
raise InvalidCylindersError.new("There was an error retrieving the cylinders information.") if cylinder_entry.kind_of?(Array)
seperated_entries = cylinder_entry.split(',')
cylinder_count = array_element(seperated_entries, 'cylinders')
return cylinder_count.tr!('^.0-9', '')
end
|
#detect_lvm_group_path ⇒ Object
89
90
91
92
93
94
|
# File 'lib/jupiter/virtualmachine.rb', line 89
def detect_lvm_group_path
df_output = read_output_lines('df -h')
lvm_path = array_element(df_output, '/dev/mapper/')
raise CantFindLogicalVolPathError.new("Can't detect path to logical volume where the root partition resides.") if lvm_path.kind_of?(Array)
return lvm_path.split[0]
end
|
#detect_lvm_partition(fdisk_output) ⇒ Object
96
97
98
99
100
|
# File 'lib/jupiter/virtualmachine.rb', line 96
def detect_lvm_partition(fdisk_output)
lvm_entry = array_element(fdisk_output, '8e Linux LVM')
raise InvalidLVMError.new("No LVM partition detected.") if lvm_entry.kind_of?(Array)
return lvm_entry.split[0]
end
|
#get_users_groups(user) ⇒ Object
26
27
28
|
# File 'lib/jupiter/virtualmachine.rb', line 26
def get_users_groups(user)
groups = ssh_deploy.exec!(%Q[id -nG #{user}]).chomp.split(/ /)
end
|
#read_output_lines(command) ⇒ Object
110
111
112
113
|
# File 'lib/jupiter/virtualmachine.rb', line 110
def read_output_lines(command)
output = ssh_deploy.exec!(command).to_s
output.split("\n")
end
|
#remove_from_sudo_group(user) ⇒ Object
30
31
32
|
# File 'lib/jupiter/virtualmachine.rb', line 30
def remove_from_sudo_group(user)
result = ssh_deploy.exec!(%Q[sudo gpasswd -d #{user} sudo])
end
|
#rename_linux_guest(newname) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/jupiter/virtualmachine.rb', line 34
def rename_linux_guest(newname)
old_hostname = ssh_deploy.exec!("hostname").to_s.chomp
puts %Q[\nCurrent hostname is: #{old_hostname}\n]
ssh_deploy.exec!(%Q[sudo /bin/hostname #{newname}])
ssh_deploy.exec!(%Q[sudo /bin/chmod 666 /etc/hostname])
ssh_deploy.exec!(%Q[sudo /bin/echo '#{newname}' > /etc/hostname])
ssh_deploy.exec!(%Q[sudo /bin/chmod 644 /etc/hostname])
ssh_deploy.exec!(%Q[sudo /bin/chmod 666 /etc/hosts])
ssh_deploy.exec!(%Q[sudo /bin/sed -i 's|#{old_hostname}|#{newname}|g' /etc/hosts])
ssh_deploy.exec!(%Q[sudo /bin/chmod 644 /etc/hosts])
ssh_deploy.exec!(%Q[sudo /etc/init.d/hostname restart])
puts %Q[\nNew hostname is....]
puts ssh_deploy.exec!("hostname")
close_ssh_deploy
puts %Q[\nFinished changing hostname.]
end
|
#resize_lvm_volume_step_1 ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/jupiter/virtualmachine.rb', line 51
def resize_lvm_volume_step_1
partitions = read_output_lines('sudo fdisk -l /dev/sda')
lvm_partition = detect_lvm_partition(partitions)
lvm_partition_number = lvm_partition.tr!('^0-9', '')
cylinders = detect_cylinders(partitions)
ssh_deploy.exec!(%Q[sudo /sbin/fdisk /dev/sda]) do |channel, stream, data|
channel.send_data(%Q[p\n])
channel.send_data(%Q[d\n])
channel.send_data(%Q[#{lvm_partition_number}\n])
channel.send_data(%Q[n\n])
channel.send_data(%Q[p\n])
channel.send_data(%Q[#{lvm_partition_number}\n])
channel.send_data(%Q[\n])
channel.send_data(%Q[\n])
channel.send_data(%Q[t\n])
channel.send_data(%Q[#{lvm_partition_number}\n])
channel.send_data(%Q[8e\n])
channel.send_data(%Q[w\n])
end
close_ssh_deploy
result = "\nThe system needs to reboot to read the new partition table. Please wait...\n"
end
|
#resize_lvm_volume_step_2 ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/jupiter/virtualmachine.rb', line 76
def resize_lvm_volume_step_2
partitions = read_output_lines('sudo fdisk -l /dev/sda')
lvm_partition = detect_lvm_partition(partitions)
pvs = ssh_deploy.exec!(%Q[sudo pvs])
pvresize = ssh_deploy.exec!(%Q[sudo pvresize #{lvm_partition}])
vgs = ssh_deploy.exec!(%Q[sudo vgs])
logical_vol = detect_lvm_group_path
lvextend = ssh_deploy.exec!(%Q[sudo lvextend -l +100%FREE #{logical_vol}])
resize2fs = ssh_deploy.exec!(%Q[sudo resize2fs #{logical_vol}])
close_ssh_deploy
result = "\n#{pvresize}\n\n#{vgs}\n\n#{lvextend}\n\n#{resize2fs}\n"
end
|
#ssh_deploy ⇒ Object
16
17
18
|
# File 'lib/jupiter/virtualmachine.rb', line 16
def ssh_deploy
@ssh_deploy ||= Net::SSH.start(ip, @deploy_user, password: @deploy_pass, port: port)
end
|