Browse Source

PEP8, lint and some random typos

Antonio Peña 5 years ago
parent
commit
cc6522e6e9
50 changed files with 553 additions and 357 deletions
  1. 1 1
      doc/source/_themes/aj/sidebar.html
  2. 1 0
      doc/source/_themes/sphinx_bootstrap_theme/__init__.py
  3. 33 29
      examples/complex.py
  4. 19 15
      examples/ec2_example.py
  5. 7 1
      examples/stupid.py
  6. 0 1
      mico/__init__.py
  7. 5 4
      mico/decorators.py
  8. 3 1
      mico/environ.py
  9. 10 3
      mico/hook.py
  10. 1 0
      mico/lib/__init__.py
  11. 32 24
      mico/lib/aws/ec2/__init__.py
  12. 51 32
      mico/lib/aws/ec2/autoscale.py
  13. 7 4
      mico/lib/aws/ec2/cw.py
  14. 15 11
      mico/lib/aws/ec2/ebs.py
  15. 5 4
      mico/lib/aws/ec2/eip.py
  16. 11 10
      mico/lib/aws/ec2/elb.py
  17. 24 20
      mico/lib/aws/ec2/sg.py
  18. 5 1
      mico/lib/aws/iam.py
  19. 8 3
      mico/lib/aws/r53.py
  20. 1 1
      mico/lib/core/__init__.py
  21. 5 0
      mico/lib/core/dir.py
  22. 35 12
      mico/lib/core/file.py
  23. 14 4
      mico/lib/core/group.py
  24. 11 7
      mico/lib/core/local.py
  25. 18 16
      mico/lib/core/network.py
  26. 13 10
      mico/lib/core/package/__init__.py
  27. 24 5
      mico/lib/core/package/apt.py
  28. 9 1
      mico/lib/core/package/yum.py
  29. 16 6
      mico/lib/core/service.py
  30. 6 3
      mico/lib/core/ssh.py
  31. 5 1
      mico/lib/core/sudo.py
  32. 5 3
      mico/lib/core/ulimit.py
  33. 13 5
      mico/lib/core/user.py
  34. 35 21
      mico/output.py
  35. 5 6
      mico/path.py
  36. 6 3
      mico/run.py
  37. 1 0
      mico/script/__init__.py
  38. 32 32
      mico/script/cmdline.py
  39. 4 5
      mico/stack/__init__.py
  40. 7 6
      mico/stack/ec2/__init__.py
  41. 13 14
      mico/stack/ec2/as/__init__.py
  42. 1 3
      mico/stack/ec2/as/alarms.py
  43. 1 3
      mico/stack/ec2/as/policies.py
  44. 3 3
      mico/stack/ec2/ebs.py
  45. 4 2
      mico/stack/ec2/sg.py
  46. 3 2
      mico/stack/r53/__init__.py
  47. 1 0
      mico/util/__init__.py
  48. 13 10
      mico/util/storage.py
  49. 4 2
      mico/util/switch.py
  50. 7 7
      setup.py

+ 1 - 1
doc/source/_themes/aj/sidebar.html

@@ -3,7 +3,7 @@
 	<img class="logo" src="{{ theme_logo_path }}" title="{{ theme_logo_title }}" />
 </a>
 <p><iframe
-	src="http://ghbtns.com/github-btn.html?user={{ theme_repo_author }}&amp;repo={{ theme_repo_name }}&amp;type=watch&amp;count=true&amp;size=large" 
+	src="http://ghbtns.com/github-btn.html?user={{ theme_repo_author }}&amp;repo={{ theme_repo_name }}&amp;type=watch&amp;count=true&amp;size=large"
 	allowtransparency="true" frameborder="0" scrolling="0" width="200px"
 	height="35px"></iframe>
 </p>

+ 1 - 0
doc/source/_themes/sphinx_bootstrap_theme/__init__.py

@@ -6,6 +6,7 @@ VERSION = (0, 2, 5)
 __version__ = ".".join(str(v) for v in VERSION)
 __version_full__ = __version__
 
+
 def get_html_theme_path():
     """Return list of HTML theme paths."""
     cur_dir = os.path.abspath(os.path.dirname(__file__))

+ 33 - 29
examples/complex.py

@@ -12,6 +12,7 @@ apt_update: true
 apt_upgrade: true
 """
 
+
 def main(*args):
     "Deploy the entire nosy production environment"
 
@@ -31,49 +32,52 @@ def main(*args):
         start_services(arg)
 
 
-
-
 def create_security(args):
     "Create security groups required for the environment."
 
     sg_frontend = sg_ensure(
-            name        = "frontend",
-            description = 'frontend security group',
-            rules = [
+            name="frontend",
+            description='frontend security group',
+            rules=[
                 sg_rule(
-                    protocol = "tcp",
-                    source   = "0.0.0.0/0",
-                    port     = "22"
+                    protocol="tcp",
+                    source="0.0.0.0/0",
+                    port="22"
                 ),
                 sg_rule(
-                    protocol = "tcp",
-                    source = "0.0.0.0/0",
-                    port = "80"
+                    protocol="tcp",
+                    source="0.0.0.0/0",
+                    port="80"
                 )
             ]
     )
 
+    return sg_frontend
+
 
 def create_instance(arg):
     "Create EBS instance for nosy."
 
     instance = ec2_ensure(
-            ami = "ami-10314d79",
-            name = arg,
-            instance_type = "t1.micro",
-            user_data = USERDATA % ( arg, "%s.example.com" % arg,) ,
-            key_name = "root-us-east-virginia",
-            security_groups = [ "frontend" ],
-            placement = "us-east-1a"
+            ami="ami-10314d79",
+            name=arg,
+            instance_type="t1.micro",
+            user_data=USERDATA % (arg, "%s.example.com" % arg, ),
+            key_name="root-us-east-virginia",
+            security_groups=["frontend"],
+            placement="us-east-1a"
     )
 
-    xvdf = ebs_ensure(
-        size        = 8,
-        zone        = "us-east-1a",
-        volume_type = "standard",
-        instance    = instance,
-        device      = "/dev/sdf",
-    )
+# UNUSED
+#    xvdf=ebs_ensure(
+#        size=8,
+#        zone="us-east-1a",
+#        volume_type="standard",
+#        instance=instance,
+#        device="/dev/sdf",
+#    )
+
+    return instance
 
 
 def install_nginx(arg):
@@ -84,7 +88,6 @@ def install_nginx(arg):
         service_ensure_boot("nginx")
 
 
-
 def install_uwsgi(args):
     "Install the uwsgi components into the system."
 
@@ -108,8 +111,9 @@ def create_users(args):
 
     with mode_sudo():
         user_ensure("ajdiaz")
-        group_user_add("sudo","ajdiaz")
-        ssh_authorize("ajdiaz","ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDX0Ila4UIQkgYRCTeHGzXkZbqYEpwwDlUDDM3oM4j2/hkrd9AY5p/ug921TpN4qi5pooSRwcD5ZiWZNqzPfEefl4A+3pGVfObN3NNUbc0Iry/T6MRdmnWB3LQxc2R45UOhVjLTuCKWRNLyZ1A7sIp7yrBt36BHoSoL40AIBbdEp37oSgubCI953UGNIN70BGAF/Cm0SW5f47NqDM2N2Fz/LA6Zf3NYXqYRzkOkpHxJ10DUvaAXoLiFVQPEGVdgwQJpIUdGZYkhPbgs5yBhBU5y2BLABJb/b1Yt3Yl6qSuBNOPP4oIMYjkUWXHu81hYJe68GpBwWN1AwGv3g7LeFOcx near.to")
+        group_user_add("sudo", "ajdiaz")
+        ssh_authorize("ajdiaz",
+            "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDX0Ila4UIQkgYRCTeHGzXkZbqYEpwwDlUDDM3oM4j2/hkrd9AY5p/ug921TpN4qi5pooSRwcD5ZiWZNqzPfEefl4A+3pGVfObN3NNUbc0Iry/T6MRdmnWB3LQxc2R45UOhVjLTuCKWRNLyZ1A7sIp7yrBt36BHoSoL40AIBbdEp37oSgubCI953UGNIN70BGAF/Cm0SW5f47NqDM2N2Fz/LA6Zf3NYXqYRzkOkpHxJ10DUvaAXoLiFVQPEGVdgwQJpIUdGZYkhPbgs5yBhBU5y2BLABJb/b1Yt3Yl6qSuBNOPP4oIMYjkUWXHu81hYJe68GpBwWN1AwGv3g7LeFOcx near.to")
 
 
 def configure_sudoers(args):
@@ -123,7 +127,7 @@ def configure_sudoers(args):
 
 def install_global_pip_packages(args):
     "Install pip packages for host."
-    PIP_PACKAGES = [ "redis", "bottle" ]
+    PIP_PACKAGES = ["redis", "bottle"]
 
     for package in PIP_PACKAGES:
         sudo("pip install --upgrade %s" % package)

+ 19 - 15
examples/ec2_example.py

@@ -10,10 +10,12 @@ apt_update: true
 apt_upgrade: true
 """
 
+
 def main(*args):
     for arg in args:
         deploy(arg)
 
+
 @async
 def deploy(host):
     """Generic deploy system for a number of hosts.
@@ -25,29 +27,31 @@ def deploy(host):
     host, domain = host.split('.', 1) if '.' in host else (host, 'localdomain')
 
     sg_test = sg_ensure(
-        name        = "sec-test",
-        description = "security group to test mico",
-        rules = [
+        name="sec-test",
+        description="security group to test mico",
+        rules=[
             sg_rule(
-                protocol = "tcp",
-                source   = "0.0.0.0/0",
-                port     = "22"
+                protocol="tcp",
+                source="0.0.0.0/0",
+                port="22"
             ),
             sg_rule(
-                protocol = "tcp",
-                source = "0.0.0.0/0",
-                port = "80"
+                protocol="tcp",
+                source="0.0.0.0/0",
+                port="80"
             )
         ]
     )
 
     instance = ec2_ensure(
-        ami = "ami-3d4ff254",
-        name = host,
-        instance_type = "t1.micro",
-        user_data = USERDATA % ( host, "%s.%s" % (host, domain,) ),
-        key_name = "root-us-east-virginia",
-        security_groups = [ sg_test ]
+        ami="ami-3d4ff254",
+        name=host,
+        instance_type="t1.micro",
+        user_data=USERDATA % (host, "%s.%s" % (host, domain,)),
+        key_name="root-us-east-virginia",
+        security_groups=[sg_test]
     )
 
+    return instance
+
 

+ 7 - 1
examples/stupid.py

@@ -4,22 +4,28 @@ print a message in stdout. Of course, it not very useful :)
 
 import random
 from time import sleep
+from mico.lib.aws import *
+
 
 def hello():
     print "Hello world!"
 
+
 def bye():
     print "Good bye cruel world..."
 
+
 @async
 def launch_async(arg):
-    sleep(random.randint(2,10))
+    sleep(random.randint(2, 10))
     print "Hola %s" % arg
 
+
 @serial
 def pfinish():
     print "Finished!"
 
+
 def main(*args):
     # next two in serial
     hello()

+ 0 - 1
mico/__init__.py

@@ -33,4 +33,3 @@ __builtin__.execute = mico.run.execute
 # XXX: Evaluate if storage has sense in environ
 #__builtin__.env.storage = FileStorage(mico.path.get_cache_path())
 
-

+ 5 - 4
mico/decorators.py

@@ -39,12 +39,13 @@ def async(func):
 
         t1.join()
         t2.join()
-	"""
+        """
+    from mico import env
 
     @wraps(func)
     def _inner(*args, **kwargs):
         if env.parallel:
-            func_th = Thread(target = func, args = args, kwargs = kwargs)
+            func_th = Thread(target=func, args=args, kwargs=kwargs)
             func_th.start()
             return func_th
         else:
@@ -78,10 +79,10 @@ def sync(func):
         t2 = task2()
         t3 = task3() # Only runs when task1 and task2 finished.
     """
