Class: ConfigKit::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/config_kit/tool.rb

Defined Under Namespace

Classes: ConfigMetadataError, MissingDefaultVersionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, api_version = ConfigKit.config.api_version, kind = ConfigKit.config.kind) ⇒ Tool

Returns a new instance of Tool.



10
11
12
13
14
15
16
# File 'lib/config_kit/tool.rb', line 10

def initialize(opts={}, api_version=ConfigKit.config.api_version, kind=ConfigKit.config.kind)
  @kind = kind
  @api_version = api_version
  @url = opts.fetch(:url, ConfigKit.config.url)
  @acl_token = opts.fetch(:acl_token, ConfigKit.config.acl_token)
  @client = ConfigKit::Client.new(@url, @acl_token)
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



9
10
11
# File 'lib/config_kit/tool.rb', line 9

def api_version
  @api_version
end

#config_dataObject (readonly)

Returns the value of attribute config_data.



9
10
11
# File 'lib/config_kit/tool.rb', line 9

def config_data
  @config_data
end

#deploy_dataObject (readonly)

Returns the value of attribute deploy_data.



9
10
11
# File 'lib/config_kit/tool.rb', line 9

def deploy_data
  @deploy_data
end

#idc_dataObject (readonly)

Returns the value of attribute idc_data.



9
10
11
# File 'lib/config_kit/tool.rb', line 9

def idc_data
  @idc_data
end

#kindObject (readonly)

Returns the value of attribute kind.



9
10
11
# File 'lib/config_kit/tool.rb', line 9

def kind
  @kind
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



9
10
11
# File 'lib/config_kit/tool.rb', line 9

def namespace
  @namespace
end

Instance Method Details

#all_appsObject



138
139
140
141
# File 'lib/config_kit/tool.rb', line 138

def all_apps
  apps = @client.read(deploy_path)[kind]['deploy'].keys
  apps
end

#bootstrap_config(data, name) ⇒ Object



18
19
20
# File 'lib/config_kit/tool.rb', line 18

def bootstrap_config(data, name)
  ConfigKit::ConfigData.new(data, name, api_version, kind)
end

#bootstrap_txn(data, name) ⇒ Object



22
23
24
25
# File 'lib/config_kit/tool.rb', line 22

def bootstrap_txn(data,name)
  @config_data = bootstrap_config(data, name)
  @client.create_txn(@config_data)  
end

#check_version_for?(app, version) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/config_kit/tool.rb', line 43

def check_version_for?(app, version)
  path = path_for(app, version)
  @client.read(path).nil?
end

#delete_txn(data, name, version = nil) ⇒ Object



97
98
99
# File 'lib/config_kit/tool.rb', line 97

def delete_txn(data, name, version=nil)
  config = bootstrap_config(data)
end

#deploy_app_path(app) ⇒ Object



155
156
157
# File 'lib/config_kit/tool.rb', line 155

def deploy_app_path(app)
  "#{deploy_path}/#{app}/default"
end

#deploy_config(name, version, extra, cs) ⇒ Object



27
28
29
# File 'lib/config_kit/tool.rb', line 27

def deploy_config(name, version,extra,cs)
  ConfigKit::DeployData.new(name, version, extra: extra, cs: cs)
end

#deploy_pathObject



159
160
161
# File 'lib/config_kit/tool.rb', line 159

def deploy_path
  "#{kind}/deploy"
end

#deploy_txn(app, version, extra, cs = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/config_kit/tool.rb', line 31

def deploy_txn(app,version,extra,cs=nil)
  if extra != :no_default && check_version_for?(app,version)
    ConfigKit.logger.debug "Missing version(#{version}) for app(#{app}) to set default"
    raise MissingDefaultVersionError.new "Missing default version for app(#{app}), pls set default version first.\n"
  end
  ConfigKit.logger.debug "compose deploy data for app(#{app}:#{version})"
  @deploy_data = deploy_config(app,version,extra, cs)
  @client.create_txn(@deploy_data)
  @client.perform_txn
  @deploy_data
end

