vagrant-tools 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d2898850a57d6033da0766d357602395ba69b1d
4
- data.tar.gz: 65271bbbca73801f2223bfebdd95965181be4ad6
3
+ metadata.gz: 223938120159ab83c8dcbd85f7b159cf985cd966
4
+ data.tar.gz: 116911824dbf2fe3b9b9290da10f9e73ff8b02d4
5
5
  SHA512:
6
- metadata.gz: da9a691708420a95151da7c89cc5c588c3aadac1f596215766cf19217df319a12cac5302049efee3b2448af0036df4f47f46c901b244cac99845efcdd9a78f57
7
- data.tar.gz: c1325e21edf6785ca70e9ebe54c14018afb805b910f9eff437e653f9cd5a44acb5be7ab38a1dba4c0de2953deaec21743b89e63e0d4563e3d99be1eb732cb163
6
+ metadata.gz: f12b48fad246a5c6bfe7ddefb1d989ecaa49d4a228f0b6bfe44f8af2745d49f8e9723fb9eab143eb09acc5f2d735b01046a035fbf4ae1c70636e86e327e47f70
7
+ data.tar.gz: 4efa91af35cb46613d1e4aad748b47f44728b73983f89a0c1895c992d0db6d117ef3aa04af481aef14196f181bf56e5cdd65eca7d73effec5aa8907b80b9f75c
data/README.md CHANGED
@@ -1,10 +1,30 @@
1
1
  # Vagrant::Tools
2
2
 
3
- Vagrant configuration management tool (via cli).
3
+ Vagrant cli configuration management tool. The missing tool for vagrant that allows you to control all your vagrant instances. Use cli command: `vgls` to list all vagrant configs (all Vagrantfiles), and `vgctl [target] [vgcmd]` to control configurations from outside the Vagrantfile dir.
4
4
 
5
- By using cli commands: `vgls` you can list all .vagrant configs on your system.
6
- Use `vgctl -t config -c (vagrant command)` to run vagrant commands from any path (working dir) on your system.
5
+ **Example**
7
6
 
7
+ ```bash
8
+ $ vgls
9
+ some-project (/Users/dblommesteijn/Programming/some-project)
10
+ - testing (vmid: cee72fc1-f647-4a2a-be48-04b4c1adeb2d)
11
+ - deployment (vmid: d46784f8-5a76-49eb-a511-1ab6c50d777d)
12
+ $ vgctl some-project status
13
+ Current machine states:
14
+
15
+ testing poweroff (virtualbox)
16
+ deployment poweroff (virtualbox)
17
+
18
+ This environment represents multiple VMs. The VMs are all listed
19
+ above with their current state. For more information about a specific
20
+ VM, run `vagrant status NAME`.
21
+ $ vgctl some-project up testing
22
+ ... starting the vm
23
+ $ vgctl some-project ssh testing
24
+ ... interactive ssh shell
25
+ $ vgctl some-project destroy testing
26
+ ... destroying vm [y/N]
27
+ ```
8
28
 
9
29
  ## Roadmap
10
30
 
@@ -14,18 +34,28 @@ Use `vgctl -t config -c (vagrant command)` to run vagrant commands from any path
14
34
  * List all vagrant configurations
15
35
  * Add basic controls for up/ halt/ destroy etc.
16
36
 
17
- * Version (0.1.0) Current
37
+ * Version (0.1.0)
18
38
 
19
39
  * Enable `vagrant ssh` interactive shell
20
40
 
21
41
  * Version (0.1.1)
22
42
 
23
- * Filter only active/ inactive
24
- * Add option to switch to .config path `vgctl -t target-box -c cd`
43
+ * Filter show only active
44
+ * Changed formatting of target and command
45
+ * Add option to switch to .config path `vgctl target-box shell`
46
+ * Filter show only zombies
47
+
48
+ * Current/ Master
49
+
50
+ * stub
25
51
 
26
52
 
27
53
  ## Installation
28
54
 
55
+ Install the latest version from the git repository, or install it via RubyGems
56
+
57
+ #### From Source
58
+
29
59
  **Clone the git repository to your local machine.**
30
60
 
31
61
  ```bash
@@ -40,6 +70,17 @@ rake install
40
70
  # vgls and vgctl will be available system wide
41
71
  ```
42
72
 
73
+ #### RubyGems
74
+
75
+ **Get the package from RubyGems**
76
+
77
+ ```bash
78
+ gem install vagrant-tools
79
+ ```
80
+
81
+
82
+ ## Usage
83
+
43
84
  **Run commands**
44
85
 
45
86
  ```bash
@@ -49,8 +90,6 @@ vgls
49
90
  vgctl
50
91
  ```
51
92
 
52
- ## Usage
53
-
54
93
  Verbose, this will output operations to STDOUT
55
94
 
56
95
  ```bash
@@ -65,8 +104,8 @@ Change target configuration (find operation), base from where .vagrant configs a
65
104
  *NOTE: this will force cache refresh*
66
105
 
67
106
  ```bash
68
- vgls -p `$HOME`/path/to/your/repos
69
- vgctl -p `$HOME`/path/to/your/repos
107
+ vgls -p $HOME/path/to/your/repos
108
+ vgctl -p $HOME/path/to/your/repos
70
109
  ```
71
110
 
72
111
  Target or list vagrant config relative to a given config
@@ -74,8 +113,8 @@ Target or list vagrant config relative to a given config
74
113
  *NOTE: duplicate config names will get a _n offset*
75
114
 
76
115
  ```bash
