Class: Cnvrg::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/cnvrg/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_home = nil, slug: nil, owner: nil) ⇒ Project

Returns a new instance of Project.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cnvrg/project.rb', line 12

def initialize(project_home=nil, slug: nil, owner: nil)
  begin
    @local_path = project_home
    @working_dir = project_home
    config = YAML.load_file(project_home + "/.cnvrg/config.yml") rescue {owner: owner, project_slug: slug}
    @title = config[:project_name]
    @slug = config[:project_slug]
    @owner = config[:owner]
    @is_branch = config[:is_branch]
    @is_git = config[:git] || false
    @base_resource = "users/#{@owner}/projects/#{@slug}/"
    @machines = nil
  rescue => e
  end
end

Instance Attribute Details

#is_branchObject (readonly)

Returns the value of attribute is_branch.



6
7
8
# File 'lib/cnvrg/project.rb', line 6

def is_branch
  @is_branch
end

#is_gitObject (readonly)

Returns the value of attribute is_git.



6
7
8
# File 'lib/cnvrg/project.rb', line 6

def is_git
  @is_git
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



6
7
8
# File 'lib/cnvrg/project.rb', line 6

def local_path
  @local_path
end

#machinesObject (readonly)

Returns the value of attribute machines.



6
7
8
# File 'lib/cnvrg/project.rb', line 6

def machines
  @machines
end

#ownerObject (readonly)

Returns the value of attribute owner.



6
7
8
# File 'lib/cnvrg/project.rb', line 6

def owner
  @owner
end

#slugObject (readonly)

Returns the value of attribute slug.



6
7
8
# File 'lib/cnvrg/project.rb', line 6

def slug
  @slug
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/cnvrg/project.rb', line 6

def title
  @title
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



6
7
8
# File 'lib/cnvrg/project.rb', line 6

def working_dir
  @working_dir
end

Class Method Details

.clone_dir(project_slug, project_owner, project_name, is_git = false) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/cnvrg/project.rb', line 221

def self.clone_dir(project_slug, project_owner, project_name, is_git = false)
  list_dirs = [project_name,
               project_name + "/.cnvrg"
  ]


  list_files = [
      project_name + "/.cnvrg/config.yml",
      project_name + "/.cnvrgignore",
  ]
  begin
    config = {project_name: project_name,
              project_slug: project_slug,
              owner: project_owner,
              git: is_git}
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files
    cnvrgignore = Helpers.cnvrgignore_content


    File.open(project_name + "/.cnvrg/config.yml", "w+") {|f| f.write config.to_yaml}
    File.open(project_name + "/.cnvrgignore", "w+") {|f| f.write cnvrgignore}

  rescue
    return false
  end
  return true
end

.clone_dir_remote(project_slug, project_owner, project_name, is_git = false) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/cnvrg/project.rb', line 271

def self.clone_dir_remote(project_slug, project_owner, project_name, is_git = false)
  cli = Cnvrg::CLI.new()
  begin
    list_dirs = [
        ".cnvrg"
    ]


    list_files = [
        ".cnvrg/config.yml",

    ]


    config = {project_name: project_name,
              project_slug: project_slug,
              owner: project_owner,
              git: is_git
    }
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files


    File.open(".cnvrg/config.yml", "w+") {|f| f.write config.to_yaml}
    if !File.exist? ".cnvrgignore"
      FileUtils.touch ".cnvrgignore"
      list_files << ".cnvrgignore"
      cnvrgignore = Helpers.cnvrgignore_content
      File.open(".cnvrgignore", "w+") {|f| f.write cnvrgignore}


    end
    true
  rescue => e
    cli.log_message(e.message)
    cli.log_error(e)
    false
  end
end

.create(project_name, clean, with_docker = false, bucket: nil) ⇒ Object

Create project



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cnvrg/project.rb', line 130