#describe(app, version) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/config_kit/tool.rb', line 75

def describe(app, version)
  @content = {}
  data = @client.read(idc_path)
  return @content if data.nil?
  @content['idc'] = data['config_kit']['idc']
  data = @client.read(deploy_path)
  return @content if data.nil?
  @content['deploy'] = data['config_kit']['deploy']
  unless app == 'idc'
    if version.nil?
      data = @client.read(path_for(app))
      return @content if data.nil?
      @content[app] = data['config_kit'][app]
    else
      data = @client.read(path_for(app))
      return @content if data.nil?
      @content[app] = data['config_kit'][app][version]
    end
  end
  @content
end

#get(app) ⇒ Object



101
102
103
104
# File 'lib/config_kit/tool.rb', line 101

def get(app)
  return get_all if app == 'all'
  get_one(app)
end

#get_allObject



106
107
108
109
110
111
112
# File 'lib/config_kit/tool.rb', line 106

def get_all()
  config_data = {}
  all_apps.each do |app|
    config_data = config_data.deep_merge(get_one(app))
  end
  config_data
end

#get_app(app) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/config_kit/tool.rb', line 126

def get_app(app)
  version = version_for(app)
  if version.nil?
    ConfigKit.logger.debug "Missing default version for app(#{app})"
    raise MissingDefaultVersionError.new "Missing default version for app(#{app}), pls set default version first.\n"
  end
  app_path = path_for(app, version)
  app_version = @client.read(app_path)[kind]
  app_data = {}
  app_data[app] = app_version[app][version]
end

#get_idcObject



57
58
59
60
61
# File 'lib/config_kit/tool.rb', line 57

def get_idc
  data =  @client.read(idc_path)
  return {'idc' => 'N/A'} if data.nil?
  data['config_kit']['idc']
end

#get_idc_codenameObject



69
70
71
72
73
# File 'lib/config_kit/tool.rb', line 69

def get_idc_codename
  data =  @client.read(idc_path)
  return nil if data.nil?
  data['config_kit']['idc']['codename'] 
end

#get_idc_envObject



63
64
65
66
67
# File 'lib/config_kit/tool.rb', line 63

def get_idc_env
  data =  @client.read(idc_path)
  return nil if data.nil?
  data['config_kit']['idc']['env'] 
end

#get_one(app) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/config_kit/tool.rb', line 114

def get_one(app)
  context = {}
  app_data = get_app(app)
  context[app] = app_data
  context[app]['idc'] = get_idc
  unless app == 'infra'
    infra_data = get_app('infra')['service'] 
    context[app]['service'] = infra_data
  end
  context
end

#idc_config(name, env, codename, opts) ⇒ Object



48
49
50
# File 'lib/config_kit/tool.rb', line 48

def idc_config(name, env, codename, opts)
  ConfigKit::IDCData.new(name, env, codename, opts)
end

#idc_init_txn(name, env, codename, opts = {}) ⇒ Object



52
53
54
55
# File 'lib/config_kit/tool.rb', line 52

def idc_init_txn(name, env, codename, opts={})
  @idc_data = idc_config(name, env, codename, opts)
  @client.create_txn(@idc_data)
end

#idc_pathObject



168
169
170
# File 'lib/config_kit/tool.rb', line 168

def idc_path
  "#{kind}/idc"
end

#init_txnObject



147
148
149
# File 'lib/config_kit/tool.rb', line 147

def init_txn
  @client.init_txn
end

#path_for(app, version = nil) ⇒ Object



163
164
165
166
# File 'lib/config_kit/tool.rb', line 163

def path_for(app, version=nil)
  return "#{kind}/#{app}" if version.nil?
  "#{kind}/#{app}/#{version}" 
end

#perform_txnObject



151
152
153
# File 'lib/config_kit/tool.rb', line 151

def perform_txn
  @client.perform_txn
end

#version_for(app) ⇒ Object



143
144
145
# File 'lib/config_kit/tool.rb', line 143

def version_for(app)
  @client.read(deploy_app_path(app),false)
end