diff --git a/.travis.yml b/.travis.yml index 6aa799eb8..d07fff77d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/agents/ovh_cloud_rest/fence_ovh_cloud_rest.py b/agents/ovh_cloud_rest/fence_ovh_cloud_rest.py new file mode 100644 index 000000000..63c0bc730 --- /dev/null +++ b/agents/ovh_cloud_rest/fence_ovh_cloud_rest.py @@ -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": + 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() diff --git a/tests/data/metadata/fence_ovh_cloud_rest.xml b/tests/data/metadata/fence_ovh_cloud_rest.xml new file mode 100644 index 000000000..922142a9f --- /dev/null +++ b/tests/data/metadata/fence_ovh_cloud_rest.xml @@ -0,0 +1,112 @@ + + +Fence agent for OVH Cloud (REST API) with authentication via Oauth +https://api.ovh.com/ + + + + + Fencing action + + + + + OVH Oauth application key + + + + + OVH Oauth application secret + + + + + OVH Oauth token + + + + + OVH region + + + + + OVH service name + + + + + OVH service name + + + + + Disable logging to stderr. Does not affect --verbose or --debug-file or logging to syslog. + + + + + Verbose mode + + + + + Write debug information to given file + + + + + Write debug information to given file + + + + + Display version information and exit + + + + + Display help and exit + + + + + Wait X seconds before fencing is started + + + + + Wait X seconds for cmd prompt after login + + + + + Test X seconds for status change after ON/OFF + + + + + Wait X seconds after issuing ON/OFF + + + + + Wait X seconds for cmd prompt after issuing command + + + + + Count of attempts to retry power on + + + + + + + + + + + + +