vagrant-registration 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,7 @@ module VagrantPlugins
28
28
 
29
29
  name "Registration"
30
30
  description <<-DESC
31
- This plugin adds register and unregister functionality to Vagrant Guests that
31
+ This plugin adds register and unregister functionality to Vagrant Guests that
32
32
  support the capability
33
33
  DESC
34
34
 
@@ -38,12 +38,13 @@ module VagrantPlugins
38
38
  action_hook(:registration_unregister, :machine_action_destroy, &method(:unregister))
39
39
 
40
40
  config(:registration) do
41
+ setup_logging
41
42
  require_relative 'config'
42
43
  Config
43
44
  end
44
45
 
45
46
  # This sets up our log level to be whatever VAGRANT_LOG is
46
- # for loggers prepended with 'vagrant_libvirt'
47
+ # for loggers prepended with 'vagrant_registration'
47
48
  def self.setup_logging
48
49
  require 'log4r'
49
50
  level = nil
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module Registration
3
- VERSION = "0.0.11"
3
+ VERSION = "0.0.12"
4
4
  end
5
5
  end
@@ -0,0 +1,67 @@
1
+ module VagrantPlugins
2
+ module GuestRedHat
3
+ module Cap
4
+ # This provides registration capabilities for vagrant-registration
5
+ #
6
+ # As we might support more registration options (managers), this
7
+ # just calls the capabilities of the selected registration manager
8
+ # (from config.registration.manager).
9
+ class Registration
10
+ # Register the given machine
11
+ def self.registration_register(machine)
12
+ cap = "#{self.registration_manager(machine).to_s}_register".to_sym
13
+ if machine.guest.capability?(cap)
14
+ machine.guest.capability(cap, machine.config.registration)
15
+ else
16
+ false
17
+ end
18
+ end
19
+
20
+ # Unregister the given machine
21
+ def self.registration_unregister(machine)
22
+ cap = "#{self.registration_manager(machine).to_s}_unregister".to_sym
23
+ if machine.guest.capability?(cap)
24
+ machine.guest.capability(cap)
25
+ else
26
+ false
27
+ end
28
+ end
29
+
30
+ # Check that the machine has the selected registration manager installed
31
+ def self.registration_manager_installed(machine)
32
+ cap = "#{self.registration_manager(machine).to_s}".to_sym
33
+ if machine.guest.capability?(cap)
34
+ machine.guest.capability(cap)
35
+ else
36
+ false
37
+ end
38
+ end
39
+
40
+ # Required configuration options of the registration manager
41
+ def self.registration_credentials(machine)
42
+ cap = "#{self.registration_manager(machine).to_s}_credentials".to_sym
43
+ if machine.guest.capability?(cap)
44
+ machine.guest.capability(cap)
45
+ else
46
+ []
47
+ end
48
+ end
49
+
50
+ # Return secret options for the registration manager
51
+ def self.registration_secrets(machine)
52
+ cap = "#{self.registration_manager(machine).to_s}_secrets".to_sym
53
+ if machine.guest.capability?(cap)
54
+ machine.guest.capability(cap)
55
+ else
56
+ []
57
+ end
58
+ end
59
+
60
+ # Return selected registration manager or default
61
+ def self.registration_manager(machine)
62
+ (machine.config.registration.manager || 'subscription_manager').to_sym
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -2,8 +2,55 @@ module VagrantPlugins
2
2
  module GuestRedHat
3
3
  module Cap
4
4
  class SubscriptionManager
5
+ # Test that we have subscription-manager installed
5
6
  def self.subscription_manager(machine)
