niman 0.0.3 → 0.0.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: f4b4fbde2c69d5428cdeef5956a0781eded47766
4
- data.tar.gz: cc99ba94749c533e2d0a953695a4b03ec0aec32e
3
+ metadata.gz: 77f2ee2cb32e24e29d3e081d50044edd26819fc8
4
+ data.tar.gz: 5fdb6bbfa078331f80e538a9e0de5fde3d8d831c
5
5
  SHA512:
6
- metadata.gz: 25581ac834d6daa7134e5ecf6708a22bd5e7e7bc74568325fd3389bafa7f2e9b7d3079115beed68848f69222933c7da194c70903360a24fae1909595e872c0ea
7
- data.tar.gz: a98b0c0aa0417876e61c269aa8c203181ea74ae28194a397f6c79593c0d3ff39bf27af2152d170baf9be62378695e01c394ca538fa92211824abd276da2bc7fc
6
+ metadata.gz: de38665f05cfacb23ac3e7b471b60536c4739c18ab3638c74951e7aeae0111a6dbb9c01fe0a523c9652867edf24ad18248a4a3e798f34a029bc57d83490f3bfb
7
+ data.tar.gz: bcb6565a2b73e9b14bf005f6f9a2a2fba08749ab6299a9c4cf8c635bd249d8f799c0213dd287a73c9d524acc728ae21b26f31fea3c43bf61239f3d49fda9fc68
data/.travis.yml CHANGED
@@ -1,10 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - "1.9.3"
4
3
  - "2.0.0"
5
4
  - "2.1.5"
6
5
  - "2.2.0"
7
- - jruby-19mode # JRuby in 1.9 mode
8
- - rbx
9
6
  env:
