Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Add environment variable capability for config setting.
Browse files Browse the repository at this point in the history
Bump to version 1.7.0.
  • Loading branch information
christiankakesa committed Jan 9, 2016
1 parent f30a8a0 commit 1f287d8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
19 changes: 9 additions & 10 deletions bin/netsoul-ruby
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ module Netsoul
@sock = TCPSocket.new(@config.server_host, @config.server_port)
fail Netsoul::SocketError, 'Could not open a socket. Connection is unavailable.'.freeze unless @sock
_cmd, _socket_num, md5_hash, client_ip, client_port, _server_timestamp = sock_get.split

@config.build_user_connection_info md5_hash: md5_hash, client_ip: client_ip, client_port: client_port

auth_ag
auth_method
auth_status

@started = true
end

Expand All @@ -75,8 +78,8 @@ module Netsoul
sock, = IO.select([@sock], nil, nil, SOCKET_READ_TIMEOUT)
fail Netsoul::SocketError, 'Timeout or fail on read socket' if sock.nil? || sock.empty?
res = sock.first.gets
log :info, "[get ] #{res.chomp}"
res
log :info, "[get ] #{res.chomp}" if res
res || ''
end

def sock_close
Expand All @@ -99,12 +102,7 @@ OptionParser.new do |opts|
opts.separator 'Netsoul-Ruby options:'.freeze

opts.on('-c'.freeze, '--config FILE'.freeze, 'Configuration file in YAML'.freeze) do |file|
options[:config] = file
unless File.file?(options[:config])
puts '[ERROR] Configuration is not a valid file'
exit
end
options[:user_opts] = YAML.load_file(options[:config])
options[:user_opts] = YAML.load_file(file) if File.file?(file)
end

opts.on('-h', '--help', 'Display this screen') do
Expand All @@ -113,8 +111,9 @@ OptionParser.new do |opts|
end
end.parse!

unless options.include?(:config)
puts '[ERROR] Configuration file is not provided'
unless ENV.to_a.count { |k, _v| %w(NETSOUL_LOGIN NETSOUL_SOCKS_PASSWORD).include?(k) } == 2 ||
ENV.to_a.count { |k, _v| %w(NETSOUL_LOGIN NETSOUL_UNIX_PASSWORD NETSOUL_AUTH_METHOD).include?(k) } == 3
puts '[ERROR] You have to specify a configuration file or environment variables'
exit
end

Expand Down
35 changes: 24 additions & 11 deletions lib/netsoul/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,33 @@ class Config

attr_reader :client_name

# rubocop:disable Metrics/AbcSize
# Supported environment variables:
#
# +NETSOUL_SERVER_HOST+: Netsoul server host, default 'ns-server.epita.fr'
# +NETSOUL_SERVER_PORT+: Netsoul server port, default 4242
# +NETSOUL_LOGIN+: IONIS account name
# +NETSOUL_SOCKS_PASSWORD+: IONIS socks password
# +NETSOUL_UNIX_PASSWORD+: IONIS unix password
# +NETSOUL_AUTH_METHOD+: Authentication method, default :std. Valid options are => @see +Config::AUTH_METHODS+
# +NETSOUL_STATE+: User status, default is :none. Valid options are => @see +Config::USER_STATES+
# +NETSOUL_LOCATION+: User location is free text of your position. If you ar in IONIS network an automatic mapping is proceed to detect your location
# +NETSOUL_USER_GROUP+: Free text specifying your promo or whatever else
# +NETSOUL_CLIENT_NAME+: Redefine the client name exposed to the Netsoul server
#
# rubocop:disable all
def initialize(opts = {})
@server_host = opts.fetch(:server_host, 'ns-server.epita.fr'.freeze)
@server_port = Integer(opts.fetch(:server_port, 4242))
@login = opts.fetch(:login, 'ionis'.freeze)
@socks_password = opts.fetch(:socks_password, 'socks_password'.freeze)
@unix_password = opts.fetch(:unix_password, 'unix_password'.freeze)
@auth_method = AUTH_METHODS.include?(opts[:auth_method]) ? opts[:auth_method] : :std
@state = USER_STATES.include?(opts[:state]) ? opts[:state] : :none
@location = opts.fetch(:location, 'Home'.freeze)
@user_group = opts.fetch(:user_group, 'ETNA_2008'.freeze)
@server_host = ENV['NETSOUL_SERVER_HOST'] || opts.fetch(:server_host, 'ns-server.epita.fr'.freeze)
@server_port = Integer(ENV['NETSOUL_SERVER_PORT'] || opts.fetch(:server_port, 4242))
@login = ENV['NETSOUL_LOGIN'] || opts.fetch(:login, 'ionis'.freeze)
@socks_password = ENV['NETSOUL_SOCKS_PASSWORD'] || opts.fetch(:socks_password, 'socks_password'.freeze)
@unix_password = ENV['NETSOUL_UNIX_PASSWORD'] || opts.fetch(:unix_password, 'unix_password'.freeze)
@auth_method = (ENV['NETSOUL_AUTH_METHOD'] || (AUTH_METHODS.include?(opts[:auth_method]) ? opts[:auth_method] : :std)).to_sym
@state = (ENV['NETSOUL_STATE'] || (USER_STATES.include?(opts[:state]) ? opts[:state] : :none)).to_sym
@location = ENV['NETSOUL_LOCATION'] || opts.fetch(:location, 'Home'.freeze)
@user_group = ENV['NETSOUL_USER_GROUP'] || opts.fetch(:user_group, 'ETNA_2008'.freeze)
@user_connection_info = {}

@client_name = opts.fetch(:client_name, '(Netsoul-Ruby) -> { Christian Kakesa, since 2009}'.freeze)
@client_name = ENV['NETSOUL_CLIENT_NAME'] || opts.fetch(:client_name, '(Netsoul-Ruby) -> { Christian Kakesa, since 2009}'.freeze)
end

def build_user_connection_info(opts = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/netsoul/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Netsoul
VERSION = '1.6.0'.freeze
VERSION = '1.7.0'.freeze
end

0 comments on commit 1f287d8

Please sign in to comment.