Class: Bebox::ProjectWizard

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

Constant Summary collapse

BEBOX_BOXES_PATH =

Bebox boxes directory

'~/.bebox/boxes'

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

#ask_uriObject

Asks vagrant box location to user until is valid



58
59
60
61
62
# File 'lib/bebox/wizards/project_wizard.rb', line 58

def ask_uri
  vbox_uri = write_input('Write the URI (http, local_path) for the vagrant box to be used in the project:', 'http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-nocm.box')
  # If valid return uri if not keep asking for uri
  uri_valid?(vbox_uri) ? (return vbox_uri) : ask_uri
end

#bebox_boxes_setupObject

Setup the bebox boxes directory



50
51
52
53
54
55
# File 'lib/bebox/wizards/project_wizard.rb', line 50

def bebox_boxes_setup
  # Create user project directories
  `mkdir -p #{BEBOX_BOXES_PATH}/tmp`
  # Clear partial downloaded boxes
  `rm -f #{BEBOX_BOXES_PATH}/tmp/*`
end

#box_exists?(valid_box_uri) ⇒ Boolean

Check if a box with the same name already exist

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/bebox/wizards/project_wizard.rb', line 96

def box_exists?(valid_box_uri)
  box_name = valid_box_uri.split('/').last
  boxes = get_existing_boxes
  boxes.any? { |val| /#{box_name}/ =~ val }
end

#choose_box(boxes) ⇒ Object

Asks to choose an existing box in the bebox boxes directory



112
113
114
115
116
117
118
# File 'lib/bebox/wizards/project_wizard.rb', line 112

def choose_box(boxes)
  # Menu to choose vagrant box provider
  other_box_message = 'Download/Select a new box'
  boxes << other_box_message
  current_box = choose_option(boxes, 'Choose an existing box or download/select a new box')
  current_box = (current_box == other_box_message) ? nil : current_box
end

#create_new_project(project_name) ⇒ Object

Asks for the project parameters and create the project skeleton



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bebox/wizards/project_wizard.rb', line 10

def create_new_project(project_name)
  # Check project existence
  (error('Project not created. There is already a project with that name in the current directory.'); return false) if project_exists?(Dir.pwd, project_name)
  # Setup the bebox boxes directory
  bebox_boxes_setup
  # Asks to choose an existing box
  current_box = choose_box(get_existing_boxes)
  vagrant_box_base = "#{BEBOX_BOXES_PATH}/#{get_valid_box_uri(current_box)}"
  # Asks user to choose vagrant box provider
  vagrant_box_provider = choose_option(%w{virtualbox vmware}, 'Choose the vagrant box provider')
  # Set default environments
  default_environments = %w{vagrant staging production}
  # Project creation
  project = Bebox::Project.new(project_name, vagrant_box_base, Dir.pwd, vagrant_box_provider, default_environments)
  output = project.create
  ok "Project '#{project_name}' created!.\nMake: cd #{project_name}\nNow you can add new environments or new nodes to your project.\nSee bebox help."
  return output
end

#download_box(uri) ⇒ Object

Download a box by the specified uri



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bebox/wizards/project_wizard.rb', line 121

def download_box(uri)
  @counter = 0
  url = uri.path
  file_name = uri.path.split('/').last
  expanded_directory = File.expand_path(BEBOX_BOXES_PATH)
  # Download file to bebox boxes tmp
  require 'net/http'
  require 'uri'
  Net::HTTP.start(uri.host) do |http|
    response = http.request_head(URI.escape(url))
    ProgressBar
    pbar = ProgressBar.new('file name:', response['content-length'].to_i)
    File.open("#{expanded_directory}/tmp/#{file_name}", 'w') {|f|
      http.get(URI.escape(url)) do |str|
        f.write str
        @counter += str.length
        pbar.set(@counter)
      end
    }
    # In download completion move from tmp to bebox boxes dir
    pbar.finish
    `mv #{BEBOX_BOXES_PATH}/tmp/#{file_name} #{BEBOX_BOXES_PATH}/`
  end
end

#file_uri_valid?(uri) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/bebox/wizards/project_wizard.rb', line 91

def file_uri_valid?(uri)
  File.file?(uri.path) ? (return true) : error('File path not exist!.')
end

#get_existing_boxesObject

Obtain the current boxes downloaded or linked in the bebox user home



103
104
105
106
107
108
109
# File 'lib/bebox/wizards/project_wizard.rb', line 103

def get_existing_boxes
  # Converts the bebox boxes directory to an absolute pathname
  expanded_directory = File.expand_path("#{BEBOX_BOXES_PATH}")
  # Get an array of bebox boxes paths
  boxes = Dir["#{expanded_directory}/*"].reject {|f| File.directory? f}
  boxes.map{|box| box.split('/').last}
end

#get_valid_box_uri(current_box) ⇒ Object

If choose to download/select new box get a valid uri



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

def get_valid_box_uri(current_box)
  return current_box unless current_box.nil?
  # Keep asking for valid uri or overwriting until confirmation
  confirm = false
  begin
    # Asks vagrant box location to user if not choose an existing box
    valid_box_uri = ask_uri
    # Confirm if the box already exist
    confirm = box_exists?(valid_box_uri) ? confirm_action?('There is already a box with that name, do you want to overwrite it?') : true
  end while !confirm
  # Setup the box with the valid uri
  set_box(valid_box_uri)
end

#http_uri_valid?(uri) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
# File 'lib/bebox/wizards/project_wizard.rb', line 83

def http_uri_valid?(uri)
  require 'net/http'
  request = Net::HTTP.new uri.host
  response = request.request_head uri.path
  error('Redirections not supported.') if response.code.to_i == 302
  ( response.code.to_i == 200) ? (return true) : error('Download link not valid!.')
end

#project_exists?(parent_path, project_name) ⇒ Boolean

Check if there’s an existing project in that dir

Returns:

  • (Boolean)


45
46
47
# File 'lib/bebox/wizards/project_wizard.rb', line 45

def project_exists?(parent_path, project_name)
  Dir.exists?("#{parent_path}/#{project_name}")
end

#set_box(box_uri) ⇒ Object

Setup the box in the bebox boxes directory



65
66
67
68
69
70
71
72
73
74
# File 'lib/bebox/wizards/project_wizard.rb', line 65

def set_box(box_uri)
  require 'uri'
  uri = URI.parse(box_uri)
  if %w{http https}.include?(uri.scheme)
    info 'Downloading box ...'
    download_box(uri)
  else
    `ln -fs #{uri.path} #{BEBOX_BOXES_PATH}/#{uri.path.split('/').last}`
  end
end

#uri_valid?(vbox_uri) ⇒ Boolean

Validate uri download or local box existence

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/bebox/wizards/project_wizard.rb', line 77

def uri_valid?(vbox_uri)
  require 'uri'
  uri = URI.parse(vbox_uri)
  %w{http https}.include?(uri.scheme) ? http_uri_valid?(uri) : file_uri_valid?(uri)
end