Class: DEIS::Config

Inherits:
Thor
  • Object
show all
Extended by:
Utils
Includes:
Utils
Defined in:
lib/deis_deploy/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

config_data, data_to_deis_config_set, file_store, target_host

Class Method Details

.config_storageObject

make and return the directory path for data storage



250
251
252
# File 'lib/deis_deploy/config.rb', line 250

def self.config_storage
  self.file_store(DEIS::CONFIG_STORE)
end

.current(env = "staging") ⇒ Object

find and return current config



261
262
263
264
265
266
267
268
# File 'lib/deis_deploy/config.rb', line 261

def self.current(env="staging")
  name = DEIS::Config.full_name(env)
  "found config name: #{name}".log
  if DEIS::Config.data.has_key?(name)
    return DEIS::Config.data[name]
  end
  return nil
end

.dataObject

fetch & parse json file containing the data for this app



255
256
257
# File 'lib/deis_deploy/config.rb', line 255

def self.data
  self.config_data
end

.deis_end_pointObject



200
201
202
# File 'lib/deis_deploy/config.rb', line 200

def self.deis_end_point
  self.target_host("deis_router_end_point")
end

.existing_name(repo = nil) ⇒ Object



218
219
220
221
222
223
224
225
226
227
# File 'lib/deis_deploy/config.rb', line 218

def self.existing_name(repo=nil)
  repo = DEIS::Git.repo if repo.nil?
  names = DEIS::Config.names(repo)
  name = nil
  if names && names.size > 0
     content = names.select{|r| r["active"]}
     name = if !content.nil? then content.first["name"] end
  end
  name
end

.full_name(env = "staging", repo = nil, name = nil) ⇒ Object



240
241
242
243
244
# File 'lib/deis_deploy/config.rb', line 240

def self.full_name(env="staging", repo=nil, name=nil)
  repo = DEIS::Git.repo if repo.nil?
  name = self.name if name.nil?
  repo+"-"+name+"-"+env
end

.generate_name(repo) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/deis_deploy/config.rb', line 232

def self.generate_name(repo)
  name = DEIS::Config.new_name(repo)
  names = DEIS::Config.names(repo)
  data = names.push({:name => name, :active => true} ).to_json
  DEIS::Config.save_names(data, repo)
  name
end

.nameObject

check for / generate a name and store that name in the config file



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/deis_deploy/config.rb', line 205

def self.name
  repo = DEIS::Git.repo
  names = DEIS::Config.names(repo)
  if names.size > 0
    name = DEIS::Config.existing_name(repo)
    "Found existing name: #{name}".log
  else
    name =  DEIS::Config.generate_name(repo)
    "Created name: #{name}".log
  end
  return name
end

.name_storageObject



246
247
248
# File 'lib/deis_deploy/config.rb', line 246

def self.name_storage
  self.file_store(DEIS::NAMES_STORE)
end

.names(repo = nil) ⇒ Object

fetch all names



138
139
140
141
142
143
144
145
146
# File 'lib/deis_deploy/config.rb', line 138

def self.names(repo=nil)
  repo = DEIS::Git.repo if repo.nil?
  file = DEIS::Config.name_storage+repo+".json"
  if File.exists?(file)
    return JSON.parse( File.open(file, "r"){|f| f.read} )
  else
    return []
  end
end

.new_name(repo = nil) ⇒ Object



229
230
231
# File 'lib/deis_deploy/config.rb', line 229

def self.new_name(repo=nil)
  Faker::App.name.downcase.gsub(" ", "-")
end

.save(array) ⇒ Object

save the config array to a file



193
194
195
196
197
# File 'lib/deis_deploy/config.rb', line 193

def self.save(array)
  file = DEIS::Config.config_storage+"#{DEIS::Git.repo}.json"
  data = array.to_json
  File.open(file, "w") {|f| f.write( data ) }
end

.save_current(env, config, name = nil) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/deis_deploy/config.rb', line 270

def self.save_current(env, config, name=nil)
  name = DEIS::Config.full_name(env) if name.nil?
  stored = DEIS::Config.data
  current = if stored.nil? then {} else stored end
  current[name] = config
  DEIS::Config.save(current)
end

.save_names(names, repo = nil) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/deis_deploy/config.rb', line 148

def self.save_names(names, repo=nil)
  repo = DEIS::Git.repo if repo.nil?
  file = DEIS::Config.name_storage+repo+".json"
  if names.class == Array
    names = names.to_json
  end
  File.open(file, "w") {|f| f.write( names ) }
end

Instance Method Details

#domain(env = "staging", domain) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/deis_deploy/config.rb', line 32

def domain(env="staging", domain)
  DEIS::Validations.config
  stored = DEIS::Config.data
  current = if stored.nil? then {} else stored end
  name = DEIS::Config.full_name(env)
  "Adding domain #{domain} to #{env}".info
  "Using name: #{name}".log
  if current.has_key?(name)
    current[name]["url"] = {} if current[name]["url"].nil?
    current[name]["url"][env] = [] if current[name]["url"][env].nil?
    current[name]["url"][env].push(domain)
    DEIS::Config.save(current)
    "Complete (run 'deisDeploy #{env}' to deploy changes)".important
  else
    "No config values set, create those first".fatal
    exit 1
  end
end

#domains(env = "staging") ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/deis_deploy/config.rb', line 52