def self.create(project_name, clean, with_docker = false, bucket: nil)
  if clean
    list_dirs = [project_name, project_name + "/.cnvrg"]
  else

    list_dirs = [project_name,
                 project_name + "/models",
                 project_name + "/notebooks",
                 project_name + "/src",
                 project_name + "/src/visualizations",
                 project_name + "/src/features",
                 project_name + "/.cnvrg"
    ]
  end


  list_files = [
      project_name + "/README.md",
      project_name + "/.cnvrgignore",
      project_name + "/.cnvrg/config.yml"
  ]
  cnvrgreadme = Helpers.readme_content
  cnvrgignore = Helpers.cnvrgignore_content
  cnvrghyper = Helpers.hyper_content

  begin

    owner = Cnvrg::CLI.get_owner()
    response = Cnvrg::API.request("cli/create_project", 'POST', {title: project_name, owner: owner, is_docker: with_docker, bucket: bucket})
    Cnvrg::CLI.is_response_success(response)
    response = JSON.parse response["result"]
    project_slug = response["slug"]

    config = {project_name: project_name,
              project_slug: project_slug,
              owner: owner,
              docker: with_docker}
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files

    File.open(project_name + "/.cnvrg/config.yml", "w+") {|f| f.write config.to_yaml}
    File.open(project_name + "/.cnvrgignore", "w+") {|f| f.write cnvrgignore}
    File.open(project_name + "/README.md", "w+") {|f| f.write cnvrgreadme}
    File.open(project_name + "/src/hyper.yaml", "w+") {|f| f.write cnvrghyper}

  rescue
    return false
  end
  return true
end


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/cnvrg/project.rb', line 181

def self.link(owner, project_name, docker = false, git = false, bucket: nil)
  ignore_exits = File.exist? ".cnvrgignore"
  list_dirs = [".cnvrg"
  ]
  list_files = [
      ".cnvrg/config.yml"
  ]
  if !ignore_exits
    list_files <<
        ".cnvrgignore"
  end

  cnvrgreadme = Helpers.readme_content
  cnvrgignore = Helpers.cnvrgignore_content
  begin
    response = Cnvrg::API.request("cli/create_project", 'POST', {title: project_name, owner: owner, is_docker: docker, bucket: bucket})
    Cnvrg::CLI.is_response_success(response)
    response = JSON.parse response["result"]
    project_slug = response["slug"]

    config = {project_name: project_name,
              project_slug: project_slug,
              owner: owner,
              git: git}
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files
    File.open(".cnvrg/config.yml", "w+") {|f| f.write config.to_yaml}
    File.open(".cnvrgignore", "w+") {|f| f.write cnvrgignore} unless ignore_exits
    if !File.exist? "README" and !File.exist? "README.md"
      FileUtils.touch ["README.md"]
      File.open("README.md", "w+") {|f| f.write cnvrgreadme}
    end

  rescue => e
    puts e
    return false
  end
  return true
end

.stop_if_project_present(project_home, project_name, owner) ⇒ Object



832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/cnvrg/project.rb', line 832

def self.stop_if_project_present(project_home, project_name, owner)
  cli = Cnvrg::CLI.new()
  config = YAML.load_file(project_home + "/.cnvrg/config.yml")
  local_commit = YAML.load_file(project_home + "/.cnvrg/idx.yml")[:commit] rescue nil
  return if local_commit.blank?
  if config[:project_name] == project_name && config[:owner] == owner
    cli.log_message("Project already present, clone aborted")
    exit(0)
  end
rescue => e
  nil
end

.verify_cnvrgignore_exist(project_name, remote) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/cnvrg/project.rb', line 250

def self.verify_cnvrgignore_exist(project_name, remote)
  path = ".cnvrgignore"
  if !File.exist? path
    path = "#{project_name}/.cnvrgignore"
  end
  ignore_exits = File.exist? path
  if !ignore_exits
    begin
      list_files = [
          path
      ]
      FileUtils.touch list_files
      cnvrgignore = Helpers.cnvrgignore_content
      File.open(path, "w+") {|f| f.write cnvrgignore}
    rescue => e
      return false
    end

  end
end

Instance Method Details

#base_resourceObject



28
29
30
# File 'lib/cnvrg/project.rb', line 28

def base_resource
  "users/#{@owner}/projects/#{@slug}/"
end

#check_job_pod_restart(job_type: nil, job_id: nil) ⇒ Object



802
803
804
805
806
807
808
809
810
811
812
# File 'lib/cnvrg/project.rb', line 802

