Class: WcoHosting::ApplianceTmplsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/wco_hosting/appliance_tmpls_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#home

Instance Method Details

#add_task_tmplObject



6
7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 6

def add_task_tmpl
  @appliance_tmpl = WcoHosting::ApplianceTmpl.find params[:id]
  authorize! :edit, @appliance_tmpl
  @appliance_tmpl.task_tmpls.push WcoHosting::TaskTmpl.find( params[:task_tmpl_id] )
  if @appliance_tmpl.save
    flash_notice @appliance_tmpl
  else
    flash_alert @appliance_tmpl
  end
  redirect_to request.referrer
end

#createObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 18

def create
  # params[:tmpl][:leadset_ids].delete ''

  @appliance_tmpl = WcoHosting::ApplianceTmpl.new params[:tmpl].permit!
  authorize! :create, @appliance_tmpl

  flag = @appliance_tmpl.save
  if flag
    flash[:notice] = 'Success.'
  else
    flash[:alert] = "Cannot create appliance tmplate: #{@appliance_tmpl.errors.full_messages.join(', ')}."
  end

  redirect_to appliance_tmpls_path
end

#destroyObject



34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 34

def destroy
  @appliance_tmpl = WcoHosting::ApplianceTmpl.find params[:id]
  authorize! :destroy, @appliance_tmpl
  flag = @appliance_tmpl.delete
  if flag
    flash_notice 'ok'
  else
    flash_alert @appliance_tmpl
  end
  redirect_to action: 'index'
end

#editObject



46
47
48
49
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 46

def edit
  @appliance_tmpl = WcoHosting::ApplianceTmpl.unscoped.find params[:id]
  authorize! :edit, @appliance_tmpl
end

#indexObject



51
52
53
54
55
56
57
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 51

def index
  authorize! :index, WcoHosting::ApplianceTmpl
  @appliance_tmpls = WcoHosting::ApplianceTmpl.all
  if params[:deleted]
    @appliance_tmpls = WcoHosting::ApplianceTmpl.unscoped.deleted
  end
end

#newObject



59
60
61
62
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 59

def new
  authorize! :index, WcoHosting::ApplianceTmpl
  @appliance_tmpl = WcoHosting::ApplianceTmpl.new
end

#run_taskObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 64

def run_task
  @appliance_tmpl = WcoHosting::ApplianceTmpl.find params[:id]
  @tmpl           = @appliance_tmpl
  authorize! :run_task, @appliance_tmpl

  @appliance = WcoHosting::Appliance.where( id: params[:appliance_id] ).first
  rc_json = JSON.parse @appliance.rc_json

  case params[:task]
  when WcoHosting::Runner::TASK_REDEPLOY_ECS_TASK_DEFINITION

    template = ERB.new( @appliance_tmpl.ecs_task_definition_erb )
    file = Tempfile.new('prefix')
    rendered_str = template.result(binding)
    puts! rendered_str, 'rendered_str'
    file.write rendered_str
    file.close
    out = WcoHosting::Runner.do_exec <<~AOL
      aws ecs register-task-definition \
        --cli-input-json file:///#{file.path} \
        --profile ecs_driver_1
    AOL

    if out[:stdout].present?
      Wco::Log.puts! out[:stdout], 'stdout', obj: @appliance_tmpl
    end
    if out[:stderr].present?
      Wco::Log.puts! out[:stderr], 'stderr', obj: @appliance_tmpl
    end

  when WcoHosting::Runner::TASK_REDEPLOY_ECS_TASK

    out = WcoHosting::Runner.do_exec <<~AOL
      aws ecs update-service --service #{@appliance_tmpl.kind} \
        --task-definition #{@appliance_tmpl.kind} \
        --cluster fragile-cluster \
        --enable-execute-command \
        --force-new-deployment \
        --profile ecs_driver_1
    AOL

    if out[:stdout].present?
      Wco::Log.puts! out[:stdout], 'stdout', obj: @appliance
    end
    if out[:stderr].present?
      Wco::Log.puts! out[:stderr], 'stderr', obj: @appliance
    end

  when WcoHosting::Runner::TASK_SYNC_ECS_TASK

    out = WcoHosting::Runner.do_exec <<~AOL
      aws ecs list-tasks \
        --cluster fragile-cluster \
        --service #{@appliance_tmpl.kind} \
        --profile ecs_driver_1
    AOL
    out     = JSON.parse out[:stdout]
    task_id = out['taskArns'][0].split('/').last
    puts! task_id, 'task_id'
    rc_json[:task_id] = task_id

    out = WcoHosting::Runner.do_exec <<~AOL
      aws ecs describe-tasks --cluster fragile-cluster \
        --tasks arn:aws:ecs:us-east-2:831556125887:task/fragile-cluster/#{task_id} \
        --profile ecs_driver_1
    AOL
    out = JSON.parse out[:stdout]
    eni_id = out['tasks'][0]['attachments'][0]['details'].select { |d|
      d['name'] == 'networkInterfaceId'
    }[0]['value']
    puts! eni_id, 'eni_id'
    rc_json[:eni_id] = eni_id

    out = WcoHosting::Runner.do_exec <<~AOL
      aws ec2 describe-network-interfaces \
        --network-interface-id #{eni_id} \
        --profile ecs_driver_1
    AOL
    out = JSON.parse out[:stdout]
    public_ip = out['NetworkInterfaces'][0]['Association']['PublicIp']
    puts! public_ip, 'public_ip'
    rc_json[:public_ip] = public_ip
    rc_json[:ip_addr]   = out['NetworkInterfaces'][0]['PrivateIpAddress']

    @appliance.update({ rc_json: rc_json })

  end


  flash[:notice] = "Status: #{out[:status]}"
  redirect_to  request.referrer
end

#showObject



157
158
159
160
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 157

def show
  @appliance_tmpl = WcoHosting::ApplianceTmpl.find params[:id]
  authorize! :show, @appliance_tmpl
end

#updateObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/controllers/wco_hosting/appliance_tmpls_controller.rb', line 162

def update
  # params[:tmpl][:leadset_ids].delete ''

  @appliance_tmpl = WcoHosting::ApplianceTmpl.find params[:id]
  authorize! :update, @appliance_tmpl

  # price = Wco::Price.find( params[:tmpl][:price] )
  # price.product = @appliance_tmpl
  # price.save

  # params[:tmpl][:price_id] = price.price_id
  # params[:tmpl][:price] = price

  flag = @appliance_tmpl.update params[:tmpl].permit!
  if flag
    flash_notice 'success'
  else
    flash_alert "Cannot update appliance template: #{@appliance_tmpl.errors.full_messages.join(', ')}."
  end
  redirect_to action: :index
end