foreman_kubevirt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +619 -0
  3. data/README.md +156 -0
  4. data/Rakefile +44 -0
  5. data/app/assets/javascripts/foreman_kubevirt/kubevirt.js +31 -0
  6. data/app/assets/javascripts/foreman_kubevirt/nic_info.js +9 -0
  7. data/app/controllers/foreman_kubevirt/concerns/api/compute_resources_controller_extensions.rb +26 -0
  8. data/app/models/concerns/fog_extensions/kubevirt/network.rb +11 -0
  9. data/app/models/concerns/fog_extensions/kubevirt/server.rb +55 -0
  10. data/app/models/concerns/fog_extensions/kubevirt/vmnic.rb +12 -0
  11. data/app/models/concerns/fog_extensions/kubevirt/volume.rb +26 -0
  12. data/app/models/foreman_kubevirt/kubevirt.rb +370 -0
  13. data/app/views/api/v2/compute_resources/kubevirt.json.rabl +1 -0
  14. data/app/views/compute_resources/form/_kubevirt.html.erb +14 -0
  15. data/app/views/compute_resources/show/_kubevirt.html.erb +14 -0
  16. data/app/views/compute_resources_vms/form/kubevirt/_base.html.erb +27 -0
  17. data/app/views/compute_resources_vms/form/kubevirt/_network.html.erb +15 -0
  18. data/app/views/compute_resources_vms/form/kubevirt/_volume.html.erb +21 -0
  19. data/app/views/compute_resources_vms/index/_kubevirt.html.erb +29 -0
  20. data/app/views/compute_resources_vms/show/_kubevirt.html.erb +55 -0
  21. data/app/views/images/form/_kubevirt.html.erb +4 -0
  22. data/config/routes.rb +2 -0
  23. data/lib/foreman_kubevirt/engine.rb +75 -0
  24. data/lib/foreman_kubevirt/version.rb +3 -0
  25. data/lib/foreman_kubevirt.rb +4 -0
  26. data/lib/tasks/foreman_kubevirt_tasks.rake +47 -0
  27. data/locale/Makefile +60 -0
  28. data/locale/en/foreman_kubevirt.po +19 -0
  29. data/locale/foreman_kubevirt.pot +19 -0
  30. data/locale/gemspec.rb +2 -0
  31. data/test/factories/compute_resource_factories.rb +10 -0
  32. data/test/factories/host_factories.rb +49 -0
  33. data/test/factories/nic_factories.rb +31 -0
  34. data/test/models/compute_resources/kubevirt_test.rb +69 -0
  35. data/test/test_plugin_helper.rb +7 -0
  36. data/test/unit/foreman_kubevirt_test.rb +45 -0
  37. metadata +127 -0
