Skip to content
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

Add agent for OVH Cloud (REST API) #202

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ before_install:
- pip install pexpect
- pip install boto3
- pip install google-api-python-client
- pip install ovh

before_script:
- wget https://github.com/Openwsman/openwsman/archive/v2.6.3.tar.gz
Expand Down
119 changes: 119 additions & 0 deletions agents/ovh_cloud_rest/fence_ovh_cloud_rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!@PYTHON@ -tt

import sys
import time
import atexit
import ovh
sys.path.append("@FENCEAGENTSLIBDIR@")
sys.path.append("/root/fence-agents/lib")
from fencing import *
from fencing import fail_usage, run_command, run_delay

POLL_INTERVAL_SECONDS = 1

def define_new_opts():
all_opt["application_key"] = {
"getopt" : "k:",
"longopt" : "application-key",
"help" : "-k, --application-key=[application-key] OVH Oauth application key",
"required" : "1",
"shortdesc" : "OVH Oauth application key",
"order" : 1}
all_opt["application_secret"] = {
"getopt" : "s:",
"longopt" : "application-secret",
"help" : "-s, --application-secret=[application-secret] OVH Oauth application secret",
"required" : "1",
"shortdesc" : "OVH Oauth application secret",
"order" : 2}
all_opt["token"] = {
"getopt" : "t:",
"longopt" : "token",
"help" : "-t, --token=[token] OVH Oauth token ",
"required" : "1",
"shortdesc" : "OVH Oauth token ",
"order" : 3}
all_opt["region"] = {
"getopt" : "r:",
"longopt" : "region",
"help" : "-r, --region=[region] OVH region (example: ovh-ca)",
"required" : "1",
"shortdesc" : "OVH region",
"order" : 4}
all_opt["service_name"] = {
"getopt" : "n:",
"longopt" : "service-name",
"help" : "-n, --service-name=[service-name] OVH service name",
"required" : "1",
"shortdesc" : "OVH service name",
"order" : 5}
all_opt["instance_id"] = {
"getopt" : "i:",
"longopt" : "instance-id",
"help" : "-i, --instance-id=[instance-id] OVH instance id",
"required" : "1",
"shortdesc" : "OVH service name",
"order" : 6}

def ovh_login(options):
session = ovh.Client(
endpoint=options["--region"],
application_key=options["--application-key"],
application_secret=options["--application-secret"],
consumer_key=options["--token"],
)
options["session"] = session;
return session;

def status(options, expected_status):
session = options["session"]
status_url = "/cloud/project/%s/instance/%s" % (options['--service-name'], options['--instance-id'])
response = session.get(status_url)
return True if response["status"] == expected_status else False

def main():
atexit.register(atexit_handler)

device_opt = ["application_key", "application_secret", "token", "region", "service_name", "instance_id", "no_password"]

define_new_opts()
options = check_input(device_opt, process_input(device_opt), other_conditions=True)

docs = {}
docs["shortdesc"] = "Fence agent for OVH Cloud (REST API)"
docs["longdesc"] = "Fence agent for OVH Cloud (REST API) with authentication via Oauth"
docs["vendorurl"] = "https://api.ovh.com/"
show_docs(options, docs)

run_delay(options)

session = ovh_login(options);

rescue_url = "/cloud/project/%s/instance/%s/rescueMode" % (options['--service-name'], options['--instance-id'])

if options["--action"] == "off":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should put the on/off code into e.g. set_power_status() and make a get_power_status() where it returns on/off/unknown. This way it can e.g. send reboot as send power-off, wait for power off, send power-on, wait for power-on.

Similar agent with a good example on how to do this:
https://github.com/ClusterLabs/fence-agents/blob/master/agents/vmware_rest/fence_vmware_rest.py#L178

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good idea. I misread part of the FenceAgentAPI doc and thought it meant reboot was a bad idea in general (but I see it suggests that only that is a bad idea for fabric fencing).

I'll make this change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oalbrigt One question -- does it matter if my get_power_status fails? As in, will the code in fencing.py retry until it gets a valid status? Or does my code need to sleep and poll until it gets a valid power status (as I've done in the code here)? It's not 100% obvious to me as I don't know if the vmware example one sometimes returns states between off and on as the OVH one does (ie if going to RESCUE, it returns RESCUING until it is ready and then RESCUE).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make it return Unknown when it's in those states. That way it will keep on trying until it gets OFF/ON or times out. You can try running it with -o off, on or reboot and using the -v parameter to see what it's actually doing (requires you to use "result = fence_action(conn, options, set_power_status, get_power_status, get_list)" to set the functions used for set_power_status/get_power_status).

try:
response = session.post(rescue_url, rescue=True)
if response["adminPassword"] != None:
result = 1
while not status(options, "RESCUE"):
time.sleep(POLL_INTERVAL_SECONDS)
except Exception as exception:
print(exception)
result = 0

