| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import absolute_import, division, print_function |
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
|
| 23 |
DOCUMENTATION = ''' |
| 24 |
--- |
| 25 |
module: content_upload |
| 26 |
version_added: 1.0.0 |
| 27 |
short_description: Upload content to a repository |
| 28 |
description: |
| 29 |
- Allows the upload of content to a repository |
| 30 |
author: "Eric D Helms (@ehelms)" |
| 31 |
requirements: |
| 32 |
- python-debian (For deb Package upload) |
| 33 |
- rpm (For rpm upload) |
| 34 |
options: |
| 35 |
src: |
| 36 |
description: |
| 37 |
- File (on the remote/target machine) to upload |
| 38 |
required: true |
| 39 |
type: path |
| 40 |
aliases: |
| 41 |
- file |
| 42 |
repository: |
| 43 |
description: |
| 44 |
- Repository to upload file in to |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
product: |
| 48 |
description: |
| 49 |
- Product to which the repository lives in |
| 50 |
required: true |
| 51 |
type: str |
| 52 |
ostree_repository_name: |
| 53 |
description: |
| 54 |
- Name of repository within the OSTree archive. |
| 55 |
- Required for OSTree uploads. |
| 56 |
required: false |
| 57 |
type: str |
| 58 |
notes: |
| 59 |
- Currently only uploading to deb, RPM, OSTree & file repositories is supported |
| 60 |
- For anything but file repositories, a supporting library must be installed. See Requirements. |
| 61 |
- OSTree content upload is not idempotent - running mutliple times will attempt to upload the content unit. |
| 62 |
extends_documentation_fragment: |
| 63 |
- theforeman.foreman.foreman |
| 64 |
- theforeman.foreman.foreman.organization |
| 65 |
''' |
| 66 |
|
| 67 |
EXAMPLES = ''' |
| 68 |
- name: "Upload my.rpm" |
| 69 |
theforeman.foreman.content_upload: |
| 70 |
username: "admin" |
| 71 |
password: "changeme" |
| 72 |
server_url: "https://foreman.example.com" |
| 73 |
src: "my.rpm" |
| 74 |
repository: "Build RPMs" |
| 75 |
product: "My Product" |
| 76 |
organization: "Default Organization" |
| 77 |
|
| 78 |
- name: "Upload ostree-archive.tar" |
| 79 |
theforeman.foreman.content_upload: |
| 80 |
username: "admin" |
| 81 |
password: "changeme" |
| 82 |
server_url: "https://foreman.example.com" |
| 83 |
src: "ostree_archive.tar" |
| 84 |
repository: "My OStree Repository" |
| 85 |
product: "My Product" |
| 86 |
organization: "Default Organization" |
| 87 |
ostree_repository_name: "small" |
| 88 |
''' |
| 89 |
|
| 90 |
RETURN = ''' # ''' |
| 91 |
|
| 92 |
import os |
| 93 |
import traceback |
| 94 |
|
| 95 |
from ansible.module_utils.common.text.converters import to_bytes, to_native |
| 96 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import KatelloAnsibleModule, missing_required_lib |
| 97 |
|
| 98 |
try: |
| 99 |
from debian import debfile |
| 100 |
HAS_DEBFILE = True |
| 101 |
DEBFILE_IMP_ERR = None |
| 102 |
except ImportError: |
| 103 |
HAS_DEBFILE = False |
| 104 |
DEBFILE_IMP_ERR = traceback.format_exc() |
| 105 |
|
| 106 |
HAS_RPM_LIB = False |
| 107 |
HAS_RPM_BIN = os.path.exists('/usr/bin/rpm') |
| 108 |
try: |
| 109 |
import rpm |
| 110 |
HAS_RPM_LIB = True |
| 111 |
RPM_IMP_ERR = None |
| 112 |
except ImportError: |
| 113 |
RPM_IMP_ERR = traceback.format_exc() |
| 114 |
HAS_RPM = HAS_RPM_LIB or HAS_RPM_BIN |
| 115 |
|
| 116 |
CONTENT_CHUNK_SIZE = 1 * 1024 * 1024 |
| 117 |
|
| 118 |
|
| 119 |
def get_deb_info(path): |
| 120 |
control = debfile.DebFile(path).debcontrol() |
| 121 |
return control['package'], control['version'], control['architecture'] |
| 122 |
|
| 123 |
|
| 124 |
def get_rpm_info(module, path): |
| 125 |
if HAS_RPM_LIB: |
| 126 |
return get_rpm_info_lib(path) |
| 127 |
return get_rpm_info_bin(module, path) |
| 128 |
|
| 129 |
|
| 130 |
def get_rpm_info_lib(path): |
| 131 |
ts = rpm.TransactionSet() |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
try: |
| 136 |
vsflags = rpm.RPMVSF_MASK_NOSIGNATURES |
| 137 |
except AttributeError: |
| 138 |
vsflags = rpm._RPMVSF_NOSIGNATURES |
| 139 |
ts.setVSFlags(vsflags) |
| 140 |
|
| 141 |
with open(path) as rpmfile: |
| 142 |
rpmhdr = ts.hdrFromFdno(rpmfile) |
| 143 |
|
| 144 |
epoch = rpmhdr[rpm.RPMTAG_EPOCHNUM] |
| 145 |
name = to_native(rpmhdr[rpm.RPMTAG_NAME]) |
| 146 |
version = to_native(rpmhdr[rpm.RPMTAG_VERSION]) |
| 147 |
release = to_native(rpmhdr[rpm.RPMTAG_RELEASE]) |
| 148 |
arch = to_native(rpmhdr[rpm.RPMTAG_ARCH]) |
| 149 |
if arch == 'noarch' and rpmhdr[rpm.RPMTAG_SOURCEPACKAGE] == 1: |
| 150 |
arch = 'src' |
| 151 |
|
| 152 |
return (name, epoch, version, release, arch) |
| 153 |
|
| 154 |
|
| 155 |
def get_rpm_info_bin(module, path): |
| 156 |
_rc, rpmresult, _rpmerr = module.run_command( |
| 157 |
['rpm', '--nosignature', '--queryformat', '%{NAME} %{EPOCHNUM} %{VERSION} %{RELEASE} %{ARCH} %{SOURCEPACKAGE}', |
| 158 |
'-qp', path], check_rc=True) |
| 159 |
|
| 160 |
name, epoch, version, release, arch, sourcepackage = rpmresult.split() |
| 161 |
|
| 162 |
if arch == 'noarch' and sourcepackage == '1': |
| 163 |
arch = 'src' |
| 164 |
|
| 165 |
return (name, epoch, version, release, arch) |
| 166 |
|
| 167 |
|
| 168 |
def main(): |
| 169 |
module = KatelloAnsibleModule( |
| 170 |
foreman_spec=dict( |
| 171 |
src=dict(required=True, type='path', aliases=['file']), |
| 172 |
repository=dict(required=True, type='entity', scope=['product'], thin=False), |
| 173 |
product=dict(required=True, type='entity', scope=['organization']), |
| 174 |
ostree_repository_name=dict(required=False, type='str'), |
| 175 |
), |
| 176 |
) |
| 177 |
|
| 178 |
with module.api_connection(): |
| 179 |
repository_scope = module.scope_for('repository') |
| 180 |
|
| 181 |
b_src = to_bytes(module.foreman_params['src']) |
| 182 |
filename = os.path.basename(module.foreman_params['src']) |
| 183 |
|
| 184 |
checksum = module.sha256(module.foreman_params['src']) |
| 185 |
|
| 186 |
content_unit = None |
| 187 |
if module.foreman_params['repository']['content_type'] == 'deb': |
| 188 |
if not HAS_DEBFILE: |
| 189 |
module.fail_json(msg=missing_required_lib("python-debian"), exception=DEBFILE_IMP_ERR) |
| 190 |
|
| 191 |
name, version, architecture = get_deb_info(b_src) |
| 192 |
query = 'name = "{0}" and version = "{1}" and architecture = "{2}"'.format(name, version, architecture) |
| 193 |
content_unit = module.find_resource('debs', query, params=repository_scope, failsafe=True) |
| 194 |
elif module.foreman_params['repository']['content_type'] == 'yum': |
| 195 |
if not HAS_RPM: |
| 196 |
module.fail_json(msg=missing_required_lib("rpm"), exception=RPM_IMP_ERR) |
| 197 |
|
| 198 |
name, epoch, version, release, arch = get_rpm_info(module, b_src) |
| 199 |
query = 'name = "{0}" and epoch = "{1}" and version = "{2}" and release = "{3}" and arch = "{4}"'.format(name, epoch, version, release, arch) |
| 200 |
content_unit = module.find_resource('packages', query, params=repository_scope, failsafe=True) |
| 201 |
elif module.foreman_params['repository']['content_type'] == 'file': |
| 202 |
query = 'name = "{0}" and checksum = "{1}"'.format(filename, checksum) |
| 203 |
content_unit = module.find_resource('file_units', query, params=repository_scope, failsafe=True) |
| 204 |
elif module.foreman_params['repository']['content_type'] == 'ostree': |
| 205 |
try: |
| 206 |
ostree_repository_name = module.foreman_params['ostree_repository_name'] |
| 207 |
except KeyError: |
| 208 |
module.fail_json(msg="The 'ostree_repository_name' parameter is required when uploading to OSTree repositories!") |
| 209 |
else: |
| 210 |
|
| 211 |
module.fail_json(msg="Uploading to a {0} repository is not supported yet.".format(module.foreman_params['repository']['content_type'])) |
| 212 |
|
| 213 |
if not content_unit: |
| 214 |
if not module.check_mode: |
| 215 |
size = os.stat(module.foreman_params['src']).st_size |
| 216 |
content_upload_payload = {'size': size} |
| 217 |
content_upload_payload.update(repository_scope) |
| 218 |
|
| 219 |
content_upload = module.resource_action('content_uploads', 'create', content_upload_payload) |
| 220 |
content_upload_scope = {'id': content_upload['upload_id']} |
| 221 |
content_upload_scope.update(repository_scope) |
| 222 |
|
| 223 |
offset = 0 |
| 224 |
|
| 225 |
with open(b_src, 'rb') as contentfile: |
| 226 |
for chunk in iter(lambda: contentfile.read(CONTENT_CHUNK_SIZE), b""): |
| 227 |
data = {'content': chunk, 'offset': offset, 'size': size} |
| 228 |
module.resource_action('content_uploads', 'update', params=content_upload_scope, data=data) |
| 229 |
|
| 230 |
offset += len(chunk) |
| 231 |
|
| 232 |
uploads = [{'id': content_upload['upload_id'], 'name': filename, |
| 233 |
'size': offset, 'checksum': checksum}] |
| 234 |
import_params = {'id': module.foreman_params['repository']['id'], 'uploads': uploads} |
| 235 |
if module.foreman_params['repository']['content_type'] == 'ostree': |
| 236 |
ostree_parameters = {'ostree_repository_name': ostree_repository_name, 'content_type': 'ostree_ref'} |
| 237 |
import_params.update(ostree_parameters) |
| 238 |
|
| 239 |
module.resource_action('repositories', 'import_uploads', import_params) |
| 240 |
|
| 241 |
module.resource_action('content_uploads', 'destroy', content_upload_scope) |
| 242 |
else: |
| 243 |
module.set_changed() |
| 244 |
|
| 245 |
|
| 246 |
if __name__ == '__main__': |
| 247 |
main() |