foreman_fog_proxmox 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +619 -0
- data/README.md +175 -0
- data/Rakefile +47 -0
- data/app/assets/javascripts/foreman_fog_proxmox/proxmox.js +110 -0
- data/app/controllers/concerns/foreman_fog_proxmox/controller/parameters/compute_resource.rb +41 -0
- data/app/controllers/foreman_fog_proxmox/compute_resources_controller.rb +38 -0
- data/app/helpers/proxmox_compute_helper.rb +154 -0
- data/app/helpers/proxmox_compute_selectors_helper.rb +148 -0
- data/app/models/concerns/fog_extensions/proxmox/disk.rb +29 -0
- data/app/models/concerns/fog_extensions/proxmox/server.rb +82 -0
- data/app/models/concerns/fog_extensions/proxmox/server_config.rb +49 -0
- data/app/models/concerns/fog_extensions/proxmox/volume.rb +29 -0
- data/app/models/foreman_fog_proxmox/options_select.rb +36 -0
- data/app/models/foreman_fog_proxmox/proxmox.rb +394 -0
- data/app/views/api/v2/compute_resources/proxmox.json.rabl +1 -0
- data/app/views/compute_resources/form/_proxmox.html.erb +24 -0
- data/app/views/compute_resources/show/_proxmox.html.erb +21 -0
- data/app/views/compute_resources_vms/form/proxmox/_base.html.erb +45 -0
- data/app/views/compute_resources_vms/form/proxmox/_config.html.erb +55 -0
- data/app/views/compute_resources_vms/form/proxmox/_network.html.erb +24 -0
- data/app/views/compute_resources_vms/form/proxmox/_volume.html.erb +23 -0
- data/app/views/compute_resources_vms/index/_proxmox.html.erb +45 -0
- data/app/views/compute_resources_vms/show/_proxmox.html.erb +36 -0
- data/app/views/dashboard/_foreman_fog_proxmox_widget.erb +19 -0
- data/app/views/images/form/_proxmox.html.erb +4 -0
- data/config/routes.rb +24 -0
- data/lib/foreman_fog_proxmox.rb +23 -0
- data/lib/foreman_fog_proxmox/engine.rb +92 -0
- data/lib/foreman_fog_proxmox/version.rb +22 -0
- data/lib/tasks/foreman_fog_proxmox_tasks.rake +47 -0
- data/locale/Makefile +60 -0
- data/locale/en/foreman_fog_proxmox.po +19 -0
- data/locale/foreman_fog_proxmox.pot +19 -0
- data/locale/fr/foreman_fog_proxmox.po +19 -0
- data/locale/gemspec.rb +2 -0
- data/test/factories/foreman_fog_proxmox_factories.rb +33 -0
- data/test/helpers/proxmox_compute_helper_test.rb +130 -0
- data/test/test_plugin_helper.rb +15 -0
- data/test/unit/foreman_fog_proxmox_test.rb +11 -0
- metadata +144 -0
data/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.com/tristanrobert/foreman_proxmox)
|
|
4
|
+
[](https://codeclimate.com/github/tristanrobert/foreman_proxmox/maintainability)
|
|
5
|
+
[](https://codeclimate.com/github/tristanrobert/foreman_proxmox/test_coverage)
|
|
6
|
+
|
|
7
|
+
# ForemanFogProxmox
|
|
8
|
+
|
|
9
|
+
[Foreman](http://theforeman.org/) plugin that adds [Proxmox](https://www.proxmox.com/en/proxmox-ve) compute resource: managing virtual machines and containers using the [fog-proxmox](https://github.com/fog/fog-proxmox) module.
|
|
10
|
+
|
|
11
|
+
It is intended to satisfy this [feature](http://projects.theforeman.org/issues/2186).
|
|
12
|
+
|
|
13
|
+
## Compatibility
|
|
14
|
+
|
|
15
|
+
Tested with:
|
|
16
|
+
|
|
17
|
+
* Foreman = 1.17.1
|
|
18
|
+
* Fog-proxmox >= 0.4.0
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
### Prerequisites
|
|
23
|
+
|
|
24
|
+
You need [nodejs](https://nodejs.org/en/download/package-manager/) installed in order to use foreman-assets package.
|
|
25
|
+
|
|
26
|
+
### From gem
|
|
27
|
+
|
|
28
|
+
See complete details in [plugin installation from gem](https://theforeman.org/plugins/#2.3AdvancedInstallationfromGems)
|
|
29
|
+
|
|
30
|
+
Here is a Debian sample:
|
|
31
|
+
|
|
32
|
+
* Install foreman 1.17 [from OS packages](https://theforeman.org/manuals/1.17/index.html#3.3InstallFromPackages):
|
|
33
|
+
|
|
34
|
+
```shell
|
|
35
|
+
sudo apt install -y foreman foreman-compute foreman-sqlite3 foreman-assets
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
* Use only foreman user (**not root!**) `sudo -u foreman ...`
|
|
39
|
+
* In /usr/share/foreman/bundler.d directory, add Gemfile.local.rb file and add this line in it:
|
|
40
|
+
|
|
41
|
+
```shell
|
|
42
|
+
echo "gem 'foreman_fog_proxmox'" | sudo -u foreman tee /usr/share/foreman/bundler.d/Gemfile.local.rb
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
* Install the gem plugin:
|
|
46
|
+
|
|
47
|
+
```shell
|
|
48
|
+
sudo -u foreman /usr/bin/foreman-ruby /usr/bin/bundle install
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
* Precompile plugin assets:
|
|
52
|
+
|
|
53
|
+
```shell
|
|
54
|
+
/usr/bin/foreman-ruby /usr/bin/bundle exec bin/rake plugin:assets:precompile[foreman_fog_proxmox]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
* Complete installation of foreman 1.17 with foreman-installer:
|
|
58
|
+
|
|
59
|
+
```shell
|
|
60
|
+
sudo apt install -y foreman-installer
|
|
61
|
+
sudo foreman-installer
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
If you don't want to have HTTP 503 errors when apt is trying to install puppetserver, then add this before launching foreman-installer:
|
|
65
|
+
|
|
66
|
+
```shell
|
|
67
|
+
echo 'Acquire::http::User-agent "Mozilla/5.0 (Linux)";' | sudo tee /etc/apt/apt.conf.d/96useragent
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
See complete details in [plugin installation from gem](https://theforeman.org/plugins/#2.3.2Debiandistributions)
|
|
71
|
+
|
|
72
|
+
Then you can check plugin installation after login into your new foreman server seeing the about foreman page:
|
|
73
|
+
|
|
74
|
+

|
|
75
|
+

|
|
76
|
+
|
|
77
|
+
### From OS packages
|
|
78
|
+
|
|
79
|
+
Deb, rpm: work in progress...
|
|
80
|
+
|
|
81
|
+
Please see the Foreman manual for complete instructions:
|
|
82
|
+
|
|
83
|
+
* [Foreman: How to Install a Plugin](http://theforeman.org/manuals/latest/index.html#6.1InstallaPlugin)
|
|
84
|
+
|
|
85
|
+
## Usage
|
|
86
|
+
|
|
87
|
+
* [Compute resource](.github/compute_resource.md)
|
|
88
|
+
* [Manage hosts](.github/hosts.md)
|
|
89
|
+
|
|
90
|
+
## Development
|
|
91
|
+
|
|
92
|
+
### Prerequisites
|
|
93
|
+
|
|
94
|
+
You need a Proxmox VE >= 5.1 server running.
|
|
95
|
+
|
|
96
|
+
You also need nodejs in your dev machine to run webpack-dev-server.
|
|
97
|
+
|
|
98
|
+
### Platform
|
|
99
|
+
|
|
100
|
+
* Fork this github repo.
|
|
101
|
+
* Clone it on your local machine
|
|
102
|
+
* Install foreman v1.17 on your machine:
|
|
103
|
+
|
|
104
|
+
```shell
|
|
105
|
+
git clone https://github.com/theforeman/foreman
|
|
106
|
+
git checkout tags/1.17
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
* Create a Gemfile.local.rb file in foreman/bundler.d/
|
|
110
|
+
* Add this line:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
gem 'foreman_fog_proxmox', :path => '/your_path_to/foreman_proxmox'
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
* In foreman directory, install dependencies:
|
|
117
|
+
|
|
118
|
+
```shell
|
|
119
|
+
bundle install
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
* Configure foreman settings:
|
|
123
|
+
|
|
124
|
+
```shell
|
|
125
|
+
cp config/settings.yaml.example config/settings.yaml
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
* Install foreman database (sqlite is default in rails development):
|
|
129
|
+
|
|
130
|
+
```shell
|
|
131
|
+
cp config/database.yaml.example config/database.yaml
|
|
132
|
+
bundle exec rake db:migrate
|
|
133
|
+
bundle exec rake db:seed
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
* You can reset admin password if needed:
|
|
137
|
+
|
|
138
|
+
```shell
|
|
139
|
+
bundle exec rake permissions:reset
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
* In foreman directory, after you modify foreman_fog_proxmox specific assets (proxmox.js, etc) you have to precompile it:
|
|
143
|
+
|
|
144
|
+
```shell
|
|
145
|
+
bundle plugin:assets:precompile[foreman_fog_proxmox]
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
* In foreman directory, run rails server:
|
|
149
|
+
|
|
150
|
+
```shell
|
|
151
|
+
rails server
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
* In foreman directory, run in a new terminal the webpack-dev-server:
|
|
155
|
+
|
|
156
|
+
```shell
|
|
157
|
+
./node_modules/.bin/webpack-dev-server --config config/webpack.config.js
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
See details in [foreman plugin development](https://projects.theforeman.org/projects/foreman/wiki/How_to_Create_a_Plugin)
|
|
161
|
+
|
|
162
|
+
## Contributing
|
|
163
|
+
|
|
164
|
+
You can reach the [contributors](CONTRIBUTORS.md).
|
|
165
|
+
Bug reports and pull requests are welcome on GitHub at [ForemanFogProxmox](https://github.com/tristanrobert/foreman_proxmox). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
166
|
+
|
|
167
|
+
Please read [how to contribute](CONTRIBUTING.md).
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
The code is available as open source under the terms of the [GNU Public License v3](LICENSE).
|
|
172
|
+
|
|
173
|
+
## Code of Conduct
|
|
174
|
+
|
|
175
|
+
Everyone interacting in the ForemanProxmox project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env rake
|
|
2
|
+
begin
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
rescue LoadError
|
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
|
6
|
+
end
|
|
7
|
+
begin
|
|
8
|
+
require 'rdoc/task'
|
|
9
|
+
rescue LoadError
|
|
10
|
+
require 'rdoc/rdoc'
|
|
11
|
+
require 'rake/rdoctask'
|
|
12
|
+
RDoc::Task = Rake::RDocTask
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
17
|
+
rdoc.title = 'ForemanFogProxmox'
|
|
18
|
+
rdoc.options << '--line-numbers'
|
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
|
|
24
|
+
|
|
25
|
+
Bundler::GemHelper.install_tasks
|
|
26
|
+
|
|
27
|
+
require 'rake/testtask'
|
|
28
|
+
|
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
|
30
|
+
t.libs << 'lib'
|
|
31
|
+
t.libs << 'test'
|
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
|
33
|
+
t.verbose = false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
task default: :test
|
|
37
|
+
|
|
38
|
+
begin
|
|
39
|
+
require 'rubocop/rake_task'
|
|
40
|
+
RuboCop::RakeTask.new
|
|
41
|
+
rescue StandardError => _
|
|
42
|
+
puts 'Rubocop not loaded.'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
task :default do
|
|
46
|
+
Rake::Task['rubocop'].execute
|
|
47
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Copyright 2018 Tristan Robert
|
|
2
|
+
|
|
3
|
+
// This file is part of ForemanFogProxmox.
|
|
4
|
+
|
|
5
|
+
// ForemanFogProxmox is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
|
|
10
|
+
// ForemanFogProxmox is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
// You should have received a copy of the GNU General Public License
|
|
16
|
+
// along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
function cdromSelected(item) {
|
|
19
|
+
var selected = $(item).val();
|
|
20
|
+
var cdrom_image_form = $('#cdrom_image_form');
|
|
21
|
+
|
|
22
|
+
switch (selected) {
|
|
23
|
+
case 'none':
|
|
24
|
+
initStorage();
|
|
25
|
+
initOptions('iso');
|
|
26
|
+
cdrom_image_form.hide();
|
|
27
|
+
break;
|
|
28
|
+
case 'cdrom':
|
|
29
|
+
initStorage();
|
|
30
|
+
initOptions('iso');
|
|
31
|
+
cdrom_image_form.hide();
|
|
32
|
+
break;
|
|
33
|
+
case 'image':
|
|
34
|
+
initStorage();
|
|
35
|
+
initOptions('iso');
|
|
36
|
+
cdrom_image_form.show();
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function initStorage(){
|
|
45
|
+
var select = '#host_compute_attributes_config_attributes_cdrom_storage';
|
|
46
|
+
$(select + ' option:selected').prop('selected',false);
|
|
47
|
+
$(select).val('');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function initOptions(name){
|
|
51
|
+
var select = '#host_compute_attributes_config_attributes_cdrom_'+name;
|
|
52
|
+
$(select).empty();
|
|
53
|
+
$(select).append($("<option></option>").val('').text(''));
|
|
54
|
+
$(select).val('');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function storageIsoSelected(item) {
|
|
58
|
+
var storage = $(item).val();
|
|
59
|
+
if (storage != '') {
|
|
60
|
+
tfm.tools.showSpinner();
|
|
61
|
+
$.getJSON({
|
|
62
|
+
type: 'get',
|
|
63
|
+
url: '/foreman_fog_proxmox/isos/'+storage,
|
|
64
|
+
complete: function(){
|
|
65
|
+
tfm.tools.hideSpinner();
|
|
66
|
+
},
|
|
67
|
+
error: function(j,status,error){
|
|
68
|
+
console.log("Error=" + error +", status=" + status + " loading isos for storage=" + storage);
|
|
69
|
+
},
|
|
70
|
+
success: function(isos) {
|
|
71
|
+
initOptions('iso');
|
|
72
|
+
$.each(isos, function(i,iso){
|
|
73
|
+
$('#host_compute_attributes_config_attributes_cdrom_iso').append($("<option></option>").val(iso.volid).text(iso.volid));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
} else {
|
|
78
|
+
initOptions('iso');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function controllerSelected(item){
|
|
83
|
+
var controller = $(item).val();
|
|
84
|
+
var id = $(item).attr('id');
|
|
85
|
+
var pattern = /(\w+)(\d+)(\w+)/i;
|
|
86
|
+
var index = pattern.exec(id)[2];
|
|
87
|
+
var max = computeControllerMaxDevice(controller);
|
|
88
|
+
$('#host_compute_attributes_volumes_attributes_' + index + '_device').attr('data-soft-max',max);
|
|
89
|
+
tfm.numFields.initAll();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function computeControllerMaxDevice(controller){
|
|
93
|
+
switch (controller) {
|
|
94
|
+
case 'ide':
|
|
95
|
+
return 3;
|
|
96
|
+
break;
|
|
97
|
+
case 'sata':
|
|
98
|
+
return 5;
|
|
99
|
+
break;
|
|
100
|
+
case 'scsi':
|
|
101
|
+
return 13;
|
|
102
|
+
break;
|
|
103
|
+
case 'virtio':
|
|
104
|
+
return 15;
|
|
105
|
+
break;
|
|
106
|
+
default:
|
|
107
|
+
return 1;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2018 Tristan Robert
|
|
4
|
+
|
|
5
|
+
# This file is part of ForemanFogProxmox.
|
|
6
|
+
|
|
7
|
+
# ForemanFogProxmox 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
|
+
# ForemanFogProxmox 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 ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
|
|
20
|
+
module ForemanFogProxmox
|
|
21
|
+
module Controller
|
|
22
|
+
module Parameters
|
|
23
|
+
module ComputeResource
|
|
24
|
+
extend ActiveSupport::Concern
|
|
25
|
+
|
|
26
|
+
class_methods do
|
|
27
|
+
def compute_resource_params_filter
|
|
28
|
+
super.tap do |filter|
|
|
29
|
+
filter.permit :ssl_verify_peer,
|
|
30
|
+
:ssl_certs, :disable_proxy
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def compute_resource_params
|
|
35
|
+
self.class.compute_resource_params_filter.filter_params(params, parameter_filter_context)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2018 Tristan Robert
|
|
4
|
+
|
|
5
|
+
# This file is part of ForemanFogProxmox.
|
|
6
|
+
|
|
7
|
+
# ForemanFogProxmox 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
|
+
# ForemanFogProxmox 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 ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
|
|
20
|
+
module ForemanFogProxmox
|
|
21
|
+
class ComputeResourcesController < ::ApplicationController
|
|
22
|
+
before_action :load_compute_resource
|
|
23
|
+
|
|
24
|
+
# GET foreman_fog_proxmox/isos/:storage
|
|
25
|
+
def isos
|
|
26
|
+
volumes = @compute_resource.isos(params[:storage])
|
|
27
|
+
respond_to do |format|
|
|
28
|
+
format.json { render :json => volumes }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def load_compute_resource
|
|
35
|
+
@compute_resource = ComputeResource.find_by(type: 'ForemanFogProxmox::Proxmox')
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2018 Tristan Robert
|
|
4
|
+
|
|
5
|
+
# This file is part of ForemanFogProxmox.
|
|
6
|
+
|
|
7
|
+
# ForemanFogProxmox 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
|
+
# ForemanFogProxmox 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 ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
|
|
20
|
+
require 'fog/proxmox/helpers/disk_helper'
|
|
21
|
+
require 'fog/proxmox/helpers/nic_helper'
|
|
22
|
+
|
|
23
|
+
module ProxmoxComputeHelper
|
|
24
|
+
|
|
25
|
+
KILO = 1024
|
|
26
|
+
MEGA = KILO * KILO
|
|
27
|
+
GIGA = KILO * MEGA
|
|
28
|
+
|
|
29
|
+
def parse_vm(args)
|
|
30
|
+
return {} unless args
|
|
31
|
+
return {} if args.empty?
|
|
32
|
+
config = args['config_attributes']
|
|
33
|
+
cdrom_a = %w[cdrom cdrom_storage cdrom_iso]
|
|
34
|
+
cdrom = parse_cdrom(config.select { |key,_value| cdrom_a.include? key })
|
|
35
|
+
volumes = parse_volumes(args['volumes_attributes'])
|
|
36
|
+
cpu_a = %w[cpu_type spectre pcid vcpus cpulimit cpuunits cores sockets numa]
|
|
37
|
+
cpu = parse_cpu(config.select { |key,_value| cpu_a.include? key })
|
|
38
|
+
memory_a = %w[memory min_memory balloon shares]
|
|
39
|
+
memory = parse_memory(config.select { |key,_value| memory_a.include? key })
|
|
40
|
+
interfaces_attributes = args['interfaces_attributes']
|
|
41
|
+
networks = parse_interfaces(interfaces_attributes)
|
|
42
|
+
general_a = %w[node config_attributes volumes_attributes interfaces_attributes firmware_type provision_method]
|
|
43
|
+
logger.debug("general_a: #{general_a}")
|
|
44
|
+
parsed_vm = args.reject { |key,value| general_a.include?(key) || value.empty? }
|
|
45
|
+
config_a = []
|
|
46
|
+
config_a += cpu_a
|
|
47
|
+
config_a += cdrom_a
|
|
48
|
+
config_a += memory_a
|
|
49
|
+
parsed_config = config.reject { |key,value| config_a.include?(key) || value.empty? }
|
|
50
|
+
logger.debug("parse_config(): #{parsed_config}")
|
|
51
|
+
parsed_vm = parsed_vm.merge(parsed_config).merge(cpu).merge(memory).merge(cdrom)
|
|
52
|
+
networks.each { |network| parsed_vm = parsed_vm.merge(network) }
|
|
53
|
+
volumes.each { |volume| parsed_vm = parsed_vm.merge(volume) }
|
|
54
|
+
logger.debug("parse_vm(): #{parsed_vm}")
|
|
55
|
+
parsed_vm
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def parse_memory(args)
|
|
59
|
+
memory = { memory: args['memory'].to_i }
|
|
60
|
+
ballooned = args['balloon'].to_i == 1
|
|
61
|
+
if ballooned
|
|
62
|
+
memory.store(:shares,args['shares'].to_i)
|
|
63
|
+
memory.store(:balloon,args['min_memory'].to_i)
|
|
64
|
+
else
|
|
65
|
+
memory.store(:balloon,args['balloon'].to_i)
|
|
66
|
+
end
|
|
67
|
+
logger.debug("parse_memory(): #{memory}")
|
|
68
|
+
memory
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def parse_cpu(args)
|
|
72
|
+
cpu = "cputype=#{args['cpu_type']}"
|
|
73
|
+
spectre = args['spectre'].to_i == 1
|
|
74
|
+
pcid = args['pcid'].to_i == 1
|
|
75
|
+
cpu += ",flags=" if spectre || pcid
|
|
76
|
+
cpu += "+spec-ctrl" if spectre
|
|
77
|
+
cpu += ";" if spectre && pcid
|
|
78
|
+
cpu += "+pcid" if pcid
|
|
79
|
+
args.delete_if { |key,value| %w[cpu_type spectre pcid].include?(key) || value.empty? }
|
|
80
|
+
args.each_value { |value| value.to_i }
|
|
81
|
+
parsed_cpu = { cpu: cpu }.merge(args)
|
|
82
|
+
logger.debug("parse_cpu(): #{parsed_cpu}")
|
|
83
|
+
parsed_cpu
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def parse_cdrom(args)
|
|
87
|
+
cdrom = args['cdrom']
|
|
88
|
+
cdrom_image = args['cdrom_iso']
|
|
89
|
+
volid = cdrom_image.empty? ? cdrom : cdrom_image
|
|
90
|
+
cdrom = "#{volid},media=cdrom"
|
|
91
|
+
{ide2: cdrom}
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def parse_volume(args)
|
|
95
|
+
disk = {}
|
|
96
|
+
id = args['id']
|
|
97
|
+
id = "#{args['controller']}#{args['device']}" unless id
|
|
98
|
+
delete = args['_delete'].to_i == 1
|
|
99
|
+
args.delete_if { |_key,value| value.empty? }
|
|
100
|
+
if delete
|
|
101
|
+
logger.debug("parse_volume(): delete id=#{id}")
|
|
102
|
+
disk.store(:delete, id)
|
|
103
|
+
disk
|
|
104
|
+
else
|
|
105
|
+
disk.store(:id, id)
|
|
106
|
+
disk.store(:volid, args['volid'])
|
|
107
|
+
disk.store(:storage, args['storage'].to_s)
|
|
108
|
+
disk.store(:size, args['size'])
|
|
109
|
+
options = args.reject { |key,_value| %w[id volid controller device storage size _delete].include? key}
|
|
110
|
+
disk.store(:options, options)
|
|
111
|
+
logger.debug("parse_volume(): add disk=#{disk}")
|
|
112
|
+
Fog::Proxmox::DiskHelper.flatten(disk)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def parse_volumes(args)
|
|
117
|
+
volumes = []
|
|
118
|
+
args.each_value { |value| volumes.push(parse_volume(value))} if args
|
|
119
|
+
logger.debug("parse_volumes(): volumes=#{volumes}")
|
|
120
|
+
volumes
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def parse_interfaces(args)
|
|
124
|
+
nics = []
|
|
125
|
+
args.each_value { |value| nics.push(parse_interface(value))} if args
|
|
126
|
+
logger.debug("parse_interfaces(): nics=#{nics}")
|
|
127
|
+
nics
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def parse_interface(args)
|
|
131
|
+
args.delete_if { |_key,value| value.empty? }
|
|
132
|
+
nic = {}
|
|
133
|
+
id = args['id']
|
|
134
|
+
logger.debug("parse_interface(): id=#{id}")
|
|
135
|
+
delete = args['_delete'].to_i == 1
|
|
136
|
+
if delete
|
|
137
|
+
logger.debug("parse_interface(): delete id=#{id}")
|
|
138
|
+
nic.store(:delete, id)
|
|
139
|
+
nic
|
|
140
|
+
else
|
|
141
|
+
nic.store(:id, id)
|
|
142
|
+
nic.store(:tag, args['vlan'].to_i) if args['vlan']
|
|
143
|
+
nic.store(:model, args['model'].to_s)
|
|
144
|
+
nic.store(:bridge, args['bridge'].to_s) if args['bridge']
|
|
145
|
+
nic.store(:firewall, args['firewall'].to_i) if args['firewall']
|
|
146
|
+
nic.store(:rate, args['rate'].to_i) if args['rate']
|
|
147
|
+
nic.store(:link_down, args['disconnect'].to_i) if args['disconnect']
|
|
148
|
+
nic.store(:queues, args['queues'].to_i) if args['queues']
|
|
149
|
+
logger.debug("parse_interface(): add nic=#{nic}")
|
|
150
|
+
Fog::Proxmox::NicHelper.flatten(nic)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
end
|