Class: Fog::Proxmox::Compute::Server

Inherits:
Compute::Server
  • Object
show all
Defined in:
lib/fog/proxmox/compute/models/server.rb

Overview

Server model rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(new_attributes = {}) ⇒ Server

Returns a new instance of Server.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fog/proxmox/compute/models/server.rb', line 64

def initialize(new_attributes = {})
  prepare_service_value(new_attributes)
  Fog::Proxmox::Attributes.set_attr_and_sym('node_id', attributes, new_attributes)
  Fog::Proxmox::Attributes.set_attr_and_sym('type', attributes, new_attributes)
  Fog::Proxmox::Attributes.set_attr_and_sym('vmid', attributes, new_attributes)
  requires :node_id, :type, :vmid
  initialize_config(new_attributes)
  initialize_snapshots
  initialize_tasks
  super(new_attributes)
end

Instance Method Details

#action(action, options = {}) ⇒ Object

Raises:

  • (Fog::Errors::Error)


134
135
136
137
138
139
140
141
# File 'lib/fog/proxmox/compute/models/server.rb', line 134

def action(action, options = {})
  action_known = %w[start stop resume suspend shutdown reset].include? action
  message = "Action #{action} not implemented"
  raise Fog::Errors::Error, message unless action_known

  request(:action_server, options, action: action, vmid: vmid)
  reload
end

#attach(disk, options = {}) ⇒ Object



193
194
195
196
# File 'lib/fog/proxmox/compute/models/server.rb', line 193

def attach(disk, options = {})
  disk_hash = Fog::Proxmox::DiskHelper.flatten(disk.merge(options: options))
  update(disk_hash)
end

#backup(options = {}) ⇒ Object



147
148
149
150
# File 'lib/fog/proxmox/compute/models/server.rb', line 147

def backup(options = {})
  request(:create_backup, options.merge(vmid: vmid))
  reload
end

#backupsObject



240
241
242
# File 'lib/fog/proxmox/compute/models/server.rb', line 240

def backups
  list 'backup'
end

#clone(newid, options = {}) ⇒ Object



161
162
163
164
# File 'lib/fog/proxmox/compute/models/server.rb', line 161

def clone(newid, options = {})
  request(:clone_server, options.merge(newid: newid), vmid: vmid)
  reload
end

#connect_vnc(options = {}) ⇒ Object



234
235
236
237
238
# File 'lib/fog/proxmox/compute/models/server.rb', line 234

def connect_vnc(options = {})
  path_params = { node: node_id, type: type, vmid: vmid }
  query_params = { port: options['port'], vncticket: options['ticket'] }
  service.get_vnc(path_params, query_params)
end

#container?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/fog/proxmox/compute/models/server.rb', line 80

def container?
  type == 'lxc'
end

#create_template(options = {}) ⇒ Object



166
167
168
169
# File 'lib/fog/proxmox/compute/models/server.rb', line 166

def create_template(options = {})
  service.template_server({ node: node_id, type: type, vmid: vmid }, options)
  reload
end

#destroy(options = {}, query: nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fog/proxmox/compute/models/server.rb', line 121

def destroy(options = {}, query: nil)
  # stop vm before destroying if it's an HA resource
  if ha['managed'].to_i == 1 && status == 'running'
    action('stop')
    # wait for vm to stop
    Fog.wait_for(10, 5) do
      reload
      status == 'stopped'
    end
  end
  request(:delete_server, options, { vmid: vmid }, query)
end

#detach(diskid) ⇒ Object



198
199
200
# File 'lib/fog/proxmox/compute/models/server.rb', line 198

def detach(diskid)
  update(delete: diskid)
end

#extend(disk, size, options = {}) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/fog/proxmox/compute/models/server.rb', line 175

def extend(disk, size, options = {})
  if container?
    request(:resize_container, options.merge(disk: disk, size: size), vmid: vmid)
  else
    service.resize_server({ vmid: vmid, node: node_id }, options.merge(disk: disk, size: size))
  end
  reload
end

#imagesObject