def domains(env="staging")
  DEIS::Validations.config
  stored = DEIS::Config.data
  name = DEIS::Config.full_name(env)
  current = if stored.nil? then {} else stored end
  "Listing domains in #{env}..".info
  if current.has_key?(name) && current[name].has_key?("url") && current[name]["url"].has_key?(env)
    "Found domains".log
    printf(DEIS::FORMAT_OK, "Domain", "")
    printf(DEIS::FORMAT_OK, "-------------------------------", "")
    current[name]["url"][env].each{|v| printf(DEIS::FORMAT_OK, "#{v}", "") }
  else
    "No domains found".info
  end
  "Complete".important
end

#list(env = "staging") ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/deis_deploy/config.rb', line 70

def list(env="staging")
  DEIS::Validations.config
  stored = DEIS::Config.data
  name = DEIS::Config.full_name(env)
  "Getting config data for #{name}\n".info
  if !stored.nil? && !name.nil? && !stored[name].nil? && !stored[name]["config"].nil?
    "Found data for #{name}".log
    printf(DEIS::FORMAT_OK, "Name", "Value")
    printf(DEIS::FORMAT_OK, "-------------------------------", "---------")
    stored[name]['config'].each {|k,v| printf(DEIS::FORMAT_OK, "#{k}", "#{v}") }
  else
    "No data".info
  end
  "checking for config data that will be removed..".log
  if !stored.nil? && !name.nil? && !stored[name].nil? && !stored[name]["removed_config"].nil? && stored[name]["removed_config"].size > 0
    "\nThe following config items are waiting to be removed".info
    printf(DEIS::FORMAT_OK, "Name", "Value")
    printf(DEIS::FORMAT_OK, "-------------------------------", "---------")
    stored[name]['removed_config'].each {|r| printf(DEIS::FORMAT_OK, "#{r['k']}", "#{r['v']}") }
  end

  "Complete".important
end

#name(name) ⇒ Object



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

def name(name)
  DEIS::Validations.config
  file = DEIS::Config.name_storage+DEIS::Git.repo+".json"
  if File.exists?(file)
    "Cannot set name, use rename instead".fatal
    exit
  end
  data = [ {:name => name, :active => true} ].to_json
  File.open(file, "w") {|f| f.write( data ) }
  "Complete".important
end

#namesObject



129
130
131
132
133
134
135
# File 'lib/deis_deploy/config.rb', line 129

def names
  DEIS::Validations.config
  file = DEIS::Config.name_storage+DEIS::Git.repo+".json"
  printf(DEIS::FORMAT_OK, "Name", "")
  printf(DEIS::FORMAT_OK, "-------------------------------", "")
  JSON.parse( File.open(file, "r"){|f| f.read} ).each{ |row| printf(DEIS::FORMAT_OK, row['name'], "")}
end

#rename(env, newname = nil) ⇒ Object



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
189
190
# File 'lib/deis_deploy/config.rb', line 158

def rename(env, newname=nil)
  DEIS::Validations.config
  repo = DEIS::Git.repo
  "Warning, this may break deployed versions of this app.".warning
  newname = DEIS::Config.new_name(repo) if newname.nil?
  # find the current name
  currentname = DEIS::Config.full_name(env)
  current_short_name = DEIS::Config.name
  "#{currentname}".log
  names = DEIS::Config.names(repo)
  exists = names.select{ |row| row['name'] == newname}
  if exists.nil? || exists.size == 0
    "Renaming #{current_short_name} to #{newname}".log
    # adjust the name file
    names = names.select!{|row| row['name'] != current_short_name}
    names = [] if names.nil?
    names = names.push({:name => newname, :active => true})
    # adjust config file
    stored = DEIS::Config.data
    if stored.has_key?(currentname)
      key = DEIS::Config.full_name(env, repo, newname)
      stored[key] = stored[currentname]
      stored[key]['deis_name'] = newname
      stored.delete(currentname)
      DEIS::Config.save(stored)
      DEIS::Config.save_names(names.to_json, repo)
      "Renamed to #{newname}".info
      "Complete".important
    end
  else
    "Cannot rename, #{newname} has already been used".fatal
  end
end

#rm(env, varname) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/deis_deploy/config.rb', line 95

def rm(env, varname)
  DEIS::Validations.config
  "Removing #{varname} from #{env}".info
  stored = DEIS::Config.data
  if !stored.nil?
    name = DEIS::Config.full_name(env)
    "Found data store for #{name}".log
    if !stored[name].nil? && !stored[name]["config"].nil? && stored[name]["config"].has_key?(varname)
      value = stored[name]["config"][varname]
      "Value was #{value}".log
      stored[name]["removed_config"] = [] if ! stored[name].has_key?("removed_config")
      stored[name]["removed_config"].push( {:k => varname, :v => value} )
      stored[name]["config"].tap{|r| r.delete(varname) }
      DEIS::Config.save(stored)
    end
  end
  "Complete (run 'deisDeploy deis_unset_config #{env}' to deploy changes)".important
end

#set(env, varname, value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deis_deploy/config.rb', line 8

def set(env, varname, value)
  DEIS::Validations.config
  stored = DEIS::Config.data
  current = if stored.nil? then {} else stored end
  name = DEIS::Config.full_name(env)
  "Setting #{varname} to #{value} in #{env}".info
  "Using name: #{name}".log
  sub_config = if !current[name].nil? && !current[name]["config"].nil? then current[name]["config"] else {:RACK_EV => env, :RAILS_ENV => env} end
  sub_config[varname] = value
  current[name] = {
    :deis_name => "#{name}",
    :dir => "#{name}-#{Time.now.to_i}",
    :remote => DEIS::Git.remote,
    :repo => DEIS::Git.repo,
    :env => env,
    :config => sub_config,
    :removed_config => []
  }
  # log output
  DEIS::Config.save(current)
  "Complete (run 'deisDeploy #{env}' to deploy changes)".important
end