Class: Perkins::Runner

Inherits:
Object show all
Defined in:
lib/perkins/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#branchObject

include Process



7
8
9
# File 'lib/perkins/runner.rb', line 7

def branch
  @branch
end

#build_timeObject (readonly)

Returns the value of attribute build_time.



8
9
10
# File 'lib/perkins/runner.rb', line 8

def build_time
  @build_time
end

#commandObject

include Process



7
8
9
# File 'lib/perkins/runner.rb', line 7

def command
  @command
end

#configObject

include Process



7
8
9
# File 'lib/perkins/runner.rb', line 7

def config
  @config
end

#current_buildObject (readonly)

Returns the value of attribute current_build.



8
9
10
# File 'lib/perkins/runner.rb', line 8

def current_build
  @current_build
end

#durationObject (readonly)

Returns the value of attribute duration.



8
9
10
# File 'lib/perkins/runner.rb', line 8

def duration
  @duration
end

#repoObject

include Process



7
8
9
# File 'lib/perkins/runner.rb', line 7

def repo
  @repo
end

#reportObject

include Process



7
8
9
# File 'lib/perkins/runner.rb', line 7

def report
  @report
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/perkins/runner.rb', line 8

def response
  @response
end

#shaObject

include Process



7
8
9
# File 'lib/perkins/runner.rb', line 7

def sha
  @sha
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/perkins/runner.rb', line 8

def status
  @status
end

Instance Method Details

#config_command_with_empty_value?(result, process_status) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/perkins/runner.rb', line 124

def config_command_with_empty_value?(result, process_status)
  process_status.exitstatus.to_i == 1 && result.empty?
end

#exec(cmd) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/perkins/runner.rb', line 10

def exec(cmd)
  result = run_script(cmd)
  @response = result.join("")
  
  if result.last.chomp.include?("with 0")
    @status = true
  elsif result.last.chomp.include?("with 1")
    @status = false
  else
    puts "status result not found!!"
    @status = false
  end
      
  #puts result
  #process_status = $?
  #if successful_command?(process_status) || config_command_with_empty_value?(result,process_status)
  #  @response = result
  #  @status = true
  #  return result
  #else
  #  @response = result
  #  @status = false
  #end
end

#get_buildsObject



155
156
157
# File 'lib/perkins/runner.rb', line 155

def get_builds
  repo.build_reports
end

#git_update(branch) ⇒ Object



132
133
134
135
136
# File 'lib/perkins/runner.rb', line 132

def git_update(branch)
  puts "fetch repo & reset to sha #{sha}".green
  repo.git.fetch()
  repo.git.reset_hard(sha)
end

#pipe_command(cmd) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/perkins/runner.rb', line 48

def pipe_command(cmd)
  output = []
  r, io = IO.pipe
  pid = fork do
    @process = system(cmd, out: io, err: :out)
  end
  io.close
  r.each_line{|l| 
    puts l.yellow
    output << l
    #puts "CURRENT DIR: #{Dir.pwd} !!!!!"
    #puts "CURRENT GIT DIR: #{repo.git.dir.path} !!!!!"
    #puts "#{repo.download_name} !!!!!!!"
    #updates each time, this should trigger event to interface to refresh
    @current_report.update_column(:response, output.join(""))
  } 
  #Process.waitpid(p1) #this is to get the $ exitstatus
  output
end

#run(sha) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/perkins/runner.rb', line 68

def run(sha)
  self.sha = sha
  start_build
  self.repo.virtual_sha = "-#{@current_report.id}-#{self.sha}"
  #it actually clone repo and instantiates git data
  repo.load_git

  script = Perkins::Build::script(config, repo)
  sh = script.compile

  repo.git.chdir do
    #puts "CURRENT DIR: #{Dir.pwd} !!!!!"
    #puts "CURRENT GIT DIR: #{repo.git.dir.path} !!!!!"
    git_update(sha)
    set_build_stats do
      puts "perform build".green
      self.exec(sh)
    end
  end
  #store_report
  stop_build
end

#run_script(source) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/perkins/runner.rb', line 35

def run_script(source)
  script = File.expand_path(
    "~/.perkins/.build/#{repo.name}/travis-build-#{sha}" #<< stages.join('-')
  )
  FileUtils.mkdir_p(File.dirname(script))
  File.open(script, 'w') { |f| f.write(source) }
  FileUtils.chmod(0755, script)
  Bundler.with_clean_env{
    pipe_command("#{script} 2>&1")
    #`bash #{script} 2>&1`.chomp
  }
end

#running?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/perkins/runner.rb', line 116

def running?
  @running
end

#set_build_stats(&block) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/perkins/runner.rb', line 107

def set_build_stats(&block)
  up = Time.now
  #call the command itself
  block.call
  down = Time.now
  @build_time = down
  @duration = down - up
end

#start_buildObject



91
92
93
94
95
96
# File 'lib/perkins/runner.rb', line 91

def start_build
  @running = true
  store_report
  @current_report.start!
  @repo.update_column(:build_status, "started")
end

#stop_buildObject



98
99
100
101
102
103
104
105
# File 'lib/perkins/runner.rb', line 98

def stop_build
  @running = false
  @current_report.stop!
  
  @current_report.update_attributes(self.to_report)

  @repo.update_column(:build_status, "stopped")
end

#store_reportObject



151
152
153
# File 'lib/perkins/runner.rb', line 151

def store_report
  @current_report = Perkins::BuildReport.find(report)
end

#successful_command?(process_status) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/perkins/runner.rb', line 120

def successful_command?(process_status)
  process_status.exitstatus.to_i == 0
end

#to_reportObject

TODO: add a serialized commit in order to avoid Perkins::Commit initialization in every instantiation



140
141
142
143
144
145
146
147
148
149
# File 'lib/perkins/runner.rb', line 140

def to_report
  {
    build_time: self.build_time,
    duration: self.duration,
    response: self.response,
    status: self.status,
    sha: self.sha,
    branch: self.branch
  }
end

#working_dirObject



128
129
130
# File 'lib/perkins/runner.rb', line 128

def working_dir
  repo.git.dir.path
end