fog-proxmox-configlmm 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/LICENSE +674 -0
  4. data/README.md +110 -0
  5. data/docs/compute.md +874 -0
  6. data/docs/connection_parameters.md +45 -0
  7. data/docs/getting_started.md +53 -0
  8. data/docs/identity.md +382 -0
  9. data/examples/compute.rb +276 -0
  10. data/examples/identity.rb +203 -0
  11. data/lib/fog/proxmox/attributes.rb +34 -0
  12. data/lib/fog/proxmox/auth/token/access_ticket.rb +96 -0
  13. data/lib/fog/proxmox/auth/token/user_token.rb +107 -0
  14. data/lib/fog/proxmox/auth/token.rb +94 -0
  15. data/lib/fog/proxmox/compute/models/disk.rb +94 -0
  16. data/lib/fog/proxmox/compute/models/disks.rb +55 -0
  17. data/lib/fog/proxmox/compute/models/interface.rb +56 -0
  18. data/lib/fog/proxmox/compute/models/interfaces.rb +44 -0
  19. data/lib/fog/proxmox/compute/models/node.rb +93 -0
  20. data/lib/fog/proxmox/compute/models/nodes.rb +39 -0
  21. data/lib/fog/proxmox/compute/models/server.rb +271 -0
  22. data/lib/fog/proxmox/compute/models/server_config.rb +161 -0
  23. data/lib/fog/proxmox/compute/models/servers.rb +80 -0
  24. data/lib/fog/proxmox/compute/models/snapshot.rb +83 -0
  25. data/lib/fog/proxmox/compute/models/snapshots.rb +47 -0
  26. data/lib/fog/proxmox/compute/models/storage.rb +65 -0
  27. data/lib/fog/proxmox/compute/models/storages.rb +51 -0
  28. data/lib/fog/proxmox/compute/models/task.rb +79 -0
  29. data/lib/fog/proxmox/compute/models/tasks.rb +65 -0
  30. data/lib/fog/proxmox/compute/models/volume.rb +68 -0
  31. data/lib/fog/proxmox/compute/models/volumes.rb +58 -0
  32. data/lib/fog/proxmox/compute/requests/action_server.rb +45 -0
  33. data/lib/fog/proxmox/compute/requests/clone_server.rb +44 -0
  34. data/lib/fog/proxmox/compute/requests/create_backup.rb +42 -0
  35. data/lib/fog/proxmox/compute/requests/create_server.rb +43 -0
  36. data/lib/fog/proxmox/compute/requests/create_snapshot.rb +46 -0
  37. data/lib/fog/proxmox/compute/requests/create_spice.rb +44 -0
  38. data/lib/fog/proxmox/compute/requests/create_term.rb +45 -0
  39. data/lib/fog/proxmox/compute/requests/create_vnc.rb +44 -0
  40. data/lib/fog/proxmox/compute/requests/delete_server.rb +45 -0
  41. data/lib/fog/proxmox/compute/requests/delete_snapshot.rb +47 -0
  42. data/lib/fog/proxmox/compute/requests/delete_volume.rb +40 -0
  43. data/lib/fog/proxmox/compute/requests/get_node_statistics.rb +44 -0
  44. data/lib/fog/proxmox/compute/requests/get_server_config.rb +60 -0
  45. data/lib/fog/proxmox/compute/requests/get_server_status.rb +63 -0
  46. data/lib/fog/proxmox/compute/requests/get_snapshot_config.rb +44 -0
  47. data/lib/fog/proxmox/compute/requests/get_task.rb +41 -0
  48. data/lib/fog/proxmox/compute/requests/get_vnc.rb +45 -0
  49. data/lib/fog/proxmox/compute/requests/get_volume.rb +42 -0
  50. data/lib/fog/proxmox/compute/requests/list_nodes.rb +48 -0
  51. data/lib/fog/proxmox/compute/requests/list_servers.rb +113 -0
  52. data/lib/fog/proxmox/compute/requests/list_snapshots.rb +66 -0
  53. data/lib/fog/proxmox/compute/requests/list_storages.rb +46 -0
  54. data/lib/fog/proxmox/compute/requests/list_tasks.rb +44 -0
  55. data/lib/fog/proxmox/compute/requests/list_volumes.rb +44 -0
  56. data/lib/fog/proxmox/compute/requests/log_task.rb +44 -0
  57. data/lib/fog/proxmox/compute/requests/migrate_server.rb +44 -0
  58. data/lib/fog/proxmox/compute/requests/move_disk.rb +43 -0
  59. data/lib/fog/proxmox/compute/requests/move_volume.rb +43 -0
  60. data/lib/fog/proxmox/compute/requests/next_vmid.rb +42 -0
  61. data/lib/fog/proxmox/compute/requests/resize_container.rb +43 -0
  62. data/lib/fog/proxmox/compute/requests/resize_server.rb +43 -0
  63. data/lib/fog/proxmox/compute/requests/rollback_snapshot.rb +48 -0
  64. data/lib/fog/proxmox/compute/requests/status_task.rb +41 -0
  65. data/lib/fog/proxmox/compute/requests/stop_task.rb +41 -0
  66. data/lib/fog/proxmox/compute/requests/template_server.rb +44 -0
  67. data/lib/fog/proxmox/compute/requests/update_server.rb +44 -0
  68. data/lib/fog/proxmox/compute/requests/update_snapshot.rb +45 -0
  69. data/lib/fog/proxmox/compute.rb +141 -0
  70. data/lib/fog/proxmox/core.rb +147 -0
  71. data/lib/fog/proxmox/errors.rb +65 -0
  72. data/lib/fog/proxmox/hash.rb +37 -0
  73. data/lib/fog/proxmox/helpers/controller_helper.rb +63 -0
  74. data/lib/fog/proxmox/helpers/cpu_helper.rb +88 -0
  75. data/lib/fog/proxmox/helpers/disk_helper.rb +185 -0
  76. data/lib/fog/proxmox/helpers/ip_helper.rb +101 -0
  77. data/lib/fog/proxmox/helpers/nic_helper.rb +132 -0
  78. data/lib/fog/proxmox/identity/models/domain.rb +69 -0
  79. data/lib/fog/proxmox/identity/models/domain_type.rb +47 -0
  80. data/lib/fog/proxmox/identity/models/domains.rb +44 -0
  81. data/lib/fog/proxmox/identity/models/group.rb +51 -0
  82. data/lib/fog/proxmox/identity/models/groups.rb +44 -0
  83. data/lib/fog/proxmox/identity/models/permission.rb +66 -0
  84. data/lib/fog/proxmox/identity/models/permissions.rb +46 -0
  85. data/lib/fog/proxmox/identity/models/pool.rb +89 -0
  86. data/lib/fog/proxmox/identity/models/pools.rb +46 -0
  87. data/lib/fog/proxmox/identity/models/principal.rb +42 -0
  88. data/lib/fog/proxmox/identity/models/role.rb +48 -0
  89. data/lib/fog/proxmox/identity/models/roles.rb +44 -0
  90. data/lib/fog/proxmox/identity/models/token.rb +72 -0
  91. data/lib/fog/proxmox/identity/models/token_info.rb +41 -0
  92. data/lib/fog/proxmox/identity/models/tokens.rb +54 -0
  93. data/lib/fog/proxmox/identity/models/user.rb +82 -0
  94. data/lib/fog/proxmox/identity/models/users.rb +44 -0
  95. data/lib/fog/proxmox/identity/requests/change_password.rb +41 -0
  96. data/lib/fog/proxmox/identity/requests/check_permissions.rb +41 -0
  97. data/lib/fog/proxmox/identity/requests/create_domain.rb +40 -0
  98. data/lib/fog/proxmox/identity/requests/create_group.rb +40 -0
  99. data/lib/fog/proxmox/identity/requests/create_pool.rb +40 -0
  100. data/lib/fog/proxmox/identity/requests/create_role.rb +40 -0
  101. data/lib/fog/proxmox/identity/requests/create_token.rb +40 -0
  102. data/lib/fog/proxmox/identity/requests/create_user.rb +40 -0
  103. data/lib/fog/proxmox/identity/requests/delete_domain.rb +40 -0
  104. data/lib/fog/proxmox/identity/requests/delete_group.rb +40 -0
  105. data/lib/fog/proxmox/identity/requests/delete_pool.rb +39 -0
  106. data/lib/fog/proxmox/identity/requests/delete_role.rb +40 -0
  107. data/lib/fog/proxmox/identity/requests/delete_token.rb +40 -0
  108. data/lib/fog/proxmox/identity/requests/delete_user.rb +39 -0
  109. data/lib/fog/proxmox/identity/requests/get_domain.rb +41 -0
  110. data/lib/fog/proxmox/identity/requests/get_group.rb +41 -0
  111. data/lib/fog/proxmox/identity/requests/get_pool.rb +41 -0
  112. data/lib/fog/proxmox/identity/requests/get_role.rb +41 -0
  113. data/lib/fog/proxmox/identity/requests/get_token_info.rb +41 -0
  114. data/lib/fog/proxmox/identity/requests/get_user.rb +41 -0
  115. data/lib/fog/proxmox/identity/requests/list_domains.rb +41 -0
  116. data/lib/fog/proxmox/identity/requests/list_groups.rb +41 -0
  117. data/lib/fog/proxmox/identity/requests/list_permissions.rb +41 -0
  118. data/lib/fog/proxmox/identity/requests/list_pools.rb +41 -0
  119. data/lib/fog/proxmox/identity/requests/list_roles.rb +40 -0
  120. data/lib/fog/proxmox/identity/requests/list_tokens.rb +41 -0
  121. data/lib/fog/proxmox/identity/requests/list_user_permissions.rb +44 -0
  122. data/lib/fog/proxmox/identity/requests/list_users.rb +42 -0
  123. data/lib/fog/proxmox/identity/requests/read_version.rb +39 -0
  124. data/lib/fog/proxmox/identity/requests/update_domain.rb +41 -0
  125. data/lib/fog/proxmox/identity/requests/update_group.rb +40 -0
  126. data/lib/fog/proxmox/identity/requests/update_permissions.rb +41 -0
  127. data/lib/fog/proxmox/identity/requests/update_pool.rb +41 -0
  128. data/lib/fog/proxmox/identity/requests/update_role.rb +41 -0
  129. data/lib/fog/proxmox/identity/requests/update_token.rb +41 -0
  130. data/lib/fog/proxmox/identity/requests/update_user.rb +41 -0
  131. data/lib/fog/proxmox/identity.rb +144 -0
  132. data/lib/fog/proxmox/json.rb +32 -0
  133. data/lib/fog/proxmox/network/models/network.rb +76 -0
  134. data/lib/fog/proxmox/network/models/networks.rb +48 -0
  135. data/lib/fog/proxmox/network/models/node.rb +75 -0
  136. data/lib/fog/proxmox/network/models/nodes.rb +39 -0
  137. data/lib/fog/proxmox/network/requests/create_network.rb +41 -0
  138. data/lib/fog/proxmox/network/requests/delete_network.rb +42 -0
  139. data/lib/fog/proxmox/network/requests/get_network.rb +43 -0
  140. data/lib/fog/proxmox/network/requests/get_node.rb +41 -0
  141. data/lib/fog/proxmox/network/requests/list_networks.rb +43 -0
  142. data/lib/fog/proxmox/network/requests/list_nodes.rb +42 -0
  143. data/lib/fog/proxmox/network/requests/power_node.rb +43 -0
  144. data/lib/fog/proxmox/network/requests/update_network.rb +42 -0
  145. data/lib/fog/proxmox/network.rb +90 -0
  146. data/lib/fog/proxmox/storage/requests/create.rb +23 -0
  147. data/lib/fog/proxmox/storage/requests/download_appliance.rb +24 -0
  148. data/lib/fog/proxmox/storage/requests/list.rb +22 -0
  149. data/lib/fog/proxmox/storage/requests/list_appliances.rb +23 -0
  150. data/lib/fog/proxmox/storage/requests/upload.rb +44 -0
  151. data/lib/fog/proxmox/storage.rb +82 -0
  152. data/lib/fog/proxmox/string.rb +32 -0
  153. data/lib/fog/proxmox/variables.rb +40 -0
  154. data/lib/fog/proxmox/version.rb +24 -0
  155. data/lib/fog/proxmox.rb +53 -0
  156. data/spec/compute_spec.rb +447 -0
  157. data/spec/fixtures/proxmox/compute/common_auth.yml +40 -0
  158. data/spec/fixtures/proxmox/compute/containers.yml +6921 -0
  159. data/spec/fixtures/proxmox/compute/nodes.yml +115 -0
  160. data/spec/fixtures/proxmox/compute/servers.yml +19011 -0
  161. data/spec/fixtures/proxmox/compute/snapshots.yml +1376 -0
  162. data/spec/fixtures/proxmox/compute/storages.yml +151 -0
  163. data/spec/fixtures/proxmox/compute/tasks.yml +465 -0
  164. data/spec/fixtures/proxmox/identity/auth.yml +42 -0
  165. data/spec/fixtures/proxmox/identity/auth_access_ticket.yml +77 -0
  166. data/spec/fixtures/proxmox/identity/auth_user_token.yml +77 -0
  167. data/spec/fixtures/proxmox/identity/common_auth.yml +40 -0
  168. data/spec/fixtures/proxmox/identity/domains.yml +665 -0
  169. data/spec/fixtures/proxmox/identity/groups.yml +345 -0
  170. data/spec/fixtures/proxmox/identity/permissions.yml +911 -0
  171. data/spec/fixtures/proxmox/identity/pools.yml +946 -0
  172. data/spec/fixtures/proxmox/identity/read_version.yml +40 -0
  173. data/spec/fixtures/proxmox/identity/roles.yml +345 -0
  174. data/spec/fixtures/proxmox/identity/tokens.yml +494 -0
  175. data/spec/fixtures/proxmox/identity/users.yml +725 -0
  176. data/spec/fixtures/proxmox/network/common_auth.yml +40 -0
  177. data/spec/fixtures/proxmox/network/networks.yml +375 -0
  178. data/spec/fixtures/proxmox/pve.home +34 -0
  179. data/spec/hash_spec.rb +43 -0
  180. data/spec/helpers/controller_helper_spec.rb +157 -0
  181. data/spec/helpers/cpu_helper_spec.rb +82 -0
  182. data/spec/helpers/disk_helper_spec.rb +304 -0
  183. data/spec/helpers/ip_helper_spec.rb +177 -0
  184. data/spec/helpers/nic_helper_spec.rb +144 -0
  185. data/spec/identity_spec.rb +416 -0
  186. data/spec/network_spec.rb +67 -0
  187. data/spec/proxmox_vcr.rb +97 -0
  188. data/spec/spec_helper.rb +41 -0
  189. metadata +566 -0
