module PulpHelper::Repo

Constants

PUPPET_DISTRIBUTOR_ID
PUPPET_IMPORTER_ID
REPO_TYPE_PUPPET
REPO_TYPE_RPM
YUM_DISTRIBUTOR_ID
YUM_EXPORT_DISTRIBUTOR_ID
YUM_IMPORTER_ID

Public Instance Methods

copy_puppet_between_repo!(from_repo, to_repo, author,name, version, delete_new=false, auto_publish=true) click to toggle source
# File lib/pulphelper/repo.rb, line 347
def copy_puppet_between_repo!(from_repo, to_repo, author,name, version, delete_new=false, auto_publish=true)
  search_params = get_puppet_search_params(author, name, version)
  begin
    puts "search_params :#{search_params}"
    unit_search_response=client.resources.unit.search("puppet_module", search_params[:criteria], search_params[:optional])
    if unit_search_response.code !=200
      raise "Exception: unit search faild with code #{unit_search_response.code}"
    end
    puts "search_response : #{unit_search_response.to_json}"
    found_units=JSON.parse(unit_search_response.to_json)
    unit_id=found_units.first['_id']

    unless unit_id && unit_id.length >0
      raise "No puppet module found to copy"
    end
    copy_unit_ids= {
        :ids => [unit_id]
    }
    copy_response=client.extensions.puppet_module.copy(from_repo, to_repo, copy_unit_ids)
    if copy_response.code !=200 && copy_response.code !=202
      raise "Exception: unit copy failed with code #{copy_response.code}"
    end
    if(delete_new)
      delete_puppet_newer!(to_repo,author, name, version, auto_publish )
    end
  rescue StandardError => e
    raise "Error copy module between repo: #{e.to_s}"
  end
end
copy_rpm_between_repo!(from_repo, to_repo, name, version, release, arch, delete_new=false, auto_publish=true) click to toggle source
# File lib/pulphelper/repo.rb, line 295
def copy_rpm_between_repo!(from_repo, to_repo, name, version, release, arch, delete_new=false, auto_publish=true)
  search_params=get_rpm_search_params(name, version, release, arch)
  begin
    unit_search_response=client.resources.unit.search('rpm', search_params[:criteria], search_params[:optional])

    if unit_search_response.code !=200
      raise "Exception: Cannot find unit"
    end
    found_units=JSON.parse(unit_search_response.to_json)
    unit_id=found_units.first['_id']

    copy_unit_ids= {
        :ids => [unit_id]
    }
    copy_response=client.extensions.rpm.copy(from_repo, to_repo, copy_unit_ids)
    if copy_response.code !=200 && copy_response.code !=202
      raise "Exception: unit copy failed with code #{copy_response.code}"
    end

    if(delete_new)
      delete_rpm_newer!(to_repo, name, version, release, arch,  auto_publish)
    end
  rescue StandardError => e
    raise "Exception: Error copy module between repo: #{e.to_s}"
  end
end
create_puppet_repo(repo_id:, display_name: nil , description: '', feed_url: nil, queries: nil, remove_missing: false, serve_http: true, serve_https: false, auto_publish: false) click to toggle source
# File lib/pulphelper/repo.rb, line 188
def create_puppet_repo(repo_id:, display_name: nil , description: '', feed_url: nil, queries: nil,  remove_missing: false,  serve_http: true, serve_https: false, auto_publish: false)
  importer = Runcible::Models::PuppetImporter.new
  importer.feed = feed_url if feed_url
  importer.remove_missing = remove_missing
  importer.queries = queries
  puppet_distributor = Runcible::Models::PuppetDistributor.new nil, serve_http, serve_https
  puppet_distributor.auto_publish = auto_publish
  puppet_distributor.id = 'puppet_distributor'

  optional = {
    :display_name => display_name,
    :description => description,
  }

  begin
    response = client.extensions.repository.create_with_importer_and_distributors(repo_id, importer, [puppet_distributor], optional)
    code=response.code
    body=response.body
    puts "code #{code}"
    case code
    when 201
      return true
    default
      raise "Operation failed, response code:#{code}, #{response}"
    end
  rescue Exception => e
    raise "Failed to create repo, #{e.message}"
  end
