Class: Bosh::Cli::Config

Inherits:
Object show all
Defined in:
lib/bosh/gen/bosh-config.rb

Constant Summary collapse

VALID_ID =
/^[-a-z0-9_.]+$/i

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, work_dir = Dir.pwd) ⇒ Config

Returns a new instance of Config.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bosh/gen/bosh-config.rb', line 49

def initialize(filename, work_dir = Dir.pwd)
  @filename = File.expand_path(filename || Bosh::Cli::DEFAULT_CONFIG_PATH)
  @work_dir = work_dir

  unless File.exists?(@filename)
    File.open(@filename, "w") { |f| Psych.dump({}, f) }
    File.chmod(0600, @filename)
  end

  @config_file = load_yaml_file(@filename, nil)

  unless @config_file.is_a?(Hash)
    @config_file = {} # Just ignore it if it's malformed
  end

rescue SystemCallError => e
  raise ConfigError, "Cannot read config file: #{e.message}"
end

Class Attribute Details

.colorizeBoolean

Returns Should CLI output be colorized?.

Returns:

  • (Boolean)

    Should CLI output be colorized?



11
12
13
# File 'lib/bosh/gen/bosh-config.rb', line 11

def colorize
  @colorize
end

.commandsHash<String,Bosh::Cli::CommandDefinition> (readonly)

Returns Available commands.

Returns:

  • (Hash<String,Bosh::Cli::CommandDefinition>)

    Available commands



8
9
10
# File 'lib/bosh/gen/bosh-config.rb', line 8

def commands
  @commands
end

.interactiveBoolean

Returns Is CLI being used interactively?.

Returns:

  • (Boolean)

    Is CLI being used interactively?



17
18
19
# File 'lib/bosh/gen/bosh-config.rb', line 17

def interactive
  @interactive
end

.max_parallel_downloadsInteger

Returns CLI max parallel downloads.

Returns:

  • (Integer)

    CLI max parallel downloads



23
24
25
# File 'lib/bosh/gen/bosh-config.rb', line 23

def max_parallel_downloads
  @max_parallel_downloads
end

.outputIO

Returns Where output goes.

Returns:

  • (IO)

    Where output goes



14
15
16
# File 'lib/bosh/gen/bosh-config.rb', line 14

def output
  @output
end

.poll_intervalInteger

Returns CLI polling interval.

Returns:

  • (Integer)

    CLI polling interval



20
21
22
# File 'lib/bosh/gen/bosh-config.rb', line 20

def poll_interval
  @poll_interval
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



272
273
274
# File 'lib/bosh/gen/bosh-config.rb', line 272

def filename
  @filename
end

Class Method Details

.register_command(command) ⇒ void

This method returns an undefined value.

Register command with BOSH CLI

Parameters:

  • command (Bosh::Cli::CommandDefinition)


34
35
36
37
38
39
# File 'lib/bosh/gen/bosh-config.rb', line 34

def self.register_command(command)
  if @commands.has_key?(command.usage)
    raise CliError, "Duplicate command '#{command.usage}'"
  end
  @commands[command.usage] = command
end

.use_color?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/bosh/gen/bosh-config.rb', line 41

def self.use_color?
  # colorization explicitly enabled, or output is tty
  return false if Bosh::Cli::Config.colorize == false

  # colorization explicitly enabled, or output is tty
  Bosh::Cli::Config.colorize || Bosh::Cli::Config.output.tty?
end

Instance Method Details

#access_token(target) ⇒ String

Returns Token associated with target.

Parameters:

  • target (String)

    Target director url

Returns:

  • (String)

    Token associated with target



127
128
129
# File 'lib/bosh/gen/bosh-config.rb', line 127

def access_token(target)
  credentials_for(target)["access_token"]
end

#aliases(category) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/bosh/gen/bosh-config.rb', line 91

def aliases(category)
  if @config_file.has_key?("aliases") && @config_file["aliases"].is_a?(Hash)
    @config_file["aliases"][category.to_s]
  else
    nil
  end
