Repository: cloudstack-cloudmonkey Updated Branches: refs/heads/master cdc995359 -> 0b8e537e6
cloudmonkey: use url variable to capture protocol, host, port, path Signed-off-by: Rohit Yadav <rohit.ya...@shapeblue.com> Project: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/commit/a87a232f Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/tree/a87a232f Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/diff/a87a232f Branch: refs/heads/master Commit: a87a232fc85df2a1bf1bc2bc5634b9a7766b8595 Parents: cdc9953 Author: Rohit Yadav <rohit.ya...@shapeblue.com> Authored: Fri Aug 15 23:55:09 2014 +0200 Committer: Rohit Yadav <rohit.ya...@shapeblue.com> Committed: Fri Aug 15 23:55:09 2014 +0200 ---------------------------------------------------------------------- cloudmonkey/cloudmonkey.py | 17 ++++++----------- cloudmonkey/config.py | 24 ++++++++++-------------- cloudmonkey/requester.py | 25 ++++++++++++------------- 3 files changed, 28 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/blob/a87a232f/cloudmonkey/cloudmonkey.py ---------------------------------------------------------------------- diff --git a/cloudmonkey/cloudmonkey.py b/cloudmonkey/cloudmonkey.py index 100a3cc..c43b061 100644 --- a/cloudmonkey/cloudmonkey.py +++ b/cloudmonkey/cloudmonkey.py @@ -257,10 +257,8 @@ class CloudMonkeyShell(cmd.Cmd, object): def make_request(self, command, args={}, isasync=False): response, error = monkeyrequest(command, args, isasync, self.asyncblock, logger, - self.host, self.port, - self.credentials, - self.timeout, self.protocol, - self.path, self.expires) + self.url, self.credentials, + self.timeout, self.expires) if error is not None: self.monkeyprint(error) return response @@ -400,11 +398,11 @@ class CloudMonkeyShell(cmd.Cmd, object): def do_set(self, args): """ Set config for cloudmonkey. For example, options can be: - host, port, apikey, secretkey, log_file, history_file + url, auth, log_file, history_file You may also edit your ~/.cloudmonkey_config instead of using set. Example: - set host 192.168.56.2 + set url http://localhost:8080/client/api set prompt ðµ cloudmonkey> set log_file /var/log/cloudmonkey.log """ @@ -425,8 +423,7 @@ class CloudMonkeyShell(cmd.Cmd, object): Login using stored credentials. Starts a session to be reused for subsequent api calls """ - url = "%s://%s:%s%s" % (self.protocol, self.host, self.port, self.path) - session, sessionkey = login(url, self.username, self.password) + session, sessionkey = login(self.url, self.username, self.password) self.credentials['session'] = session self.credentials['sessionkey'] = sessionkey @@ -435,9 +432,7 @@ class CloudMonkeyShell(cmd.Cmd, object): Logout of session started with login with username and password """ try: - url = "%s://%s:%s%s" % (self.protocol, self.host, - self.port, self.path) - logout(url, self.credentials.get('session')) + logout(self.url, self.credentials.get('session')) self.credentials['session'] = None self.credentials['sessionkey'] = None except TypeError: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/blob/a87a232f/cloudmonkey/config.py ---------------------------------------------------------------------- diff --git a/cloudmonkey/config.py b/cloudmonkey/config.py index 6f043d4..afe1976 100644 --- a/cloudmonkey/config.py +++ b/cloudmonkey/config.py @@ -56,20 +56,14 @@ config_fields['ui']['color'] = 'true' config_fields['ui']['prompt'] = '> ' config_fields['ui']['display'] = 'default' -# server -config_fields['server']['host'] = 'localhost' -config_fields['server']['path'] = '/client/api' -config_fields['server']['port'] = '8080' -config_fields['server']['protocol'] = 'http' +# server: default profile +config_fields['server']['url'] = 'http://localhost:8080/client/api' config_fields['server']['timeout'] = '3600' config_fields['server']['expires'] = '600' - -# user -config_fields['user']['apikey'] = '' -config_fields['user']['secretkey'] = '' -config_fields['user']['username'] = '' -config_fields['user']['password'] = '' - +config_fields['server']['username'] = 'admin' +config_fields['server']['password'] = 'password' +config_fields['server']['apikey'] = '' +config_fields['server']['secretkey'] = '' def write_config(get_attr, config_file, first_time=False): global config_fields @@ -114,10 +108,12 @@ def read_config(get_attr, set_attr, config_file): try: set_attr(key, config.get(section, key)) except Exception: + set_attr(key, config_fields[section][key]) missing_keys.append(key) if len(missing_keys) > 0: - print "Please fix `%s` in %s" % (', '.join(missing_keys), config_file) - sys.exit() + print "Found missing config keys and replace with default value:" + print "`%s` in %s" % (', '.join(missing_keys), config_file) + write_config(get_attr, config_file, False) return config_options http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/blob/a87a232f/cloudmonkey/requester.py ---------------------------------------------------------------------- diff --git a/cloudmonkey/requester.py b/cloudmonkey/requester.py index 6787d7f..9abbb4d 100644 --- a/cloudmonkey/requester.py +++ b/cloudmonkey/requester.py @@ -137,12 +137,12 @@ def make_request_with_password(command, args, logger, url, credentials): return result, error -def make_request(command, args, logger, host, port, - credentials, protocol, path, expires): +def make_request(command, args, logger, url, + credentials, expires): response = None error = None - if protocol != 'http' and protocol != 'https': + if not url.startswith('http'): error = "Protocol must be 'http' or 'https'" return None, error @@ -158,8 +158,7 @@ def make_request(command, args, logger, host, port, # try to use the apikey/secretkey method by default # followed by trying to check if we're using integration port # finally use the username/password method - if not credentials['apikey'] and (long(port) != 8096): - url = "%s://%s:%s%s" % (protocol, host, port, path) + if not credentials['apikey'] and not ("8096" in url): return make_request_with_password(command, args, logger, url, credentials) @@ -177,7 +176,7 @@ def make_request(command, args, logger, host, port, sig = urllib.quote_plus(base64.encodestring(hmac.new(secretkey, hashStr, hashlib.sha1).digest()).strip()) request_url += "&signature=%s" % sig - request_url = "%s://%s:%s%s?%s" % (protocol, host, port, path, request_url) + request_url = "%s?%s" % (url, request_url) try: logger_debug(logger, "Request sent: %s" % request_url) @@ -196,14 +195,14 @@ def make_request(command, args, logger, host, port, return response, error -def monkeyrequest(command, args, isasync, asyncblock, logger, host, port, - credentials, timeout, protocol, path, expires): +def monkeyrequest(command, args, isasync, asyncblock, logger, url, + credentials, timeout, expires): response = None error = None logger_debug(logger, "======== START Request ========") logger_debug(logger, "Requesting command=%s, args=%s" % (command, args)) - response, error = make_request(command, args, logger, host, - port, credentials, protocol, path, expires) + response, error = make_request(command, args, logger, url, + credentials, expires) logger_debug(logger, "======== END Request ========\n") @@ -239,9 +238,9 @@ def monkeyrequest(command, args, isasync, asyncblock, logger, host, port, timeout = timeout - pollperiod progress += 1 logger_debug(logger, "Job %s to timeout in %ds" % (jobid, timeout)) - response, error = make_request(command, request, logger, - host, port, credentials, - protocol, path, expires) + + response, error = make_request(command, request, logger, url, + credentials, expires) if error is not None: return response, error