Class: VmShepherd::Shepherd

Inherits:
Object
  • Object
show all
Defined in:
lib/vm_shepherd/shepherd.rb

Defined Under Namespace

Classes: InvalidIaas

Instance Method Summary collapse

Constructor Details

#initialize(settings:) ⇒ Shepherd

Returns a new instance of Shepherd.



11
12
13
14
15
# File 'lib/vm_shepherd/shepherd.rb', line 11

def initialize(settings:)
  @iaas_type  = settings.dig('iaas_type')
  @configs    = settings.dig('vm_shepherd', 'vm_configs') || []
  @env_config = settings.dig('vm_shepherd', 'env_config')
end

Instance Method Details

#clean_environmentObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/vm_shepherd/shepherd.rb', line 142

def clean_environment
  unless valid_iaas_types.include?(@iaas_type)
    fail(InvalidIaas, "Unknown IaaS type: #{@iaas_type.inspect}")
  end
  case @iaas_type
    when VmShepherd::VCLOUD_IAAS_TYPE then
      @configs.each do |vm_shepherd_config|
        VmShepherd::VcloudManager.new(
          {
            url:          vm_shepherd_config.dig('creds', 'url'),
            organization: vm_shepherd_config.dig('creds', 'organization'),
            user:         vm_shepherd_config.dig('creds', 'user'),
            password:     vm_shepherd_config.dig('creds', 'password'),
          },
          vm_shepherd_config.dig('vdc', 'name'),
          stdout_logger,
        ).clean_environment(vm_shepherd_config.dig('vapp', 'product_names')|| [], vm_shepherd_config.dig('vapp', 'product_catalog'))
      end
    when VmShepherd::VSPHERE_IAAS_TYPE then
      @configs.each do |vm_shepherd_config|
        VmShepherd::VsphereManager.new(
          vm_shepherd_config.dig('vcenter_creds', 'ip'),
          vm_shepherd_config.dig('vcenter_creds', 'username'),
          vm_shepherd_config.dig('vcenter_creds', 'password'),
          vm_shepherd_config.dig('cleanup', 'datacenter'),
          stdout_logger,
        ).clean_environment(
          datacenter_folders_to_clean: vm_shepherd_config.dig('cleanup', 'datacenter_folders_to_clean'),
          datastores:                  vm_shepherd_config.dig('cleanup', 'datastores'),
          datastore_folders_to_clean:  vm_shepherd_config.dig('cleanup', 'datastore_folders_to_clean'),
        )
      end
    when VmShepherd::AWS_IAAS_TYPE then
      ami_manager.clean_environment
    when VmShepherd::OPENSTACK_IAAS_TYPE then
      @configs.each do |vm_shepherd_config|
        openstack_vm_manager(vm_shepherd_config).clean_environment
      end
  end
end

#deploy(paths:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vm_shepherd/shepherd.rb', line 17

def deploy(paths:)
  unless valid_iaas_types.include?(@iaas_type)
    fail(InvalidIaas, "Unknown IaaS type: #{@iaas_type.inspect}")
  end
  unless @configs.size == paths.size
    fail(ArgumentError, "mismatch in available images to deploy (needed #{@configs.size}, got #{paths.size})")
  end
  @configs.zip(paths).each do |vm_shepherd_config, path|
    case @iaas_type
      when VmShepherd::VCLOUD_IAAS_TYPE then
        VmShepherd::VcloudManager.new(
          {
            url:          vm_shepherd_config.dig('creds', 'url'),
            organization: vm_shepherd_config.dig('creds', 'organization'),
            user:         vm_shepherd_config.dig('creds', 'user'),
            password:     vm_shepherd_config.dig('creds', 'password'),
          },
          vm_shepherd_config.dig('vdc', 'name'),
          stdout_logger
        ).deploy(
          path,
          vcloud_deploy_options(vm_shepherd_config),
        )
      when VmShepherd::VSPHERE_IAAS_TYPE then
        VmShepherd::VsphereManager.new(
          vm_shepherd_config.dig('vcenter_creds', 'ip'),
          vm_shepherd_config.dig('vcenter_creds', 'username'),
          vm_shepherd_config.dig('vcenter_creds', 'password'),
          vm_shepherd_config.dig('vsphere', 'datacenter'),
          stdout_logger,
        ).deploy(
          path,
          {
            ip:          vm_shepherd_config.dig('vm', 'ip'),
            gateway:     vm_shepherd_config.dig('vm', 'gateway'),
            netmask:     vm_shepherd_config.dig('vm', 'netmask'),
            dns:         vm_shepherd_config.dig('vm', 'dns'),
            ntp_servers: vm_shepherd_config.dig('vm', 'ntp_servers'),
          },
          {
            cluster:       vm_shepherd_config.dig('vsphere', 'cluster'),
            resource_pool: vm_shepherd_config.dig('vsphere', 'resource_pool'),
            datastore:     vm_shepherd_config.dig('vsphere', 'datastore'),
            network:       vm_shepherd_config.dig('vsphere', 'network'),
            folder:        vm_shepherd_config.dig('vsphere', 'folder'),
          }
        )
      when VmShepherd::AWS_IAAS_TYPE then
        ami_manager.deploy(ami_file_path: path, vm_config: vm_shepherd_config)
      when VmShepherd::OPENSTACK_IAAS_TYPE then
        openstack_vm_manager(vm_shepherd_config).deploy(path, openstack_vm_options(vm_shepherd_config))
    end
  end