end

#ca_cert(for_target = nil) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/bosh/gen/bosh-config.rb', line 217

def ca_cert(for_target=nil)
  if for_target
    return @config_file.fetch('ca_cert', {}).fetch(for_target, nil)
  end

  return nil if target.nil?

  @config_file.fetch('ca_cert', {}).fetch(target, nil)
end

#credentials_for(target) ⇒ Hash

Returns Director credentials.

Returns:

  • (Hash)

    Director credentials



69
70
71
72
73
74
75
76
77
78
# File 'lib/bosh/gen/bosh-config.rb', line 69

def credentials_for(target)
  if @config_file["auth"].is_a?(Hash) && @config_file["auth"][target]
    @config_file["auth"][target]
  else
    {
      "username" => nil,
      "password" => nil
    }
  end
end

#deploymentString?

Read the deployment configuration. Return the deployment for the current target.

Returns:

  • (String?)

    The deployment path for the current target.



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bosh/gen/bosh-config.rb', line 151

def deployment
  return nil if target.nil?
  if @config_file.has_key?("deployment")
    if is_old_deployment_config?
      set_deployment(@config_file["deployment"])
      save
    end
    if @config_file["deployment"].is_a?(Hash)
      return @config_file["deployment"][target]
    end
  end
end

#is_old_deployment_config?Boolean

Deployment used to be a string that was only stored for your current target. As soon as you switched targets, the deployment was erased. If the user has the old config we convert it to the new config.

Returns:

  • (Boolean)

    Whether config is using the old deployment format.



143
144
145
# File 'lib/bosh/gen/bosh-config.rb', line 143

def is_old_deployment_config?
  @config_file["deployment"].is_a?(String)
end

#max_parallel_downloadsInteger

Read the max parallel downloads configuration.

Returns:

  • (Integer)

    The maximum number of parallel downloads



240
241
242
# File 'lib/bosh/gen/bosh-config.rb', line 240

def max_parallel_downloads
  self.class.max_parallel_downloads || @config_file.fetch("max_parallel_downloads", 1)
end

#password(target) ⇒ String

Returns Password associated with target.

Parameters:

  • target (String)

    Target director url

Returns:

  • (String)

    Password associated with target



121
122
123
# File 'lib/bosh/gen/bosh-config.rb', line 121

def password(target)
  credentials_for(target)["password"]
end

#read(attr, try_local_first = true) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/bosh/gen/bosh-config.rb', line 244

def read(attr, try_local_first = true)
  attr = attr.to_s
  if try_local_first && @config_file[@work_dir].is_a?(Hash) &&
      @config_file[@work_dir].has_key?(attr)
    @config_file[@work_dir][attr]
  else
    @config_file[attr]
  end
end

#refresh_token(target) ⇒ String

Returns Refresh token associated with target.

Parameters:

  • target (String)

    Target director url

Returns:

  • (String)

    Refresh token associated with target



133
134
135
# File 'lib/bosh/gen/bosh-config.rb', line 133

def refresh_token(target)
  credentials_for(target)["refresh_token"]
end

#releaseObject



201
202
203
# File 'lib/bosh/gen/bosh-config.rb', line 201

def release
  read(:release, false)
end

#release=(value) ⇒ Object



205
206
207
# File 'lib/bosh/gen/bosh-config.rb', line 205

def release=(value)
  write_global(:release, value)
end

#resolve_alias(category, alias_name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bosh/gen/bosh-config.rb', line 99

def resolve_alias(category, alias_name)
  category = category.to_s

  if @config_file.has_key?("aliases") &&
      @config_file["aliases"].is_a?(Hash) &&
      @config_file["aliases"].has_key?(category) &&
      @config_file["aliases"][category].is_a?(Hash) &&
      !@config_file["aliases"][category][alias_name].blank?
    @config_file["aliases"][category][alias_name].to_s
  else
    nil
  end
end

#saveObject



263
264
265
266
267
268
269
270
# File 'lib/bosh/gen/bosh-config.rb', line 263

