Class: Tenderloin::Commands

Inherits:
Object
  • Object
show all
Extended by:
Util
Defined in:
lib/tenderloin/commands.rb

Overview

Contains all the command-line commands invoked by the binaries. Having them all in one location assists with documentation and also takes the commands out of some of the other classes.

Class Method Summary collapse

Methods included from Util

error_and_exit, included, logger, wrap_output

Class Method Details

.box(argv) ⇒ Object

Manages the ‘tenderloin box` command, allowing the user to add and remove boxes. This single command, given an array, determines which action to take and calls the respective action method (see box_add and box_remove)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tenderloin/commands.rb', line 94

def box(argv)
  Env.load!

  sub_commands = ["list", "add", "remove"]

  if !sub_commands.include?(argv[0])
    error_and_exit(<<-error)
Please specify a valid action to take on the boxes, either
`add` or `remove`. Examples:

tenderloin box add name uri
tenderloin box remove name
error
  end

  send("box_#{argv[0]}", *argv[1..-1])
end

.box_add(name, path) ⇒ Object

Adds a box to the local filesystem, given a URI.



129
130
131
# File 'lib/tenderloin/commands.rb', line 129

def box_add(name, path)
  Box.add(name, path)
end

.box_listObject

Lists all added boxes



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tenderloin/commands.rb', line 113

def box_list
  boxes = Box.all.sort

  wrap_output do
    if !boxes.empty?
      puts "Installed Tenderloin Boxes:\n\n"
      boxes.each do |box|
        puts box
      end
    else
      puts "No Tenderloin Boxes Added!"
    end
  end
end

.box_remove(name) ⇒ Object

Removes a box.



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tenderloin/commands.rb', line 134

def box_remove(name)
  box = Box.find(name)
  if box.nil?
    error_and_exit(<<-error)
The box you're attempting to remove does not exist!
error
    return # for tests
  end

  box.destroy
end

.destroyObject

Tear down a tenderloin instance. This not only shuts down the instance (if its running), but also deletes it from the system, including the hard disks associated with it.

This command requires that an instance already be brought up with ‘tenderloin up`.



49
50
51
52
53
# File 'lib/tenderloin/commands.rb', line 49

def destroy
  Env.load!
  Env.require_persisted_vm
  Env.persisted_vm.destroy
end

.haltObject

Halts a running tenderloin instance. This forcibly halts the instance; it is the equivalent of pulling the power on a machine. The instance can be restarted again with up.

This command requires than an instance already be brought up with ‘tenderloin up`.



84
85
86
87
88
# File 'lib/tenderloin/commands.rb', line 84

def halt
  Env.load!
  Env.require_persisted_vm
  Env.persisted_vm.execute!(Actions::VM::Halt)
end

.initObject

Initializes a directory for use with tenderloin. This command copies an initial ‘Tenderfile` into the current working directory so you can begin using tenderloin. The configuration file contains some documentation to get you started.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tenderloin/commands.rb', line 14

def init
  rootfile_path = File.join(Dir.pwd, $ROOTFILE_NAME)
  if File.exist?(rootfile_path)
    error_and_exit(<<-error)
It looks like this directory is already setup for tenderloin! (A #{$ROOTFILE_NAME}
already exists.)
error
  end

  # Copy over the rootfile template into this directory
  FileUtils.cp(File.join(PROJECT_ROOT, "templates", $ROOTFILE_NAME), rootfile_path)
end

.provisionObject

Runs the provisioning script



147
148
149
150
151
# File 'lib/tenderloin/commands.rb', line 147

def provision
  Env.load!
  Env.require_persisted_vm
  Env.persisted_vm.execute!(Actions::VM::Provision)
end

.reloadObject

Reload the environment. This is almost equivalent to the up command except that it doesn’t import the VM and do the initialize bootstrapping of the instance. Instead, it forces a shutdown (if its running) of the VM, updates the metadata (shared folders, forwarded ports), restarts the VM, and then reruns the provisioning if enabled.



60
61
62
63
64
# File 'lib/tenderloin/commands.rb', line 60

def reload
  Env.load!
  Env.require_persisted_vm
  Env.persisted_vm.execute!(Actions::VM::Reload)
end

.sshObject

SSH into the tenderloin instance. This will setup an SSH connection into the tenderloin instance, replacing the running ruby process with the SSH connection.

This command requires that an instance already be brought up with ‘tenderloin up`.



72
73
74
75
76
# File 'lib/tenderloin/commands.rb', line 72

def ssh
  Env.load!
  Env.require_persisted_vm
  SSH.connect Env.persisted_vm.fusion_vm.ip
end

.upObject

Bring up a tenderloin instance. This handles everything from importing the base VM, setting up shared folders, forwarded ports, etc to provisioning the instance with chef. up also starts the instance, running it in the background.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tenderloin/commands.rb', line 31

def up
  Env.load!

  if Env.persisted_vm
    logger.info "VM already created. Starting VM if its not already running..."
    Env.persisted_vm.start
  else
    Env.require_box
    VM.execute!(Actions::VM::Up)
  end
end