end

#destroyObject



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
# File 'lib/vm_shepherd/shepherd.rb', line 109

def destroy
  unless valid_iaas_types.include?(@iaas_type)
    fail(InvalidIaas, "Unknown IaaS type: #{@iaas_type.inspect}")
  end
  @configs.each do |vm_shepherd_config|
    case @iaas_type
      when VmShepherd::VCLOUD_IAAS_TYPE then
        VmShepherd::VcloudManager.new(
          {
            url:          vm_shepherd_config.dig('creds', 'url'),
            organization: vm_shepherd_config.dig('creds', 'organization'),
            user:         vm_shepherd_config.dig('creds', 'user'),
            password:     vm_shepherd_config.dig('creds', 'password'),
          },
          vm_shepherd_config.dig('vdc', 'name'),
          stdout_logger
        ).destroy([vm_shepherd_config.dig('vapp', 'ops_manager_name')], vm_shepherd_config.dig('vdc', 'catalog'))
      when VmShepherd::VSPHERE_IAAS_TYPE then
        VmShepherd::VsphereManager.new(
          vm_shepherd_config.dig('vcenter_creds', 'ip'),
          vm_shepherd_config.dig('vcenter_creds', 'username'),
          vm_shepherd_config.dig('vcenter_creds', 'password'),
          vm_shepherd_config.dig('vsphere', 'datacenter'),
          stdout_logger,
        ).destroy(vm_shepherd_config.dig('vm', 'ip'), vm_shepherd_config.dig('vsphere', 'resource_pool'))
      when VmShepherd::AWS_IAAS_TYPE then
        ami_manager.destroy(vm_shepherd_config)
      when VmShepherd::OPENSTACK_IAAS_TYPE then
        openstack_vm_manager(vm_shepherd_config).destroy(openstack_vm_options(vm_shepherd_config))
    end
  end
end

#prepare_environmentObject



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
# File 'lib/vm_shepherd/shepherd.rb', line 72

def prepare_environment
  unless valid_iaas_types.include?(@iaas_type)
    fail(InvalidIaas, "Unknown IaaS type: #{@iaas_type.inspect}")
  end
  case @iaas_type
    when VmShepherd::VCLOUD_IAAS_TYPE then
      @configs.each do |vm_shepherd_config|
        VmShepherd::VcloudManager.new(
          {
            url:          vm_shepherd_config.dig('creds', 'url'),
            organization: vm_shepherd_config.dig('creds', 'organization'),
            user:         vm_shepherd_config.dig('creds', 'user'),
            password:     vm_shepherd_config.dig('creds', 'password'),
          },
          vm_shepherd_config.dig('vdc', 'name'),
          stdout_logger
        ).prepare_environment
      end
    when VmShepherd::VSPHERE_IAAS_TYPE then
      @configs.each do |vm_shepherd_config|
        VmShepherd::VsphereManager.new(
          vm_shepherd_config.dig('vcenter_creds', 'ip'),
          vm_shepherd_config.dig('vcenter_creds', 'username'),
          vm_shepherd_config.dig('vcenter_creds', 'password'),
          vm_shepherd_config.dig('vsphere', 'datacenter'),
          stdout_logger,
        ).prepare_environment
      end
    when VmShepherd::AWS_IAAS_TYPE then
      ami_manager.prepare_environment(@env_config.dig('json_file'))
    when VmShepherd::OPENSTACK_IAAS_TYPE then
      @configs.each do |vm_shepherd_config|
        openstack_vm_manager(vm_shepherd_config).prepare_environment
      end
  end
end