knife-flip 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # knife-flip
2
+
3
+ A knife plugin to move a node from one environment to another
4
+
5
+ ## What it does
6
+
7
+ ````
8
+ knife node flip mynode.foo.com myenv
9
+ ````
10
+
11
+ will move the node mynode.foo.com into the environment myenv
12
+
data/Rakefile ADDED
@@ -0,0 +1,122 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :validate
47
+
48
+ desc "Open an irb session preloaded with this library"
49
+ task :console do
50
+ sh "irb -rubygems -r ./lib/#{name}.rb"
51
+ end
52
+
53
+ #############################################################################
54
+ #
55
+ # Custom tasks (add your own tasks here)
56
+ #
57
+ #############################################################################
58
+
59
+
60
+
61
+ #############################################################################
62
+ #
63
+ # Packaging tasks
64
+ #
65
+ #############################################################################
66
+
67
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
68
+ task :release => :build do
69
+ unless `git branch` =~ /^\* master$/
70
+ puts "You must be on the master branch to release!"
71
+ exit!
72
+ end
73
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
74
+ sh "git tag v#{version}"
75
+ sh "git push origin master"
76
+ sh "git push origin v#{version}"
77
+ sh "gem push pkg/#{name}-#{version}.gem"
78
+ end
79
+
80
+ desc "Build #{gem_file} into the pkg directory"
81
+ task :build => :gemspec do
82
+ sh "mkdir -p pkg"
83
+ sh "gem build #{gemspec_file}"
84
+ sh "mv #{gem_file} pkg"
85
+ end
86
+
87
+ desc "Generate #{gemspec_file}"
88
+ task :gemspec => :validate do
89
+ # read spec file and split out manifest section
90
+ spec = File.read(gemspec_file)
91
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
92
+
93
+ # replace name version and date
94
+ replace_header(head, :name)
95
+ replace_header(head, :version)
96
+ replace_header(head, :date)
97
+ #comment this out if your rubyforge_project has a different name
98
+ replace_header(head, :rubyforge_project)
99
+
100
+ # determine file list from git ls-files
101
+ files = `git ls-files`.
102
+ split("\n").
103
+ sort.
104
+ reject { |file| file =~ /^\./ }.
105
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
106
+ map { |file| " #{file}" }.
107
+ join("\n")
108
+
109
+ # piece file back together and write
110
+ manifest = " s.files = %w[\n#{files}\n ]\n"
111
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
112
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
113
+ puts "Updated #{gemspec_file}"
114
+ end
115
+
116
+ desc "Validate #{gemspec_file}"
117
+ task :validate do
118
+ unless Dir['VERSION*'].empty?
119
+ puts "A `VERSION` file at root level violates Gem best practices."
120
+ exit!
121
+ end
122
+ end
@@ -0,0 +1,61 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'knife-flip'
16
+ s.version = '0.1.0'
17
+ s.date = '2012-01-28'
18
+ s.rubyforge_project = 'knife-flip'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "A knife plugin to move a node from one environment to another"
23
+ s.description = "A knife plugin to move a node from one environment to another"
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["Jon Cowie"]
29
+ s.email = 'jonlives@gmail.com'
30
+ s.homepage = 'https://github.com/jonlives/knife-flip'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ ## Specify any RDoc options here. You'll want to add your README and
37
+ ## LICENSE files to the extra_rdoc_files list.
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.extra_rdoc_files = %w[README.md]
40
+
41
+ ## List your runtime dependencies here. Runtime dependencies are those
42
+ ## that are needed for an end user to actually USE your code.
43
+ s.add_dependency('chef', [">= 0.10.4"])
44
+
45
+ ## Leave this section as-is. It will be automatically generated from the
46
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
47
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
48
+ # = MANIFEST =
49
+ s.files = %w[
50
+ README.md
51
+ Rakefile
52
+ knife-flip.gemspec
53
+ lib/chef/knife/flip.rb
54
+ lib/knife-flip.rb
55
+ ]
56
+ # = MANIFEST =
57
+
58
+ ## Test files will be grabbed from the file list. Make sure the path glob
59
+ ## matches what you actually use.
60
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
61
+ end
@@ -0,0 +1,62 @@
1
+ ## Based on https://gist.github.com/961827 by nstielau
2
+
3
+
4
+ require 'chef/knife'
5
+
6
+ module KnifeFlip
7
+ class NodeFlip < Chef::Knife
8
+
9
+ deps do
10
+ require 'chef/search/query'
11
+ require 'chef/knife/search'
12
+ require 'chef/knife/core/object_loader'
13
+ end
14
+
15
+ banner "knife node flip NODE ENVIRONMENT"
16
+
17
+ def run
18
+ unless @node_name = name_args[0]
19
+ ui.error "You need to specify a node"
20
+ exit 1
21
+ end
22
+
23
+ unless @environment = name_args[1]
24
+ ui.error "You need to specify an environment"
25
+ exit 1
26
+ end
27
+
28
+ puts "Looking for #{@node_name}"
29
+
30
+ searcher = Chef::Search::Query.new
31
+ result = searcher.search(:node, "name:#{@node_name}")
32
+
33
+ knife_search = Chef::Knife::Search.new
34
+ node = result.first.first
35
+ if node.nil?
36
+ puts "Could not find a node named #{@node_name}"
37
+ exit 1
38
+ end
39
+
40
+ begin
41
+ e = Chef::Environment.load(@environment)
42
+ rescue Net::HTTPServerException => e
43
+ if e.response.code.to_s == "404"
44
+ ui.error "The environment #{@environment} does not exist on the server, aborting."
45
+ Chef::Log.debug(e)
46
+ exit 1
47
+ else
48
+ raise
49
+ end
50
+ end
51
+
52
+ puts "Setting environment to #{@environment}"
53
+ node.chef_environment(@environment)
54
+ node.save
55
+
56
+ knife_search = Chef::Knife::Search.new
57
+ knife_search.name_args = ['node', "name:#{@node_name}"]
58
+ knife_search.run
59
+
60
+ end
61
+ end
62
+ end
data/lib/knife-flip.rb ADDED
@@ -0,0 +1,3 @@
1
+ module KnifeFlip
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-flip
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Jon Cowie
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-01-28 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: chef
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 10
31
+ - 4
32
+ version: 0.10.4
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: A knife plugin to move a node from one environment to another
36
+ email: jonlives@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ files:
44
+ - README.md
45
+ - Rakefile
46
+ - knife-flip.gemspec
47
+ - lib/chef/knife/flip.rb
48
+ - lib/knife-flip.rb
49
+ has_rdoc: true
50
+ homepage: https://github.com/jonlives/knife-flip
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project: knife-flip
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: A knife plugin to move a node from one environment to another
81
+ test_files: []
82
+