Class: Hula::BoshDirector

Inherits:
Object
  • Object
show all
Defined in:
lib/hula/bosh_director.rb

Defined Under Namespace

Classes: DirectorIsBroken, DirectorPortNotOpen, NoManifestSpecified

Instance Method Summary collapse

Constructor Details

#initialize(target_url:, username:, password:, manifest_path: nil, command_runner: CommandRunner.new, logger: default_logger, certificate_path: nil, env_login: false) ⇒ BoshDirector

Returns a new instance of BoshDirector.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hula/bosh_director.rb', line 24

def initialize(
  target_url:,
  username:,
  password:,
  manifest_path: nil,
  command_runner: CommandRunner.new,
  logger: default_logger,
  certificate_path: nil,
  env_login: false
)
  @target_url            = target_url
  @username              = username
  @password              = password
  @default_manifest_path = manifest_path
  @command_runner        = command_runner
  @logger                = logger
  @certificate_path      = certificate_path
  @env_login             = 

  
end

Instance Method Details

#delete_deployment(deployment_name, force: false) ⇒ Object



56
57
58
59
60
# File 'lib/hula/bosh_director.rb', line 56

def delete_deployment(deployment_name, force: false)
  cmd = ["delete deployment #{deployment_name}"]
  cmd << '-f' if force
  run_bosh(cmd.join(' '))
end

#deploy(manifest_path = default_manifest_path) ⇒ Object



52
53
54
# File 'lib/hula/bosh_director.rb', line 52

def deploy(manifest_path = default_manifest_path)
  run_bosh("--deployment #{manifest_path} deploy")
end

#deployment_namesObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hula/bosh_director.rb', line 125

def deployment_names
  deployments = run_bosh('deployments')
  # [\n\r]+ a new line,
  # \s* maybe followed by whitespace,
  # \| followed by a pipe,
  # \s+ followed by whitespace,
  # ([^\s]+) followed some characters (ie, not whitespace, or a pipe) — this is the match
  first_column = deployments.scan(/[\n\r]+\s*\|\s+([^\s\|]+)/).flatten

  first_column.drop(1) # without header
end

#download_manifest(deployment_name) ⇒ Object



176
177
178
179
180
181
# File 'lib/hula/bosh_director.rb', line 176

def download_manifest(deployment_name)
  Tempfile.open 'manifest' do |file|
    run_bosh("download manifest #{deployment_name} #{file.path}")
    return YAML.load_file("#{file.path}")
  end
end

#has_logfiles?(job_name, logfile_names) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
# File 'lib/hula/bosh_director.rb', line 117

def has_logfiles?(job_name, logfile_names)
  logs = job_logfiles(job_name)
  logfile_names.each do |logfile_name|
    return false unless logs.include?(logfile_name)
  end
  true
end

#ips_for_job(job, deployment_name = nil) ⇒ Object

Parses output of ‘bosh vms` like below, getting an array of IPs for a job name ------------------------------------———---------------————–+ | Job/index | State | Resource Pool | IPs | ------------------------------------———---------------————–+ | api_z1/0 | running | large_z1 | 10.244.0.138 | …

Also matches output from 1.3184 bosh_cli e.g.

------------------------------------------------———----------------————–+ | VM | State | AZ | VM Type | IPs | ------------------------------------------------———----------------————–+ | api_z1/0 (fe04916e-afd0-42a3-aaf5-52a8b163f1ab)| running | n/a | large_z1 | 10.244.0.138 | …



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/hula/bosh_director.rb', line 152

def ips_for_job(job, deployment_name = nil)
  output = run_bosh("vms #{deployment_name}")
  deployments = output.split(/^Deployment/)

  job_ip_map = {}

  deployments.each do |deployment|
    rows = deployment.split("\n")
    row_cols = rows.map { |row| row.split('|') }
    job_cols = row_cols.  select { |cols| cols.length == 5 || cols.length == 6 } # match job boxes
    job_ip_pairs = job_cols.map { |cols| [cols[1].strip.split(' ')[0], cols.last.strip] }
    jobs_with_real_ips = job_ip_pairs.select { |pairs| pairs.last =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ }
    # converts eg   cf-redis-broker/2  to cf-redis-broker
    jobs_without_instance_numbers = jobs_with_real_ips.map { |pair| [pair.first.gsub(/\/.*/, ''), pair.last] }
    jobs_without_instance_numbers.each do |job|
      name, ip = job
      job_ip_map[name] ||= []
      job_ip_map[name] << ip
    end
  end

  job_ip_map.fetch(job, [])
end

#job_logfiles(job_name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/hula/bosh_director.rb', line 106

def job_logfiles(job_name)
  tmpdir = Dir.tmpdir
  run_bosh("logs #{job_name} 0 --job --dir #{tmpdir}")
  tarball = Dir[File.join(tmpdir, job_name.to_s + '*.tgz')].last
  output = command_runner.run("tar tf #{tarball}")
  lines = output.split(/\n+/)
  filepaths = lines.map { |f| Pathname.new(f) }
  logpaths = filepaths.select { |f| f.extname == '.log' }
  logpaths.map(&:basename).map(&:to_s)
end

#lite?Boolean

Should rely on ‘bosh status` and CPI, but currently Bosh Lite is reporting ’vsphere’ instead of ‘warden’.

Returns:

  • (Boolean)


48
49
50
# File 'lib/hula/bosh_director.rb', line 48

def lite?
  target_url.include? '192.168.50.4'
end

#recreate(name) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/hula/bosh_director.rb', line 84

def recreate(name)
  properties = job_properties(name)

  instances = properties.fetch('instances')

  instances.times do |instance_index|
    run_bosh("recreate #{name} #{instance_index}")
  end
end

#recreate_all(jobs) ⇒ Object



72
73
74
75
76
# File 'lib/hula/bosh_director.rb', line 72

def recreate_all(jobs)
  jobs.each do |name|
    recreate(name)
  end
end

#recreate_instance(name, index) ⇒ Object



78
79
80
81
82
# File 'lib/hula/bosh_director.rb', line 78

def recreate_instance(name, index)
  validate_job_instance_index(name, index)

  run_bosh("recreate #{name} #{index}")
end

#run_errand(name, manifest_path: default_manifest_path, keep_alive: false) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/hula/bosh_director.rb', line 62

def run_errand(name, manifest_path: default_manifest_path, keep_alive: false)
    command = "--deployment #{manifest_path} run errand #{name}"

  if keep_alive
    command << " --keep-alive"
  end

  run_bosh(command)
end

#start(name, index) ⇒ Object



100
101
102
103
104
# File 'lib/hula/bosh_director.rb', line 100

def start(name, index)
  validate_job_instance_index(name, index)

  run_bosh("start #{name} #{index} --force")
end

#stop(name, index) ⇒ Object



94
95
96
97
98
# File 'lib/hula/bosh_director.rb', line 94

def stop(name, index)
  validate_job_instance_index(name, index)

  run_bosh("stop #{name} #{index} --force")
end