end
create_rpm_repo(repo_id:, display_name: nil , description: '', feed_url: nil, relative_url: nil, serve_http: true, serve_https: false, auto_publish: false) click to toggle source
# File lib/pulphelper/repo.rb, line 141
def create_rpm_repo(repo_id:, display_name: nil , description: '', feed_url: nil, relative_url: nil, serve_http: true, serve_https: false, auto_publish: false)

  if relative_url.nil? || relative_url.strip.length<1
    raise "Invalid relative_url"
  end
  importer = Runcible::Models::YumImporter.new
  importer.feed = feed_url if feed_url
  yum_distributor = Runcible::Models::YumDistributor.new relative_url, serve_http, serve_https
  yum_distributor.auto_publish = auto_publish
  yum_distributor.id = 'yum_distributor'

  export_distributor = Runcible::Models::ExportDistributor.new serve_http, serve_https, relative_url
  export_distributor.auto_publish = auto_publish
  export_distributor.id = 'export_distributor'

  optional = {
    :display_name => display_name,
    :description => description,
  }

  begin
    version = get_version #get server version
    # pulp issue https://pulp.plan.io/issues/1520
    if Gem::Version.new('2.8.0') > Gem::Version.new(version)
      def export_distributor.config
        to_ret = as_json
        to_ret.delete('auto_publish')
        to_ret.delete('id')
        to_ret.delete('relative_url')
        to_ret
      end
    end
    response = client.extensions.repository.create_with_importer_and_distributors(repo_id, importer, [yum_distributor, export_distributor], optional)
    code=response.code
    body=response.body
    puts "code #{code}"
    case code
    when 201
      return true
    default
      raise "Operation failed, response code:#{code}, #{response}"
    end
  rescue Exception => e
    raise "Failed to create repo, #{e.message}"
  end
end
delete_puppet_newer!(forge_id, author, name, version, auto_publish=false) click to toggle source
# File lib/pulphelper/repo.rb, line 322
def delete_puppet_newer!(forge_id, author, name, version, auto_publish=false)
  #/pulp/api/v2/repositories/<repo_id>/actions/unassociate/
  criteria = get_puppet_unit_assoc_criteria(author, name, version)
  begin
    unassociate_response=client.resources.repository.unassociate_units(forge_id, criteria)
    #pulp api documented response code
    if unassociate_response.code !=202
      raise "Exception: cannot unassociate unit, response code: #{unassociate_response.code}"
    end
    if auto_publish
      publish_response=client.extensions.repository.publish_all(forge_id)

      presponse=JSON.parse(publish_response.to_json)
      if presponse.nil? || presponse[0]["spawned_tasks"].length<1
        raise "Exception: repo publish requeste failed, response : #{publish_response}"
      end
      # if publish_response.code !=202 && !"#{publish_response.code}".start_with?("20")
      #    raise "Exception: repo publish requeste failed, response code : #{publish_response.code}"
      # end
    end
  rescue StandardError => e
    raise "Exception: Error delete module newer than #{author}-#{name}-#{version} from repo #{forge_id}: #{e.message}"
  end
end
delete_repository(repo_id) click to toggle source
# File lib/pulphelper/repo.rb, line 377
def delete_repository(repo_id)
  begin
    response=client.resources.repository.delete(repo_id)
    case response.code
    when 202
      true
    when 404
      rase "Repository does not exist."
    default
      raise "Failed to delete repository."
    end
  rescue StandardError => e
    raise "Excpetion: Failed to delete, #{e.message}"
  end
