Project

General

Profile

Download (3.22 KB) Statistics
| Branch: | Tag: | Revision:

foreman_pipeline / app / lib / actions / foreman_pipeline / job / create_host.rb @ 4c01c2ee

1
module Actions
2
  module ForemanPipeline
3
    module Job
4
      class CreateHost < Actions::EntryAction
5
        middleware.use ::Actions::Middleware::KeepCurrentUser
6

    
7
        def plan(name, hostgroup, compute_resource, options = {})
8
          compute_attributes = hostgroup.compute_profile.compute_attributes
9
                                .where(compute_resource_id: compute_resource.id)
10
                                .first.vm_attrs
11

    
12
          plan_self name:               name,
13
                    hostgroup_id:       hostgroup.id,
14
                    compute_attributes: compute_attributes,
15
                    options:            options
16
          input.update compute_resource_id: compute_resource.id if compute_resource
17
        end
18

    
19
        def run
20
          hostgroup = Hostgroup.find(input[:hostgroup_id])
21
          location = Location.where(:name => "foreman_pipeline").first_or_create
22
          location.subnet_ids = (location.subnet_ids + [hostgroup.subnet_id]).uniq
23
          location.save!
24
          host = ::Host::Managed.new(
25
                    name:                 input[:name],
26
                    hostgroup:            hostgroup,
27
                    build:                true,
28
                    managed:              true,
29
                    enabled:              true,
30
                    environment:          hostgroup.environment,
31
                    compute_resource_id:  input.fetch(:compute_resource_id),
32
                    compute_attributes:   input[:compute_attributes],
33
                    puppet_proxy:         hostgroup.puppet_proxy,
34
                    puppet_ca_proxy:      hostgroup.puppet_ca_proxy,
35
                    organization_id:      input[:options][:org_id],
36
                    location:             location
37
                  )
38

    
39
          organization_param
40
          keys_param
41

    
42
          host.save!
43
          jenkins_pubkey_param_for host
44
          host.power.start
45

    
46
          output.update host: { id: host.id,
47
                                name: host.name,
48
                                ip: host.ip,
49
                                mac: host.mac,
50
                                params: host.params }
51
        end
52

    
53
        private
54

    
55
        def kt_org
56
           ::Organization.find(input[:options][:org_id]).name
57
        end
58

    
59
        def organization_param
60
          org_cp = ::CommonParameter.find_by_name('kt_org')
61
          if org_cp.nil?
62
            ::CommonParameter.create(:name => 'kt_org', :value => kt_org)
63
          else
64
            org_cp.update_attributes(:value => kt_org)
65
          end
66
        end
67

    
68
        def keys_param
69
          keys_cp = ::CommonParameter.find_by_name('kt_activation_keys')
70
          if keys_cp.nil?
71
            ::CommonParameter.create(:name => 'kt_activation_keys', :value => input[:options][:activation_key][:name])
72
          else
73
            keys_cp.update_attributes(:value => input[:options][:activation_key][:name])
74
          end
75
        end
76

    
77
        def jenkins_pubkey
78
          ::ForemanPipeline::JenkinsInstance.find(input.fetch(:options).fetch(:jenkins_instance_id)).pubkey
79
        end
80

    
81
        def jenkins_pubkey_param_for(host)
82
          ::HostParameter.create(:name => 'foreman_pipeline_jenkins_pubkey', :value => jenkins_pubkey, :host => host)
83
        end
84

    
85
      end
86
    end
87
  end
88
end