6
- machine.communicate.test("/sbin/subscription-manager", sudo: true)
7
+ machine.communicate.test("/sbin/subscription-manager", sudo: true)
8
+ end
9
+
10
+ # Register the machine using 'register' option, config is (Open)Struct
11
+ def self.subscription_manager_register(machine, config)
12
+ command = "subscription-manager register #{configuration_to_options(config)}"
13
+ machine.communicate.execute("cmd=$(#{command}); if [ \"$?\" != \"0\" ]; then echo $cmd | grep 'This system is already registered' || (echo $cmd 1>&2 && exit 1) ; fi", sudo: true)
14
+ end
15
+
16
+ # Unregister the machine using 'unregister' option
17
+ def self.subscription_manager_unregister(machine)
18
+ machine.communicate.execute("subscription-manager unregister || :", sudo: true)
19
+ end
20
+
21
+ # Return required configuration options for subscription-manager
22
+ def self.subscription_manager_credentials(machine)
23
+ [:username, :password]
24
+ end
25
+
26
+ # Return secret options for subscription-manager
27
+ def self.subscription_manager_secrets(machine)
28
+ [:password]
29
+ end
30
+
31
+ private
32
+
33
+ # Build additional subscription-manager options based on plugin configuration
34
+ def self.configuration_to_options(config)
35
+ # Defaults
36
+ config.force = true unless config.force
37
+ config.auto_attach = true unless config.auto_attach
38
+
39
+ options = []
40
+ options << "--username=#{config.username}"
41
+ options << "--password=#{config.password}"
42
+ options << "--serverurl=#{config.serverurl}" if config.serverurl
43
+ options << "--baseurl=#{config.baseurl}" if config.baseurl
44
+ options << "--org=#{config.org}" if config.org
45
+ options << "--environment=#{config.environment}" if config.environment
46
+ options << "--name=#{config.name}" if config.name
47
+ options << "--auto-attach" if config.auto_attach
48
+ options << "--activationkey=#{config.activationkey}" if config.activationkey
49
+ options << "--servicelevel=#{config.servicelevel}" if config.servicelevel
50
+ options << "--release=#{config.release}" if config.release
51
+ options << "--force" if config.force
52
+ options << "--type=#{config.type}" if config.type
53
+ options.join(' ')
7
54
  end
8
55
  end
9
56
  end
@@ -3,14 +3,34 @@ require "vagrant"
3
3
  module VagrantPlugins
4
4
  module GuestRedHat
5
5
  class Plugin < Vagrant.plugin("2")
6
- guest_capability("redhat", "register") do
7
- require_relative "cap/register"
8
- Cap::Register
6
+ guest_capability("redhat", "registration_register") do
7
+ require_relative "cap/registration"
8
+ Cap::Registration
9
9
  end
10
10
 
11
- guest_capability("redhat", "unregister") do
12
- require_relative "cap/unregister"
13
- Cap::Unregister
11
+ guest_capability("redhat", "registration_unregister") do
12
+ require_relative "cap/registration"
13
+ Cap::Registration
14
+ end
15
+
16
+ guest_capability("redhat", "registration_manager_installed") do
17
+ require_relative "cap/registration"
18
+ Cap::Registration
19
+ end
20
+
21
+ guest_capability("redhat", "registration_credentials") do
22
+ require_relative "cap/registration"
23
+ Cap::Registration
24
+ end
25
+
26
+ guest_capability("redhat", "registration_secrets") do
27
+ require_relative "cap/registration"
28
+ Cap::Registration
29
+ end
30
+
31
+ guest_capability("redhat", "registration_manager") do
32
+ require_relative "cap/registration"
33
+ Cap::Registration
14
34
  end
15
35
 
16
36
  guest_capability("redhat", "subscription_manager") do
@@ -18,6 +38,26 @@ module VagrantPlugins
18
38
  Cap::SubscriptionManager
19
39
  end
20
40
 
41
+ guest_capability("redhat", "subscription_manager_register") do
42
+ require_relative "cap/subscription_manager"
43
+ Cap::SubscriptionManager
44
+ end
45
+
46
+ guest_capability("redhat", "subscription_manager_unregister") do
47
+ require_relative "cap/subscription_manager"
48
+ Cap::SubscriptionManager
49
+ end
50
+
51
+ guest_capability("redhat", "subscription_manager_credentials") do
52
+ require_relative "cap/subscription_manager"
53
+ Cap::SubscriptionManager
54
+ end
55
+
56
+ guest_capability("redhat", "subscription_manager_secrets") do
57
+ require_relative "cap/subscription_manager"
58
+ Cap::SubscriptionManager
59
+ end
60
+
21
61
  guest_capability("redhat", "rhcert") do
22
62
  require_relative "cap/redhat_certification_tool"
23
63
  Cap::RedHatCertification
