gogetit 0.12.3 → 0.12.4

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: 0890aeef5f381b7a754be4830c3cb9e4ac68505a
4
- data.tar.gz: 3d984331c7ea4ddd357544e6e82161167d054333
3
+ metadata.gz: e0f13b5df25763b6b37fa24f18706e2f58579806
4
+ data.tar.gz: cb56da342f4477e93a731039d6fe3e0a18922a22
5
5
  SHA512:
6
- metadata.gz: 066366221ebb84db77e72387836b0ae08221caa25d6c8698773e3bfe82c0038bdf4e2742dcb9fea2df65985bea01e3cb1938f913c2a8eb9acb34efb81e6e4fa9
7
- data.tar.gz: f549b6db39ab79199e082ba7cef1b237cfaac796404cebf1029a7580363d1c8dd51cdb520d355b50a933f94d84f2ac2530f206dede2a0d490bbc2f8af59e1a2b
6
+ metadata.gz: 47a396841a95400ee9996411ae3a2306dd3224e2fcf5a0c1ce4c5d7ff0a2eb0f71a5266c26d0753c22863a4414e24d416258d86f023e86bbd72620b7b1dd58ea
7
+ data.tar.gz: 14e411b2ce7f3cfc39ac0372283742b0770a9ab677164c4860f4e815b5bb0d4ae545f18d6aaffe8383d2b20700c910a5cde93b31afd39ed5b5f3bb5d8b8dbdd8
@@ -16,16 +16,4 @@ module Gogetit
16
16
  @lxd = Gogetit::GogetLXD.new(config, maas, logger)
17
17
  @libvirt = Gogetit::GogetLibvirt.new(config, maas, logger)
18
18
 
19
- def self.get_provider_of(name)
20
- if lxd.container_exists?(name)
21
- logger.info("Calling <#{__method__.to_s}>, It is a LXD container.")
22
- return 'lxd'
23
- elsif libvirt.domain_exists?(name)
24
- logger.info("Calling <#{__method__.to_s}>, It is a KVM domain.")
25
- return 'libvirt'
26
- else
27
- puts "#{name} is not found"
28
- return nil
29
- end
30
- end
31
19
  end
@@ -4,34 +4,36 @@ require 'gogetit/util'
4
4
 
5
5
  module Gogetit
6
6
 
7
- @@result = nil
8
-
9
- def self.set_result(x)
10
- @@result = x
11
- end
12
-
13
- def self.get_result
14
- @@result
15
- end
16
-
17
7
  class CLI < Thor
18
8
  include Gogetit::Util
19
9
  package_name 'Gogetit'
20
10
 
21
- # attr_accessor :result
11
+ @result = nil
12
+ class << self
13
+ attr_accessor :result
14
+ end
22
15
 
23
- # def initialize(*args)
24
- # super
25
- # @result = nil
26
- # end
16
+ attr_reader :config, :logger, :lxd, :libvirt, :providers
17
+
18
+ def initialize(*args)
19
+ super
20
+ @config = Gogetit.config
21
+ @logger = Gogetit.logger
22
+ @lxd = Gogetit.lxd
23
+ @libvirt = Gogetit.libvirt
24
+ @providers = {
25
+ lxd: lxd,
26
+ libvirt: libvirt
27
+ }
28
+ end
27
29
 
28
30
  desc 'list', 'List containers and instances, running currently.'
29
31
  def list
30
- puts "Listing LXD containers on #{Gogetit.config[:lxd][:nodes][0][:url]}.."
31
- system("lxc list #{Gogetit.config[:lxd][:nodes][0][:name]}:")
32
+ puts "Listing LXD containers on #{config[:lxd][:nodes][0][:url]}.."
33
+ system("lxc list #{config[:lxd][:nodes][0][:name]}:")
32
34
  puts ''
33
- puts "Listing KVM domains on #{Gogetit.config[:libvirt][:nodes][0][:url]}.."
34
- system("virsh -c #{Gogetit.config[:libvirt][:nodes][0][:url]} list --all")
35
+ puts "Listing KVM domains on #{config[:libvirt][:nodes][0][:url]}.."
36
+ system("virsh -c #{config[:libvirt][:nodes][0][:url]} list --all")
35
37
  end
36
38
 
37
39
  desc 'create NAME', 'Create either a container or KVM domain.'
@@ -69,43 +71,38 @@ module Gogetit
69
71
 
70
72
  case options['provider']
71
73
  when 'lxd'
