-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Added Extension for MaaS integration in CloudStack #11613
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
Open
harikrishna-patnala
wants to merge
11
commits into
apache:main
Choose a base branch
from
shapeblue:ExtensionMaaS
base: main
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.
+302
−9
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
db0391c
Adding extension support for Baremetal MaaS
harikrishna-patnala 9e552f6
Added sql entries
harikrishna-patnala 1f4bbbe
Make distro_series param read from template or VM details
harikrishna-patnala 6d811e9
Fix script
harikrishna-patnala b3ef746
Fix system id usage
harikrishna-patnala 9a53d80
Added redirect url for MaaS instance page
harikrishna-patnala 14d4b74
Ignore 404 during expunge
harikrishna-patnala d00a48a
Add net-setup-method instead of userdata
harikrishna-patnala e54072b
Fix reboot
harikrishna-patnala 666149e
Added os, release, architecture to the parameters
harikrishna-patnala 1a5e86f
use only distro_series
harikrishna-patnala 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
#!/usr/bin/env python3 | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import sys | ||
import json | ||
import time | ||
from requests_oauthlib import OAuth1Session | ||
|
||
|
||
def fail(message): | ||
print(json.dumps({"error": message})) | ||
sys.exit(1) | ||
|
||
|
||
def succeed(data): | ||
print(json.dumps(data)) | ||
sys.exit(0) | ||
|
||
|
||
class MaasManager: | ||
def __init__(self, config_path): | ||
self.config_path = config_path | ||
self.data = self.parse_json() | ||
self.session = self.init_session() | ||
|
||
def parse_json(self): | ||
try: | ||
with open(self.config_path, "r") as f: | ||
json_data = json.load(f) | ||
|
||
extension = json_data.get("externaldetails", {}).get("extension", {}) | ||
host = json_data.get("externaldetails", {}).get("host", {}) | ||
vm = json_data.get("externaldetails", {}).get("virtualmachine", {}) | ||
|
||
endpoint = host.get("endpoint") or extension.get("endpoint") | ||
apikey = host.get("apikey") or extension.get("apikey") | ||
|
||
details = json_data.get("cloudstack.vm.details", {}).get("details", {}) | ||
|
||
os_name = details.get("os") or vm.get("os") | ||
architecture = details.get("architecture") or vm.get("architecture") | ||
distro_series = details.get("distro_series") or vm.get("distro_series") | ||
|
||
if not endpoint or not apikey: | ||
fail("Missing MAAS endpoint or apikey") | ||
|
||
if not endpoint.startswith("http://") and not endpoint.startswith("https://"): | ||
endpoint = "http://" + endpoint | ||
endpoint = endpoint.rstrip("/") | ||
|
||
parts = apikey.split(":") | ||
if len(parts) != 3: | ||
fail("Invalid apikey format. Expected consumer:token:secret") | ||
|
||
consumer, token, secret = parts | ||
|
||
system_id = details.get("maas_system_id") or vm.get("maas_system_id", "") | ||
|
||
vm_name = vm.get("vm_name") or json_data.get("cloudstack.vm.details", {}).get("name") | ||
if not vm_name: | ||
vm_name = f"cs-{system_id}" if system_id else "cs-unknown" | ||
|
||
return { | ||
"endpoint": endpoint, | ||
"consumer": consumer, | ||
"token": token, | ||
"secret": secret, | ||
"distro_series": distro_series or "ubuntu/focal", | ||
"os": os_name, | ||
"architecture": architecture, | ||
"system_id": system_id, | ||
"vm_name": vm_name, | ||
} | ||
except Exception as e: | ||
fail(f"Error parsing JSON: {str(e)}") | ||
|
||
def init_session(self): | ||
return OAuth1Session( | ||
self.data["consumer"], | ||
resource_owner_key=self.data["token"], | ||
resource_owner_secret=self.data["secret"], | ||
) | ||
|
||
def call_maas(self, method, path, data=None, ignore_404=False): | ||
if not path.startswith("/"): | ||
path = "/" + path | ||
url = f"{self.data['endpoint']}:5240/MAAS/api/2.0{path}" | ||
resp = self.session.request(method, url, data=data) | ||
|
||
if resp.status_code == 404 and ignore_404: | ||
return None | ||
|
||
if not resp.ok: | ||
fail(f"MAAS API error: {resp.status_code} {resp.text}") | ||
|
||
try: | ||
return resp.json() if resp.text else {} | ||
except ValueError: | ||
return {} | ||
|
||
def prepare(self): | ||
machines = self.call_maas("GET", "/machines/") | ||
ready = [m for m in machines if m.get("status_name") == "Ready"] | ||
if not ready: | ||
fail("No Ready machines available") | ||
|
||
sysid = self.data.get("system_id") | ||
|
||
if sysid: | ||
match = next((m for m in ready if m["system_id"] == sysid), None) | ||
if not match: | ||
fail(f"Provided system_id '{sysid}' not found among Ready machines") | ||
system = match | ||
else: | ||
system = ready[0] | ||
|
||
system_id = system["system_id"] | ||
mac = system.get("interface_set", [{}])[0].get("mac_address") | ||
hostname = system.get("hostname", "") | ||
|
||
if not mac: | ||
fail("No MAC address found") | ||
|
||
# Load original JSON so we can update nics | ||
with open(self.config_path, "r") as f: | ||
json_data = json.load(f) | ||
|
||
if json_data.get("cloudstack.vm.details", {}).get("nics"): | ||
json_data["cloudstack.vm.details"]["nics"][0]["mac"] = mac | ||
|
||
console_url = f"http://{self.data['endpoint'].replace('http://','').replace('https://','')}:5240/MAAS/r/machine/{system_id}/summary" | ||
|
||
result = { | ||
"nics": json_data["cloudstack.vm.details"]["nics"], | ||
"details": { | ||
"External:mac_address": mac, | ||
"External:maas_system_id": system_id, | ||
"External:hostname": hostname, | ||
"External:console_url": console_url, | ||
}, | ||
} | ||
succeed(result) | ||
|
||
def create(self): | ||
sysid = self.data.get("system_id") | ||
if not sysid: | ||
fail("system_id missing for create") | ||
|
||
ds = self.data.get("distro_series", None) | ||
os_name = self.data.get("os") | ||
arch = self.data.get("architecture") | ||
|
||
deploy_payload = {"op": "deploy"} | ||
|
||
if os_name or arch: | ||
if os_name: | ||
deploy_payload["os"] = os_name | ||
if arch: | ||
deploy_payload["architecture"] = arch | ||
if ds: | ||
deploy_payload["distro_series"] = ds | ||
else: | ||
deploy_payload["distro_series"] = ds or "ubuntu/focal" | ||
|
||
deploy_payload["net-setup-method"] = "curtin" | ||
|
||
self.call_maas("POST", f"/machines/{sysid}/", deploy_payload) | ||
|
||
succeed({"status": "success", "message": "Instance created", "requested": deploy_payload}) | ||
|
||
def delete(self): | ||
sysid = self.data.get("system_id") | ||
if not sysid: | ||
fail("system_id missing for delete") | ||
|
||
self.call_maas("POST", f"/machines/{sysid}/", {"op": "release"}, ignore_404=True) | ||
succeed({"status": "success", "message": f"Instance deleted or not found ({sysid})"}) | ||
|
||
def start(self): | ||
sysid = self.data.get("system_id") | ||
if not sysid: | ||
fail("system_id missing for start") | ||
self.call_maas("POST", f"/machines/{sysid}/", {"op": "power_on"}) | ||
succeed({"status": "success", "power_state": "PowerOn"}) | ||
|
||
def stop(self): | ||
sysid = self.data.get("system_id") | ||
if not sysid: | ||
fail("system_id missing for stop") | ||
self.call_maas("POST", f"/machines/{sysid}/", {"op": "power_off"}) | ||
succeed({"status": "success", "power_state": "PowerOff"}) | ||
|
||
def reboot(self): | ||
sysid = self.data.get("system_id") | ||
if not sysid: | ||
fail("system_id missing for reboot") | ||
|
||
self.call_maas("POST", f"/machines/{sysid}/", {"op": "power_off"}) | ||
time.sleep(5) | ||
self.call_maas("POST", f"/machines/{sysid}/", {"op": "power_on"}) | ||
|
||
succeed({"status": "success", "power_state": "PowerOn", "message": "Reboot completed"}) | ||
|
||
def status(self): | ||
sysid = self.data.get("system_id") | ||
if not sysid: | ||
fail("system_id missing for status") | ||
resp = self.call_maas("GET", f"/machines/{sysid}/") | ||
state = resp.get("power_state", "") | ||
if state == "on": | ||
mapped = "PowerOn" | ||
elif state == "off": | ||
mapped = "PowerOff" | ||
else: | ||
mapped = "PowerUnknown" | ||
harikrishna-patnala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
succeed({"status": "success", "power_state": mapped}) | ||
|
||
|
||
def main(): | ||
if len(sys.argv) < 3: | ||
fail("Usage: maas.py <action> <json-file-path>") | ||
|
||
action = sys.argv[1].lower() | ||
json_file = sys.argv[2] | ||
|
||
try: | ||
manager = MaasManager(json_file) | ||
except FileNotFoundError: | ||
fail(f"JSON file not found: {json_file}") | ||
|
||
actions = { | ||
"prepare": manager.prepare, | ||
"create": manager.create, | ||
"delete": manager.delete, | ||
"start": manager.start, | ||
"stop": manager.stop, | ||
"reboot": manager.reboot, | ||
"status": manager.status, | ||
} | ||
|
||
if action not in actions: | ||
fail("Invalid action") | ||
|
||
actions[action]() | ||
|
||
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.