Class: EYCli::Controller::Environments

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_cli/controllers/environments.rb

Instance Method Summary collapse

Instance Method Details

#create(app, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ey_cli/controllers/environments.rb', line 4

def create(app, options = {})
  framework_env = options[:framework_env] || 'production'
  env_name      = options[:name] || "#{app.name}_#{framework_env}"

  create_options = {
    :app                               => app,
    'environment[name]'                => env_name,
    'environment[framework_env]'       => framework_env
  }
  create_options.merge! fetch_custom_arguments(options)

  create_options.merge! fetch_cluster_options(options[:cluster_configuration])
  env = EYCli::Model::Environment.create(create_options)

  if env.errors?
    EYCli.term.print_errors(env.errors, "Environment creation failed:")
  else
    EYCli.term.success("Environment '#{env.name}' created successfully")
  end
  env
end

#deploy(app, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ey_cli/controllers/environments.rb', line 26

def deploy(app, options = {})
  env = fetch_environment(app, options)

  if env
    deploy = env.deploy(app, options)

    if deploy.errors?
      EYCli.term.print_errors(deploy.errors, "Application deployment failed:")
    else
      EYCli.term.success('Application deployed successfully')
    end
  else
    EYCli.term.say('Nothing deployed')
  end
end

#fetch_cluster_options(options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ey_cli/controllers/environments.rb', line 42

def fetch_cluster_options(options)
  return {} unless options
  cluster = {
    'cluster_configuration[configuration]' => options[:configuration],
    'cluster_configuration[ip_id]'         => 'new'
  }
  if options[:configuration] == 'custom'
    cluster['cluster_configuration[app_server_count]'] = options[:app_instances] || 2
    cluster['cluster_configuration[use_separate_db]']  = options[:use_separate_db] || 'true'
    cluster['cluster_configuration[db_slave_count]']   = options[:db_instances] || 0
    cluster['cluster_configuration[instance_size]']    = options[:app_size] if options[:app_size]
    cluster['cluster_configuration[db_instance_size]'] = options[:db_size] if options[:db_size]
  end
  cluster
end

#fetch_custom_arguments(options) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/ey_cli/controllers/environments.rb', line 58

def fetch_custom_arguments(options)
  custom = {}
  custom['app_deployment[new][domain_name]'] = options[:url] if options[:url]
  custom['environment[app_server_stack_name]'] = options[:stack] if options[:stack]
  custom['environment[db_stack_name]'] = options[:db_stack] if options[:db_stack]
  custom['environment[ruby_version]'] = options[:ruby_version] if options[:ruby_version]
  custom
end

#fetch_environment(app, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ey_cli/controllers/environments.rb', line 67

def fetch_environment(app, options = {})
  if !app.environments? || app.environments.empty?
    EYCli.term.error <<-EOF
You don't have any environment associated to this application.
Try running `ey_cli create_env' to create your first environment.
EOF
  elsif app.environments.size == 1
    app.environments.first
  else
    name = EYCli.term.choose_resource(app.environments,
                                      "I don't know which environment deploy on.",
                                      "Please, select and environment")
    EYCli::Model::Environment.find_by_name(name, app.environments)
  end
end