vagrant-templated 0.1.5 → 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: 196467dafc7d0fb838929cfdfebc9052cd810801
4
- data.tar.gz: 25537cefd79b74d9b6ca8111c909daa2c8750891
3
+ metadata.gz: 0de3022048ea907bdc99c4a5e89d69d851ed14d8
4
+ data.tar.gz: 7d3b70f7a0b47f25e332c2d03458e8e34e2e557e
5
5
  SHA512:
6
- metadata.gz: 68e1887a3e31f12cde65436d300b8a211df90f13f328e63ee9792a3fa758d04c31c4f472f7aa26700819bd1999e332090fcd2247077f8a483892149265450767
7
- data.tar.gz: 74df6feaef448cf17bdf717e3dcd4655449f4dda4f8f58807b4ec1a8684db77d30b52338c32a62f2a2da62cc923858f9b4e9eed47eed65a2d967d9a6e3679fea
6
+ metadata.gz: 6443c74321a96df9b12863f43d88a8d5bb1abdc1180bfb74c02e0c192db84b23cf787818ff3d9d04d7b716a0ef509b3c9d66792fb75215b0b053c4538556032e
7
+ data.tar.gz: c54d2c5a4171e52787923a05528f194f89e0be7c4c23af415179f0794a35cc7b38ee505d6498983d2931a6521953f8f5df2d6bd4d00268fa30f04a7936aceff0
data/Berksfile CHANGED
@@ -1,5 +1,5 @@
1
- # Berksfile generated with https://github.com/afaundez/vagrant-templated
2
-
1
+ # vagrant-templated berksfile for vagrant-plugin 1.9
2
+ # check https://github.com/afaundez/vagrant-templated for more options
3
3
  source 'https://supermarket.chef.io'
4
4
 
5
5
  cookbook 'apt', '~> 6.1.0'
@@ -6,6 +6,24 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.2.0] - 2017-08-12
10
+ ### Added
11
+ - Node.js 6 template
12
+ - Cat option
13
+ ### Changed
14
+ - Refactor config directories
15
+ - Lock chef version for all templates
16
+ - Move version from template name to a new option. If version not provided, using largest version
17
+ - Refactor loading and accessing template attributes
18
+ - Creating all error in vagrant templated module
19
+ - Update vagrantfile and berksfile
20
+ - Replace suffix with output option
21
+ - Update readme
22
+ ## Removed
23
+ - Vagrantfile's magic comments
24
+ - Extra empty lines in templates
25
+
26
+
9
27
  ## [0.1.5] - 2017-08-07
10
28
  ### Added