@@ -0,0 +1,271 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Fog::Proxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/compute/models/server'
21
+ require 'fog/proxmox/helpers/disk_helper'
22
+ require 'fog/proxmox/hash'
23
+ require 'fog/proxmox/errors'
24
+
25
+ module Fog
26
+ module Proxmox
27
+ class Compute
28
+ # Server model
29
+ # rubocop:disable ClassLength
30
+ class Server < Fog::Compute::Server
31
+ identity :vmid
32
+ attribute :type
33
+ attribute :node_id
34
+ attribute :config
35
+ attribute :digest
36
+ attribute :name
37
+ attribute :maxdisk
38
+ attribute :disk
39
+ attribute :diskwrite
40
+ attribute :diskread
41
+ attribute :uptime
42
+ attribute :netout
43
+ attribute :netin
44
+ attribute :cpu
45
+ attribute :cpus
46
+ attribute :template
47
+ attribute :status
48
+ attribute :maxcpu
49
+ attribute :mem
50
+ attribute :maxmem
51
+ attribute :qmpstatus
52
+ attribute :ha
53
+ attribute :pid
54
+ attribute :blockstat
55
+ attribute :balloon
56
+ attribute :ballooninfo
57
+ attribute :snapshots
58
+ attribute :tasks
59
+ attribute :vmgenid
60
+ attribute :lock
61
+ attribute :maxswap
62
+ attribute :swap
63
+
64
+ def initialize(new_attributes = {})
65
+ prepare_service_value(new_attributes)
66
+ Fog::Proxmox::Attributes.set_attr_and_sym('node_id', attributes, new_attributes)
67
+ Fog::Proxmox::Attributes.set_attr_and_sym('type', attributes, new_attributes)
68
+ Fog::Proxmox::Attributes.set_attr_and_sym('vmid', attributes, new_attributes)
69
+ requires :node_id, :type, :vmid
70
+ initialize_config(new_attributes)
71
+ initialize_snapshots
72
+ initialize_tasks
73
+ super(new_attributes)
74
+ end
75
+
76
+ def to_s
77
+ name
78
+ end
79
+
80
+ def container?
81
+ type == 'lxc'
82
+ end
83
+
84
+ def persisted?
85
+ service.next_vmid(vmid: vmid)
86
+ false
87
+ rescue Excon::Error::InternalServerError
88
+ false
89
+ rescue Excon::Error::BadRequest
90
+ true
91
+ end
92
+
93
+ # request with async task
94
+ def request(name, body_params = {}, path_params = {})
95
+ requires :node_id, :type
96
+ path = path_params.merge(node: node_id, type: type)
97
+ task_upid = service.send(name, path, body_params)
98
+ tasks.wait_for(task_upid)
99
+ end
100
+
101
+ def save(new_attributes = {})
102
+ body_params = new_attributes.merge(vmid: vmid)
103
+ body_params = body_params.merge(config.flatten) unless persisted?
104
+ request(:create_server, body_params)
105
+ reload
106
+ end
107
+
108
+ def update(new_attributes = {})
109
+ if container?
110
+ path_params = { node: node_id, type: type, vmid: vmid }
111
+ body_params = new_attributes
112
+ service.update_server(path_params, body_params)
113
+ else
114
+ request(:update_server, new_attributes, vmid: vmid)
115
+ end
116
+ reload
117
+ end
118
+
119
+ def destroy(options = {})
120
+ request(:delete_server, options, vmid: vmid)
121
+ end
122
+
123
+ def action(action, options = {})
124
+ action_known = %w[start stop resume suspend shutdown reset].include? action
125
+ message = "Action #{action} not implemented"
126
+ raise Fog::Errors::Error, message unless action_known
127
+
128
+ request(:action_server, options, action: action, vmid: vmid)
129
+ reload
130
+ end
131
+
132
+ def ready?
133
+ status == 'running'
134
+ end
135
+
136
+ def backup(options = {})
137
+ request(:create_backup, options.merge(vmid: vmid))
138
+ reload
139
+ end
140
+
141
+ def restore(backup, options = {})
142
+ attr_hash = if container?
143
+ options.merge(ostemplate: backup.volid, force: 1, restore: 1)
144
+ else
145
+ options.merge(archive: backup.volid, force: 1)
146
+ end
147
+ save(attr_hash)
148
+ end
149
+
150
+ def clone(newid, options = {})
151
+ request(:clone_server, options.merge(newid: newid), vmid: vmid)
152
+ reload
153
+ end
154
+
155
+ def create_template(options = {})
156
+ service.template_server({ node: node_id, type: type, vmid: vmid }, options)
157
+ reload
158
+ end
159
+
160
+ def migrate(target, options = {})
161
+ request(:migrate_server, options.merge(target: target), vmid: vmid)
162
+ end
163
+
164
+ def extend(disk, size, options = {})
165
+ if container?
166
+ request(:resize_container, options.merge(disk: disk, size: size), vmid: vmid)
167
+ else
168
+ service.resize_server({ vmid: vmid, node: node_id }, options.merge(disk: disk, size: size))
169
+ end
170
+ reload
171
+ end
172
+
173
+ def move(volume, storage, options = {})
174
+ if container?
175
+ request(:move_volume, options.merge(volume: volume, storage: storage), vmid: vmid)
176
+ else
177
+ request(:move_disk, options.merge(disk: volume, storage: storage), vmid: vmid)
178
+ end
179
+ reload
180
+ end
181
+
182
+ def attach(disk, options = {})
183
+ disk_hash = Fog::Proxmox::DiskHelper.flatten(disk.merge(options: options))
184
+ update(disk_hash)
185
+ end
186
+
187
+ def detach(diskid)
188
+ update(delete: diskid)
189
+ end
190
+
191
+ def start_console(options = {})
192
+ unless ready?
193
+ raise ::Fog::Proxmox::Errors::ServiceError,
194
+ 'Unable to start console because server not running.'
195
+ end
196
+
197
+ if container?
198
+ type_console = options[:console]
199
+ options.delete_if { |option| [:console].include? option }
200
+ unless type_console
201
+ raise ::Fog::Proxmox::Errors::ServiceError,
202
+ 'Unable to start console because console container config is not set or unknown.'
203
+ end
204
+ else
205
+ type_console = config.type_console
206
+ unless type_console
207
+ raise ::Fog::Proxmox::Errors::ServiceError,
208
+ 'Unable to start console because VGA display server config is not set or unknown.'
209
+ end
210
+ end
211
+ requires :vmid, :node_id, :type
212
+ path_params = { node: node_id, type: type, vmid: vmid }
213
+ body_params = options
214
+ data = service.send(('create_' + type_console).to_sym, path_params, body_params)
215
+ task_upid = data['upid']
216
+ if task_upid
217
+ task = tasks.get(task_upid)
218
+ task.wait_for { running? }
219
+ end
220
+ data
221
+ end
222
+
223
+ def connect_vnc(options = {})
224
+ path_params = { node: node_id, type: type, vmid: vmid }
225
+ query_params = { port: options['port'], vncticket: options['ticket'] }
226
+ service.get_vnc(path_params, query_params)
227
+ end
228
+
229
+ def backups
230
+ list 'backup'
231
+ end
232
+
233
+ def images
234
+ list 'images'
235
+ end
236
+
237
+ def list(content)
238
+ storages = node.storages.list_by_content_type content
239
+ volumes = []
240
+ storages.each { |storage| volumes += storage.volumes.list_by_content_type_and_by_server(content, identity) }
241
+ volumes
242
+ end
243
+
244
+ protected
245
+
246
+ def initialize_config(new_attributes = {})
247
+ options = { service: service, vmid: vmid }
248
+ attributes[:config] = Fog::Proxmox::Compute::ServerConfig.new(options.merge(new_attributes))
249
+ end
250
+
251
+ private
252
+
253
+ def initialize_snapshots
254
+ attributes[:snapshots] =
255
+ Fog::Proxmox::Compute::Snapshots.new(service: service, server_id: vmid, server_type: type, node_id: node_id)
256
+ end
257
+
258
+ def initialize_tasks
259
+ attributes[:tasks] = Fog::Proxmox::Compute::Tasks.new(service: service, node_id: node_id).select do |task|
260
+ task.id == vmid
261
+ end
262
+ end
263
+
264
+ def node
265
+ Fog::Proxmox::Compute::Node.new(service: service, node: node_id)
266
+ end
267
+ end
268
+ # rubocop:enable ClassLength
269
+ end
270
+ end
271
+ end
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Fog::Proxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/attributes'
21
+ require 'fog/proxmox/helpers/nic_helper'
22
+ require 'fog/proxmox/helpers/controller_helper'
23
+
24
+ module Fog
25
+ module Proxmox
26
+ class Compute
27
+ # ServerConfig model: https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/{qemu|lxc}/{vmid}/config
28
+ # memory, balloon, shares and swap are in Mb
29
+ class ServerConfig < Fog::Model
30
+ identity :vmid
31
+ attribute :description
32
+ attribute :ostype
33
+ attribute :smbios1
34
+ attribute :bios
35
+ attribute :numa
36
+ attribute :kvm
37
+ attribute :vcpus
38
+ attribute :cores
39
+ attribute :bootdisk
40
+ attribute :onboot
41
+ attribute :boot
42
+ attribute :agent
43
+ attribute :scsihw
44
+ attribute :sockets
45
+ attribute :memory
46
+ attribute :shares
47
+ attribute :balloon
48
+ attribute :name
49
+ attribute :cpu
50
+ attribute :cpulimit
51
+ attribute :cpuunits
52
+ attribute :keyboard
53
+ attribute :vga
54
+ attribute :storage
55
+ attribute :template
56
+ attribute :arch
57
+ attribute :swap
58
+ attribute :hostname
59
+ attribute :nameserver
60
+ attribute :searchdomain
61
+ attribute :password
62
+ attribute :startup
63
+ attribute :console
64
+ attribute :cmode
65
+ attribute :tty
66
+ attribute :force
67
+ attribute :lock
68
+ attribute :pool
69
+ attribute :bwlimit
70
+ attribute :unprivileged
71
+ attribute :interfaces
72
+ attribute :disks
73
+ attribute :ciuser
74
+ attribute :cipassword
75
+ attribute :cicustom
76
+ attribute :citype
77
+ attribute :ipconfigs
78
+ attribute :sshkeys
79
+ attribute :searchdomain
80
+ attribute :nameserver
81
+
82
+ def initialize(new_attributes = {})
83
+ prepare_service_value(new_attributes)
84
+ Fog::Proxmox::Attributes.set_attr_and_sym('vmid', attributes, new_attributes)
85
+ requires :vmid
86
+ initialize_interfaces(new_attributes)
87
+ initialize_disks(new_attributes)
88
+ super(new_attributes)
89
+ end
90
+
91
+ def mac_addresses
92
+ Fog::Proxmox::NicHelper.to_mac_adresses_array(interfaces)
93
+ end
94
+
95
+ def type_console
96
+ console = 'vnc' if %w[std cirrus vmware].include?(vga)
97
+ console = 'spice' if %w[qxl qxl2 qxl3 qxl4].include?(vga)
98
+ console = 'term' if %w[serial0 serial1 serial2 serial3].include?(vga)
99
+ console
100
+ end
101
+
102
+ def flatten
103
+ flat_hash = attributes.reject { |attribute| %i[node_id type vmid disks interfaces].include? attribute }
104
+ flat_hash.merge(interfaces_flatten)
105
+ flat_hash.merge(disks_flatten)
106
+ flat_hash
107
+ end
108
+
109
+ private
110
+
111
+ def interfaces_flatten
112
+ flat_hash = {}
113
+ interfaces.each { |interface| flat_hash.merge(interface.flatten) }
114
+ flat_hash
115
+ end
116
+
117
+ def disks_flatten
118
+ flat_hash = {}
119
+ disks.each { |disk| flat_hash.merge(disk.flatten) }
120
+ flat_hash
121
+ end
122
+
123
+ def initialize_interfaces(new_attributes)
124
+ nets = Fog::Proxmox::NicHelper.collect_nics(new_attributes)
125
+ attributes[:interfaces] = Fog::Proxmox::Compute::Interfaces.new
126
+ nets.each do |key, value|
127
+ nic_hash = {
128
+ id: key.to_s,
129
+ model: Fog::Proxmox::NicHelper.extract_nic_id(value),
130
+ macaddr: Fog::Proxmox::NicHelper.extract_mac_address(value)
131
+ }
132
+ names = Fog::Proxmox::Compute::Interface.attributes.reject do |attribute|
133
+ %i[id macaddr model].include? attribute
134
+ end
135
+ names.each { |name| nic_hash.store(name.to_sym, Fog::Proxmox::ControllerHelper.extract(name, value)) }
136
+ attributes[:interfaces] << Fog::Proxmox::Compute::Interface.new(nic_hash)
137
+ end
138
+ end
139
+
140
+ def initialize_disks(new_attributes)
141
+ controllers = Fog::Proxmox::ControllerHelper.collect_controllers(new_attributes)
142
+ attributes[:disks] = Fog::Proxmox::Compute::Disks.new
143
+ controllers.each do |key, value|
144
+ storage, volid, size = Fog::Proxmox::DiskHelper.extract_storage_volid_size(value)
145
+ disk_hash = {
146
+ id: key.to_s,
147
+ size: size,
148
+ volid: volid,
149
+ storage: storage
150
+ }
151
+ names = Fog::Proxmox::Compute::Disk.attributes.reject do |attribute|
152
+ %i[id size storage volid].include? attribute
153
+ end
154
+ names.each { |name| disk_hash.store(name.to_sym, Fog::Proxmox::ControllerHelper.extract(name, value)) }
155
+ attributes[:disks] << Fog::Proxmox::Compute::Disk.new(disk_hash)
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Fog::Proxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/compute/models/server'
21
+
22
+ module Fog
23
+ module Proxmox
24
+ class Compute
25
+ # Servers Collection
26
+ class Servers < Fog::Collection
27
+ model Fog::Proxmox::Compute::Server
28
+ attribute :node_id
29
+ attribute :type
30
+
31
+ def new(new_attributes = {})
32
+ super({ node_id: node_id, type: type }.merge(new_attributes))
33
+ end
34
+
35
+ def next_id
36
+ service.next_vmid
37
+ end
38
+
39
+ def id_valid?(vmid)
40
+ service.next_vmid(vmid: vmid)
41
+ true
42
+ rescue Excon::Errors::BadRequest
43
+ false
44
+ end
45
+
46
+ def containers?
47
+ type == 'lxc'
48
+ end
49
+
50
+ def get(id)
51
+ path_params = { node: node_id, type: type, vmid: id }
52
+ begin
53
+ status_data = service.get_server_status path_params
54
+ config_data = service.get_server_config path_params
55
+ rescue StandardError => e
56
+ if e.respond_to?('response') && e.response.respond_to?('data') && e.response.data.has_key?(:reason_phrase) && e.response.data[:reason_phrase].end_with?('does not exist')
57
+ raise(Fog::Errors::NotFound)
58
+ end
59
+
60
+ raise(e)
61
+ else
62
+ data = status_data.merge(config_data).merge(node: node_id, vmid: id)
63
+ new(data)
64
+ end
65
+ end
66
+
67
+ def all(options = {})
68
+ body_params = options.merge(node: node_id, type: type)
69
+ load service.list_servers(body_params)
70
+ end
71
+
72
+ def create(new_attributes = {})
73
+ object = new(new_attributes.select { |key, _value| %i[node_id vmid type].include? key.to_sym })
74
+ object.save(new_attributes.reject { |key, _value| %i[node_id vmid type].include? key.to_sym })
75
+ object
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ # Copyright 2018 Tristan Robert
12
+
13
+ # This file is part of Fog::Proxmox.
14
+
15
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
16
+ # it under the terms of the GNU General Public License as published by
17
+ # the Free Software Foundation, either version 3 of the License, or
18
+ # (at your option) any later version.
19
+
20
+ # Fog::Proxmox is distributed in the hope that it will be useful,
21
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ # GNU General Public License for more details.
24
+
25
+ # You should have received a copy of the GNU General Public License
26
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
27
+
28
+ module Fog
29
+ module Proxmox
30
+ class Compute
31
+ # class Snapshot model
32
+ class Snapshot < Fog::Model
33
+ identity :name
34
+ attribute :description
35
+ attribute :snaptime
36
+ attribute :vmstate
37
+ attribute :node_id
38
+ attribute :server_id
39
+ attribute :server_type
40
+ attribute :vmgenid
41
+
42
+ def initialize(new_attributes = {})
43
+ prepare_service_value(new_attributes)
44
+ Fog::Proxmox::Attributes.set_attr_and_sym('node_id', attributes, new_attributes)
45
+ Fog::Proxmox::Attributes.set_attr_and_sym('server_id', attributes, new_attributes)
46
+ Fog::Proxmox::Attributes.set_attr_and_sym('server_type', attributes, new_attributes)
47
+ Fog::Proxmox::Attributes.set_attr_and_sym('name', attributes, new_attributes)
48
+ requires :node_id, :server_id, :server_type, :name
49
+ super(new_attributes)
50
+ end
51
+
52
+ def save
53
+ path_params = { node: node_id, type: server_type, vmid: server_id }
54
+ body_params = { snapname: name, vmstate: vmstate }
55
+ server.tasks.wait_for(service.create_snapshot(path_params, body_params))
56
+ end
57
+
58
+ def update
59
+ path_params = { node: node_id, type: server_type, vmid: server_id, snapname: name }
60
+ body_params = { description: description }
61
+ service.update_snapshot(path_params, body_params)
62
+ end
63
+
64
+ def rollback
65
+ path_params = { node: node_id, type: server_type, vmid: server_id, snapname: name }
66
+ server.tasks.wait_for(service.rollback_snapshot(path_params))
67
+ end
68
+
69
+ def destroy(force = 0)
70
+ path_params = { node: node_id, type: server_type, vmid: server_id, snapname: name }
71
+ query_params = { force: force }
72
+ server.tasks.wait_for(service.delete_snapshot(path_params, query_params))
73
+ end
74
+
75
+ private
76
+
77
+ def server
78
+ Fog::Proxmox::Compute::Server.new(service: service, node_id: node_id, type: server_type, vmid: server_id)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of Fog::Proxmox.
6
+
7
+ # Fog::Proxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Fog::Proxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/compute/models/snapshot'
21
+
22
+ module Fog
23
+ module Proxmox
24
+ class Compute
25
+ # class Snapshots Collection of snapshots
26
+ class Snapshots < Fog::Collection
27
+ model Fog::Proxmox::Compute::Snapshot
28
+ attribute :server_id
29
+ attribute :server_type
30
+ attribute :node_id
31
+
32
+ def new(new_attributes = {})
33
+ super({ node_id: node_id, server_id: server_id, server_type: server_type }.merge(new_attributes))
34
+ end
35
+
36
+ def all
37
+ path_params = { node: node_id, type: server_type, vmid: server_id }
38
+ load service.list_snapshots(path_params)
39
+ end
40
+
41
+ def get(name)
42
+ all.find { |snapshot| snapshot.identity == name }
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end