Class: ForemanAcd::AppInstancesController

Inherits:
ApplicationController show all
Includes:
Foreman::Controller::AutoCompleteSearch, Concerns::AppInstanceMixins, Concerns::AppInstanceParameters
Defined in:
app/controllers/foreman_acd/app_instances_controller.rb

Overview

Application Instance Controller

Instance Method Summary collapse

Methods included from Concerns::AppInstanceMixins

#collect_host_report_data

Methods included from Concerns::AppInstanceParameters

#app_instance_params

Methods inherited from ApplicationController

#resource_class

Instance Method Details

#action_permissionObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 63

def action_permission
  case params[:action]
  when 'deploy'
    :deploy
  when 'report'
    :report
  when 'destroy_with_hosts'
    :destroy
  else
    super
  end
end

#app_instance_has_foreman_hostsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 105

def app_instance_has_foreman_hosts
  hosts = JSON.parse(@app_instance.hosts)
  session[:remember_hosts] = []
  hosts.each do |h|
    if @app_instance.foreman_hosts.where(:hostname => h['hostname']).exists?
      old_host = @app_instance.foreman_hosts.find_by(:hostname => h['hostname'])

      @app_instance.foreman_hosts.where(:hostname => h['hostname']).
        update(:service => h['service'], :description => h['description'],
          :foremanParameters => JSON.dump(h['foremanParameters']), :ansibleParameters => JSON.dump(h['ansibleParameters']))

      updated_host = @app_instance.foreman_hosts.find_by(:hostname => h['hostname'])

      # Store hosts if updated for safe deploy
      session[:remember_hosts] << updated_host.id if updated_host.updated_at != old_host.updated_at
    else
      @app_instance.foreman_hosts.create(:hostname => h['hostname'],
        :service => h['service'],
        :description => h['description'],
        :is_existing_host => h['isExistingHost'],
        :foremanParameters => JSON.dump(h['foremanParameters']),
        :ansibleParameters => JSON.dump(h['ansibleParameters']))
      # Store new hosts for safe deploy
      session[:remember_hosts] << @app_instance.foreman_hosts.find_by(:hostname => h['hostname']).id
    end
  end

  # Delete record if json hosts are deleted
  deleted_json_hosts = @app_instance.foreman_hosts.pluck('hostname') - hosts.pluck('hostname')
  @app_instance.foreman_hosts.where(:hostname => deleted_json_hosts).destroy_all if deleted_json_hosts
end

#collect_hosts_dataObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 137

def collect_hosts_data
  hosts_data = []
  @app_instance.foreman_hosts.each do |h|
    hosts_data << {
      :id => h.id,
      :hostname => h.hostname,
      :service => h.service,
      :description => h.description,
      :isExistingHost => h.is_existing_host,
      :foremanParameters => JSON.parse(h.foremanParameters),
      :ansibleParameters => JSON.parse(h.ansibleParameters),
    }
  end
  hosts_data
end

#createObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 27

def create
  @app_instance = AppInstance.new(app_instance_params)
  begin
    if @app_instance.save!
      app_instance_has_foreman_hosts
      process_success
    else
      process_error
    end
  rescue StandardError, ValidationError => e
    redirect_to new_app_instance_path, :flash => { :error => _(e.message) }
  end
end

#deployObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 76

def deploy
  value = false
  @app_instance.update!({ :last_deploy_task_id => nil,
                          :initial_configure_task_id => nil })
  @app_instance.foreman_hosts.each { |f| f.update!(:last_progress_report => nil) }
  @app_instance.clean_all_hosts if params[:delete_hosts]
  value = safe_deploy? if params[:safe_deploy]
  session.delete(:remember_hosts)
  logger.info('Run async foreman task to deploy hosts')
  async_task = ForemanTasks.async_task(::Actions::ForemanAcd::DeployAllHosts, @app_instance, value)
  @app_instance.update!(:last_deploy_task => async_task)

  redirect_to report_app_instance_path, :success => _('Started task to deploy hosts for %s') % @app_instance
rescue StandardError => e
  error_msg = "Error happend while deploying hosts of #{@app_instance}: #{e.message}"
  logger.error("#{error_msg} - #{e.class}\n#{e.backtrace.join($INPUT_RECORD_SEPARATOR)}")
  process_error :error_msg => error_msg
end

#destroy_with_hostsObject



53
54
55
56
57
58
59
60
61
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 53

def destroy_with_hosts
  @app_instance = AppInstance.find(params[:id])
  @app_instance.clean_hosts_by_id(params[:foreman_host_ids]) if params[:foreman_host_ids]
  if @app_instance.destroy
    redirect_to app_instances_path, :flash => { :success => _('Successfully deleted %s') % @app_instance }
  else
    process_error
  end
end

#editObject



41
42
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 41

def edit
end

#indexObject



15
16
17
18
19
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 15

def index
  @app_instances = resource_base.where(:organization => @organization).
                   where(:location => @location).
                   search_for(params[:search], :order => params[:order]).paginate(:page => params[:page])
end

#newObject



21
22
23
24
25
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 21

def new
  @app_instance = AppInstance.new
  @app_instance.organization = @organization
  @app_instance.location = @location
end

#reportObject



100
101
102
103
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 100

def report
  @report_hosts = collect_host_report_data(@app_instance)
  logger.debug("app instance host details: #{@report_hosts.inspect}")
end

#safe_deploy?Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 95

def safe_deploy?
  return false if session[:remember_hosts].empty?
  session[:remember_hosts]
end

#updateObject



44
45
46
47
48
49
50
51
# File 'app/controllers/foreman_acd/app_instances_controller.rb', line 44

def update
  if @app_instance.update(app_instance_params)
    app_instance_has_foreman_hosts
    process_success
  else
    process_error
  end
end