Class: Fastlane::Helper::DynatraceHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/dynatrace/helper/dynatrace_helper.rb

Class Method Summary collapse

Class Method Details

.get_dss_client(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fastlane/plugin/dynatrace/helper/dynatrace_helper.rb', line 8

def self.get_dss_client(params)
  dynatraceDir = "dynatrace"
  versionFile = "version"
  dtxDssClientBin = "DTXDssClient"
  dtxDssClientPath = "#{dynatraceDir}/#{dtxDssClientBin}"

  if (params.all_keys.include? :dtxDssClientPath and not params[:dtxDssClientPath].nil?)
    UI.message "DEPRECATION WARNING: DTXDssClientPath doesn't need to be specified anymore, the DTXDssClient is downloaded and updated automatically."
    dtxDssClientPath = params[:dtxDssClientPath]
  else
    # get latest version info
    clientUri = URI("#{self.get_server_base_url(params)}/api/config/v1/symfiles/dtxdss-download?Api-Token=#{params[:apitoken]}")
    response = Net::HTTP.get_response(clientUri)

    if not response.kind_of? Net::HTTPSuccess
      base_error = "Couldn't update DTXDssClient (invalid response: #{response.message} (#{response.code})) for URL: #{clientUri})"
      if File.exists?("#{dynatraceDir}/#{dtxDssClientBin}")
        UI.important base_error
        UI.important "Using cached DTXDssClient: #{dynatraceDir}/#{dtxDssClientBin}"
        return dtxDssClientPath
      else
        UI.user_error! base_error
      end
    end

    remoteClientUrl = JSON.parse(response.body)["dssClientUrl"]
    UI.message "Remote DSS client: #{remoteClientUrl}"

    # check local state
    if (!File.directory?(dynatraceDir))
      Dir.mkdir(dynatraceDir) 
    end

    if (!File.exists?("#{dynatraceDir}/#{versionFile}") or
        !File.exists?("#{dynatraceDir}/#{dtxDssClientBin}") or 
        File.read("#{dynatraceDir}/#{versionFile}") != remoteClientUrl)
      # update local state
      UI.message "Found a different remote DTXDssClient client. Updating local version."
      File.delete("#{dynatraceDir}/#{versionFile}") if File.exist?("#{dynatraceDir}/#{versionFile}")
      File.delete("#{dynatraceDir}/#{dtxDssClientBin}") if File.exist?("#{dynatraceDir}/#{dtxDssClientBin}")

      File.write("#{dynatraceDir}/#{versionFile}", remoteClientUrl)

      # get client from served archive
      open(remoteClientUrl) do |zipped|
        Zip::InputStream.open(zipped) do |unzipped|
          entry = unzipped.get_next_entry
          if (entry.name == dtxDssClientBin)
            IO.copy_stream(entry.get_input_stream, "#{dynatraceDir}/#{dtxDssClientBin}")
            FileUtils.chmod("+x", "#{dynatraceDir}/#{dtxDssClientBin}")
          end
        end
      end
    end
  end
  return dtxDssClientPath
end

.get_server_base_url(params) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/fastlane/plugin/dynatrace/helper/dynatrace_helper.rb', line 66

def self.get_server_base_url(params)
  if params[:server][-1] == '/'
    return params[:server][0..-2]
  else
    return params[:server]
  end
end