@@ -0,0 +1,47 @@
1
+ require 'rake/testtask'
2
+
3
+ # Tasks
4
+ namespace :foreman_kubevirt do
5
+ namespace :example do
6
+ desc 'Example Task'
7
+ task task: :environment do
8
+ # Task goes here
9
+ end
10
+ end
11
+ end
12
+
13
+ # Tests
14
+ namespace :test do
15
+ desc 'Test ForemanKubevirt'
16
+ Rake::TestTask.new(:foreman_kubevirt) do |t|
17
+ test_dir = File.join(File.dirname(__FILE__), '../..', 'test')
18
+ t.libs << ['test', test_dir]
19
+ t.pattern = "#{test_dir}/**/*_test.rb"
20
+ t.verbose = true
21
+ t.warning = false
22
+ end
23
+ end
24
+
25
+ namespace :foreman_kubevirt do
26
+ task :rubocop do
27
+ begin
28
+ require 'rubocop/rake_task'
29
+ RuboCop::RakeTask.new(:rubocop_foreman_kubevirt) do |task|
30
+ task.patterns = ["#{ForemanKubevirt::Engine.root}/app/**/*.rb",
31
+ "#{ForemanKubevirt::Engine.root}/lib/**/*.rb",
32
+ "#{ForemanKubevirt::Engine.root}/test/**/*.rb"]
33
+ end
34
+ rescue StandardError
35
+ puts 'Rubocop not loaded.'
36
+ end
37
+
38
+ Rake::Task['rubocop_foreman_kubevirt'].invoke
39
+ end
40
+ end
41
+
42
+ Rake::Task[:test].enhance ['test:foreman_kubevirt']
43
+
44
+ load 'tasks/jenkins.rake'
45
+ if Rake::Task.task_defined?(:'jenkins:unit')
46
+ Rake::Task['jenkins:unit'].enhance ['test:foreman_kubevirt', 'foreman_kubevirt:rubocop']
47
+ end
data/locale/Makefile ADDED
@@ -0,0 +1,60 @@
1
+ #
2
+ # Makefile for PO merging and MO generation. More info in the README.
3
+ #
4
+ # make all-mo (default) - generate MO files
5
+ # make check - check translations using translate-tool
6
+ # make tx-update - download and merge translations from Transifex
7
+ # make clean - clean everything
8
+ #
9
+ DOMAIN = foreman_kubevirt
10
+ VERSION = $(shell ruby -e 'require "rubygems";spec = Gem::Specification::load(Dir.glob("../*.gemspec")[0]);puts spec.version')
11
+ POTFILE = $(DOMAIN).pot
12
+ MOFILE = $(DOMAIN).mo
13
+ POFILES = $(shell find . -name '$(DOMAIN).po')
14
+ MOFILES = $(patsubst %.po,%.mo,$(POFILES))
15
+ POXFILES = $(patsubst %.po,%.pox,$(POFILES))
16
+ EDITFILES = $(patsubst %.po,%.edit.po,$(POFILES))
17
+
18
+ %.mo: %.po
19
+ mkdir -p $(shell dirname $@)/LC_MESSAGES
20
+ msgfmt -o $(shell dirname $@)/LC_MESSAGES/$(MOFILE) $<
21
+
22
+ # Generate MO files from PO files
23
+ all-mo: $(MOFILES)
24
+
25
+ # Check for malformed strings
26
+ %.pox: %.po
27
+ msgfmt -c $<
28
+ pofilter --nofuzzy -t variables -t blank -t urls -t emails -t long -t newlines \
29
+ -t endwhitespace -t endpunc -t puncspacing -t options -t printf -t validchars --gnome $< > $@
30
+ cat $@
31
+ ! grep -q msgid $@
32
+
33
+ %.edit.po:
34
+ touch $@
35
+
36
+ check: $(POXFILES)
37
+
38
+ # Unify duplicate translations
39
+ uniq-po:
40
+ for f in $(shell find ./ -name "*.po") ; do \
41
+ msguniq $$f -o $$f ; \
42
+ done
43
+
44
+ tx-pull: $(EDITFILES)
45
+ tx pull -f
46
+ for f in $(EDITFILES) ; do \
47
+ sed -i 's/^\("Project-Id-Version: \).*$$/\1$(DOMAIN) $(VERSION)\\n"/' $$f; \
48
+ done
49
+
50
+ tx-update: tx-pull
51
+ @echo
52
+ @echo Run rake plugin:gettext[$(DOMAIN)] from the Foreman installation, then make -C locale mo-files to finish
53
+ @echo
54
+
55
+ mo-files: $(MOFILES)
56
+ git add $(POFILES) $(POTFILE) ../locale/*/LC_MESSAGES
57
+ git commit -m "i18n - pulling from tx"
58
+ @echo
59
+ @echo Changes commited!
60
+ @echo
@@ -0,0 +1,19 @@
1
+ # foreman_kubevirt
2
+ #
3
+ # This file is distributed under the same license as foreman_kubevirt.
4
+ #
5
+ #, fuzzy
6
+ msgid ""
7
+ msgstr ""
8
+ "Project-Id-Version: version 0.0.1\n"
9
+ "Report-Msgid-Bugs-To: \n"
10
+ "POT-Creation-Date: 2014-08-20 08:46+0100\n"
11
+ "PO-Revision-Date: 2014-08-20 08:54+0100\n"
12
+ "Last-Translator: Foreman Team <foreman-dev@googlegroups.com>\n"
13
+ "Language-Team: Foreman Team <foreman-dev@googlegroups.com>\n"
14
+ "Language: \n"
15
+ "MIME-Version: 1.0\n"
16
+ "Content-Type: text/plain; charset=UTF-8\n"
17
+ "Content-Transfer-Encoding: 8bit\n"
18
+ "Plural-Forms: nplurals=2; plural=(n != 1);\n"
19
+
@@ -0,0 +1,19 @@
1
+ # foreman_kubevirt
2
+ #
3
+ # This file is distributed under the same license as foreman_kubevirt.
4
+ #
5
+ #, fuzzy
6
+ msgid ""
7
+ msgstr ""
8
+ "Project-Id-Version: version 0.0.1\n"
9
+ "Report-Msgid-Bugs-To: \n"
10
+ "POT-Creation-Date: 2014-08-20 08:46+0100\n"
11
+ "PO-Revision-Date: 2014-08-20 08:46+0100\n"
12
+ "Last-Translator: Foreman Team <foreman-dev@googlegroups.com>\n"
13
+ "Language-Team: Foreman Team <foreman-dev@googlegroups.com>\n"
14
+ "Language: \n"
15
+ "MIME-Version: 1.0\n"
16
+ "Content-Type: text/plain; charset=UTF-8\n"
17
+ "Content-Transfer-Encoding: 8bit\n"
18
+ "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
19
+
data/locale/gemspec.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Matches foreman_kubevirt.gemspec
2
+ _('KubeVirt as a compute resource for Foreman')
@@ -0,0 +1,10 @@
1
+ FactoryBot.define do
2
+ factory :compute_resource_kubevirt, class: 'ForemanKubevirt::Kubevirt' do
3
+ provider "Kubevirt"
4
+ name 'kubevirt-multus'
5
+ hostname "192.168.111.13"
6
+ api_port "6443"
7
+ namespace "default"
8
+ token "kubetoken"
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ FactoryBot.define do
2
+ factory :host_kubevirt, class: 'Host::Managed' do
3
+ name "susie-baynham.example.com"
4
+ last_compile nil
5
+ last_report nil
6
+ updated_at nil
7
+ root_pass "$5$pzp5IoYXH9WQT6Ks$fk1OzfJAK1"
8
+ architecture_id 1
9
+ operatingsystem_id 3
10
+ environment_id 3
11
+ ptable_id 91
12
+ medium_id 13
13
+ build true
14
+ comment ""
15
+ disk ""
16
+ installed_at nil
17
+ model_id nil
18
+ hostgroup_id 31
19
+ owner_id 4
20
+ owner_type "User"
21
+ enabled true
22
+ puppet_ca_proxy_id nil
23
+ managed true
24
+ use_image nil
25
+ image_file nil
26
+ uuid nil
27
+ compute_resource_id 7
28
+ puppet_proxy_id 1
29
+ certname nil
30
+ image_id nil
31
+ organization_id 2
32
+ location_id 1
33
+ type "Host::Managed"
34
+ otp nil
35
+ realm_id nil
36
+ compute_profile_id nil
37
+ provision_method "build"
38
+ grub_pass "$6$aRZAzZm1TOa5WAGX$.Cwxy5zCrjVQqHUE/0Ic9oQ"
39
+ global_status 0
40
+ lookup_value_matcher "fqdn=susie-baynham.example.com"
41
+ pxe_loader "PXELinux BIOS"
42
+ initiated_at nil
43
+ build_errors nil
44
+
45
+ trait :with_interfaces do
46
+ interfaces { build_list :nic_kubevirt, 1 }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,31 @@
1
+ FactoryBot.define do
2
+ factory :nic_kubevirt, class: 'Nic::Managed' do
3
+ mac "a2:b4:a2:b2:a2:a8"
4
+ ip "192.168.111.200"
5
+ type "Nic::Managed"
6
+ name "elton-kniola.example.com"
7
+ host_id 1
8
+ subnet_id 2
9
+ domain_id 1
10
+ attrs {}
11
+ created_at nil
12
+ updated_at nil
13
+ provider nil
14
+ username nil
15
+ password nil
16
+ virtual false
17
+ link true
18
+ identifier ""
19
+ tag ""
20
+ attached_to ""
21
+ managed true
22
+ mode "balance-rr"
23
+ attached_devices ""
24
+ bond_options ""
25
+ primary true
26
+ provision true
27
+ compute_attributes { { "cni_provider" => "multus", "network" => "ovs-foreman" } }
28
+ ip6 ""
29
+ subnet6_id nil
30
+ end
31
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class ForemanKubevirtTest < ActiveSupport::TestCase
4
+ setup do
5
+ User.current = User.find_by login: 'admin'
6
+ end
7
+
8
+ def new_kubevirt_vcr
9
+ ::FactoryBot.build(:compute_resource_kubevirt)
10
+ end
11
+
12
+ test "host_interfaces_attrs" do
13
+ record = new_kubevirt_vcr
14
+ host = ::FactoryBot.build(:host_kubevirt, :with_interfaces)
15
+ result = record.host_interfaces_attrs(host)
16
+ expected_res = { "0" => { "cni_provider" => "multus",
17
+ "network" => "ovs-foreman",
18
+ :ip => "192.168.111.200",
19
+ :mac => "a2:b4:a2:b2:a2:a8",
20
+ :provision => true } }
21
+ assert_equal expected_res, result
22
+ end
23
+
24
+ describe "networks" do
25
+ test "returns list of networksattachmentdefs" do
26
+ Fog.mock!
27
+ compute_resource = FactoryBot.build(:compute_resource_kubevirt)
28
+ res = compute_resource.networks
29
+ assert_equal 1, res.count
30
+ assert_equal '0e35b868-2464-11e9-93b4-525400c5a686', res.first.uid
31
+ end
32
+
33
+ test "in case of exception, returns an empty array" do
34
+ compute_resource = FactoryBot.build(:compute_resource_kubevirt)
35
+ client = stub
36
+ compute_resource.stubs(:client).returns(client)
37
+ client.stubs(:networkattachmentdefs).raises("exception")
38
+ res = compute_resource.networks
39
+ assert_equal 0, res.count
40
+ end
41
+ end
42
+
43
+ describe "find_vm_by_uuid" do
44
+ test "it finds the vm" do
45
+ Fog.mock!
46
+ compute_resource = FactoryBot.build(:compute_resource_kubevirt)
47
+ res = compute_resource.find_vm_by_uuid("robin-rykert.example.com")
48
+ assert_equal "robin-rykert.example.com", res.name
49
+ assert_equal "default", res.namespace
50
+ end
51
+
52
+ test "it raises RecordNotFound when it fails" do
53
+ Fog.mock!
54
+ compute_resource = FactoryBot.build(:compute_resource_kubevirt)
55
+ assert_raises ActiveRecord::RecordNotFound do
56
+ compute_resource.find_vm_by_uuid("not_found.example.com")
57
+ end
58
+ end
59
+ end
60
+
61
+ test "test_connection should fail if client does not support virt" do
62
+ client = stub
63
+ client.stubs(:virt_supported?).returns(false)
64
+ client.stubs(:valid?).returns(true)
65
+ record = new_kubevirt_vcr
66
+ record.stubs(:client).returns(client)
67
+ assert_equal false, record.test_connection
68
+ end
69
+ end
@@ -0,0 +1,7 @@
1
+ # This calls the main test_helper in Foreman-core
2
+ require 'test_helper'
3
+ require 'kubeclient'
4
+
5
+ # Add plugin to FactoryBot's paths
6
+ FactoryBot.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
7
+ FactoryBot.reload
@@ -0,0 +1,45 @@
1
+ require 'test_plugin_helper'
2
+
3
+ class ForemanKubevirtTest < ActiveSupport::TestCase
4
+ setup do
5
+ User.current = User.find_by login: 'admin'
6
+ end
7
+
8
+ def new_kubevirt_vcr
9
+ ::FactoryBot.build(:compute_resource_kubevirt)
10
+ ComputeResource.new_provider(
11
+ :provider => "Kubevirt",
12
+ :name => 'kubevirt-multus',
13
+ :hostname => "192.168.111.13",
14
+ :api_port => "6443",
15
+ :namespace => "default",
16
+ :token => "kubetoken"
17
+ )
18
+ end
19
+
20
+ require 'kubeclient'
21
+
22
+ test "create_vm network based" do
23
+ vm_args = { "cpu_cores" => "1", "memory" => "1073741824", "start" => "1", "volumes_attributes" => { "1554559479978" => { "_delete" => "", "storage_class" => "local-storage", "capacity" => "1", "bootable" => "true" }, "1554559483803" => { "_delete" => "", "storage_class" => "local-storage", "capacity" => "2" } }, "name" => "robin-rykert.example.com", "provision_method" => "build", "firmware_type" => :bios, "interfaces_attributes" => { "0" => { "cni_provider" => "multus", "network" => "ovs-foreman", "ip" => "192.168.111.193", "mac" => "a2:b4:a2:b6:a2:a8", "provision" => true } } }
24
+ Fog.mock!
25
+ compute_resource = new_kubevirt_vcr
26
+ server = compute_resource.create_vm(vm_args)
27
+
28
+ assert_equal "robin-rykert.example.com", server.name
29
+ assert_equal 2, server.volumes.count
30
+ assert_equal 2, server.disks.count
31
+ assert_equal 1, server.interfaces.count
32
+ end
33
+
34
+ test "create_vm image based" do
35
+ vm_args = { "cpu_cores" => "1", "memory" => "1073741824", "start" => "1", "volumes_attributes" => { "1554649143334" => { "_delete" => "", "storage_class" => "local-storage", "capacity" => "1", "bootable" => "true" } }, "image_id" => "kubevirt/fedora-cloud-registry-disk-demo", "name" => "olive-kempter.example.com", "provision_method" => "image", "firmware_type" => :bios, "interfaces_attributes" => { "0" => { "cni_provider" => "multus", "network" => "ovs-foreman", "ip" => "192.168.111.184", "mac" => "a2:a4:a2:b2:a2:b6", "provision" => true } } }
36
+ Fog.mock!
37
+ compute_resource = new_kubevirt_vcr
38
+ server = compute_resource.create_vm(vm_args)
39
+
40
+ assert_equal "olive-kempter.example.com", server.name
41
+ assert_equal 2, server.volumes.count
42
+ assert_equal 2, server.disks.count
43
+ assert_equal 1, server.interfaces.count
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foreman_kubevirt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Moti Asayag
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '12.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.64'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.64'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fog-kubevirt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ description: Provision and manage Kubevirt Virtual Machines from Foreman.
56
+ email:
57
+ - masayag@redhat.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/assets/javascripts/foreman_kubevirt/kubevirt.js
66
+ - app/assets/javascripts/foreman_kubevirt/nic_info.js
67
+ - app/controllers/foreman_kubevirt/concerns/api/compute_resources_controller_extensions.rb
68
+ - app/models/concerns/fog_extensions/kubevirt/network.rb
69
+ - app/models/concerns/fog_extensions/kubevirt/server.rb
70
+ - app/models/concerns/fog_extensions/kubevirt/vmnic.rb
71
+ - app/models/concerns/fog_extensions/kubevirt/volume.rb
72
+ - app/models/foreman_kubevirt/kubevirt.rb
73
+ - app/views/api/v2/compute_resources/kubevirt.json.rabl
74
+ - app/views/compute_resources/form/_kubevirt.html.erb
75
+ - app/views/compute_resources/show/_kubevirt.html.erb
76
+ - app/views/compute_resources_vms/form/kubevirt/_base.html.erb
77
+ - app/views/compute_resources_vms/form/kubevirt/_network.html.erb
78
+ - app/views/compute_resources_vms/form/kubevirt/_volume.html.erb
79
+ - app/views/compute_resources_vms/index/_kubevirt.html.erb
80
+ - app/views/compute_resources_vms/show/_kubevirt.html.erb
81
+ - app/views/images/form/_kubevirt.html.erb
82
+ - config/routes.rb
83
+ - lib/foreman_kubevirt.rb
84
+ - lib/foreman_kubevirt/engine.rb
85
+ - lib/foreman_kubevirt/version.rb
86
+ - lib/tasks/foreman_kubevirt_tasks.rake
87
+ - locale/Makefile
88
+ - locale/en/foreman_kubevirt.po
89
+ - locale/foreman_kubevirt.pot
90
+ - locale/gemspec.rb
91
+ - test/factories/compute_resource_factories.rb
92
+ - test/factories/host_factories.rb
93
+ - test/factories/nic_factories.rb
94
+ - test/models/compute_resources/kubevirt_test.rb
95
+ - test/test_plugin_helper.rb
96
+ - test/unit/foreman_kubevirt_test.rb
97
+ homepage: https://github.com/masayag/foreman_kubevirt
98
+ licenses:
99
+ - GPL-3.0
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.14
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Provision and manage Kubevirt Virtual Machines from Foreman
121
+ test_files:
122
+ - test/factories/nic_factories.rb
123
+ - test/factories/compute_resource_factories.rb
124
+ - test/factories/host_factories.rb
125
+ - test/test_plugin_helper.rb
126
+ - test/models/compute_resources/kubevirt_test.rb
127
+ - test/unit/foreman_kubevirt_test.rb