Class: Bosh::Cli::Command::Micro

Inherits:
Base
  • Object
show all
Defined in:
lib/bosh/cli/commands/micro.rb

Constant Summary collapse

MICRO_DIRECTOR_PORT =
25555
DEFAULT_CONFIG_PATH =
File.expand_path('~/.bosh_deployer_config')
MICRO_BOSH_YAML =
'micro_bosh.yml'

Instance Method Summary collapse

Constructor Details

#initialize(runner) ⇒ Micro

Returns a new instance of Micro.



13
14
15
16
# File 'lib/bosh/cli/commands/micro.rb', line 13

def initialize(runner)
  super(runner)
  options[:config] ||= DEFAULT_CONFIG_PATH # hijack Cli::Config
end

Instance Method Details

#agent(*args) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/bosh/cli/commands/micro.rb', line 283

def agent(*args)
  message = args.shift
  args = args.map do |arg|
    if File.exists?(arg)
      load_yaml_file(arg)
    else
      arg
    end
  end

  say(deployer.agent.send(message.to_sym, *args).pretty_inspect)
end

#apply(spec) ⇒ Object



298
299
300
301
# File 'lib/bosh/cli/commands/micro.rb', line 298

def apply(spec)
  config = Bosh::Deployer::Config
  deployer.apply(Bosh::Deployer::Specification.new(load_yaml_file(spec), config))
end

#deleteObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/bosh/cli/commands/micro.rb', line 193

def delete
  unless deployer.exists?
    err 'No existing instance to delete'
  end

  name = deployer.state.name

  say(
    "\nYou are going to delete micro BOSH deployment '#{name}'.\n\n" +
    "THIS IS A VERY DESTRUCTIVE OPERATION AND IT CANNOT BE UNDONE!\n".make_red
  )

  unless confirmed?
    say 'Canceled deleting deployment'.make_green
    return
  end

  renderer = Bosh::Deployer::DeployerRenderer.new(Bosh::Cli::TaskTracking::EventLogRenderer.new)
  deployer.renderer = renderer

  start_time = Time.now

  deployer.delete_deployment

  renderer.finish('done')

  duration = renderer.duration || (Time.now - start_time)

  say("Deleted deployment '#{name}', took #{format_time(duration).make_green} to complete")
end

#listObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/bosh/cli/commands/micro.rb', line 226

def list
  file = File.join(work_dir, Bosh::Deployer::DeploymentsState::DEPLOYMENTS_FILE)
  if File.exists?(file)
    deployments = load_yaml_file(file)['instances']
  else
    deployments = []
  end

  err('No deployments') if deployments.size == 0

  na = 'n/a'

  deployments_table = table do |t|
    t.headings = ['Name', 'VM name', 'Stemcell name']
    deployments.each do |r|
      t << [r[:name], r[:vm_cid] || na, r[:stemcell_cid] || na]
    end
  end

  say("\n")
  say(deployments_table)
  say("\n")
  say("Deployments total: #{deployments.size}")
end

#micro_deployment(name = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/bosh/cli/commands/micro.rb', line 30

def micro_deployment(name = nil)
  if name
    set_current(name)
  else
    show_current
  end
end

#micro_helpObject



20
21
22
23
24
25
26
# File 'lib/bosh/cli/commands/micro.rb', line 20

def micro_help
  say('bosh micro sub-commands:')
  nl
  cmds = Bosh::Cli::Config.commands
  cmds = cmds.values.find_all { |c| c.usage =~ /^micro/ }
  Bosh::Cli::Command::Help.list_commands(cmds)
end

#perform(stemcell = nil) ⇒ Object



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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/bosh/cli/commands/micro.rb', line 105