77
- vgls -t my-test-box
78
- vgctl -t my-test-box
116
+ vgls my-test-box
117
+ vgctl my-test-box
79
118
  ```
80
119
 
81
120
  Refresh cache, by default cache is stored at `$HOME/.vagrant-tools/settings.json`
@@ -89,12 +128,31 @@ vgctl -x
89
128
  Run a vagrant command (-c prepends `vagrant ` to all commands)
90
129
 
91
130
  ```bash
92
- vgctl -t my-test-box -c list-commands
131
+ vgctl my-test-box list-commands
93
132
  # runs `vagrant list-commands` in the path of `my-test-box`
94
133
  # ... etc
95
134
  ```
96
135
 
97
- Print help file
136
+ Show only active instances
137
+
138
+ ```bash
139
+ vgls -a
140
+ ```
141
+
142
+ Show detached instances (running without .config)
143
+
144
+ ```bash
145
+ vgls -z
146
+ ```
147
+
148
+ Launch a shell targeted at the 'cwd' relative to the target
149
+
150
+ ```bash
151
+ vgctl my-text-box shell
152
+ # launches a shell: pwd => $HOME/path/to/your/repos/my-text-box
153
+ ```
154
+
155
+ Print help message
98
156
 
99
157
  ```bash
100
158
  vgls -h
@@ -109,16 +167,16 @@ Listing all vagrant configs, and starting VM
109
167
  # list all configs
110
168
  vgls
111
169
  # execute `vagrant up` on path of `my-test-box`
112
- vgctl -t my-test-box -c up
113
- vgctl -t my-test-box -c ssh
170
+ vgctl my-test-box up
171
+ vgctl my-test-box ssh
114
172
  ```
115
173
 
116
174
  Destroy a running VM
117
175
 
118
176
  ```bash
119
- # list all configs
120
- vgls
177
+ # list running configs
178
+ vgls -a
121
179
  # execute `vagrant destroy` on path of `my-test-box`
122
- vgctl -t my-test-box -c destory
180
+ vgctl my-test-box destory
123
181
  ```
124
182
 
data/bin/vgctl CHANGED
@@ -1,84 +1,121 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require File.expand_path(File.dirname(__FILE__) + "/../lib/vagrant/tools")
4
- require 'optparse'
4
+ require 'getoptlong'
5
5
 
6
6
 
7
- def main(argv)
8
- # set configuration
7
+ def main()
8
+ # configure vgtools
9
9
  vgtools_cfg = Vagrant::Tools.config do |c|
10
10
  # defaults
11
11
  c.prefix = ENV["HOME"]
12
12
  c.verbose = false
13
- # getopts
14
- opt_parser = OptionParser.new do |opts|
15
- opts.banner = "Vagrant control, Usage: vgctl [options]"
13
+ c.output = {machine: false, long: false, only_active: false}
14
+ c.target = nil
15
+ c.cmd = nil
16
+ c.refresh_cache = false
17
+ # parse regular commands with getopt
18
+ begin
19
+ opts = GetoptLong.new(
20
+ ['--prefix', '-p', GetoptLong::REQUIRED_ARGUMENT],
21
+ ['--verbose', '-v', GetoptLong::NO_ARGUMENT],
22
+ ['--refresh-cache', '-x', GetoptLong::NO_ARGUMENT],
23
+ ['--only-active', '-a', GetoptLong::NO_ARGUMENT],
24
+ ['--help', '-h', GetoptLong::NO_ARGUMENT],
25
+ )
26
+ opts.each do |opt, arg|
27
+ case opt
28
+ when '--prefix'
29
+ c.prefix = arg
30
+ c.refresh_cache = true
31
+ when '--verbose'
32
+ c.verbose = true
33
+ when '--refresh-cache'
34
+ c.refresh_cache = true
35
+ when '--help'
36
+ puts <<-EOF
37
+ vgctl, version: #{Vagrant::Tools::VERSION}
16
38
 
17
- opts.on("-p", "--prefix DIR", String, "Define prefix (default $HOME)") do |v|
18
- c.prefix = v
19
- c.refresh_cache = true
20
- end
21
- opts.on("-v", "--verbose", "Verbose output") do |v|
22
- c.verbose = true
23
- end
24
- opts.on("-t", "--target TARGET", String, "Target specific Vagrantfile directory") do |v|
25
- c.target = v
26
- end
27
- opts.on("-c", "--cmd COMMAND", String, "Run vagrant command relative to -t TARGET (-c \"up\")") do |v|
28
- c.cmd = v
29
- end
30
- opts.on("-x", "--refresh-cache", "Refresh cached results") do |v|
31
- c.refresh_cache = v
32
- end
33
- opts.on_tail("-h", "--help", "Show this message") do
34
- puts opts
35
- exit 1
39
+ usage: vgctl [-pvxhc] [TARGET [COMMAND | shell]]
40
+
41
+ p : set lookup prefix (default $HOME)
42
+ v : verbose output
43
+ x : refresh cached results (.vagrant lookup)
44
+ TARGET : target a specific config (vagrantfile dir)
45
+ COMMAND : run vagrant command at target's `cwd`
46
+ shell : start a shell at target's `cwd`
47
+
48
+ h : show this help message
49
+ EOF
50
+ exit
51
+ else
52
+ puts "extra: " + opt + "|" + arg
53
+ end
36
54
  end
55
+ rescue GetoptLong::InvalidOption => e
56
+ exit
37
57
  end
38
- begin
39
- opt_parser.parse!
40
- rescue OptionParser::MissingArgument => e
41
- puts e.message
42
- exit 1
43
- rescue OptionParser::InvalidOption => e
44
- puts e.message
45
- exit 1
58
+
59
+ # parse commands and targets (outside getopt)
60
+ ARGV.each_with_index do |arg, i|
61
+ case i
62
+ when 0
63
+ c.target = arg
64
+ when 1
65
+ c.cmd = ARGV[1..-1].join(" ")
66
+ break
67
+ end
46
68
  end
47
69
  end
70
+ # configuration
71
+ cfg = Vagrant::Tools.get_config
72
+ # output buffer
73
+ output = Vagrant::Tools::Output.new(cfg)
74
+ output.append("running `#{File.basename(__FILE__)}` in verbose mode (disable by removing [-v])", :verbose)
75
+ output.append(cfg.to_s, :verbose)
76
+
48
77
  cfg = Vagrant::Tools.get_config
