class Solano::SCM

Constants

SCMS

Attributes

default_origin_url[RW]

Public Class Methods

configure() click to toggle source
# File lib/solano/scm/configure.rb, line 5
def self.configure
  scm = nil
  ok = false
  scms = [::Solano::Git, ::Solano::Hg]

  # Select SCM based on command availability and current repo type
  scms.each do |scm_class|
    sniff_scm = scm_class.new
    if sniff_scm.repo? && scm_class.version_ok then
      ok = true
      scm = sniff_scm
      break
    end
  end

  # Fall back to first SCM type that is available
  if !ok then
    scms.each do |scm_class|
      sniff_scm = scm_class.new
      if scm_class.version_ok then
        ok = true
        scm = sniff_scm
        break
      end
    end
  end

  # Default to a null SCM implementation
  scm ||= ::Solano::StubSCM.new
  return [scm, ok]
end
new() click to toggle source
# File lib/solano/scm/scm.rb, line 8
def initialize
  @default_origin_url = nil
end
parse_scp_url(url, scp_path) click to toggle source
# File lib/solano/scm/url.rb, line 7
def self.parse_scp_url(url, scp_path)
  # remove the ssh scheme (if any)
  url_to_parse = without_scheme(url, "ssh")

  # This is a gross abuse of Adressable::URI
  # It will treat git@github.com in git@github.com:user/repo.git as a scheme
  uri = Addressable::URI.parse(url_to_parse)
  raise SolanoError.new("invalid repo url #{url}") if uri.scheme.nil?

  scheme_parts = uri.scheme.split("@")
  uri.path = "/#{uri.path}" unless uri.path.to_s[0] == "/"
  uri.scheme = "ssh"
  uri.host = scheme_parts.last
  uri.user = scheme_parts.first
  uri
end
scp_url?(raw_url, attempted_parse) click to toggle source

Returns scp path if it looks like this is an SCP url

# File lib/solano/scm/url.rb, line 25
def self.scp_url?(raw_url, attempted_parse)
  if attempted_parse && attempted_parse.scheme == 'https' then
    return nil
  end
  raw_url_elements = without_scheme(raw_url).split(":")
  scp_path = raw_url_elements.last if raw_url_elements.size > 1
  if scp_path then
    path_elements = scp_path.split(/\//)
    return scp_path if path_elements[0] !~ /^\d+$/
  end
  return nil
end
valid_repo_url?(url) click to toggle source

Weak validator; server will also validate

# File lib/solano/scm/url.rb, line 44
def self.valid_repo_url?(url)
  if url.nil? || url.strip.empty? then
    raise SolanoError.new("invalid empty scm url")
  end

  begin
    attempted_parse = Addressable::URI.parse(url)
  rescue Exception => e
    raise SolanoError.new(e.message)
  end

  if scp_path = scp_url?(url, attempted_parse) then
    uri = parse_scp_url(url, scp_path)
  else
    uri = attempted_parse
  end

  scheme_pattern = SCMS+SCMS.map { |scm| scm+"+ssh" }
  scheme_pattern = scheme_pattern.join("|")
  scheme_pattern = "https?|ssh|"+scheme_pattern

  ok =  uri.scheme =~ /^(#{scheme_pattern})$/
  ok &&= uri.host && uri.host.size > 0
  ok &&= uri.path && uri.path.size > 0

  if !ok then
    raise SolanoError.new("invalid repo url: '#{url}'")
  end
  return ok
end
without_scheme(url, scheme = nil) click to toggle source
# File lib/solano/scm/url.rb, line 38
def self.without_scheme(url, scheme = nil)
  scheme ||= "[a-z]+"
  url.gsub(/^#{scheme}\:\/\//, "")
end

Public Instance Methods

changes?(options={}) click to toggle source
# File lib/solano/scm/scm.rb, line 65
def changes?(options={})
  return false
end
commits() click to toggle source
# File lib/solano/scm/scm.rb, line 108
def commits
  []
end
create_patch(session_id, options={}) click to toggle source
# File lib/solano/scm/scm.rb, line 81
def create_patch(session_id, options={})
  raise Text::Error:PATCH_NOT_SUPPORTED
end
create_snapshot(session_id, options={}) click to toggle source
# File lib/solano/scm/scm.rb, line 77
def create_snapshot(session_id, options={})
  raise Text::Error:SNAPSHOT_NOT_SUPPORTED
end
current_branch() click to toggle source
# File lib/solano/scm/scm.rb, line 57
def current_branch
  return nil
end
current_commit() click to toggle source
# File lib/solano/scm/scm.rb, line 104
def current_commit
  return nil
end
default_branch() click to toggle source
# File lib/solano/scm/scm.rb, line 61
def default_branch
  return nil
end
get_snap_id() click to toggle source
# File lib/solano/scm/scm.rb, line 69
def get_snap_id
  return @snap_id
end
ignore_path() click to toggle source
# File lib/solano/scm/scm.rb, line 53
def ignore_path
  return nil
end
latest_commit() click to toggle source
# File lib/solano/scm/scm.rb, line 116
def latest_commit
  return nil
end
number_of_commits(id_from, id_to) click to toggle source
# File lib/solano/scm/scm.rb, line 112
def number_of_commits(id_from, id_to)
  return 0
end
origin_url() click to toggle source
# File lib/solano/scm/scm.rb, line 49
def origin_url
  return @default_origin_url
end
push_latest(session_data, suite_details, options={}) click to toggle source
# File lib/solano/scm/scm.rb, line 73
def push_latest(session_data, suite_details, options={})
  return false
end
repo?() click to toggle source
# File lib/solano/scm/scm.rb, line 37
def repo?
  return false
end
repo_name() click to toggle source
# File lib/solano/scm/scm.rb, line 45
def repo_name
  return "unknown"
end
root() click to toggle source
# File lib/solano/scm/scm.rb, line 41
def root
  return Dir.pwd
end
scm_name() click to toggle source
# File lib/solano/scm/scm.rb, line 34
def scm_name
end
support_data() click to toggle source
# File lib/solano/scm/scm.rb, line 12
def support_data
  data = Hash.new
  data['scm_name'] = self.scm_name
  if !self.repo? then
    data['is_repo'] = false
    return data
  end

  %w(scm_name repo? root repo_name origin_url current_branch default_branch changes?).each do |method|
    key = method
    if method =~ /[?]\z/ then
      key = "is_#{method.sub(/[?]\z/, '')}"
    end

    value = self.send(method.to_sym) rescue nil

    data[key] = value
  end

  return data
end
upload_file(auth_url, file_path) click to toggle source
# File lib/solano/scm/scm.rb, line 85
def upload_file(auth_url, file_path)
  if (`which curl >/dev/null 2>&1 ; echo $?`).to_i == 0 then
    `curl -f --upload-file "#{file_path}" "#{auth_url}"`
    if(`echo $?`).to_i == 22  then
      raise "Failed to upload #{file_path} URL (#{out.code})"
    end
  else
    uri = URI(auth_url)
    body = File.read(file_path)
    out = Net::HTTP.start(uri.host, :use_ssl => true) do |http|
        http.send_request("PUT", uri.request_uri, body, {"content-type" => "",})
    end
    if out.code.to_i != 200
      raise "Failed to upload to #{file_path} (#{out.code})"
    end
  end
end