complex.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from mico.lib.aws.ec2 import *
  2. from mico.lib.core import *
  3. USERDATA = """
  4. #cloud-config
  5. hostname: %s
  6. fqdn: %s
  7. manage_etc_hosts: true
  8. locale: C
  9. timezone: Europe/Madrid
  10. apt_update: true
  11. apt_upgrade: true
  12. """
  13. def main(*args):
  14. "Deploy the entire nosy production environment"
  15. import pickle
  16. pickle.dump(create_security,open("/tmp/caca","w"))
  17. for arg in args:
  18. create_security(arg)
  19. create_instance(arg)
  20. install_nginx(arg)
  21. install_uwsgi(arg)
  22. install_python(arg)
  23. create_users(arg)
  24. configure_sudoers(arg)
  25. install_global_pip_packages(arg)
  26. create_virtualenv(arg)
  27. start_services(arg)
  28. def create_security(args):
  29. "Create security groups required for the environment."
  30. sg_frontend = sg_ensure(
  31. name = "frontend",
  32. description = 'frontend security group',
  33. rules = [
  34. sg_rule(
  35. protocol = "tcp",
  36. source = "0.0.0.0/0",
  37. port = "22"
  38. ),
  39. sg_rule(
  40. protocol = "tcp",
  41. source = "0.0.0.0/0",
  42. port = "80"
  43. )
  44. ]
  45. )
  46. def create_instance(arg):
  47. "Create EBS instance for nosy."
  48. instance = ec2_ensure(
  49. ami = "ami-d0f89fb9",
  50. name = arg,
  51. instance_type = "t1.micro",
  52. user_data = USERDATA % ( arg, "%s.example.com" % arg,) ,
  53. key_name = "example",
  54. security_groups = [ "frontend" ]
  55. )
  56. def install_nginx(arg):
  57. "Install nginx host."
  58. with mode_sudo():
  59. package_ensure("nginx-full")
  60. service_ensure_boot("nginx")
  61. def install_uwsgi(args):
  62. "Install the uwsgi components into the system."
  63. with mode_sudo():
  64. package_ensure("uwsgi")
  65. package_ensure("uwsgi-infrastructure-plugins")
  66. package_ensure("uwsgi-plugin-python")
  67. def install_python(args):
  68. "Install python environment."
  69. with mode_sudo():
  70. package_ensure("python")
  71. package_ensure("python-virtualenv")
  72. package_ensure("python-pip")
  73. def create_users(args):
  74. "Create users for host."
  75. with mode_sudo():
  76. user_ensure("ajdiaz")
  77. group_user_add("sudo","ajdiaz")
  78. ssh_authorize("ajdiaz","ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDX0Ila4UIQkgYRCTeHGzXkZbqYEpwwDlUDDM3oM4j2/hkrd9AY5p/ug921TpN4qi5pooSRwcD5ZiWZNqzPfEefl4A+3pGVfObN3NNUbc0Iry/T6MRdmnWB3LQxc2R45UOhVjLTuCKWRNLyZ1A7sIp7yrBt36BHoSoL40AIBbdEp37oSgubCI953UGNIN70BGAF/Cm0SW5f47NqDM2N2Fz/LA6Zf3NYXqYRzkOkpHxJ10DUvaAXoLiFVQPEGVdgwQJpIUdGZYkhPbgs5yBhBU5y2BLABJb/b1Yt3Yl6qSuBNOPP4oIMYjkUWXHu81hYJe68GpBwWN1AwGv3g7LeFOcx near.to")
  79. def configure_sudoers(args):
  80. "Configure sudoers file."
  81. with mode_sudo():
  82. file_content(
  83. src="data/sudoers.tpl",
  84. dst="/etc/sudoers2",
  85. )
  86. def install_global_pip_packages(args):
  87. "Install pip packages for host."
  88. PIP_PACKAGES = [ "redis", "bottle" ]
  89. for package in PIP_PACKAGES:
  90. sudo("pip install --upgrade %s" % package)
  91. def create_virtualenv(args):
  92. "Create virtualenv for host."
  93. with mode_sudo():
  94. dir_ensure("/srv/app/src")
  95. dir_ensure("/srv/app/env")
  96. sudo("virtualenv /srv/app/env")
  97. def start_services(args):
  98. "Start boot services if not running."
  99. service_ensure("nginx")
  100. service_reload("nginx")
  101. service_ensure("uwsgi")
  102. service_restart("uwsgi")
  103. service_ensure_boot("uwsgi")