class Shelly::App

Constants

DATABASE_CHOICES
DATABASE_KINDS
REGIONS
SERVER_SIZES

Attributes

code_name[RW]
databases[RW]
domains[RW]
environment[RW]
git_url[RW]
organization_name[RW]
region[RW]
ruby_version[RW]
size[RW]
thin[RW]
traffic[RW]
usage[RW]
web_server_ip[RW]
zone[RW]

Public Class Methods

from_attributes(attributes) click to toggle source
# File lib/shelly/app.rb, line 24
def self.from_attributes(attributes)
  new(attributes["code_name"]).tap do |app|
    app.attributes = attributes
  end
end
inside_git_repository?() click to toggle source
# File lib/shelly/app.rb, line 308
def self.inside_git_repository?
  system("git status > /dev/null 2>&1")
end
new(code_name = nil) click to toggle source
# File lib/shelly/app.rb, line 20
def initialize(code_name = nil)
  self.code_name = code_name
end

Public Instance Methods

add_git_remote(remote_name = 'shelly') click to toggle source
# File lib/shelly/app.rb, line 46
def add_git_remote(remote_name = 'shelly')
  system("git remote rm #{remote_name} > /dev/null 2>&1")
  system("git remote add #{remote_name} #{git_url}")
end
admin_maintenance_in_progress?() click to toggle source
# File lib/shelly/app.rb, line 292
def admin_maintenance_in_progress?
  attributes["maintenance"]
end
application_logs(options = {}) click to toggle source
# File lib/shelly/app.rb, line 109
def application_logs(options = {})
  shelly.application_logs(code_name, options)
end
application_logs_tail() { |l| ... } click to toggle source
# File lib/shelly/app.rb, line 113
def application_logs_tail
  shelly.application_logs_tail(code_name) { |l| yield(l) }
end
attributes() click to toggle source
# File lib/shelly/app.rb, line 256
def attributes
  @attributes ||= shelly.app(code_name)
end
attributes=(attributes) click to toggle source
# File lib/shelly/app.rb, line 30
def attributes=(attributes)
  @attributes = attributes
end
backup_databases() click to toggle source

Public: Return databases to backup for given Cloud in Cloudfile Returns Array of databases, except redis db

# File lib/shelly/app.rb, line 415
def backup_databases
  cloud_databases - ['redis']
end
cloud_databases() click to toggle source

Public: Return databases for given Cloud in Cloudfile Returns Array of databases

# File lib/shelly/app.rb, line 377
def cloud_databases
  content["servers"].map do |server, settings|
    settings["databases"]
  end.flatten.uniq
end
config(path) click to toggle source
# File lib/shelly/app.rb, line 200
def config(path)
  shelly.app_config(code_name, path)
end
config_exists?(path) click to toggle source
# File lib/shelly/app.rb, line 216
def config_exists?(path)
  configs.any? { |config| config["path"] == path }
end
configs() click to toggle source
# File lib/shelly/app.rb, line 188
def configs
  @configs ||= shelly.app_configs(code_name)
end
console(server = nil) click to toggle source
# File lib/shelly/app.rb, line 336
def console(server = nil)
  ssh(:command => "start_console", :server => server)
end
content() click to toggle source
# File lib/shelly/app.rb, line 371
def content
  @content ||= Cloudfile.new.content[code_name]
end
create() click to toggle source
# File lib/shelly/app.rb, line 72
def create
  attributes = {:code_name => code_name,
                :organization_name => organization_name,
                :zone => zone,
                :region => @region}
  response = shelly.create_app(attributes)
  assign_attributes(response)
end
create_cloudfile() click to toggle source
# File lib/shelly/app.rb, line 81
def create_cloudfile
  cloudfile = Cloudfile.new
  cloudfile.code_name = code_name
  cloudfile.ruby_version = ruby_version
  cloudfile.environment = environment
  cloudfile.domains = domains
  cloudfile.size = size
  if ruby_version == 'jruby'
    cloudfile.puma = puma
  else
    cloudfile.thin = thin
  end
  cloudfile.databases = databases
  cloudfile.create
end
create_config(path, content) click to toggle source
# File lib/shelly/app.rb, line 204
def create_config(path, content)
  shelly.app_create_config(code_name, path, content)
end
create_endpoint(certificate, key, sni) click to toggle source
# File lib/shelly/app.rb, line 228
def create_endpoint(certificate, key, sni)
  shelly.create_endpoint(code_name, certificate, key, sni)
end
credit() click to toggle source
# File lib/shelly/app.rb, line 300
def credit
  attributes["organization"]["credit"].to_f
end
database_backup(handler) click to toggle source
# File lib/shelly/app.rb, line 131
def database_backup(handler)
  attributes = shelly.database_backup(code_name, handler)
  Shelly::Backup.new(attributes.merge("code_name" => code_name))
end
database_backups() click to toggle source
# File lib/shelly/app.rb, line 125
def database_backups
  shelly.database_backups(code_name).map do |attributes|
    Shelly::Backup.new(attributes.merge("code_name" => code_name))
  end
end
databases=(dbs) click to toggle source
# File lib/shelly/app.rb, line 42
def databases=(dbs)
  @databases = dbs - ['none']
end
dbconsole() click to toggle source
# File lib/shelly/app.rb, line 244
def dbconsole
  ssh_with_db_server(:command => "start_dbconsole")
end
delayed_job?() click to toggle source

Public: Delayed job enabled? Returns true if delayed job is present

# File lib/shelly/app.rb, line 385
def delayed_job?
  option?("delayed_job")
end
delete() click to toggle source
# File lib/shelly/app.rb, line 97
def delete
  shelly.delete_app(code_name)
end
delete_config(path) click to toggle source
# File lib/shelly/app.rb, line 212
def delete_config(path)
  shelly.app_delete_config(code_name, path)
end
delete_endpoint(uuid) click to toggle source
# File lib/shelly/app.rb, line 236
def delete_endpoint(uuid)
  shelly.delete_endpoint(code_name, uuid)
end
delete_file(remote_path) click to toggle source
# File lib/shelly/app.rb, line 363
def delete_file(remote_path)
  ssh(:command => "delete_file #{remote_path}")
end
deploy_log(log) click to toggle source
# File lib/shelly/app.rb, line 105
def deploy_log(log)
  shelly.deploy_log(code_name, log)
end
deploy_logs() click to toggle source
# File lib/shelly/app.rb, line 101
def deploy_logs
  shelly.deploy_logs(code_name)
end
deployed?() click to toggle source

Public: Return true when app has been deployed false otherwise

# File lib/shelly/app.rb, line 421
def deployed?
  git_info["deployed_commit_sha"].present?
end
deployment(deployment_id) click to toggle source
# File lib/shelly/app.rb, line 184
def deployment(deployment_id)
  shelly.deployment(code_name, deployment_id)
end
download(relative_source, destination) click to toggle source
# File lib/shelly/app.rb, line 356
def download(relative_source, destination)
  tunnel_connection.tap do |conn|
    source = File.join("#{conn['host']}:#{persistent_disk}", relative_source)
    rsync(source, destination, conn)
  end
end
download_application_logs(options, callback) click to toggle source
# File lib/shelly/app.rb, line 121
def download_application_logs(options, callback)
  shelly.download_file(code_name, options["filename"], options["url"], callback)
end
download_application_logs_attributes(date) click to toggle source
# File lib/shelly/app.rb, line 117
def download_application_logs_attributes(date)
  shelly.download_application_logs_attributes(code_name, date)
end
edit_billing_url() click to toggle source
# File lib/shelly/app.rb, line 324
def edit_billing_url
  "#{shelly.shellyapp_url}/organizations/#{organization_name || attributes['organization']['name']}/edit"
end
endpoint(uuid) click to toggle source
# File lib/shelly/app.rb, line 224
def endpoint(uuid)
  shelly.endpoint(code_name, uuid)
end
endpoints() click to toggle source
# File lib/shelly/app.rb, line 220
def endpoints
  shelly.endpoints(code_name)
end
finish_maintenance() click to toggle source
# File lib/shelly/app.rb, line 175
def finish_maintenance
  shelly.finish_maintenance(code_name)
end
git_fetch_remote(remote = 'shelly') click to toggle source
# File lib/shelly/app.rb, line 55
def git_fetch_remote(remote = 'shelly')
  system("git fetch #{remote} > /dev/null 2>&1")
end
git_info() click to toggle source
# File lib/shelly/app.rb, line 280
def git_info
  attributes["git_info"]
end
git_remote_exist?(remote_name = 'shelly') click to toggle source
# File lib/shelly/app.rb, line 51
def git_remote_exist?(remote_name = 'shelly')
  IO.popen("git remote").read.include?(remote_name)
end
git_remote_name() click to toggle source
# File lib/shelly/app.rb, line 66
def git_remote_name
  io = IO.popen("git remote -v").readlines
  remote = io.select {|line| line.include? git_info["repository_url"]}.first
  remote.split("\t").first unless remote.nil?
end
import_database(kind, filename, server) click to toggle source
# File lib/shelly/app.rb, line 140
def import_database(kind, filename, server)
  ssh_with_db_server(:command => "import_database #{kind.downcase} #{filename}",
    :server => server)
end
in_deploy_failed_state?() click to toggle source
# File lib/shelly/app.rb, line 316
def in_deploy_failed_state?
  state == "deploy_failed"
end
list_files(path) click to toggle source
# File lib/shelly/app.rb, line 340
def list_files(path)
  ssh(:command => "ls -l #{persistent_disk}/#{path}")
