Class: Mamiya::Script

Inherits:
DSL
  • Object
show all
Defined in:
lib/mamiya/script.rb

Defined Under Namespace

Classes: CommandFailed

Instance Attribute Summary

Attributes inherited from DSL

#hooks, #tasks

Instance Method Summary collapse

Methods inherited from DSL

#[], add_hook, defaults, define_variable_accessor, #evaluate!, #initialize, #invoke, #load!, #load_path, #set, set_default, #set_default, #task, #use

Constructor Details

This class inherits a constructor from Mamiya::DSL

Instance Method Details

#build_fromObject



141
142
143
# File 'lib/mamiya/script.rb', line 141

def build_from
  self[:build_from] && Pathname.new(self[:build_from])
end

#cd(*args) ⇒ Object



136
137
138
139
# File 'lib/mamiya/script.rb', line 136

def cd(*args)
  logger.info "$ cd #{args[0]}"
  Dir.chdir *args
end

#current_pathObject



157
158
159
# File 'lib/mamiya/script.rb', line 157

def current_path
  deploy_to && deploy_to.join('current')
end

#deploy_toObject



145
146
147
# File 'lib/mamiya/script.rb', line 145

def deploy_to
  self[:deploy_to] && Pathname.new(self[:deploy_to])
end

#release_pathObject



149
150
151
# File 'lib/mamiya/script.rb', line 149

def release_path
  self[:release_path] && Pathname.new(self[:release_path])
end

#run(*args, allow_failure: false) ⇒ Object



56
57
58
59
60
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mamiya/script.rb', line 56

def run(*args, allow_failure: false)
  # TODO: Stop when fail
  actual = -> do
    started_at = Time.now
    run_id = generate_run_id()
    logger = self.logger["run:#{run_id}"]

    env = args.last.is_a?(Hash) ? args.pop : {}
    shellenv = env.empty? ? nil : "#{escape_env(env)} "

    logger.info("$ #{shellenv}#{args.shelljoin}")

    err_r, err_w = IO.pipe
    out_r, out_w = IO.pipe

    pid = spawn(env, *args.map(&:to_s), out: out_w, err: err_w)

    [out_w, err_w].each(&:close)

    buf = ""
    last_out = Time.now

    ths = {:info => out_r, :warn => err_r}.map do |severity, io|
      Thread.new {
        until io.eof?
          str = io.gets
          logger.__send__(severity, "  #{str.chomp}")
          buf << str
          last_out = Time.now
        end
      }.tap { |_| _.abort_on_exception = true }
    end

    timekeeper_th = Thread.new do
      l = logger['timekeeper']
      loop do
        if 90 < (Time.now - last_out)
          l.warn "pid #! {pid} still running; since #{started_at}"
        end
        sleep 60
      end
    end
    timekeeper_th.abort_on_exception = true

    pid, status = Process.waitpid2(pid)
    timekeeper_th.kill if timekeeper_th.alive?

    begin
      Timeout.timeout(3) { ths.each(&:join) }
    rescue Timeout::Error
    end
    ths.each { |_| _.alive? && _.kill }

    [out_r, err_r].each(&:close)

    unless allow_failure || status.success?
      failure_msg = "Execution failed (" \
        "status=#{status.exitstatus}" \
        " pid=#{status.pid}" \
        "#{status.signaled? ? "termsig=#{status.termsig.inspect} stopsig=#{status.stopsig.inspect}" : nil}" \
        "#{status.stopped? ? " stopped" : nil}" \
        "): #{args.inspect}"

      logger.error failure_msg
      raise CommandFailed, failure_msg

    end

    logger.info "* pid #{pid} completed: #{args.inspect}"

    buf
  end

  if defined? Bundler
    Bundler.with_clean_env(&actual)
  else
    actual.call
  end
end

#shared_pathObject



153
154
155
# File 'lib/mamiya/script.rb', line 153

def shared_path
  deploy_to && deploy_to.join('shared')
end