-
Notifications
You must be signed in to change notification settings - Fork 37
Add cloud-connector as a native foremanctl feature #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jeremylenz
wants to merge
18
commits into
theforeman:master
Choose a base branch
from
jeremylenz:cloud-connector-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+408
−0
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
640932c
Add cloud-connector as a native foremanctl feature
jeremylenz 40ae75b
Add early pre-checks for cloud-connector feature
jeremylenz fd1892e
Fix SSL cert verification for Foreman API call
jeremylenz 4f1c3bb
Call announce_to_sources after setting rhc_instance_id
jeremylenz b1f41f1
Fix TLS verification for cloud connector worker
jeremylenz fd45777
Enable automatic inventory upload during cloud-connector setup
jeremylenz 8be79b5
Add consumer certificate pre-check for cloud-connector
jeremylenz f679cdc
Address code review feedback
jeremylenz 02d2c51
Address code review feedback
jeremylenz 5e332f3
Use dedicated service user for cloud connector worker
jeremylenz 07f6e93
Rename cloud_connector_user/password to cloud_connector_admin_*
jeremylenz 7e0a611
Address code review feedback
jeremylenz 5b6891c
Fix service user login name in defaults
jeremylenz 93edc70
Make CA certificate path optional in cloud_connector role
jeremylenz 34cd0df
Remove foreman-protector disable_plugin from package install
jeremylenz 014cc7e
Address code review feedback
jeremylenz 9b8b99d
Switch cloud connector API auth from admin credentials to OAuth
jeremylenz 4eb0858
Add library symlink so ansible-lint resolves foremanctl_api module
jeremylenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,140 @@ | ||
| from __future__ import absolute_import, division, print_function | ||
| __metaclass__ = type | ||
|
|
||
| DOCUMENTATION = ''' | ||
| --- | ||
| module: foremanctl_api | ||
| short_description: Make authenticated Foreman API calls using OAuth1 | ||
| description: | ||
| - Make HTTP requests to the Foreman API using OAuth1 authentication. | ||
| - Useful for one-off API calls where no dedicated Foreman Ansible Module exists. | ||
| options: | ||
| server_url: | ||
| description: Foreman server URL | ||
| required: true | ||
| type: str | ||
| oauth1_consumer_key: | ||
| description: OAuth1 consumer key | ||
| required: true | ||
| type: str | ||
| oauth1_consumer_secret: | ||
| description: OAuth1 consumer secret | ||
| required: true | ||
| type: str | ||
| no_log: true | ||
| endpoint: | ||
| description: API endpoint path (e.g. /api/v2/rh_cloud/announce_to_sources) | ||
| required: true | ||
| type: str | ||
| method: | ||
| description: HTTP method | ||
| default: GET | ||
| type: str | ||
| choices: [GET, POST, PUT, DELETE, PATCH] | ||
| body: | ||
| description: Request body (sent as JSON) | ||
| type: dict | ||
| ca_path: | ||
| description: Path to CA certificate for SSL verification | ||
| type: str | ||
| status_code: | ||
| description: List of acceptable HTTP status codes | ||
| default: [200] | ||
| type: list | ||
| elements: int | ||
| ''' | ||
|
|
||
| EXAMPLES = ''' | ||
| - name: Announce to Sources | ||
| foremanctl_api: | ||
| server_url: "https://foreman.example.com" | ||
| oauth1_consumer_key: "{{ foreman_oauth_consumer_key }}" | ||
| oauth1_consumer_secret: "{{ foreman_oauth_consumer_secret }}" | ||
| endpoint: /api/v2/rh_cloud/announce_to_sources | ||
| method: POST | ||
| status_code: [200, 201] | ||
| ''' | ||
|
|
||
| import json | ||
|
|
||
| from ansible.module_utils.basic import AnsibleModule | ||
|
|
||
| try: | ||
| import requests | ||
| from requests_oauthlib import OAuth1 | ||
| HAS_DEPS = True | ||
| except ImportError: | ||
| HAS_DEPS = False | ||
|
|
||
|
|
||
| def run_module(): | ||
| module = AnsibleModule( | ||
| argument_spec=dict( | ||
| server_url=dict(required=True, type='str'), | ||
| oauth1_consumer_key=dict(required=True, type='str'), | ||
| oauth1_consumer_secret=dict(required=True, type='str', no_log=True), | ||
| endpoint=dict(required=True, type='str'), | ||
| method=dict(default='GET', type='str', choices=['GET', 'POST', 'PUT', 'DELETE', 'PATCH']), | ||
| body=dict(type='dict'), | ||
| ca_path=dict(type='str'), | ||
| status_code=dict(default=[200], type='list', elements='int'), | ||
| ), | ||
| supports_check_mode=True, | ||
| ) | ||
|
|
||
| if not HAS_DEPS: | ||
| module.fail_json(msg='requests and requests-oauthlib are required for this module') | ||
|
|
||
| url = module.params['server_url'].rstrip('/') + module.params['endpoint'] | ||
| method = module.params['method'] | ||
| body = module.params['body'] | ||
| ca_path = module.params['ca_path'] | ||
| expected_status = module.params['status_code'] | ||
|
|
||
| auth = OAuth1( | ||
| module.params['oauth1_consumer_key'], | ||
| client_secret=module.params['oauth1_consumer_secret'], | ||
| ) | ||
|
|
||
| headers = {'Content-Type': 'application/json'} | ||
|
|
||
| try: | ||
| response = requests.request( | ||
| method=method, | ||
| url=url, | ||
| auth=auth, | ||
| headers=headers, | ||
| json=body, | ||
| verify=ca_path if ca_path else True, | ||
| ) | ||
| except requests.exceptions.RequestException as e: | ||
| module.fail_json(msg=f'Request failed: {e}', url=url) | ||
|
|
||
| response_body = None | ||
| try: | ||
| response_body = response.json() | ||
| except (json.JSONDecodeError, ValueError): | ||
| response_body = response.text | ||
|
|
||
| if response.status_code not in expected_status: | ||
| module.fail_json( | ||
| msg=f'Unexpected status code {response.status_code} (expected {expected_status})', | ||
| url=url, | ||
| status_code=response.status_code, | ||
| body=response_body, | ||
| ) | ||
|
|
||
| module.exit_json( | ||
| changed=method != 'GET', | ||
| url=url, | ||
| status_code=response.status_code, | ||
| body=response_body, | ||
| ) | ||
|
|
||
|
|
||
| def main(): | ||
| run_module() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or 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,21 @@ | ||
| --- | ||
| - name: Verify cloud-connector is not used with iop | ||
| ansible.builtin.assert: | ||
| that: | ||
| - "'iop' not in enabled_features" | ||
| fail_msg: >- | ||
| The cloud-connector feature cannot be used together with the iop feature. | ||
| Remove one of them with --remove-feature before deploying. | ||
|
|
||
| - name: Check that consumer certificate exists | ||
| ansible.builtin.stat: | ||
| path: /etc/pki/consumer/cert.pem | ||
| register: check_cloud_connector_consumer_cert | ||
|
|
||
| - name: Verify consumer certificate exists | ||
| ansible.builtin.assert: | ||
| that: | ||
| - check_cloud_connector_consumer_cert.stat.exists | ||
| fail_msg: >- | ||
| /etc/pki/consumer/cert.pem not found. | ||
| The system must be registered with subscription-manager. |
This file contains hidden or 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 hidden or 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,5 @@ | ||
| --- | ||
| cloud_connector_url: "https://{{ ansible_facts['fqdn'] }}" | ||
| cloud_connector_service_user: cloud_connector_user | ||
| cloud_connector_service_password: changeme # noqa: no-static-secrets | ||
| cloud_connector_config_file: /etc/rhc/workers/foreman_rh_cloud.toml |
This file contains hidden or 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 @@ | ||
| --- | ||
| - name: Update system CA trust | ||
| ansible.builtin.command: update-ca-trust | ||
| changed_when: true # noqa: no-changed-when | ||
| listen: Foreman CA changed | ||
|
|
||
| - name: Restart rhcd | ||
| ansible.builtin.systemd_service: | ||
| name: rhcd | ||
| state: restarted | ||
| daemon_reload: true | ||
|
jeremylenz marked this conversation as resolved.
|
||
| listen: Foreman CA changed | ||
This file contains hidden or 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 @@ | ||
| ../../plugins/modules |
This file contains hidden or 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,17 @@ | ||
| --- | ||
| - name: Create systemd drop-in directory for rhcd | ||
| ansible.builtin.file: | ||
| state: directory | ||
| path: /etc/systemd/system/rhcd.service.d | ||
| owner: root | ||
| group: root | ||
| mode: '0755' | ||
|
|
||
| - name: Deploy HTTP proxy systemd drop-in for rhcd | ||
| ansible.builtin.template: | ||
| src: proxy.conf.j2 | ||
| dest: /etc/systemd/system/rhcd.service.d/proxy.conf | ||
| owner: root | ||
| group: root | ||
| mode: '0640' | ||
| notify: Restart rhcd | ||
|
jeremylenz marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the
has_featurefilter.