class BBC::Cosmos::Tools::Commands::Release

Attributes

api[RW]

Public Class Methods

new(args = [], local_options = {}, thor_config = {}) click to toggle source
Calls superclass method BBC::Cosmos::Tools::Commands::Base::new
# File lib/bbc/cosmos/tools/commands/release.rb, line 31
def initialize(args = [], local_options = {}, thor_config = {})
  super(args, local_options, thor_config)
  @api = BBC::Cosmos::Tools::API.new(config.app["api"], options[:key_path])
end

Public Instance Methods

deploy(component = nil, release_id = nil) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 67
def deploy(component = nil, release_id = nil)
  say banner

  components(options, component).each do |id|
    begin

      component_release = release_id
      if release_id.nil?

        post_data = {}
        endpoint = config.app["deploy"]

        case options[:from]
        when "release"
          response = api.get sprintf(config.app["release"], id)
          api_error response unless response.success?
          component_release = JSON.parse(response.body)["releases"].first["version"]
          post_data = {
            "release_version" => component_release
          }
        when "int", "test", "live"

          endpoint = config.app["snapshot_deploy"]
          response = api.get sprintf(config.app["deployments"], id, options[:from])
          api_error response unless response.success?
          snapshot_id = JSON.parse(response.body).first["snapshot"]["snapshot_id"]
          post_data = {
            "snapshot_id" => snapshot_id
          }

        else
          fail("Invalid location: #{options[:from]} to deploy from")
        end
      end

      response = begin
                   check_config
                   api.post sprintf(endpoint, options[:env], id), JSON.generate(post_data)
                 rescue IncorrectUrlException => e
                   say "\nWarning: #{e.message}\n", :red
                   NullResponse.new
                 end

      if response.success?

        say get_key_value("\nComponent", id)
        say get_key_value("Version", post_data["snapshot_id"] ? post_data["snapshot_id"] : post_data["release_version"])

        data = JSON.parse(response.body)
        say "\nDeployment started successfully: #{config.app['admin']}/cosmos#{data['url']}\n", :green

      else
        api_error response
      end

    rescue Exception => e
      error(e.message, false)
    end
  end
end
deployed(component = nil) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 130
def deployed(component = nil)
  say banner

  components(options, component).each do |id|
    response = api.get sprintf(config.app["deployments"], id, options[:env])
    say get_key_value("\nComponent", id)

    if response.success?
      data = JSON.parse(response.body)

      if data.empty?
        say "There are currently no deployments for this component", :yellow
      else

        deployments = []

        data.slice!(0, options[:limit].to_i).map do |release|
          deployments << [
            release["release"]["version"],
            release["started_at"],
            release["id"],
            release["created_by"]["email_address"],
            deploy_status(release["status"])
          ]
        end

        print_table(
          build_table(["Release", "Deployed at", "Deployment id", "Deployed by", "Status"], deployments)
        )

        say "\n"
      end
    else
      api_error response
    end
  end

rescue Exception => e
  error e.message
end
list(component = nil) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 38
def list(component = nil)
  say banner

  components(options, component).each do |id|
    response = api.get sprintf(config.app["release"], id)
    say get_key_value("\nComponent", id)

    if response.success?
      data = []
      JSON.parse(response.body)["releases"].slice!(0, options[:limit]).map do |release|
        data << [release["version"], release["created_at"]]
      end

      print_table(
        build_table(["Version", "Created at"], data)
      )
      say "\n"
    else
      api_error response
    end
  end

rescue Exception => e
  error e.message
end
redeploy(component) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 172
def redeploy(component)
  build_id = make_request("deployments/component/#{component}/env/#{options[:env]}")
  start_redeploy(component, build_id[0]["id"])
end

Private Instance Methods

check_config() click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 179
def check_config
  raise IncorrectUrlException.new("Your configs/app.yaml is missing -> admin: 'https://admin.live.bbc.co.uk'") unless config.app.has_key? "admin"
end
deploy_status(status) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 211
def deploy_status(status)
  case status
  when "pending_bake"
    set_color(status, :yellow)
  when "pending_stack_update_resolution"
    set_color(status, :yellow)
  when "done"
    set_color(status, :green)
  when "failed"
    set_color(status, :red)
  else
    set_color(status, :white)
  end
end
make_request(path) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 205
def make_request(path)
  JSON.parse(api.get("/cosmos/#{path}").tap do |request|
    fail Exception, say(request.body, :red) if request.status != 200
  end.body)
end
post_json(endpoint, post_data) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 201
def post_json(endpoint, post_data)
  api.post(endpoint, (post_data.to_json))
end
redeploy_status(component, response) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 190
def redeploy_status(component, response)
  base_url = "https://admin.live.bbc.co.uk/cosmos"
  if response.success?
    success = JSON.parse(response.body)
    say "#{component} successfully redeployed", :green
    say "View deployment of #{component} here #{base_url}#{success['url']}", :green
  else
    api_error(response)
  end
end
start_redeploy(component, id) click to toggle source
# File lib/bbc/cosmos/tools/commands/release.rb, line 183
def start_redeploy(component, id)
  post_data = { "deployment_id" => id }
  endpoint = "/cosmos/env/#{options[:env]}/component/#{component}/redeploy"
  response = post_json(endpoint, post_data)
  redeploy_status(component, response)
end