Class: Actions::Staypuft::Host::Create

Inherits:
Base
  • Object
show all
Defined in:
app/lib/actions/staypuft/host/create.rb

Instance Method Summary collapse

Instance Method Details

#humanized_inputObject



86
87
88
# File 'app/lib/actions/staypuft/host/create.rb', line 86

def humanized_input
  input[:name]
end

#plan(name, hostgroup, compute_resource, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/lib/actions/staypuft/host/create.rb', line 20

def plan(name, hostgroup, compute_resource, options = {})
  Type! hostgroup, ::Hostgroup
  Type! compute_resource, ComputeResource, NilClass

  options = { start: true, assign: true, fake: false }.merge options

  compute_attributes = if options[:fake]
                         {}
                       else
                         hostgroup.
                             compute_profile.
                             compute_attributes.
                             where(compute_resource_id: compute_resource.id).
                             first.
                             vm_attrs
                       end


  plan_self name:               name,
            hostgroup_id:       hostgroup.id,
            compute_attributes: compute_attributes,
            options:            options
  input.update compute_resource_id: compute_resource.id if compute_resource
end

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/lib/actions/staypuft/host/create.rb', line 45

def run
  fake   = input.fetch(:options).fetch(:fake)
  assign = input.fetch(:options).fetch(:assign)

  host = if fake
           raise if assign
           ::Host::Managed.new(
               name:         input[:name],
               hostgroup_id: input[:hostgroup_id],
               build:        true,
               managed:      true,
               enabled:      true,
               environment:  Environment.get_discovery,
               mac:          '0a:' + Array.new(5).map { format '%0.2X', rand(256) }.join(':'))
         else
           ::Host::Managed.new(
               name:                input[:name],
               hostgroup_id:        input[:hostgroup_id],
               build:               true,
               managed:             true,
               enabled:             true,
               environment:         Environment.get_discovery,
               compute_resource_id: input.fetch(:compute_resource_id),
               compute_attributes:  input[:compute_attributes])
         end

  host.save!
  host.power.start if input.fetch(:options).fetch(:start)

  unless assign
    host.reload
    host.hostgroup = nil
    host.save!
  end

  output.update host: { id:   host.id,
                        name: host.name,
                        ip:   host.ip,
                        mac:  host.mac }
end