Top Level Namespace

Defined Under Namespace

Modules: Capper

Instance Method Summary collapse

Methods included from Capper::Utils::Systemd

#systemctl

Methods included from Capper::Utils::Multistage

#stage

Methods included from Capper::Utils::Templates

#upload_template, #upload_template_file, #upload_template_string

Instance Method Details

#_cset(name, *args, &block) ⇒ Object



6
7
8
9
10
# File 'lib/capper/base.rb', line 6

def _cset(name, *args, &block)
  unless exists?(name)
    set(name, *args, &block)
  end
end

#confirm_if(prompt = "Continue? ") ⇒ Object

confirm if a conditoin is true (e.g. after 6pm)



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/capper/base.rb', line 164

def confirm_if(prompt="Continue? ")
  if yield
    yes = HighLine.new.agree(prompt) do |q|
      q.overwrite = false
      q.default = 'n'
    end

    unless yes
      exit(-1)
    end
  end
end

#ensure_role(role, &block) ⇒ Object

make sure a block and all commands within are only executed for servers matching the specified role even if ROLES or HOSTS is specified on the command line, in which case capistrano matches all servers by default. sigh



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/capper/base.rb', line 126

def ensure_role(role, &block)
  if task = current_task
    servers = find_servers_for_task(task)
  else
    servers = find_servers
  end

  servers = servers.select do |server|
    self.roles[role.to_sym].include?(server)
  end

  return if servers.empty?

  original, ENV['HOSTS'] = ENV['HOSTS'], servers.map { |s| s.host }.join(',')

  begin
    yield
  ensure
    ENV['HOSTS'] = original
  end
end

#run_locally(cmd) ⇒ Object

logs the command then executes it locally. returns the command output as a string



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/capper/base.rb', line 150

def run_locally(cmd)
  logger.trace "executing locally: #{cmd.inspect}" if logger
  output_on_stdout = nil
  elapsed = Benchmark.realtime do
    output_on_stdout = `#{cmd}`
  end
  if $?.to_i > 0 # $? is command exit code (posix style)
    raise Capistrano::LocalArgumentError, "Command #{cmd} returned status code #{$?}"
  end
  logger.trace "command finished in #{(elapsed * 1000).round}ms" if logger
  output_on_stdout
end

#with_env(name, value) ⇒ Object

Temporarily sets an environment variable, yields to a block, and restores the value when it is done.



116
117
118
119
120
121
# File 'lib/capper/base.rb', line 116

def with_env(name, value)
  saved, ENV[name] = ENV[name], value
  yield
ensure
  ENV[name] = saved
end