Class: SANStore::CLI::Commands::NewVol

Inherits:
Cri::Command show all
Defined in:
lib/SANStore/cli/commands/new_vol.rb

Overview

The new_vol command adds a new ZFS volume to the specified volume store, and sets it up as an iSCSI target. Defaults are supplied to all arguments, but can be overridden by the user for slightly customised set-up of the volume store. However, the user interface should be kept as simple as possible.

Author:

  • David Love

Instance Attribute Summary

Attributes inherited from Cri::Command

#base

Instance Method Summary collapse

Methods inherited from Cri::Command

#<=>, #help

Instance Method Details

#aliasesObject

The aliases this sub-command is known by



47
48
49
50
51
# File 'lib/SANStore/cli/commands/new_vol.rb', line 47

def aliases
  [
    "new", "add", "add_vol"
  ]
end

#long_descObject

A longer description, detailing both the purpose and the use of this command



60
61
62
63
64
65
66
# File 'lib/SANStore/cli/commands/new_vol.rb', line 60

def long_desc
  'By default, this command creates a 20G ZFS volume, and marks it for ' +
  'sharing as an iSCSI target on the local network.' + "\n\n" +
  'Warning: By default this commands sets up the iSCSI target with NO ' +
  'security. This is fine for testing and use in the labs, but obviously ' +
  'is not ideal if you care about the data stored on this new volume...'
end

#nameObject

The name of the sub-command (as it appears in the command line app)



42
43
44
# File 'lib/SANStore/cli/commands/new_vol.rb', line 42

def name
  'new_vol'
end

#option_definitionsObject

Define the options for this command



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/SANStore/cli/commands/new_vol.rb', line 74

def option_definitions
  [
    { :short => 'v', :long => 'volume_store', :argument => :optional,
      :desc => 'specifify the ZFS root of the new iSCSI volume. Defaults to "store/volumes".'
    },
    { :short => 'n', :long => 'name', :argument => :optional,
      :desc => 'the name of the new volume. This must be a valid ZFS volume name, and defaults to ' +
        'an RFC 4122 GUID.'
    },
    { :short => 's', :long => 'size', :argument => :optional,
      :desc => 'the size of the new iSCSI volume. Note that while ZFS allows you to change the size ' +
        'of the new volume relatively easily, because the iSCSI initiator sees this volume as a raw ' +
        'device changing the size later may be very easy or very difficult depending on the initiators ' +
        'operating system (and the specific file system being used). In other words, choose with care: ' +
        'by default this command uses a size of 20G, which should be enough for most tasks in the labs.'  
    },
  ]
end

#run(options, arguments) ⇒ Object

Execute the command



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
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/SANStore/cli/commands/new_vol.rb', line 94

def run(options, arguments)

  # Look at the options, and if we don't find any (or some are
  # missing), set them to the default values
  if options[:volume_store].nil? or options[:volume_store].empty? then
    volume_store = "store/volumes"
    options[:volume_store] = volume_store
  end
  
  if options[:name].nil? or options[:name].empty? then
    name = UUIDTools::UUID.timestamp_create
    options[:name] = name.to_s
  end
  
  if options[:size].nil? or options[:size].empty? then
    size = "20G" 
    options[:size] = size
  end
  
  SANStore::CLI::Logger.instance.log_level(:low, :info, "Using #{options[:name]} as the volume identifier")

  # Ask for a new volume
  ZFS.new_volume(options[:volume_store] + "/" + options[:name], options[:size]) 

  # Set the volume up as an iSCSI target
  target_name = COMStar.new_target(options[:volume_store] + "/" + options[:name], options[:name])

  # Tell the caller what the new volume name is
  text = "\n"
  text << "A new iSCSI target has been created with the following properties\n"
  text << "\n" 
  text << sprintf("    %-25s %s\n", ANSI.red{ "Name:" }, target_name.wrap_and_indent(78, 20).lstrip)
  text << sprintf("    %-25s %s\n", ANSI.red{ "IPv4 Address:" }, Socket::getaddrinfo(Socket.gethostname,"echo",Socket::AF_INET)[0][3])
  text << "\n" 

  puts text
end

#short_descObject

A short help text describing the purpose of this command



54
55
56
# File 'lib/SANStore/cli/commands/new_vol.rb', line 54

def short_desc
  'Create a new iSCSI volume in the SAN volume store.'
end

#usageObject

Show the user the basic syntax of this command



69
70
71
# File 'lib/SANStore/cli/commands/new_vol.rb', line 69

def usage
  "store new_volume [--volume-store ZFS_PATH][--name GUID] [--size INTEGER]"
end