obelisk 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4986444eb31f4e8e6cd29150d70e40e206e9c8ab
4
- data.tar.gz: 0b63597964c9aef1659d7e1c15219756921ad81f
3
+ metadata.gz: c847ea4d549f8d213b7107d9d7b712222bd0458a
4
+ data.tar.gz: 0f397ee7d0c430ad7f31837512b7431c7c83c652
5
5
  SHA512:
6
- metadata.gz: ee4f6de9fabb817572a1a65a960cc4a721f5eb0da21255b7651899872d0b44198171993efc60cf42fe8de842b51e3cd56b8d59c2bffba89ef01c3af5dd83e228
7
- data.tar.gz: 00665d0131a3f9c1587308ff8998c09c89e5763ba9dfb763142b1847474238975d83b01dcae623bac4b1c40c5ceb205cec3c17daf8514517a297308375114662
6
+ metadata.gz: 41393c14c86b8cc4662f260b021cacc0dfa722a067792a568fb775ad21b86b427c30ad5d7f5946079bef29e8cc4414ccf9a09ac0b56c48889f03eaecae61f046
7
+ data.tar.gz: adb3540fd474e7a57fde7e3266c8f719e445f90b2bbf38a1abbaf8cbd4d5402e60e49b2ed95eb9bfe3009dfd9cf9c0d40f47c16ae63c85ab2cd3630e2ab692be
@@ -3,31 +3,53 @@
3
3
  lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
4
  $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
5
 
6
- require 'obelisk'
7
- Obelisk.load_conf
8
-
9
- ARGV << "help" if ARGV.empty?
10
-
11
- if ARGV.include? "install"
12
- Obelisk.save_def_conf
13
- puts "Default configuration file's been saved as #{Obelisk::DEFAULT_CONFIG_PATH}. Change it according to your needs."
14
- exit
15
- else
16
- ARGV.each do |arg|
17
- case arg
18
- when "conf"
19
- Obelisk.make_erb_conf
20
- puts "All files has been generated."
21
- when "updb"
22
- puts "Not implemented yet"
23
- else
24
- puts %{Usage: obelisk_cli install
25
- obelisk_cli conf [updb]
6
+ require "optparse"
7
+ require "securerandom"
8
+ require "obelisk"
9
+
10
+ options = {}
11
+ VALID_COMMANDS = %w{install generate update}
12
+
13
+ op = OptionParser.new do |opts|
14
+ opts.banner =
15
+ %Q{Usage: obelisk_cli [options] command
26
16
  Commands:
27
- install - save default config file for obelisk to #{Obelisk::DEFAULT_CONFIG_PATH}
28
- conf - generate config files for Asterisk and place it to #{$conf[:asterisk_conf_dir]}
29
- updb - update Rails database for Asterisk Portal [NOT implemented yet]}
30
- exit
17
+ install Copy or merge configuration file for Obelisk
18
+ generate Generate config files for Asterisk
19
+ update Replace config files for Asterisk in case of any change
20
+ Options:}
21
+ opts.on("-o", "--outdir [DIR]", "Output directory for generated config files") { |dir| options[:dir] = dir }
22
+ opts.on("-r", "--restart", "Restart Asterisk in case of config change") { options[:restart] = true }
23
+ opts.on("-c", "--config [FILE]", "Specify config FILE for Obelisk") { |file| options[:config] = file }
24
+ opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
25
+ end
26
+
27
+ op.parse!
28
+
29
+ ARGV.select! { |arg| VALID_COMMANDS.include? arg }
30
+ (puts op.help; exit 1) if ARGV.empty?
31
+
32
+ Obelisk.load_conf options[:config]
33
+ $conf[:asterisk_conf_dir] = options[:dir] if options[:dir]
34
+
35
+ ARGV.each do |arg|
36
+ case arg
37
+ when "install"
38
+ if Obelisk.save_def_conf options[:config]
39
+ puts "Found and updated config file. Please, check it."
40
+ else
41
+ puts "Default configuration file's been saved. Change it accordingly."
42
+ end
43
+ when "generate"
44
+ Obelisk.make_erb_conf :force => true, :restart => options[:restart]
45
+ puts "All files has been generated."
46
+ when "update"
47
+ if Obelisk.make_erb_conf(:restart => options[:restart])
48
+ puts "Asterisk config files have been updated"
49
+ else
50
+ puts "Already up to date."
31
51
  end
52
+ else
53
+ puts op
32
54
  end
33
55
  end
@@ -1,5 +1,4 @@
1
1
  ; This file is generated by Obelisk <%=Obelisk::VERSION %>
2
- ; Date: <%= DateTime.now %>
3
2
 
4
3
  <% current_context = nil -%>
5
4
  <% users.sort { |a, b| a[:context] <=> b[:context] }.each do |user| -%>
@@ -1,5 +1,4 @@
1
1
  ; This file is generated by Obelisk <%=Obelisk::VERSION %>
2
- ; Date: <%= DateTime.now %>
3
2
  <% users.each do |user| -%>
4
3
 
5
4
  ; Name: <%= user[:name] %>
@@ -10,42 +10,57 @@ module Obelisk
10
10
  DEFAULT_CONFIG_PATH = "#{Dir.home}/.obelisk.conf"
