Skip to content

Commit

Permalink
Add support for self-hosted AI server
Browse files Browse the repository at this point in the history
Signed-off-by: Rabin Yasharzadehe <[email protected]>
  • Loading branch information
rabin-io committed May 20, 2024
1 parent 8c44141 commit 9a19c01
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 63 deletions.
4 changes: 4 additions & 0 deletions plugins/module_utils/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MANIFESTS = 'manifests'
ACTIONS_INSTALL = 'actions/install'
REGISTER_CLUSTER = 'clusters'
REGISTER_INFRASTRUCTURE = 'infra-envs'
2 changes: 2 additions & 0 deletions plugins/module_utils/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ai_api_endpoint = 'https://api.openshift.com/api/assisted-install/v2'
validate_certificate = True
39 changes: 31 additions & 8 deletions plugins/modules/create_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import access_token
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import api
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import defaults

DOCUMENTATION = r'''
---
Expand All @@ -21,6 +23,14 @@
description: Creates a new OpenShift cluster definition using Assisted Installer
options:
ai_api_endpoint:
description: The AI Endpoint
required: false
type: str
validate_certificate:
description: validate the API certificate
required: false
type: bool
name:
description: Name of the cluster
required: true
Expand Down Expand Up @@ -171,6 +181,8 @@
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
ai_api_endpoint=dict(type='str', required=False, default=defaults.ai_api_endpoint),
validate_certificate=dict(type='bool', required=False, default=defaults.validate_certificate),
name=dict(type='str', required=True),
offline_token=dict(type='str', required=True),
openshift_version=dict(type='str', required=True),
Expand Down Expand Up @@ -215,26 +227,37 @@ def run_module():
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True,

)
response = access_token._get_access_token(module.params['offline_token'])
if response.status_code != 200:
module.fail_json(msg='Error getting access token ', **response.json())

headers = {
"Content-Type": "application/json"
}

if module.params['offline_token'] != 'None':
response = access_token._get_access_token(module.params['offline_token'])
if response.status_code != 200:
module.fail_json(msg='Error getting access token ', **response.json())
headers.update({
"Authorization": "Bearer " + response.json()["access_token"],
})

# if the user is working with this module in only check mode we do not
# want to make any changes to the environment, just return the current
# state with no modifications
if module.check_mode:
module.exit_json(**result)
headers = {
"Authorization": "Bearer " + response.json()["access_token"],
"Content-Type": "application/json"
}

params = module.params.copy()
params.pop("offline_token")
params.pop("ai_api_endpoint")
params.pop("validate_certificate")

if "cluster_id" in params:
params.pop("cluster_id")
params["pull_secret"] = json.loads(params["pull_secret"])
response = session.post(
"https://api.openshift.com/api/assisted-install/v2/clusters",
module.params['ai_api_endpoint'] + '/' + api.REGISTER_CLUSTER,
headers=headers,
json=params
)
Expand Down
46 changes: 35 additions & 11 deletions plugins/modules/create_infra_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import access_token
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import api
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import defaults


DOCUMENTATION = r'''
Expand All @@ -22,12 +24,24 @@
description: Creates a new OpenShift Discovery ISO for Assisted Installer
options:
ai_api_endpoint:
description: The AI Endpoint
required: false
type: str
validate_certificate:
description: validate the API certificate
required: false
type: bool
offline_token:
description: Offline token from console.redhat.com
required: true
type: str
additional_ntp_sources:
description: A comma-separated list of NTP sources (name or IP) going to be added to all the hosts.
required: false
type: str
additional_trust_bundle:
description: PEM-encoded X.509 certificate bundle. Hosts discovered by this infra-env will trust the certificates in this bundle. Clusters formed from the hosts discovered by this infra-env will also trust the certificates in this bundle.
description: PEM-encoded X.509 certificate bundle. Hosts discovered by this infra-env will trust the certificates in this bundle. Clusters formed from the hosts discovered by this infra-env will also trust the certificates in this bundle.
required: false
type: str
cluster_id:
Expand Down Expand Up @@ -100,6 +114,8 @@
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
ai_api_endpoint=dict(type='str', required=False, default=defaults.ai_api_endpoint),
validate_certificate=dict(type='bool', required=False, default=defaults.validate_certificate),
name=dict(type='str', required=True),
offline_token=dict(type='str', required=True),
cluster_id=dict(type='str', required=True),
Expand Down Expand Up @@ -138,27 +154,35 @@ def run_module():
supports_check_mode=True
)

response = access_token._get_access_token(module.params['offline_token'])
if response.status_code != 200:
module.fail_json(msg='Error getting access token ', **response.json())
headers = {
"Content-Type": "application/json"
}

if module.params['offline_token'] != 'None' :
response = access_token._get_access_token(module.params['offline_token'])
if response.status_code != 200:
module.fail_json(msg='Error getting access token ', **response.json())

result['access_token'] = response.json()["access_token"]
headers.update({
"Authorization": "Bearer " + response.json()["access_token"],
})


# if the user is working with this module in only check mode we do not
# want to make any changes to the environment, just return the current
# state with no modifications
if module.check_mode:
module.exit_json(**result)

result['access_token'] = response.json()["access_token"]

headers = {
"Authorization": "Bearer " + response.json()["access_token"],
"Content-Type": "application/json"
}
params = module.params.copy()
params.pop("offline_token")
params.pop("ai_api_endpoint")
params.pop("validate_certificate")

params["pull_secret"] = json.loads(params["pull_secret"])
response = session.post(
"https://api.openshift.com/api/assisted-install/v2/infra-envs",
module.params['ai_api_endpoint'] + '/' + api.REGISTER_INFRASTRUCTURE,
headers=headers,
json=params
)
Expand Down
37 changes: 29 additions & 8 deletions plugins/modules/download_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import access_token
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import api
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import defaults


DOCUMENTATION = r'''
Expand All @@ -22,6 +24,14 @@
description: Downloads credentials relating to the installed/installing cluster.
options:
ai_api_endpoint:
description: The AI Endpoint
required: false
type: str
validate_certificate:
description: validate the API certificate
required: false
type: bool
cluster_id:
description: ID of the cluster
required: true
Expand Down Expand Up @@ -61,6 +71,8 @@
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
ai_api_endpoint=dict(type='str', required=False, default=defaults.ai_api_endpoint),
validate_certificate=dict(type='bool', required=False, default=defaults.validate_certificate),
cluster_id=dict(type='str', required=True),
offline_token=dict(type='str', required=True),
file_name=dict(type='str', required=True),
Expand Down Expand Up @@ -88,22 +100,31 @@ def run_module():
argument_spec=module_args,
supports_check_mode=True
)
response = access_token._get_access_token(module.params['offline_token'])
if response.status_code != 200:
module.fail_json(msg='Error getting access token ', **response.json())
result['access_token'] = response.json()["access_token"]
params = module.params.copy()
params.pop("offline_token")

headers = {
"Authorization": "Bearer " + response.json()["access_token"],
"Content-Type": "application/json"
}

if module.params['offline_token'] != 'None':
response = access_token._get_access_token(module.params['offline_token'])
if response.status_code != 200:
module.fail_json(msg='Error getting access token ', **response.json())
result['access_token'] = response.json()["access_token"]
headers.update({
"Authorization": "Bearer " + response.json()["access_token"],
})

params = module.params.copy()
params.pop("offline_token")
params.pop("ai_api_endpoint")
params.pop("validate_certificate")

response = session.get(
"https://api.openshift.com/api/assisted-install/v2/clusters/" + module.params['cluster_id'] + "/downloads/credentials",
f"{module.params['ai_api_endpoint']}/{api.REGISTER_CLUSTER}/{module.params['cluster_id']}/downloads/credentials",
headers=headers,
params=params
)

if "code" in response:
module.fail_json(msg='Request failed: ' + response)

Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/download_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

DOCUMENTATION = r'''
---
module: download_credentials
module: download_files
short_description: Downloads files relating to the installed/installing cluster.
Expand Down
38 changes: 31 additions & 7 deletions plugins/modules/get_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import access_token
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import api
from ansible_collections.rhpds.assisted_installer.plugins.module_utils import defaults

import logging
import logging.handlers
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
my_logger.addHandler(handler)

DOCUMENTATION = r'''
---
Expand All @@ -21,11 +29,18 @@
description: Get the cluster admin credentials.
options:
ai_api_endpoint:
description: The AI Endpoint
required: false
type: str
validate_certificate:
description: validate the API certificate
required: false
type: bool
cluster_id:
description: ID of the cluster
required: true
type: str
offline_token:
description: Offline token from console.redhat.com
required: true
Expand Down Expand Up @@ -54,6 +69,8 @@
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
ai_api_endpoint=dict(type='str', required=False, default=defaults.ai_api_endpoint),
validate_certificate=dict(type='bool', required=False, default=defaults.validate_certificate),
cluster_id=dict(type='str', required=True),
offline_token=dict(type='str', required=True),
)
Expand All @@ -79,20 +96,27 @@ def run_module():
argument_spec=module_args,
supports_check_mode=True
)
response = access_token._get_access_token(module.params['offline_token'])
if response.status_code != 200:
module.fail_json(msg='Error getting access token ', **response.json())
result['access_token'] = response.json()["access_token"]

headers = {
"Authorization": "Bearer " + response.json()["access_token"],
"Content-Type": "application/json"
}

if module.params['offline_token'] != 'None':
response = access_token._get_access_token(module.params['offline_token'])
if response.status_code != 200:
module.fail_json(msg='Error getting access token ', **response.json())
result['access_token'] = response.json()["access_token"]
headers.update({
"Authorization": "Bearer " + response.json()["access_token"],
})

response = session.get(
"https://api.openshift.com/api/assisted-install/v2/clusters/" + module.params['cluster_id'] + "/credentials",
f"{module.params['ai_api_endpoint']}/{api.REGISTER_CLUSTER}/{module.params['cluster_id']}/credentials",
headers=headers,
)

if "code" in response.json():
my_logger.error(response.json())
module.fail_json(msg='Request failed: ' + response)
else:
result['result'] = response.json()
Expand Down
Loading

0 comments on commit 9a19c01

Please sign in to comment.