knife-bulkchangeenvironment 0.1.0
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.
- data/README.md +16 -0
- data/Rakefile +122 -0
- data/knife-bulkchangeenv.gemspec +61 -0
- data/lib/chef/knife/bulkchangeenv.rb +100 -0
- data/lib/knife-bulkchangeenv.rb +3 -0
- metadata +82 -0
data/README.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# knife-bulkchangeenv
|
2
|
+
|
3
|
+
A plugin for Chef::Knife which lets you move all nodes in one environment into another.
|
4
|
+
|
5
|
+
## Preface
|
6
|
+
|
7
|
+
Searches for all nodes in a particular environment and moves them to another
|
8
|
+
|
9
|
+
## What it does
|
10
|
+
|
11
|
+
knife node bulk_change_environment "_default" development
|
12
|
+
will move all nodes in the default environment (in quotes because of the underscore) into the development environment.
|
13
|
+
|
14
|
+
## Notes
|
15
|
+
Please be careful when using this plugin, especially if you're already using environments with version constraints configured. You don't want all your nodes moved to the wrong one by mistake!
|
16
|
+
|
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-bulkchangeenvironment'
|
16
|
+
s.version = '0.1.0'
|
17
|
+
s.date = '2012-01-28'
|
18
|
+
s.rubyforge_project = 'knife-bulkchangeenvironment'
|
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 all nodes from one environment to another"
|
23
|
+
s.description = "A knife plugin to move all nodes 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-bulkchangeenvironment'
|
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-bulkchangeenv.gemspec
|
53
|
+
lib/chef/knife/bulkchangeenv.rb
|
54
|
+
lib/knife-bulkchangeenv.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,100 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Jon Cowie (<jonlives@gmail.com>)
|
3
|
+
# Copyright:: Copyright (c) 2011 Jon Cowie
|
4
|
+
# License:: GPL
|
5
|
+
|
6
|
+
|
7
|
+
require 'chef/knife'
|
8
|
+
|
9
|
+
module KnifeBulkChangeEnv
|
10
|
+
class NodeBulkChangeEnvironment < Chef::Knife
|
11
|
+
|
12
|
+
deps do
|
13
|
+
require 'chef/search/query'
|
14
|
+
require 'chef/knife/search'
|
15
|
+
end
|
16
|
+
|
17
|
+
banner "knife node bulk_change_environment OLDENVIRONMENT NEWENVIRONMENT"
|
18
|
+
|
19
|
+
def run
|
20
|
+
unless @old_env = name_args[0]
|
21
|
+
ui.error "You need to specify a Environment to update from"
|
22
|
+
exit 1
|
23
|
+
end
|
24
|
+
|
25
|
+
unless @new_env = name_args[1]
|
26
|
+
ui.error "You need to specify an environment to update to"
|
27
|
+
exit 1
|
28
|
+
end
|
29
|
+
|
30
|
+
puts "Checking for an environment called #{@old_env} to update from..."
|
31
|
+
|
32
|
+
searcher = Chef::Search::Query.new
|
33
|
+
result = searcher.search(:environment, "name:#{@old_env}")
|
34
|
+
|
35
|
+
knife_search = Chef::Knife::Search.new
|
36
|
+
env = result.first.first
|
37
|
+
if env.nil?
|
38
|
+
puts "Could not find an environment named #{@old_env}. Can't update nodes in a non-existant environment!"
|
39
|
+
exit 1
|
40
|
+
else
|
41
|
+
puts "Found!\n"
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
puts "Checking for an environment called #{@new_env} to update to..."
|
46
|
+
|
47
|
+
searcher = Chef::Search::Query.new
|
48
|
+
result = searcher.search(:environment, "name:#{@new_env}")
|
49
|
+
|
50
|
+
knife_search = Chef::Knife::Search.new
|
51
|
+
env = result.first.first
|
52
|
+
if env.nil?
|
53
|
+
puts "Could not find an environment named #{@new_env}. Please create it before trying to put nodes in it!"
|
54
|
+
exit 1
|
55
|
+
else
|
56
|
+
puts "Found!\n"
|
57
|
+
end
|
58
|
+
|
59
|
+
#puts "Setting environment to #{@environment}"
|
60
|
+
#node.chef_environment(@environment)
|
61
|
+
#node.save
|
62
|
+
|
63
|
+
q_nodes = Chef::Search::Query.new
|
64
|
+
node_query = "chef_environment:#{@old_env}"
|
65
|
+
query_nodes = URI.escape(node_query,
|
66
|
+
Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
67
|
+
|
68
|
+
result_items = []
|
69
|
+
result_count = 0
|
70
|
+
|
71
|
+
ui.msg("\nFinding all nodes in environment #{@old_env} and moving them to environment #{@new_env}...\n")
|
72
|
+
|
73
|
+
begin
|
74
|
+
q_nodes.search('node', query_nodes) do |node_item|
|
75
|
+
|
76
|
+
node_item.chef_environment(@new_env)
|
77
|
+
node_item.save
|
78
|
+
formatted_item_node = format_for_display(node_item)
|
79
|
+
if formatted_item_node.respond_to?(:has_key?) && !formatted_item_node.has_key?('id')
|
80
|
+
formatted_item_node['id'] = node_item.has_key?('id') ? node_item['id'] : node_item.name
|
81
|
+
end
|
82
|
+
ui.msg("Updated #{formatted_item_node.name}...")
|
83
|
+
result_items << formatted_item_node
|
84
|
+
result_count += 1
|
85
|
+
end
|
86
|
+
rescue Net::HTTPServerException => e
|
87
|
+
msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
|
88
|
+
ui.error("knife preflight failed: #{msg}")
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
|
92
|
+
if ui.interchange?
|
93
|
+
output({:results => result_count, :rows => result_items})
|
94
|
+
else
|
95
|
+
ui.msg "#{result_count} Nodes updated"
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knife-bulkchangeenvironment
|
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 all nodes 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-bulkchangeenv.gemspec
|
47
|
+
- lib/chef/knife/bulkchangeenv.rb
|
48
|
+
- lib/knife-bulkchangeenv.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: https://github.com/jonlives/knife-bulkchangeenvironment
|
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-bulkchangeenvironment
|
77
|
+
rubygems_version: 1.3.7
|
78
|
+
signing_key:
|
79
|
+
specification_version: 2
|
80
|
+
summary: A knife plugin to move all nodes from one environment to another
|
81
|
+
test_files: []
|
82
|
+
|