49
78
  verify = vgtools_cfg.verify? do |errors|
50
- # if cfg.target.nil? || cfg.target == "
51
- errors[:target] = "target does not exist" if cfg.target.nil?
52
- errors[:cmd] = "invalid" if cfg.cmd.nil? || cfg.cmd == ""
53
- # errors[:]
79
+ errors[:target] = "missing target option (print help -h)" if cfg.target.nil? || cfg.target == ""
80
+ errors[:cmd] = "missing command option (print help -h)" if cfg.cmd.nil? || cfg.cmd == ""
54
81
  end
55
82
  unless verify
56
- puts vgtools_cfg.error_messages
57
- exit 1
83
+ output.append(vgtools_cfg.error_messages)
84
+ output.render()
85
+ exit
58
86
  end
59
87
 
60
- # print verbose message
61
- if cfg.verbose
62
- puts "Running `#{File.basename(__FILE__)}` in verbose mode (disable by removing -v [options])"
88
+ # build root object from found configs (internal)
89
+ root = Vagrant::Tools::Root.new(cfg, output)
90
+ nodes = []
91
+
92
+ # visit configs
93
+ root.visit() do |node|
94
+ if node.is_a?(Vagrant::Tools::Orm::Config)
95
+ nodes << node if node.match_target?(cfg.target)
96
+ end
63
97
  end
64
-
65
- # find config root by target name (project root name)
66
- root = Vagrant::Tools::Root.new
67
- config = root.find_by_project_root(cfg.target)
68
- if config.nil?
69
- puts "target config not found"
70
- exit
71
- else
72
- puts "executing: #{cfg.cmd}" if cfg.verbose
98
+
99
+ begin
100
+ # verify config target, and run on one specific config
101
+ if nodes.empty?
102
+ output.append("target config not found: `#{cfg.target}`")
103
+ elsif nodes.size > 1
104
+ output.append("multiple configs found (this should never happen)")
105
+ else
106
+ output.append("executing: #{cfg.cmd}", :verbose)
107
+ nodes.first.exec_vagrant_command(cfg.cmd)
108
+ end
109
+ rescue Exception => e
110
+ output.append(e.message)
73
111
  end
74
- # execute command relative to config
75
- config.exec_vagrant_command(cfg.cmd)
112
+ output.render()
76
113
  end
77
114
 
78
-
115
+ # entry point
79
116
  if File.basename(__FILE__) == File.basename($0)
80
117
  begin
81
- main(ARGV)
118
+ main()
82
119
  rescue Interrupt
83
120
  exit 0
84
121
  end
data/bin/vgls CHANGED
@@ -1,71 +1,198 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require File.expand_path(File.dirname(__FILE__) + "/../lib/vagrant/tools")
4
- require 'optparse'
4
+ require 'getoptlong'
5
5
 
6
6
 
7
- def main(argv)
8
- # set configuration
7
+ def main()
8
+ # configure vgtools
9
9
  vgtools_cfg = Vagrant::Tools.config do |c|
10
10
  # defaults
11
11
  c.prefix = ENV["HOME"]
12
12
  c.verbose = false
13
- # getopts
14
- opt_parser = OptionParser.new do |opts|
15
- opts.banner = "Vagrant list, Usage: vgls [options]"
16
- opts.on("-p", "--prefix DIR", String, "Define prefix (default $HOME)") do |v|
17
- c.prefix = v
18
- c.refresh_cache = true
19
- end
20
- opts.on("-v", "--verbose", "Verbose output") do |v|
21
- c.verbose = true
22
- end
23
- opts.on("-t", "--target TARGET", String, "Target specific config (Vagrantfile dir)") do |v|
24
- c.target = v
25
- end
26
- opts.on("-x", "--refresh-cache", "Refresh cached results") do |v|
27
- c.refresh_cache = v
28
- end
29
- opts.on_tail("-h", "--help", "Show this message") do
30
- puts opts
31
- exit 1
32
- end
13
+ c.output = {machine: false, long: false, only_active: false, vagrantfile: false, zombies: false}
14
+ c.target = nil
15
+ c.cmd = nil
16
+ c.refresh_cache = false
33
17
 
34
- end
18
+ # parse regular commands with getopt
35
19
  begin