72
- result = Gogetit.lxd.create(name, options.to_hash)
73
- Gogetit.set_result(result)
74
+ Gogetit::CLI.result = lxd.create(name, options.to_hash)
74
75
  when 'libvirt'
75
- result = Gogetit.libvirt.create(name, options.to_hash)
76
- Gogetit.set_result(result)
76
+ Gogetit::CLI.result = libvirt.create(name, options.to_hash)
77
77
  else
78
78
  abort('Invalid argument entered.')
79
79
  end
80
80
 
81
81
  # post-tasks
82
82
  if options['chef']
83
- knife_bootstrap(name, options[:provider], Gogetit.config, Gogetit.logger)
84
- update_databags(Gogetit.config, Gogetit.logger)
83
+ knife_bootstrap(name, options[:provider], config, logger)
84
+ update_databags(config, logger)
85
85
  end
86
86
  end
87
87
 
88
88
  desc 'destroy NAME', 'Destroy either a container or KVM instance.'
89
89
  method_option :chef, :type => :boolean, :desc => "Enable chef awareness."
90
90
  def destroy(name)
91
- # Let Gogetit recognize the provider.
92
- provider = Gogetit.get_provider_of(name)
91
+ provider = get_provider_of(name, providers)
93
92
  if provider
94
93
  case provider
95
94
  when 'lxd'
96
- result = Gogetit.lxd.destroy(name)
97
- Gogetit.set_result(result)
95
+ Gogetit::CLI.result = lxd.destroy(name)
98
96
  when 'libvirt'
99
- result = Gogetit.libvirt.destroy(name)
100
- Gogetit.set_result(result)
97
+ Gogetit::CLI.result = libvirt.destroy(name)
101
98
  else
102
99
  abort('Invalid argument entered.')
103
100
  end
104
101
  end
105
102
  # post-tasks
106
103
  if options['chef']
107
- knife_remove(name, Gogetit.logger) if options[:chef]
108
- update_databags(Gogetit.config, Gogetit.logger)
104
+ knife_remove(name) if options[:chef]
105
+ update_databags(config)
109
106
  end
110
107
  end
111
108
 
@@ -115,25 +112,24 @@ module Gogetit
115
112
  method_option :chef, :aliases => '-c', :type => :boolean, \
116
113
  :default => false, :desc => 'Chef awareness'
117
114
  def deploy(name)
118
- Gogetit.set_result(Gogetit.libvirt.deploy(name, options.to_hash))
115
+ Gogetit::CLI.result = libvirt.deploy(name, options.to_hash)
119
116
 
120
117
  # post-tasks
121
118
  if options['chef']
122
- knife_bootstrap(name, options[:provider], Gogetit.config, Gogetit.logger)
123
- update_databags(Gogetit.config, Gogetit.logger)
119
+ knife_bootstrap(name, options[:provider], config, logger)
120
+ update_databags(config, logger)
124
121
  end
125
122
  end
126
123
 
127
124
  desc 'release NAME', 'Release a node in MAAS'
128
125
  method_option :chef, :type => :boolean, :desc => "Enable chef awareness."
129
126
  def release(name)
130
- result = Gogetit.libvirt.release(name)
131
- Gogetit.set_result(result)
127
+ Gogetit::CLI.result = libvirt.release(name)
132
128
 
133
129
  # post-tasks
134
130
  if options['chef']
135
- knife_remove(name, Gogetit.logger) if options[:chef]
136
- update_databags(Gogetit.config, Gogetit.logger)
131
+ knife_remove(name) if options[:chef]
132
+ update_databags(config)
137
133
  end
138
134
  end
139
135
 
@@ -142,19 +138,22 @@ module Gogetit
142
138
  method_option :chef, :aliases => '-c', :type => :boolean, \
143
139
  :default => false, :desc => 'Chef awareness'
144
140
  def rebuild(name)
145
- # Let Gogetit recognize the provider.
146
- provider = Gogetit.get_provider_of(name)
141
+ provider = get_provider_of(name, providers)
147
142
  if provider
148
143
  case provider
149
144
  when 'lxd'
145
+ 1.upto(100) { print '_' }; puts
146
+ puts "Destroying #{name}.."
150
147
  invoke :destroy, [name]
151
148
  alias_name = YAML.load(
152
- Gogetit.get_result[:info][:config][:"user.user-data"]
149
+ Gogetit::CLI.result[:info][:config][:"user.user-data"]
153
150
  )['source_image_alias']
