Class: Konstant::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/konstant/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ Builder

Returns a new instance of Builder.



3
4
5
6
7
# File 'lib/konstant/builder.rb', line 3

def initialize(project)
  @project = project
  @timestamp = Time.now.strftime("%Y%m%d%H%M%S")
  @new_build = Konstant::Build.new project, timestamp
end

Instance Attribute Details

#new_buildObject (readonly)

Returns the value of attribute new_build.



9
10
11
# File 'lib/konstant/builder.rb', line 9

def new_build
  @new_build
end

#projectObject (readonly)

Returns the value of attribute project.



9
10
11
# File 'lib/konstant/builder.rb', line 9

def project
  @project
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



9
10
11
# File 'lib/konstant/builder.rb', line 9

def timestamp
  @timestamp
end

Instance Method Details

#buildObject



44
45
46
# File 'lib/konstant/builder.rb', line 44

def build
  Konstant::Runner.new(new_build, "build").run
end

#build_statusObject



92
93
94
# File 'lib/konstant/builder.rb', line 92

def build_status
  new_build.status
end

#build_stderrObject



100
101
102
# File 'lib/konstant/builder.rb', line 100

def build_stderr
  new_build.stderr
end

#build_stdoutObject



96
97
98
# File 'lib/konstant/builder.rb', line 96

def build_stdout
  new_build.stdout
end

#cleanupObject



52
53
54
# File 'lib/konstant/builder.rb', line 52

def cleanup
  Konstant::Runner.new(new_build, "cleanup").run
end

#cleanup_old_buildsObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/konstant/builder.rb', line 56

def cleanup_old_builds
  builds = project.builds
  limit = Konstant.config["builds_to_keep"]

  if builds.size > limit
    Konstant.logger.info "removing #{builds.size - limit} build(s) from project '#{project.id}'"
    builds[limit..-1].each do |build|
      build.destroy
    end
  end
end

#deployObject



48
49
50
# File 'lib/konstant/builder.rb', line 48

def deploy
  Konstant::Runner.new(new_build, "deploy").run
end

#notify(message) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/konstant/builder.rb', line 72

def notify(message)
  unless Konstant.config["notify"].empty?
    Mail.deliver do
      from Konstant.config["mail_sender"]
      to Konstant.config["notify"]

      case message
        when :failure
          subject "Konstant: Project failed: #{project.id}"
        when :recovery
          subject "Konstant: Project recovered: #{project.id}"
        when :cleanup_failed
          subject "Konstant: Project could not be cleaned up: #{project.id}"
        when :deploy_failed
          subject "Konstant: Project could not be deployed: #{project.id}"
      end
    end
  end
end

#previous_build_failed?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/konstant/builder.rb', line 104

def previous_build_failed?
  new_build.previous ? new_build.previous.failure? : false
end

#runObject



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
# File 'lib/konstant/builder.rb', line 11

def run
  Konstant.logger.info "building project '#{project.id}'"

  if build
    if previous_build_failed?
      Konstant.logger.info "project '#{project.id}' recovered"
      notify :recovery
    else
      Konstant.logger.info "project '#{project.id}' built successfully"
    end

    Konstant.logger.info "deploying project '#{project.id}'"
    unless deploy
      notify :deploy_failed
    end
    Konstant.logger.info "project '#{project.id}' deployed"
  else  
    Konstant.logger.info "project '#{project.id}' failed to build"
    notify :failure
  end

  Konstant.logger.info "cleaning up project '#{project.id}'"
  unless cleanup
    notify :cleanup_failed
  end
  Konstant.logger.info "project '#{project.id}' cleaned up"

  symlink
  Konstant.logger.info "finished building project '#{project.id}'"

  cleanup_old_builds
end


68
69
70
# File 'lib/konstant/builder.rb', line 68

def symlink
  system "ln -sfn #{new_build.path} #{project.path}/current"
end