Class: Staypuft::DeploymentsController

Inherits:
ApplicationController show all
Includes:
Foreman::Controller::AutoCompleteSearch
Defined in:
app/controllers/staypuft/deployments_controller.rb

Instance Method Summary collapse

Instance Method Details

#associate_hostObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/staypuft/deployments_controller.rb', line 84

def associate_host
  deployment             = Deployment.find(params[:id])
  hostgroup              = ::Hostgroup.find params[:hostgroup_id]
  deployment_in_progress = ForemanTasks::Lock.locked?(deployment, nil)

  hosts_to_assign = ::Host::Base.find Array(params[:host_ids])

  unassigned_hosts = hosts_to_assign.reduce([]) do |unassigned_hosts, discovered_host|
    success, host = assign_host_to_hostgroup discovered_host, hostgroup
    success ? unassigned_hosts : [*unassigned_hosts, host]
  end

  unless unassigned_hosts.empty?
    unassigned_messages = 'Unassigned hosts: <ul>' + unassigned_hosts.
                map { |h| format '<li>%s <ul>%s</ul></li>', h.name_was, h.errors.full_messages.map {|msg|
                                 "<li>#{msg}</li>"}.join}.
                join + '</ul>'

    flash[:error] = unassigned_messages
    Rails.logger.warn('Unassigned hosts:\n' + unassigned_hosts.
                map { |h| format '%s (%s)', h.name_was, h.errors.full_messages.join(',') }.
                join("\n") 
)
  end

  redirect_to deployment_path(deployment)
end

#deployObject



69
70
71
72
73
# File 'app/controllers/staypuft/deployments_controller.rb', line 69

def deploy
  deployment = Deployment.find(params[:id])
  task       = ForemanTasks.async_task ::Actions::Staypuft::Deployment::Deploy, deployment
  redirect_to deployment_path(deployment)
end

#destroyObject



64
65
66
67
# File 'app/controllers/staypuft/deployments_controller.rb', line 64

def destroy
  Deployment.find(params[:id]).destroy
  process_success
end

#editObject



59
60
61
62
# File 'app/controllers/staypuft/deployments_controller.rb', line 59

def edit
  @deployment            = Deployment.find(params[:id])
  @service_hostgroup_map = @deployment.services_hostgroup_map
end

#export_configObject



130
131
132
133
134
135
# File 'app/controllers/staypuft/deployments_controller.rb', line 130

def export_config
  @deployment = Deployment.find(params[:id])
  send_data DeploymentParamExporter.new(@deployment).to_hash.to_yaml,
            :type     => "application/x-yaml", :disposition => 'attachment',
            :filename => @deployment.name + '.yml'
end

#import_configObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/staypuft/deployments_controller.rb', line 137

def import_config
  @deployment = Deployment.find(params[:id])
  unless params[:deployment_config_file].nil?
    begin
      new_config = YAML.load_file(params[:deployment_config_file].path)
      DeploymentParamImporter.new(@deployment).import(new_config)

      flash[:notice] = "Updated parameter values"
    rescue Psych::SyntaxError => e
      flash[:error] = "Invalid input file: #{e}"
    rescue ArgumentError => e
      flash[:error] = "Invalid input file: #{e}"
    end
  else
    flash[:error] = "No import file specified"
  end
  redirect_to deployment_path(@deployment)
end

#indexObject



5
6
7
# File 'app/controllers/staypuft/deployments_controller.rb', line 5

def index
  @deployments = Deployment.search_for(params[:search], :order => params[:order]).paginate(:page => params[:page]) || nil
end

#newObject



9
10
11
12
13
14
# File 'app/controllers/staypuft/deployments_controller.rb', line 9

def new
  deployment = Deployment.new(:name => Deployment::NEW_NAME_PREFIX+SecureRandom.hex)
  deployment.save!

  redirect_to deployment_steps_path(deployment_id: deployment)
end

#populateObject

TODO remove, it’s temporary



76
77
78
79
80
81
82
# File 'app/controllers/staypuft/deployments_controller.rb', line 76

def populate
  task = ForemanTasks.async_task ::Actions::Staypuft::Deployment::Populate,
                                 Deployment.find(params[:id]),
                                 fake:   !!params[:fake],
                                 assign: !!params[:assign]
  redirect_to foreman_tasks_task_url(id: task)
end

#refresh_assigned_hostsObject



156
157
158
159
160
# File 'app/controllers/staypuft/deployments_controller.rb', line 156

def refresh_assigned_hosts
  @deployment = Deployment.find(params[:id])
  assigned_hosts = @deployment.hosts.select { |h| !h.open_stack_deployed? }
  render partial: "hosts_table", locals: { hosts: assigned_hosts, deployment_role_col: true, path: deployment_path(@deployment.id) }
end

#showObject



16
17
18
19
20
21
22
23
24
# File 'app/controllers/staypuft/deployments_controller.rb', line 16

def show
  @deployment = Deployment.find(params[:id])
  respond_to do | format |
    format.html {}
    format.json do
      render :status => 200, :json => @deployment.to_json(:methods => [:progress, :progress_summary])
    end
  end
end

#summaryObject



26
27
28
29
# File 'app/controllers/staypuft/deployments_controller.rb', line 26

def summary
  @deployment            = Deployment.find(params[:id])
  @service_hostgroup_map = @deployment.services_hostgroup_map
end

#unassign_hostObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/staypuft/deployments_controller.rb', line 112

def unassign_host
  deployment             = Deployment.find(params[:id])
  deployment_in_progress = ForemanTasks::Lock.locked?(deployment, nil)

  hosts_to_unassign = ::Host::Base.find Array(params[:host_ids])

  hosts_to_unassign.each do |host|
    unless host.open_stack_deployed? && deployment_in_progress
      host.open_stack_unassign
      host.environment = Environment.get_discovery
      host.save!
      host.setBuild
    end
  end

  redirect_to deployment_path(deployment)
end

#updateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/staypuft/deployments_controller.rb', line 31

def update
  respond_to do | format |

    format.html do
      if params[:staypuft_deployment]
        param_data = params[:staypuft_deployment][:hostgroup_params]
        param_data.each do |hostgroup_id, hostgroup_params|
          hostgroup = Hostgroup.find(hostgroup_id)
          hostgroup_params[:puppetclass_params].each do |puppetclass_id, puppetclass_params|
            puppetclass = Puppetclass.find(puppetclass_id)
            puppetclass_params.each do |param_name, param_value|
              hostgroup.set_param_value_if_changed(puppetclass, param_name, param_value)
            end
          end
        end
      end
      redirect_to "#{deployment_path(params[:id])}#advanced_configuration"
    end

    format.json do
      @deployment = Deployment.find(params[:id])
      @deployment.update_attributes(params[:deployment])
      render :status => 200, :json => @deployment.to_json
    end

  end
end