-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from SUSE/custom-repos
Added custom repositories - Card 4721
- Loading branch information
Showing
28 changed files
with
513 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class CreateRepositoryService | ||
|
||
class InvalidExternalUrl < RuntimeError | ||
end | ||
|
||
def call(product_service, url, attributes, custom: false) | ||
repository = Repository.find_or_initialize_by(external_url: url) | ||
|
||
# TODO: See if we can clean this up | ||
repository.attributes = attributes.select do |k, _| | ||
repository.attributes.keys.member?(k.to_s) && k.to_s != 'id' | ||
end | ||
repository.scc_id = attributes[:id] unless custom | ||
|
||
repository.external_url = url | ||
repository.local_path = Repository.make_local_path(url) | ||
|
||
raise InvalidExternalUrl.new(url) if repository.local_path.to_s == '' | ||
|
||
ActiveRecord::Base.transaction do | ||
repository.save! | ||
|
||
RepositoriesServicesAssociation.find_or_create_by( | ||
service_id: product_service.id, | ||
repository_id: repository.id | ||
) | ||
end | ||
|
||
repository | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AddSccIdToRepositories < ActiveRecord::Migration[5.1] | ||
|
||
def change | ||
add_column :repositories, :scc_id, 'BIGINT UNSIGNED', after: :id | ||
Repository.all.each do |repo| | ||
repo.update(scc_id: repo.id) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module RMT::CLI::ArrayPrintable | ||
|
||
def array_to_table(array, options) | ||
rows = [] | ||
|
||
array.all.each do |element| | ||
rows << options.keys.map { |k| element[k] } | ||
end | ||
Terminal::Table.new headings: options.values, rows: rows | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
class RMT::CLI::CustomRepos < RMT::CLI::Base | ||
|
||
include ::RMT::CLI::ArrayPrintable | ||
|
||
desc 'add URL NAME PRODUCT_ID', 'Add a custom repository to a product' | ||
def add(url, name, product_id) | ||
product = Product.find_by(id: product_id) | ||
|
||
if product.nil? | ||
warn "Cannot find product by id #{product_id}." | ||
return | ||
end | ||
|
||
service = product.service | ||
previous_repository = Repository.by_url(url) | ||
|
||
if previous_repository && !previous_repository.custom? | ||
warn "A non-custom repository by URL \"#{url}\" already exists." | ||
return | ||
end | ||
|
||
begin | ||
create_repository_service.call(service, url, { | ||
name: name, | ||
mirroring_enabled: true, | ||
autorefresh: 1, | ||
enabled: 0 | ||
}, custom: true) | ||
|
||
puts 'Successfully added custom repository.' | ||
rescue CreateRepositoryService::InvalidExternalUrl => e | ||
warn "Invalid URL \"#{e.message}\" provided." | ||
end | ||
end | ||
|
||
desc 'list', 'List all custom repositories' | ||
def list | ||
repositories = Repository.only_custom | ||
|
||
if repositories.empty? | ||
warn 'No custom repositories found.' | ||
else | ||
puts array_to_table(repositories, { | ||
id: 'ID', | ||
name: 'Name', | ||
enabled: 'Mandatory?', | ||
mirroring_enabled: 'Mirror?', | ||
last_mirrored_at: 'Last Mirrored' | ||
}) | ||
end | ||
end | ||
map ls: :list | ||
|
||
desc 'remove ID', 'Remove a custom repository' | ||
def remove(repository_id) | ||
repository = Repository.by_id(repository_id, custom: true) | ||
|
||
if repository.nil? | ||
warn "Cannot find custom repository by id \"#{repository_id}\"." | ||
return | ||
end | ||
|
||
unless repository.destroy | ||
warn 'Cannot remove non-custom repositories.' | ||
return | ||
end | ||
|
||
puts "Removed custom repository by id \"#{repository.id}\"." | ||
end | ||
map rm: :remove | ||
|
||
private | ||
|
||
def create_repository_service | ||
@create_repository_service ||= CreateRepositoryService.new | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.