if options["--action"] == "on":
try:
response = session.post(rescue_url, rescue=False)
if response["adminPassword"] == None:
result = 1
while not status(options, "ACTIVE"):
time.sleep(POLL_INTERVAL_SECONDS)
except Exception as exception:
print(exception)
result = 0

sys.exit(result)

if __name__ == "__main__":
main()
112 changes: 112 additions & 0 deletions tests/data/metadata/fence_ovh_cloud_rest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" ?>
<resource-agent name="fence_ovh_cloud_rest" shortdesc="Fence agent for OVH Cloud (REST API)" >
<longdesc>Fence agent for OVH Cloud (REST API) with authentication via Oauth</longdesc>
<vendor-url>https://api.ovh.com/</vendor-url>
<parameters>
<parameter name="action" unique="0" required="1">
<getopt mixed="-o, --action=[action]" />
<content type="string" default="reboot" />
<shortdesc lang="en">Fencing action</shortdesc>
</parameter>
<parameter name="application_key" unique="0" required="1">
<getopt mixed="-k, --application-key=[application-key]" />
<content type="string" />
<shortdesc lang="en">OVH Oauth application key</shortdesc>
</parameter>
<parameter name="application_secret" unique="0" required="1">
<getopt mixed="-s, --application-secret=[application-secret]" />
<content type="string" />
<shortdesc lang="en">OVH Oauth application secret</shortdesc>
</parameter>
<parameter name="token" unique="0" required="1">
<getopt mixed="-t, --token=[token]" />
<content type="string" />
<shortdesc lang="en">OVH Oauth token </shortdesc>
</parameter>
<parameter name="region" unique="0" required="1">
<getopt mixed="-r, --region=[region]" />
<content type="string" />
<shortdesc lang="en">OVH region</shortdesc>
</parameter>
<parameter name="service_name" unique="0" required="1">
<getopt mixed="-n, --service-name=[service-name]" />
<content type="string" />
<shortdesc lang="en">OVH service name</shortdesc>
</parameter>
<parameter name="instance_id" unique="0" required="1">
<getopt mixed="-i, --instance-id=[instance-id]" />
<content type="string" />
<shortdesc lang="en">OVH service name</shortdesc>
</parameter>
<parameter name="quiet" unique="0" required="0">
<getopt mixed="-q, --quiet" />
<content type="boolean" />
<shortdesc lang="en">Disable logging to stderr. Does not affect --verbose or --debug-file or logging to syslog.</shortdesc>
</parameter>
<parameter name="verbose" unique="0" required="0">
<getopt mixed="-v, --verbose" />
<content type="boolean" />
<shortdesc lang="en">Verbose mode</shortdesc>
</parameter>
<parameter name="debug" unique="0" required="0" deprecated="1">
<getopt mixed="-D, --debug-file=[debugfile]" />
<content type="string" />
<shortdesc lang="en">Write debug information to given file</shortdesc>
</parameter>
<parameter name="debug_file" unique="0" required="0" obsoletes="debug">
<getopt mixed="-D, --debug-file=[debugfile]" />
<content type="string" />
<shortdesc lang="en">Write debug information to given file</shortdesc>
</parameter>
<parameter name="version" unique="0" required="0">
<getopt mixed="-V, --version" />
<content type="boolean" />
<shortdesc lang="en">Display version information and exit</shortdesc>
</parameter>
<parameter name="help" unique="0" required="0">
<getopt mixed="-h, --help" />
<content type="boolean" />
<shortdesc lang="en">Display help and exit</shortdesc>
</parameter>
<parameter name="delay" unique="0" required="0">
<getopt mixed="--delay=[seconds]" />
<content type="second" default="0" />
<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
</parameter>
<parameter name="login_timeout" unique="0" required="0">
<getopt mixed="--login-timeout=[seconds]" />
<content type="second" default="5" />
<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
</parameter>
<parameter name="power_timeout" unique="0" required="0">
<getopt mixed="--power-timeout=[seconds]" />
<content type="second" default="20" />
<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
</parameter>
<parameter name="power_wait" unique="0" required="0">
<getopt mixed="--power-wait=[seconds]" />
<content type="second" default="0" />
<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
</parameter>
<parameter name="shell_timeout" unique="0" required="0">
<getopt mixed="--shell-timeout=[seconds]" />
<content type="second" default="3" />
<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
</parameter>
<parameter name="retry_on" unique="0" required="0">
<getopt mixed="--retry-on=[attempts]" />
<content type="integer" default="1" />
<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
</parameter>
</parameters>
<actions>
<action name="on" automatic="0"/>
<action name="off" />
<action name="reboot" />
<action name="status" />
<action name="monitor" />
<action name="metadata" />
<action name="manpage" />
<action name="validate-all" />
</actions>
</resource-agent>