36
- opt_parser.parse!
37
- rescue OptionParser::InvalidOption => e
38
- puts e.message
39
- exit 1
40
- rescue OptionParser::MissingArgument => e
41
- puts e.message
42
- exit 1
20
+ opts = GetoptLong.new(
21
+ ['--prefix', '-p', GetoptLong::REQUIRED_ARGUMENT],
22
+ ['--verbose', '-v', GetoptLong::NO_ARGUMENT],
23
+ ['--refresh-cache', '-x', GetoptLong::NO_ARGUMENT],
24
+ ['--only-active', '-a', GetoptLong::NO_ARGUMENT],
25
+ ['--zombies', '-z', GetoptLong::NO_ARGUMENT],
26
+ ['--vagrantfile', '-V', GetoptLong::NO_ARGUMENT],
27
+ ['--help', '-h', GetoptLong::NO_ARGUMENT],
28
+ )
29
+ opts.each do |opt, arg|
30
+ case opt
31
+ when '--prefix'
32
+ c.prefix = arg
33
+ c.refresh_cache = true
34
+ when '--verbose'
35
+ c.verbose = true
36
+ when '--refresh-cache'
37
+ c.refresh_cache = true
38
+ when '--only-active'
39
+ c.output[:only_active] = true
40
+ when '--vagrantfile'
41
+ c.output[:vagrantfile] = true
42
+ when '--zombies'
43
+ c.output[:zombies] = true
44
+ when '--help'
45
+ puts <<-EOF
46
+ vgls, version: #{Vagrant::Tools::VERSION}
47
+
48
+ usage: vgls [-pvxh] [TARGET]
49
+
50
+ p : set lookup prefix (default $HOME)
51
+ v : verbose output
52
+ x : refresh cached results (.vagrant lookup)
53
+ V : output Vagrantfile
54
+ a : filter on running instances
55
+ z : filter on zombie instances (no .config and running)
56
+ TARGET : target a specific config (Vagrantfile dir)
57
+
58
+ h : show this help message
59
+ EOF
60
+ exit
61
+ end
62
+ end
63
+ rescue GetoptLong::InvalidOption => e
64
+ exit
65
+ end
66
+
67
+ # parse commands and targets (outside getopt)
68
+ ARGV.each_with_index do |arg, i|
69
+ case i
70
+ when 0
71
+ c.target = arg
72
+ when 1
73
+ c.cmd = ARGV[1..-1].join(" ")
74
+ break
75
+ end
43
76
  end
44
77
  end
45
78
 
79
+ # configuration
80
+ cfg = Vagrant::Tools.get_config
81
+ # output buffer
82
+ output = Vagrant::Tools::Output.new(cfg)
83
+ output.append("running `#{File.basename(__FILE__)}` in verbose mode (disable by removing [-v])", :verbose)
84
+ output.append(cfg.to_s, :verbose)
85
+
86
+ # verify errors
46
87
  verify = vgtools_cfg.verify? do |errors|
47
- # if
48
- # errors[:]
88
+ # TODO: not sure if this should be here!?
89
+ if cfg.output[:only_active] && cfg.output[:zombies]
90
+ errors[:zombies] = "cannot lookup both only-active and zombies"
91
+ end
49
92
  end
93
+ # act on errors
50
94
  unless verify
51
95
  puts vgtools_cfg.error_messages
52
96
  exit 1
53
97
  end
98
+ # build root object from found configs (internal)
99
+ root = Vagrant::Tools::Root.new(cfg, output)
100
+ nodes = []
101
+
102
+ if !cfg.output[:zombies]
103
+ # filter no-nodes
104
+ if !cfg.output[:only_active] && cfg.target.nil?
105
+ root.visit() do |node|
106
+ nodes << node
107
+ end
108
+ # filter `only-active`
109
+ elsif cfg.output[:only_active] && cfg.target.nil?
110
+ root.visit() do |node|
111
+ if node.is_a?(Vagrant::Tools::Orm::Config)
112
+ nodes << node if node.has_active_machines?
113
+ elsif node.is_a?(Vagrant::Tools::Orm::Machine)
114
+ nodes << node if node.has_active_providers?
115
+ elsif node.is_a?(Vagrant::Tools::Orm::Provider)
116
+ nodes << node if node.active?
117
+ end
118
+ end
119
+ # filter `target`
120
+ elsif !cfg.output[:only_active] && !cfg.target.nil?
121
+ root.visit() do |node|
122
+ if node.is_a?(Vagrant::Tools::Orm::Config)
123
+ nodes << node if node.match_target?(cfg.target)
124
+ elsif node.is_a?(Vagrant::Tools::Orm::Machine)
125
+ nodes << node if node.parent.match_target?(cfg.target)
126
+ elsif node.is_a?(Vagrant::Tools::Orm::Provider)
127
+ nodes << node if node.parent.match_target?(cfg.target)
128
+ end
129
+ end
130
+ # filter `target` and `only-active`
131
+ elsif cfg.output[:only_active] && !cfg.target.nil?
132
+ root.visit() do |node|
133
+ if node.is_a?(Vagrant::Tools::Orm::Config)
134
+ nodes << node if node.has_active_machines? && node.match_target?(cfg.target)
135
+ elsif node.is_a?(Vagrant::Tools::Orm::Machine)
136
+ nodes << node if node.has_active_providers? && node.parent.match_target?(cfg.target)
137
+ elsif node.is_a?(Vagrant::Tools::Orm::Provider)
138
+ nodes << node if node.active? && node.parent.match_target?(cfg.target)
139
+ end
140
+ end
141
+ end
142
+ # filter `zombies` detached VMs
143
+ else
144
+ running_vms = Vagrant::Tools::Helper.get_running_vms
145
+ root.visit() do |node|
146
+ if node.is_a?(Vagrant::Tools::Orm::Provider)
147
+ if !node.process.nil?
148
+ running_vms.delete(node.process.pid)
149
+ end
150
+ end
151
+ end
152
+ if running_vms.size > 0
153
+ running_vms.each do |pid, vm|
154
+ output.append("#{vm.pid}, #{vm.cmdline}")
155
+ end
156
+ end
157
+ end
54
158
 