def check_job_pod_restart(job_type: nil, job_id: nil)
  job_type ||= ENV['CNVRG_JOB_TYPE']
  job_id ||= ENV['CNVRG_JOB_ID']
  if job_type.blank? or job_id.blank?
    raise StandardError.new("Cant find job env variables")
  end
  resp = Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/jobs/#{job_type.underscore}/#{job_id}/check_pod_restart", "GET", {job_type: job_type, job_id: job_id})
  return [false, false] if resp.blank?
  Cnvrg::Logger.log_info("Checked for pod restart got response #{resp}")
  [resp['project_downloaded'], resp['dataset_downloaded']]
end

#check_machine(machine) ⇒ Object



707
708
709
710
711
712
# File 'lib/cnvrg/project.rb', line 707

def check_machine(machine)
  Cnvrg::Logger.log_info("Check if #{machine} machine exists")
  machines = get_machines
  return true if machines.blank?
  machines.include? machine
end

#clone(remote = 0, commit) ⇒ Object



504
505
506
507
# File 'lib/cnvrg/project.rb', line 504

def clone(remote = 0, commit)
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/clone", 'POST', {project_slug: self.slug, remote: remote, commit: commit})
  return response
end

#compare_commit(commit) ⇒ Object



608
609
610
611
612
613
614
615
616
# File 'lib/cnvrg/project.rb', line 608

def compare_commit(commit)
  if commit.nil? or commit.empty?
    commit = last_local_commit
  end
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/commit/compare", 'POST', {current_commit: commit})
  CLI.is_response_success(response, false)
  update_is_new_branch(response["result"]["new_branch"])
  return response["result"]["new_branch"]
end

#compare_idx(new_branch, force: false, deploy: false, in_exp: false, specific_files: [], download: false) ⇒ Object



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/cnvrg/project.rb', line 522

def compare_idx(new_branch, force:false, deploy: false, in_exp:false, specific_files: [], download: false)
  is_download = download
  if is_download
    local_idx = self.get_idx
  else
    #upload
    local_idx = self.generate_idx(deploy: deploy, files: specific_files)
  end

  commit = local_idx[:commit]
  tree = local_idx[:tree]
  ignore_list = self.send_ignore_list()
  if force or specific_files.present?
    added = []
    if tree.present?
      added += local_idx[:tree].keys
    end
    response = { "result" => { "commit" => nil, "tree" => { "added" => added,
                                                            "updated_on_server" => [],
                                                            "updated_on_local" => [],
                                                            "update_local" => [],
                                                            "deleted" => [],
                                                            "conflicts" => [] } } }
    return response
  end
  #we dont want to send it on download - we only compare between commits sha1 in download.
  if is_download
    #the new server doesnt need the tree, but the old probably needs :X
    local_idx[:tree] = {} if Cnvrg::Helpers.server_version > 0
  end

  response = Cnvrg::API.request(@base_resource + "status", 'POST', {idx: local_idx, new_branch: new_branch,
                                                                    current_commit: commit, ignore: ignore_list, force: force, in_exp: in_exp, download: download})

  CLI.is_response_success(response, true)
  if is_download
    if Cnvrg::Helpers.server_version > 0
      #trying to optimize the download using resolver
      resolve = response['result']['tree']['resolver'] || tree #tree of file -> sha1 from current commit to check conflicts
      destination_files = response['result']['tree']['destination'] || {} #tree of file -> sha1 from last commit to check files that already downloaded
    else
      resolve = tree
      destination_files = {}
    end
    @files = self.get_files
    local_tree = @files.calculate_sha1(resolve.keys)
    changed_files = resolve.keys.select {|file| local_tree[file] != resolve[file]}

    # means that the user changed the file locally
    response['result']['tree']['update_local'] = changed_files

    # means that we already downloaded this file and we dont need it anymore
    downloaded_files = destination_files.keys.select {|file| local_tree[file] == destination_files[file]}
    response['result']['tree']['added'] -= downloaded_files
    response['result']['tree']['updated_on_server'] -= downloaded_files
  end
  Cnvrg::Logger.log_json(response, msg: "Comparing IDX response(tree)")
  return response
end

#deploy(file_to_run, function, input_params, commit_to_run, instance_type, image_slug, scheduling_query, local_timestamp, workers, file_input, title) ⇒ Object



629
630
631
632
633
634
635
636
637
638
639
# File 'lib/cnvrg/project.rb', line 629