244
245
246
# File 'lib/fog/proxmox/compute/models/server.rb', line 244

def images
  list 'images'
end

#list(content) ⇒ Object



248
249
250
251
252
253
# File 'lib/fog/proxmox/compute/models/server.rb', line 248

def list(content)
  storages = node.storages.list_by_content_type content
  volumes = []
  storages.each { |storage| volumes += storage.volumes.list_by_content_type_and_by_server(content, identity) }
  volumes
end

#migrate(target, options = {}) ⇒ Object



171
172
173
# File 'lib/fog/proxmox/compute/models/server.rb', line 171

def migrate(target, options = {})
  request(:migrate_server, options.merge(target: target), vmid: vmid)
end

#move(volume, storage, options = {}) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/fog/proxmox/compute/models/server.rb', line 184

def move(volume, storage, options = {})
  if container?
    request(:move_volume, options.merge(volume: volume, storage: storage), vmid: vmid)
  else
    request(:move_disk, options.merge(disk: volume, storage: storage), vmid: vmid)
  end
  reload
end

#persisted?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
# File 'lib/fog/proxmox/compute/models/server.rb', line 84

def persisted?
  service.next_vmid(vmid: vmid)
  false
rescue Excon::Error::InternalServerError
  false
rescue Excon::Error::BadRequest
  true
end

#ready?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/fog/proxmox/compute/models/server.rb', line 143

def ready?
  status == 'running'
end

#request(name, body_params = {}, path_params = {}, query = {}) ⇒ Object

request with async task



94
95
96
97
98
99
100
101
# File 'lib/fog/proxmox/compute/models/server.rb', line 94

def request(name, body_params = {}, path_params = {}, query = {})
  requires :node_id, :type
  path = path_params.merge(node: node_id, type: type)
  args = [name, path, body_params]
  args << query if query&.any?
  task_upid = service.send(*args)
  tasks.wait_for(task_upid)
end

#restore(backup, options = {}) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/fog/proxmox/compute/models/server.rb', line 152

def restore(backup, options = {})
  attr_hash = if container?
                options.merge(ostemplate: backup.volid, force: 1, restore: 1)
              else
                options.merge(archive: backup.volid, force: 1)
              end
  save(attr_hash)
end

#save(new_attributes = {}) ⇒ Object



103
104
105
106
107
108
# File 'lib/fog/proxmox/compute/models/server.rb', line 103

def save(new_attributes = {})
  body_params = new_attributes.merge(vmid: vmid)
  body_params = body_params.merge(config.flatten) unless persisted?
  request(:create_server, body_params)
  reload
end

#start_console(options = {}) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fog/proxmox/compute/models/server.rb', line 202

def start_console(options = {})
  unless ready?
    raise ::Fog::Proxmox::Errors::ServiceError,
          'Unable to start console because server not running.'
  end

  if container?
    type_console = options[:console]
    options.delete_if { |option| [:console].include? option }
    unless type_console
      raise ::Fog::Proxmox::Errors::ServiceError,
            'Unable to start console because console container config is not set or unknown.'
    end
  else
    type_console = config.type_console
    unless type_console
      raise ::Fog::Proxmox::Errors::ServiceError,
            'Unable to start console because VGA display server config is not set or unknown.'
    end
  end
  requires :vmid, :node_id, :type
  path_params = { node: node_id, type: type, vmid: vmid }
  body_params = options
  data = service.send(('create_' + type_console).to_sym, path_params, body_params)
  task_upid = data['upid']
  if task_upid
    task = tasks.get(task_upid)
    task.wait_for { running? }
  end
  data
end

#to_sObject



76
77
78
# File 'lib/fog/proxmox/compute/models/server.rb', line 76

def to_s
  name
end

#update(new_attributes = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/fog/proxmox/compute/models/server.rb', line 110

def update(new_attributes = {})
  if container?
    path_params = { node: node_id, type: type, vmid: vmid }
    body_params = new_attributes
    service.update_server(path_params, body_params)
  else
    request(:update_server, new_attributes, vmid: vmid)
  end
  reload
end