class Solano::ApiConfig

Attributes

cli_options[R]
config[R]

Public Class Methods

new(scm, tddium_client, host, cli_options) click to toggle source

BOTCH: should be a state object rather than entire CLI object

# File lib/solano/cli/config.rb, line 81
def initialize(scm, tddium_client, host, cli_options)
  @scm = scm
  @tddium_client = tddium_client
  @host = host
  @cli_options = cli_options
  @config = Hash.new
end

Public Instance Methods

delete_suite(branch, account=nil) click to toggle source
# File lib/solano/cli/config.rb, line 144
def delete_suite(branch, account=nil)
  branches = @config["branches"] || {}
  branches.delete_if do |k, v|
    v['branch'] == branch && (account.nil? || v['account'] == account)
  end
end
get_api_key(options = {}) click to toggle source
# File lib/solano/cli/config.rb, line 118
def get_api_key(options = {})
  options.any? ? load_config(options)['api_key'] : @config['api_key']
end
get_branch(branch, var, options={}) click to toggle source
# File lib/solano/cli/config.rb, line 105
def get_branch(branch, var, options={})
  if options['account'].nil? && @cli_options[:account] then
    options['account'] = @cli_options[:account]
  end

  val = fetch_branch(branch, var, options)
  return val unless val.nil?

  populate_branches(branch)

  return fetch_branch(branch, var, options)
end
load_config(options = {}) click to toggle source
# File lib/solano/cli/config.rb, line 151
def load_config(options = {})
  global_config = load_config_from_file(:global)
  return global_config if options[:global]

  repo_config = load_config_from_file
  return repo_config if options[:repo]

  @config = global_config.merge(repo_config)
end
logout() click to toggle source
# File lib/solano/cli/config.rb, line 94
def logout
  remove_solano_files
end
populate_branches(branch) click to toggle source
# File lib/solano/cli/config.rb, line 98
def populate_branches(branch)
  suites = @solano_api.get_suites(:repo_url => @scm.origin_url, :branch=>branch)
  suites.each do |ste|
    set_suite(ste)
  end
end
scm_ready_sleep() click to toggle source
# File lib/solano/cli/config.rb, line 126
def scm_ready_sleep
  s = ENV["SOLANO_SCM_READY_SLEEP"] || Default::SCM_READY_SLEEP
  s.to_f
end
set_api(solano_api) click to toggle source

BOTCH: fugly

# File lib/solano/cli/config.rb, line 90
def set_api(solano_api)
  @solano_api = solano_api
end
set_api_key(api_key, user) click to toggle source
# File lib/solano/cli/config.rb, line 122
def set_api_key(api_key, user)
  @config['api_key'] = api_key
end
set_suite(suite) click to toggle source
# File lib/solano/cli/config.rb, line 131
def set_suite(suite)
  id = suite['id']
  branch = suite['branch']
  return if id.nil? || branch.nil? || branch.empty?

  keys = %w(id branch account repo_id ci_ssh_pubkey)
  metadata = keys.inject({}) { |h, v| h[v] = suite[v]; h }

  branches = @config["branches"] || {}
  branches.merge!({id => metadata})
  @config.merge!({"branches" => branches})
end
solano_deploy_key_file_name() click to toggle source
# File lib/solano/cli/config.rb, line 213
def solano_deploy_key_file_name
  return solano_file_name(:repo, '-deploy-key')
end
solano_file_name(scope=:repo, kind='', root=nil) click to toggle source
# File lib/solano/cli/config.rb, line 199
def solano_file_name(scope=:repo, kind='', root=nil)
  ext = (@host == 'ci.predix.io') ? '' : ".#{@host}"

  case scope
  when :repo
    root ||= @scm.repo? ? @scm.root : Dir.pwd

  when :global
    root = ENV['HOME']
  end

  return File.join(root, ".predix-ci#{kind}#{ext}")
end
write_config() click to toggle source
# File lib/solano/cli/config.rb, line 161
def write_config
  path = solano_file_name(:global)
  File.open(path, File::CREAT|File::TRUNC|File::RDWR, 0600) do |file|
    config = Hash.new
    config['api_key'] = @config['api_key'] if @config.member?('api_key')
    file.write(config.to_json)
  end

  path = solano_file_name(:repo)
  File.open(path, File::CREAT|File::TRUNC|File::RDWR, 0600) do |file|
    file.write(@config.to_json)
  end

  if @scm.repo? then
    branch = @scm.current_branch
    id = get_branch(branch, 'id', {})
    suite = @config['branches'][id] rescue nil

    if suite then
      path = solano_deploy_key_file_name
      File.open(path, File::CREAT|File::TRUNC|File::RDWR, 0644) do |file|
        file.write(suite["ci_ssh_pubkey"])
      end
    end
    write_scm_ignore                # BOTCH: no need to write every time
  end
end
write_scm_ignore() click to toggle source
# File lib/solano/cli/config.rb, line 189
def write_scm_ignore
  path = @scm.ignore_path
  content = File.exists?(path) ? File.read(path) : ''
  unless content.include?(".predix-ci*\n")
    File.open(path, File::CREAT|File::APPEND|File::RDWR, 0644) do |file|
      file.write(".predix-ci*\n")
    end
  end
end

Protected Instance Methods

fetch_branch(branch, var, options) click to toggle source
# File lib/solano/cli/config.rb, line 219
def fetch_branch(branch, var, options)
  h = @config['branches']
  return nil unless h.is_a?(Hash)
  h.each_pair do |id, data|
    next unless data.is_a?(Hash)
    branch_name = data['branch']
    next unless branch_name == branch
    if options.keys.all? { |k| data.member?(k) && data[k] == options[k] }
      return data[var]
    end
  end
  return nil
end

Private Instance Methods

load_config_from_file(solano_file_type = :repo) click to toggle source
# File lib/solano/cli/config.rb, line 241
def load_config_from_file(solano_file_type = :repo)
  path = solano_file_name(solano_file_type)
  File.exists?(path) ? (JSON.parse(File.read(path)) rescue {}) : {}
end
remove_solano_files() click to toggle source
# File lib/solano/cli/config.rb, line 235
def remove_solano_files
  [solano_file_name, solano_file_name(:global)].each do |solano_file_path|
    File.delete(solano_file_path) if File.exists?(solano_file_path)
  end
end