dtk-client 0.5.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/Gemfile +5 -0
- data/Gemfile_dev +12 -0
- data/README.md +78 -0
- data/bin/dtk +54 -0
- data/bin/dtk-shell +15 -0
- data/dtk-client.gemspec +49 -0
- data/lib/auxiliary.rb +13 -0
- data/lib/bundler_monkey_patch.rb +9 -0
- data/lib/client.rb +48 -0
- data/lib/command_helper.rb +16 -0
- data/lib/command_helpers/git_repo.rb +391 -0
- data/lib/command_helpers/jenkins_client/config_xml.rb +271 -0
- data/lib/command_helpers/jenkins_client.rb +91 -0
- data/lib/command_helpers/service_importer.rb +99 -0
- data/lib/command_helpers/service_link.rb +18 -0
- data/lib/command_helpers/ssh_processing.rb +43 -0
- data/lib/commands/common/thor/assembly_workspace.rb +1089 -0
- data/lib/commands/common/thor/clone.rb +39 -0
- data/lib/commands/common/thor/common.rb +34 -0
- data/lib/commands/common/thor/edit.rb +168 -0
- data/lib/commands/common/thor/list_diffs.rb +84 -0
- data/lib/commands/common/thor/pull_clone_changes.rb +11 -0
- data/lib/commands/common/thor/pull_from_remote.rb +99 -0
- data/lib/commands/common/thor/purge_clone.rb +26 -0
- data/lib/commands/common/thor/push_clone_changes.rb +45 -0
- data/lib/commands/common/thor/push_to_remote.rb +45 -0
- data/lib/commands/common/thor/reparse.rb +36 -0
- data/lib/commands/common/thor/set_required_params.rb +29 -0
- data/lib/commands/common/thor/task_status.rb +81 -0
- data/lib/commands/thor/account.rb +213 -0
- data/lib/commands/thor/assembly.rb +329 -0
- data/lib/commands/thor/attribute.rb +62 -0
- data/lib/commands/thor/component.rb +52 -0
- data/lib/commands/thor/component_module.rb +829 -0
- data/lib/commands/thor/component_template.rb +153 -0
- data/lib/commands/thor/dependency.rb +18 -0
- data/lib/commands/thor/developer.rb +105 -0
- data/lib/commands/thor/dtk.rb +117 -0
- data/lib/commands/thor/library.rb +107 -0
- data/lib/commands/thor/node.rb +411 -0
- data/lib/commands/thor/node_group.rb +211 -0
- data/lib/commands/thor/node_template.rb +88 -0
- data/lib/commands/thor/project.rb +17 -0
- data/lib/commands/thor/provider.rb +155 -0
- data/lib/commands/thor/repo.rb +35 -0
- data/lib/commands/thor/service.rb +656 -0
- data/lib/commands/thor/service_module.rb +806 -0
- data/lib/commands/thor/state_change.rb +10 -0
- data/lib/commands/thor/target.rb +94 -0
- data/lib/commands/thor/task.rb +100 -0
- data/lib/commands/thor/utils.rb +4 -0
- data/lib/commands/thor/workspace.rb +437 -0
- data/lib/commands.rb +40 -0
- data/lib/config/cacert.pem +3785 -0
- data/lib/config/client.conf.header +18 -0
- data/lib/config/configuration.rb +82 -0
- data/lib/config/default.conf +14 -0
- data/lib/config/disk_cacher.rb +60 -0
- data/lib/configurator.rb +92 -0
- data/lib/context_router.rb +23 -0
- data/lib/core.rb +460 -0
- data/lib/domain/git_adapter.rb +221 -0
- data/lib/domain/response.rb +234 -0
- data/lib/dtk-client/version.rb +3 -0
- data/lib/dtk_constants.rb +23 -0
- data/lib/dtk_logger.rb +96 -0
- data/lib/error.rb +74 -0
- data/lib/git-logs/git.log +0 -0
- data/lib/parser/adapters/option_parser.rb +53 -0
- data/lib/parser/adapters/thor/common_option_defs.rb +12 -0
- data/lib/parser/adapters/thor.rb +509 -0
- data/lib/require_first.rb +87 -0
- data/lib/search_hash.rb +27 -0
- data/lib/shell/context.rb +975 -0
- data/lib/shell/context_aux.rb +29 -0
- data/lib/shell/domain.rb +447 -0
- data/lib/shell/header_shell.rb +27 -0
- data/lib/shell/help_monkey_patch.rb +221 -0
- data/lib/shell/interactive_wizard.rb +233 -0
- data/lib/shell/parse_monkey_patch.rb +22 -0
- data/lib/shell/status_monitor.rb +105 -0
- data/lib/shell.rb +219 -0
- data/lib/util/console.rb +143 -0
- data/lib/util/dtk_puppet.rb +46 -0
- data/lib/util/os_util.rb +265 -0
- data/lib/view_processor/augmented_simple_list.rb +27 -0
- data/lib/view_processor/hash_pretty_print.rb +106 -0
- data/lib/view_processor/simple_list.rb +139 -0
- data/lib/view_processor/table_print.rb +277 -0
- data/lib/view_processor.rb +112 -0
- data/puppet/manifests/init.pp +72 -0
- data/puppet/manifests/params.pp +16 -0
- data/puppet/r8meta.puppet.yml +18 -0
- data/puppet/templates/bash_profile.erb +2 -0
- data/puppet/templates/client.conf.erb +1 -0
- data/puppet/templates/dtkclient.erb +2 -0
- data/spec/assembly_spec.rb +50 -0
- data/spec/assembly_template_spec.rb +51 -0
- data/spec/component_template_spec.rb +40 -0
- data/spec/dependency_spec.rb +6 -0
- data/spec/dtk_shell_spec.rb +13 -0
- data/spec/dtk_spec.rb +33 -0
- data/spec/lib/spec_helper.rb +10 -0
- data/spec/lib/spec_thor.rb +105 -0
- data/spec/module_spec.rb +35 -0
- data/spec/node_spec.rb +43 -0
- data/spec/node_template_spec.rb +25 -0
- data/spec/project_spec.rb +6 -0
- data/spec/repo_spec.rb +7 -0
- data/spec/response_spec.rb +52 -0
- data/spec/service_spec.rb +41 -0
- data/spec/state_change_spec.rb +7 -0
- data/spec/table_print_spec.rb +48 -0
- data/spec/target_spec.rb +57 -0
- data/spec/task_spec.rb +28 -0
- data/views/assembly/augmented_simple_list.rb +12 -0
- data/views/assembly_template/augmented_simple_list.rb +12 -0
- data/views/list_task/augmented_simple_list.rb +12 -0
- metadata +351 -0
data/lib/core.rb
ADDED
@@ -0,0 +1,460 @@
|
|
1
|
+
#TODO: user common utils in DTK::Common::Rest
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'singleton'
|
5
|
+
require 'restclient'
|
6
|
+
require 'colorize'
|
7
|
+
require 'json'
|
8
|
+
require 'pp'
|
9
|
+
#TODO: for testing; fix by pass in commadn line argument
|
10
|
+
#RestClient.log = STDOUT
|
11
|
+
|
12
|
+
dtk_require_from_base('domain/response')
|
13
|
+
dtk_require_from_base('util/os_util')
|
14
|
+
dtk_require("config/configuration")
|
15
|
+
|
16
|
+
def top_level_execute(entity_name, method_name, context_params=nil, options_args=nil, shell_execute=false)
|
17
|
+
begin
|
18
|
+
top_level_execute_core(entity_name, method_name, context_params, options_args, shell_execute)
|
19
|
+
rescue DTK::Client::DtkLoginRequiredError
|
20
|
+
# re-logging user and repeating request
|
21
|
+
DTK::Client::OsUtil.print("Session expired: re-establishing session & repeating given task", :yellow)
|
22
|
+
DTK::Client::Session.re_initialize()
|
23
|
+
top_level_execute_core(entity_name, method_name, context_params, options_args, shell_execute)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def top_level_execute_core(entity_name, method_name, context_params=nil, options_args=nil, shell_execute=false)
|
28
|
+
extend DTK::Client::OsUtil
|
29
|
+
|
30
|
+
entity_class = nil
|
31
|
+
|
32
|
+
begin
|
33
|
+
include DTK::Client::Auxiliary
|
34
|
+
|
35
|
+
entity_name = entity_name.gsub("-","_")
|
36
|
+
load_command(entity_name)
|
37
|
+
conn = DTK::Client::Session.get_connection()
|
38
|
+
|
39
|
+
# if connection parameters are not set up properly then don't execute any command
|
40
|
+
return if validate_connection(conn)
|
41
|
+
|
42
|
+
# call proper thor class and task
|
43
|
+
entity_class = DTK::Client.const_get "#{cap_form(entity_name)}"
|
44
|
+
|
45
|
+
# call forwarding, in case there is no task for given entity we switch to last (n-context) and try than
|
46
|
+
unless (entity_class.task_names.include?(method_name))
|
47
|
+
entity_class = DTK::Client.const_get "#{cap_form(context_params.last_entity_name.to_s)}"
|
48
|
+
end
|
49
|
+
|
50
|
+
response_ruby_obj = entity_class.execute_from_cli(conn,method_name,context_params,options_args,shell_execute)
|
51
|
+
|
52
|
+
# this will raise error if found
|
53
|
+
DTK::Client::ResponseErrorHandler.check(response_ruby_obj)
|
54
|
+
|
55
|
+
# this will find appropriate render adapter and give output, returns boolean
|
56
|
+
if print = response_ruby_obj.render_data()
|
57
|
+
print = [print] unless print.kind_of?(Array)
|
58
|
+
print.each do |el|
|
59
|
+
|
60
|
+
if el.kind_of?(String)
|
61
|
+
el.each_line{|l| STDOUT << l}
|
62
|
+
else
|
63
|
+
PP.pp(el,STDOUT)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
rescue DTK::Client::DtkLoginRequiredError => e
|
68
|
+
# this error is handled in method above
|
69
|
+
raise e
|
70
|
+
rescue DTK::Client::DSLParsing => e
|
71
|
+
DTK::Client::OsUtil.print(e.message, :red)
|
72
|
+
rescue DTK::Client::DtkValidationError => e
|
73
|
+
validation_message = e.message
|
74
|
+
|
75
|
+
# if !e.skip_usage_info && entity_class && method_name
|
76
|
+
# usage_info = entity_class.get_usage_info(entity_name, method_name)
|
77
|
+
# validation_message += ", usage: #{usage_info}"
|
78
|
+
# end
|
79
|
+
|
80
|
+
if e.display_usage_info && entity_class && method_name
|
81
|
+
usage_info = entity_class.get_usage_info(entity_name, method_name)
|
82
|
+
validation_message += ", usage: #{usage_info}"
|
83
|
+
|
84
|
+
validation_message.gsub!("^^", '') if validation_message.include?("^^")
|
85
|
+
end
|
86
|
+
|
87
|
+
DTK::Client::OsUtil.print(validation_message, :yellow)
|
88
|
+
rescue DTK::Client::DtkError => e
|
89
|
+
# this are expected application errors
|
90
|
+
DtkLogger.instance.error_pp(e.message, e.backtrace)
|
91
|
+
rescue Exception => e
|
92
|
+
DtkLogger.instance.fatal_pp("[INTERNAL ERROR] DTK has encountered an error #{e.class}: #{e.message}", e.backtrace)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def load_command(command_name)
|
97
|
+
parser_adapter = DTK::Client::Config[:cli_parser] || "thor"
|
98
|
+
|
99
|
+
dtk_nested_require("parser/adapters",parser_adapter)
|
100
|
+
dtk_nested_require("commands/#{parser_adapter}",command_name)
|
101
|
+
end
|
102
|
+
|
103
|
+
# check if connection is set up properly
|
104
|
+
def validate_connection(connection)
|
105
|
+
if connection.connection_error?
|
106
|
+
connection.print_warning
|
107
|
+
puts "\nDTK will now exit. Please set up your connection properly and try again."
|
108
|
+
return true
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# check if .add_direct_access file exists, if not then add direct access and create .add_direct_access file
|
113
|
+
def check_direct_access(params)
|
114
|
+
return if params[:file_exists]
|
115
|
+
|
116
|
+
puts "Adding direct access for current user..."
|
117
|
+
# response = DTK::Client::Account.add_access(params[:ssh_key_path])
|
118
|
+
response = DTK::Client::Account.add_key(params[:ssh_key_path])
|
119
|
+
|
120
|
+
if response.nil? || !response.ok?
|
121
|
+
DTK::Client::OsUtil.print("We were not able to add direct access for current user. In order to properly use dtk-shell you will have to add access manually ('dtk account add-ssh-key').\n", :yellow)
|
122
|
+
return
|
123
|
+
end
|
124
|
+
|
125
|
+
FileUtils.touch(params[:file_path])
|
126
|
+
end
|
127
|
+
|
128
|
+
module DTK
|
129
|
+
module Client
|
130
|
+
class ResponseErrorHandler
|
131
|
+
class << self
|
132
|
+
|
133
|
+
def check_for_session_expiried(response_ruby_obj)
|
134
|
+
error_code = nil
|
135
|
+
if response_ruby_obj && response_ruby_obj['errors']
|
136
|
+
response_ruby_obj['errors'].each do |err|
|
137
|
+
error_code = err["code"]||(err["errors"] && err["errors"].first["code"])
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
return (error_code == "forbidden")
|
142
|
+
end
|
143
|
+
|
144
|
+
def check(response_ruby_obj)
|
145
|
+
# check for errors in response
|
146
|
+
|
147
|
+
unless response_ruby_obj["errors"].nil?
|
148
|
+
error_msg = ""
|
149
|
+
error_internal = nil
|
150
|
+
error_backtrace = nil
|
151
|
+
error_code = nil
|
152
|
+
error_on_server = nil
|
153
|
+
|
154
|
+
#TODO: below just 'captures' first error
|
155
|
+
response_ruby_obj['errors'].each do |err|
|
156
|
+
error_msg += err["message"] unless err["message"].nil?
|
157
|
+
error_msg += err["error"] unless err["error"].nil?
|
158
|
+
error_on_server = true unless err["on_client"]
|
159
|
+
error_code = err["code"]||(err["errors"] && err["errors"].first["code"])
|
160
|
+
error_internal ||= (err["internal"] or error_code == "not_found") #"not_found" code is at Ramaze level; so error_internal not set
|
161
|
+
error_backtrace ||= err["backtrace"]
|
162
|
+
end
|
163
|
+
|
164
|
+
# normalize it for display
|
165
|
+
error_msg = error_msg.empty? ? 'Internal DTK Client error, please try again' : "#{error_msg}"
|
166
|
+
|
167
|
+
# if error_internal.first == true
|
168
|
+
if error_code == "unauthorized"
|
169
|
+
raise DTK::Client::DtkError, "[UNAUTHORIZED] Your session has been suspended, please log in again."
|
170
|
+
elsif error_code == "session_timeout"
|
171
|
+
raise DTK::Client::DtkError, "[SESSION TIMEOUT] Your session has been suspended, please log in again."
|
172
|
+
elsif error_code == "broken"
|
173
|
+
raise DTK::Client::DtkError, "[BROKEN] Unable to connect to the DTK server at host: #{Config[:server_host]}"
|
174
|
+
elsif error_code == "forbidden"
|
175
|
+
raise DTK::Client::DtkLoginRequiredError, "[FORBIDDEN] Access not granted, please log in again."
|
176
|
+
elsif error_code == "timeout"
|
177
|
+
raise DTK::Client::DtkError, "[TIMEOUT ERROR] Server is taking too long to respond."
|
178
|
+
elsif error_code == "connection_refused"
|
179
|
+
raise DTK::Client::DtkError, "[CONNECTION REFUSED] Connection refused by server."
|
180
|
+
elsif error_code == "resource_not_found"
|
181
|
+
raise DTK::Client::DtkError, "[RESOURCE NOT FOUND] #{error_msg}"
|
182
|
+
elsif error_code == "pg_error"
|
183
|
+
raise DTK::Client::DtkError, "[PG_ERROR] #{error_msg}"
|
184
|
+
elsif error_internal
|
185
|
+
where = (error_on_server ? "SERVER" : "CLIENT")
|
186
|
+
#opts = (error_backtrace ? {:backtrace => error_backtrace} : {})
|
187
|
+
raise DTK::Client::DtkError.new("[#{where} INTERNAL ERROR] #{error_msg}", :backtrace => error_backtrace)
|
188
|
+
else
|
189
|
+
# if usage error occurred, display message to console and display that same message to log
|
190
|
+
raise DTK::Client::DtkError, "[ERROR] #{error_msg}."
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
|
198
|
+
class Log
|
199
|
+
#TODO Stubs
|
200
|
+
def self.info(msg)
|
201
|
+
pp "info: #{msg}"
|
202
|
+
end
|
203
|
+
def self.error(msg)
|
204
|
+
pp "error: #{msg}"
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
module ParseFile
|
209
|
+
|
210
|
+
def parse_key_value_file(file)
|
211
|
+
#adapted from mcollective config
|
212
|
+
ret = Hash.new
|
213
|
+
raise DTK::Client::DtkError,"Config file (#{file}) does not exists" unless File.exists?(file)
|
214
|
+
File.open(file).each do |line|
|
215
|
+
# strip blank spaces, tabs etc off the end of all lines
|
216
|
+
line.gsub!(/\s*$/, "")
|
217
|
+
unless line =~ /^#|^$/
|
218
|
+
if (line =~ /(.+?)\s*=\s*(.+)/)
|
219
|
+
key = $1
|
220
|
+
val = $2
|
221
|
+
ret[key.to_sym] = val
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
ret
|
226
|
+
end
|
227
|
+
end
|
228
|
+
class Config < Hash
|
229
|
+
include Singleton
|
230
|
+
include ParseFile
|
231
|
+
dtk_require_from_base('configurator')
|
232
|
+
|
233
|
+
CONFIG_FILE = ::DTK::Client::Configurator.CONFIG_FILE
|
234
|
+
CRED_FILE = ::DTK::Client::Configurator.CRED_FILE
|
235
|
+
|
236
|
+
REQUIRED_KEYS = [:server_host]
|
237
|
+
|
238
|
+
def self.[](k)
|
239
|
+
Config.instance[k]
|
240
|
+
end
|
241
|
+
private
|
242
|
+
def initialize()
|
243
|
+
set_defaults()
|
244
|
+
load_config_file()
|
245
|
+
validate()
|
246
|
+
end
|
247
|
+
def set_defaults()
|
248
|
+
self[:server_port] = 80
|
249
|
+
self[:assembly_module_base_location] = 'assemblies'
|
250
|
+
self[:secure_connection] = true
|
251
|
+
self[:secure_connection_server_port] = 443
|
252
|
+
end
|
253
|
+
|
254
|
+
def load_config_file()
|
255
|
+
parse_key_value_file(CONFIG_FILE).each{|k,v|self[k]=v}
|
256
|
+
end
|
257
|
+
|
258
|
+
def validate
|
259
|
+
#TODO: need to check for legal values
|
260
|
+
missing_keys = REQUIRED_KEYS - keys
|
261
|
+
raise DTK::Client::DtkError,"Missing config keys (#{missing_keys.join(",")}). Please check your configuration file #{CONFIG_FILE} for required keys!" unless missing_keys.empty?
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
|
266
|
+
##
|
267
|
+
# Session Singleton we will use to hold connection instance, just a singleton wrapper.
|
268
|
+
# During shell input it will be needed only once, so singleton was obvious solution.
|
269
|
+
#
|
270
|
+
class Session
|
271
|
+
include Singleton
|
272
|
+
|
273
|
+
attr_accessor :conn
|
274
|
+
|
275
|
+
def initialize()
|
276
|
+
@conn = DTK::Client::Conn.new()
|
277
|
+
end
|
278
|
+
|
279
|
+
def self.get_connection()
|
280
|
+
Session.instance.conn
|
281
|
+
end
|
282
|
+
|
283
|
+
def self.re_initialize()
|
284
|
+
Session.instance.conn = nil
|
285
|
+
Session.instance.conn = DTK::Client::Conn.new()
|
286
|
+
Session.instance.conn.cookies
|
287
|
+
end
|
288
|
+
|
289
|
+
def self.logout()
|
290
|
+
# from this point @conn is not valid, since there are no cookies set
|
291
|
+
Session.instance.conn.logout()
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
class Conn
|
296
|
+
def initialize()
|
297
|
+
@cookies = Hash.new
|
298
|
+
@connection_error = nil
|
299
|
+
login()
|
300
|
+
end
|
301
|
+
|
302
|
+
VERBOSE_MODE_ON = ::DTK::Configuration.get(:verbose_rest_calls)
|
303
|
+
|
304
|
+
attr_reader :connection_error, :cookies
|
305
|
+
|
306
|
+
if VERBOSE_MODE_ON
|
307
|
+
require 'ap'
|
308
|
+
end
|
309
|
+
|
310
|
+
def self.get_timeout()
|
311
|
+
DefaultRestOpts[:timeout]
|
312
|
+
end
|
313
|
+
|
314
|
+
def self.set_timeout(timeout_sec)
|
315
|
+
DefaultRestOpts[:timeout] = timeout_sec
|
316
|
+
end
|
317
|
+
|
318
|
+
|
319
|
+
def rest_url(route=nil)
|
320
|
+
protocol, port = "http", Config[:server_port].to_s
|
321
|
+
protocol, port = "https", Config[:secure_connection_server_port].to_s if Config[:secure_connection] == "true"
|
322
|
+
|
323
|
+
"#{protocol}://#{Config[:server_host]}:#{port}/rest/#{route}"
|
324
|
+
end
|
325
|
+
|
326
|
+
def get(command_class,url)
|
327
|
+
ap "GET #{url}" if VERBOSE_MODE_ON
|
328
|
+
|
329
|
+
check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(get_raw(url)) })
|
330
|
+
end
|
331
|
+
|
332
|
+
def post(command_class,url,body=nil)
|
333
|
+
if VERBOSE_MODE_ON
|
334
|
+
ap "POST (REST) #{url}"
|
335
|
+
ap "params: "
|
336
|
+
ap body
|
337
|
+
end
|
338
|
+
|
339
|
+
check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(post_raw(url,body)) })
|
340
|
+
end
|
341
|
+
|
342
|
+
def post_file(command_class,url,body=nil)
|
343
|
+
if VERBOSE_MODE_ON
|
344
|
+
ap "POST (FILE) #{url}"
|
345
|
+
ap "params: "
|
346
|
+
ap body
|
347
|
+
end
|
348
|
+
|
349
|
+
check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(post_raw(url,body,{:content_type => 'avro/binary'})) })
|
350
|
+
end
|
351
|
+
|
352
|
+
# method will repeat request in case session has expired
|
353
|
+
def check_and_wrap_response(command_class, rest_method_func)
|
354
|
+
response = rest_method_func.call
|
355
|
+
|
356
|
+
if ResponseErrorHandler.check_for_session_expiried(response)
|
357
|
+
# re-logging user and repeating request
|
358
|
+
DTK::Client::OsUtil.print("Session expired: re-establishing session & re-trying request ...", :yellow)
|
359
|
+
@cookies = DTK::Client::Session.re_initialize()
|
360
|
+
response = rest_method_func.call
|
361
|
+
end
|
362
|
+
|
363
|
+
Response.new(command_class, response)
|
364
|
+
end
|
365
|
+
|
366
|
+
|
367
|
+
|
368
|
+
def connection_error?
|
369
|
+
return !@connection_error.nil?
|
370
|
+
end
|
371
|
+
|
372
|
+
def logout()
|
373
|
+
response = get_raw rest_url("user/process_logout")
|
374
|
+
|
375
|
+
# save cookies - no need to persist them
|
376
|
+
# DiskCacher.new.save_cookie(@cookies)
|
377
|
+
|
378
|
+
raise DTK::Client::DtkError, "Failed to logout, and terminate session!" unless response
|
379
|
+
@cookies = nil
|
380
|
+
end
|
381
|
+
|
382
|
+
##
|
383
|
+
# Method will warn user that connection could not be established. User should check configuration
|
384
|
+
# to make sure that connection is properly set.
|
385
|
+
#
|
386
|
+
def print_warning
|
387
|
+
puts "[WARNING] Unable to connect to server, please check you configuration."
|
388
|
+
puts "========================== Configuration =========================="
|
389
|
+
printf "%15s %s\n", "REST endpoint:", rest_url
|
390
|
+
creds = get_credentials
|
391
|
+
printf "%15s %s\n", "Username:", "#{creds[:username]}"
|
392
|
+
printf "%15s %s\n", "Password:", "#{creds[:password] ? creds[:password].gsub(/./,'*') : 'No password set'}"
|
393
|
+
puts "==================================================================="
|
394
|
+
error_code = self.connection_error['errors'].first['errors'].first['code']
|
395
|
+
print " Error code: "
|
396
|
+
DTK::Client::OsUtil.print(error_code, :red)
|
397
|
+
|
398
|
+
end
|
399
|
+
|
400
|
+
private
|
401
|
+
|
402
|
+
include ParseFile
|
403
|
+
|
404
|
+
def login()
|
405
|
+
creds = get_credentials()
|
406
|
+
response = post_raw rest_url("user/process_login"),creds
|
407
|
+
errors = response['errors']
|
408
|
+
|
409
|
+
if response.kind_of?(Common::Response) and not response.ok?
|
410
|
+
puts errors.first['code']
|
411
|
+
if (errors && errors.first['code']=="pg_error")
|
412
|
+
DTK::Client::OsUtil.print(errors.first['message'].gsub!("403 Forbidden", "[PG_ERROR]"), :red)
|
413
|
+
exit
|
414
|
+
end
|
415
|
+
@connection_error = response
|
416
|
+
else
|
417
|
+
@cookies = response.cookies
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
def get_credentials()
|
422
|
+
cred_file = Config::CRED_FILE
|
423
|
+
raise DTK::Client::DtkError,"Authorization configuration file (#{cred_file}) does not exist" unless File.exists?(cred_file)
|
424
|
+
ret = parse_key_value_file(cred_file)
|
425
|
+
[:username,:password].each{|k|raise DTK::Client::DtkError,"cannot find #{k}" unless ret[k]}
|
426
|
+
ret
|
427
|
+
end
|
428
|
+
|
429
|
+
####
|
430
|
+
RestClientWrapper = Common::Response::RestClientWrapper
|
431
|
+
|
432
|
+
# In development mode we want bigger timeout allowing us to debbug on server while still
|
433
|
+
# keeping connection alive and receivinga response
|
434
|
+
if ::DTK::Configuration.get(:development_mode)
|
435
|
+
DefaultRestOpts = {:timeout => 2000, :open_timeout => 2, :error_response_class => Client::Response::Error}
|
436
|
+
# DefaultRestOpts = {:timeout => 50, :open_timeout => 2, :error_response_class => Client::Response::Error}
|
437
|
+
else
|
438
|
+
DefaultRestOpts = {:timeout => 100, :open_timeout => 1, :error_response_class => Client::Response::Error}
|
439
|
+
end
|
440
|
+
|
441
|
+
# enable SSL verification
|
442
|
+
DefaultRestOpts.merge!(:verify_ssl => OpenSSL::SSL::VERIFY_PEER)
|
443
|
+
# Net:HTTP from Ruby 1.8.7 doesn't verify SSL certs correctly
|
444
|
+
# this is a CA bundle downloaded from http://curl.haxx.se/docs/caextract.html,
|
445
|
+
# and it will only be used for 1.8.7, otherwise the default (system) CA will be used
|
446
|
+
DefaultRestOpts.merge!(:ssl_ca_file => File.expand_path('../lib/config/cacert.pem', File.dirname(__FILE__)))
|
447
|
+
|
448
|
+
def get_raw(url)
|
449
|
+
RestClientWrapper.get_raw(url, {}, DefaultRestOpts.merge(:cookies => @cookies))
|
450
|
+
end
|
451
|
+
def post_raw(url,body,params={})
|
452
|
+
RestClientWrapper.post_raw(url, body, DefaultRestOpts.merge(:cookies => @cookies).merge(params))
|
453
|
+
end
|
454
|
+
|
455
|
+
def json_parse_if_needed(item)
|
456
|
+
RestClientWrapper.json_parse_if_needed(item)
|
457
|
+
end
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
require 'git'
|
2
|
+
|
3
|
+
module DTK
|
4
|
+
module Client
|
5
|
+
class GitAdapter
|
6
|
+
attr_accessor :git_repo
|
7
|
+
|
8
|
+
def initialize(repo_dir, branch = nil, opts = {})
|
9
|
+
@git_repo = Git.init(repo_dir)
|
10
|
+
@git_repo.branch(branch) if branch
|
11
|
+
end
|
12
|
+
|
13
|
+
def changed?
|
14
|
+
(!(@git_repo.status.changed.empty? && @git_repo.status.untracked.empty? && @git_repo.status.deleted.empty?) || staged_commits?)
|
15
|
+
end
|
16
|
+
|
17
|
+
def stage_changes()
|
18
|
+
@git_repo.add(@git_repo.status.untracked().keys)
|
19
|
+
@git_repo.add(@git_repo.status.changed().keys)
|
20
|
+
@git_repo.status.deleted().each do |file, status|
|
21
|
+
# this indicates that change has not been staged
|
22
|
+
if status.stage
|
23
|
+
@git_repo.remove(file)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def print_status()
|
29
|
+
changes = [@git_repo.status.changed().keys, @git_repo.status.untracked().keys, @git_repo.status.deleted().keys]
|
30
|
+
puts "\nModified files:\n".colorize(:green) unless changes[0].empty?
|
31
|
+
changes[0].each { |item| puts "\t#{item}" }
|
32
|
+
puts "\nAdded files:\n".colorize(:yellow) unless changes[1].empty?
|
33
|
+
changes[1].each { |item| puts "\t#{item}" }
|
34
|
+
puts "\nDeleted files:\n".colorize(:red) unless changes[2].empty?
|
35
|
+
changes[2].each { |item| puts "\t#{item}" }
|
36
|
+
puts ""
|
37
|
+
end
|
38
|
+
|
39
|
+
def diff_summary(local_branch, remote_reference)
|
40
|
+
branch_local_obj = @git_repo.branches.local.find { |b| b.name == local_branch }
|
41
|
+
branch_remote_obj = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == remote_reference }
|
42
|
+
|
43
|
+
if branch_local_obj && branch_remote_obj
|
44
|
+
|
45
|
+
difference = @git_repo.diff(branch_local_obj, branch_remote_obj)
|
46
|
+
|
47
|
+
files_modified = difference.stats[:files] ? difference.stats[:files].keys.collect { |file| { :path => file }} : []
|
48
|
+
{
|
49
|
+
:files_modified => files_modified
|
50
|
+
}
|
51
|
+
else
|
52
|
+
raise Error.new("Error finding branches: local branch '#{local_branch}' (found: #{!branch_local_obj.nil?}), remote branch '#{remote_reference}' (found: #{!branch_remote_obj.nil?})")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def local_summary()
|
57
|
+
{
|
58
|
+
:files_added => (@git_repo.status.untracked().keys + @git_repo.status.added().keys).collect { |file| { :path => file }},
|
59
|
+
:files_modified => @git_repo.status.changed().keys.collect { |file| { :path => file }},
|
60
|
+
:files_deleted => @git_repo.status.deleted().keys.collect { |file| { :path => file }}
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def new_version()
|
65
|
+
return local_summary()
|
66
|
+
end
|
67
|
+
|
68
|
+
def commit(commit_msg = "")
|
69
|
+
@git_repo.commit(commit_msg)
|
70
|
+
end
|
71
|
+
|
72
|
+
def add_remote(name, url)
|
73
|
+
unless is_there_remote?(name)
|
74
|
+
@git_repo.add_remote(name, url)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def fetch(remote = 'origin')
|
79
|
+
@git_repo.fetch(remote)
|
80
|
+
end
|
81
|
+
|
82
|
+
def rev_list(commit_sha)
|
83
|
+
git_command('rev-list', commit_sha)
|
84
|
+
end
|
85
|
+
|
86
|
+
def staged_commits?()
|
87
|
+
response = git_command('diff','--cached')
|
88
|
+
!response.empty?
|
89
|
+
end
|
90
|
+
|
91
|
+
def rev_list_contains?(container_sha, index_sha)
|
92
|
+
results = rev_list(container_sha)
|
93
|
+
!results.split("\n").grep(index_sha).empty?
|
94
|
+
end
|
95
|
+
|
96
|
+
def head_commit_sha()
|
97
|
+
current_branch.gcommit.sha
|
98
|
+
end
|
99
|
+
|
100
|
+
def find_remote_sha(ref)
|
101
|
+
remote = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == ref}
|
102
|
+
remote.gcommit.sha
|
103
|
+
end
|
104
|
+
|
105
|
+
def merge_relationship(type, ref, opts={})
|
106
|
+
ref_remote, ref_branch = ref.split('/')
|
107
|
+
# fetch remote branch
|
108
|
+
fetch(ref_remote) if opts[:fetch_if_needed]
|
109
|
+
|
110
|
+
|
111
|
+
git_reference = case type
|
112
|
+
when :remote_branch
|
113
|
+
@git_repo.branches.remote.find { |r| "#{r.remote}/#{r.name}" == ref }
|
114
|
+
when :local_branch
|
115
|
+
# DEBUG SNIPPET >>>> REMOVE <<<<
|
116
|
+
# TODO: HARIS LOOK INTO THIS
|
117
|
+
# raise "Invalid ref #{ref}"
|
118
|
+
@git_repo.branches.find { |b| b.name == ref }
|
119
|
+
else
|
120
|
+
raise Error.new("Illegal type parameter (#{type}) passed to merge_relationship")
|
121
|
+
end
|
122
|
+
|
123
|
+
local_sha = current_branch.gcommit.sha
|
124
|
+
|
125
|
+
opts[:ret_commit_shas][:local_sha] = local_sha if opts[:ret_commit_shas]
|
126
|
+
|
127
|
+
unless git_reference
|
128
|
+
return :no_remote_ref if type.eql?(:remote_branch)
|
129
|
+
|
130
|
+
raise Error.new("Cannot find git ref '#{ref}'")
|
131
|
+
end
|
132
|
+
|
133
|
+
git_reference_sha = git_reference.gcommit.sha
|
134
|
+
opts[:ret_commit_shas][:other_sha] = git_reference_sha if opts[:ret_commit_shas]
|
135
|
+
|
136
|
+
# shas can be different but content the same
|
137
|
+
if git_reference_sha.eql?(local_sha) || !any_differences?(local_sha, git_reference_sha)
|
138
|
+
:equal
|
139
|
+
else
|
140
|
+
if rev_list_contains?(local_sha, git_reference_sha)
|
141
|
+
:local_ahead
|
142
|
+
elsif rev_list_contains?(git_reference_sha, local_sha)
|
143
|
+
:local_behind
|
144
|
+
else
|
145
|
+
:branchpoint
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def push(remote_branch_ref)
|
151
|
+
remote, remote_branch = remote_branch_ref.split('/')
|
152
|
+
push_with_remote(remote, remote_branch)
|
153
|
+
end
|
154
|
+
|
155
|
+
def push_with_remote(remote, remote_branch)
|
156
|
+
branch_for_push = "#{current_branch_name}:refs/heads/#{remote_branch||current_branch_name}"
|
157
|
+
@git_repo.push(remote, branch_for_push)
|
158
|
+
end
|
159
|
+
|
160
|
+
def add_file(file_rel_path, content)
|
161
|
+
content ||= String.new
|
162
|
+
file_path = "#{@git_repo.dir}/#{file_rel_path}"
|
163
|
+
File.open(file_path,"w"){|f|f << content}
|
164
|
+
@git_repo.add(file_path)
|
165
|
+
end
|
166
|
+
|
167
|
+
def pull_remote_to_local(remote_branch, local_branch, remote='origin')
|
168
|
+
@git_repo.pull(remote,"#{remote_branch}:#{local_branch}")
|
169
|
+
end
|
170
|
+
|
171
|
+
def merge(remote_branch_ref)
|
172
|
+
@git_repo.merge(remote_branch_ref)
|
173
|
+
end
|
174
|
+
|
175
|
+
def self.clone(repo_url, target_path, branch)
|
176
|
+
git_base = Git.clone(repo_url, target_path)
|
177
|
+
git_base.branch(branch).checkout unless branch.nil?
|
178
|
+
git_base
|
179
|
+
end
|
180
|
+
|
181
|
+
def repo_dir
|
182
|
+
@git_repo.dir.path
|
183
|
+
end
|
184
|
+
|
185
|
+
def current_branch_name
|
186
|
+
current_branch.name
|
187
|
+
end
|
188
|
+
|
189
|
+
def current_branch
|
190
|
+
@git_repo.branches.local.find { |b| b.current }
|
191
|
+
end
|
192
|
+
|
193
|
+
private
|
194
|
+
|
195
|
+
def is_there_remote?(remote_name)
|
196
|
+
@git_repo.remotes.find { |r| r.name == remote_name }
|
197
|
+
end
|
198
|
+
|
199
|
+
def any_differences?(sha1, sha2)
|
200
|
+
@git_repo.diff(sha1, sha2).size > 0
|
201
|
+
end
|
202
|
+
|
203
|
+
def git_command(cmd, opts=[])
|
204
|
+
ENV['GIT_DIR'] = "#{@git_repo.dir.path}/.git"
|
205
|
+
ENV['GIT_INDEX_FILE'] = @git_repo.index.path
|
206
|
+
|
207
|
+
path = @git_repo.dir.path
|
208
|
+
|
209
|
+
opts = [opts].flatten.join(' ')
|
210
|
+
|
211
|
+
response = `git #{cmd} #{opts}`.chomp
|
212
|
+
|
213
|
+
ENV.delete('GIT_DIR')
|
214
|
+
ENV.delete('GIT_INDEX_FILE')
|
215
|
+
|
216
|
+
return response
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|