def deploy(file_to_run, function, input_params, commit_to_run, instance_type, image_slug, scheduling_query, local_timestamp, workers, file_input, title)
  response = Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/deploy", 'POST', {file_to_run: file_to_run, function: function,
                                                                                     image_slug: image_slug, input_params: input_params,
                                                                                     commit_sha1: commit_to_run,
                                                                                     instance_type: instance_type,
                                                                                     scheduling_query: scheduling_query,
                                                                                     local_timestamp: local_timestamp,
                                                                                     workers: workers, file_input: file_input,
                                                                                     title: title})
  return response
end

#fetch_projectObject



715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/cnvrg/project.rb', line 715

def fetch_project
  resp = Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/get_project", "GET")
  res = JSON.parse(resp['result']) rescue nil
  return if res.blank?
  config = self.get_config
  config[:project_name] = res['title']
  config[:project_slug] = @slug
  config[:owner] = @owner
  config[:git] = res['git'] || false
  config[:is_git] = res['git'] || false
  self.set_config(config)
end

#fetch_webapp_slugs(webapp_slug, slugs: nil) ⇒ Object



662
663
664
665
666
667
668
669
670
671
# File 'lib/cnvrg/project.rb', line 662

def fetch_webapp_slugs(webapp_slug, slugs: nil)
  response = Cnvrg::API_V2.request("#{self.owner}/projects/#{self.slug}/webapps/#{webapp_slug}" , 'GET')

  if response.key?("experiments")
    return response["experiments"]
  end
  return response["data"]["attributes"]["experiments"]
rescue
  slugs
end

#generate_git_diffObject



380
381
382
383
384
385
# File 'lib/cnvrg/project.rb', line 380

def generate_git_diff
  git_diff = `git diff --name-only`
  git_diff.split("\n")
rescue
  []
end

#generate_idx(deploy: false, files: []) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/cnvrg/project.rb', line 432