-
     @wraps(func)
     def _inner(*args, **kwargs):
-        import threading, time
+        import threading
+        import time
         while threading.activeCount() > 1:
             time.sleep(1)
         return func(*args, **kwargs)

+ 3 - 1
mico/environ.py

@@ -11,7 +11,7 @@ Also, this module provides a decorator to create new environment
 properties.
 """
 
-import sys
+import mico
 
 from mico.decorators import environ
 
@@ -35,6 +35,8 @@ def _env_kernel():
 def _env_operation_system():
     """Get the Operating System of the remote host.
     """
+    from __builtin__ import env
+
     # TODO: Add other operating systems here...
     if env.custom.kernel == "linux":
         if file_exists("/etc/lsb-release"):

+ 10 - 3
mico/hook.py

@@ -10,11 +10,12 @@ from functools import wraps
 import mico.output
 import mico.run
 
-from __builtin__ import env
+import mico.env
 
 # global variable for add_hooks()
 parent_task_name = ''
 
+
 def task_add_post_run_hook(hook, *args, **kwargs):
     '''Run hook after Fabric tasks have completed on all hosts
 
@@ -103,21 +104,25 @@ def add_hooks(pre=None, pre_args=(), pre_kwargs={},
 
 from Queue import Queue
 
+
 def add_hook(context, action, parameters=()):
     """Enqueue one action with action parameters in environment"""
 
     if context in env:
-        env[context].put((action,parameters))
+        env[context].put((action, parameters))
     else:
         env[context] = Queue()
-        env[context].put((action,parameters))
+        env[context].put((action, parameters))
+
 
 def add_pre_hook(action, parameters=()):
     return add_hook("pre_hook", action, parameters)
 
+
 def add_post_hook(action, parameters=()):
     return add_hook("post_hook", action, parameters)
 
+
 def run_hook(context):
     if context in env:
         run = set()
@@ -134,9 +139,11 @@ def run_hook(context):
     else:
         return []
 
+
 def run_pre_hook():
     return run_hook("pre_hook")
 
+
 def run_post_hook():
     return run_hook("post_hook")
 

+ 1 - 0
mico/lib/__init__.py

@@ -0,0 +1 @@
+# -*- coding: utf-8 -*-

+ 32 - 24
mico/lib/aws/ec2/__init__.py

@@ -12,7 +12,6 @@ Example of usage in host definitions::
 """
 
 import time
-import itertools
 from os import environ as os_environ
 from fnmatch import fnmatch
 
@@ -21,6 +20,7 @@ from boto.ec2.connection import EC2Connection
 
 import mico.output
 
+
 class EC2LibraryError(Exception):
     """Model an exception related with EC2 API."""
 
@@ -38,14 +38,14 @@ def ec2_connect(region=None):
         region = env.get("ec2_region")
 
     region = get_region(region,
-            aws_access_key_id = os_environ.get("AWS_ACCESS_KEY_ID"),
-            aws_secret_access_key = os_environ.get("AWS_ACCESS_SECRET_KEY")
+            aws_access_key_id=os_environ.get("AWS_ACCESS_KEY_ID"),
+            aws_secret_access_key=os_environ.get("AWS_ACCESS_SECRET_KEY")
     )
 
     connection = EC2Connection(
             os_environ.get("AWS_ACCESS_KEY_ID"),
             os_environ.get("AWS_SECRET_ACCESS_KEY"),
-            region = region
+            region=region
     )
 
     return connection
@@ -61,15 +61,15 @@ def ec2_tag(resource, **kwargs):
     connection.create_tags([resource.id], kwargs)
     mico.output.debug("tag instance %s:" % (
         resource.id,
-        ",".join(map(lambda x:"%s=%s" % x, [x for x in kwargs.iteritems()]))
+        ",".join(map(lambda x: "%s=%s" % x, [x for x in kwargs.iteritems()]))
     ))
 
+
 def ec2_tag_volumes(instance):
     """Tag volumes in the instance following a basic notation Name as
     hostname of the instance host and Device to the properly device in
     the system.
     """
-
     connection = ec2_connect()
 
     _obj = instance.get_attribute("blockDeviceMapping")
@@ -77,7 +77,7 @@ def ec2_tag_volumes(instance):
         _obj = _obj[u"blockDeviceMapping"]
         for device, obj in _obj.items():
             if obj.volume_id is not None:
-                _d = { "Device": device }
+                _d = {"Device": device}
                 if "Name" in instance.tags:
                     _d["Name"] = instance.tags["Name"]
                 connection.create_tags([obj.volume_id], _d)
@@ -183,16 +183,14 @@ def ec2_ensure(ami, name=None, address=None, wait_until_running=True,
         if _obj:
             status = _obj.update()
             if status != "terminated":
-                mico.output.info("use existent instance: %s [%s]" % (_obj.id, _obj.ip_address or 'no ip found') )
-                if getattr(_obj,"ip_address", None) and _obj.ip_address:
+                mico.output.info("use existent instance: %s [%s]" % (_obj.id, _obj.ip_address or 'no ip found'))
+                if getattr(_obj, "ip_address", None) and _obj.ip_address:
                     if 'mico' in env.roledefs:
                         env.roledefs['mico'].append(_obj.ip_address)
                     else:
-                        env.roledefs['mico'] = [ _obj.ip_address ]
-
+                        env.roledefs['mico'] = [_obj.ip_address]
                     if 'mico' not in env.roles:
                         env.roles.append('mico')
-
                 return _obj
 
     kwargs["disable_api_termination"] = termination_protection
@@ -204,7 +202,7 @@ def ec2_ensure(ami, name=None, address=None, wait_until_running=True,
     status = instance.update()
 
     if name is not None:
-        connection.create_tags([instance.id], { "Name": name })
+        connection.create_tags([instance.id], {"Name": name})
 
     if tags:
         connection.create_tags([instance.id], tags)
@@ -216,7 +214,7 @@ def ec2_ensure(ami, name=None, address=None, wait_until_running=True,
         mico.output.debug("waiting 10 secs for instance initialiation...")
         time.sleep(10)
         status = instance.update()
-    time.sleep(2) # yes... amazon weird behaviour :/
+    time.sleep(2)  # yes... amazon weird behaviour :/
 
     for device, volume in volumes.items():
         connection.attach_volume(volume.id, instance.id, device)
@@ -237,14 +235,14 @@ def ec2_ensure(ami, name=None, address=None, wait_until_running=True,
             instance.id
         ))
         connection.associate_address(instance.id, address)
-        time.sleep(2) # amazon needs time to think about how to associate an address.
+        time.sleep(2)  # amazon needs time to think about how to associate an address.
 
-    if getattr(instance,"ip_address", None) and instance.ip_address:
+    if getattr(instance, "ip_address", None) and instance.ip_address:
         mico.output.info("created instance: %s as %s [%s]" % (instance.id, instance.instance_type, instance.ip_address))
         if 'mico' in env.roledefs:
             env.roledefs['mico'].append(instance.ip_address)
         else:
-            env.roledefs['mico'] = [ instance.ip_address ]
+            env.roledefs['mico'] = [instance.ip_address]
 
         if 'mico' not in env.roles:
             env.roles.append('mico')
@@ -252,12 +250,11 @@ def ec2_ensure(ami, name=None, address=None, wait_until_running=True,
     else:
         mico.output.info("created instance: %s [<unassigned address>]" % (instance.id,))
 
-    time.sleep(2) # yes... another amazon weird behaviour :/
+    time.sleep(2)  # yes... another amazon weird behaviour :/
 
     return instance
 
 
-
 def ec2_exists(tags={}):
     """Returns if tagged instance already exists, if exists return the object,
     otherwise returns None.
@@ -265,7 +262,7 @@ def ec2_exists(tags={}):
     connection = ec2_connect()
     ret = []
 
-    for reservation in connection.get_all_instances(None, dict(map(lambda (x,y):("tag:%s" % x, y), tags.items()))):
+    for reservation in connection.get_all_instances(None, dict(map(lambda (x, y): ("tag:%s" % x, y), tags.items()))):
         for instance in reservation.instances:
             if instance.update() != "terminated":
                 ret.append(instance)
@@ -275,6 +272,7 @@ def ec2_exists(tags={}):
     else:
         return ret
 
+
 def ec2_list(*args):
     """List instances filtering with tag name, provided in arguments. Glob
     expressions are allowed in filters as multiple filters too, for
@@ -297,7 +295,7 @@ def ec2_list(*args):
                         yield instance
                 elif arg.startswith("sec:"):
                     arg = arg[4:]
-                    for group in map(lambda x:x.name, instance.groups):
+                    for group in map(lambda x: x.name, instance.groups):
                         if fnmatch(group, arg):
                             if "Name" in instance.tags:
                                 instance.name = instance.tags["Name"]
@@ -312,51 +310,61 @@ def ec2_list(*args):
 
 ec2_launch = ec2_create = ec2_run = ec2_ensure
 
-from mico.lib.aws.ec2.sg  import *
-from mico.lib.aws.ec2.cw  import *
+from mico.lib.aws.ec2.sg import *
+from mico.lib.aws.ec2.cw import *
 from mico.lib.aws.ec2.eip import *
 from mico.lib.aws.ec2.ebs import *
 from mico.lib.aws.ec2.elb import *
-from mico.lib.aws.ec2.autoscale  import *
+from mico.lib.aws.ec2.autoscale import *
 
 from mico.environ import environ
 
+
 @environ('ec2_ami')
 def ec2_get_ami():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "ami-id")
 
+
 @environ('ec2_hostname')
 def ec2_get_hostname():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "hostname")
 
+
 @environ('ec2_instance_action')
 def ec2_get_instance_action():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "instance_action")
 
+
 @environ('ec2_instance_type')
 def ec2_get_instance_type():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "instance_type")
 
+
 @environ('ec2_aki')
 def ec2_get_aki():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "kernel-id")
 
+
 @environ('ec2_local_hostname')
 def ec2_get_local_hostname():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "local-hostname")
 
+
 @environ('ec2_public_hostname')
 def ec2_get_public_hostname():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "public-hostname")
 
+
 @environ('ec2_local_ipv4')
 def ec2_get_local_ipv4():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "local-ipv4")
 
+
 @environ('ec2_public_ipv4')
 def ec2_get_public_ipv4():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "public-ipv4")
 
+
 @environ('ec2_mac')
 def ec2_get_mac():
     return run("curl http://169.254.169.254/latest/meta-data/%s" % "mac")

+ 51 - 32
mico/lib/aws/ec2/autoscale.py

@@ -20,19 +20,23 @@ from boto.ec2.autoscale import Tag
 
 import mico.output
 from mico.lib.aws.ec2 import EC2LibraryError
-from mico.lib.aws.ec2 import ec2_connect, get_region
-from mico.lib.aws.ec2 import ec2_tag_volumes
-from mico.lib.aws.ec2.cw import cw_connect, _cw_define, cw_exists
 from mico.lib.aws.ec2 import ec2_connect
+from mico.lib.aws.ec2.cw import cw_connect
+from mico.lib.aws.ec2.cw import _cw_define
+from mico.lib.aws.ec2.cw import cw_exists
+
+from mico import lock
+from mico import env
 
 
 @lock
 def _as_get_timestamp():
     # TODO: In the future this function will be in utils package.
     if not getattr(_as_get_timestamp, "_timestamp", None):
-        _as_get_timestamp._timestamp = time.strftime("%Y%m%d%H%S",time.localtime())
+        _as_get_timestamp._timestamp = time.strftime("%Y%m%d%H%S", time.localtime())
     return _as_get_timestamp._timestamp
 
