Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Populate a Satellite 6 with NailGun.
import random
import re
from nailgun import client
from nailgun import entity_mixins
from nailgun.config import ServerConfig
from nailgun.entities import (
ActivationKey,
ContentView,
ContentViewVersion,
DockerComputeResource,
DockerHubContainer,
Domain,
HostCollection,
LibvirtComputeResource,
LifecycleEnvironment,
Location,
Organization,
Product,
Repository,
SmartProxy,
Subnet,
)
entity_mixins.CREATE_MISSING = True
MANIFEST_FILE = '/Users/omaciel/Downloads/latest_manifest.zip'
RHEL_7_RPMS = {
'product': u'Red Hat Enterprise Linux Server',
'repo': u'Red Hat Enterprise Linux 7 Server (RPMs)',
'arch': u'x86_64',
'release': u'7Server',
}
RHEL_7_KICKSTART = {
'product': u'Red Hat Enterprise Linux Server',
'repo': u'Red Hat Enterprise Linux 7 Server (Kickstart)',
'arch': u'x86_64',
'release': u'7.1',
}
RHEL_7_TOOLS = {
'product': u'Red Hat Enterprise Linux Server',
'repo': u'Red Hat Satellite Tools 6 Beta (for RHEL 7 Server) (RPMs)',
'arch': u'x86_64',
'release': u'7Server',
}
RHEL_7_CAPSULE = {
'product': u'Red Hat Satellite Capsule Beta',
'repo': u'Red Hat Satellite Capsule 6 Beta (for RHEL 7 Server) (RPMs)',
'arch': u'x86_64',
'release': u'7Server',
}
SATELLITE_SUBSCRIPTION = u'Red Hat Satellite Employee Subscription'
def enable_rh_repository(org, prod_set):
"""Enables a Red Hat Repository Set.
Returns the repository entity.
"""
# Fetch product ID for 'Red Hat Satellite Capsule'
product_id = Product().fetch_rhproduct_id(prod_set['product'], org.id)
product = Product(id=product_id)
# Fetch the RepoSet ID for ''
reposet_id = product.fetch_reposet_id(name=prod_set['repo'])
# Enable the repository
assert product.enable_rhrepo(
base_arch=prod_set['arch'],
release_ver=prod_set['release'],
reposet_id=reposet_id,
)['result'] == u'success'
# Fetch the repository ID by using its name w/o parenthesis
# e.g.
# Red Hat Enterprise Linux 7 Server RPMs x86_64 7Server
# instead of
# Red Hat Enterprise Linux 7 Server (RPMs) x86_64 7Server
name = re.sub(
r'(\(|\))',
'',
u'{repo} {arch} {release}'.format(**prod_set)
)
return Repository(id=Repository().fetch_repoid(org.id, name)).read()
# If you already have a nailgun server config file
# server_config = ServerConfig.get('default')
# else
server_config = ServerConfig(
'https://YOUR-SATELLITE-6-FQDN',
auth=('admin', 'changeme'),
verify=False
)
server_config.save()
# Organization
org = Organization().search(
query={'search': 'name="Default Organization"'},
)[0]
# Create Lifecycle Environments
dev_lce = LifecycleEnvironment(name='DEV', organization=org).create()
qe_lce = LifecycleEnvironment(
name='QE',
organization=org,
prior=dev_lce,
).create()
# Location
loc = Location().search(
query={'search': 'name="Default Location"'},
)[0]
# Get the default domain...
domain_name = '.'.join(server_config.url.split('.')[1:])
domain = Domain().search(
query={'search': 'name="{0}"'.format(domain_name)},
)[0]
# Capsule
capsule = SmartProxy().search(
query={'search': 'name="{0}"'.format(server_config.url[8:])},
)[0]
# Subnet
subnet = Subnet(
name='libvirt_subnet',
domain=[domain],
dns=capsule,
dhcp=capsule,
tftp=capsule,
# discovery=capsule, # We need further work on NailGun
network='192.168.100.0',
mask='255.255.255.0',
location=[loc],
organization=[org],
).create()
# Add domain and subnet to 'Default Organization'.
org.domain = [domain]
org.subnet = [subnet]
org = org.update(['subnet'])
# Add domain, subnet and 'Default Organization' to 'Default Location'.
loc.domain = [domain]
loc.subnet = [subnet]
loc.organization = [org]
loc = loc.update(['domain', 'organization'])
# Associate default capsule to default domain
domain.dns = capsule
domain = domain.update(['dns'])
# Create a Puppet repository
custom_prod = Product(organization=org, name=u'Puppet Modules').create()
puppet_repo = Repository(
name=u'Random Puppet',
product=custom_prod,
content_type=u'puppet',
url=u'https://omaciel.fedorapeople.org/random_puppet/',
).create()
# Sync it
puppet_repo.sync()
# Import a Red Hat manifest file
response = Organization(id=org.id).upload_manifest(path=MANIFEST_FILE)
assert response['result'] == u'success'
# Enable Red Hat repositories
rhel_7_rpms_repo = enable_rh_repository(org, RHEL_7_RPMS)
rhel_7_kickstart_repo = enable_rh_repository(org, RHEL_7_KICKSTART)
rhel_7_tools_repo = enable_rh_repository(org, RHEL_7_TOOLS)
rhel_7_capsule_repo = enable_rh_repository(org, RHEL_7_CAPSULE)
# Sync them
try:
old_task_timeout = entity_mixins.TASK_TIMEOUT
entity_mixins.TASK_TIMEOUT = 3600
for repo in [
rhel_7_rpms_repo,
rhel_7_kickstart_repo,
rhel_7_tools_repo,
rhel_7_capsule_repo,
]:
response = Repository(id=repo.id).sync()
assert response['result'] == u'success'
finally:
entity_mixins.TASK_TIMEOUT = old_task_timeout
# Create content view for RHEL 7
rhel_7_cv = ContentView(
organization=org,
name=u'RHEL 7 CV',
repository=[
rhel_7_rpms_repo,
rhel_7_kickstart_repo,
rhel_7_tools_repo
]).create()
# Add a random puppet module
puppet_mod = random.choice(rhel_7_cv.available_puppet_modules()['results'])
rhel_7_cv.add_puppet_module(puppet_mod['author'], puppet_mod['name'])
# Publish it
rhel_7_cv.publish()
rhel_7_cv = rhel_7_cv.read()
# Fetch version ID for published content view
cvv_id = ContentView(id=rhel_7_cv.id).read_json()['versions']
assert len(cvv_id) == 1
cvv_id = cvv_id[0]
# Promote it to 'DEV'
response = ContentViewVersion(id=cvv_id['id']).promote(dev_lce.id)
assert response['result'] == u'success'
# Create host collection
rhel_7_hc = HostCollection(
organization=org,
name=u'RHEL 7 Systems',
max_content_hosts=5,
).create()
# Create activation key
rhel_7_ak = ActivationKey(
name=u'ak-rhel-7',
organization=org,
content_view=rhel_7_cv,
environment=dev_lce.id,
host_collection=[rhel_7_hc],
).create()
# Fetch available subscriptions
subscriptions = org.subscriptions()
assert len(subscriptions) > 0
# Add subscriptions to activation key
for subscription in subscriptions:
# Skip the Satellite repository
if subscription['product_name'] != SATELLITE_SUBSCRIPTION:
params = {u'subscription_id': subscription['id']}
rhel_7_ak.add_subscriptions(params)
# Create a 'libvirt' compute resurce
libvirt_cr = LibvirtComputeResource(
name=u'local_libvirt',
location=[loc],
organization=[org],
set_console_password=True,
url=u'qemu+tcp://localhost:16509/system',
display_type=u'VNC',
).create()
# Docker-related
docker_prod = Product(organization=org, name=u'Docker Containers').create()
docker_repo = Repository(
name=u'Docker CentOS',
docker_upstream_name=u'centos',
product=docker_prod,
content_type=u'docker',
url=u'https://registry.hub.docker.com',
unprotected=True,
).create()
# Sync it
docker_repo.sync()
# Create a 'docker' compute resource
docker_cr = DockerComputeResource(
name=u'local_docker',
location=[loc],
organization=[org],
url=u'http://localhost:2375',
).create()
# TODO: create new nailgun.entities to allow creation of container
# from managed repositories
# Docker container from Docker Hub
busybox = DockerHubContainer(
compute_resource=docker_cr,
repository_name=u'busybox',
name=u'docker-busybox',
command=u'top',
).create(True)
@Ichimonji10

This comment has been minimized.

Copy link

commented Jun 24, 2015

See an updated version here and a diff here.

@omaciel

This comment has been minimized.

Copy link
Owner Author

commented Jun 27, 2015

Thank you @Ichimonji10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.