end
delete_rpm_newer!(forge_id, name, version, release, arch, auto_publish=false) click to toggle source
# File lib/pulphelper/repo.rb, line 271
def delete_rpm_newer!(forge_id, name, version, release, arch, auto_publish=false)
  criteria = get_rpm_unit_ass_criteria(name, version, release, arch)
  begin
    unassociate_response=client.resources.repository.unassociate_units(forge_id, criteria)
    #pulp api documented response code
    if unassociate_response.code !=202
      raise "Exception: cannot unassociate unit, response code: #{unassociate_response.code}"
    end
    if auto_publish
      publish_response=client.extensions.repository.publish_all(forge_id)
      presponse=JSON.parse(publish_response.to_json)
      if presponse.nil? || presponse[0]["spawned_tasks"].length<1
        raise "Exception: repo publish requeste failed, response : #{publish_response}"
      end

      # if publish_response.code !=202 && !"#{publish_response.code}".start_with?("20")
      #    raise "Exception: repo publish requeste failed, response code : #{publish_response.code}"
      # end
    end
  rescue StandardError => e
    raise "Error delete rpm pakcage older than #{name}-#{version}-#{release} from repo #{forge_id}: #{e.message}"
  end
end
get_repo(repo_id) click to toggle source

puppet repo

distributors:
 distributor_type_id: "puppet_distributor"
   auto_publish:
   last_publish
   config :
importers:
  importer_type_id: "puppet_importer"
# File lib/pulphelper/repo.rb, line 71
def get_repo(repo_id)
  response=client.extensions.repository.retrieve_with_details(repo_id)
  code=response.code
  body=response.body
  case code
  when 200
    repo=JSON.parse(body.to_json)
    type = repo["notes"]["_repo-type"]
    #puts repos
    repo_data=nil
    case type
      when REPO_TYPE_RPM
        yum_distributor = repo["distributors"].select{ |d| d["distributor_type_id"] == 'yum_distributor'}[0]
        yum_importer = repo["distributors"].select{ |d| d["distributor_type_id"] == 'yum_importer'}[0]
        distributor = nil
        if  yum_distributor
          distributor = {
            :auto_publish => yum_distributor["auto_publish"],
            :last_publish => yum_distributor["last_publish"],
            :config => yum_distributor["config"]
          }
        end
        importer = nil
        if yum_importer
          importer = {
            :last_sync => yum_importer["last_sync"],
            :config => yum_importer["config"]
          }
        end

        repo_data={
          :id => repo["id"],
          :name => repo["display_name"],
          :description => repo["description"],
          :content_unit_counts => repo["content_unit_counts"],
          :type => REPO_TYPE_RPM,
          :last_unit_removed => repo["last_unit_removed"],
          :last_unit_added => repo["last_unit_added"],
          :distributor => distributor,
          :importer => importer,
        }
        #puts repos
      when REPO_TYPE_PUPPET
        puppet_distributor = repo["distributors"].select{ |d| d["distributor_type_id"] == 'puppet_distributor'}[0]
        distributor =nil
        if puppet_distributor
          distributor = {
            :auto_publish => puppet_distributor["auto_publish"],
            :last_publish => puppet_distributor["last_publish"],
            :config => puppet_distributor["config"]
          }
        end
        repo_data={
          :id => repo["id"],
          :name => repo["display_name"],
          :description => repo["description"],
          :content_unit_counts => repo["content_unit_counts"],
          :type => REPO_TYPE_PUPPET,
          :last_unit_removed => repo["last_unit_removed"],
          :last_unit_added => repo["last_unit_added"],
          :distributor => distributor
        }
      else
      end
      repo_data
  else
    raise "Exception: cannot get repository detail: response code :#{code}"
  end
end
list_repo(type) click to toggle source
# File lib/pulphelper/repo.rb, line 16
def list_repo(type)
  # use $in seems buggy
  criteria = {
    "filters" => {
      "notes._repo-type" => type
    }
  }
  #puts "criteria:#{criteria}"
  response=client.resources.repository.search(criteria)
  code=response.code
  body=response.body
  #puts "list result: #{body}"
  result=[]
  case code
  when 200
    repos=JSON.parse(body.to_json)
    #puts repos
    repos.each do |repo|
        repo_data={
          :id => repo["id"],
          :name => repo["display_name"],
          :description => repo["description"]
        }
        #puts repos
        result << repo_data
    end
  else
    raise "Exception: cannot list repository: response code :#{code}"
  end
  return result
end
publish_repo!(forge_id) click to toggle source
# File lib/pulphelper/repo.rb, line 218
def publish_repo!(forge_id)
  message = "Publish #{forge_id} submitted successfully"
  begin
    publish_response=client.extensions.repository.publish_all(forge_id)

    ##
    ## Runcilbe does not proper include respone code
    ##
    presponse=JSON.parse(publish_response.to_json)
    if presponse.nil? || presponse[0]["spawned_tasks"].length<1
      raise "Exception: repo publish requeste failed, response : #{publish_response}"
    end
    publish_response
  rescue StandardError => e
    raise "Excpetion: Failed to publish, #{e.message}"
  end
end
sync_repo!(forge_id) click to toggle source
# File lib/pulphelper/repo.rb, line 236
def sync_repo!(forge_id)
  begin
    sync_response=client.extensions.repository.sync(forge_id)
    ##
    ## Runcilbe does not proper include respone code
    ##
    if sync_response.code !=201 && !"#{sync_response.code}".start_with?("20")
      if sync_response.code == 404
        raise "repository #{forge_id} not found"
      end
      raise "Sync #{forge_id} failed with http response code: #{sync_response.code}"
    end
    sync_response
  rescue StandardError => e
    raise "Excpetion: Failed to Sync, #{e.message}"
  end
end
sync_status(forge_id) click to toggle source
# File lib/pulphelper/repo.rb, line 254
def sync_status(forge_id)
  begin
    sync_response=client.extensions.repository.sync_status(forge_id)
    ##
    ## Runcilbe does not proper include respone code
    ##
    if sync_response.code !=200 && !"#{sync_response.code}".start_with?("20")
      if sync_response.code == 404
        raise "repository #{forge_id} not found"
      end
      raise "Sync #{forge_id} failed with http response code: #{sync_response.code}"
    end
  rescue StandardError => e
    raise "Excpetion: Failed to Sync, #{e.message}"
  end
end

Private Instance Methods

get_puppet_search_params(author, name, version) click to toggle source
# File lib/pulphelper/repo.rb, line 444
def get_puppet_search_params(author, name, version)
  search_optional= {
      :include_repos => true
  }
  search_criteria ={
    :filters => {
        :author => author,
        :name => name,
        :version=> version
    },
    :sort => [["version", "descending"]],
    :limit => 1,
    :fields => ["author", "name", "version"]
  }

  search_params={
    :criteria =>  search_criteria,
    :optional => search_optional,
  }

  return search_params
end
get_puppet_unit_assoc_criteria(author, name, version) click to toggle source
# File lib/pulphelper/repo.rb, line 466
def get_puppet_unit_assoc_criteria(author, name, version)
   criteria= {
    :filters => {
        :unit => {
          '$and' => [
              {:name => name},
              {:author => author},
              :version=> {
                '$gt' => version
              }
          ]
        }
    },
    :type_ids => ["puppet_module"]
  }
  return criteria
end
get_rpm_search_params(name, version, release, arch) click to toggle source

private functions #####

# File lib/pulphelper/repo.rb, line 395
def get_rpm_search_params (name, version, release, arch)
  search_optional= {
      :include_repos => true
  }
  search_criteria ={
    :filters => {
        :name => name,
        :version=> version,
        :release => release,
        :arch => arch,
    },
    :sort => [["version", "descending"]],
    :limit => 1,
    :fields => ["name", "version", "release", "arch"]
  }

  search_params= {
    :optional => search_optional,
    :criteria => search_criteria,
  }

  return search_params
end
get_rpm_unit_ass_criteria(name, version, release, arch) click to toggle source
# File lib/pulphelper/repo.rb, line 419
def get_rpm_unit_ass_criteria(name, version, release, arch)
   #/pulp/api/v2/repositories/<repo_id>/actions/unassociate/
  criteria= {
    :filters => {
        :unit => {
          '$and' => [
              {:name => name},
              {:arch => arch},
              '$or' => [
                :version=> {
                  '$gt' => version
                },
                :release => {
                  '$gt' => release
                }
              ]
          ]
        }
    },
    :type_ids => ["rpm"]
  }

  return criteria
end