def generate_idx(deploy: false, files: [])
  if File.exists? "#{self.local_path}/.cnvrg/idx.yml"
    old_idx = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") rescue {:tree => {}, :commit => nil}
  else
    old_idx = {:tree => {}, :commit => nil}
  end

  tree_idx = Hash.new(0)

  ### if file specified, just take them, dont calculate everything from scratch
  list_ignore = self.get_ignore_list()
  if files.blank?
    list = Dir.glob("#{self.local_path}/**/*", File::FNM_DOTMATCH).reject {|x| (x =~ /\/\.{1,2}$/) or (x =~ /^#{self.local_path}\/\.cnvrg\/*/) or (x =~ /^#{self.local_path}\/\.git\/*/) or (x =~ /^#{self.local_path}\/\.cnvrgignore.conflict*/) and not (x =~ /^#{self.local_path}\/\.cnvrgignore/)}
  else
    list = files
  end
  if deploy
    list_ignore += ["main.py", "main.pyc", "__init__.py", "uwsgi.ini"]
    list_ignore.flatten!
  end
  list_ignore_new = list_ignore.map{|x| x.gsub("//","/")} rescue []
  # list.each do |e|
  Parallel.map(list, in_threads: IDXParallelThreads) do |e|
    label = e.sub(self.local_path + "/", "")

    if list_ignore_new.include? label
      next
    end
    if File.symlink?(e)
      Cnvrg::Logger.log_info("Skipping symlink #{e}")
      next
    end
    if File.directory? e
      dir_name = (label.ends_with? "/") ? label : (label + "/")
      tree_idx[dir_name] = nil
    else
      file_in_idx = old_idx[:tree][label] rescue nil
      last_modified = File.mtime(e).to_f
      if file_in_idx.present? and last_modified == file_in_idx[:last_modified]
        sha1 = file_in_idx[:sha1]
      else
        sha1 = OpenSSL::Digest::SHA1.file(e).hexdigest
      end

      if old_idx.nil? or old_idx.to_h[:tree].nil?
        tree_idx[label] = {sha1: sha1, commit_time: nil, last_modified: last_modified}
      elsif file_in_idx.nil? or file_in_idx[:sha1] != sha1 or file_in_idx[:last_modified].blank? or file_in_idx[:last_modified] != last_modified
        tree_idx[label] = {sha1: sha1, commit_time: nil, last_modified: last_modified}
      else
        tree_idx[label] = old_idx[:tree][label]
      end
    end
  end
  old_idx[:tree] = tree_idx
  File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write old_idx.to_yaml}
  return YAML.load_file("#{self.local_path}/.cnvrg/idx.yml")
end

#generate_output_dir(output_dir, local: false) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/cnvrg/project.rb', line 387

def generate_output_dir(output_dir, local: false)
  Cnvrg::Logger.log_info("Generating output dir for #{output_dir}")
  upload_list = []
  list = []
  list = Dir.glob("/cnvrg/#{output_dir}/**/*", File::FNM_DOTMATCH)
  if local
    list += Dir.glob("#{output_dir}/**/*", File::FNM_DOTMATCH)
  end
  list.uniq!
  Parallel.map(list, in_threads: IDXParallelThreads) do |e|
    next if e.end_with? "/."
    if File.directory? e
      upload_list << e + "/"
    else
      upload_list << e
    end
  end
  if Dir.exists? output_dir
    upload_list << output_dir + "/"
  end
  Cnvrg::Logger.log_info("Uploading: #{upload_list.join(", ")}")
  upload_list
end

#generate_output_dir_tmp(output_dir) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/cnvrg/project.rb', line 411

def generate_output_dir_tmp(output_dir)
  upload_list = []
  list = Dir.glob("#{output_dir}/**/*", File::FNM_DOTMATCH)
  Parallel.map(list, in_threads: IDXParallelThreads) do |e|
    next if e.end_with? "/."
    if File.directory? e

      next
    else
      upload_list << e
    end
  end
  # if Dir.exists? output_dir
  #   upload_list << output_dir + "/"
  # end


  return upload_list

end

#get_configObject



326
327
328
# File 'lib/cnvrg/project.rb', line 326

def get_config
  YAML.load_file(@working_dir + "/.cnvrg/config.yml") rescue {}
end

#get_experiment(slug) ⇒ Object



655
656
657
658
659
660
# File 'lib/cnvrg/project.rb', line 655

def get_experiment(slug)
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/experiments/#{slug}", 'GET')
  response['status']=200
  CLI.is_response_success(response)
  return response
end

#get_experimentsObject



648
649
650
651
652
# File 'lib/cnvrg/project.rb', line 648

def get_experiments
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/experiments/list", 'GET')
  CLI.is_response_success(response)
  return response
end

#get_filesObject



582
583
584
# File 'lib/cnvrg/project.rb', line 582

def get_files
  Cnvrg::Files.new(self.owner, self.slug, project_home: @local_path)
end

#get_idxObject



490
491
492
493
494
495
496
497
# File 'lib/cnvrg/project.rb', line 490

def get_idx
  unless File.exists? "#{self.local_path}/.cnvrg/idx.yml"
    empty_idx = {:commit => nil, :tree => {}}
    File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write empty_idx.to_yaml}
    return empty_idx
  end
  YAML.load_file("#{self.local_path}/.cnvrg/idx.yml")
end

#get_ignore_listObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cnvrg/project.rb', line 61

def get_ignore_list
  ignore_list = []
  if !File.exist? self.local_path + "/.cnvrgignore"
    return ignore_list
  end
  File.open(self.local_path + "/.cnvrgignore", "r").each_line do |line|
    line = line.strip
    if line.start_with? "#" or ignore_list.include? line or line.empty?
      next
    end
    if line.end_with? "*"
      list_regex = Dir.glob("**/#{line}", File::FNM_DOTMATCH).flatten
      list_regex.each do |l|
        ignore_list << l
        if File.directory?(l)
          all_sub = Dir.glob("#{line}/**/*", File::FNM_DOTMATCH).flatten

          ignore_list << all_sub.flatten
        end

      end
    elsif line.end_with? "/*"
      line = line.gsub("/*", "")
      regex_list = Dir.glob("**/#{line}/**/*", File::FNM_DOTMATCH).flatten
      ignore_list << regex_list
    elsif line.include? "*"
      regex_list = Dir.glob("**/#{line}").flatten
      ignore_list << regex_list
    elsif line.end_with? "/" or File.directory?(line)
      ignore_list << line
      all_sub = Dir.glob("#{line}/**/*", File::FNM_DOTMATCH).flatten

      ignore_list << all_sub.flatten

    else
      ignore_list << line
    end
  end
  return ignore_list.flatten

end

#get_job_last_commit(job_type, job_id) ⇒ Object



515
516
517
518
519
520
# File 'lib/cnvrg/project.rb', line 515