+
 def as_connect(region=None, *args, **kwargs):
     """Helper to connect to Amazon Web Services EC2, using identify provided
     by environment, as also optional region in arguments.
@@ -52,18 +56,20 @@ def as_connect(region=None, *args, **kwargs):
     connection = AutoScaleConnection(
             os_environ.get("AWS_ACCESS_KEY_ID"),
             os_environ.get("AWS_SECRET_ACCESS_KEY"),
-            region = region,
+            region=region,
             *args,
             **kwargs
     )
 
     return connection
 
+
 def as_config_exists(name):
     """Return the instance config with specific name."""
     connection = as_connect()
     return connection.get_all_launch_configurations(names=[name])
 
+
 def as_config(name, ami, force=False, *args, **kwargs):
     """Create a instance config to be used in autoscale group. In general
     this config will be defined as another standalone instance, according
@@ -118,12 +124,12 @@ def as_config(name, ami, force=False, *args, **kwargs):
     _sgs = None
     if "security_groups" in kwargs:
         if not isinstance(kwargs["security_groups"], list):
-            _sgs = [ kwargs["security_groups"] ]
+            _sgs = [kwargs["security_groups"]]
         else:
             _sgs = kwargs["security_groups"]
 
         _sgs = map(
-                lambda x:x.name if isinstance(x, SecurityGroup) else x,
+                lambda x: x.name if isinstance(x, SecurityGroup) else x,
                 _sgs
         )
 
@@ -143,6 +149,7 @@ def as_config(name, ami, force=False, *args, **kwargs):
     mico.output.info("create new config %s" % name)
     return config
 
+
 def as_pause(group):
     """Pause autoscaling group activity. When paused an autoscaling
     group does not grow nor srink.
@@ -158,6 +165,7 @@ def as_pause(group):
 
     return _x
 
+
 def as_resume(group):
     """Resume autoscaling group activity.
 
@@ -172,6 +180,7 @@ def as_resume(group):
 
     return _x
 
+
 def as_resize(group, size):
     """Change autoscaling group desired capacity to size.
 
@@ -190,6 +199,7 @@ def as_resize(group, size):
 
     return _x
 
+
 def as_policy(name, adjustment_type='ChangeInCapacity',
         scaling_adjustment=2, cooldown=60, *args, **kwargs):
     """