55
- cfg = Vagrant::Tools.get_config
56
- if cfg.verbose
57
- puts "Running `#{File.basename(__FILE__)}` in verbose mode (disable by removing -v [options])"
159
+ # determine how the nodes are output
160
+ nodes.each do |node|
161
+ # show vagrantfile
162
+ if cfg.output[:vagrantfile]
163
+ if node.is_a?(Vagrant::Tools::Orm::Config)
164
+ output.append(node)
165
+ output.append("-- Vagrantfile (#{node.vagrantfile}): ---------------------")
166
+ output.append(node.vagrantfile_contents)
167
+ end
168
+ else
169
+ # print all nodes
170
+ if node.is_a?(Vagrant::Tools::Orm::Config)
171
+ output.append(node)
172
+ elsif node.is_a?(Vagrant::Tools::Orm::Machine)
173
+ output.append("- #{node.name} ", :nonewline)
174
+ elsif node.is_a?(Vagrant::Tools::Orm::Provider)
175
+ output.append("(#{node.status_s})")
176
+ end
177
+ end
58
178
  end
59
179
 
60
- # output lists
61
- root = Vagrant::Tools::Root.new
62
- puts root.to_outputs
180
+ # append cache status
181
+ if root.cache.cache_old?
182
+ a = root.cache.cache_time_a
183
+ output.append("cache is %dd, %dh, %dm, and %ds old (consider refreshing -x)" % a)
184
+ elsif cfg.verbose
185
+ a = root.cache.cache_time_a
186
+ output.append("cache is %dd, %dh, %dm, and %ds old" % a, :verbose)
187
+ end
188
+ # print output
189
+ output.render()
63
190
  end
64
191
 
65
-
192
+ # entry point
66
193
  if File.basename(__FILE__) == File.basename($0)
67
194
  begin
68
- main(ARGV)
195
+ main()
69
196
  rescue Interrupt
70
197
  exit 0
71
198
  end
@@ -10,26 +10,32 @@ module Vagrant
10
10
  @path = path
11
11
  @cfg = Vagrant::Tools.get_config
12
12
  unless File.exists?(@path)
13
- puts "Creating `#{path}`" if @cfg.verbose
13
+ puts "creating `#{path}`" if @cfg.verbose
14
14
  FileUtils.mkpath(@path)
15
15
  end
16
16
  @filename = "#{@path}/settings.json"
17
+ @delta = 0.0
17
18
  end
18
19
 
19
20
  def get_config
20
21
  return {} unless File.exists?(@filename)
21
- #TODO: read from file
22
22
  begin
23
+ puts "reading cache file: `#{@filename}`" if @cfg.verbose
23
24
  json = JSON.parse(File.read(@filename), {symbolize_names: true})
24
- json[:configs]
25
- rescue
25
+ c_time = Time.at(json[:configs_date])
26
+ puts "cache time: `#{c_time}`" if @cfg.verbose
27
+ @delta = Cache.time_delta(c_time) #if json.include?(:configs_date)
28
+ return json[:configs]
29
+ rescue Exception => e
30
+ puts e.message
31
+ puts e
26
32
  return {}
27
33
  end
28
34
  end
29
35
 
30
36
  def set_config(dirs)
31
- puts "Writing to `#{@filename}`" if @cfg.verbose
32
- config_paths = { configs: dirs.flat_map{|k,v| v.map(&:config_path)} }
37
+ puts "writing to `#{@filename}`" if @cfg.verbose
38
+ config_paths = { configs_date: Time.now.to_i, configs: dirs.flat_map{|k,v| v.map(&:config_path)} }
33
39
  flat_json = JSON.pretty_generate(config_paths)
34
40
  puts flat_json if @cfg.verbose
35
41
  File.open(@filename,"w") do |f|
@@ -37,6 +43,25 @@ module Vagrant
37
43
  end
38
44
  end
39
45
 
46
+ def cache_old?
47
+ @delta > 864000.0
48
+ end
49
+
50
+ def cache_time_a
51
+ mm, ss = @delta.divmod(60)
52
+ hh, mm = mm.divmod(60)
53
+ dd, hh = hh.divmod(24)
54
+ [dd, hh, mm, ss]
55
+ end
56
+
57
+ protected
58
+
59
+ class << self
60
+ def time_delta(time)
61
+ now = Time.now
62
+ Time.now - time
63
+ end
64
+ end
40
65
  end
41
66
 
42
67
  end
@@ -7,11 +7,6 @@ module Vagrant
7
7
 
8
8
  def initialize
9
9
  @errors = {}
10
- self.prefix = ENV["HOME"]
11
- self.verbose = false
12
- self.output = {machine: false, long: false}
13
- self.target = nil
14
- self.cmd = nil
15
10
  end
16
11
 
17
12
  def verify?(&block)
@@ -29,8 +24,19 @@ module Vagrant
29
24
  end
30
25
 
31
26
  def error_messages
32
- @errors.map{|k,v| "Error `#{k}`: #{v}"}
27
+ @errors.map{|k,v| "error `#{k}`: #{v}"}
33
28
  end
29
+
30
+ def to_hash
31
+ hash = {}
32
+ instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var) }
33
+ hash
34
+ end
35
+
36
+ def to_s
37
+ self.to_hash.map{|k,v| "#{k}: #{v}"}.join("\n")
38
+ end
39
+
34
40
 
35
41
  end
36
42
 
@@ -1,25 +1,31 @@
1
1
  module Vagrant
2
2
  module Tools
3
-
4
3
  class Helper
5
-
6
4
  class << self
