bin/forj in forj-1.0.1 vs bin/forj in forj-1.0.2
- old
+ new
@@ -14,53 +14,43 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'rubygems'
+#require 'debugger' # Use to debug with Ruby < 2.0
+#require 'byebug' # Use to debug with Ruby >= 2.0
require 'bundler/setup'
-require 'require_relative'
+#require 'require_relative'
require 'thor'
require 'ansi'
$APP_PATH = File.dirname(__FILE__)
$LIB_PATH = File.expand_path(File.join(File.dirname($APP_PATH),'lib'))
-$LOAD_PATH << $LIB_PATH
-$LOAD_PATH << File.join($LIB_PATH, 'lib-forj', 'lib')
-
require 'appinit.rb' # Load generic Application level function
# Initialize forj paths
AppInit::forj_initialize()
-# Initialize global Log object
-$FORJ_LOGGER=ForjLog.new()
+require 'lorj' # Use lorj library for Cloud agnostics feature.
-require 'forj-config.rb' # Load class ForjConfig and Meta data class variables. Requires Logging to be fully set.
-require 'forj-account.rb' # Load class ForjAccount
-require 'connection.rb' # Load class ForjConnection
+PrcLib.app_name = 'forj'
+PrcLib.app_defaults = 'forj'
-require 'down.rb'
+require 'forj-settings.rb' # Settings features
require 'ssh.rb'
-include Down
-include Ssh
-require 'forj-settings.rb' # Settings features
-#require 'debugger' # Use to debug with Ruby < 2.0
-#require 'byebug' # Use to debug with Ruby >= 2.0
-require 'lib-forj.rb'
-
$LIB_FORJ_DEBUG = 1 # less verbose
class ForjThor < Thor
class_option :debug, :aliases => '-d', :desc => 'Set debug mode'
class_option :verbose, :aliases => '-v', :desc => 'Set verbose mode'
class_option :config, :aliases => '-c', :desc => 'Path to a different forj config file. By default, use ~/.forj/config.yaml'
- class_option :libforj_debug, :desc => "Set lib-forj debug level verbosity. 1 to 5. Default is one."
+ class_option :lorj_debug, :desc => "Set lorj debug level verbosity. 1 to 5. Default is one."
desc "help [action]", "Describe available FORJ actions or one specific action"
def help(task = nil, subcommand = false)
if task
@@ -138,66 +128,58 @@
method_option :test_box, :aliases => '-T', :desc => "Identify a path to become your test-box repository.
Ex: if your maestro is in ~/src/forj-oss, --test_box ~/src/forj-oss/maestro build.sh and
test-box will send your local maestro repo to your box, for boot."
def boot(blueprint, on_or_name, old_accountname = nil, as = nil, old_name = nil)
+ Forj::Settings.common_options(options)
- Logging.set_level(Logger::INFO) if options[:verbose]
- Logging.set_level(Logger::DEBUG) if options[:debug]
+ oConfig = Lorj::Config.new(options[:config])
- unless options[:libforj_debug].nil?
- $LIB_FORJ_DEBUG = options[:libforj_debug].to_i
- Logging.set_level(Logger::DEBUG)
- end
-
- oConfig = ForjConfig.new(options[:config])
-
-
# depreciated: <BluePrint> on <AccountName> as <InstanceName>
if old_accountname and as and old_name
msg = "The syntax `forj boot '%s' on '%s' as '%s'` is depreciated.\nUse `forj boot '%s' '%s'" % [blueprint, old_accountname, old_name, blueprint, old_name]
if oConfig.get('account_name') == old_accountname
- Logging.warning("%s` instead." % msg)
+ PrcLib.warning("%s` instead." % msg)
else
- Logging.warning("%s -a '%s'` instead." % [msg, old_accountname])
+ PrcLib.warning("%s -a '%s'` instead." % [msg, old_accountname])
end
name = old_name
oConfig.set(:account_name, old_accountname)
else
name = on_or_name
end
- Logging.fatal( 1, "instance name '%s' not supported. Support only lower case, numeric and dash caracters." % [name]) if not (/^[\d[[:lower:]]-]+$/ =~ name)
+ PrcLib.fatal( 1, "instance name '%s' not supported. Support only lower case, numeric and dash caracters." % [name]) if not (/^[\d[[:lower:]]-]+$/ =~ name)
# Options are added if they are set. Otherwise, get will retrieve the default value.
oConfig.set(:account_name, options[:account_name]) if options[:account_name]
- oForjAccount = ForjAccount.new(oConfig)
+ oForjAccount = Lorj::Account.new(oConfig)
oForjAccount.ac_load()
oConfig.set(:infra_repo, options[:infra])
oConfig.set(:keypair_name, options[:key_name])
oConfig.set(:keypair_path, options[:key_path])
oConfig.set(:security_group, options[:security_group])
- oConfig.set(:image_name, options[:image])
+ oConfig.set(:image_name, options[:image_name])
oConfig.set(:flavor, options[:maestro_flavor])
oConfig.set(:bp_flavor, options[:bp_flavor])
oConfig.set(:maestro_repo , options[:maestro_repo])
oConfig.set(:branch , options[:branch])
oConfig.set(:test_box, File.expand_path(options[:test_box])) if options[:test_box] and File.directory?(File.expand_path(options[:test_box]))
- Logging.warning("test_box is currently disabled in this version. It will be re-activated in newer version.") if options[:test_box]
+ PrcLib.warning("test_box is currently disabled in this version. It will be re-activated in newer version.") if options[:test_box]
if options[:key_path]
mFound = options[:key_path].match(/^(.*)(\.pub)?$/)
if mFound
key_path = File.expand_path(mFound[1])
if mFound[2] and not File.exists?(File.expand_path(mFound[1]+mFound[2]))
- Logging.fatal(1, "'%s' is not a valid keypair files. At least the public key (.pub) is have to exist.")
+ PrcLib.fatal(1, "'%s' is not a valid keypair files. At least the public key (.pub) is have to exist.")
end
oConfig.set(:keypair_path, key_path)
else
- Logging.fatal(1, "'%s' is not a valid keypair files. At least the public key (.pub) is have to exist.")
+ PrcLib.fatal(1, "'%s' is not a valid keypair files. At least the public key (.pub) is have to exist.")
end
end
aProcesses = []
# Defines how to manage Maestro and forges
@@ -206,25 +188,25 @@
# Defines how cli will control FORJ features
# boot/down/ssh/...
aProcesses << File.join($LIB_PATH, 'forj', 'ForjCli.rb')
- oCloud = ForjCloud.new(oForjAccount, oConfig[:account_name], aProcesses)
+ #oCloud = ForjCloud.new(oForjAccount, oConfig[:account_name], aProcesses)
+ oCloud = Lorj::CloudCore.new(oForjAccount, oConfig[:account_name], aProcesses)
oConfig[:instance_name] = name
if blueprint == 'maestro'
- Logging.info("Starting boot process of '%s'. No blueprint requested." % oConfig[:instance_name])
+ PrcLib.info("Starting boot process of '%s'. No blueprint requested." % oConfig[:instance_name])
else
oConfig[:blueprint] = blueprint
- Logging.info("Starting boot process of '%s' with blueprint '%s'." % [oConfig[:instance_name], oConfig[:blueprint]])
+ PrcLib.info("Starting boot process of '%s' with blueprint '%s'." % [oConfig[:instance_name], oConfig[:blueprint]])
end
- Logging.high_level_msg ("Preparing your forge '%s'. Please be patient\n" % oConfig[:instance_name])
+ PrcLib.high_level_msg ("Preparing your forge '%s'. Please be patient. more output in '%s'\n" % [oConfig[:instance_name], File.join($FORJ_DATA_PATH, "forj.log")])
oCloud.Create(:forge)
- #Boot.boot(blueprint, name, options[:build], options[:boothook], options[:box_name], oForjAccount)
end
################################# Show defaults
desc 'show <Object> [name]', 'Show Object (default valuesr, account data, etc...) values.'
long_desc <<-LONGDESC
@@ -235,20 +217,22 @@
- defaults : Provide the list of predefined values, configured by forj, or by you in your ~/.forj/config.yaml (or another config file with -c)
- account [name] : without name, forj will give you the list of account saved. Otherwise print 'name' account data.
LONGDESC
def show(object, name = nil)
+ Settings.common_options(options)
+
case object
when 'defaults'
- oConfig=ForjConfig.new()
+ oConfig=Lorj::Config.new()
puts 'List of default values: (local refer to your config file. hash refer to your FORJ account data)'
puts oConfig.default_dump().to_yaml
puts '---'
- puts "To change default values, update your ~/.forj/config.yaml and add the key/value entry under 'default' section."
+ puts "To change default values, use 'forj get' to check valid keys, and update with 'forj set'"
when 'account'
- oConfig=ForjConfig.new()
+ oConfig=Lorj::Config.new()
if not name
puts "List of FORJ accounts: Use 'forj account YourAccount' to see one account details."
oAccounts = ForjAccounts.new()
puts oAccounts.dump().to_yaml
else
@@ -261,32 +245,89 @@
puts '---'
puts "To change those values, execute 'forj setup -a %s'." % options[:account_name]
end
else
- Logging.error("object '%s' unknown." % name)
+ PrcLib.error("object '%s' unknown." % name)
end
end
-################################# DOWN
- desc 'down <InstanceName>', 'delete the Maestro box and all systems installed by the blueprint'
+################################# DESTROY
+ desc 'destroy <InstanceName> [options]', 'delete the Maestro box and all systems installed by the blueprint'
long_desc <<-LONGDESC
-This action all servers that has been created under the instance name.
+This action destroy all servers found under the instance name and allow you to destroy all of them or just one of them.
-Warning! This action don't removed any network/security groups cloud object.
+Warning! This action do not remove any network/security groups cloud object.
LONGDESC
- def down(name)
- Logging.set_level(Logger::INFO) if options[:verbose]
- Logging.set_level(Logger::DEBUG) if options[:debug]
+ method_option :force, :aliases => "-f", :desc => "force deletion of all servers for the given InstanceName"
- oConfig = ForjConfig.new(options[:config])
+ def destroy(name)
+ Forj::Settings.common_options(options)
+
+ oConfig = Lorj::Config.new(options[:config])
oConfig.set(:account_name, options[:account_name]) if options[:account_name]
- oForjAccount = ForjAccount.new(oConfig)
+ oForjAccount = Lorj::Account.new(oConfig)
oForjAccount.ac_load()
- Down.down(oForjAccount, name)
+ aProcesses = []
+
+ # Defines how to manage Maestro and forges
+ # create a maestro box. Identify a forge instance, delete it,...
+ aProcesses << File.join($LIB_PATH, 'forj', 'ForjCore.rb')
+
+ # Defines how cli will control FORJ features
+ # boot/down/ssh/...
+ aProcesses << File.join($LIB_PATH, 'forj', 'ForjCli.rb')
+
+ oCloud = Lorj::CloudCore.new(oForjAccount, oConfig[:account_name], aProcesses)
+
+ oForge = oCloud.Get(:forge, name)
+
+ if oForge[:servers].count > 0
+
+ if options[:force]
+ #Destroy all servers found
+ oCloud.Delete(:forge)
+ else
+ #Ask the user to get server(s) to destroy
+ serverList = []
+ index = 0
+
+ oForge[:servers].each{ |server|
+ serverList[index] = server[:name]
+ index = index + 1
+ }
+
+ serverList << "all"
+ serverList << "esc"
+
+ say("Select the index of the server you want to destroy")
+ value = choose { | q |
+ q.choices(*serverList)
+ }
+
+ oServerNumber = serverList.index(value)
+
+ if oServerNumber >= 0 and oServerNumber < oForge[:servers].count
+ #Destroy selected server
+ oConfig.set(:forge_server, oForge[:servers][oServerNumber][:id] )
+ oCloud.Delete(:forge)
+ elsif oServerNumber == serverList.index("all")
+ #Destroy all servers found
+ oCloud.Delete(:forge)
+ elsif oServerNumber == serverList.index("esc")
+ #esc
+ PrcLib.high_level_msg("No server destroyed on your demand.\n" % name )
+ end
+
+ end
+
+ else
+ PrcLib.high_level_msg("No server(s) found on forge instance '%s'.\n" % name )
+ end
+
end
################################# SET
desc 'set [key=value] [...] [options]', 'Set one or more variables in defaults or a forj account.'
long_desc <<-LONGDESC
@@ -319,11 +360,11 @@
def set(*p)
Logging.set_level(Logger::INFO) if options[:verbose]
Logging.set_level(Logger::DEBUG) if options[:debug]
- oConfig=ForjConfig.new()
+ oConfig=Lorj::Config.new()
if p.length == 0 and not options[:account_name]
Forj::Settings::config_show_all(oConfig)
elsif p.length == 0 and options[:account_name]
@@ -336,12 +377,12 @@
Forj::Settings::config_set(oConfig, p)
end
end
################################# GET
- desc 'get', 'Get data from defaults or account values.'
- long_desc <<-LONGDESC
+ desc 'get', 'Get data from defaults or account values.'
+ long_desc <<-LONGDESC
forj cli maintain a list of key/value at 3 Levels:
\x5- Application defaults
\x5- Local config defaults
\x5- Account data
@@ -354,19 +395,19 @@
Ex: To get the keypair_name defined from the account, or from your ~/.forj/config.yaml, or if not found, from application defaults.
`forj get keypair_name -a dev`
- LONGDESC
+ LONGDESC
- method_option :account_name, :aliases => '-a', :desc => "Set the forj account name to use. By default, uses the default account set in your local config file."
+ method_option :account_name, :aliases => '-a', :desc => "Set the forj account name to use. By default, uses the default account set in your local config file."
def get(key = nil)
- Logging.set_level(Logger::INFO) if options[:verbose]
- Logging.set_level(Logger::DEBUG) if options[:debug]
+ PrcLib.set_level(Logger::INFO) if options[:verbose]
+ PrcLib.set_level(Logger::DEBUG) if options[:debug]
- oConfig = ForjConfig.new()
+ oConfig = Lorj::Config.new()
if not options[:account_name] and not key
Forj::Settings::config_get_all(oConfig)
elsif options[:account_name] and key
Forj::Settings::account_get(oConfig, options[:account_name], key)
@@ -378,25 +419,31 @@
Forj::Settings::account_get_all(oConfig, options[:account_name])
end
end
################################# SSH
- desc 'ssh <Instance> <Server> [options]', 'connect to your forge thru ssh'
- long_desc <<-LONGDESC
+ desc 'ssh <Instance> [options]', 'connect to your forge thru ssh'
+ long_desc <<-LONGDESC
Connect through ssh to a node attached to an instance
-ex: forj ssh myforge review
- LONGDESC
+ex: forj ssh myforge -n review
+ LONGDESC
- def ssh(name, server)
- Logging.set_level(Logger::INFO) if options[:verbose]
- Logging.set_level(Logger::DEBUG) if options[:debug]
- oConfig = ForjConfig.new(options[:config])
- oConfig.set(:account_name, options[:account_name]) if options[:account_name]
- Ssh.connect(name, server, oConfig)
- end
+ method_option :box_name, :aliases => '-n', :desc => "box name to create ssh connection"
+ method_option :identity, :aliases => '-i', :desc => "Private key file name."
+ def ssh(oInstanceName)
+ Forj::Settings.common_options(options)
+
+ oConfig = Lorj::Config.new(options[:config])
+
+ oConfig.set(:box_ssh, options[:box_name]) if options[:box_name]
+ oConfig.set(:identity, options[:identity]) if options[:identity]
+
+ Forj::Ssh.connect(oInstanceName, oConfig)
+ end
+
################################# SETUP
desc 'setup [AccountName [Provider]] [options]', "Setup FORJ cloud account credentials and information."
long_desc <<-LONGDESC
@@ -413,19 +460,13 @@
\x5- user/password (password is encrypted)
\x5- DNS settings if you want Maestro to manage it.
\x5- domain name to add to each boxes hostname
LONGDESC
def setup(sAccountName = 'hpcloud', sProvider = "hpcloud")
- Logging.set_level(Logger::INFO) if options[:verbose]
- Logging.set_level(Logger::DEBUG) if options[:debug]
+ Forj::Settings.common_options(options)
- unless options[:libforj_debug].nil?
- $LIB_FORJ_DEBUG = options[:libforj_debug].to_i
- Logging.set_level(Logger::DEBUG)
- end
-
- oConfig=ForjConfig.new(options[:config])
+ oConfig=Lorj::Config.new(options[:config])
oConfig.set(:provider_name, sProvider)
oConfig.set(:account_name, sAccountName)
aProcesses = []
@@ -435,10 +476,12 @@
# Defines how cli will control FORJ features
# boot/down/ssh/...
aProcesses << File.join($LIB_PATH, 'forj', 'ForjCli.rb')
- oCloud = ForjCloud.new(oConfig, sAccountName, aProcesses)
+ # Loading CloudCore embedding provider controller + its process.
+ oCloud = Lorj::CloudCore.new(oConfig, oConfig[:account_name], aProcesses)
+
oCloud.Setup(:forge, sAccountName)
oCloud.config.ac_save()
end