18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/vagrant/hivemind/commands/morph.rb', line 18
def execute
options = {}
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant hivemind morph [options]"
o.separator ""
o.separator "Options:"
o.separator ""
o.on("-n", "--hostname HOSTNAME", "The hostname of the Drone (REQUIRED)") do |n|
options[:hostname] = n
end
o.on("-a", "--ip-address IPADDRESS", "The new IP address of the Drone") do |a|
options[:ip_address] = a
end
o.on("-c", "--control", "Promote the Drone to be a Control Machine") do |c|
options[:control] = c
end
o.on("-s", "--size SIZE", BOX_SIZES.keys.map(&:to_s), "The new Box Size of the Drone [:extra_small, :small, :medium, :large, :extra_large] (default: :small)") do |s|
options[:size] = s
end
o.on("-p", "--forwarded-port PORTPAIR", "Forward GUESTPORT to HOSTPORT (the format is GUESTPORT:HOSTPORT)") do |p|
options[:forwarded_port] = p
end
o.on("-D", "--detach-data", "Detach the Drone's data directory as a synced folder to the host") do |d|
options[:detach] = d
end
o.on("-d", "--directory DIRECTORY", "Specify the directory where '#{HIVE_FILE}' is located (default: current directory)") do |d|
options[:directory] = []
options[:directory] << d
end
end
argv = parse_options(opts)
return if !argv
root_path = Path.get_root_path_from_options options
unless options[:hostname]
@env.ui.info opts.help
return 0
end
unless HiveFile.exist? root_path
@env.ui.error "There is no Hive file in the working directory."
return 1
end
hosts = HiveFile.read_from root_path
unless hosts.values.map(&:hostname).include? options[:hostname]
@env.ui.error "The specified hostname does not exist!"
return 1
end
if options[:ip_address]
validation_error = is_valid_ip_address?(options[:ip_address], hosts)
if validation_error
@env.ui.error validation_error
return 1
end
end
if options[:forwarded_port]
validation_error = is_valid_forwarded_port?(options[:forwarded_port], hosts)
if validation_error
@env.ui.error validation_error
return 1
end
end
host = hosts[options[:hostname]]
if options[:ip_address]
host.ip_address = options[:ip_address]
end
if options[:control]
host.is_control = true
end
if options[:size]
host.box_size = options[:size]
end
if options[:forwarded_port]
guest_port, host_port = Network.port_pair_to_i(options[:forwarded_port])
host.forwarded_ports ||= []
port_pair = Network.get_host_port_pair_with_guest_port(guest_port, host)
if port_pair
port_pair["host_port"] = host_port
else
port_pair = {
"guest_port" => guest_port,
"host_port" => host_port
}
host.forwarded_ports << port_pair
end
end
if options[:detach]
host.is_data_detached = true
end
hosts.delete options[:hostname]
drone = {
options[:hostname] => host
}
HiveFile.write_to hosts.merge(drone), root_path
@env.ui.info "Morphed the Drone with hostname '#{options[:hostname]}'"
0
end
|