Class: VMCMicro::McfCommand

Inherits:
VMC::CLI
  • Object
show all
Defined in:
lib/mcf-vmc-plugin/plugin.rb

Constant Summary collapse

MICRO_FILE =
'~/.vmc/micro.yml'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.platformObject



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mcf-vmc-plugin/plugin.rb', line 176

def self.platform
  case RUBY_PLATFORM
  when /darwin/  # x86_64-darwin11.2.0
    :darwin
  when /linux/   # x86_64-linux
    :linux
  when /mingw|mswin32|cygwin/ # i386-mingw32
    :windows
  else
    RUBY_PLATFORM
  end
end

Instance Method Details

#build_configObject

Returns the configuration needed to run the micro related subcommands. First loads saved config from file (if there is any), then overrides loaded values with command line arguments, and finally tries to guess in case neither was used:

vmx       location of micro.vmx file
vmrun     location of vmrun command
password  password for vcap user (in the guest vm)
platform  current platform


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mcf-vmc-plugin/plugin.rb', line 126

def build_config
  conf = micro # returns {} if there isn't a saved config

  override(conf, :vmx, true) do
    locate_vmx(McfCommand.platform)
  end

  override(conf, :vmrun, true) do
    VMCMicro::VMrun.locate(McfCommand.platform)
  end

  override(conf, :password) do
    ask("Please enter your MCF VM password (vcap user) password", :echo => "*")
  end

  conf[:platform] = McfCommand.platform

  conf
end

#check_vm_runningObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mcf-vmc-plugin/plugin.rb', line 87

def check_vm_running
  unless runner.running?
    if ask("MCF VM is not running. Do you want to start it?", :default => true)
      with_progress("Starting MCF VM") do
        runner.start!
      end
    else
      fail "MCF VM needs to be running."
    end
  end

  unless runner.ready?
    fail "MCF VM initial setup needs to be completed before using 'vmc micro'"
  end
end

#locate_vmx(platform) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/mcf-vmc-plugin/plugin.rb', line 168

def locate_vmx(platform)
  paths = YAML.load_file(VMCMicro.config_file('paths.yml'))
  vmx_paths = paths[platform.to_s]['vmx']
  vmx = VMCMicro.locate_file('micro.vmx', 'micro', vmx_paths)
  fail "Unable to locate micro.vmx, please supply --vmx option" unless vmx
  vmx
end

#microObject



189
190
191
192
193
194
# File 'lib/mcf-vmc-plugin/plugin.rb', line 189

def micro
  micro_file = File.expand_path(MICRO_FILE)
  return {} unless File.exists? micro_file
  contents = File.read(micro_file).strip
  MultiJson.load(contents)
end

#micro_offlineObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mcf-vmc-plugin/plugin.rb', line 39

def micro_offline
  if !runner.nat?
    if ask("Reconfigure MCF VM network to nat mode and reboot?", :default => true)
      with_progress("Rebooting MCF VM") do
        runner.reset_to_nat!
      end
    else
      fail "Aborted"
    end
  end

  with_progress("Setting MCF VM to offline mode") do
    runner.offline!
  end

  with_progress("Setting host DNS server") do
    runner.set_host_dns!
  end
end

#micro_onlineObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/mcf-vmc-plugin/plugin.rb', line 65

def micro_online
  runner
  with_progress("Unsetting host DNS server") do
    runner.unset_host_dns!
  end

  with_progress("Setting MCF VM to online mode") do
    runner.online!
  end
end

#micro_statusObject



23
24
25
26
27
28
29
30
31
# File 'lib/mcf-vmc-plugin/plugin.rb', line 23

def micro_status
  mode = runner.offline? ? 'offline' : 'online'

  line "Micro Cloud Foundry VM currently in #{b(mode)} mode"
  # should the VMX path be unescaped?
  line "VMX Path: #{c(runner.vmx, :good)}"
  line "Domain: #{c(runner.domain, :good)}"
  line "IP Address: #{c(runner.ip, :good)}"
end

#override(config, option, escape = false, &blk) ⇒ Object

override with command line arguments and yield the block in case the option isn’t set



159
160
161
162
163
164
165
166
# File 'lib/mcf-vmc-plugin/plugin.rb', line 159

def override(config, option, escape=false, &blk)
  # override if given on the command line
  if opt = input[option]
    opt = VMCMicro.escape_path(opt) if escape
    config[option] = opt
  end
  config[option] = yield unless config[option]
end

#runnerObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/mcf-vmc-plugin/plugin.rb', line 76

def runner
  return @runner if @runner

  config = build_config
  @runner = switcher(config)
  check_vm_running
  store_config(config)

  @runner
end

#store_config(config) ⇒ Object

Save the cleartext password if –save is supplied. Note: it is due to vix we have to use a cleartext password :( Only if –password is used and not –save is the password deleted from the config file before it is stored to disk.



150
151
152
153
154
155
156
# File 'lib/mcf-vmc-plugin/plugin.rb', line 150

def store_config(config)
  if input[:save]
    warn("cleartext password saved in: #{MICRO_FILE}")
  end

  store_micro(config)
end

#store_micro(micro) ⇒ Object



196
197
198
199
200
201
# File 'lib/mcf-vmc-plugin/plugin.rb', line 196

def store_micro(micro)
  micro_file = File.expand_path(MICRO_FILE)
  File.open(micro_file, 'w') do |file|
    file.write(MultiJson.dump(micro))
  end
end

#switcher(config) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mcf-vmc-plugin/plugin.rb', line 103

def switcher(config)
  case McfCommand.platform
  when :darwin
    VMCMicro::Switcher::Darwin.new(config)
  when :linux
    VMCMicro::Switcher::Linux.new(config)
  when :windows
    VMCMicro::Switcher::Windows.new(config)
  when :dummy # for testing only
    VMCMicro::Switcher::Dummy.new(config)
  else
    fail "unsupported platform: #{McfCommand.platform}"
  end
end