11
29
  - Update Vagrantfile from vagrant-templated
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
- [![GitHub version](https://badge.fury.io/gh/afaundez%2Fvagrant-templated.svg)](https://badge.fury.io/gh/afaundez%2Fvagrant-templated)
2
1
  [![Gem Version](https://badge.fury.io/rb/vagrant-templated.svg)](https://badge.fury.io/rb/vagrant-templated)
3
2
 
4
3
  # VAGRANT TEMPLATED PLUGIN
5
4
 
6
- Vagrant Templated is [Vagrant](https://www.vagrantup.com/downloads.html) plugin that add a new vagrant action to start a new Project. You will be able to choose between templates and start with a project with a Vagrantfile (and Berksfile) with cookbooks and configurations.
5
+ Vagrant Templated is [Vagrant](https://www.vagrantup.com/downloads.html) plugin that add a new vagrant action to start a new Project. You will be able to choose between templates and start with a project with a Vagrantfile (and Berksfile) with cookbooks and configurations already configured.
7
6
 
8
7
  I started this gem because every time I start a new project, for production or just testing something, after the traditional vagrant init I had look in others projects looking for a specific configuration (for example, a Rails 5 project)
9
8
 
@@ -15,7 +14,7 @@ With vagrant:
15
14
  vagrant plugin install vagrant-templated
16
15
  ```
17
16
 
18
- To use the Vagranfile and Berksfile created, you will need [chefdk](https://downloads.chef.io/chefdk), [vagrant-berkshelf](https://github.com/berkshelf/vagrant-berkshelf) and [vagrant-omnibus](https://github.com/chef/vagrant-omnibus).
17
+ To use the Vagranfile and Berksfile created, you will need [chefdk](https://downloads.chef.io/chefdk), [vagrant-berkshelf](https://github.com/berkshelf/vagrant-berkshelf) and [vagrant-omnibus](https://github.com/chef/vagrant-omnibus).
19
18
 
20
19
 
21
20
  ## Usage
@@ -23,23 +22,29 @@ To use the Vagranfile and Berksfile created, you will need [chefdk](https://down
23
22
  The new init action runs like this:
24
23
 
25
24
  ```shell
26
- vagrant templated init <template>
25
+ vagrant templated init <template> [version]
27
26
  ```
28
27
 
29
28
  The templates available at this moment are:
30
29
 
31
- - `base`: apt and timezone update
32
- - `vagrant-plugin`: base and ruby 2.3
33
- - `django1.11`: base, python 2/3 (pyenv)
34
- - `rails5`: base, ruby 2.4 (rbenv), node and postgresql.
30
+ - `base`
31
+ - `1.0`: apt and timezone update
32
+ - `vagrant-plugin`:
33
+ - `1.9`: base, rbenv, ruby 2.3 and bundler
34
+ - `django`:
35
+ - `1.11`: base, pyenv and python 2/3
36
+ - `rails`:
37
+ - `5.1`: base, rbenv, ruby 2.4, bundler, node and postgresql
38
+ - `nodejs`:
39
+ - `6.11`: base, nodenv and node 6.11
35
40
 
36
- In both cases a Vagrantfile and a Berksfile will be generated where you executing the command.
37
-
38
- Then, you just should `vagrant up` and enter the matrix.
41
+ In both cases a Vagrantfile and a Berksfile will be generated where you executing the command. You should adapt these files to you projects, but it should work as a starting point.
39
42
 
40
43
  ## Development
41
44
 
42
- After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake spec` to run the tests. Run `bundle exec vagrant templated init -h` and help will arise.
45
+ After checking out the repo, you just should `vagrant up` and enter the matrix. Everything is at `/vagrant`.
46
+
47
+ Inside the box run `bundle install` to install dependencies. Then, run `bundle exec rake spec` to run the tests. Run `bundle exec vagrant templated init -h` and help will arise.
43
48
 
44
49
  ## Contributing
45
50
 
@@ -47,11 +52,9 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/afaund
47
52
 
48
53
  ## TODO list
49
54
 
50
- - Refactor template management in a isolated component, making plugin more skinnier
51
55
  - Add multiple choices for some components. For example, rails5 add postgresql by default, but it could be mysql or sqlite3 (that what I have used). Maybe it would better to add everything and leave everything commented but one default.
52
56
  - Display ad hoc tips when new files are created
53
57
  - Add tips as a command
54
- - Refactor versions, maybe add version to the options
55
58
 
56
59
  ## License
57
60
 
@@ -1,7 +1,5 @@
1
- # -*- mode: ruby -*-
2
- # vi: set ft=ruby :
3
- # Vagrantfile generated with https://github.com/afaundez/vagrant-templated
4
-
1
+ # vagrant-templated vagrantfile for vagrant-plugin 1.9
2
+ # check https://github.com/afaundez/vagrant-templated for more options
5
3
  ["vagrant-omnibus", "vagrant-berkshelf"].each do |plugin_name|
6
4
  unless Vagrant.has_plugin? plugin_name
7
5
  raise "#{plugin_name} plugin is required. Please run `vagrant plugin install #{plugin_name}`"
@@ -10,8 +8,7 @@ end
10
8
 
11
9
  Vagrant.configure('2') do |config|
12
10
  config.vm.box = 'bento/ubuntu-16.04'
13
-
14
- config.omnibus.chef_version = 'latest'
11
+ config.omnibus.chef_version = '13.2.20'
15
12
  config.vm.provision :chef_solo do |chef|
16
13
  chef.add_recipe 'apt'
17
14
  chef.add_recipe 'ruby_build'
@@ -0,0 +1,16 @@
1
+ omnibus:
2
+ chef_version: 13.2.20
3
+ berkself:
4
+ cookbooks:
5
+ apt: ~> 6.1.3
6
+ timezone_iii: ~> 1.0.4
7
+ chef:
8
+ recipes:
9
+ - apt
10
+ - timezone_iii
11
+ json:
12
+ timezone_iii: UTC
13
+ require:
14
+ vagrant-plugins:
15
+ - vagrant-omnibus
16
+ - vagrant-berkshelf
@@ -0,0 +1,31 @@
1
+ omnibus:
2
+ chef_version: 13.2.20
3
+ network:
4
+ private_network:
5
+ ip: 192.168.33.101
6
+ comments:
7
+ - "TIP: the static IP must not collide with other machines on the same network"
8
+ - "TIP: Make sure to have something like this on your settings.py:"
9
+ - "ALLOWED_HOSTS = ['192.168.33.101', 'localhost', '127.0.0.1']"
10
+ berkself:
11
+ cookbooks:
12
+ apt: ~> 6.1.3
13
+ timezone_iii: ~> 1.0.4
14
+ pyenv: ~> 0.1.4
15
+ chef:
16
+ recipes:
17
+ - apt
18
+ - timezone_iii
19
+ - pyenv::user
20
+ json:
21
+ timezone: UTC
22
+ pyenv:
23
+ user_installs:
24
+ - user: vagrant
25
+ pythons:
26
+ - 3.4.0
27
+ - 2.7.6
28
+ require:
29
+ vagrant-plugins:
30
+ - vagrant-omnibus
31
+ - vagrant-berkshelf
@@ -0,0 +1,28 @@
1
+ omnibus:
2
+ chef_version: 13.2.20
3
+ network:
4
+ private_network:
5
+ ip: 192.168.33.101
6
+ comments:
7
+ - "TIP: the static IP must not collide with other machines on the same network"
8
+ berkself:
9
+ cookbooks:
10
+ apt: ~> 6.1.3
11
+ timezone_iii: ~> 1.0.4
12
+ nodenv: ~> 0.1.2
13
+ chef:
14
+ recipes:
15
+ - apt
16
+ - timezone_iii
17
+ - nodenv
18
+ json:
19
+ timezone: UTC
20
+ nodenv:
21
+ user_installs:
22
+ - user: vagrant
23
+ nodes:
24
+ - 6.11.2
25
+ require:
26
+ vagrant-plugins:
27
+ - vagrant-omnibus
28
+ - vagrant-berkshelf
@@ -0,0 +1,40 @@
1
+ omnibus:
2
+ chef_version: 13.2.20
3
+ network:
4
+ private_network:
5
+ ip: 192.168.33.101
6
+ comments:
7
+ - "TIP: the static IP must not collide with other machines on the same network"
8
+ berkself:
9
+ cookbooks:
10
+ apt: ~> 6.1.0
11
+ timezone_iii: ~> 1.0.4
12
+ ruby_rbenv: ~> 1.2.0
13
+ nodejs: ~> 4.0.0
14
+ postgresql: ~> 6.1.1
15
+ chef:
16
+ recipes:
17
+ - apt
18
+ - timezone_iii
19
+ - ruby_build
20
+ - ruby_rbenv::user
21
+ - nodejs
22
+ - postgresql::server
23
+ json:
24
+ timezone: UTC
25
+ rbenv:
26
+ user_installs:
27
+ - user: vagrant
28
+ rubies:
29
+ - 2.4.1
30
+ global: 2.4.1
31
+ gems:
32
+ 2.4.1:
33
+ - name: bundler
34
+ postgresql:
35
+ password:
36
+ postgres: password
37
+ require:
38
+ vagrant-plugins:
39
+ - vagrant-omnibus
40
+ - vagrant-berkshelf
@@ -0,0 +1,25 @@
1
+ omnibus:
2
+ chef_version: 13.2.20
3
+ berkself:
4
+ cookbooks:
5
+ apt: ~> 6.1.0
6
+ ruby_rbenv: ~> 1.2.0
7
+ chef:
8
+ recipes:
9
+ - apt
10
+ - ruby_build
11
+ - ruby_rbenv::user
12
+ json:
13
+ rbenv:
14
+ user_installs:
15
+ - user: vagrant
16
+ rubies:
17
+ - 2.3.4
18
+ global: 2.3.4
19
+ gems:
20
+ 2.3.4:
21
+ - name: bundler
22
+ require:
23
+ vagrant-plugins:
24
+ - vagrant-omnibus
25
+ - vagrant-berkshelf
@@ -1,12 +1,25 @@
1
1
  en:
2
2
  vagrant:
3
3
  errors:
4
- vagrantfile_templated_exists: |-
4
+ vagrant_templated_vagrantfile_exists: |-
5
5
  A `Vagrantfile` already exists in this directory. Remove it before
6
- running `vagrant templated init`.
7
- berksfile_templated_exists: |-
6
+ running `vagrant templated init` o use force option.
7
+ vagrant_templated_berksfile_exists: |-
8
8
  A `Berksfile` already exists in this directory. Remove it before
9
- running `vagrant templated init`.
10
- vagrant_templated_option_not_found: |-
11
- Option for template not available. Check templates availables
9
+ running `vagrant templated init` o use force option.
10
+ vagrant_templated_template_not_found: |-
11
+ Template not available. Check templates availables
12
12
  running `vagrant templated init --help`.
13
+ vagrant_templated_version_not_found: |-
14
+ Version for template not available. Check versions availables
15
+ running `vagrant templated init --help`.
16
+ vagrant_templated_berksfile_write_error: |-
17
+ The user that is running Vagrant doesn't have the proper permissions
18
+ to write a Vagrantfile to the specified location. Please ensure that
19
+ you call `vagrant init` in a location where the proper permissions
20
+ are in place to create a Vagrantfile.
21
+ vagrant_templated_berksfile_write_error: |-
22
+ The user that is running Vagrant doesn't have the proper permissions
23
+ to write a Berksfile to the specified location. Please ensure that
24
+ you call `vagrant init` in a location where the proper permissions
25
+ are in place to create a Berksfile.
@@ -1,5 +1,5 @@
1
- # Berksfile generated with https://github.com/afaundez/vagrant-templated
2
-
1
+ # vagrant-templated berksfile for <%= template %> <%= version %>
2
+ # check https://github.com/afaundez/vagrant-templated for more options
3
3
  source 'https://supermarket.chef.io'
4
4
 
5
5
  <%- template_attributes['berkself']['cookbooks'].each do |name, version| -%>
@@ -1,18 +1,15 @@
1
- # -*- mode: ruby -*-
2
- # vi: set ft=ruby :
3
- # Vagrantfile generated with https://github.com/afaundez/vagrant-templated
4
-
1
+ # vagrant-templated vagrantfile for <%= template %> <%= version %>
2
+ # check https://github.com/afaundez/vagrant-templated for more options
5
3
  <%- if template_attributes['require'] && template_attributes['require']['vagrant-plugins'] -%>
6
4
  <%= template_attributes['require']['vagrant-plugins'].to_a %>.each do |plugin_name|
7
5
  unless Vagrant.has_plugin? plugin_name
8
6
  raise "#{plugin_name} plugin is required. Please run `vagrant plugin install #{plugin_name}`"
9
7
  end
10
8
  end
11
- <%- end -%>
12
9
 
10
+ <%- end -%>
13
11
  Vagrant.configure('2') do |config|
14
12
  config.vm.box = 'bento/ubuntu-16.04'
15
-
16
13
  <%- if template_attributes['network'] && template_attributes['network']['private_network'] -%>
17
14
 
18
15
  <%- Array(template_attributes['network']['private_network']['comments']).each do |comment| -%>
@@ -7,10 +7,9 @@ end
7
7
  require 'vagrant-templated/version'
8
8
  require 'vagrant-templated/plugin'
9
9
  require 'vagrant-templated/errors'
10
+ require 'vagrant-templated/catalog'
10
11
  require 'vagrant-templated/command/root'
11
12
  require 'vagrant-templated/command/init'
12
13
 
13
- require 'erb'
14
- require 'yaml'
15
14
  require 'i18n'
16
15
  I18n.load_path << Dir.glob(File.join(File.dirname(File.expand_path(__FILE__)), "../config/locales/*.{rb,yml}"))
@@ -0,0 +1,72 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+
4
+ module Vagrant
5
+ module Templated
6
+ module Catalog
7
+ class << self
8
+
9
+ def vagrantfile_for(template, version)
10
+ template_attributes = attributes_for template, version
11
+ vagrantfile_template = File.read(Catalog.root.join 'config/templates/Vagrantfile.erb')
12
+ ERB.new(vagrantfile_template, nil, '-').result binding
13
+ end
14
+
15
+ def berksfile_for(template, version)
16
+ template_attributes = attributes_for template, version
17
+ berksfile_template = File.read(Catalog.root.join 'config/templates/Berksfile.erb')
18
+ ERB.new(berksfile_template, nil, '-').result binding
19
+ end
20
+
21
+ def patch(template, version)
22
+ max_version_for(template)
23
+ end
24
+
25
+ def templates
26
+ attributes.keys
27
+ end
28
+
29
+ def attributes_for(template, version)
30
+ raise Vagrant::Templated::Errors::VersionNotFound unless versions_for(template).include? version
31
+ attributes[template][version]
32
+ end
33
+
34
+ def max_version_for(template)
35
+ versions_for(template).max do |a, b|
36
+ Gem::Version.new(a) <=> Gem::Version.new(b)
37
+ end
38
+ end
39
+
40
+ def versions_for(template)
41
+ raise Vagrant::Templated::Errors::TemplateNotFound unless attributes.include? template
42
+ attributes[template].keys
43
+ end
44
+
45
+ def root
46
+ @root ||= Pathname.new File.expand_path '../..', File.dirname(File.expand_path(__FILE__))
47
+ end
48
+
49
+ private
50
+
51
+ def attributes
52
+ @attributes ||= load_attributes_files
53
+ end
54
+
55
+ def load_attributes_files
56
+ attributes = {}
57
+ attributes_filelist.each do |attributes_file|
58
+ template = File.basename File.dirname(attributes_file)
59
+ version = File.basename(attributes_file, '.yml')
60
+ attributes[template] ||= {}
61
+ attributes[template][version] ||= YAML.load_file(attributes_file)
62
+ end
63
+ attributes
64
+ end
65
+
66
+ def attributes_filelist
67
+ Dir.glob(root.join 'config/attributes/**/*.yml')
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,106 +1,110 @@
1
- require 'optparse'
2
-
3
1
  module Vagrant
4
2
  module Templated
5
3
  module Command
6
4
  class Init < Vagrant.plugin('2', :command)
7
5
 
8
6
  def execute
9
- root_path = File.expand_path '../../../', File.dirname(File.expand_path(__FILE__))
10
- templates_attributes = Dir.glob(File.expand_path("config/templates/attributes/*.yml", root_path)).collect do |template|
11
- YAML.load_file(template)
12
- end.reduce Hash.new, :merge
13
-
14
- options = {}
15
- opts = OptionParser.new do |o|
16
- o.banner = 'Usage: vagrant templated init [options] <template>'
17
-
18
- o.separator ''
19
- o.separator 'Templates availables:'
20
- o.separator ''
21
- templates_attributes.keys.each do |template|
22
- o.separator " #{template}"
23
- end
24
-
25
- o.separator ''
26
- o.separator 'Options:'
27
- o.separator ''
28
-
29
- o.on('-s', '--suffix SUFFIX', String,
30
- "Output suffix for Vagrantfile and Berksfile. '-' for stdout") do |suffix|
31
- options[:suffix] = suffix
32
- end
33
-
34
- o.on('-f', '--force', 'Overwrite an existing box if it exists') do |f|
35
- options[:force] = f
36
- end
37
- end
38
-
39
- argv = parse_options(opts)
40
- return if !argv
41
- if argv.empty? || argv.length > 2
42
- raise Vagrant::Errors::CLIInvalidUsage,
43
- help: opts.help.chomp
7
+ template, version, options = setup
8
+ return if template.nil?
9
+ @env.ui.info "vagrant-templated detected #{template} template", new_line: true
10
+ if version.nil? || version.empty?
11
+ version = Catalog.patch template, version
12
+ @env.ui.info "vagrant-templated didn't received a version, using largest available #{version}"
13
+ else
14
+ @env.ui.info "vagrant-templated detected #{version} version"
44
15
  end
45
16
 
46
- template = argv[0]
47
- if argv.length == 2
48
- options[:name] = argv[0]
49
- template = argv[1]
50
- end
17
+ vagrantfile = Catalog.vagrantfile_for template, version
18
+ berksfile = Catalog.berksfile_for template, version
51
19
 
52
- @env.ui.info("Detected template: #{template}", prefix: false, color: :yellow)
53
- raise Vagrant::Errors::VagrantTemplatedOptionNotFound unless templates_attributes.keys.include? template
54
- template_attributes = templates_attributes[template]
20
+ if options[:cat]
21
+ @env.ui.output vagrantfile
22
+ @env.ui.output berksfile
23
+ else
24
+ files_root = @env.cwd
25
+ if options[:output]
26
+ @env.ui.info "vagrant-templated received output path #{options[:output]}"
27
+ output_path = Pathname.new options[:output]
28
+ files_root = if output_path.relative?
29
+ output_path.expand_path files_root
30
+ else
31
+ output_path
32
+ end
33
+ @env.ui.info "vagrant-templated will write the files in: #{files_root}"
34
+ end
55
35
 
56
- vagrantfile_save_path = nil
57
- berksfile_save_path = nil
58
- if options[:suffix] != "-"
59
- vagrantfile_save_path = Pathname.new(['Vagrantfile', options[:suffix]].compact.join('.')).expand_path(@env.cwd)
36
+ vagrantfile_save_path = Pathname.new('Vagrantfile').expand_path files_root
60
37
  vagrantfile_save_path.delete if vagrantfile_save_path.exist? && options[:force]
61
- raise Vagrant::Errors::VagrantfileTemplatedExistsError if vagrantfile_save_path.exist?
62
- berksfile_save_path = Pathname.new(['Berksfile', options[:suffix]].compact.join('.')).expand_path(@env.cwd)
63
- berksfile_save_path.delete if berksfile_save_path.exist? && options[:force]
64
- raise Vagrant::Errors::BerksfileTemplatedExistsError if berksfile_save_path.exist?
65
- end
38
+ raise Vagrant::Templated::Errors::VagrantfileExistsError if vagrantfile_save_path.exist?
66
39
 
67
- vagrantfile = ERB.new File.read(File.expand_path('config/templates/files/Vagrantfile.erb', root_path)), nil, '-'
68
- berksfile = ERB.new File.read(File.expand_path('config/templates/files/Berksfile.erb', root_path)), nil, '-'
40
+ berksfile_save_path = Pathname.new('Berksfile').expand_path files_root
41
+ berksfile_save_path.delete if berksfile_save_path.exist? && options[:force]
42
+ raise Vagrant::Templated::Errors::BerksfileExistsError if berksfile_save_path.exist?
69
43
 
70
- contents = vagrantfile.result binding
71
- if vagrantfile_save_path
72
44
  begin
73
45
  vagrantfile_save_path.open('w+') do |f|
74
- f.write(contents)
46
+ f.write vagrantfile
75
47
  end
48
+ @env.ui.info "vagrant-templated created #{vagrantfile_save_path}"
76
49
  rescue Errno::EACCES
77
- raise Vagrant::Errors::VagrantfileWriteError
50
+ raise Vagrant::Templated::Errors::VagrantfileWriteError
78
51
  end
79
52
 
80
- @env.ui.info('Templated Vagranfile created successfully', prefix: false)
81
- else
82
- @env.ui.info('VAGRANTFILE', prefix: false, color: :green)
83
- @env.ui.info(contents, prefix: false)
84
- end
85
-
86
- contents = berksfile.result binding
87
- if berksfile_save_path
88
53
  begin
89
54
  berksfile_save_path.open('w+') do |f|
90
- f.write(contents)
55
+ f.write berksfile
91
56
  end
57
+ @env.ui.info "vagrant-templated created #{berksfile_save_path}"
92
58
  rescue Errno::EACCES
93
- raise Vagrant::Errors::BerksfileWriteError
59
+ raise Vagrant::Templated::Errors::BerksfileWriteError
94
60
  end
95
-
96
- @env.ui.info('Templated Berksfile created successfully', prefix: false)
97
- else
98
- @env.ui.info('BERKSFILE', prefix: false, color: :green)
99
- @env.ui.info(contents, prefix: false)
100
61
  end
101
- @env.ui.info('Vagrant Templated finished successfully.', prefix: false, color: :green)
62
+
63
+ @env.ui.success 'vagrant-templated finished successfully'
102
64
  0
103
65
  end
66
+
67
+ private
68
+
69
+ def setup
70
+ options = {}
71
+ opts = OptionParser.new do |o|
72
+ o.banner = 'Usage: vagrant templated init [options] <template> [version]'
73
+
74
+ o.separator ''
75
+ o.separator 'Templates and versions availables:'
76
+ o.separator ''
77
+ Catalog.templates.each do |template|
78
+ o.separator " #{template}: #{Catalog.versions_for(template).join(', ')}"
79
+ end
80
+ o.separator ''
81
+ o.separator 'If no version is provided, larger version will be used.'
82
+
83
+ o.separator ''
84
+ o.separator 'Options:'
85
+ o.separator ''
86
+
87
+ o.on('-o', '--output OUTPUT', String, 'Output path for files. Default to `.`') do |o|
88
+ options[:output] = o
89
+ end
90
+
91
+ o.on('-c', '--cat', 'Output Vagrantfile and Berksfile to stdout') do |c|
92
+ options[:cat] = c
93
+ end
94
+
95
+ o.on('-f', '--force', 'Overwrite existing Vagrantfile and Berksfile if they exists') do |f|
96
+ options[:force] = f
97
+ end
98
+ end
99
+ argv = parse_options opts
100
+ return if !argv
101
+ if !argv || argv.empty? || argv.length > 2
102
+ raise Vagrant::Errors::CLIInvalidUsage,
103
+ help: opts.help.chomp
104
+ end
105
+ template, version = argv
106
+ [template, version, options]
107
+ end
104
108
  end
105
109
  end
106
110
  end
@@ -1,15 +1,29 @@
1
1
  module Vagrant
2
- module Errors
3
- class VagrantfileTemplatedExistsError < VagrantError
4
- error_key(:vagrantfile_templated_exists)
5
- end
2
+ module Templated
3
+ module Errors
4
+ class VagrantfileExistsError < Vagrant::Errors::VagrantError
5
+ error_key(:vagrant_templated_vagrantfile_exists)
6
+ end
6
7
 
7
- class BerksfileTemplatedExistsError < VagrantError
8
- error_key(:berksfile_templated_exists)
9
- end
8
+ class BerksfileExistsError < Vagrant::Errors::VagrantError
9
+ error_key(:vagrant_templated_berksfile_exists)
10
+ end
11
+
12
+ class TemplateNotFound < Vagrant::Errors::VagrantError
13
+ error_key(:vagrant_templated_template_not_found)
14
+ end
15
+
16
+ class VersionNotFound < Vagrant::Errors::VagrantError
17
+ error_key(:vagrant_templated_version_not_found)
18
+ end
19
+
20
+ class BerksfileWriteError < Vagrant::Errors::VagrantError
21
+ error_key(:vagrant_templated_berksfile_write_error)
22
+ end
10
23
 
11
- class VagrantTemplatedOptionNotFound < VagrantError
12
- error_key(:vagrant_templated_option_not_found)
24
+ class VagrantfileWriteError < Vagrant::Errors::VagrantError
25
+ error_key(:vagrant_templated_vagrantfile_write_error)
26
+ end
13
27
  end
14
28
  end
15
29
  end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Templated
3
- VERSION = '0.1.5'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -42,70 +42,102 @@ describe Vagrant::Templated::Command::Init do
42
42
 
43
43
  context 'when using a valid template' do
44
44
 
45
+ let(:vagrantfile_header) {
46
+ ERB.new "# vagrant-templated vagrantfile for <%= template %> <%= version %>\n"\
47
+ '# check https://github.com/afaundez/vagrant-templated for more options'
48
+ }
49
+ let(:berksfile_header) {
50
+ ERB.new "# vagrant-templated berksfile for <%= template %> <%= version %>\n"\
51
+ '# check https://github.com/afaundez/vagrant-templated for more options'
52
+ }
53
+
45
54
  it 'should raise error if Vagrantfile exists' do
46
55
  FileUtils.touch @vagrantfile
47
56
  expect{
48
- described_class.new(['rails5'], @env).execute
49
- }.to raise_error Vagrant::Errors::VagrantfileTemplatedExistsError
57
+ described_class.new(['rails'], @env).execute
58
+ }.to raise_error Vagrant::Templated::Errors::VagrantfileExistsError
50
59
  end
51
60
 
52
- let(:vagrantfile_tag) { '# Vagrantfile generated with ' \
53
- 'https://github.com/afaundez/vagrant-templated'
54
- }
55
- let(:berksfile_tag) { '# Berksfile generated with ' \
56
- 'https://github.com/afaundez/vagrant-templated'
57
- }
58
-
59
61
  it 'should raise error if Berksfile exists' do
60
62
  FileUtils.touch @berksfile
61
63
  expect{
62
- described_class.new(['rails5'], @env).execute
63
- }.to raise_error Vagrant::Errors::BerksfileTemplatedExistsError
64
+ described_class.new(['rails'], @env).execute
65
+ }.to raise_error Vagrant::Templated::Errors::BerksfileExistsError
64
66
  end
65
67
 
66
68
  it 'should create Vagrantfile and Berksfile if both do not exist using template base' do
69
+ template = 'base'
70
+ version = '1.0'
67
71
  expect{
68
- described_class.new(['base'], @env).execute
72
+ described_class.new([template], @env).execute
69
73
  }.to_not raise_error
70
- expect(File.open(@vagrantfile).read).to include(vagrantfile_tag)
71
- expect(File.open(@berksfile).read).to include(berksfile_tag)
74
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
75
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
72
76
  end
73
77
 
74
78
  it 'should create Vagrantfile and Berksfile if both do not exist using template vagrant-plugin' do
79
+ template = 'vagrant-plugin'
80
+ version = '1.9'
75
81
  expect{
76
- described_class.new(['vagrant-plugin'], @env).execute
82
+ described_class.new([template], @env).execute
77
83
  }.to_not raise_error
78
- expect(File.open(@vagrantfile).read).to include(vagrantfile_tag)
79
- expect(File.open(@berksfile).read).to include(berksfile_tag)
84
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
85
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
80
86
  end
81
87
 
82
- it 'should create Vagrantfile and Berksfile if both do not exist using template django1.11' do
88
+
89
+ it 'should create Vagrantfile and Berksfile if both do not exist using template django' do
90
+ template = 'django'
91
+ version = '1.11'
83
92
  expect{
84
- described_class.new(['django1.11'], @env).execute
93
+ described_class.new([template], @env).execute
85
94
  }.to_not raise_error
86
- expect(File.open(@vagrantfile).read).to include(vagrantfile_tag)
87
- expect(File.open(@berksfile).read).to include(berksfile_tag)
95
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
96
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
88
97
  end
89
98
 
90
- it 'should create Vagrantfile and Berksfile if both do not exist using template rails5' do
99
+
100
+ it 'should create Vagrantfile and Berksfile if both do not exist using template rails' do
101
+ template = 'rails'
102
+ version = '5.1'
91
103
  expect{
92
- described_class.new(['rails5'], @env).execute
104
+ described_class.new([template], @env).execute
93
105
  }.to_not raise_error
94
- expect(File.open(@vagrantfile).read).to include(vagrantfile_tag)
95
- expect(File.open(@berksfile).read).to include(berksfile_tag)
106
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
107
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
96
108
  end
97
109
 
98
- context 'and using a suffix' do
110
+
111
+ it 'should create Vagrantfile and Berksfile if both do not exist using template nodejs' do
112
+ template = 'nodejs'
113
+ version = '6.11'
114
+ expect{
115
+ described_class.new([template], @env).execute
116
+ }.to_not raise_error
117
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
118
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
119
+ end
120
+
121
+ context 'and using and output' do
122
+
123
+ before :example do
124
+ @cwd = File.join @tmp, SecureRandom.urlsafe_base64
125
+ FileUtils.mkdir_p @cwd
126
+ @env = Vagrant::Environment.new cwd: @cwd
127
+ @output = 'output'
128
+ FileUtils.mkdir_p File.join(@cwd, @output)
129
+ @vagrantfile = File.join @cwd, @output, 'Vagrantfile'
130
+ @berksfile = File.join @cwd, @output, 'Berksfile'
131
+ end
99
132
 
100
133
  it 'should create Vagrantfile and Berksfile if both do not exist' do
101
- suffix = 'custom-sufix'
134
+ template = 'rails'
135
+ version = '5.1'
102
136
  expect{
103
- described_class.new(['rails5', '--suffix', suffix], @env).execute
137
+ described_class.new([template, '--output', @output], @env).execute
104
138
  }.to_not raise_error
105
- tag_vagrantfile = [@vagrantfile, suffix].join('.')
106
- expect(File.open(tag_vagrantfile).read).to include(vagrantfile_tag)
107
- tag_berksfile = [@berksfile, suffix].join('.')
108
- expect(File.open(tag_berksfile).read).to include(berksfile_tag)
139
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
140
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
109
141
  end
110
142
 
111
143
  end
@@ -113,34 +145,40 @@ describe Vagrant::Templated::Command::Init do
113
145
  context 'and forcing' do
114
146
 
115
147
  it 'should replace existing Vagrantfile' do
148
+ template = 'rails'
149
+ version = '5.1'
116
150
  FileUtils.touch File.join(@cwd, 'Vagrantfile')
117
- args = ['rails5', '--force']
151
+ args = [template, '--force']
118
152
  expect{
119
153
  described_class.new(args, @env).execute
120
154
  }.to_not raise_error
121
- expect(File.open(@vagrantfile).read).to include(vagrantfile_tag)
122
- expect(File.open(@berksfile).read).to include(berksfile_tag)
155
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
156
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
123
157
  end
124
158
 
125
159
  it 'should replace existing Berksfile' do
160
+ template = 'rails'
161
+ version = '5.1'
126
162
  FileUtils.touch File.join(@cwd, 'Berksfile')
127
- args = ['rails5', '--force']
163
+ args = [template, '--force']
128
164
  expect{
129
165
  described_class.new(args, @env).execute
130
166
  }.to_not raise_error
131
- expect(File.open(@vagrantfile).read).to include(vagrantfile_tag)
132
- expect(File.open(@berksfile).read).to include(berksfile_tag)
167
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
168
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
133
169
  end
134
170
 
135
171
  it 'should replace existing Vagrantfile and Berksfile' do
172
+ template = 'rails'
173
+ version = '5.1'
136
174
  FileUtils.touch File.join(@cwd, 'Vagrantfile')
137
175
  FileUtils.touch File.join(@cwd, 'Berksfile')
138
- args = ['rails5', '--force']
176
+ args = [template, '--force']
139
177
  expect{
140
178
  described_class.new(args, @env).execute
141
179
  }.to_not raise_error
142
- expect(File.open(@vagrantfile).read).to include(vagrantfile_tag)
143
- expect(File.open(@berksfile).read).to include(berksfile_tag)
180
+ expect(File.open(@vagrantfile).read).to include(vagrantfile_header.result binding)
181
+ expect(File.open(@berksfile).read).to include(berksfile_header.result binding)
144
182
  end
145
183
 
146
184
  end
@@ -152,7 +190,7 @@ describe Vagrant::Templated::Command::Init do
152
190
  it 'should raise an error' do
153
191
  expect{
154
192
  described_class.new(['fake-template'], @env).execute
155
- }.to raise_error Vagrant::Errors::VagrantTemplatedOptionNotFound
193
+ }.to raise_error Vagrant::Templated::Errors::TemplateNotFound
156
194
  end
157
195
 
158
196
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-templated
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Álvaro Faúndez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-07 00:00:00.000000000 Z
11
+ date: 2017-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,14 +77,16 @@ files:
77
77
  - Vagrantfile
78
78
  - bin/console
79
79
  - bin/setup
80
+ - config/attributes/base/1.0.yml
81
+ - config/attributes/django/1.11.yml
82
+ - config/attributes/nodejs/6.11.yml
83
+ - config/attributes/rails/5.1.yml
84
+ - config/attributes/vagrant-plugin/1.9.yml
80
85
  - config/locales/en.yml
81
- - config/templates/attributes/base.yml
82
- - config/templates/attributes/django1.11.yml
83
- - config/templates/attributes/rails5.yml
84
- - config/templates/attributes/vagrant-plugin.yml
85
- - config/templates/files/Berksfile.erb
86
- - config/templates/files/Vagrantfile.erb
86
+ - config/templates/Berksfile.erb
87
+ - config/templates/Vagrantfile.erb
87
88
  - lib/vagrant-templated.rb
89
+ - lib/vagrant-templated/catalog.rb
88
90
  - lib/vagrant-templated/command/init.rb
89
91
  - lib/vagrant-templated/command/root.rb
90
92
  - lib/vagrant-templated/errors.rb
@@ -1,17 +0,0 @@
1
- base:
2
- omnibus:
3
- chef_version: latest
4
- berkself:
5
- cookbooks:
6
- apt: ~> 6.1.3
7
- timezone_iii: ~> 1.0.4
8
- chef:
9
- recipes:
10
- - apt
11
- - timezone_iii
12
- json:
13
- timezone_iii: UTC
14
- require:
15
- vagrant-plugins:
16
- - vagrant-omnibus
17
- - vagrant-berkshelf
@@ -1,32 +0,0 @@
1
- django1.11:
2
- omnibus:
3
- chef_version: 12.21.1
4
- network:
5
- private_network:
6
- ip: 192.168.33.101
7
- comments:
8
- - "TIP: Change the IP if it causes problem"
9
- - "TIP: Make sure to have something like this on your settings.py:"
10
- - "ALLOWED_HOSTS = ['192.168.33.101', 'localhost', '127.0.0.1']"
11
- berkself:
12
- cookbooks:
13
- apt: ~> 6.1.3
14
- timezone_iii: ~> 1.0.4
15
- pyenv: ~> 0.1.4
16
- chef:
17
- recipes:
18
- - apt
19
- - timezone_iii
20
- - pyenv::user
21
- json:
22
- timezone: UTC
23
- pyenv:
24
- user_installs:
25
- - user: vagrant
26
- pythons:
27
- - 3.4.0
28
- - 2.7.6
29
- require:
30
- vagrant-plugins:
31
- - vagrant-omnibus
32
- - vagrant-berkshelf
@@ -1,41 +0,0 @@
1
- rails5:
2
- omnibus:
3
- chef_version: latest
4
- network:
5
- private_network:
6
- ip: 192.168.33.101
7
- comments:
8
- - "TIP: Change the IP if it causes problem"
9
- berkself:
10
- cookbooks:
11
- apt: ~> 6.1.0
12
- timezone_iii: ~> 1.0.4
13
- ruby_rbenv: ~> 1.2.0
14
- nodejs: ~> 4.0.0
15
- postgresql: ~> 6.1.1
16
- chef:
17
- recipes:
18
- - apt
19
- - timezone_iii
20
- - ruby_build
21
- - ruby_rbenv::user
22
- - nodejs
23
- - postgresql::server
24
- json:
25
- timezone: UTC
26
- rbenv:
27
- user_installs:
28
- - user: vagrant
29
- rubies:
30
- - 2.4.1
31
- global: 2.4.1
32
- gems:
33
- 2.4.1:
34
- - name: bundler
35
- postgresql:
36
- password:
37
- postgres: password
38
- require:
39
- vagrant-plugins:
40
- - vagrant-omnibus
41
- - vagrant-berkshelf
@@ -1,26 +0,0 @@
1
- vagrant-plugin:
2
- omnibus:
3
- chef_version: latest
4
- berkself:
5
- cookbooks:
6
- apt: ~> 6.1.0
7
- ruby_rbenv: ~> 1.2.0
8
- chef:
9
- recipes:
10
- - apt
11
- - ruby_build
12
- - ruby_rbenv::user
13
- json:
14
- rbenv:
15
- user_installs:
16
- - user: vagrant
17
- rubies:
18
- - 2.3.4
19
- global: 2.3.4
20
- gems:
21
- 2.3.4:
22
- - name: bundler
23
- require:
24
- vagrant-plugins:
25
- - vagrant-omnibus
26
- - vagrant-berkshelf