templates.rst 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Writing templates
  2. -----------------
  3. Templates are the base of mico. Mico by default looking for templates in
  4. current working directory as well as ``/etc/mico`` and ``~/.config/mico``.
  5. Template is a python file which can access to all mico builtins methods
  6. (like ``@serial`` or ``@async`` decorators), and also functions in libraries
  7. where imported.
  8. To call a template just run mico with the template name as first argument.
  9. Optionally you can specify a function to run, in the form::
  10. $ mico template:function
  11. If not function specified, then use ``main`` function by default.
  12. Here are a basic template for mico:
  13. .. code-block:: python
  14. from mico.lib.aws import *
  15. from mico.lib.core import *
  16. def deploy(\*args):
  17. for host in args:
  18. host, domain = host.split('.', 1) \
  19. if '.' in host \
  20. else (host, 'localdomain')
  21. sg_test = sg_ensure(
  22. name = "sg-test",
  23. description = "security group to test mico",
  24. rules = [
  25. sg_rule(
  26. protocol = "tcp",
  27. source = "0.0.0.0/0",
  28. port = "22"
  29. ),
  30. sg_rule(
  31. protocol = "tcp",
  32. source = "0.0.0.0/0",
  33. port = "80"
  34. )
  35. ]
  36. )
  37. instance = ec2_ensure(
  38. ami = "ami-3d4ff254",
  39. name = host,
  40. instance_type = "t1.micro",
  41. key_name = "root-us-east-virginia",
  42. security_groups = sg_test
  43. )
  44. .. note:: You don't need to be worried about how to link EC2 instance with
  45. host where local commands will be executed. In previous example the
  46. *pkg_ensure* command will be executed rightly in the instance which has
  47. been created in the previous line with the *ec2_ensure* command.
  48. This works fine, because mico works sequentially for each task.