Class: Bebox::RoleWizard

Inherits:
Object
  • Object
show all
Includes:
Logger, WizardsHelper
Defined in:
lib/bebox/wizards/role_wizard.rb

Instance Method Summary collapse

Methods included from WizardsHelper

#choose_option, #confirm_action?, #valid_puppet_class_name?, #write_input

Methods included from Logger

#error, #highline_quest, #highline_warn, included, #info, #linebreak, #msg, #ok, #quest, #title, #warn

Instance Method Details

#add_profile(project_root) ⇒ Object

Add a profile to a role



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bebox/wizards/role_wizard.rb', line 46

def add_profile(project_root)
  roles = Bebox::Role.list(project_root)
  profiles = Bebox::Profile.list(project_root)
  role = choose_option(roles, 'Choose an existing role:')
  profile = choose_option(profiles, 'Choose the profile to add:')
  if Bebox::Role.profile_in_role?(project_root, role, profile)
    warn "Profile '#{profile}' already in the Role '#{role}'. No changes were made."
    output = false
  else
    output = Bebox::Role.add_profile(project_root, role, profile)
    ok "Profile '#{profile}' added to Role '#{role}'."
  end
  return output
end

#create_new_role(project_root, role_name) ⇒ Object

Create a new role



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bebox/wizards/role_wizard.rb', line 8

def create_new_role(project_root, role_name)
  # Check if the role name is valid
  return error "The role name can only contain:\n
  \n* Lowercase letters
  \n* Numbers
  \n* Underscores
  \n* Must begin with an Lowercase letter
  \n* Can not be any of: #{Bebox::RESERVED_WORDS.join(', ')}
  \n\nNo changes were made." unless valid_puppet_class_name?(role_name)
  # Check if the role exist
  return error("The '#{role_name}' role already exist. No changes were made.") if role_exists?(project_root, role_name)
  # Role creation
  role = Bebox::Role.new(role_name, project_root)
  output = role.create
  ok 'Role created!.'
  return output
end

#remove_profile(project_root) ⇒ Object

Remove a profile in a role



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bebox/wizards/role_wizard.rb', line 62

def remove_profile(project_root)
  roles = Bebox::Role.list(project_root)
  profiles = Bebox::Profile.list(project_root)
  role = choose_option(roles, 'Choose an existing role:')
  profile = choose_option(profiles, 'Choose the profile to remove:')
  if Bebox::Role.profile_in_role?(project_root, role, profile)
    output = Bebox::Role.remove_profile(project_root, role, profile)
    ok "Profile '#{profile}' removed from Role '#{role}'."
  else
    warn "Profile '#{profile}' is not in the Role '#{role}'. No changes were made."
    output = false
  end
  return output
end

#remove_role(project_root) ⇒ Object

Removes an existing role



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bebox/wizards/role_wizard.rb', line 27

def remove_role(project_root)
  # Choose a role from the availables
  roles = Bebox::Role.list(project_root)
  # Get a role if exist.
  if roles.count > 0
    role_name = choose_option(roles, 'Choose the role to remove:')
  else
    return error "There are no roles to remove. No changes were made."
  end
  # Ask for deletion confirmation
  return warn('No changes were made.') unless confirm_action?('Are you sure that you want to delete the role?')
  # Role deletion
  role = Bebox::Role.new(role_name, project_root)
  output = role.remove
  ok 'Role removed!.'
  return output
end

#role_exists?(project_root, role_name) ⇒ Boolean

Check if there’s an existing role in the project

Returns:

  • (Boolean)


78
79
80
# File 'lib/bebox/wizards/role_wizard.rb', line 78

def role_exists?(project_root, role_name)
  Dir.exists?("#{project_root}/puppet/roles/#{role_name}")
end