5
+ def iterate_processes(&block)
6
+ Sys::ProcTable.ps(&block)
7
+ end
7
8
 
8
- # collecting user input, continue if input == y else exit
9
- # def collect_input_yn_exit!(action)
10
- # STDOUT.write "#{action} are you sure [y/N]? "
11
- # STDOUT.flush
12
- # a = gets
13
- # case a.downcase
14
- # when "y\n"
15
- # return true
16
- # else
17
- # exit
18
- # end
19
- # end
9
+ def get_running_vms
10
+ vms = []
11
+ Helper.iterate_processes do |p|
12
+ if p.cmdline.include?("VBoxHeadless")
13
+ vms << p
14
+ #elsif... add other providers
15
+ end
16
+ end
17
+ # remove all non-vagrant boxes
18
+ # vms.reject{|vm| vm.cmdline.match(/--startvm ([0-9a-f\-]+)/).size <= 0}#.map{|t| {t.pid => t}}
20
19
 
20
+ ret = {}
21
+ vms.each do |vm|
22
+ tmp = vm.cmdline.match(/--startvm ([0-9a-f\-]+)/)
23
+ next if tmp.size <= 0
24
+ ret[vm.pid] = vm
25
+ end
26
+ ret
27
+ end
21
28
  end
22
-
23
29
  end
24
30
  end
25
31
  end
@@ -6,15 +6,21 @@ module Vagrant
6
6
 
7
7
  class Config
8
8
 
9
- attr_accessor :name, :project_root, :offset, :config_path
9
+ ENV_SHELL = ENV["SHELL"]
10
10
 
11
- def initialize(config_path)
12
- @cfg = Vagrant::Tools.get_config
11
+ attr_accessor :name, :project_root, :offset, :config_path, :vagrantfile, :parent
12
+
13
+ def initialize(cfg, output, parent, config_path)
14
+ @cfg = cfg
15
+ @output = output
16
+ self.parent = parent
13
17
  self.config_path = config_path
14
18
  self.project_root = File.absolute_path("#{config_path}/../")
15
- @machines = Dir["#{self.config_path}/machines/*"].flat_map{|t| Machine.new(t)}
19
+ self.vagrantfile = File.absolute_path("#{self.project_root}/Vagrantfile")
20
+ @machines = Dir["#{self.config_path}/machines/*"].flat_map{|t| Machine.new(@cfg, @output, self, t)}
16
21
  self.name = File.basename(File.absolute_path("#{config_path}/../"))
17
22
  self.offset = 0
23
+ @output.append("found config: `#{self.config_path}`", :verbose)
18
24
  end
19
25
 
20
26
  def project_root_name
@@ -26,9 +32,18 @@ module Vagrant
26
32
  "#{self.project_root_name}#{o}"
27
33
  end
28
34
 
29
- def exec_vagrant_command(cmd)
30
- cmd = "(cd #{self.project_root} && vagrant #{cmd})"
31
- puts cmd if @cfg.verbose
35
+ def exec_vagrant_command(command)
36
+ # TODO: add some cmd check?
37
+ cmd = ""
38
+ case command
39
+ when 'shell'
40
+ raise "no shell found (in $SHELL)" if ENV_SHELL.nil? || ENV_SHELL == ""
41
+ cmd = "(cd #{self.project_root} && #{ENV_SHELL})"
42
+ else
43
+ cmd = "(cd #{self.project_root} && vagrant #{command})"
44
+ end
45
+ @output.append(cmd, :verbose)
46
+ # system call to command
32
47
  system(cmd)
33
48
  end
34
49
 
@@ -36,11 +51,43 @@ module Vagrant
36
51
  @machines.map(&:name)
37
52
  end
38
53
 
54
+ def vagrantfile_contents
55
+ return nil unless File.exists?(self.vagrantfile)
56
+ File.read(self.vagrantfile)
57
+ end
58
+
59
+ def has_active_machines?
60
+ @machines.map(&:has_active_providers?).include?(true)
61
+ end
62
+
63
+ def match_target?(target)
64
+ self.project_root_name_with_offset == target
65
+ end
66
+
67
+ def pretty_name
68
+ "#{self.project_root_name_with_offset} (#{@project_root})"
69
+ end
70
+
39
71
  def to_outputs
40
72
  machines = @machines.map(&:to_outputs).join("")
41
- "#{self.project_root_name_with_offset} (#{@project_root})\n#{machines}"
73
+ if !@cfg.output[:only_active]
74
+ "#{self.project_root_name_with_offset} (#{@project_root})\n#{machines}"
75
+ else
76
+ if !machines.empty?
77
+ "#{self.project_root_name_with_offset} (#{@project_root})\n#{machines}"
78
+ end
79
+ end
42
80
  end
43
81
 
82
+ def visit(&block)
83
+ block.call(self)
84
+ @machines.each do |machine|
85
+ machine.visit(&block)
86
+ end
87
+ end
88
+
89
+ protected
90
+
44
91
  end
45
92
 
46
93
  end
@@ -4,12 +4,15 @@ module Vagrant
4
4
 
5
5
  class Machine
6
6
 
7
- attr_accessor :name
7
+ attr_accessor :name, :parent
8
8
 
9
- def initialize(machine_path)
9
+ def initialize(cfg, output, parent, machine_path)
10
+ @cfg = cfg
11
+ @output = output
12
+ self.parent = parent
10
13
  @machine_path = machine_path
11
14
  self.name = File.basename(machine_path)
12
- @provider = Dir["#{@machine_path}/*"].flat_map{|t| Provider.new(t)}
15
+ @provider = Dir["#{@machine_path}/*"].flat_map{|t| Provider.new(@cfg, @output, parent, t)}
13
16
  end