151
+ 1.upto(100) { print '_' }; puts
152
+ puts "Creating #{name}.."
154
153
  invoke :create, [name], :alias => alias_name
155
154
  when 'libvirt'
156
155
  invoke :release, [name]
157
- distro_name = Gogetit.get_result[:info][:machine]['distro_series']
156
+ distro_name = Gogetit::CLI.result[:info][:machine]['distro_series']
158
157
  invoke :deploy, [name], :distro => distro_name
159
158
  else
160
159
  abort('Invalid argument entered.')
@@ -162,8 +161,8 @@ module Gogetit
162
161
  end
163
162
  # post-tasks
164
163
  if options['chef']
165
- knife_remove(name, Gogetit.logger) if options[:chef]
166
- update_databags(Gogetit.config, Gogetit.logger)
164
+ knife_remove(name, logger) if options[:chef]
165
+ update_databags(config, logger)
167
166
  end
168
167
  end
169
168
  end
@@ -14,50 +14,50 @@ module Gogetit
14
14
  # TODO: It might be needed to disable one of logging devices according to satiation.
15
15
  STDOUT.sync = true
16
16
  log_to_stdout = Logger.new(STDOUT)
17
+ # Instantiate main objects
17
18
  @logger = Gogetit::MultiLogger.new(:loggers => log_to_stdout)
18
- logger.debug('Instantiate main objects..')
19
19
 
20
20
  @config = {}
21
21
 
22
- logger.debug('Defining home directory..')
22
+ # Define home directory
23
23
  user_gogetit_home = Dir.home + '/.gogetit'
24
24
  config[:user_gogetit_home] = user_gogetit_home
25
25
  if not File.directory?(user_gogetit_home)
26
- logger.debug('Creating home directory..')
26
+ logger.info('Creating home directory..')
27
27
  FileUtils.mkdir(user_gogetit_home)
28
28
  end
29
29
 
30
30
  # TODO: It can be used to provide different behavior according to consumer.
31
- logger.debug('Defining default consumer..')
31
+ # Define default consumer
32
32
  config[:consumer] = 'gogetit_cli'
33
33
 
34
- logger.debug('Defining log directory..')
34
+ # Define log directory
35
35
  log_dir = user_gogetit_home + '/log'
36
36
  config[:log_dir] = log_dir
37
37
  if not File.directory?(log_dir)
38
- logger.debug('Creating log directory..')
38
+ logger.info('Creating log directory..')
39
39
  FileUtils.mkdir(log_dir)
40
40
  end
41
41
 
42
- logger.debug('Define file log devices..')
42
+ # Define file log devices
43
43
  log_file = File.open(log_dir + '/debug.log', 'a')
44
44
  log_file.sync = true
45
45
  log_to_file = Logger.new(log_file)
46
46
  logger.add_logger(log_to_file)
47
47
 
48
- logger.debug('Defining logger..')
48
+ # Define logger
49
49
  # logger.datetime_format = "%Y-%m-%d %H:%M:%S"
50
50
  logger.progname = 'GoGetIt'
51
51
 
52
52
  lib_dir = Gem::Specification.find_by_name('gogetit').gem_dir + '/lib'
53
53
 
54
54
  config[:lib_dir] = lib_dir
55
- logger.debug('Loading GoGetIt default configuration..')
55
+ # Load GoGetIt default configuration
56
56
  conf_file = user_gogetit_home + '/gogetit.yml'
57
57
  if not File.exists?(conf_file)
58
58
  src = File.new(lib_dir + '/sample_conf/gogetit.yml')
59
59
  dst = Dir.new(user_gogetit_home)
60
- logger.debug('Copying GoGetIt default configuration..')
60
+ logger.info('Copying GoGetIt default configuration..')
61
61
  FileUtils.cp(src, dst)
62
62
  abort('Please define default configuration for GoGetIt at ~/.gogetit/gogetit.yml.')
63
63
  end
@@ -45,7 +45,11 @@ module Gogetit
45
45
 
46
46
  Logger::Severity.constants.each do |level|
47
47
  define_method(level.downcase) do |*args|
48
- @loggers.each { |logger| logger.send(level.downcase, args) }
48
+ if level == :ERROR
49
+ @loggers.each { |logger| logger.send(level.downcase, "\e[31m#{args}\e[0m") }
50
+ else
51
+ @loggers.each { |logger| logger.send(level.downcase, "\e[36m#{args}\e[0m") }
52
+ end
49
53
  end
50
54
 