def perform(stemcell = nil)
  update = !!options[:update]

  err 'No deployment set' unless deployment

  manifest = load_yaml_file(deployment)

  if stemcell.nil?
    unless manifest.is_a?(Hash)
      err('Invalid manifest format')
    end

    stemcell = dig_hash(manifest, 'resources', 'cloud_properties', 'image_id')

    if stemcell.nil?
      err 'No stemcell provided'
    end
  end

  deployer.check_dependencies

  rel_path = strip_relative_path(deployment)

  desc = "'#{rel_path.make_green}' to '#{target_name.make_green}'"

  if deployer.exists?
    if !options[:update_if_exists] && !update
      err 'Instance exists. Did you mean to --update?'
    end
    confirmation = 'Updating'
    method = :update_deployment
  else
    prefered_dir = File.dirname(File.dirname(deployment))

    unless prefered_dir == Dir.pwd
      confirm_deployment(
        "\n#{'No `bosh-deployments.yml` file found in current directory.'.make_red}\n\n" +
        'Conventionally, `bosh-deployments.yml` should be saved in ' +
        "#{prefered_dir.make_green}.\n" +
        "Is #{Dir.pwd.make_yellow} a directory where you can save state?"
      )
    end

    err 'No existing instance to update' if update
    confirmation = 'Deploying new micro BOSH instance'
    method = :create_deployment

    # make sure the user knows a persistent disk is required
    unless dig_hash(manifest, 'resources', 'persistent_disk')
      quit("No persistent disk configured in #{MICRO_BOSH_YAML}".make_red)
    end
  end

  confirm_deployment("#{confirmation} #{desc}")

  if File.extname(stemcell) == '.tgz'
    stemcell_file = Bosh::Cli::Stemcell.new(stemcell)

    say("\nVerifying stemcell...")
    stemcell_file.validate
    say("\n")

    unless stemcell_file.valid?
      err('Stemcell is invalid, please fix, verify and upload again')
    end

    stemcell_archive = Bosh::Stemcell::Archive.new(stemcell)
  end

  renderer = Bosh::Deployer::DeployerRenderer.new(Bosh::Cli::TaskTracking::EventLogRenderer.new)
  deployer.renderer = renderer

  start_time = Time.now

  deployer.send(method, stemcell, stemcell_archive)

  renderer.finish('done')

  duration = renderer.duration || (Time.now - start_time)

  update_target

  say("Deployed #{desc}, took #{format_time(duration).make_green} to complete")
end

#set_current(name) ⇒ Object

rubocop:disable MethodLength



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
# File 'lib/bosh/cli/commands/micro.rb', line 39

def set_current(name)
  manifest_filename = find_deployment(name)

  unless File.exists?(manifest_filename)
    err "Missing manifest for #{name} (tried '#{manifest_filename}')"
  end

  manifest = load_yaml_file(manifest_filename)

  unless manifest.is_a?(Hash)
    err 'Invalid manifest format'
  end

  if manifest['network'].blank?
    err 'network is not defined in deployment manifest'
  end
  ip = deployer(manifest_filename).client_services_ip

  if target
    old_director_ip = URI.parse(target).host
  else
    old_director_ip = nil
  end

  if old_director_ip != ip
    set_target(ip)
    say "#{'WARNING!'.make_red} Your target has been changed to '#{target.make_red}'!"
  end

  say "Deployment set to '#{manifest_filename.make_green}'"
  config.set_deployment(manifest_filename)
  config.save
end

#show_currentObject

rubocop:enable MethodLength



74
75
76
# File 'lib/bosh/cli/commands/micro.rb', line 74

def show_current
  say(deployment ? "Current deployment is '#{deployment.make_green}'" : 'Deployment not set')
end

#statusObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bosh/cli/commands/micro.rb', line 80

def status
  stemcell_cid = deployer_state(:stemcell_cid)
  stemcell_name = deployer_state(:stemcell_name)
  vm_cid = deployer_state(:vm_cid)
  disk_cid = deployer_state(:disk_cid)
  deployment = config.deployment ? config.deployment.make_green : 'not set'.make_red

  say('Stemcell CID'.ljust(15) + stemcell_cid)
  say('Stemcell name'.ljust(15) + stemcell_name)
  say('VM CID'.ljust(15) + vm_cid)
  say('Disk CID'.ljust(15) + disk_cid)
  say('Micro BOSH CID'.ljust(15) + Bosh::Deployer::Config.uuid)
  say('Deployment'.ljust(15) + deployment)

  update_target

  target_name = target ? target.make_green : 'not set'.make_red
  say('Target'.ljust(15) + target_name)
end