Class: JDCMicro::McfCommand

Inherits:
JDC::CLI show all
Defined in:
lib/micro/plugin.rb

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from JDC::CLI

#add_exception_name_to_msg, #build_client, #check_key, #check_logged_in, #check_organization_and_space, #check_target, client, #client, client=, #client_target, #color_enabled?, #debug?, #default_action, #ensure_config_dir, #err, #execute, #fail, #fail_unknown, #force?, #formatted_exception_output, #help, #help_header, #invalidate_client, #log_error, #name_list, #normalize_targets_info, #one_of, #quiet?, #remove_target_info, #sane_target_url, #save_target_info, #save_targets, #save_token_if_it_changes, #set_target, #table, #target_info, #targets_info, #user_colors, #verbose?, #wrap_errors

Methods included from JDC::Spacing

#indented, #justify, #line, #lines, #quiet?, #spaced, #start_line, #tabular, #text_width, #trim_escapes

Methods included from JDC::Interactive

#ask, #handler, #input_state, #list_choices, #prompt, #show_default

Class Method Details

.platformObject



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

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


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/micro/plugin.rb', line 120

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
    JDCMicro::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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/micro/plugin.rb', line 81

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 'jdc micro'"
  end
end

#locate_vmx(platform) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/micro/plugin.rb', line 162

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

#microObject



183
184
185
186
187
188
# File 'lib/micro/plugin.rb', line 183

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/micro/plugin.rb', line 34

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/micro/plugin.rb', line 59

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



20
21
22
23
24
25
26
27
# File 'lib/micro/plugin.rb', line 20

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

  line "Micro Cloud Foundry VM currently in #{b(mode)} mode"
  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



153
154
155
156
157
158
159
160
# File 'lib/micro/plugin.rb', line 153

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

#runnerObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/micro/plugin.rb', line 70

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.



144
145
146
147
148
149
150
# File 'lib/micro/plugin.rb', line 144

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

  store_micro(config)
end

#store_micro(micro) ⇒ Object



190
191
192
193
194
195
# File 'lib/micro/plugin.rb', line 190

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/micro/plugin.rb', line 97

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