Top Level Namespace

Defined Under Namespace

Classes: Wait

Instance Method Summary collapse

Instance Method Details

#add_circleci_flavor(flavor) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/tasks/ci/common.rb', line 127

def add_circleci_flavor(flavor)
  new_file = "#{ENV['SDK_HOME']}/circle.yml.new"
  File.open(new_file, 'w') do |fo|
    File.foreach("#{ENV['SDK_HOME']}/circle.yml") do |line|
      fo.puts "        - rake ci:run[#{flavor}]" if line =~ /bundle\ exec\ rake\ requirements/
      fo.puts line
    end
  end
  move_file(new_file, "#{ENV['SDK_HOME']}/circle.yml")
end

#add_travis_flavor(flavor, version = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tasks/ci/common.rb', line 111

def add_travis_flavor(flavor, version = nil)
  new_file = "#{ENV['SDK_HOME']}/.travis.yml.new"
  version = 'latest' if version.nil?
  added = false
  File.open(new_file, 'w') do |fo|
    File.foreach("#{ENV['SDK_HOME']}/.travis.yml") do |line|
      if !added && line =~ /# END OF TRAVIS MATRIX|- TRAVIS_FLAVOR=#{flavor}/
        fo.puts "    - TRAVIS_FLAVOR=#{flavor} FLAVOR_VERSION=#{version}"
        added = true
      end
      fo.puts line
    end
  end
  move_file(new_file, "#{ENV['SDK_HOME']}/.travis.yml")
end

#bin_in_path?(binary) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/tasks/ci/common.rb', line 15

def bin_in_path?(binary)
  ENV['PATH'].split(':').collect { |d| Dir.entries d if Dir.exist? d }.flatten.include? binary
end

#can_skip?Boolean

Returns:

  • (Boolean)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/tasks/ci/common.rb', line 259

def can_skip?
  return false, [] unless travis_pr?

  modified_checks = []
  puts "Comparing #{ENV['TRAVIS_PULL_REQUEST_SHA']} with #{ENV['TRAVIS_BRANCH']}"
  git_output = `git diff --name-only #{ENV['TRAVIS_BRANCH']}...#{ENV['TRAVIS_PULL_REQUEST_SHA']}`
  puts "Git diff: \n#{git_output}"
  git_output.each_line do |filename|
    filename.strip!
    puts filename
    return false, [] if filename.split('/').length < 2

    check_name = filename.split('/')[0]
    modified_checks << check_name unless modified_checks.include? check_name
  end
  [true, modified_checks]
end

#check_envObject



11
12
13
# File 'lib/tasks/ci/common.rb', line 11

def check_env
  abort 'SDK_HOME env variable must be defined in your Rakefile to used this gem.' unless ENV['SDK_HOME']
end

#check_travis_flavor(flavor, version = nil) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/tasks/ci/common.rb', line 103

def check_travis_flavor(flavor, version = nil)
  version = 'latest' if version.nil?
  File.foreach("#{ENV['SDK_HOME']}/.travis.yml") do |line|
    return false if line =~ /- TRAVIS_FLAVOR=#{flavor} FLAVOR_VERSION=#{version}/
  end
  true
end

#copy_skeleton(source, dst, integration) ⇒ Object



138
139
140
141
# File 'lib/tasks/ci/common.rb', line 138

def copy_skeleton(source, dst, integration)
  gem_home = Bundler.rubygems.find_name('datadog-sdk-testing').first.full_gem_path
  sh "cp #{gem_home}/#{source} #{ENV['SDK_HOME']}/#{integration}/#{dst}"
end

#create_integration_path(integration) ⇒ Object



143
144
145
# File 'lib/tasks/ci/common.rb', line 143

def create_integration_path(integration)
  sh "mkdir -p #{ENV['SDK_HOME']}/#{integration}/ci"
end

#create_skeleton(integration) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/tasks/ci/common.rb', line 175

def create_skeleton(integration)
  if File.directory?("#{ENV['SDK_HOME']}/#{integration}")
    puts "directory already exists for #{integration} - bailing out."
    return
  end

  puts "generating skeleton files for #{integration}"
  create_integration_path(integration.to_s)
  generate_skeleton(integration.to_s)
  rename_skeleton(integration.to_s)

  replace_guid(integration.to_s)

  add_travis_flavor(integration)
  add_circleci_flavor(integration)
end

#generate_skeleton(integration) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/tasks/ci/common.rb', line 163

def generate_skeleton(integration)
  copy_skeleton('lib/config/ci/skeleton.rake', "ci/#{integration}.rake", integration)
  copy_skeleton('lib/config/manifest.json', 'manifest.json', integration)
  copy_skeleton('lib/config/check.py', 'check.py', integration)
  copy_skeleton('lib/config/test_skeleton.py', "test_#{integration}.py", integration)
  copy_skeleton('lib/config/metadata.csv', 'metadata.csv', integration)
  copy_skeleton('lib/config/requirements.txt', 'requirements.txt', integration)
  copy_skeleton('lib/config/README.md', 'README.md', integration)
  copy_skeleton('lib/config/CHANGELOG.md', 'CHANGELOG.md', integration)
  copy_skeleton('lib/config/conf.yaml.example', 'conf.yaml.example', integration)
end

#in_ci_envObject



19
20
21
# File 'lib/tasks/ci/common.rb', line 19

def in_ci_env
  ENV['TRAVIS'] || ENV['CIRCLECI']
end

#in_venvObject



64
65
66
# File 'lib/tasks/ci/common.rb', line 64

def in_venv
  ENV['RUN_VENV'] && ENV['RUN_VENV'] == 'true' ? true : false
end

#install_requirements(req_file, pip_options = nil, output = nil, use_venv = nil) ⇒ Object



68
69
70
71
72
73
# File 'lib/tasks/ci/common.rb', line 68

def install_requirements(req_file, pip_options = nil, output = nil, use_venv = nil)
  pip_command = use_venv ? "#{ENV['SDK_HOME']}/venv/bin/pip" : 'pip'
  redirect_output = output ? "2>&1 >> #{output}" : ''
  pip_options = '' if pip_options.nil?
  sh "#{pip_command} install -r #{req_file} #{pip_options} #{redirect_output}"
end

#integration_tests(root_dir) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tasks/ci/common.rb', line 81

def integration_tests(root_dir)
  sdk_dir = ENV['SDK_HOME'] || root_dir
  integrations = []
  untested = []
  testable = []
  test_files(sdk_dir).each do |check|
    integration_name = /test_((\w|_)+).py$/.match(check)[1]
    integrations.push(integration_name)
    if Dir.exist?(File.join(sdk_dir, integration_name))
      testable.push(check)
    else
      untested.push(check)
    end
  end
  [testable, untested]
end

#move_file(src, dst) ⇒ Object



98
99
100
101
# File 'lib/tasks/ci/common.rb', line 98

def move_file(src, dst)
  File.delete(dst)
  File.rename(src, dst)
end

#rename_skeleton(integration) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/tasks/ci/common.rb', line 147

def rename_skeleton(integration)
  capitalized = integration.capitalize
  Dir.glob("#{ENV['SDK_HOME']}/#{integration}/**/*") do |f|
    if File.file?(f)
      sed(f, 's', 'skeleton', integration.to_s, 'g')
      sed(f, 's', 'Skeleton', capitalized.to_s, 'g')
    end
  end
end

#replace_guid(integration) ⇒ Object



157
158
159
160
161
# File 'lib/tasks/ci/common.rb', line 157

def replace_guid(integration)
  guid = SecureRandom.uuid
  f = "#{ENV['SDK_HOME']}/#{integration}/manifest.json"
  sed(f, 's', 'guid_replaceme', guid.to_s, 'g')
end

#section(name) ⇒ Object



57
58
59
60
61
62
# File 'lib/tasks/ci/common.rb', line 57

def section(name)
  timestamp = Time.now.utc.iso8601
  puts ''
  puts "[#{timestamp}] >>>>>>>>>>>>>> #{name} STAGE".black.on_white
  puts ''
end

#sed(source, op, a, b, mods) ⇒ Object



27
28
29
30
31
# File 'lib/tasks/ci/common.rb', line 27

def sed(source, op, a, b, mods)
  cmd = "#{op}/#{a}"
  cmd = "#{cmd}/#{b}" unless b.nil? || b.empty?
  sh "sed -i '' \"#{cmd}/#{mods}\" #{source} || sed -i \"#{cmd}/#{mods}\" #{source}"
end

#sleep_for(secs) ⇒ Object



33
34
35
36
# File 'lib/tasks/ci/common.rb', line 33

def sleep_for(secs)
  puts "Sleeping for #{secs}s".blue
  sleep(secs)
end

#test_files(sdk_dir) ⇒ Object



75
76
77
78
79
# File 'lib/tasks/ci/common.rb', line 75

def test_files(sdk_dir)
  Dir.glob(File.join(sdk_dir, '**/test_*.py')).reject do |path|
    !%r{#{sdk_dir}/embedded/.*$}.match(path).nil? || !%r{#{sdk_dir}\/venv\/.*$}.match(path).nil?
  end
end

#travis_circle_envObject



23
24
25
# File 'lib/tasks/ci/common.rb', line 23

def travis_circle_env
  abort 'You are not in a Travis/Circle CI environment, this task wont apply.' unless in_ci_env
end

#travis_pr?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/tasks/ci/common.rb', line 255

def travis_pr?
  !ENV['TRAVIS'].nil? && ENV['TRAVIS_EVENT_TYPE'] == 'pull_request'
end

#wait_on_docker_logs(c_name, max_wait, *include_array) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tasks/ci/common.rb', line 38

def wait_on_docker_logs(c_name, max_wait, *include_array)
  count = 0
  logs = `docker logs #{c_name} 2>&1`
  puts "Waiting for #{c_name} to come up"

  until count == max_wait || include_array.any? { |phrase| logs.include?(phrase) }
    sleep(1)
    logs = `docker logs #{c_name} 2>&1`
    count += 1
  end

  if include_array.any? { |phrase| logs.include?(phrase) }
    puts "#{c_name} is up!"
  else
    sh %(docker logs #{c_name} 2>&1)
    raise
  end
end