def get_job_last_commit(job_type, job_id)
  base_url = "users/#{self.owner}/projects/#{self.slug}/jobs/#{job_type.underscore}/#{job_id}"
  resp = Cnvrg::API.request("#{base_url}/last_commit", "GET")
  commit = resp["commit"]
  return commit
end

#get_latest_commitObject



358
359
360
361
362
# File 'lib/cnvrg/project.rb', line 358

def get_latest_commit
  resp = clone(0, '')

  resp['result']['commit']
end

#get_machine_activityObject



782
783
784
785
786
787
788
789
790
791
# File 'lib/cnvrg/project.rb', line 782

def get_machine_activity
  begin
    machine_activity = File.open("#{@local_path}/.cnvrg/machine_activity", "rb").read
    machine_activity = machine_activity.to_s.strip
    ma_id = machine_activity.to_i
    return ma_id
  rescue
    return nil
  end
end

#get_machinesObject



693
694
695
696
697
# File 'lib/cnvrg/project.rb', line 693

def get_machines
  init_machines if @machines.nil?
  @machines = false if @machines.nil?
  @machines || []
end

#get_new_branchObject



317
318
319
320
321
322
323
324
# File 'lib/cnvrg/project.rb', line 317

def get_new_branch
  begin
    config = YAML.load_file(@working_dir + "/.cnvrg/config.yml")
    return config[:new_branch]
  rescue => e
    return false
  end
end

#get_storage_clientObject

Raises:

  • (StandardError)


330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/cnvrg/project.rb', line 330

def get_storage_client
  client_params = nil
  i = 0
  begin
    response = Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/client", 'GET')
    unless Cnvrg::CLI.is_response_success(response, false)
      raise StandardError.new("Can't find project credentials")
    end
    client_params = response['client']
  rescue StandardError
    i += 1
    sleep(5 * i)
    retry if i < 10
    client_params = get_storage_client_fallback
  end
  raise StandardError.new("Can't find project credentials") unless client_params
  Cnvrg::Downloader::Client.factory(client_params)
end

#get_storage_client_fallbackObject

Raises:

  • (StandardError)


349
350
351
352
353
354
355
356
# File 'lib/cnvrg/project.rb', line 349

def get_storage_client_fallback
  response = Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/download_files", "POST", {files: [], commit: get_latest_commit})
  raise StandardError.new("Can't find project credentials") unless Cnvrg::CLI.is_response_success(response, false)
  files = response['result']
  storage = files['is_s3'] ? 's3' : 'minio'
  files['storage'] = storage
  files
end

#git_download_commit(commit) ⇒ Object



509
510
511
512
513
# File 'lib/cnvrg/project.rb', line 509

def git_download_commit(commit)
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/git_download_commit", 'POST', {commit_sha1: commit})
  CLI.is_response_success(response, true)
  return response
end

#init_machinesObject



686
687
688
689
690
691
# File 'lib/cnvrg/project.rb', line 686

def init_machines
  Cnvrg::Logger.log_info("Init machines.")
  resp = Cnvrg::API.request("users/#{@owner}/machines", "GET")
  return unless Cnvrg::CLI.is_response_success(resp, false)
  @machines = resp['result']['machines']
end

#job_commandsObject



742
743
744
745
# File 'lib/cnvrg/project.rb', line 742

def job_commands
  job_type, job_id = ENV['CNVRG_JOB_TYPE'], ENV['CNVRG_JOB_ID']
  resp = Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/jobs/#{job_type.underscore}/#{job_id}/commands", "GET")
end

#job_log(logs, level: 'info', step: nil, job_type: nil, job_id: nil) ⇒ Object



728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/cnvrg/project.rb', line 728

def job_log(logs, level: 'info', step: nil, job_type: nil, job_id: nil)
  job_type ||= ENV['CNVRG_JOB_TYPE']
  job_id ||= ENV['CNVRG_JOB_ID']
  logs = [logs].flatten
  if job_type.blank? or job_id.blank?
    raise StandardError.new("Cant find job env variables")
  end
  logs.each_slice(10).each do |temp_logs|
    Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/jobs/#{job_type.underscore}/#{job_id}/log", "POST", {job_type: job_type, job_id: job_id, logs: temp_logs, log_level: level, step: step, timestamp: Time.now})
    sleep(1)
  end
