class Locomotive::Wagon::PushCommand

Constants

RESOURCES
RESOURCES_WITH_CONTENT

Attributes

credentials[RW]
platform_url[RW]

Public Class Methods

push(env, path, options, shell) click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 30
def self.push(env, path, options, shell)
  self.new(env, path, options, shell).push
end

Public Instance Methods

push() click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 34
def push
  require 'haml'

  Locomotive::Wagon.require_misc_gems

  api_client = build_api_site_client(connection_information)

  return if api_client.nil?

  # Ask for a confirmation (Warning) if we deploy with the -d option
  # since it alters content on the remote engine
  if options[:data]
    return unless ask_for_performing("Warning! You're about to deploy data which will alter the content of your site.")
  end

  if options[:verbose]
    PushLogger.new
    _push(api_client)
  else
    show_wait_spinner('Deploying...') { _push(api_client) }
  end
end

Private Instance Methods

_push(api_client) click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 59
def _push(api_client)
  validate!

  content_assets_pusher = Locomotive::Wagon::PushContentAssetsCommand.new(api_client, steam_services)

  each_resource do |klass|
    klass.push(api_client, steam_services, content_assets_pusher, remote_site) do |pusher|
      pusher.with_data if options[:data]
      pusher.only(options[:filter]) unless options[:filter].blank?
    end
  end

  print_result_message
end
ask_for_performing(message) click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 183
def ask_for_performing(message)
  shell.say(message, :yellow)

  unless shell.yes?("Are you sure you want to perform this action? (answer yes or no)")
    shell.say("Deployment canceled!", :yellow)
    return false
  end

  true
end
ask_for_platform_url() click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 154
def ask_for_platform_url
  default = ENV['LOCOMOTIVE_PLATFORM_URL'] || DEFAULT_PLATFORM_URL

  url = shell.try(:ask, "What is the URL of your platform? (default: #{default})")

  self.platform_url = url.blank? ? default : url
end
connection_information() click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 98
def connection_information
  if information = read_deploy_settings(self.env, self.path)
    # the deployment env exists and contains all the information we need to move on
    information
  else
    # warn the user that we're going to create a new site. Ask her/him if we continue
    return unless ask_for_performing('You are about to deploy a new site')

    # mandatory to sign in
    load_credentials_from_netrc

    return if self.credentials.nil?

    # create the remote site on the platform
    site = create_remote_site

    # update the deploy.yml by adding the new env since we've got all the information
    write_deploy_settings(self.env, self.path, {
      'host'    => api_host,
      'handle'  => site.handle,
      'email'   => credentials[:email],
      'api_key' => credentials[:api_key]
    }.tap { |options| options['ssl'] = true if api_host.ends_with?(':443') })
  end
end
create_remote_site() click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 124
def create_remote_site
  # get an instance of the Steam services in order to load the information about the site (SiteRepository)
  steam_services.current_site.tap do |site|
    # ask for a handle if not found (blank: random one)
    if (handle = shell.try(:ask, "What is the handle of your site? (default: a random one)")).present?
      site[:handle] = handle
    end

    # create the site
    attributes = SiteDecorator.new(site).to_hash

    _site = api_client.sites.create(attributes)
    site[:handle] = _site.handle

    instrument :site_created, site: site
  end
end
each_resource() { |klass| ... } click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 86
def each_resource
  RESOURCES.each do |name|
    next if !options[:resources].blank? && !options[:resources].include?(name)

    next if RESOURCES_WITH_CONTENT.include?(name) && !options[:data]

    klass = "Locomotive::Wagon::Push#{name.camelcase}Command".constantize

    yield klass
  end
end
load_credentials_from_netrc() click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 142
def load_credentials_from_netrc
  # ask for the platform URL (or LOCOMOTIVE_PLATFORM_URL env variable)
  ask_for_platform_url

  # retrieve email + api_key. If no entry present in the .netrc, raise an error
  self.credentials = read_credentials_from_netrc(self.api_host)

  if self.credentials.nil?
    shell.say "Sorry, we were unable to find the credentials for this platform.\nPlease first login using the \"bundle exec wagon auth\"", :yellow
  end
end
local_site() click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 162
def local_site
  return @local_site if @local_site
  @local_site = SiteDecorator.new(steam_services.repositories.site.first)
end
print_result_message() click to toggle source
remote_site() click to toggle source
# File lib/locomotive/wagon/commands/push_command.rb, line 167
def remote_site
  return @remote_site if @remote_site

  attribute = nil

  begin
    attributes = @api_site_client.current_site.get.attributes
  rescue Locomotive::Coal::UnknownResourceError
    shell.say 'Sorry, we were unable to find your site on the remote platform. Check the information in your config/deploy.yml file.', :red
    raise 'Unable to find your site on the remote platform'
  end

  _site         = Locomotive::Steam::Site.new(attributes)
  @remote_site  = RemoteSiteDecorator.new(_site)
end
validate!() click to toggle source

To push all the other resources, the big requirement is to have the same locales between the local site and the remote one.

# File lib/locomotive/wagon/commands/push_command.rb, line 76
def validate!
  if local_site.default_locale != remote_site.default_locale && remote_site.edited?
    raise "Your Wagon site locale (#{local_site.default_locale}) is not the same as the one in the back-office (#{remote_site.default_locale})."
  end

  if local_site.locales != remote_site.locales
    instrument :warning, message: "Your Wagon site locales (#{local_site.locales.join(', ')}) are not the same as the ones in the back-office (#{remote_site.locales.join(', ')})."
  end
end