data/tests/helpers.sh ADDED
@@ -0,0 +1,124 @@
1
+ # Set up test environment
2
+ function setup_tests() {
3
+ check_credentials
4
+ install_dependencies
5
+
6
+ # Test results that should be printed at the end
7
+ TEST_RESULTS=''
8
+ FAILED=0
9
+ SUCCEDED=0
10
+ EXIT_CODE=0
11
+ }
12
+
13
+ # Print test results
14
+ function print_results() {
15
+ if [ "$TEST_RESULTS" != "" ]; then
16
+ printf "\n$TEST_RESULTS\n"
17
+ fi
18
+ printf "\n$SUCCEDED succeded, $FAILED failed.\n"
19
+ exit $EXIT_CODE
20
+ }
21
+
22
+ # Clean up before each test
23
+ function clean_up() {
24
+ # Clean up Vagrant metadata
25
+ rm -rf $DIR/.vagrant
26
+ }
27
+
28
+ # Check that we have credentials to run the test suite
29
+ function check_credentials() {
30
+ if [ "$VAGRANT_REGISTRATION_USERNAME" = "" ] || [ "$VAGRANT_REGISTRATION_PASSWORD" = "" ]; then
31
+ echo "VAGRANT_REGISTRATION_USERNAME and VAGRANT_REGISTRATION_PASSWORD needs to be provided."
32
+ exit 1
33
+ fi
34
+ }
35
+
36
+ # Install vagrant and vagrant-registration
37
+ function install_dependencies() {
38
+ # Install Vagrant if it's not present
39
+ PLUGIN_INSTALLED=$(vagrant --help)
40
+ if [ $? -ne 0 ]; then
41
+ sudo yum install vagrant-libvirt -y
42
+ fi
43
+
44
+ # Uninstall vagrant-registration if installed
45
+ # TODO: Uninstall RPM package if needed
46
+ PLUGIN_INSTALLED=$(vagrant plugin list | grep vagrant-registration)
47
+ if [ -z "$PLUGIN_INSTALLED" ]; then
48
+ vagrant plugin uninstall vagrant-registration
49
+ fi
50
+
51
+ # Install vagrant-registration from current sources
52
+ rm -rf pkg
53
+ rake build
54
+ vagrant plugin install pkg/vagrant-registration*.gem
55
+ }
56
+
57
+ # Test that command succeded
58
+ #
59
+ # Usage:
60
+ #
61
+ # test_success TEST_NAME COMMAND_TO_RUN
62
+ #
63
+ # Example:
64
+ #
65
+ # test_success "ls won't fail" "ls -all"
66
+ function test_success() {
67
+ eval $2 >&1 >/dev/null
68
+ if [ $? -ne 0 ]; then
69
+ printf "F"
70
+ FAILED=$((FAILED + 1))
71
+ EXIT_CODE=1
72
+ TEST_RESULTS="$TEST_RESULTS\nTest '$1' failed with command:"
73
+ TEST_RESULTS="$TEST_RESULTS\n $2"
74
+ else
75
+ SUCCEDED=$((SUCCEDED + 1))
76
+ printf '.'
77
+ fi
78
+ }
79
+
80
+ # Test that command failed
81
+ #
82
+ # Usage:
83
+ #
84
+ # test_failure TEST_NAME COMMAND_TO_RUN
85
+ #
86
+ # Example:
87
+ #
88
+ # test_failure "this should fail" "echoo"
89
+ function test_failure() {
90
+ eval $2 >&1 >/dev/null
91
+ if [ $? -ne 0 ]; then
92
+ SUCCEDED=$((SUCCEDED + 1))
93
+ printf '.'
94
+ else
95
+ printf "F"
96
+ FAILED=$((FAILED + 1))
97
+ EXIT_CODE=1
98
+ TEST_RESULTS="$TEST_RESULTS\nTest '$1' dit not fail with command:"
99
+ TEST_RESULTS="$TEST_RESULTS\n $2"
100
+ fi
101
+ }
102
+
103
+ # Test that command produced an expected output
104
+ #
105
+ # Usage:
106
+ #
107
+ # test_output TEST_NAME COMMAND_TO_RUN EXPECTED_OUTPUT
108
+ #
109
+ # Example:
110
+ #
111
+ # test_output "echo abc outputs abc" "echo 'abc'" "abc"
112
+ function test_output() {
113
+ eval $2 >&1| grep "$3" >/dev/null
114
+ if [ $? -ne 0 ]; then
115
+ printf "F"
116
+ FAILED=$((FAILED + 1))
117
+ EXIT_CODE=1
118
+ TEST_RESULTS="$TEST_RESULTS\nTest '$1' failed with command:"
119
+ TEST_RESULTS="$TEST_RESULTS\n $2 | grep '$3'"
120
+ else
121
+ SUCCEDED=$((SUCCEDED + 1))
122
+ printf '.'
123
+ fi
124
+ }
data/tests/run.sh ADDED
@@ -0,0 +1,53 @@
1
+ #!/bin/bash
2
+
3
+ # This tests test vagrant-registration plugin running on Fedora
4
+ # with libvirt. If you do not have Vagrant installed or if you
5
+ # have vagrant-libvirt package on your system, Vagrant will be
6
+ # installed or vagrant-libvirt removed respectively. In that case
7
+ # you need sudo to run the tests. If you do not run Fedora, make
8
+ # sure you have Vagrant installed (any provider should do if you
9
+ # add RHEL box called 'rhel-7').
10
+ #
11
+ # IMPORTANT: Tests need valid credentials to actually test
12
+ # registration. This can be provided in form of environment
13
+ # variables.
14
+ #
15
+ # For testing subscription-manager on RHEL export
16
+ # VAGRANT_REGISTRATION_USERNAME and
17
+ # VAGRANT_REGISTRATION_PASSWORD.
18
+ #
19
+ # NOTE: This will install a development version of
20
+ # vagrant-registration on your system.
21
+ #
22
+
23
+ DIR=$(dirname $(readlink -f "$0"))
24
+
25
+ # Import test helpers
26
+ . $DIR/helpers.sh
27
+
28
+ setup_tests
29
+
30
+ # Test correct credentials
31
+ clean_up
32
+ export VAGRANT_VAGRANTFILE=$DIR/vagrantfiles/Vagrantfile.rhel_multi_machine
33
+
34
+ test_success 'vagrant up on RHEL multi_machine setup' 'vagrant up'
35
+
36
+ test_output 'first machine is registered' \
37
+ 'vagrant ssh rhel1-valid-credentials -c '\''sudo subscription-manager register'\''' \
38
+ 'This system is already registered.'
39
+
40
+ test_output 'second machine is registered' \
41
+ 'vagrant ssh rhel2-valid-credentials -c '\''sudo subscription-manager register'\''' \
42
+ 'This system is already registered.'
43
+
44
+ test_success 'vagrant halt on RHEL multi_machine setup' 'vagrant halt rhel1-valid-credentials'
45
+ test_success 'vagrant halt on RHEL multi_machine setup' 'vagrant destroy'
46
+
47
+ # Test wrong credentials
48
+ clean_up
49
+ export VAGRANT_VAGRANTFILE=$DIR/vagrantfiles/Vagrantfile.rhel_wrong_credentials
50
+ test_failure 'vagrant up on RHEL with wrong credentials should fail' 'vagrant up'
51
+ test_success 'vagrant destroy on RHEL with wrong credentials' 'vagrant destroy'
52
+
53
+ print_results
@@ -0,0 +1,19 @@
1
+ # Spin 2 RHEL machines that will be registered
2
+
3
+ ENV['VAGRANT_DEFAULT_PROVIDER'] ||= 'libvirt'
4
+ ENV['VAGRANT_REGISTRATION_RHEL_BOX'] ||= 'rhel-7.0'
5
+
6
+ Vagrant.configure('2') do |config|
7
+ config.vm.box = ENV['VAGRANT_REGISTRATION_RHEL_BOX']
8
+
9
+ config.vm.define "rhel1-valid-credentials" do |c|
10
+ c.registration.username = ENV['VAGRANT_REGISTRATION_USERNAME']
11
+ c.registration.password = ENV['VAGRANT_REGISTRATION_PASSWORD']
12
+ end
13
+
14
+ config.vm.define "rhel2-valid-credentials" do |c|
15
+ c.registration.username = ENV['VAGRANT_REGISTRATION_USERNAME']
16
+ c.registration.password = ENV['VAGRANT_REGISTRATION_PASSWORD']
17
+ end
18
+
19
+ end
@@ -0,0 +1,14 @@
1
+ # Spin 1 RHEL machine with wrong credentials
2
+
3
+ ENV['VAGRANT_DEFAULT_PROVIDER'] ||= 'libvirt'
4
+ ENV['VAGRANT_REGISTRATION_RHEL_BOX'] ||= 'rhel-7.0'
5
+
6
+ Vagrant.configure('2') do |config|
7
+ config.vm.box = ENV['VAGRANT_REGISTRATION_RHEL_BOX']
8
+
9
+ config.vm.define "rhel1-wrong-credentials" do |c|
10
+ c.registration.username = 'wrong_username'
11
+ c.registration.password = 'wrong_password'
12
+ end
13
+
14
+ end
@@ -46,5 +46,6 @@ Gem::Specification.new do |s|
46
46
  s.files = unignored_files
47
47
  s.executables = unignored_files.map { |f| f[/^bin\/(.*)/, 1] }.compact
48
48
  s.require_path = 'lib'
49
- end
50
49
 
50
+ s.add_development_dependency 'yard'
51
+ end