14
17
 
15
18
  def ids
@@ -20,10 +23,15 @@ module Vagrant
20
23
  @provider.map(&:process)
21
24
  end
22
25
 
23
- def to_outputs
24
- provider = @provider.map(&:to_outputs).join("")
25
- return "- #{self.name} (no provider found)\n" if provider.empty?
26
- "- #{self.name} (#{provider})\n"
26
+ def has_active_providers?
27
+ @provider.map(&:active?).include?(true)
28
+ end
29
+
30
+ def visit(&block)
31
+ block.call(self)
32
+ @provider.each do |provider|
33
+ provider.visit(&block)
34
+ end
27
35
  end
28
36
 
29
37
  end
@@ -6,32 +6,41 @@ module Vagrant
6
6
 
7
7
  class Provider
8
8
 
9
- attr_accessor :id
9
+ attr_accessor :id, :parent
10
10
 
11
- def initialize(provider_path)
11
+ def initialize(cfg, output, parent, provider_path)
12
+ @cfg = cfg
13
+ @output = output
14
+ self.parent = parent
12
15
  @provider_path = provider_path
13
16
  @id_file = "#{@provider_path}/id"
14
17
  self.id = File.read(@id_file) if self.valid?
15
18
  @process = nil
19
+ self.process
16
20
  end
17
21
 
18
22
  def valid?
19
23
  File.exists?(@id_file)
20
24
  end
21
25
 
26
+ def active?
27
+ !@process.nil?
28
+ end
29
+ # alias_method :has_active?, :active?
30
+
22
31
  def process
23
- return @process unless @process.nil?
24
- Sys::ProcTable.ps do |p|
32
+ return @process if !@process.nil? || self.id.nil?
33
+ Helper.iterate_processes do |p|
25
34
  @process = p if p.cmdline.include?(self.id)
26
35
  end
27
36
  @process
28
37
  end
29
38
 
30
- def to_outputs
39
+ def status_s
31
40
  ret = []
32
41
  if self.valid?
33
42
  p = self.process
34
- if p.nil?
43
+ if !self.active?
35
44
  ret << "vmid: #{self.id}"
36
45
  else
37
46
  ret << "pid: #{p.pid}"
@@ -42,6 +51,10 @@ module Vagrant
42
51
  ret.join
43
52
  end
44
53
 
54
+ def visit(&block)
55
+ block.call(self)
56
+ end
57
+
45
58
  end
46
59
 
47
60
  end
@@ -0,0 +1,36 @@
1
+ module Vagrant
2
+ module Tools
3
+
4
+ class Output
5
+
6
+ def initialize(config)
7
+ @cfg = config
8
+ @buffer = []
9
+ end
10
+
11
+ def render()
12
+ @buffer.each do |buffer|
13
+ if (!buffer.last.include?(:verbose) || @cfg.verbose)
14
+ STDOUT.write(buffer.first)
15
+ STDOUT.write("\n") if !buffer.last.include?(:nonewline)
16
+ STDOUT.flush
17
+ end
18
+ end
19
+ end
20
+
21
+ def append(node, options=[])
22
+ options = [options] unless options.is_a?(Array)
23
+ if node.is_a?(Vagrant::Tools::Orm::Config)
24
+ @buffer << [node.pretty_name, options]
25
+ elsif node.is_a?(String)
26
+ @buffer << [node, options]
27
+ elsif node.is_a?(Array)
28
+ node.each do |n|
29
+ @buffer << [n, options]
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -6,29 +6,32 @@ module Vagrant
6
6
 
7
7
  class Root
8
8
 
9
+ attr_accessor :cache
10
+
9
11
  LOOKUP_DIR = ".vagrant"
10
12
 
11
- def initialize
12
- @cfg = Vagrant::Tools.get_config
13
+ def initialize(cfg, output)
14
+ @cfg = cfg
15
+ @output = output
13
16
  @dirs = {}
17
+ self.cache = Cache.new
14
18
  self.find_vagrant_configs
15
19
  end
16
20
 
17
- def find_by_project_root(project_root_name)
18
- self.get_config_without_offset(project_root_name)
19
- end
21
+ # def find_by_project_root(project_root_name)
22
+ # self.get_config_without_offset(project_root_name)
23
+ # end
20
24
 
21
25
  def find_vagrant_configs
22
26
  unless @dirs.empty?
23
- puts "Config already loaded (use -x to force reload)" if @cfg.verbose
27
+ @output.append("config already loaded (use -x to force reload)", :verbose)
24
28
  return @dirs
25
29
  end
26
30
  prefix = @cfg.prefix
27
- cache = Cache.new
28
- cache_configs = cache.get_config
31
+ cache_configs = self.cache.get_config
29
32
  # break if config found (unless refreshing)
30
33
  if !cache_configs.empty? && !@cfg.refresh_cache
31
- puts "Reading config from cache" if @cfg.verbose
34
+ @output.append("reading config from cache", :verbose)
32
35
  cache_configs.each do |config|
33
36
  # create new config instance
34
37
  self.add_config_dirs(config)
@@ -37,34 +40,68 @@ module Vagrant
37
40
  end
38
41
  # findin configs via find
39
42
  cmd = "find \"#{prefix}\" -type d -name \"#{LOOKUP_DIR}\""
40
- puts "Finding vagrant configs: `#{cmd}`..." if @cfg.verbose
43
+ @output.append("finding vagrant configs: `#{cmd}`...", :verbose)
41
44
  Open3.popen3(cmd) do |stdin, stdout, stderr|