51
55
  define_method("#{ level.downcase }?".to_sym) do
@@ -9,10 +9,23 @@ require 'timeout'
9
9
  module Gogetit
10
10
  module Util
11
11
  def run_command(cmd)
12
- logger.info("Calling <#{__method__.to_s}> to run #{cmd}")
12
+ logger.info("Calling <#{__method__.to_s}> to run '#{cmd}'")
13
13
  system(cmd)
14
14
  end
15
15
 
16
+ def get_provider_of(name, providers)
17
+ if providers[:lxd].container_exists?(name)
18
+ logger.info("Calling <#{__method__.to_s}>, It is a LXD container.")
19
+ return 'lxd'
20
+ elsif providers[:libvirt].domain_exists?(name)
21
+ logger.info("Calling <#{__method__.to_s}>, It is a KVM domain.")
22
+ return 'libvirt'
23
+ else
24
+ puts "#{name} is not found"
25
+ return nil
26
+ end
27
+ end
28
+
16
29
  def is_port_open?(ip, port)
17
30
  logger.info("Calling <#{__method__.to_s}> to check #{ip}:#{port}")
18
31
  begin
@@ -42,11 +55,11 @@ module Gogetit
42
55
  if res.code == "200"
43
56
  res.body
44
57
  else
45
- logger.info("Unable to reach the content of #{url}.")
58
+ logger.error("Unable to reach the content of #{url}.")
46
59
  false
47
60
  end
48
61
  else
49
- logger.info("Unable to reach the server: #{uri.host} or port: #{uri.port}.")
62
+ logger.error("Unable to reach the server: #{uri.host} or port: #{uri.port}.")
50
63
  false
51
64
  end
52
65
  end
@@ -144,19 +157,6 @@ module Gogetit
144
157
  end
145
158
  end
146
159
 
147
- def recognize_env
148
- thedir = 'lib/env'
149
- gateway = get_gateway(4)
150
- Dir.foreach(thedir) do |item|
151
- if item.match(/\.json$/)
152
- env_data = JSON.parse(File.read(thedir+'/'+item))
153
- if gateway =~ Regexp.new(env_data['regexp_pattern'])
154
- return env_data['name']
155
- end
156
- end
157
- end
158
- end
159
-
160
160
  def get_gateway(version)
161
161
  IO.popen("ip -#{version.to_s} route").read.each_line do |route|
162
162
  if route.include? 'default'
@@ -1,3 +1,3 @@
1
1
  module Gogetit
2
- VERSION = "0.12.3"
2
+ VERSION = "0.12.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gogetit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.3
4
+ version: 0.12.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Draper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-16 00:00:00.000000000 Z
11
+ date: 2018-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -218,7 +218,6 @@ files:
218
218
  - bin/gogetit
219
219
  - bin/setup
220
220
  - gogetit.gemspec
221
- - lib/etcd.rb
222
221
  - lib/executionhooks.rb
223
222
  - lib/gogetit.rb
224
223
  - lib/gogetit/cli.rb
@@ -257,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
257
256
  version: '0'
258
257
  requirements: []
259
258
  rubyforge_project:
260
- rubygems_version: 2.6.11
259
+ rubygems_version: 2.6.14
261
260
  signing_key:
262
261
  specification_version: 4
263
262
  summary: Libraries with a CLI tool for dealing with things like MAAS, LXD and Libvirt.
@@ -1,37 +0,0 @@
1
- require 'json'
2
- require 'etcd'
3
-
4
- module Etcd
5
- class Etcd
6
- attr_reader :etcd_conn
7
-
8
- include Gogetit::Util
9
-
10
- def initialize(config)
11
- @etcd_conn = Etcd::Client.connect(uris: config[:etcd_url]).connect
12
- end
13
-
14
- def env_name
15
- if etcd_conn.get('env_name') == nil or etcd_conn.get('env_name') == ''
16
- etcd_conn.set('env_name', recognize_env)
17
- etcd_conn.get('env_name')
18
- else
19
- etcd_conn.get('env_name')
20
- end
21
- end
22
-
23
- def import_env
24
- file = File.read('lib/env/'+env_name+'.json')
25
- env_data = JSON.parse(file)
26
- etcd_conn.set('env', env_data.to_json)
27
- end
28
-
29
- def env
30
- if ! etcd_conn.get('env')
31
- import_env
32
- else
33
- JSON.parse(etcd_conn.get('env'))
34
- end
35
- end
36
- end
37
- end