end

#jump_idx(destination: self.last_local_commit) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/cnvrg/project.rb', line 586

def jump_idx(destination: self.last_local_commit)
  local_idx = self.generate_idx
  ignore_list = self.send_ignore_list
  current_commit = local_idx[:commit]
  tree = local_idx[:tree]
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/jump_to",
                                'POST',
                                {tree: tree, ignore: ignore_list,
                                 dest_commit: destination, current_commit: current_commit})
  CLI.is_response_success(response, false)
  response
end

#last_local_commitObject



33
34
35
# File 'lib/cnvrg/project.rb', line 33

def last_local_commit
  YAML.load_file(@local_path + "/.cnvrg/idx.yml")[:commit] rescue nil
end

#list_commitsObject



641
642
643
644
645
# File 'lib/cnvrg/project.rb', line 641

def list_commits
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/commits/list", 'GET')
  CLI.is_response_success(response)
  return response
end

#pre_job_pod_restartObject



814
815
816
817
818
819
820
821
# File 'lib/cnvrg/project.rb', line 814

def pre_job_pod_restart
  job_type ||= ENV['CNVRG_JOB_TYPE']
  job_id ||= ENV['CNVRG_JOB_ID']
  if job_type.blank? or job_id.blank?
    raise StandardError.new("Cant find job env variables")
  end
  Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/jobs/#{job_type.underscore}/#{job_id}/pre_pod_restart", "POST", {job_type: job_type, job_id: job_id})
end

#remove_new_branchObject



374
375
376
377
378
# File 'lib/cnvrg/project.rb', line 374

def remove_new_branch
  config = YAML.load_file(@working_dir + "/.cnvrg/config.yml")
  new_config = config.except(:new_branch)
  File.open(@working_dir + "/.cnvrg/config.yml", "w+") {|f| f.write new_config.to_yaml}
end

#revert(working_dir) ⇒ Object



682
683
684
# File 'lib/cnvrg/project.rb', line 682

def revert(working_dir)
  FileUtils.rm_rf working_dir
end

#send_ignore_listObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cnvrg/project.rb', line 103

def send_ignore_list()
  begin
    ignore_list = []
    File.open(self.local_path + "/.cnvrgignore", "r").each_line do |line|
      line = line.strip
      if line.start_with? "#" or ignore_list.include? line or line.empty?
        next
      end
      if line.end_with? "/"
        ignore_list << line.gsub("/", "")
        ignore_list << line + "."
      elsif line.include? "*"
        line = line.gsub("*", ".*")
        ignore_list << line
      else
        ignore_list << line
      end
    end
    return ignore_list.flatten
  rescue
    return []
  end

end

#send_restart_request(job_id: nil, job_type: nil, ma_id: nil) ⇒ Object



778
779
780
# File 'lib/cnvrg/project.rb', line 778

def send_restart_request(job_id: nil, job_type: nil, ma_id: nil)
  Cnvrg::API.request("#{base_resource}/spot_restart", 'POST', {job_type: job_type, job_id: job_id, machine_activity: ma_id})
end

#set_config(config) ⇒ Object



364
365
366
367
368
369
370
371
372
# File 'lib/cnvrg/project.rb', line 364

def set_config(config)
  slug = config[:project_slug] rescue nil
  owner = config[:owner] rescue nil
  name = config[:project_name] rescue nil
  if slug.blank? or owner.blank? or name.blank?
    return
  end
  File.open(@working_dir + "/.cnvrg/config.yml", "w+") {|f| f.write config.to_yaml}
end

#set_idx(idx) ⇒ Object



499
500
501
502
# File 'lib/cnvrg/project.rb', line 499

def set_idx(idx)
  FileUtils.mkdir_p("#{self.local_path}/.cnvrg")
  File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write idx.to_yaml}
end

#set_job_pod_restart(job_type: nil, job_id: nil) ⇒ Object



793
794
795
796
797
798
799
800
# File 'lib/cnvrg/project.rb', line 793

def set_job_pod_restart(job_type: nil, job_id: nil)
  job_type ||= ENV['CNVRG_JOB_TYPE']
  job_id ||= ENV['CNVRG_JOB_ID']
  if job_type.blank? or job_id.blank?
    raise StandardError.new("Cant find job env variables")
  end
  Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/jobs/#{job_type.underscore}/#{job_id}/set_pod_restart", "POST", {job_type: job_type, job_id: job_id})