@@ -199,13 +209,15 @@ def as_policy(name, adjustment_type='ChangeInCapacity',
     :param name: Name of scaling policy.
 
     :type adjustment_type: str
-    :param adjustment_type: Specifies the type of adjustment. Valid values are `ChangeInCapacity`, `ExactCapacity` and `PercentChangeInCapacity`.
+    :param adjustment_type: Specifies the type of adjustment.
+             Valid values are `ChangeInCapacity`, `ExactCapacity` and `PercentChangeInCapacity`.
 
     :type scaling_adjustment: int
     :param scaling_adjustment: Value of adjustment (type specified in `adjustment_type`).
 
     :type cooldown: int
-    :param cooldown: Time (in seconds) before Alarm related Scaling Activities can start after the previous Scaling Activity ends.
+    :param cooldown: Time (in seconds) before Alarm
+                    related Scaling Activities can start after the previous Scaling Activity ends.
     """
 
     name = "%s-%s" % (name, _as_get_timestamp())
@@ -218,22 +230,23 @@ def as_policy(name, adjustment_type='ChangeInCapacity',
     _x.update(kwargs)
     return _x
 
+
 def as_exists(name):
     """Return a list of autoscale groups which match with specific name."""
     connection = as_connect()
-    return connection.get_all_groups(names = [name])
+    return connection.get_all_groups(names=[name])
+
 
 def as_ensure(
         name,
         zones,
         instance,
-        balancers = [],
-        events = [],
-        min_size = 2,
-        max_size = 20,
-        desired_size = None,
-        force = False
-):
+        balancers=[],
+        events=[],
+        min_size=2,
+        max_size=20,
+        desired_size=None,
+        force=False):
     """Create a new autoscale group.
 
     :type name: str
@@ -275,19 +288,17 @@ def as_ensure(
         else:
             _l.append(elb.name)
 
-    ag = AutoScalingGroup(
-            name = ag_name,
-            availability_zones = zones,
-            launch_config = instance,
-            load_balancers = _l,
-            min_size = min_size,
-            max_size = max_size,
-            desired_capacity = desired_size
-    )
+    ag = AutoScalingGroup(name=ag_name,
+                          availability_zones=zones,
+                          launch_config=instance,
+                          load_balancers=_l,
+                          min_size=min_size,
+                          max_size=max_size,
+                          desired_capacity=desired_size)
     connection.create_auto_scaling_group(ag)
     mico.output.info("created new autoscaling group: %s" % ag_name)
 
-    as_tag = Tag(key='Name', value = "%s" % name, propagate_at_launch=True, resource_id=ag_name)
+    as_tag = Tag(key='Name', value="%s" % name, propagate_at_launch=True, resource_id=ag_name)
 
     # Add the tag to the autoscale group
     connection.create_or_update_tags([as_tag])
@@ -295,7 +306,7 @@ def as_ensure(
     cw_connection = cw_connect()
     for condition, actions in events:
         if not isinstance(actions, list):
-            actions = [ actions ]
+            actions = [actions]
 
         condition.dimensions = {"AutoScalingGroupName": ag_name}
 
@@ -310,7 +321,7 @@ def as_ensure(
             mico.output.info("create policy %s" % policy.name)
             connection.create_scaling_policy(policy)
 
-            action = connection.get_all_policies(as_group=ag_name,policy_names=[action["name"]])[0]
+            action = connection.get_all_policies(as_group=ag_name, policy_names=[action["name"]])[0]
             condition.name = "%s-%s" % (condition.name, _as_get_timestamp())
             condition.add_alarm_action(action.policy_arn)
             mico.output.debug("add new alarm for condition %s: %s" % (condition.name, action.name))
@@ -319,6 +330,7 @@ def as_ensure(
         mico.output.info("create alarm %s" % condition.name)
     return ag
 
+
 def as_alarm(name, alarm_actions=[], *args, **kwargs):
     """Ensures that an specific alarm for autoscale group
     exists in cloudwatch.
@@ -366,6 +378,7 @@ def as_alarm(name, alarm_actions=[], *args, **kwargs):
     """
     return _cw_define(name, alarm_actions, *args, **kwargs)
 
+
 def as_event(condition, action):
     """Create a new definition of an event which produces an action.
 
@@ -378,6 +391,7 @@ def as_event(condition, action):
 
     return (condition, action)
 
+
 def as_delete(name, force=False):
     """Remove a autoscale group.
 
@@ -404,10 +418,11 @@ def as_delete_policy(name, autoscale_group=None):
     :param autoscale_group: the name of the autoscale group.
     """
     conn = as_connect()
-    _x = conn.delete_policy(name, autoscale)
+    _x = conn.delete_policy(name, autoscale_group)
     mico.output.info("Delete autoscaling policy: %s" % name)
     return _x
 
+
 def as_delete_config(name):
     """Remove an autoscaling config
 
@@ -419,6 +434,7 @@ def as_delete_config(name):
     mico.output.info("Delete autoscaling config: %s" % name)
     return _x
 
+
 def as_activity(name, max_records=None):
     """Get the scaling activity for an autoscale group
 
@@ -438,6 +454,7 @@ def as_activity(name, max_records=None):
     conn = as_connect()
     return map(_set_activity_name, conn.get_all_activities(name, max_records))
 
+
 def as_list(*args):
     """List autoscaling groups filtering by autoscale name, provided as
     argument. Glob expressiosn are allowed in filters as multiple filtes
@@ -456,6 +473,7 @@ def as_list(*args):
                 group.total_instances = len(group.instances)
                 yield group
 
+
 def as_list_policies(*args):
     """List autoscaling policies filtering by name providing as argument.
     Glob expression are allowed in filters as multiple filters too, for
@@ -471,6 +489,7 @@ def as_list_policies(*args):
             if fnmatch(policy.name, arg):
                 yield policy
 
+
 def as_list_alarms(*args):
     """List autoscaling alarms filtering by name providing as argument.
     Glob expression are allowed in filters as multiple filters too, for
@@ -479,7 +498,6 @@ def as_list_alarms(*args):
         as_list_alarms('apaches-*')
     """
     args = args or ('*',)
-    conn = as_connect()
 
     for arg in args:
         for policy in as_list_policies('*'):
@@ -489,6 +507,7 @@ def as_list_alarms(*args):
                     cw_alarm.name = alarm.name
                     yield cw_alarm
 
+
 def as_list_instances(*args):
     """List autoscaling instances which are vinculated with specified
     autoscaling group passed as argument. For example::
@@ -498,7 +517,7 @@ def as_list_instances(*args):
     for ag in as_list(*args):
         for instance in ag.instances:
             res = ec2_connect().get_all_instances([instance.instance_id])
-            for insobj in  [i for r in res for i in r.instances]:
+            for insobj in [i for r in res for i in r.instances]:
                 insobj.autoscaling_group = ag.name
                 insobj.launch_config_name = instance.launch_config_name
                 if "Name" in insobj.tags:

+ 7 - 4
mico/lib/aws/ec2/cw.py

@@ -15,6 +15,9 @@ from mico.lib.aws.ec2 import EC2LibraryError
 
 import boto.ec2.cloudwatch
 
+from mico import env
+
+
 def cw_connect(region=None, *args, **kwargs):
     """Helper to connect to Amazon Web Services EC2, using identify provided
     by environment, as also optional region in arguments.
@@ -34,7 +37,7 @@ def cw_connect(region=None, *args, **kwargs):
     connection = CloudWatchConnection(
             os_environ.get("AWS_ACCESS_KEY_ID"),
             os_environ.get("AWS_SECRET_ACCESS_KEY"),
-            region = region
+            region=region
     )
 
     return connection
@@ -58,8 +61,7 @@ def _cw_define(name, alarm_actions=[], *args, **kwargs):
     if "namespace" not in kwargs:
         kwargs["namespace"] = "AWS/EC2"
 
-
-    _x = MetricAlarm(name = name, alarm_actions = alarm_actions, *args, **kwargs)
+    _x = MetricAlarm(name=name, alarm_actions=alarm_actions, *args, **kwargs)
 
     # XXX: boto does not handle very well the alarm_actions list when the
     # same connection is used for two different cloudwatch alarms, so the
@@ -73,6 +75,7 @@ def _cw_define(name, alarm_actions=[], *args, **kwargs):
 
     return _x
 
+
 def cw_ensure(name, alarm_actions=[], *args, **kwargs):
     """
     Ensures that an specific alarm exists in cloudwatch.
@@ -124,11 +127,11 @@ def cw_ensure(name, alarm_actions=[], *args, **kwargs):
     mico.output.info("create alarm %s" % name)
     return alarm
 
+
 def cw_delete(*args):
     """Delete alarms passed in arguments.
     """
 
-    cw_connection = cw_connect()
     for arg in args:
         boto.ec2.cloudwatch.delete_alarms([arg])
         mico.output.info("remove alarm %s" % arg)

+ 15 - 11
mico/lib/aws/ec2/ebs.py

@@ -85,7 +85,7 @@ def ebs_ensure(size, zone=None, instance=None, device=None, tags={},
         _obj.update()
 
     if tags:
-        connection.create_tags([_obj.id],tags)
+        connection.create_tags([_obj.id], tags)
 
     if device and instance:
         connection.attach_volume(_obj.id, instance.id, device)
@@ -104,8 +104,9 @@ def ebs_exists(tags={}):
     connection = ec2_connect()
 
     _x = connection.get_all_volumes(None,
-            dict(map(lambda (x,y):("tag:%s" % x, y), tags.items())))
-    return filter(lambda x:x.status == 'in-use', _x)
+            dict(map(lambda (x, y): ("tag:%s" % x, y), tags.items())))
+    return filter(lambda x: x.status == 'in-use', _x)
+
 
 def ebs_delete(volumes):
     """Delete volumes passed as argument.
@@ -117,7 +118,7 @@ def ebs_delete(volumes):
     connection = ec2_connect()
 
     if not isinstance(volumes, list):
-        volumes = [ volumes ]
+        volumes = [volumes]
 
     for x in volumes:
         if isinstance(x, str):
@@ -127,6 +128,7 @@ def ebs_delete(volumes):
             connection.delete_volume(x.id)
             mico.output.info("Remove volume: %s" % x.id)
 
+
 def ebs_detach(volumes, force=False):
     """Detach a number of volumes passed as arguments.
 
@@ -136,7 +138,7 @@ def ebs_detach(volumes, force=False):
     connection = ec2_connect()
 
     if not isinstance(volumes, list):
-        volumes = [ volumes ]
+        volumes = [volumes]
 
     for x in volumes:
         if isinstance(x, str):
@@ -156,15 +158,17 @@ def ebs_list(*args):
     """
     conn = ec2_connect()
     vol = conn.get_all_volumes()
-    ins = dict(map(lambda x:(x.id,x), [i for r in conn.get_all_instances() for i in r.instances]))
+    ins = dict(map(lambda x: (x.id, x), [i for r in conn.get_all_instances() for i in r.instances]))
     args = args or ('*',)
 
     for x in vol:
         x.name = x.id
         for arg in args:
-            if x.tags.get("Name",False) and fnmatch(x.tags["Name"], arg):
-               x.device = x.attach_data.device
-               x.instance_id = ("%s (%s)" % (ins[x.attach_data.instance_id].tags.get("Name",None), x.attach_data.instance_id)) \
-                               if x.attach_data.id is not None else None
-               yield x
+            if x.tags.get("Name", False) and fnmatch(x.tags["Name"], arg):
+                x.device = x.attach_data.device
+                x.instance_id = ("%s (%s)" %
+                                 (ins[x.attach_data.instance_id].tags.get("Name", None),
+                                  x.attach_data.instance_id)) if x.attach_data.id is not None \
+                                 else None
+                yield x
 

+ 5 - 4
mico/lib/aws/ec2/eip.py

@@ -10,6 +10,7 @@ import mico.output
 from mico.lib.aws.ec2 import ec2_tag
 from mico.lib.aws.ec2 import ec2_connect
 
+
 def eip_allocate(instance=None, force=False):
     """Allocate a new Elastic IP Address.
 
@@ -22,15 +23,15 @@ def eip_allocate(instance=None, force=False):
     """
     if instance is not None:
         if "public_ip" in instance.tags:
-            ec2_connect().associate_address(instance, instace.tags["public_ip"])
+            ec2_connect().associate_address(instance, instance.tags["public_ip"])
             mico.output.info("using existent EIP %s for %s" % (
-                instace.tags["public_ip"],
+                instance.tags["public_ip"],
                 instance)
             )
             return instance.tags["public_ip"]
         else:
             address = ec2_connect().allocate_address().public_ip
-            ec2_tag(instance, public_ip = address)
+            ec2_tag(instance, public_ip=address)
             ec2_connect().associate_address(instance.id, address)
             mico.output.info("created new EIP %s for %s" % (
                 address,
@@ -71,7 +72,7 @@ def eip_release(public_ip=None, allocation_ip=None):
     :return: True if successful
     """
     connection = ec2_connect()
-    _x = connection.release_address(public_ip, allocation_id)
+    _x = connection.release_address(public_ip, allocation_ip)
     mico.output.info("released EIP: %s" % public_ip)
     return _x
 

+ 11 - 10
mico/lib/aws/ec2/elb.py

@@ -9,7 +9,6 @@ from AWS.
 from os import environ as os_environ
 
 from boto.exception import BotoServerError
-from boto.ec2 import get_region
 from boto.ec2.elb import ELBConnection
 from boto.ec2.elb import HealthCheck
 
@@ -19,6 +18,8 @@ import mico.output
 from mico.lib.aws.ec2 import EC2LibraryError
 from mico.lib.aws.ec2 import ec2_connect
 
+from mico import env
+
 
 def elb_connect(region=None, *args, **kwargs):
     """Helper to connect to Amazon Web Services EC2, using identify provided
@@ -44,7 +45,7 @@ def elb_connect(region=None, *args, **kwargs):
     connection = ELBConnection(
             os_environ.get("AWS_ACCESS_KEY_ID"),
             os_environ.get("AWS_SECRET_ACCESS_KEY"),
-            region = region,
+            region=region,
             *args,
             **kwargs
     )
@@ -66,11 +67,11 @@ def elb_check(target, interval=20, healthy_threshold=3,
     .. _HealthCheck: http://docs.amazonwebservices.com/ElasticLoadBalancing/latest/APIReference/API_HealthCheck.html
     """
     return HealthCheck(
-            target = target,
-            interval = interval,
-            timeout = timeout,
-            healthy_threshold = healthy_threshold,
-            unhealthy_threshold = unhealthy_threshold
+            target=target,
+            interval=interval,
+            timeout=timeout,
+            healthy_threshold=healthy_threshold,
+            unhealthy_threshold=unhealthy_threshold
     )
 
 
@@ -117,9 +118,9 @@ def elb_ensure(name, listeners, check, zones=None, *args, **kwargs):
         zones = ec2_conn.get_all_zones()
 
     elb = connection.create_load_balancer(
-            name = name,
-            zones = zones,
-            listeners = listeners,
+            name=name,
+            zones=zones,
+            listeners=listeners,
             *args,
             **kwargs
     )

+ 24 - 20
mico/lib/aws/ec2/sg.py

@@ -6,41 +6,40 @@
 groups.
 """
 
-import socket
-
 from boto.exception import EC2ResponseError
 from boto.ec2.elb.securitygroup import SecurityGroup
 from boto.ec2.securitygroup import SecurityGroup as SG_Instance
 
 import mico.output
-from mico.util.dicts import AttrDict
-from mico.lib.aws.ec2 import ec2_connect, ec2_list
+from mico.lib.aws.ec2 import ec2_connect
+from mico.lib.aws.ec2 import ec2_list
 from mico.lib.aws.ec2 import EC2LibraryError
 
+
 def sg_rule(protocol="tcp", source="0.0.0.0/32", port=None, from_port=None, to_port=None):
     """Return a representation of a specific security rule.
     """
 
     cidr_ip = None
     src_group_name = None
-    ret = { "ip_protocol": protocol }
+    ret = {"ip_protocol": protocol}
 
     if port is not None:
         if isinstance(port, int):
             ret["from_port"] = port
-            ret["to_port"]   = port
+            ret["to_port"] = port
         elif isinstance(port, str):
             if "-" in port:
                 port_ini, port_end = port.split("-")
                 ret["from_port"] = int(port_ini)
-                ret["to_port"]   = int(port_end)
+                ret["to_port"] = int(port_end)
             else:
                 ret["from_port"] = int(port)
-                ret["to_port"]   = int(port)
+                ret["to_port"] = int(port)
     else:
         if protocol != "icmp":
             ret["from_port"] = from_port or 0
-            ret["to_port"]   = to_port or 65535
+            ret["to_port"] = to_port or 65535
 
     def _add_source(src, d):
         r = {}
@@ -65,11 +64,11 @@ def sg_rule(protocol="tcp", source="0.0.0.0/32", port=None, from_port=None, to_p
             raise ValueError("Unknow type for sg source")
         return r
 
-
     if isinstance(source, list):
-        return [ _add_source(x, ret) for x in source ]
+        return [_add_source(x, ret) for x in source]
     else:
-        return [ _add_source(source, ret)]
+        return [_add_source(source, ret)]
+
 
 def sg_ensure(name, description, vpc_id=None, rules=[], force=False):
     """Create a new EC2 security group according with parameters passed
@@ -105,19 +104,20 @@ def sg_ensure(name, description, vpc_id=None, rules=[], force=False):
                 _obj.authorize(**r)
                 mico.output.info("add rule to security group %s: %s" % (
                     _obj.name,
-                    ",".join(map(lambda x:"%s=%s" % x, r.items()))
+                    ",".join(map(lambda x: "%s=%s" % x, r.items()))
                 ))
             except EC2ResponseError as e:
                 if e.error_code == "InvalidPermission.Duplicate":
                     mico.output.debug("skip add already exists rule to security group %s: %s" % (
                         _obj.name,
-                        ", ".join(map(lambda x:"%s=%s" % x, r.items()))
+                        ", ".join(map(lambda x: "%s=%s" % x, r.items()))
                     ))
             except Exception as e:
                 raise e
 
     return _obj
 
+
 def sg_exists(name):
     """Return the security group with name passed as argument for specified
     region or None if it does not exists.
@@ -132,18 +132,21 @@ def sg_exists(name):
     else:
         return None
 
+
 def _sg_revoke_all_rules(name, target):
     for rule in name.rules:
         dr = rule.__dict__
         if target.id in [g.group_id for g in dr["grants"]]:
-            mico.output.debug("Removed from %s the rule %s %s-%s that references %s" % (name.name, dr["ip_protocol"], dr["from_port"], dr["to_port"], target.name, ))
+            mico.output.debug("Removed from %s the rule %s %s-%s that references %s" %
+               (name.name, dr["ip_protocol"], dr["from_port"], dr["to_port"], target.name, ))
             name.revoke(
-                ip_protocol = dr["ip_protocol"],
-                from_port = dr["from_port"],
-                to_port = dr["to_port"],
-                src_group = target
+                ip_protocol=dr["ip_protocol"],
+                from_port=dr["from_port"],
+                to_port=dr["to_port"],
+                src_group=target
             )
 
+
 def sg_delete(name, force=False):
     """Deletes a security group.
     If you attempt to delete a security group that contains instances, or is referenced
@@ -164,7 +167,8 @@ def sg_delete(name, force=False):
         if force:
             instances = [x for x in ec2_list('sec:%s' % (target.name, ))]
             if instances:
-                raise EC2LibraryError('%s is in use by %s.' % (target.name, ",".join(map(lambda x:x.name, instances)),))
+                raise EC2LibraryError('%s is in use by %s.' %
+                        (target.name, ",".join(map(lambda x: x.name, instances)),))
             _sg = connection.get_all_security_groups()
             for sg in filter(lambda x: x.name != target.name, _sg):
                 _sg_revoke_all_rules(sg, target)

+ 5 - 1
mico/lib/aws/iam.py

@@ -12,9 +12,11 @@ from boto.iam.connection import IAMConnection
 
 import mico.output
 
+
 class IAMLibraryError(Exception):
     """Models an IAM library error."""
 
+
 def iam_connect(region=None, *args, **kwargs):
     """Helper to connect to Amazon Web Services IAM, using identify provided
     by environment, as also optional region in arguments.
@@ -36,6 +38,7 @@ def iam_connect(region=None, *args, **kwargs):
 
     return connection
 
+
 def iam_cert_exists(filter_expr='*'):
     """Returns the list of server certificates which match with specified
     filter expression passed as argument or None if none match.
@@ -44,12 +47,13 @@ def iam_cert_exists(filter_expr='*'):
 
     # Ohh Holy shit!!!
     meta = conn.get_all_server_certs()['list_server_certificates_response']['list_server_certificates_result']['server_certificate_metadata_list']
-    _x = filter(lambda x:fnmatch(x["server_certificate_name"], filter_expr), meta)
+    _x = filter(lambda x: fnmatch(x["server_certificate_name"], filter_expr), meta)
     if _x:
         return _x[0]
     else:
         return _x
 
+
 def iam_cert_ensure(name, public, private, chain=None, path=None):
     """Ensure that the certificate which name is passed as argument exists,
     if not and content is provided, upload the new cert.

+ 8 - 3
mico/lib/aws/r53.py

@@ -9,9 +9,11 @@ from os import environ as os_environ
 from fnmatch import fnmatch
 from boto.route53.connection import Route53Connection
 
+
 class R53LibraryError(Exception):
     """Models a R53 library error."""
 
+
 def r53_connect(region=None, *args, **kwargs):
     """Helper to connect to Amazon Web Services Route53, using identify provided
     by environment, as also optional region in arguments.
@@ -33,21 +35,24 @@ def r53_connect(region=None, *args, **kwargs):
 
     return connection
 
+
 def r53_zones(name=None):
     conn = r53_connect()
 
     if name is None:
         return conn.get_zones()
     else:
-        return [ conn.get_zone(name) ]
+        return [conn.get_zone(name)]
+
 
 def r53_records(zone, name=None):
-    conn = r53_connect()
+    #  conn = r53_connect()
 
     if name is None:
         return zone.get_records()
     else:
-        return [ zone.get_record(name) ]
+        return [zone.get_record(name)]
+
 
 def r53_list(*args):
     """Get all records in R53.

+ 1 - 1
mico/lib/core/__init__.py

@@ -8,7 +8,7 @@ readability purposes each function type will be splitted in submodules, but
 for standard use, just need to import the core one. """
 
 from mico.lib.core.ssh import *
-from mico.lib.core.dir  import *
+from mico.lib.core.dir import *
 from mico.lib.core.file import *
 from mico.lib.core.sudo import *
 from mico.lib.core.user import *

+ 5 - 0
mico/lib/core/dir.py

@@ -6,6 +6,8 @@
 directories in the remote host."""
 
 import mico.output
+from mico import run
+
 
 def dir_attribs(location, mode=None, owner=None, group=None, recursive=False):
     """Updates the mode/owner/group for the given remote directory.
@@ -13,11 +15,13 @@ def dir_attribs(location, mode=None, owner=None, group=None, recursive=False):
     from mico.lib.core.file import file_attribs
     return file_attribs(location, mode=mode, owner=owner, group=group, recursive=recursive)
 
+
 def dir_exists(location):
     """Tells if there is a remote directory at the given location.
     """
     return (run("test -d '%s'" % location, force=True)[0].return_code == 0)
 
+
 def dir_remove(location, recursive=True):
     """Removes a directory
     """
@@ -26,6 +30,7 @@ def dir_remove(location, recursive=True):
         mico.output.info("removed directory %s" % location)
         return _x
 
+
 def dir_ensure(location, recursive=True, mode=None, owner=None, group=None):
     """Ensures that there is a remote directory at the given location,
     optionally updating its mode/owner/group.

+ 35 - 12
mico/lib/core/file.py

@@ -32,7 +32,7 @@ def file_attribs(location, mode=None, owner=None, group=None, recursive=False):
     recursive = recursive and "-R " or ""
     _x = None
     if mode:
-        _x = run('chmod %s %s "%s"' % (recursive, mode,  location))[0]
+        _x = run('chmod %s %s "%s"' % (recursive, mode, location))[0]
         mico.output.info("set attributes for %s to %s" % (location, mode))
     if owner:
         _x = run('chown %s %s "%s"' % (recursive, owner, location))[0]
@@ -43,10 +43,12 @@ def file_attribs(location, mode=None, owner=None, group=None, recursive=False):
 
     return _x
 
+
 from mico.lib.core.dir import dir_ensure
 
 from mico.lib.core.local import *
 
+
 def file_local_read(location):
     """Reads a *local* file from the given location, expanding '~' and
     shell variables.
@@ -54,6 +56,7 @@ def file_local_read(location):
     with file(os.path.expandvars(os.path.expanduser(location)), "rb") as f:
         return f.read()
 
+
 def file_read(location):
     """Reads the *remote* file at the given location.
 
@@ -65,23 +68,28 @@ def file_read(location):
         if _exe.return_code == 0:
             return base64.b64decode(_exe)
 
+
 def file_exists(location):
     """Tests if there is a *remote* file at the given location."""
     _exe = run("test -e '%s'" % (location), force=True)[0]
     return _exe.return_code == 0
 
+
 def file_is_file(location):
     _exe = run("test -f '%s'" % (location))[0]
     return _exe.return_code == 0
 
+
 def file_is_dir(location):
     _exe = run("test -d '%s'" % (location))[0]
     return _exe.return_code == 0
 
+
 def file_is_link(location):
     _exe = run("test -L '%s'" % (location))[0]
     return _exe.return_code == 0
 
+
 def file_attribs_get(location):
     """Return mode, owner, and group for remote path.
     Return mode, owner, and group if remote path exists, 'None'
@@ -97,6 +105,7 @@ def file_attribs_get(location):
     else:
         return None
 
+
 def file_md5(location):
     """Returns the MD5 sum (as a hex string) for the remote file at the given location.
 
@@ -109,6 +118,7 @@ def file_md5(location):
     if sig.return_code == 0:
         return sig.split(os.linesep)[-1].strip()
 
+
 def file_sha256(location):
     """Returns the SHA256 sum (as a hex string) for the remote file at the given location.
 
@@ -121,6 +131,10 @@ def file_sha256(location):
     if sig.return_code == 0:
         return sig.split(os.linesep)[-1].strip()
 
+
+from mico import run
+
+
 def file_write(location, content, mode=None, owner=None, group=None, check=True):
     """Writes the given content to the file at the given remote
     location, optionally setting mode/owner/group.
@@ -135,14 +149,17 @@ def file_write(location, content, mode=None, owner=None, group=None, check=True)
     # Upload the content if necessary
     if sig != file_md5(location):
         if is_local():
-            run("cp '%s' '%s'"%(lpath,location))
+            result = run("cp '%s' '%s'" % (lpath, location))
         else:
             # FIXME: Put is not working properly, I often get stuff like:
-            # Fatal error: sudo() encountered an error (return code 1) while executing 'mv "3dcf7213c3032c812769e7f355e657b2df06b687" "/etc/authbind/byport/80"'
+            # Fatal error: sudo() encountered an error (return code 1)
+            #  while executing 'mv "3dcf7213c3032c812769e7f355e657b2df06b687" "/etc/authbind/byport/80"'
             #fabric.operations.put(local_path, location, use_sudo=use_sudo)
             # Hides the output, which is especially important
             # See: http://unix.stackexchange.com/questions/22834/how-to-uncompress-zlib-data-in-unix
             result = run("echo '%s' | openssl base64 -A -d -out '%s'" % (base64.b64encode(content), location))[0]
+        if result != 0:
+            raise ExecutionError("Error when in copy operation")
     # Remove the local temp file
     os.fsync(fd)
     os.close(fd)
@@ -151,22 +168,25 @@ def file_write(location, content, mode=None, owner=None, group=None, check=True)
     if check:
         file_sig = file_md5(location)
         if file_sig != sig:
-            raise ExecutionError("Signature for '%s' does not match: got %s, expects %s" % (location, repr(file_sig), repr(sig)))
+            raise ExecutionError("Signature for '%s' does not match: got %s, expects %s" %
+                                                         (location, repr(file_sig), repr(sig)))
 
-    _x =  file_attribs(location, mode=mode, owner=owner, group=group)
+    _x = file_attribs(location, mode=mode, owner=owner, group=group)
     mico.output.info("created file %s" % (location, ))
     return _x
 
+
 def file_ensure(location, mode=None, owner=None, group=None):
     """Updates the mode/owner/group for the remote file at the given
     location.
     """
     if file_exists(location):
-        return file_attribs(location,mode=mode,owner=owner,group=group)
+        return file_attribs(location, mode=mode, owner=owner, group=group)
     else:
-        return file_write(location,"",mode=mode,owner=owner,group=group)
+        return file_write(location, "", mode=mode, owner=owner, group=group)
 
-def file_update(location, updater=lambda x:x):
+
+def file_update(location, updater=lambda x: x):
     """Updates the content of the given by passing the existing
     content of the remote file at the given location to the 'updater'
     function.
@@ -184,6 +204,7 @@ def file_update(location, updater=lambda x:x):
     mico.output.info("updated %d bytes into file %s" % (len(new_content), location, ))
     return _x
 
+
 def file_append(location, content, mode=None, owner=None, group=None):
     """Appends the given content to the remote file at the given
     location, optionally updating its mode/owner/group.
@@ -193,6 +214,7 @@ def file_append(location, content, mode=None, owner=None, group=None):
     mico.output.info("appended %d bytes into file %s" % (len(content), location, ))
     return _x
 
+
 def file_unlink(path):
     """Unlink or remove a file"""
     if file_exists(path):
@@ -200,6 +222,7 @@ def file_unlink(path):
         mico.output.info("removed file %s" % (location, ))
         return _x
 
+
 def file_link(source, destination, symbolic=True, mode=None, owner=None, group=None):
     """Creates a (symbolic) link between source and destination on the remote host,
     optionally setting its mode/owner/group.
@@ -264,7 +287,7 @@ def file_content(src, dst, env={}, mode=None, owner=None, group=None,
     jinja_env = Environment(loader=FileSystemLoader([os.path.dirname(src)]))
     jinja_tpl = jinja_env.get_template(os.path.basename(src))
 
-    local_env = dict([ (k,v) for (k,v) in __builtin__.env.items() ])
+    local_env = dict([(k, v) for (k, v) in __builtin__.env.items()])
     local_env.update(env)
 
     content = jinja_tpl.render(**local_env)
@@ -274,7 +297,7 @@ def file_content(src, dst, env={}, mode=None, owner=None, group=None,
             group=group)
 
     if file_exists(dst):
-        original  = file_read(dst)
+        original = file_read(dst)
         orig_attr = file_attribs_get(dst)
 
         if override_mode and mode:
@@ -284,8 +307,8 @@ def file_content(src, dst, env={}, mode=None, owner=None, group=None,
         if override_group and group:
             orig_attr["group"] = group
     else:
-        original  = ""
-        orig_attr = { "mode":mode, "group":group, "owner":owner }
+        original = ""
+        orig_attr = {"mode": mode, "group": group, "owner": owner}
 
     hash_original = hashlib.sha1(original).hexdigest()
     print local_env

+ 14 - 4
mico/lib/core/group.py

@@ -6,14 +6,18 @@
 groups in the remote host."""
 
 import mico.output
+from __builtin__ import run, group_check
+from Runtime import ExecutionError
+
 
 def group_create(name, gid=None):
     """Creates a group with the given name, and optionally given gid.
     """
-    _x = run("groupadd %s '%s'" % ( ("-g '%s'" % gid) if gid else "", name))[0]
+    _x = run("groupadd %s '%s'" % (("-g '%s'" % gid) if gid else "", name))[0]
     mico.output.info("created group %s" % name)
     return _x
 
+
 def group_exists(name):
     """Checks if there is a group defined with the given name.
 
@@ -29,6 +33,7 @@ def group_exists(name):
     else:
         return None
 
+
 def group_remove(name):
     """Remove a group which match with specified name.
     """
@@ -48,11 +53,12 @@ def group_ensure(name, gid=None):
     if not d:
         return group_create(name, gid)
     else:
-        if gid != None and d.get("gid") != gid:
+        if gid is not None and d.get("gid") != gid:
             _x = run("groupmod -g %s '%s'" % (gid, name))[0]
             mico.output.info("changed GID for group %s to %d" % (name, gid,))
             return _x
 
+
 def group_user_exists(group, user):
     """Checks if the given user is a member of the given group. It
     will return 'False' if the group does not exist.
@@ -63,6 +69,7 @@ def group_user_exists(group, user):
     else:
         return user in d["members"]
 
+
 def group_user_add(group, user):
     """Adds the given user/list of users to the given group/groups.
     """
@@ -74,6 +81,7 @@ def group_user_add(group, user):
         mico.output.info("added user %s into group %s" % (user, group,))
         return _x
 
+
 def group_user_ensure(group, user):
     """Ensure that a given user is a member of a given group.
     """
@@ -81,6 +89,7 @@ def group_user_ensure(group, user):
     if user not in d["members"]:
         return group_user_add(group, user)
 
+
 def group_user_del(group, user):
         """Remove the given user from the given group.
         """
@@ -88,12 +97,13 @@ def group_user_del(group, user):
             raise ExecutionError("group %s does not exists" % group)
 
         if group_user_exists(group, user):
-            group_for_user = run("cat /etc/group | egrep -v '^%s:' | grep '%s' | awk -F':' '{print $1}' | grep -v %s" % (group, user, user), force=True)[0]
+            group_for_user = run("cat /etc/group | egrep -v '^%s:' | grep '%s' | awk -F':' '{print $1}' | grep -v %s" %
+                             (group, user, user), force=True)[0]
             if group_for_user.return_code == 0:
                 group_for_user = group_for_user.splitlines()
                 if group_for_user:
                     _x = run("usermod -G '%s' '%s'" % (",".join(group_for_user), user))[0]
-                    mico.output.info("removed user %s from group %s" % (user,group,))
+                    mico.output.info("removed user %s from group %s" % (user, group,))
                     return _x
 
 

+ 11 - 7
mico/lib/core/local.py

@@ -7,11 +7,13 @@ machine."""
 
 import __builtin__
 
-from hashlib import sha1
+import os
+
 from jinja2 import Environment, FileSystemLoader
 
 import mico
 
+
 def local_content(src, var={}):
     """Read a file content (which is a jinja2 template), parser it using
     provided environment plus the global environment (the local one override
@@ -27,7 +29,7 @@ def local_content(src, var={}):
     jinja_env = Environment(loader=FileSystemLoader([os.path.dirname(src)]))
     jinja_tpl = jinja_env.get_template(os.path.basename(src))
 
-    local_env = dict([ (k,v) for (k,v) in __builtin__.env.items() ])
+    local_env = dict([(k, v) for (k, v) in __builtin__.env.items()])
     local_env.update(var)
 
     content = jinja_tpl.render(**local_env)
@@ -52,6 +54,8 @@ import subprocess
 from fabric import operations
 from StringIO import StringIO
 
+from mico import env
+
 
 def run_local(command, sudo=False, shell=True, pty=True, combine_stderr=None):
     """Local implementation of fabric.api.run() using subprocess.
@@ -68,8 +72,8 @@ def run_local(command, sudo=False, shell=True, pty=True, combine_stderr=None):
 
     mico.output.debug(command)
 
-    stderr   = subprocess.STDOUT if combine_stderr else subprocess.PIPE
-    process  = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=stderr)
+    stderr = subprocess.STDOUT if combine_stderr else subprocess.PIPE
+    process = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=stderr)
     out, err = process.communicate()
 
     # FIXME: Should stream the output, and only print it if fabric's properties allow it
@@ -77,9 +81,9 @@ def run_local(command, sudo=False, shell=True, pty=True, combine_stderr=None):
     # Wrap stdout string and add extra status attributes
     result = operations._AttributeString(out.rstrip('\n'))
     result.return_code = process.returncode
-    result.succeeded   = process.returncode == 0
-    result.failed      = not result.succeeded
-    result.stderr      = StringIO(err)
+    result.succeeded = (process.returncode == 0)
+    result.failed = not result.succeeded
+    result.stderr = StringIO(err)
     return result
 
 

+ 18 - 16
mico/lib/core/network.py

@@ -5,7 +5,6 @@
 """The network core submodule provide a useful way to manage network
 interfaces in remote machine."""
 
-import os
 import mico
 
 from fabric.api import hide
@@ -15,35 +14,38 @@ from fabric.api import settings
 def network_interfaces():
     """Return a list of available network interfaces in the remote host."""
     with settings(hide('running', 'stdout')):
-         res = mico.run("/sbin/ifconfig -s")[0]
-         return map(lambda line: line.split(' ')[0], res.splitlines()[1:])
+        res = mico.run("/sbin/ifconfig -s")[0]
+        return map(lambda line: line.split(' ')[0], res.splitlines()[1:])
+
 
 def network_address(iface=""):
     """Return a list of IP addresses associated with an specific interface
     or, if not provided, the full list of the system."""
     with settings(hide('running', 'stdout')):
-         res = mico.run("/sbin/ifconfig %s | grep 'inet addr'" % iface)[0]
-         return map(lambda x:x.split()[1].split(':')[1],
+        res = mico.run("/sbin/ifconfig %s | grep 'inet addr'" % iface)[0]
+        return map(lambda x: x.split()[1].split(':')[1],
                     res.splitlines())
 
+
 def network_netmask(iface=""):
     """Return a list of IP netmask associated with an specific interface
     or, if not provided, the full list of the system."""
     with settings(hide('running', 'stdout')):
-         res = mico.run("/sbin/ifconfig %s | grep 'inet addr'" % iface)[0]
-         ret = []
-         for _res in res.splitlines():
-             field = _res.split()[2]
-             if field.startswith("Mask"):
-                 ret.append( field.split(':')[1] )
-             else:
-                 field = res.split()[3]
-                 ret.append( field.split(':')[1] )
-         return ret
+        res = mico.run("/sbin/ifconfig %s | grep 'inet addr'" % iface)[0]
+        ret = []
+        for _res in res.splitlines():
+            field = _res.split()[2]
+            if field.startswith("Mask"):
+                ret.append(field.split(':')[1])
+            else:
+                field = res.split()[3]
+                ret.append(field.split(':')[1])
+        return ret
+
 
 def network_nameservers():
     """Return a list with the nameservers present in the remote system."""
     with settings(hide('running', 'stdout')):
         res = mico.run("grep ^nameserver /etc/resolv.conf")[0]
-        return map(lambda x:x.split()[1],res.splitlines())
+        return map(lambda x: x.split()[1], res.splitlines())
 

+ 13 - 10
mico/lib/core/package/__init__.py

@@ -7,11 +7,14 @@ package repositories into a host using a properly standard way."""
 
 import mico.lib.core.package.apt
 import mico.lib.core.package.yum
+from __builtin__ import env
+
 
 class UnknowDistributionError(Exception):
     """Model an error related with distribution of the operating system
     installed in the remote machine."""
 
+
 def repository_ensure(repository, key=None):
     """Ensures that a repository is installed in the remote host. This
     function works as a wrapper over installed backend engines for package
@@ -21,12 +24,12 @@ def repository_ensure(repository, key=None):
 
     if operating_system == "debian" or \
        operating_system == "ubuntu":
-           return mico.lib.core.package.apt.repository_ensure(repository, key)
+            return mico.lib.core.package.apt.repository_ensure(repository, key)
 
     if operating_system == "centos" or \
        operating_system == "redhat" or \
        operating_system == "fedora":
-           return mico.lib.core.package.yum.repository_ensure(repository)
+            return mico.lib.core.package.yum.repository_ensure(repository)
 
 
 def package_ensure(*args, **kwargs):
@@ -34,12 +37,12 @@ def package_ensure(*args, **kwargs):
 
     if operating_system == "debian" or \
        operating_system == "ubuntu":
-           return mico.lib.core.package.apt.package_ensure(*args, **kwargs)
+            return mico.lib.core.package.apt.package_ensure(*args, **kwargs)
 
     if operating_system == "centos" or \
        operating_system == "redhat" or \
        operating_system == "fedora":
-           return mico.lib.core.package.yum.package_ensure(*args, **kwargs)
+            return mico.lib.core.package.yum.package_ensure(*args, **kwargs)
 
 
 def package_update(*args, **kwargs):
@@ -47,12 +50,12 @@ def package_update(*args, **kwargs):
 
     if operating_system == "debian" or \
        operating_system == "ubuntu":
-           return mico.lib.core.package.apt.package_update(*args, **kwargs)
+            return mico.lib.core.package.apt.package_update(*args, **kwargs)
 
     if operating_system == "centos" or \
        operating_system == "redhat" or \
        operating_system == "fedora":
-           return mico.lib.core.package.yum.package_update(*args, **kwargs)
+            return mico.lib.core.package.yum.package_update(*args, **kwargs)
 
 
 def package_upgrade(*args, **kwargs):
@@ -60,12 +63,12 @@ def package_upgrade(*args, **kwargs):
 
     if operating_system == "debian" or \
        operating_system == "ubuntu":
-           return mico.lib.core.package.apt.package_upgrade(*args, **kwargs)
+            return mico.lib.core.package.apt.package_upgrade(*args, **kwargs)
 
     if operating_system == "centos" or \
        operating_system == "redhat" or \
        operating_system == "fedora":
-           return mico.lib.core.package.yum.package_upgrade(*args, **kwargs)
+            return mico.lib.core.package.yum.package_upgrade(*args, **kwargs)
 
 
 def package_remove(*args, **kwargs):
@@ -73,10 +76,10 @@ def package_remove(*args, **kwargs):
 
     if operating_system == "debian" or \
        operating_system == "ubuntu":
-           return mico.lib.core.package.apt.package_remove(*args, **kwargs)
+            return mico.lib.core.package.apt.package_remove(*args, **kwargs)
 
     if operating_system == "centos" or \
        operating_system == "redhat" or \
        operating_system == "fedora":
-           return mico.lib.core.package.yum.package_remove(*args, **kwargs)
+            return mico.lib.core.package.yum.package_remove(*args, **kwargs)
 

+ 24 - 5
mico/lib/core/package/apt.py

@@ -7,6 +7,10 @@ packages and package repositories into a host using yum utility."""
 
 import hashlib
 
+from __builtin__ import run
+from Runtime import ExecutionError
+
+
 def repository_ensure(repository, key):
     """Ensure that an APT repository exists.
     """
@@ -18,6 +22,7 @@ def repository_ensure(repository, key):
         hashlib.sha1(repository).hexdigest(),
     ))[0]
 
+
 def package_update(package=None):
     """Update the package list or a package list referer a package passed as
     argument using APT tools.
@@ -27,7 +32,10 @@ def package_update(package=None):
     else:
         if type(package) in (list, tuple):
             package = " ".join(package)
-        return run('DEBIAN_FRONTEND=noninteractive apt-get --yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade %s' % (package))[0]
+        return run(
+            'DEBIAN_FRONTEND=noninteractive apt-get --yes -o Dpkg::Options::="--force-confdef"' +
+            'o Dpkg::Options::="--force-confold" upgrade %s' % (package))[0]
+
 
 def package_upgrade(distupgrade=False):
     """Upgrade the system, or upgrade the distribution.
@@ -36,9 +44,14 @@ def package_upgrade(distupgrade=False):
         default).
     """
     if distupgrade:
-        return run('DEBIAN_FRONTEND=noninteractive apt-get --yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade')[0]
+        return run(
+            'DEBIAN_FRONTEND=noninteractive apt-get --yes -o Dpkg::Options::="--force-confdef"' +
+            '-o Dpkg::Options::="--force-confold" dist-upgrade')[0]
     else:
-        return run('DEBIAN_FRONTEND=noninteractive apt-get --yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade')[0]
+        return run(
+            'DEBIAN_FRONTEND=noninteractive apt-get --yes -o Dpkg::Options::="--force-confdef"' +
+            '-o Dpkg::Options::="--force-confold" upgrade')[0]
+
 
 def package_install(package, update=False):
     """Install a package using APT tool.
@@ -50,7 +63,8 @@ def package_install(package, update=False):
     :param update: when True, performs an update before (False by default).
     """
     if update:
-        _x = run('DEBIAN_FRONTEND=noninteractive apt-get --yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" update')[0]
+        _x = run('DEBIAN_FRONTEND=noninteractive apt-get --yes ' +
+                 '-o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" update')[0]
         if _x.return_code != 0:
             raise ExecutionError("Unable to update, but required")
 
@@ -59,6 +73,7 @@ def package_install(package, update=False):
 
     return run("DEBIAN_FRONTEND=noninteractive apt-get --yes install %s" % (package))[0]
 
+
 def package_ensure(package, update=False):
     """Ensure apt packages are installed.
 
@@ -78,6 +93,7 @@ def package_ensure(package, update=False):
             return package_update(package)
         return status
 
+
 def package_clean(package=None):
     """Clean APT cache.
 
@@ -88,6 +104,7 @@ def package_clean(package=None):
         package = " ".join(package)
     return run("DEBIAN_FRONTEND=noninteractive apt-get -y --purge remove '%s'" % package)[0]
 
+
 def package_remove(package, autoclean=False):
     """Remove APT package.
 
@@ -98,7 +115,9 @@ def package_remove(package, autoclean=False):
     :param autoclean: If set (False by default) execute autoclean afte
         remove.
     """
-    _x = run('DEBIAN_FRONTEND=noninteractive apt-get --yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" remove "%s"' % package)[0]
+    _x = run('DEBIAN_FRONTEND=noninteractive apt-get --yes' +
+             ' -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" remove "%s"' %
+              package)[0]
     if _x.return_code == 0:
         if autoclean:
             return run('apt-get --yes autoclean')[0]

+ 9 - 1
mico/lib/core/package/yum.py

@@ -6,29 +6,36 @@
 packages and package repositories into a host using yum utility.
 """
 
+from __builtin__ import run
+from Runtime import ExecutionError
+
+
 def repository_ensure(repository):
     """Ensure that a yum repo is present.
     """
     raise Exception("Not implemented for Yum")
 
+
 def package_upgrade():
     """Upgrade (update) yum cache.
     """
     return run("yum -y update")[0]
 
+
 def package_update(package=None):
     """Upgrade a package.
 
     :type package: str or list or tuple
     :param pagage: the package(s) name to be updated or None for all.
     """
-    if package == None:
+    if package is None:
         return run("yum -y update")[0]
     else:
         if type(package) in (list, tuple):
             package = " ".join(package)
         return run("yum -y upgrade " + package)[0]
 
+
 def package_install(package, update=False):
     """Install a package
 
@@ -48,6 +55,7 @@ def package_install(package, update=False):
         package = " ".join(package)
     return run("yum -y install %s" % (package))[0]
 
+
 def package_remove(package, autoclean=False):
     """Remove YUM package.
 

+ 16 - 6
mico/lib/core/service.py

@@ -14,6 +14,8 @@ from mico.lib.core.sudo import sudo as mico_sudo
 service_string = "service %(service)s %(action)s"
 
 import mico.hook
+from mico import env
+
 
 def service_is_running(service):
     """Check if a service is running."""
@@ -24,11 +26,13 @@ def service_is_running(service):
         }, force=True)
         return res.succeeded
 
+
 def service_ensure(service):
     """Ensure that a service is running."""
     if not service_is_running(service):
         return service_start(service)
 
+
 def service_start(service):
     """Start a system service."""
     mico.hook.add_post_hook(mico_sudo, (service_string % {
@@ -37,6 +41,7 @@ def service_start(service):
     },))
     mico.output.info("service %s enqueued to start" % service)
 
+
 def service_stop(service):
     """Stop a system service."""
     mico.hook.add_post_hook(mico_sudo, (service_string % {
@@ -45,46 +50,51 @@ def service_stop(service):
     },))
     mico.output.info("service %s enqueued to stop" % service)
 
+
 def service_restart(service):
     """Restart a system service."""
     mico.hook.add_post_hook(mico_sudo, (service_string % {
         "service": service,
-        "action":  "restart"
+        "action": "restart"
     },))
     mico.output.info("service %s enqueued to restart" % service)
 
+
 def service_reload(service):
     """Reload a system service (when available)."""
     mico.hook.add_post_hook(mico_sudo, (service_string % {
         "service": service,
-        "action":  "reload"
+        "action": "reload"
     },))
     mico.output.info("service %s enqueued to reload" % service)
 
+
 def service_add_boot(service):
     """Add a service into the boot pipeline."""
     if env.custom.operating_system == "ubuntu" or \
        env.custom.operating_system == "debian":
-           _x = mico_sudo("update-rc.d %s defaults" % service)
+            _x = mico_sudo("update-rc.d %s defaults" % service)
     if env.custom.operating_system == "redhat" or \
        env.custom.operating_system == "centos" or \
        env.custom.operating_system == "fedora":
-           _x = mico_sudo("chkconfig '%s' on" % service)
+            _x = mico_sudo("chkconfig '%s' on" % service)
     mico.output.info("service %s added to booting services" % service)
     return _x
 
+
 def service_del_boot(service):
     """Remove a service from the boot pipeline."""
     if env.custom.operating_system == "ubuntu" or \
        env.custom.operating_system == "debian":
-           _x = mico_sudo("update-rc.d -f %s remove" % service)
+            _x = mico_sudo("update-rc.d -f %s remove" % service)
     if env.custom.operating_system == "redhat" or \
        env.custom.operating_system == "centos" or \
        env.custom.operating_system == "fedora":
-           _x = mico_sudo("chkconfig '%s' off" % service)
+            _x = mico_sudo("chkconfig '%s' off" % service)
     mico.output.info("service %s removed to booting services" % service)
     return _x
 
+
 def service_ensure_boot(service):
     """Ensure that a service will be started on boot."""
     return service_add_boot(service)

+ 6 - 3
mico/lib/core/ssh.py

@@ -9,10 +9,12 @@ import os
 
 import mico.output
 from mico.lib.core.dir import dir_ensure
-from mico.lib.core.sudo import mode_sudo
 from mico.lib.core.user import user_exists
 from mico.lib.core.file import file_attribs, file_exists, \
                                file_read, file_append, file_write
+from __builtin__ import run
+from Runtime import ExecutionError
+
 
 def ssh_keygen(user, key_type="rsa"):
     """Generates a pair of ssh keys in the user's home .ssh directory.
@@ -33,14 +35,15 @@ def ssh_keygen(user, key_type="rsa"):
             ))[0]
             mico.output.info("created ssh-key for user %s" % user)
             if _x.return_code == 0:
-                _x = file_attribs(os.path.join(d["home"],".ssh/id_%s" % key_type), owner=user)
+                _x = file_attribs(os.path.join(d["home"], ".ssh/id_%s" % key_type), owner=user)
                 if _x.return_code == 0:
-                    return file_attribs(os.path.join(d["home"],".ssh/id_%s.pub" % key_type), owner=user)
+                    return file_attribs(os.path.join(d["home"], ".ssh/id_%s.pub" % key_type), owner=user)
                 else:
                     return _x
             else:
                 return _x
 
+
 def ssh_authorize(user, key):
     """Adds the given key to the '.ssh/authorized_keys' for the given
     user."""

+ 5 - 1
mico/lib/core/sudo.py

@@ -6,9 +6,11 @@
 in sudo mode."""
 
 from mico.util.switch import Switcher
+from __builtin__ import env, run
 
 ENV_KEY_SUDO_PASSWORD = "sudo_password"
-ENV_KEY_SUDO_MODE     = "sudo_mode"
+ENV_KEY_SUDO_MODE = "sudo_mode"
+
 
 def sudo_password(password=None):
     """Set the password to be used with sudo
@@ -28,10 +30,12 @@ def sudo_password(password=None):
 
 mode_sudo = Switcher.from_key(ENV_KEY_SUDO_MODE, True)
 
+
 def is_sudo():
     """Return true if the current execution mode is sudo-enabled"""
     return mode_sudo.getValue(ENV_KEY_SUDO_MODE)
 
+
 def sudo(*args, **kwargs):
     """A wrapper to Fabric's run/sudo commands, using the
     mode_sudo global to tell whether the command should be run as

+ 5 - 3
mico/lib/core/ulimit.py

@@ -5,6 +5,9 @@
 """The ulimit core submodule provide a useful way to manage system
 ulimits."""
 
+from mico import run
+
+
 def ulimit_ensure(limits):
     """Ensure user limits.
 
@@ -20,9 +23,8 @@ def ulimit_ensure(limits):
         limits = limits.split("\n")
 
     # Remove extra spaces
-    limits = map(lambda x:' '.join(x.split()), limits)
+    limits = map(lambda x: ' '.join(x.split()), limits)
 
     for limit in limits:
         _x = run("sed 's:[ \\t][ \\t]*: :g' /etc/security/limits.conf " + \
-                 "| grep '%s' || ( echo '%s' >> /etc/security/limits.conf; )" % (limit,limit,))[0]
-
+                 "| grep '%s' || ( echo '%s' >> /etc/security/limits.conf; )" % (limit, limit,))[0]

+ 13 - 5
mico/lib/core/user.py

@@ -11,18 +11,23 @@ import base64
 import mico.output
 from mico.lib.core.group import group_exists
 
+from __builtin__ import run, group_check
+from Runtime import ExecutionError
+
+
 def user_password(name, password, encrypted_password=False):
     """Sets the given user password.
     """
     encoded_password = base64.b64encode("%s:%s" % (name, password))
     if encrypted_password:
-        _x = run("usermod -p '%s' '%s'" % (password,name))[0]
+        _x = run("usermod -p '%s' '%s'" % (password, name))[0]
     else:
         _x = run("echo '%s' | openssl base64 -A -d | chpasswd" % (encoded_password))[0]
 
     mico.output.info("set password for user %s" % name)
     return _x
 
+
 def user_create(name, password=None, home=None, uid=None, gid=None, shell=None,
                 uid_min=None, uid_max=None, encrypted_password=False,
                 fullname=None):
@@ -49,10 +54,11 @@ def user_create(name, password=None, home=None, uid=None, gid=None, shell=None,
         options.append("--gecos='%s'" % (fullname))
     _x = run("useradd %s '%s'" % (" ".join(options), name))[0]
     if password:
-        return user_password(name=name,password=password,encrypted_password=encrypted_password)
+        return user_password(name=name, password=password, encrypted_password=encrypted_password)
     else:
         return  _x
 
+
 def user_exists(name=None, uid=None):
     """Checks if there is a user defined with the given name.
 
@@ -68,7 +74,7 @@ def user_exists(name=None, uid=None):
     if name is not None and uid is not None:
         raise ExecutionError("user_exists require name or uid, but not both")
 
-    if name != None:
+    if name is not None:
         d = run("cat /etc/passwd | egrep '^%s:'" % (name), force=True)[0]
     else:
         d = run("cat /etc/passwd | egrep '^.*:.*:%s:'" % (uid), force=True)[0]
@@ -83,7 +89,8 @@ def user_exists(name=None, uid=None):
         if len(d) < 7:
             raise ExecutionError("malformed /etc/password, 7 fields expected")
         else:
-            return dict(zip(("name", "password", "uid", "gid", "gecos", "home", "shell"),d))
+            return dict(zip(("name", "password", "uid", "gid", "gecos", "home", "shell"), d))
+
 
 def user_ensure(name, password=None, home=None, uid=None, gid=None,
         shell=None, fullname=None, encrypted_password=False):
@@ -91,7 +98,8 @@ def user_ensure(name, password=None, home=None, uid=None, gid=None,
     """
 
     if not user_exists(name):
-        return user_create(name, password, home, uid, gid, shell, fullname,encrypted_password)
+        return user_create(name, password, home, uid, gid, shell, fullname, encrypted_password)
+
 
 def user_remove(name, rm_home=False):
     """Removes the user with the given name, optionally

+ 35 - 21
mico/output.py

@@ -1,3 +1,4 @@
+
 #! /usr/bin/env python
 # -*- encoding: utf-8 -*-
 # vim:fenc=utf-8:
@@ -9,12 +10,13 @@ import inspect
 import mico
 from mico.lib.core.local import is_local
 
+
 intros = [
         "the monkey army for the cloud",
         "just see! a monkey flying in the cloud",
         "uuuh uhuh uhuhuhuhu uh uh uh!",
         "the monkey driven cloud management",
-        "oh no! monkys are learning to fly!",
+        "oh no! monkeys are learning to fly!",
         "take your stinking paws off me, you dammed dirty ape!"
 ]
 
@@ -26,7 +28,7 @@ monkey = """
             \ '- ,\.-"`` ``"-./, -' /
              `'-' /_   ^ ^   _\\ '-'`
              .--'|  \._   _./  |'--.
-           /`    \   \ `~` /   /    `\\ 
+           /`    \   \ `~` /   /    `\\
           /       '._ '---' _.'       \\
          /           '~---~'   |       \\
         /        _.             \\       \\
@@ -49,13 +51,13 @@ monkey = """
 dump_keys = [
         "created_time", "default_cooldown", "desired_capacity", "min_size",
         "max_size", "health_check_period", "health_check_type",
-        "launch_config_name", 'reason', 'description','endElement', 'end_time',
+        "launch_config_name", 'reason', 'description', 'endElement', 'end_time',
         'progress', 'startElement', 'start_time', 'status_code',
         'adjustment_type', 'alarms', 'as_name', 'cooldown',
-        'min_adjustment_step','scaling_adjustment', "status",
+        'min_adjustment_step', 'scaling_adjustment', "status",
         'comparison', 'dimensions', 'disable_actions', 'create_time',
         'enable_actions', 'evaluation_periods', 'zone',
-        'last_updated','metric', 'instance_id', 'id', 'state',
+        'last_updated', 'metric', 'instance_id', 'id', 'state',
         'period', 'set_state', 'autoscaling_group', 'snapshot_id',
         'state_reason', 'state_value', 'statistic', 'type',
         'threshold', 'total_instances', 'device', 'size',
@@ -70,11 +72,12 @@ prompt_inf = os.environ.get("MICO_PS3", None) or "mico: "
 prompt_msg = os.environ.get("MICO_PS4", None) or "mico: "
 prompt_dbg = os.environ.get("MICO_PS4", None) or "mico: "
 
-env.loglevel = set([ "abort", "error", "warn", "info" ])
+env.loglevel = set(["abort", "error", "warn", "info"])
+
 
 def label():
     if env.get("host_label", False) and \
-       env.get("host_string",False) and \
+       env.get("host_string", False) and \
        env["host_label"][env["host_string"]]:
         return env["host_label"][env["host_string"]]
 
@@ -86,42 +89,51 @@ def label():
 
     return "cloud"
 
+
 def stack():
     return inspect.stack()[2][3]
 
+
 def abort(message):
     if "abort" in env.loglevel:
         print >> sys.stderr, "%s%s: %s: %s" % (prompt_err, label(), stack(), message)
     return message
 
+
 def error(message, *args, **kwargs):
     if "error" in env.loglevel:
         print >> sys.stderr, "%s%s: %s: %s" % (prompt_err, label(), stack(), message)
     return message
 
+
 def warn(message):
     if "warn" in env.loglevel:
         print >> sys.stderr, "%s%s: %s: %s" % (prompt_inf, label(), stack(), message)
     return message
 
+
 def puts(message, *args, **kwargs):
     if "info" in env.loglevel:
         print >> sys.stderr, "%s%s: %s: %s" % (prompt_inf, label(), stack(), message)
     return message
 
+
 def info(message, *args, **kwargs):
     if "info" in env.loglevel:
         print >> sys.stdout, "%s%s: %s: %s" % (prompt_msg, label(), stack(), message)
     return message
 
+
 def debug(message):
     if "debug" in env.loglevel:
         print >> sys.stdout, "%s%s: %s: %s" % (prompt_dbg, label(), stack(), message)
     return message
 
+
 def mute(*args, **kwargs):
     pass
 
+
 def _vars(obj):
     """Internal emulation for builtin var. Not all boto objects provides
     __dict__ function.
@@ -130,8 +142,9 @@ def _vars(obj):
     #if hasattr(obj, "__dict__"):
     #    return vars(obj)
 
-    return { k.strip("_"): getattr(obj,k) for k in filter(lambda
-        x:not hasattr(getattr(obj,x),"__call__") and not x.startswith("__"), dir(obj)) }
+    return {k.strip("_"): getattr(obj, k) for k in filter(lambda
+        x: not hasattr(getattr(obj, x), "__call__") and not x.startswith("__"), dir(obj))}
+
 
 def _str(obj):
     if hasattr(obj, "name"):
@@ -143,22 +156,23 @@ def _str(obj):
     else:
         return str(obj)
 
+
 def dump(obj, layout="horizontal", color=True):
     if color:
-        s = "%s:" % getattr(obj, "name","None")
+        s = "%s:" % getattr(obj, "name", "None")
     else:
-        s = "%s:" % getattr(obj, "name","None")
+        s = "%s:" % getattr(obj, "name", "None")
 
     v = _vars(obj)
     for key in v:
         if key in dump_keys or "debug" in env.loglevel:
             if v[key] is None:
                 continue
-            if isinstance(v[key],list):
-                _val = ", ".join(map(_str,v[key]))
+            if isinstance(v[key], list):
+                _val = ", ".join(map(_str, v[key]))
             elif isinstance(v[key], dict):
                 _val = "{ "
-                _val += ", ".join(map(lambda x:"%s:%s" % (x[0], _str(x[1]),), v[key].items()))
+                _val += ", ".join(map(lambda x: "%s:%s" % (x[0], _str(x[1]),), v[key].items()))
                 _val += " }"
             else:
                 _val = v[key]
@@ -167,12 +181,12 @@ def dump(obj, layout="horizontal", color=True):
             if layout == "vertical":
                 s += "\n  "
             if color:
-                s+=" %s = %s" % (key, _val)
+                s += " %s = %s" % (key, _val)
             else:
-                s+=" %s = %s" % (key, _val)
+                s += " %s = %s" % (key, _val)
 
     if layout == "vertical":
-        s+="\n"
+        s += "\n"
 
     print >> sys.stdout, s
 
@@ -180,7 +194,7 @@ def dump(obj, layout="horizontal", color=True):
 from fabric.state import output
 # XXX Supress some fabric messages, actually fabric message API is not very
 # useful nor well designed.
-output["debug"] =  False
+output["debug"] = False
 output["running"] = False
 output["stderr"] = False
 output["stdout"] = False
@@ -192,7 +206,7 @@ import fabric.operations
 # XXX fabric monkey patching for message output
 fabric.operations.abort = fabric.utils.abort = fabric.tasks.abort = abort
 fabric.operations.error = fabric.utils.error = fabric.tasks.error = mute
-fabric.operations.warn  = fabric.utils.warn  = fabric.tasks.warn  = mute
-fabric.operations.puts  = fabric.utils.puts  = fabric.utils.puts  = mute
-fabric.operations.fastprint = fabric.utils.fastprint  = fabric.utils.fastprint  = puts
+fabric.operations.warn = fabric.utils.warn = fabric.tasks.warn = mute
+fabric.operations.puts = fabric.utils.puts = fabric.utils.puts = mute
+fabric.operations.fastprint = fabric.utils.fastprint = fabric.utils.fastprint = puts
 

+ 5 - 6
mico/path.py

@@ -7,7 +7,6 @@ used in mico, reading environment variables when needed.
 """
 
 import os
-import sys
 
 
 def get_stack_path(env_var="MICO_STACK_PATH", path=[]):
@@ -28,13 +27,13 @@ def get_stack_path(env_var="MICO_STACK_PATH", path=[]):
     """
     _path = [
         os.curdir,
-        os.path.join(os.environ.get("HOME","/"), ".config/mico"),
+        os.path.join(os.environ.get("HOME", "/"), ".config/mico"),
         "/etc/mico",
         os.path.join(os.path.dirname(__file__), "stack"),
     ]
 
     if os.environ.get(env_var, None):
-       _path.insert(0, os.environ.get(env_var))
+        _path.insert(0, os.environ.get(env_var))
 
     if path:
         _path.extend(path)
@@ -44,7 +43,7 @@ def get_stack_path(env_var="MICO_STACK_PATH", path=[]):
 
 def get_cache_path(env_var="MICO_CACHE_PATH"):
     return os.environ.get("MICO_CACHE_PATH", None) or \
-           os.path.join(os.environ.get("HOME","/"), ".cache/mico")
+         os.path.join(os.environ.get("HOME", "/"), ".cache/mico")
 
 
 def get_library_path(env_var="MICO_LIBRARY_PATH", path=[]):
@@ -66,7 +65,7 @@ def get_library_path(env_var="MICO_LIBRARY_PATH", path=[]):
     """
     _path = [
         os.curdir,
-        os.path.join(os.environ.get("HOME","/"), ".local/share/mico"),
+        os.path.join(os.environ.get("HOME", "/"), ".local/share/mico"),
         "/usr/lib/mico",
         "/usr/local/lib/mico",
         "/usr/share/mico",
@@ -74,7 +73,7 @@ def get_library_path(env_var="MICO_LIBRARY_PATH", path=[]):
     ]
 
     if os.environ.get(env_var, None):
-       _path.insert(0, os.environ.get(env_var))
+        _path.insert(0, os.environ.get(env_var))
 
     if path:
         _path.extend(path)

+ 6 - 3
mico/run.py

@@ -11,6 +11,9 @@ import fabric.api
 from fabric.tasks import execute as fabric_execute
 
 import mico.hook
+import mico.run
+from mico import env
+
 
 from mico.lib.core.sudo import is_sudo
 from mico.lib.core.local import is_local, run_local
@@ -33,9 +36,9 @@ def execute(action, output, *args, **kwargs):
             return action(*args, **kwargs)
 
     return fabric_execute(
-             mico.hook.task_add_pre_run_hook( mico.hook.run_pre_hook )
-                ( mico.hook.task_add_post_run_hook( mico.hook.run_post_hook )
-                    ( run )), *args, **kwargs )
+             mico.hook.task_add_pre_run_hook(mico.hook.run_pre_hook)
+                (mico.hook.task_add_post_run_hook(mico.hook.run_post_hook)
+                    (run)), *args, **kwargs)
 
 
 def run(*args, **kwargs):

+ 1 - 0
mico/script/__init__.py

@@ -0,0 +1 @@
+# -*- coding: utf-8 -*-

+ 32 - 32
mico/script/cmdline.py

@@ -2,7 +2,6 @@
 # -*- encoding: utf-8 -*-
 # vim:fenc=utf-8:
 
-import os
 import sys
 import cmd
 import shlex
@@ -17,21 +16,26 @@ import mico.output
 
 from mico.stack import Stack
 
+from mico import execute
+
+from __builtin__ import env, split
+
 
 class MicoCmdline(cmd.Cmd):
     """The command line console which will be invoked from mico script."""
 
-    ruler     = '-'
-    prompt    = mico.output.prompt_usr
-    intro     = mico.output.prompt_msg + random.choice(mico.output.intros)
+    ruler = '-'
+    prompt = mico.output.prompt_usr
+    intro = mico.output.prompt_msg + random.choice(mico.output.intros)
 
     def complete(self, text, state):
         "Return a list of completion options for console."
 
         _stack_path = mico.path.get_stack_path()
 
-        options =  [i for i in map(lambda x:x[3:], filter(lambda x:x.startswith("do_"), dir(self) )) if i.startswith(text) if i != "EOF" ]
-        options += [i for j in map(pkgutil.iter_modules, _stack_path) for _,i,_ in j]
+        options = [i for i in map(lambda x: x[3:],
+            filter(lambda x: x.startswith("do_"), dir(self))) if i.startswith(text) if i != "EOF"]
+        options += [i for j in map(pkgutil.iter_modules, _stack_path) for _, i, _ in j]
 
         if state < len(options):
             return options[state]
@@ -42,10 +46,10 @@ class MicoCmdline(cmd.Cmd):
         pass
 
     def do_set(self, args):
-        "Set an environment variable, in teh form variable=value"
+        """Set an environment variable, in teh form variable=value"""
         if "=" in args:
             args = args.split("=")
-            val  = " ".join(args[1:])
+            val = " ".join(args[1:])
 
             if val == "True" or val == "true":
                 val = True
@@ -55,22 +59,20 @@ class MicoCmdline(cmd.Cmd):
                 val = "'%s'" % val
 
             try:
-                eval("env.__setitem__('%s',%s)" % ( args[0], val ))
+                eval("env.__setitem__('%s',%s)" % (args[0], val))
             except Exception, e:
                 mico.output.error("invalid evaluation: %s" % e)
         else:
             mico.output.error("invalid syntax, required var=value")
 
-
     def do_host(self, host=[]):
-        "Set hosts where command must run."
+        """Set hosts where command must run."""
 
         if host:
             if "," in host:
                 host = host.split(",")
             else:
-                host = [ host ]
-
+                host = [host]
             env.hosts = host
 
     def do_env(self, args):
@@ -79,12 +81,11 @@ class MicoCmdline(cmd.Cmd):
             if " " in args:
                 args = split(" ")