10
- - RUN=rspec
7
+ - RUN=rake
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Niman
2
+ [![Build Status](https://travis-ci.org/schultyy/Niman.svg?branch=master)](https://travis-ci.org/schultyy/Niman)
2
3
 
3
4
  Niman is a proof-of-concept provisioner.
4
5
 
@@ -76,6 +77,20 @@ To apply a `Nimanfile` run:
76
77
  $ niman apply
77
78
  ```
78
79
 
80
+ ### Use it as a Vagrant plugin
81
+
82
+ At first, install it as Vagrant plugin:
83
+ ```bash
84
+ $ vagrant plugin install niman
85
+ ```
86
+
87
+ Then, in `Vagrantfile`, add this line:
88
+
89
+ ```ruby
90
+ config.vm.provision "niman"
91
+ ```
92
+ Save the file and after this you can `vagrant provision`.
93
+
79
94
  ## Contributing
80
95
 
81
96
  1. Fork it ( https://github.com/[my-github-username]/niman/fork )
data/Rakefile CHANGED
@@ -1,2 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
-
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => [:spec]
@@ -3,6 +3,8 @@ require 'niman/recipe'
3
3
  require 'niman/provisioner'
4
4
  require "niman/shell"
5
5
  require "niman/installer"
6
+ require "niman/exceptions"
7
+ require "niman/filehandler"
6
8
 
7
9
  module Niman
8
10
  module CLI
@@ -15,33 +17,38 @@ module Niman
15
17
  @silent = false
16
18
  end
17
19
 
18
- desc "apply", "Applies a Nimanfile"
19
- def apply
20
- Niman::Recipe.from_file
21
- config = Niman::Recipe.configuration
22
- installer = Niman::Installer.new(shell: client_shell, managers:{
23
- debian: 'apt-get -y',
24
- redhat: 'yum -y'
25
- })
26
- provisioner = Niman::Provisioner.new(installer, config.instructions)
27
- this = self
28
- provisioner.run do |instruction|
29
- this.say "Executing task #{instruction.description}" unless @quiet
20
+ desc "apply [NIMANFILE]", "Applies a Nimanfile"
21
+ def apply(file='Nimanfile')
22
+ begin
23
+ Niman::Recipe.from_file(file)
24
+ config = Niman::Recipe.configuration
25
+ installer = Niman::Installer.new(shell: client_shell, managers:{
26
+ debian: 'apt-get -y',
27
+ redhat: 'yum -y'
28
+ })
29
+ filehandler = Niman::FileHandler.new(client_shell)
30
+ provisioner = Niman::Provisioner.new(installer, filehandler, config.instructions)
31
+ this = self
32
+ provisioner.run do |instruction|
33
+ this.say "Executing task #{instruction.description}" unless @quiet
34
+ end
35
+ rescue LoadError => e
36
+ client_shell.print(e.message, :error)
37
+ rescue Niman::ConfigError => cfg_error
38
+ client_shell.print(cfg_error.message, :error)
30
39
  end
31
- rescue LoadError => e
32
- error e.message
33
40
  end
34
41
 
35
- desc "setup", "Generates an empty Nimanfile"
36
- def setup
42
+ desc "setup [FILENAME]", "Generates an empty Nimanfile"
43
+ def setup(filename=Niman::Recipe::DEFAULT_FILENAME)
37
44
  content = <<-EOS
38
45
  # -*- mode: ruby -*-
39
46
  # vi: set ft=ruby :
40
47
  Niman::Recipe.configure do |config|
41
48
  end
42
49
  EOS
43
- File.open(Niman::Recipe::DEFAULT_FILENAME, "w") { |handle| handle.write(content) }
44
- say "Created new file #{Niman::Recipe::DEFAULT_FILENAME}"
50
+ File.open(filename, "w") { |handle| handle.write(content) }
51
+ say "Created new file #{filename}"
45
52
  end
46
53
  end
47
54
  end
@@ -0,0 +1,15 @@
1
+ module Niman
2
+ class FileHandler
3
+ attr_reader :shell
4
+
5
+ def initialize(shell)
6
+ @shell = shell
7
+ end
8
+
9
+ def run(files)
10
+ Array(files).each do |file|
11
+ shell.create_file(file.path, file.content)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -12,6 +12,10 @@ module Niman
12
12
  !self.path.nil? && !self.path.empty?
13
13
  end
14
14
 
15
+ def errors
16
+ ['file path must not be nil and empty']
17
+ end
18
+
15
19
  def run
16
20
  ::File.open(::File.expand_path(path), "w") { |handle| handle.write(content) }
17
21
  end
@@ -13,6 +13,10 @@ module Niman
13
13
  def valid?
14
14
  !name.empty?
15
15
  end
16
+
17
+ def errors
18
+ 'package name must not be empty'
19
+ end
16
20
  end
17
21
  end
18
22
  end
@@ -4,8 +4,9 @@ module Niman
4
4
  class Provisioner
5
5
  attr_reader :instructions
6
6
 
7
- def initialize(installer, instructions)
7
+ def initialize(installer, filehandler, instructions)
8
8
  @installer = installer
9
+ @filehandler = filehandler
9
10
  @instructions = Array(instructions)
10
11
  end
11
12
 
@@ -13,12 +14,16 @@ module Niman
13
14
  @instructions.all?(&:valid?)
14
15
  end
15
16
 
17
+ def errors
18
+ @instructions.map(&:errors).flatten.join("\n")
19
+ end
20
+
16
21
  def run
17
- raise Niman::ConfigError unless self.valid?
22
+ raise Niman::ConfigError, self.errors unless self.valid?
18
23
  @instructions.each do |instruction|
19
24
  yield(instruction) if block_given?
20
25
  if instruction.respond_to?(:run)
21
- instruction.run
26
+ @filehandler.run(instruction)
22
27
  else
23
28
  @installer.install(instruction)
24
29
  end
data/lib/niman/recipe.rb CHANGED
@@ -16,13 +16,12 @@ module Niman
16
16
  @configuration
17
17
  end
18
18
 
19
- def self.reset!
19
+ def self.reset
20
20
  @configuration = Niman::Nimanfile.new
21
21
  end
22
22
 
23
- def self.from_file
24
- path = File.join(Dir.pwd, DEFAULT_FILENAME)
25
- load DEFAULT_FILENAME
23
+ def self.from_file(filename)
24
+ load filename
26
25
  end
27
26
  end
28
27
  end
data/lib/niman/shell.rb CHANGED
@@ -23,5 +23,20 @@ module Niman
23
23
  `#{command}`
24
24
  end
25
25
  end
26
+
27
+ def print(message, type)
28
+ case type
29
+ when :error
30
+ STDERR.puts message
31
+ else
32
+ STDOUT.puts message
33
+ end
34
+ end
35
+
36
+ def create_file(path, content)
37
+ File.open(File.expand_path(path), 'w') do |handle|
38
+ handle.write(content)
39
+ end
40
+ end
26
41
  end
27
42
  end
@@ -9,9 +9,7 @@ module VagrantPlugins
9
9
  def os
10
10
  if @platform.linux?
11
11
  variant = @platform.linux_variant(-> (fn){ @machine.communicate.test("cat #{fn}")},
12
- -> (fn){ @channel.execute("cat #{fn}") do |type, data|
13
- data.chomp
14
- end})
12
+ -> (fn){ @channel.execute("cat #{fn}") { |type, data| data.chomp}})
15
13
  variant[:family]
16
14
  else
17
15
  raise Niman::UnsupportedOSError
@@ -35,6 +33,28 @@ module VagrantPlugins
35
33
  end
36
34
  end
37
35
 
36
+ def print(message, type)
37
+ case type
38
+ when :error
39
+ @machine.ui.error(message, {:color => :red})
40
+ else
41
+ @machine.ui.info(message, {:color => :green})
42
+ end
43
+ end
44
+
45
+ def create_file(path, content)
46
+ @channel.sudo(`echo #{content} > #{File.expand_path(path)}`) do |type, data|
47
+ #Output the data with the proper color based on the stream.
48
+ color = type == :stdout ? :green : :red
49
+ # Clear out the newline since we add one
50
+ data = data.chomp
51
+ next if data.empty?
52
+ options = {}
53
+ options[:color] = :green
54
+ @machine.ui.info(data.chomp, options)
55
+ end
56
+ end
57
+
38
58
  private
39
59
 
40
60
  def ruby_platform
data/lib/niman/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Niman
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'niman/filehandler'
3
+ require 'niman/shell'
4
+
5
+ describe Niman::FileHandler do
6
+ let(:file1) { Niman::Library::File.new(path: '~/foo.txt', content: 'bar') }
7
+ let(:file2) { Niman::Library::File.new(path: '~/bar.txt', content: 'baz') }
8
+ let(:files) { [file1, file2] }
9
+ let(:shell) { double(Niman::Shell) }
10
+ subject(:filehandler) { Niman::FileHandler.new(shell) }
11
+
12
+ describe "#initialize" do
13
+ it 'accepts a shell' do
14
+ expect(filehandler.shell).to eq shell
15
+ end
16
+ end
17
+
18
+ describe '#run' do
19
+ before do
20
+ allow(shell).to receive(:create_file)
21
+ end
22
+
23
+ context 'with multiple files' do
24
+ before do
25
+ filehandler.run(files)
26
+ end
27
+
28
+ it 'writes files' do
29
+ expect(shell).to have_received(:create_file).twice
30
+ end
31
+ end
32
+
33
+ context 'with single file' do
34
+ before do
35
+ filehandler.run(file1)
36
+ end
37
+
38
+ it 'writes file' do
39
+ expect(shell).to have_received(:create_file).once
40
+ end
41
+ end
42
+ end
43
+ end
@@ -8,7 +8,8 @@ describe Niman::Provisioner do
8
8
  let(:vim_package) { Niman::Library::Package.new(name: 'vim') }
9
9
  let(:instructions) { [file, vim_package] }
10
10
  let(:installer) { double(Niman::Installer) }
11
- subject(:provisioner) { Niman::Provisioner.new(installer, instructions) }
11
+ let(:filehandler) { double(Niman::FileHandler) }
12
+ subject(:provisioner) { Niman::Provisioner.new(installer, filehandler, instructions) }
12
13
 
13
14
  describe "#initialize" do
14
15
  it 'accepts a list of instructions' do
@@ -16,7 +17,7 @@ describe Niman::Provisioner do
16
17
  end
17
18
 
18
19
  it 'accepts a single instruction' do
19
- provisioner = Niman::Provisioner.new(installer, file)
20
+ provisioner = Niman::Provisioner.new(installer, filehandler, file)
20
21
  expect(provisioner.instructions).to eq [file]
21
22
  end
22
23
  end
@@ -47,13 +48,13 @@ describe Niman::Provisioner do
47
48
 
48
49
  context 'with valid instructions' do
49
50
  before do
50
- allow(file).to receive(:run)
51
+ allow(filehandler).to receive(:run)
51
52
  allow(installer).to receive(:install)
52
53
  provisioner.run
53
54
  end
54
55
 
55
- it 'calls run for runable instructions' do
56
- expect(file).to have_received(:run)
56
+ it 'calls file handler for files' do
57
+ expect(filehandler).to have_received(:run).with(file)
57
58
  end
58
59
 
59
60
  it 'calls installer for package' do
@@ -25,7 +25,7 @@ describe Niman::Recipe do
25
25
  file.content = 'hello from alice'
26
26
  end
27
27
  end
28
- Niman::Recipe.reset!
28
+ Niman::Recipe.reset
29
29
  end
30
30
 
31
31
  it 'erases current configuration' do
@@ -45,14 +45,21 @@ describe Niman::Recipe do
45
45
  end
46
46
  EOS
47
47
  File.open(Niman::Recipe::DEFAULT_FILENAME, "w") {|handle| handle.write(content) }
48
+ File.open('CustomNimanfile', "w") {|handle| handle.write(content) }
48
49
  end
49
50
  after do
50
51
  File.delete(Niman::Recipe::DEFAULT_FILENAME)
51
52
  FakeFS.activate!
53
+ Niman::Recipe.reset
52
54
  end
53
55
 
54
- it 'loads "Nimanfile" by default' do
55
- Niman::Recipe.from_file
56
+ it 'loads "Nimanfile"' do
57
+ Niman::Recipe.from_file(Niman::Recipe::DEFAULT_FILENAME)
58
+ expect(Niman::Recipe.configuration.instructions.length).to eq 1
59
+ end
60
+
61
+ it 'loads Nimanfile from custom path' do
62
+ Niman::Recipe.from_file('CustomNimanfile')
56
63
  expect(Niman::Recipe.configuration.instructions.length).to eq 1
57
64
  end
58
65
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: niman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Schulte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-08 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -113,6 +113,7 @@ files:
113
113
  - lib/niman.rb
114
114
  - lib/niman/cli/application.rb
115
115
  - lib/niman/exceptions.rb
116
+ - lib/niman/filehandler.rb
116
117
  - lib/niman/installer.rb
117
118
  - lib/niman/library/file.rb
118
119
  - lib/niman/library/package.rb
@@ -126,6 +127,7 @@ files:
126
127
  - lib/niman/vagrant/shell.rb
127
128
  - lib/niman/version.rb
128
129
  - niman.gemspec
130
+ - spec/lib/filehandler_spec.rb
129
131
  - spec/lib/installer_spec.rb
130
132
  - spec/lib/library/file_spec.rb
131
133
  - spec/lib/library/package_spec.rb
@@ -158,6 +160,7 @@ signing_key:
158
160
  specification_version: 4
159
161
  summary: Provisions your machine
160
162
  test_files:
163
+ - spec/lib/filehandler_spec.rb
161
164
  - spec/lib/installer_spec.rb
162
165
  - spec/lib/library/file_spec.rb
163
166
  - spec/lib/library/package_spec.rb