end

#set_job_startedObject



823
824
825
826
827
828
829
830
# File 'lib/cnvrg/project.rb', line 823

def set_job_started
  job_type ||= ENV['CNVRG_JOB_TYPE']
  job_id ||= ENV['CNVRG_JOB_ID']
  if job_type.blank? or job_id.blank?
    raise StandardError.new("Cant find job env variables")
  end
  Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/jobs/#{job_type.underscore}/#{job_id}/set_started", "POST", {job_type: job_type, job_id: job_id})
end

#set_on_branch(is_latest) ⇒ Object



599
600
601
602
603
604
605
606
# File 'lib/cnvrg/project.rb', line 599

def set_on_branch(is_latest)
  config = YAML.load_file(@working_dir + "/.cnvrg/config.yml")
  @is_branch = !is_latest
  config[:is_branch] = @is_branch

  File.open(@working_dir + "/.cnvrg/config.yml", 'w') {|f| f.write config.to_yaml}

end

#spot_will_terminateObject



747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/cnvrg/project.rb', line 747

def spot_will_terminate
  restart = false
  config = self.get_config
  # logic that will prevent multiple restart calls
  if config[:spot_taken]
    return false
  end
  begin
    url = URI.parse('http://169.254.169.254/latest/meta-data/spot/termination-time')
    req = Net::HTTP::Get.new(url.to_s)
    res = Net::HTTP.start(url.host, url.port) {|http|
      http.request(req)
    }
    unless res.body.include? "404"
      restart = true
    end
    if res.body.include? "Empty reply from server"
      restart = false
    end
  rescue
    restart = false
  end

  if restart
    config[:spot_taken] = true
    self.set_config(config)
  end

  return restart
end

#update_idx_with_commit!(commit, latest: nil) ⇒ Object



673
674
675
676
677
678
679
680
# File 'lib/cnvrg/project.rb', line 673

def update_idx_with_commit!(commit, latest: nil)
  idx_hash = self.get_idx
  idx_hash[:commit] = commit
  self.set_on_branch(latest) unless latest.nil?

  File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write idx_hash.to_yaml}
  return true
end

#update_idx_with_files_commits!(files, commit_time) ⇒ Object



618
619
620
621
622
623
624
625
626
627
# File 'lib/cnvrg/project.rb', line 618

def update_idx_with_files_commits!(files, commit_time)

  idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml")
  files.each do |path|
    idx_hash[:tree].to_h[path].to_h[:commit_time] = commit_time
  end
  File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write idx_hash.to_yaml}

  return true
end

#update_ignore_list(new_ignore) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cnvrg/project.rb', line 43

def update_ignore_list(new_ignore)
  if new_ignore.nil? or new_ignore.empty?
    return true
  end
  list = new_ignore.split(",")
  begin
    File.open(self.local_path + "/.cnvrgignore", "a+") do |f|
      f.puts("\n")
      list.each do |i|
        f.puts("#{i}\n")
      end
    end
    return true
  rescue
    return false
  end
end

#update_is_new_branch(new_branch) ⇒ Object



311
312
313
314
315
# File 'lib/cnvrg/project.rb', line 311

def update_is_new_branch(new_branch)
  config = YAML.load_file(@working_dir + "/.cnvrg/config.yml")
  config[:new_branch] = new_branch
  File.open(@working_dir + "/.cnvrg/config.yml", "w+") {|f| f.write config.to_yaml}
end

#update_job_jupyter_token(job_type, job_id, token) ⇒ Object



700
701
702
703
704
705
# File 'lib/cnvrg/project.rb', line 700

def update_job_jupyter_token(job_type, job_id, token)
  owner = self.owner || ENV['CNVRG_OWNER']
  slug = self.slug || ENV['CNVRG_PROJECT']
  base_url = "users/#{owner}/projects/#{slug}/jobs/#{job_type.underscore}/#{job_id}"
  Cnvrg::API.request("#{base_url}/update_jupyter_token", "POST", {token: token})
end

#urlObject



38
39
40
41
# File 'lib/cnvrg/project.rb', line 38

def url
  url = Cnvrg::Helpers.remote_url
  "#{url}/#{self.owner}/projects/#{self.slug}"
end