Last active
December 27, 2015 14:09
-
-
Save jwmatthews/7338574 to your computer and use it in GitHub Desktop.
Sample to work with Candlepin and generate a v3 cert
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
#!/usr/bin/env ruby | |
# | |
# Sample script to use Candlepin to generate Red Hat v3 certificates for unit tests | |
# Using the Candlepin Ruby API from: | |
# https://github.com/candlepin/candlepin/blob/master/client/ruby/candlepin_api.rb | |
# | |
# Requires: | |
# 1) Candlepin git checkout on a peer basis with 'cloude' | |
# 2) A Candlepin server | |
# | |
# | |
# Questions: | |
# 1) How to delete a "consume" | |
# 2) How can I check if something is consumed before I issue the command | |
# 3) To list a certificate do I need to create a Candlepin.new with the cert from the consumer? | |
# | |
CP_SERVER="ec2-50-19-29-175.compute-1.amazonaws.com" | |
CP_USER="admin" | |
CP_PASSWD="admin" | |
require 'securerandom' | |
require 'pp' | |
# | |
# Add the Candlepin Ruby API to the $LOAD_PATH | |
# Assuming that candlepin will checked out on a peer basis with the cloude git repo | |
# | |
cp_dir = File.expand_path("../../../../../../candlepin/client/ruby", File.dirname(__FILE__)) | |
$LOAD_PATH.unshift(cp_dir) unless $LOAD_PATH.include?(cp_dir) | |
require "candlepin_api" | |
def get_cp(username, password, server, port) | |
Candlepin.new(username, password, nil,nil, server, port, nil, nil, | |
false, 'candlepin', true) | |
end | |
CP = get_cp(CP_USER, CP_PASSWD, CP_SERVER, 8443) | |
def ensure_owner_exists(owner_key) | |
begin | |
CP.get_owner(owner_key) | |
rescue RestClient::ResourceNotFound | |
puts "Owner: '#{owner_key}' not found, will create." | |
CP.create_owner(owner_key) | |
end | |
end | |
def ensure_product_exists(id, name) | |
begin | |
CP.get_product(id) | |
rescue RestClient::ResourceNotFound | |
puts "Product: '#{id}' not found, will create." | |
CP.create_product(id, name) | |
end | |
end | |
def ensure_content_exists(name, id, label, type, vendor, url) | |
begin | |
CP.get_content(id) | |
rescue RestClient::BadRequest, RestClient::ResourceNotFound | |
# Note: CP returns a '400' when 'Content' doesn't exist. | |
# Expected to see a 404 returned instead. | |
puts "Content: '#{id}' not found, will create." | |
# Note this is diff than create_product() order is name first, id second | |
CP.create_content(name, id, label, type, vendor, params={:content_url => url}) | |
end | |
end | |
def ensure_subscription_exists(owner_key, prod_id) | |
subs = CP.list_subscriptions(owner_key) | |
desired_subs = subs.select {|x| x["product"]["id"] == prod_id} | |
puts "Out of #{subs.count} subscription(s) for <#{owner_key}>, #{desired_subs.count} subscription(s) matched for product <#{prod_id}>" | |
if not desired_subs.empty? | |
desired_subs.first # return first subscription that matches | |
else | |
puts "Didn't find any matching subscriptions, will create one." | |
CP.create_subscription(owner_key, prod_id) | |
end | |
end | |
def ensure_user_exists(owner, user_name, password, role="test_rhui_role_a") | |
user = nil | |
begin | |
user = CP.get_user_info(user_name) | |
rescue | |
# If a user doesn't exist we get back an empty response which causes this error: | |
# `parse': source did not contain any JSON! (JSON::ParserError) | |
puts "Didn't find a user <#{user_name}, will create one." | |
user = CP.create_user(user_name, password) | |
end | |
end | |
def ensure_role_exists(role_name, owner_key) | |
roles = CP.list_roles | |
desired_roles = roles.select {|x| x["name"] == role_name} | |
if not desired_roles.empty? | |
desired_roles.first | |
else | |
puts "Didn't find a role <#{role_name}, will create one." | |
perms = [{ | |
:owner => { :key => owner_key }, | |
:access => 'ALL' | |
}] | |
CP.create_role(role_name, perms) | |
end | |
end | |
def ensure_user_has_been_added_to_role(user_name, role) | |
users_in_role = role["users"].select {|x| x["username"] == user_name} | |
if users_in_role.empty? | |
puts "Didn't find user <#{user_name}> in role: <#{role["name"]}>, will add user to role." | |
CP.add_role_user(role['id'], user_name) | |
end | |
end | |
def ensure_consumer_is_registered(consumer_name, user_name, owner_key) | |
consumers = CP.list_consumers | |
desired_consumers = consumers.select {|x| x["name"] == consumer_name} | |
if not desired_consumers.empty? | |
desired_consumers.first | |
else | |
puts "Didn't find a consumer <#{consumer_name}> registered, will register one." | |
facts = {'system.certificate_version' => '3.2', 'uname.machine' => 'x86_64'} | |
CP.register(consumer_name, :system, nil, facts, user_name, owner_key) | |
end | |
end | |
def ensure_product_is_consumed(prod_id, consumer) | |
CP.consume_product(prod_id, {:uuid => consumer["uuid"]}) | |
end | |
if __FILE__ == $0 | |
OWNER_KEY = "admin" | |
PROD_ID = "test_product_id_a" | |
PROD_NAME = "test_product_name_a" | |
CONTENT_NAME = "test_content_name_a" | |
CONTENT_ID = "test_content_id_a" | |
CONTENT_LABEL = "test_content_label_a" | |
USER_NAME = "rhui_test_user" | |
USER_PASS = "rhui_test_pass" | |
ROLE_NAME = "rhui_test_role_a" | |
CONSUMER_NAME = "rhui_test_system_a" | |
owner = ensure_owner_exists OWNER_KEY | |
puts "Owner: \n#{owner}" | |
puts | |
product = ensure_product_exists(PROD_ID, PROD_NAME) | |
puts "Product: \n#{product}" | |
puts | |
content = ensure_content_exists(CONTENT_NAME, CONTENT_ID, CONTENT_LABEL, "yum", "Red Hat", "/rhui/test/path/#{CONTENT_LABEL}") | |
puts "Content: \n#{content}" | |
puts | |
CP.add_content_to_product(PROD_ID, CONTENT_ID) | |
sub = ensure_subscription_exists(OWNER_KEY, PROD_ID) | |
puts "Subscription: \n#{sub}" | |
puts | |
refresh_pools = CP.refresh_pools(OWNER_KEY) | |
puts "Refresh Pools: \n#{refresh_pools}" | |
puts | |
user = ensure_user_exists(owner, USER_NAME, USER_PASS) | |
puts "User: \n#{user}" | |
puts | |
role = ensure_role_exists(ROLE_NAME, OWNER_KEY) | |
puts "Role: \n#{role}" | |
puts | |
ensure_user_has_been_added_to_role(user['username'], role) | |
consumer = ensure_consumer_is_registered(CONSUMER_NAME, USER_NAME, OWNER_KEY) | |
puts "Consumer: \n#{consumer}" | |
puts | |
retval = ensure_product_is_consumed(PROD_ID, consumer) | |
puts "Consumed Product: \n#{retval}" | |
puts | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment