diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-15 18:01:03 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-15 18:01:03 -0700 |
commit | 508168acb95aee070d493b45656f781a42bdd262 (patch) | |
tree | e816b241c500d99f1289fb6afffb33abb560df99 /cloudinit/transforms/cc_phone_home.py | |
parent | 36c1da35c2c0cb1b2ee18b7374bc81df8349e3e2 (diff) | |
download | vyos-cloud-init-508168acb95aee070d493b45656f781a42bdd262.tar.gz vyos-cloud-init-508168acb95aee070d493b45656f781a42bdd262.zip |
Complete initial cleanup for refactoring/rework.
Some of the cleanups were the following
1. Using standard (logged) utility functions for sub process work, writing, reading files, and other file system/operating system options
2. Having distrobutions impelement there own subclasses to handle system specifics (if applicable)
3. Having a cloud wrapper that provides just the functionality we want to expose (cloud.py)
4. Using a path class instead of globals for all cloud init paths (it is configured via config)
5. Removal of as much shared global state as possible (there should be none, minus a set of constants)
6. Other various cleanups that remove transforms/handlers/modules from reading/writing/chmoding there own files.
a. They should be using util functions to take advantage of the logging that is now enabled in those util functions (very useful for debugging)
7. Urls being read and checked from a single module that serves this and only this purpose (+1 for code organization)
8. Updates to log whenever a transform decides not to run
9. Ensure whenever a exception is thrown (and possibly captured) that the util.logexc function is called
a. For debugging, tracing this is important to not just drop them on the floor.
10. Code shuffling into utils.py where it makes sense (and where it could serve a benefit for other code now or in the future)
Diffstat (limited to 'cloudinit/transforms/cc_phone_home.py')
-rw-r--r-- | cloudinit/transforms/cc_phone_home.py | 53 |
1 files changed, 22 insertions, 31 deletions
diff --git a/cloudinit/transforms/cc_phone_home.py b/cloudinit/transforms/cc_phone_home.py index a7ff74e1..36af6dfa 100644 --- a/cloudinit/transforms/cc_phone_home.py +++ b/cloudinit/transforms/cc_phone_home.py @@ -17,13 +17,18 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from cloudinit.CloudConfig import per_instance -import cloudinit.util as util + +from cloudinit import templater +from cloudinit import url_helper as uhelp +from cloudinit import util + +from cloudinit.settings import PER_INSTANCE + from time import sleep -frequency = per_instance -post_list_all = ['pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', 'instance_id', - 'hostname'] +frequency = PER_INSTANCE +post_list_all = ['pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', + 'instance_id', 'hostname'] # phone_home: @@ -35,7 +40,7 @@ post_list_all = ['pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', 'instance_id', # url: http://my.foo.bar/$INSTANCE_ID/ # post: [ pub_key_dsa, pub_key_rsa, pub_key_ecdsa, instance_id # -def handle(_name, cfg, cloud, log, args): +def handle(name, cfg, cloud, log, args): if len(args) != 0: ph_cfg = util.read_conf(args[0]) else: @@ -44,7 +49,8 @@ def handle(_name, cfg, cloud, log, args): ph_cfg = cfg['phone_home'] if 'url' not in ph_cfg: - log.warn("no 'url' token in phone_home") + log.warn(("Skipping module named %s, " + "no 'url' found in 'phone_home' configuration"), name) return url = ph_cfg['url'] @@ -53,8 +59,8 @@ def handle(_name, cfg, cloud, log, args): try: tries = int(tries) except: - log.warn("tries is not an integer. using 10") tries = 10 + util.logexc(log, "Configuration entry 'tries' is not an integer, using %s", tries) if post_list == "all": post_list = post_list_all @@ -71,11 +77,9 @@ def handle(_name, cfg, cloud, log, args): for n, path in pubkeys.iteritems(): try: - fp = open(path, "rb") - all_keys[n] = fp.read() - fp.close() + all_keys[n] = util.load_file(path) except: - log.warn("%s: failed to open in phone_home" % path) + util.logexc(log, "%s: failed to open, can not phone home that data", path) submit_keys = {} for k in post_list: @@ -83,24 +87,11 @@ def handle(_name, cfg, cloud, log, args): submit_keys[k] = all_keys[k] else: submit_keys[k] = "N/A" - log.warn("requested key %s from 'post' list not available") + log.warn("Requested key %s from 'post' configuration list not available", k) - url = util.render_string(url, {'INSTANCE_ID': all_keys['instance_id']}) + url = templater.render_string(url, {'INSTANCE_ID': all_keys['instance_id']}) - null_exc = object() - last_e = null_exc - for i in range(0, tries): - try: - util.readurl(url, submit_keys) - log.debug("succeeded submit to %s on try %i" % (url, i + 1)) - return - except Exception as e: - log.debug("failed to post to %s on try %i" % (url, i + 1)) - last_e = e - sleep(3) - - log.warn("failed to post to %s in %i tries" % (url, tries)) - if last_e is not null_exc: - raise(last_e) - - return + try: + uhelp.readurl(url, data=submit_keys, retries=tries, sec_between=3) + except: + util.logexc(log, "Failed to post phone home data to %s in %s tries", url, tries) |