end
logs() click to toggle source
# File lib/shelly/app.rb, line 155
def logs
  shelly.cloud_logs(code_name)
end
maintenances() click to toggle source
# File lib/shelly/app.rb, line 167
def maintenances
  shelly.maintenances(code_name)
end
mongoconsole() click to toggle source
# File lib/shelly/app.rb, line 248
def mongoconsole
  ssh_with_db_server(:command => "start_mongodb")
end
open() click to toggle source
# File lib/shelly/app.rb, line 328
def open
  Launchy.open("http://#{attributes["domain"]}")
end
organization_details_present?() click to toggle source
# File lib/shelly/app.rb, line 304
def organization_details_present?
  attributes["organization"]["details_present"]
end
pending_commits() click to toggle source

Public: Return list of not deployed commits Returns: A list of commits as a String with new line chars format: “#{short SHA} #{commit message} (#{time, ago notation})”

# File lib/shelly/app.rb, line 428
def pending_commits
  current_commit = IO.popen("git rev-parse 'HEAD'").read.strip
  format = "%C(yellow)%h%Creset %s %C(red)(%cr)%Creset"
  range = "#{git_info["deployed_commit_sha"]}..#{current_commit}"
  IO.popen(%Q{git log --no-merges --oneline --pretty=format:"#{format}" #{range}}).read.strip
end
puma() click to toggle source
# File lib/shelly/app.rb, line 38
def puma
  size == "small" ? 1 : 2
end
puma?() click to toggle source

Public: Puma app servers present? Returns true if puma is present

# File lib/shelly/app.rb, line 409
def puma?
  option?("puma")
end
rake(task, server = nil) click to toggle source
# File lib/shelly/app.rb, line 240
def rake(task, server = nil)
  ssh(:command => "rake_runner '#{task.inspect}'", :server => server)
end
redeploy() click to toggle source

returns the id of created deployment

# File lib/shelly/app.rb, line 180
def redeploy
  shelly.redeploy(code_name)["deployment"]["id"]
end
redis_cli() click to toggle source
# File lib/shelly/app.rb, line 252
def redis_cli
  ssh_with_db_server(:command => "start_redis")
end
remove_git_remote() click to toggle source
# File lib/shelly/app.rb, line 59
def remove_git_remote
  remote = git_remote_name
  if remote.present?
    system("git remote rm #{remote} > /dev/null 2>&1")
  end
end
request_backup(kinds) click to toggle source
# File lib/shelly/app.rb, line 149
def request_backup(kinds)
  Array(kinds).each do |kind|
    shelly.request_backup(code_name, kind)
  end
end
reset_database(kind) click to toggle source
# File lib/shelly/app.rb, line 145
def reset_database(kind)
  ssh_with_db_server(:command => "reset_database #{kind.downcase}")
end
restore_backup(filename) click to toggle source
# File lib/shelly/app.rb, line 136
def restore_backup(filename)
  shelly.restore_backup(code_name, filename)
end
setup_tunnel(conn, local_port) click to toggle source
# File lib/shelly/app.rb, line 367
def setup_tunnel(conn, local_port)
  system "ssh #{ssh_options(conn)} -N -L #{local_port}:localhost:#{conn['service']['port']} #{conn['host']}"
end
shelly_generated_configs() click to toggle source
# File lib/shelly/app.rb, line 196
def shelly_generated_configs
  configs.find_all { |config| config["created_by_user"] == false }
end
sidekiq?() click to toggle source

Public: Sidekiq enabled? Returns true if sidekiq is present

# File lib/shelly/app.rb, line 397
def sidekiq?
  option?("sidekiq")
end
ssh_console(server = nil) click to toggle source
# File lib/shelly/app.rb, line 332
def ssh_console(server = nil)
  ssh(:server => server)
end
start() click to toggle source
# File lib/shelly/app.rb, line 159
def start
  shelly.start_cloud(code_name)["deployment"]["id"]
end
start_maintenance(options) click to toggle source
# File lib/shelly/app.rb, line 171
def start_maintenance(options)
  shelly.start_maintenance(code_name, options)
end
state() click to toggle source
# File lib/shelly/app.rb, line 288
def state
  attributes["state"]
end
state_description() click to toggle source
# File lib/shelly/app.rb, line 296
def state_description
  attributes["state_description"]
end
statistics() click to toggle source
# File lib/shelly/app.rb, line 260
def statistics
  @stats ||= shelly.statistics(code_name)
end
stop() click to toggle source
# File lib/shelly/app.rb, line 163
def stop
  shelly.stop_cloud(code_name)["deployment"]["id"]
end
system_user() click to toggle source
# File lib/shelly/app.rb, line 272
def system_user
  attributes["system_user"]
end
thin?() click to toggle source

Public: Thin app servers present? Returns true if thin is present

# File lib/shelly/app.rb, line 403
def thin?
  option?("thin")
end
to_s() click to toggle source
# File lib/shelly/app.rb, line 320
def to_s
  code_name
end
tunnel_connection(service = "ssh", server = nil) click to toggle source

Returns first at least configured virtual server

# File lib/shelly/app.rb, line 436
def tunnel_connection(service = "ssh", server = nil)
  shelly.tunnel(code_name, service, server)
end
turned_off?() click to toggle source
# File lib/shelly/app.rb, line 312
def turned_off?
  state == 'turned_off'
end
update_config(path, content) click to toggle source
# File lib/shelly/app.rb, line 208
def update_config(path, content)
  shelly.app_update_config(code_name, path, content)
end
update_endpoint(uuid, certificate, key) click to toggle source
# File lib/shelly/app.rb, line 232
def update_endpoint(uuid, certificate, key)
  shelly.update_endpoint(code_name, uuid, certificate, key)
end
upload(source, destination) click to toggle source
# File lib/shelly/app.rb, line 344
def upload(source, destination)
  tunnel_connection.tap do |conn|
    rsync(source, "#{conn['host']}:#{persistent_disk}/#{destination}", conn, "--relative")
  end
end
upload_database(source) click to toggle source
# File lib/shelly/app.rb, line 350
def upload_database(source)
  configured_db_server_connection.tap do |conn|
    rsync(source, "#{conn['host']}:#{persistent_disk}", conn)
  end
end
user_configs() click to toggle source
# File lib/shelly/app.rb, line 192
def user_configs
  configs.find_all { |config| config["created_by_user"] }
end
whenever?() click to toggle source

Public: Whenever enabled? Returns true if whenever is present

# File lib/shelly/app.rb, line 391
def whenever?
  option?("whenever")
end

Private Instance Methods

assign_attributes(response) click to toggle source
# File lib/shelly/app.rb, line 442
def assign_attributes(response)
  self.git_url = response["git_url"]
  self.domains = response["domains"]
  self.ruby_version = detect_ruby_version || response["ruby_version"]
  self.environment = response["environment"]
end
childprocess(command) click to toggle source
# File lib/shelly/app.rb, line 501
def childprocess(command)
  ChildProcess.posix_spawn = true
  process = ::ChildProcess.build(*shellwords(command))
  process.io.inherit!
  process.start
  process.wait
end
configured_db_server_connection(server = nil) click to toggle source

Returns first at least configured virtual server if databases are configured

# File lib/shelly/app.rb, line 479
def configured_db_server_connection(server = nil)
  shelly.configured_db_server(code_name, server)
end
detect_ruby_version() click to toggle source
# File lib/shelly/app.rb, line 449
def detect_ruby_version
  jruby? ? 'jruby' : gemfile_ruby_version
end
gemfile_ruby_version() click to toggle source
# File lib/shelly/app.rb, line 453
def gemfile_ruby_version
  ruby_version = Bundler::Definition.build("Gemfile", "Gemfile.lock",
    nil).ruby_version
  return unless ruby_version

  if ruby_version.engine == 'jruby'
    'jruby'
  else
    ruby_version.version
  end
end
jruby?() click to toggle source
# File lib/shelly/app.rb, line 469
def jruby?
  RUBY_PLATFORM == 'java'
end
option?(option) click to toggle source

Internal: Checks if specified option is present in Cloudfile

# File lib/shelly/app.rb, line 474
def option?(option)
  content["servers"].any? {|_, settings| settings.has_key?(option)}
end
persistent_disk() click to toggle source
# File lib/shelly/app.rb, line 465
def persistent_disk
  "/home/#{system_user}/disk"
end
rsync(source, destination, conn, options = "") click to toggle source
# File lib/shelly/app.rb, line 497
def rsync(source, destination, conn, options = "")
  system "rsync --archive --verbose --compress #{options} -e 'ssh #{ssh_options(conn)}' --progress #{source} #{destination}"
end
ssh(options = {}) click to toggle source
# File lib/shelly/app.rb, line 483
def ssh(options = {})
  conn = tunnel_connection("ssh", options[:server])
  childprocess("ssh #{ssh_options(conn)} -t -t #{conn['host']} #{options[:command]}")
end
ssh_options(conn) click to toggle source
# File lib/shelly/app.rb, line 493
def ssh_options(conn)
  "-o StrictHostKeyChecking=no -p #{conn['port']} -l #{conn['user']}"
end
ssh_with_db_server(options = {}) click to toggle source
# File lib/shelly/app.rb, line 488
def ssh_with_db_server(options = {})
  conn = configured_db_server_connection(options[:server])
  childprocess("ssh #{ssh_options(conn)} -t -t #{conn['host']} #{options[:command]}")
end