Class: Circus::Profiles::Django

Inherits:
PythonBase show all
Defined in:
lib/circus/profiles/django.rb

Constant Summary collapse

DJANGO_APP_PROPERTY =
'django-app'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PythonBase

#prepare_for_dev

Methods inherited from Base

#cleanup_after_deploy, #extra_dirs, #mark_for_persistent_run?, #package_base_dir?, #package_for_deploy, #package_for_dev, #supported_for_development?

Constructor Details

#initialize(name, dir, props) ⇒ Django

Returns a new instance of Django.



15
16
17
18
19
# File 'lib/circus/profiles/django.rb', line 15

def initialize(name, dir, props)
  super(name, dir, props)
  
  @manage_script = props[DJANGO_APP_PROPERTY] || "manage.py"
end

Class Method Details

.accepts?(name, dir, props) ⇒ Boolean

Checks if this is a Django application. Will accept the application if it has a file named manage.py, or has a ‘django-app’ property describing the entry point.

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/circus/profiles/django.rb', line 10

def self.accepts?(name, dir, props)
  return true if props.include? DJANGO_APP_PROPERTY
  return File.exists?(File.join(dir, "manage.py"))
end

Instance Method Details

#deploy_run_script_contentObject



52
53
54
55
56
57
58
# File 'lib/circus/profiles/django.rb', line 52

def deploy_run_script_content
  shell_run_script do
    <<-EOT
    exec vendor/bin/python #{@manage_script} runserver --noreload 0.0.0.0:$LISTEN_PORT
    EOT
  end
end

#dev_run_script_contentObject



43
44
45
46
47
48
49
50
# File 'lib/circus/profiles/django.rb', line 43

def dev_run_script_content
  shell_run_script do
    <<-EOT
    cd #{@dir}
    exec vendor/bin/python #{@manage_script} runserver
    EOT
  end
end

#nameObject

The name of this profile



22
23
24
# File 'lib/circus/profiles/django.rb', line 22

def name
  "django"
end

#prepare_for_deploy(logger, overlay_dir) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/circus/profiles/django.rb', line 26

def prepare_for_deploy(logger, overlay_dir)
  return false unless super
  
  File.open(File.join(overlay_dir, 'local_settings.py'), 'w') do |f|
      f.write <<-EOT
import os
      
DATABASE_ENGINE = 'postgresql_psycopg2'
DATABASE_NAME = os.getenv('DATABASE_NAME')
DATABASE_USER = os.getenv('DATABASE_USER')
DATABASE_PASSWORD = os.getenv('DATABASE_PASSWORD')
DATABASE_HOST = os.getenv('DATABASE_HOST')
      EOT
  end
  true
end

#requirementsObject

Describes the requirements of the deployed application. Django applications automatically require a database with accessible credentials.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/circus/profiles/django.rb', line 62

def requirements
  res = super

  db_name = @props['database_name'] || @name
  res['resources'] ||= []
  res['resources'] << {
      'type' => 'Postgres',
      'name' => db_name,
      'user' => @props['database_user'] || db_name,
      'password' => @props['database_password'] || db_name
    }
    
  # TODO: The clown should be able to automatically allocate listening ports
  res['system-properties'] ||= {}
  res['system-properties']['LISTEN_PORT'] = 3000
  
  # TODO: The clown should be able to automatically respond with DB details
  res['system-properties']['DATABASE_NAME'] = db_name
  res['system-properties']['DATABASE_USER'] = db_name
  res['system-properties']['DATABASE_PASSWORD'] = @props['database_password'] || db_name
  res['system-properties']['DATABASE_HOST'] = 'localhost'
  
  res
end