def save
  File.open(@filename, "w") do |f|
    Psych.dump(@config_file, f)
  end

rescue SystemCallError => e
  raise ConfigError, e.message
end

#save_ca_cert_path(cert_path, for_target = nil) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/bosh/gen/bosh-config.rb', line 228

def save_ca_cert_path(cert_path, for_target=nil)
  expanded_path = cert_path ? File.expand_path(cert_path) : nil
  cert_target = for_target || target
  @config_file['ca_cert'] ||= {}
  @config_file['ca_cert'][cert_target] = expanded_path

  expanded_path
end

#set_alias(category, alias_name, value) ⇒ Object



85
86
87
88
89
# File 'lib/bosh/gen/bosh-config.rb', line 85

def set_alias(category, alias_name, value)
  @config_file["aliases"] ||= {}
  @config_file["aliases"][category.to_s] ||= {}
  @config_file["aliases"][category.to_s][alias_name] = value
end

#set_credentials(target, credentials) ⇒ Object



80
81
82
83
# File 'lib/bosh/gen/bosh-config.rb', line 80

def set_credentials(target, credentials)
  @config_file["auth"] ||= {}
  @config_file["auth"][target] = credentials
end

#set_deployment(deployment_file_path) ⇒ Object

Sets the deployment file for the current target. If the deployment is the old deployment configuration, it will turn it into the format.

Parameters:

  • deployment_file_path (String)

    The string path to the deployment file.

Raises:

  • (MissingTarget)

    If there is no target set.



170
171
172
173
174
175
# File 'lib/bosh/gen/bosh-config.rb', line 170

def set_deployment(deployment_file_path)
  raise MissingTarget, "Must have a target set" if target.nil?
  @config_file["deployment"] = {} if is_old_deployment_config?
  @config_file["deployment"] ||= {}
  @config_file["deployment"][target] = deployment_file_path
end

#targetObject



177
178
179
# File 'lib/bosh/gen/bosh-config.rb', line 177

def target
  read(:target, false)
end

#target=(value) ⇒ Object



181
182
183
# File 'lib/bosh/gen/bosh-config.rb', line 181

def target=(value)
  write_global(:target, value)
end

#target_nameObject



185
186
187
# File 'lib/bosh/gen/bosh-config.rb', line 185

def target_name
  read(:target_name, false)
end

#target_name=(value) ⇒ Object



189
190
191
# File 'lib/bosh/gen/bosh-config.rb', line 189

def target_name=(value)
  write_global(:target_name, value)
end

#target_uuidObject



209
210
211
# File 'lib/bosh/gen/bosh-config.rb', line 209

def target_uuid
  read(:target_uuid, false)
end

#target_uuid=(value) ⇒ Object



213
214
215
# File 'lib/bosh/gen/bosh-config.rb', line 213

def target_uuid=(value)
  write_global(:target_uuid, value)
end

#target_versionObject



193
194
195
# File 'lib/bosh/gen/bosh-config.rb', line 193

def target_version
  read(:target_version, false)
end

#target_version=(value) ⇒ Object



197
198
199
# File 'lib/bosh/gen/bosh-config.rb', line 197

def target_version=(value)
  write_global(:target_version, value)
end

#username(target) ⇒ String

Returns Username associated with target.

Parameters:

  • target (String)

    Target director url

Returns:

  • (String)

    Username associated with target



115
116
117
# File 'lib/bosh/gen/bosh-config.rb', line 115

def username(target)
  credentials_for(target)["username"]
end

#write(attr, value) ⇒ Object



254
255
256
257
# File 'lib/bosh/gen/bosh-config.rb', line 254

def write(attr, value)
  @config_file[@work_dir] ||= {}
  @config_file[@work_dir][attr.to_s] = value
end

#write_global(attr, value) ⇒ Object



259
260
261
# File 'lib/bosh/gen/bosh-config.rb', line 259

def write_global(attr, value)
  @config_file[attr.to_s] = value
end