11
11
  DEFAULT_CONFIG = {
12
12
  :ad_settings => {
13
- ldap_host: "localhost",
14
- base_dn: "ou=STAFF,dc=CORP,dc=LOCAL",
15
- bind_name: "user",
16
- bind_pass: "pass",
13
+ ldap_host: "localhost",
14
+ base_dn: "ou=STAFF,dc=CORP,dc=LOCAL",
15
+ bind_name: "user",
16
+ bind_pass: "pass",
17
17
  },
18
18
  :mapping => {
19
- samaccountname: :peer,
20
- info: :secret,
21
- facsimiletelephonenumber: :context,
22
- ipphone: :extension,
23
- pager: :callgroup,
19
+ samaccountname: :peer,
20
+ info: :secret,
21
+ facsimiletelephonenumber: :context,
22
+ ipphone: :extension,
23
+ pager: :callgroup,
24
24
  # Used for AdressBook generation
25
- displayname: :name,
26
- mobile: :mobile,
27
- homephone: :home,
28
- company: :company,
29
- department: :department,
30
- title: :title,
25
+ displayname: :name,
26
+ mobile: :mobile,
27
+ homephone: :home,
28
+ company: :company,
29
+ department: :department,
30
+ title: :title,
31
31
  },
32
32
  :defaults => {
33
- facsimiletelephonenumber: "from-internal"
33
+ facsimiletelephonenumber: "from-internal"
34
34
  },
35
- :asterisk_conf_dir => "/tmp/asterisk",
36
- :erb_file_dir => File.expand_path(File.dirname(__FILE__) + "/../erb")
35
+ :asterisk_conf_dir => "/tmp/asterisk",
36
+ :asterisk_restart_command => %q{asterisk -x "core restart gracefully"},
37
+ :erb_file_dir => File.expand_path(File.dirname(__FILE__) + "/../erb")
37
38
  }
38
39
 
39
40
  $conf = {}
40
41
 
41
- def self.make_erb_conf
42
- FileUtils.mkdir_p $conf[:asterisk_conf_dir]
42
+ def self.make_erb_conf(params = {})
43
+ params[:conf_dir] ||= $conf[:asterisk_conf_dir]
44
+ params[:force] ||= false
45
+ params[:restart] ||= false
46
+ FileUtils.mkdir_p params[:conf_dir]
43
47
  b = binding
48
+ up = false
44
49
  users = get_ad_users
45
50
  Dir[File.expand_path "*.erb", $conf[:erb_file_dir]].each do |erb|
46
- conf_file = File.expand_path File.basename(erb, ".erb"), $conf[:asterisk_conf_dir]
47
- File.open(conf_file, "w") { |file| file.write ERB.new(IO.read(erb), nil, "-").result(b) }
51
+ conf_file = File.expand_path File.basename(erb, ".erb"), params[:conf_dir]
52
+ File.open(conf_file, File.exists?(conf_file) ? "r+" : "w+") do |file|
53
+ content = ERB.new(IO.read(erb), nil, "-").result(b)
54
+ if content != file.read || params[:force]
55
+ file.seek 0
56
+ file.truncate 0
57
+ file.write content
58
+ up = true
59
+ end
60
+ end
48
61
  end
62
+ system $conf[:asterisk_restart_command] if up && params[:restart]
63
+ up
49
64
  end
50
65
 
51
66
  def self.get_ad_users(ou = nil)
@@ -68,16 +83,21 @@ module Obelisk
68
83
  end
69
84
  end
70
85
 
71
- def self.save_def_conf(file_name = DEFAULT_CONFIG_PATH)
86
+ def self.save_def_conf(file_name = nil)
87
+ file_name ||= DEFAULT_CONFIG_PATH
72
88
  conf = DEFAULT_CONFIG
89
+ updated = false
73
90
  if File.exists? file_name
74
91
  merge_proc = proc { |k, o, n| o.is_a?(Hash) && n.is_a?(Hash) ? o.merge(n, &merge_proc) : n }
75
92
  conf.merge! YAML.load(IO.read(file_name)), &merge_proc
93
+ updated = true
76
94
  end
77
95
  File.open(file_name, 'w') { |f| f.write YAML.dump conf }
96
+ updated
78
97
  end
79
98
 
80
- def self.load_conf(file_name = DEFAULT_CONFIG_PATH)
99
+ def self.load_conf(file_name = nil)
100
+ file_name ||= DEFAULT_CONFIG_PATH
81
101
  return unless $conf.empty?
82
102
  begin
83
103
  $conf = YAML.load IO.read file_name
@@ -1,3 +1,3 @@
1
1
  module Obelisk
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -4,13 +4,18 @@ require "securerandom"
4
4
 
5
5
  class ErbTestCase < Test::Unit::TestCase
6
6
  def test_make_erb_conf
7
- dir = "/tmp/#{SecureRandom.hex}"
8
- $conf[:asterisk_conf_dir] = dir
9
- Obelisk.make_erb_conf
10
- assert Dir.exists? dir
11
- assert_equal Dir[File.expand_path "*.erb", $conf[:erb_file_dir]].count, Dir[File.expand_path "*", dir].count
12
- puts %x(cat #{dir}/*)
13
- File.delete *Dir[File.expand_path "*", dir]
14
- Dir.rmdir dir
7
+ begin
8
+ dir = "/tmp/#{SecureRandom.hex}"
9
+ assert Obelisk.make_erb_conf :conf_dir => dir
10
+ assert Dir.exists? dir
11
+ assert_equal Dir[File.expand_path "*.erb", $conf[:erb_file_dir]].count, Dir[File.expand_path "*", dir].count
12
+ assert ! Obelisk.make_erb_conf(:conf_dir => dir)
13
+ assert Obelisk.make_erb_conf :conf_dir => dir, :force => true
14
+ assert ! Obelisk.make_erb_conf(:conf_dir => dir)
15
+ #puts %x(cat #{dir}/*)
16
+ ensure
17
+ File.delete *Dir[File.expand_path "*", dir]
18
+ Dir.rmdir dir
19
+ end
15
20
  end
16
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obelisk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - goredar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-24 00:00:00.000000000 Z
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler