Class: VMC::Cli::Config

Inherits:
Object show all
Defined in:
lib/cli/config.rb

Constant Summary collapse

DEFAULT_TARGET =
'api.cloud.anotherservice.com'
TARGET_FILE =
'~/.as_target'
TOKEN_FILE =
'~/.as_token'
INSTANCES_FILE =
'~/.as_instances'
ALIASES_FILE =
'~/.as_aliases'
CLIENTS_FILE =
'~/.as_clients'
MICRO_FILE =
'~/.as_micro'
STOCK_CLIENTS =
File.expand_path("../../../config/clients.yml", __FILE__)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_dir = Dir.pwd) ⇒ Config

Returns a new instance of Config.



165
166
167
# File 'lib/cli/config.rb', line 165

def initialize(work_dir = Dir.pwd)
  @work_dir = work_dir
end

Class Attribute Details

.colorizeObject

Returns the value of attribute colorize.



22
23
24
# File 'lib/cli/config.rb', line 22

def colorize
  @colorize
end

.infraObject

Returns the value of attribute infra.



26
27
28
# File 'lib/cli/config.rb', line 26

def infra
  @infra
end

.nozipObject

Returns the value of attribute nozip.



25
26
27
# File 'lib/cli/config.rb', line 25

def nozip
  @nozip
end

.outputObject

Returns the value of attribute output.



23
24
25
# File 'lib/cli/config.rb', line 23

def output
  @output
end

.traceObject

Returns the value of attribute trace.



24
25
26
# File 'lib/cli/config.rb', line 24

def trace
  @trace
end

Class Method Details

.aliasesObject



88
89
90
91
92
93
94
95
96
# File 'lib/cli/config.rb', line 88

def aliases
  aliases_file = File.expand_path(ALIASES_FILE)
  # bacward compatible
  unless File.exists? aliases_file
    old_aliases_file = File.expand_path('~/.vmc-aliases')
    FileUtils.mv(old_aliases_file, aliases_file) if File.exists? old_aliases_file
  end
  aliases = YAML.load_file(aliases_file) rescue {}
end

.all_tokens(token_file_path = nil) ⇒ Object Also known as: targets



50
51
52
53
54
55
# File 'lib/cli/config.rb', line 50

def all_tokens(token_file_path=nil)
  token_file = File.expand_path(token_file_path || TOKEN_FILE)
  return nil unless File.exists? token_file
  contents = lock_and_read(token_file).strip
  JSON.parse(contents)
end

.auth_token(token_file_path = nil) ⇒ Object



59
60
61
62
63
# File 'lib/cli/config.rb', line 59

def auth_token(token_file_path=nil)
  return @token if @token
  tokens = all_tokens(token_file_path)
  @token = tokens[target_url] if tokens
end

.base_of(url) ⇒ Object



41
42
43
# File 'lib/cli/config.rb', line 41

def base_of(url)
  url.sub(/^[^\.]+\./, "")
end

.clientsObject



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cli/config.rb', line 127

def clients
  return @clients if @clients

  stock = YAML.load_file(STOCK_CLIENTS)
  clients = File.expand_path CLIENTS_FILE
  if File.exists? clients
    user = YAML.load_file(clients)
    @clients = deep_merge(stock, user)
  else
    @clients = stock
  end
end

.deep_merge(a, b) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cli/config.rb', line 115

def deep_merge(a, b)
  merge = proc do |_, old, new|
    if new.is_a?(Hash) and old.is_a?(Hash)
      old.merge(new, &merge)
    else
      new
    end
  end

  a.merge(b, &merge)
end

.instancesObject



76
77
78
79
80
81
# File 'lib/cli/config.rb', line 76

def instances
  instances_file = File.expand_path(INSTANCES_FILE)
  return nil unless File.exists? instances_file
  contents = lock_and_read(instances_file).strip
  JSON.parse(contents)
end

.lock_and_read(file) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cli/config.rb', line 140

def lock_and_read(file)
  File.open(file, File::RDONLY) {|f|
    if defined? JRUBY_VERSION
      f.flock(File::LOCK_SH)
    else
      f.flock(File::LOCK_EX)
    end
    contents = f.read
    f.flock(File::LOCK_UN)
    contents
  }
end

.lock_and_write(file, contents) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/cli/config.rb', line 153

def lock_and_write(file, contents)
  File.open(file, File::RDWR | File::CREAT, 0600) {|f|
    f.flock(File::LOCK_EX)
    f.rewind
    f.puts contents
    f.flush
    f.truncate(f.pos)
    f.flock(File::LOCK_UN)
  }
end

.microObject



103
104
105
106
107
108
# File 'lib/cli/config.rb', line 103

def micro
  micro_file = File.expand_path(MICRO_FILE)
  return {} unless File.exists? micro_file
  contents = lock_and_read(micro_file).strip
  JSON.parse(contents)
end

.remove_token_fileObject



65
66
67
# File 'lib/cli/config.rb', line 65

def remove_token_file
  FileUtils.rm_f(File.expand_path(TOKEN_FILE))
end

.store_aliases(aliases) ⇒ Object



98
99
100
101
# File 'lib/cli/config.rb', line 98

def store_aliases(aliases)
  aliases_file = File.expand_path(ALIASES_FILE)
  File.open(aliases_file, 'wb') {|f| f.write(aliases.to_yaml)}
end

.store_instances(instances) ⇒ Object



83
84
85
86
# File 'lib/cli/config.rb', line 83

def store_instances(instances)
  instances_file = File.expand_path(INSTANCES_FILE)
  lock_and_write(instances_file, instances.to_json)
end

.store_micro(micro) ⇒ Object



110
111
112
113
# File 'lib/cli/config.rb', line 110

def store_micro(micro)
  micro_file = File.expand_path(MICRO_FILE)
  lock_and_write(micro_file, micro.to_json)
end

.store_target(target_host) ⇒ Object



45
46
47
48
# File 'lib/cli/config.rb', line 45

def store_target(target_host)
  target_file = File.expand_path(TARGET_FILE)
  lock_and_write(target_file, target_host)
end

.store_token(token, token_file_path = nil) ⇒ Object



69
70
71
72
73
74
# File 'lib/cli/config.rb', line 69

def store_token(token, token_file_path=nil)
  tokens = all_tokens(token_file_path) || {}
  tokens[target_url] = token
  token_file = File.expand_path(token_file_path || TOKEN_FILE)
  lock_and_write(token_file, tokens.to_json)
end

.target_urlObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cli/config.rb', line 28

def target_url
  return @target_url if @target_url
  target_file = File.expand_path(TARGET_FILE)
  if File.exists? target_file
    @target_url = lock_and_read(target_file).strip
  else
    @target_url  = DEFAULT_TARGET
  end
  @target_url = "http://#{@target_url}" unless /^http?/ =~ @target_url
  @target_url = @target_url.gsub(/\/+$/, '')
  @target_url
end