Class: Protobox::Command::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/protobox/command/init.rb

Class Method Summary collapse

Class Method Details

.execute(args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protobox/command/init.rb', line 6

def self.execute args
  options = { verbose: false }

  opts = help

  # Parse the options
  argv = opts.parse(args)
  #return if !argv

  #if argv.empty? || 
  if argv.first == 'help'
    puts opts
    exit
  end

  install_folder = Util::System.current_dir
  git = Util::Platform.git
  vagrant = Util::Platform.vagrant

  # Get installation folder
  puts "Protobox will now be installed at:"
  puts install_folder

  #install_folder = Protobox::Util::Input.get_input
  Util::Input.enter_or_abort # if STDIN.tty?

  # Check for git
  if !git
    raise Errors::FatalError, 'GIT is required'
  end

  # Check for vagrant
  if !vagrant
    raise Errors::FatalError, 'Vagrant is required'
  end

  # Check for previous installation
  if !Dir["#{install_folder}/.git/*"].empty?
    raise Errors::FatalError, <<-MESSAGE
    It appears something is already installed here. Please run this installer in an empty directory.
    MESSAGE
  end

  # Test for permissions
  if File.directory?(install_folder) and not File.executable?(install_folder)
    raise Errors::FatalError, <<-MESSAGE
    The installation folder, #{install_folder}, exists but is not readable. Please fix 
    the permissions and try again:
      sudo chmod 755 #{install_folder}
    MESSAGE
  end

  puts "Starting installation..."

  if File.directory?(install_folder)
    system "/bin/chmod g+rwx #{install_folder}"
  else
    system "/bin/mkdir #{install_folder}"
    system "/bin/chmod g+rwx #{install_folder}"
  end

  puts "Downloading and installing protobox..."
  Dir.chdir(install_folder) do
    if git
      # we do it in four steps to avoid merge errors when reinstalling
      system git, "init", "-q"
      system git, "remote", "add", "origin", "https://github.com/protobox/protobox"

      args = git, "fetch", "origin", "master:refs/remotes/origin/master", "-n"
      args << "--depth=1" if ARGV.include? "--fast"
      system *args

      system git, "reset", "--hard", "origin/master"
    else
      # -m to stop tar erroring out if it can't modify the mtime for root owned directories
      # pipefail to cause the exit status from curl to propogate if it fails
      # we use -k for curl because Leopard has a bunch of bad SSL certificates
      #curl_flags = "fsSL"
      #curl_flags << "k" if macos_version <= "10.5"
      #system "/bin/bash -o pipefail -c '/usr/bin/curl -#{curl_flags} https://github.com/protobox/protobox/tarball/master | /usr/bin/tar xz -m --strip 1'"
    
      raise Errors::FatalError, "GIT not found and is required to install"
    end
  end

  puts "Configuring protobox..."

  Dir.chdir(install_folder) do
    system "/bin/cp data/config/common.yml-dist data/config/common.yml"

    #system "vagrant up" if ARGV.include? "--boot"
  end

  # Install vagrant tools
  system vagrant, "plugin", "install", "vagrant-protobox"

  # TODO
  # - check for arguments to install a config and boot
  # - protobox init abc123
  # - this, sets up protobox dir in current directory
  # - installs vagrant plugin
  # - passes through vagrant install command
  # - vagrant up

  puts "Installation successful! Type the following to make sure its working: "
  #puts "cd #{install_folder}/ && protobox help"
  puts "protobox help"

  #Cli.exec("protobox help")

  # Success, exit status 0
  0
end

.helpObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/protobox/command/init.rb', line 120

def self.help 
  return OptionParser.new do |o|
    o.banner = "Usage: protobox init [options]"
    o.separator ""
    o.separator "Options:"
    o.separator ""

    o.on("-c", "--clean", "Clean any temporary download files") do |c|
      options[:clean] = c
    end

    o.on("-f", "--force", "Overwrite an existing box if it exists") do |f|
      options[:force] = f
    end

    o.on("--verbose", "Enable verbose output for the installation") do |v|
      options[:verbose] = v
    end

    o.separator ""
  end
end