42
45
  stdin.close_write
43
- stdout.read.split("\n").each do |config|
46
+ stdout.read.split("\n").each do |config_file|
44
47
  # create new config instance
45
- self.add_config_dirs(config)
48
+ self.add_config_dirs(config_file)
46
49
  end
47
50
  stderr.close_read
48
51
  end
49
- cache.set_config(@dirs)
52
+ self.cache.set_config(@dirs)
50
53
  self
51
54
  end
52
55
 
53
- def to_outputs
54
- ret = []
55
- if @cfg.target.nil?
56
- # print all
57
- ret << @dirs.flat_map{|k,t| t.map(&:to_outputs)}
58
- else
59
- ret << self.get_config_without_offset(@cfg.target).to_outputs
56
+ # def to_outputs
57
+ # ret = []
58
+ # if @cfg.target.nil?
59
+ # # print all
60
+ # ret << @dirs.flat_map{|k,t| t.map(&:to_outputs)}
61
+ # else
62
+ # ret << self.get_config_without_offset(@cfg.target).to_outputs
63
+ # end
64
+ # ret.join
65
+ # end
66
+
67
+ # if @cfg.target.nil?
68
+ # configs = @dirs.flat_map{|k,t| t}
69
+ # else
70
+ # configs = [self.get_config_without_offset(@cfg.target)]
71
+ # end
72
+
73
+ # # filter `target`
74
+ # elsif !cfg.target.nil?
75
+ # if node.is_a?(Vagrant::Tools::Orm::Root)
76
+ # nodes << node
77
+ # end
78
+
79
+ def visit(&block)
80
+ configs = @dirs.flat_map{|k,t| t}
81
+ block.call(self)
82
+ configs.each do |config|
83
+ config.visit(&block)
60
84
  end
61
- ret.join
62
85
  end
63
86
 
87
+ def to_s
88
+ "root"
89
+ end
90
+
91
+ def has_active?
92
+ false
93
+ end
94
+
95
+ def match_target?(target)
96
+ false
97
+ end
98
+
99
+
100
+
64
101
  protected
65
102
 
66
103
  def add_config_dirs(config)
67
- orm_config = Orm::Config.new(config)
104
+ orm_config = Orm::Config.new(@cfg, @output, self, config)
68
105
  n = orm_config.project_root_name
69
106
  @dirs[n] ||= []
70
107
  orm_config.offset = @dirs[n].size + 1 if @dirs[n].size > 0
@@ -72,23 +109,6 @@ module Vagrant
72
109
  nil
73
110
  end
74
111
 
75
- def get_config_without_offset(name)
76
- tmp = name.split("_")
77
- if tmp.size > 1
78
- n = tmp.first
79
- o = tmp.last.to_i - 1
80
- if @dirs.include?(n)
81
- return @dirs[n][o]
82
- end
83
- # elsif @dirs[n].nil?
84
- # puts @dirs.inspect
85
- else
86
- n = name
87
- return @dirs[n].first
88
- end
89
- nil
90
- end
91
-
92
112
  end
93
113
  end
94
114
  end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Tools
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
data/lib/vagrant/tools.rb CHANGED
@@ -2,6 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + "/tools/config")
2
2
  require File.expand_path(File.dirname(__FILE__) + "/tools/cache")
3
3
  require File.expand_path(File.dirname(__FILE__) + "/tools/root")
4
4
  require File.expand_path(File.dirname(__FILE__) + "/tools/version")
5
+ require File.expand_path(File.dirname(__FILE__) + "/tools/output")
5
6
  require File.expand_path(File.dirname(__FILE__) + "/tools/helper")
6
7
  require File.expand_path(File.dirname(__FILE__) + "/tools/orm/config")
7
8
  require File.expand_path(File.dirname(__FILE__) + "/tools/orm/machine")
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Vagrant::Tools::VERSION
9
9
  spec.authors = ["Dennis Blommesteijn"]
10
10
  spec.email = ["dennis@blommesteijn.com"]
11
- spec.summary = %q{Tools for Vagrant}
12
- spec.description = %q{Tools for Vagrant}
13
- spec.homepage = ""
11
+ spec.summary = %q{Vagrant CLI configuration management tool for Unix}
12
+ spec.description = %q{Vagrant CLI configuration management tool for Unix}
13
+ spec.homepage = "https://github.com/dblommesteijn/vagrant-tools"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Blommesteijn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-06 00:00:00.000000000 Z
11
+ date: 2014-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sys-proctable
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Tools for Vagrant
55
+ description: Vagrant CLI configuration management tool for Unix
56
56
  email:
57
57
  - dennis@blommesteijn.com
58
58
  executables:
@@ -75,10 +75,11 @@ files:
75
75
  - lib/vagrant/tools/orm/config.rb
76
76
  - lib/vagrant/tools/orm/machine.rb
77
77
  - lib/vagrant/tools/orm/provider.rb
78
+ - lib/vagrant/tools/output.rb
78
79
  - lib/vagrant/tools/root.rb
79
80
  - lib/vagrant/tools/version.rb
80
81
  - vagrant-tools.gemspec
81
- homepage: ''
82
+ homepage: https://github.com/dblommesteijn/vagrant-tools
82
83
  licenses:
83
84
  - MIT
84
85
  metadata: {}
@@ -101,5 +102,5 @@ rubyforge_project:
101
102
  rubygems_version: 2.2.2
102
103
  signing_key:
103
104
  specification_version: 4
104
- summary: Tools for Vagrant
105
+ summary: Vagrant CLI configuration management tool for Unix
105
106
  test_files: []