chefkitchen_cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ chefkitchen_cli
2
+ ===============
3
+
4
+ CLI for talking to chefkitchen to do quick chef tasks
data/bin/chefkitchen ADDED
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
+
4
+ require 'fileutils'
5
+ require 'yaml'
6
+ require 'rubygems'
7
+ require 'thor'
8
+ require 'cookie_jar'
9
+ require 'request_helper'
10
+
11
+ class Microwave < Thor
12
+ CONFIG_FILE = File.expand_path('~/.chefkitchen.yml')
13
+
14
+ desc "tag", "create a chef tag"
15
+ method_option :nodegroup, :aliases => "-n", :desc => "What nodegroup to associate the chef-tag with"
16
+ method_option :revision, :required => true, :aliases => "-r", :desc => "What svn revision to use for the tag"
17
+ method_option :conf_file, :aliases => "-c", :desc => "Configuration file"
18
+ def tag
19
+ conf = load_conf(options[:conf_file] || CONFIG_FILE)
20
+ rhelper = RequestHelper.new(conf)
21
+ rhelper.tag(options)
22
+ end
23
+
24
+ method_option :nodegroup, :required => true, :aliases => "-n", :desc => "What nodegroup to do the dryrun for"
25
+ method_option :tag, :aliases => "-t", :desc => "What chef-tag to do the dryrun for (can also be trunk or branches/mybranch or blank (for current tag)"
26
+ method_option :conf_file, :aliases => "-c", :desc => "Configuration file"
27
+ desc 'dryrun', 'perform a dryrun'
28
+ def dryrun
29
+ conf = load_conf(options[:conf_file] || CONFIG_FILE)
30
+ rhelper = RequestHelper.new(conf)
31
+ rhelper.dryrun(options)
32
+ end
33
+
34
+ method_option :conf_file, :aliases => "-c", :desc => "Configuration file"
35
+ method_option :nodegroup1, :required => true, :desc => "What nodegroup to do the diffrun for"
36
+ method_option :nodegroup2, :required => true, :desc => "What nodegroup to do the diffrun for"
37
+ method_option :tag1, :desc => "What chef-tag to do diffrun for (can also be trunk or branches/mybranch or blank (for current tag)"
38
+ method_option :tag2, :desc => "What chef-tag to do diffrun for (can also be trunk or branches/mybranch or blank (for current tag)"
39
+ method_option :conf_file, :aliases => "-c", :desc => "Configuration file"
40
+ desc 'diffrun', 'perform a diffrun'
41
+ def diffrun
42
+ conf = load_conf(options[:conf_file] || CONFIG_FILE)
43
+ rhelper = RequestHelper.new(conf)
44
+ rhelper.diffrun(options)
45
+ end
46
+
47
+ desc 'show_dryrun', 'show dryrun result'
48
+ method_option :id, :required => true, :desc => "ID of the dryrun to display"
49
+ method_option :json, :desc => "Return result in json format"
50
+ def show_dryrun
51
+ conf = load_conf(options[:conf_file] || CONFIG_FILE)
52
+ rhelper = RequestHelper.new(conf)
53
+ rhelper.show_dryrun(options)
54
+ end
55
+
56
+ desc 'show_diffrun', 'show diffrun result'
57
+ method_option :id, :required => true, :desc => "ID of the diffrun to display"
58
+ method_option :json, :desc => "Return result in json format"
59
+ def show_diffrun
60
+ conf = load_conf(options[:conf_file] || CONFIG_FILE)
61
+ rhelper = RequestHelper.new(conf)
62
+ rhelper.show_diffrun(options)
63
+ end
64
+
65
+ desc 'show_tag', 'show the result of chef tagging'
66
+ method_option :id, :required => true, :desc => "ID of the chef tag to display"
67
+ method_option :json, :desc => "Return result in json format"
68
+ def show_tag
69
+ conf = load_conf(options[:conf_file] || CONFIG_FILE)
70
+ rhelper = RequestHelper.new(conf)
71
+ rhelper.show_tag(options)
72
+ end
73
+
74
+ desc "config", "configuration setup"
75
+ method_option :conf_file, :aliases => "-c", :desc => "Where to write config to"
76
+ def config
77
+ config_file = File.expand_path(options[:conf_file] || CONFIG_FILE)
78
+ puts "Provide the following inputs so chefkitchen_cli can save them to #{config_file} for future execution"
79
+ conf = {}
80
+ conf[:username] = ask("Username: ")
81
+ conf[:password] = ask("Password: ", true)
82
+ conf[:server] = ask("Chefkitchen server: ")
83
+ File.open(config_file, 'w') do |f|
84
+ YAML.dump(conf, f)
85
+ end
86
+ FileUtils.chmod 0700, config_file
87
+ puts "#{config_file} has been created"
88
+ end
89
+
90
+ private
91
+ def ask(str,mask=false)
92
+ begin
93
+ print str
94
+ system 'stty -echo;' if mask
95
+ input = STDIN.gets.chomp
96
+ ensure
97
+ system 'stty echo; echo ""'
98
+ end
99
+ return input
100
+ end
101
+
102
+ def load_conf(conf_file)
103
+ YAML::load(File.open(File.expand_path(conf_file)))
104
+ end
105
+ end
106
+
107
+ Microwave.start
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "chefkitchen_cli"
7
+ s.version = "0.0.1"
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Darren Dao"]
10
+ s.email = ["darrendao@gmail.com"]
11
+ s.homepage = "https://github.com/darrendao/chefkitchen_cli"
12
+ s.summary = %q{Ruby script to talk to chefkitchen web services.}
13
+ s.description = %q{Ruby script to talk to chefkitchen web services.}
14
+
15
+ s.add_dependency 'thor'
16
+ s.add_dependency 'json'
17
+ s.add_dependency 'rest-client', '~> 1.6.7'
18
+ s.add_development_dependency 'yard', '~> 0.7'
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ s.extra_rdoc_files = ["README.md"]
24
+ end
25
+
data/lib/cookie_jar.rb ADDED
@@ -0,0 +1,16 @@
1
+ module CookieJar
2
+ def get_cookies
3
+ params = {:login => @username, :password => @password}
4
+ cookies = nil
5
+ RestClient.post("https://#{@server}/login/login", params) do |response, request, result, &block|
6
+ if response.code == 200
7
+ cookies = response.cookies
8
+ elsif response.code == 302 && response.headers[:location] !~ /login/
9
+ cookies = response.cookies
10
+ else
11
+ raise "Failed to authenticate"
12
+ end
13
+ end
14
+ cookies
15
+ end
16
+ end
@@ -0,0 +1,140 @@
1
+ require 'rubygems'
2
+ require 'rest_client'
3
+ require 'json'
4
+ require 'cookie_jar'
5
+
6
+ class RequestHelper
7
+ include CookieJar
8
+
9
+ def initialize(conf)
10
+ @server = conf[:server] || "localhost"
11
+ @username = conf[:username]
12
+ @password = conf[:password]
13
+ @cookies = get_cookies
14
+ end
15
+
16
+ def tag(options)
17
+ revision = options[:revision]
18
+ nodegroup = options[:nodegroup]
19
+ begin
20
+ response = RestClient.post("http://#{@server}/tags",
21
+ {:tag => {:revision => revision, :nodegroup => nodegroup}},
22
+ {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
23
+ result = JSON.parse(response)
24
+ puts "Tag id: #{result['tag']['id']}"
25
+ puts "URL: http://#{@server}/tags/#{result['tag']['id']}"
26
+ rescue => e
27
+ puts e.inspect
28
+ end
29
+ end
30
+
31
+ def diffrun(options)
32
+ begin
33
+ response = RestClient.post("http://#{@server}/diff_requests",
34
+ {:diff_request =>
35
+ {'dryruns_attributes[0][node_group]' => options[:nodegroup1], 'dryruns_attributes[0][tag]' => options[:tag1],
36
+ 'dryruns_attributes[1][node_group]' => options[:nodegroup2], 'dryruns_attributes[1][tag]' => options[:tag2]}
37
+
38
+ },
39
+ {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
40
+ result = JSON.parse(response)
41
+ puts "http://#{@server}/diff_requests/#{result['diff_request']['id']}"
42
+ rescue => e
43
+ puts e.inspect
44
+ end
45
+ end
46
+
47
+ def dryrun(options)
48
+ tag = options[:tag]
49
+ nodegroup = options[:nodegroup]
50
+ begin
51
+ response = RestClient.post("http://#{@server}/dryruns",
52
+ {:dryrun => {'node_group' => nodegroup, :tag => tag}},
53
+ {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
54
+ result = JSON.parse(response)
55
+ puts "http://#{@server}/dryruns/#{result['dryrun']['id']}"
56
+ rescue => e
57
+ puts e.inspect
58
+ end
59
+ end
60
+
61
+ def show_dryrun(options)
62
+ begin
63
+ response = RestClient.get("http://#{@server}/dryruns/#{options[:id]}",
64
+ {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
65
+ result = JSON.parse(response)
66
+ if options[:json]
67
+ puts JSON.pretty_generate(result)
68
+ else
69
+ display_dryrun(result)
70
+ end
71
+ rescue => e
72
+ puts e.inspect
73
+ end
74
+ end
75
+
76
+ def show_diffrun(options)
77
+ begin
78
+ response = RestClient.get("http://#{@server}/diff_requests/#{options[:id]}",
79
+ {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
80
+ result = JSON.parse(response)
81
+ if options[:json]
82
+ puts JSON.pretty_generate(result)
83
+ else
84
+ display_diff(result)
85
+ end
86
+ rescue => e
87
+ puts e.inspect
88
+ end
89
+ end
90
+
91
+ def show_tag(options)
92
+ begin
93
+ response = RestClient.get("http://#{@server}/tags/#{options[:id]}",
94
+ {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
95
+ result = JSON.parse(response)
96
+ if options[:json]
97
+ puts JSON.pretty_generate(result)
98
+ else
99
+ display_tag(result)
100
+ end
101
+ rescue => e
102
+ puts e.inspect
103
+ end
104
+ end
105
+
106
+ private
107
+ def display_diff(diffs)
108
+ diffs.each do |key, value|
109
+ value.each do |diff|
110
+ puts diff['diff']
111
+ puts "\n"
112
+ end
113
+ end
114
+ end
115
+
116
+ def display_dryrun(result)
117
+ puts "Dryrun info: "
118
+ puts JSON.pretty_generate(result['dryrun'])
119
+ puts "\n"
120
+ puts "Files generated: "
121
+ result['files'].each do |file|
122
+ puts file['path']
123
+ end
124
+ end
125
+
126
+ def display_tag(result)
127
+ puts "Tag info: "
128
+ puts JSON.pretty_generate(result['tag'])
129
+ puts "\n"
130
+ puts "Dryrun(s): "
131
+ result['dryruns'].each do |dryrun|
132
+ puts "http://#{@server}/dryruns/#{dryrun['dryrun']['id']}"
133
+ end
134
+ puts "\n"
135
+ puts "Diffrun(s): "
136
+ result['diff_requests'].each do |diff_request|
137
+ puts "http://#{@server}/diff_requests/#{diff_request['diff_request']['id']}"
138
+ end
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chefkitchen_cli
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Darren Dao
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-29 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thor
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: json
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rest-client
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 1
57
+ segments:
58
+ - 1
59
+ - 6
60
+ - 7
61
+ version: 1.6.7
62
+ type: :runtime
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: yard
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ hash: 5
73
+ segments:
74
+ - 0
75
+ - 7
76
+ version: "0.7"
77
+ type: :development
78
+ version_requirements: *id004
79
+ description: Ruby script to talk to chefkitchen web services.
80
+ email:
81
+ - darrendao@gmail.com
82
+ executables:
83
+ - chefkitchen
84
+ extensions: []
85
+
86
+ extra_rdoc_files:
87
+ - README.md
88
+ files:
89
+ - README.md
90
+ - bin/chefkitchen
91
+ - chefkitchen_cli.gemspec
92
+ - lib/cookie_jar.rb
93
+ - lib/request_helper.rb
94
+ homepage: https://github.com/darrendao/chefkitchen_cli
95
+ licenses: []
96
+
97
+ post_install_message:
98
+ rdoc_options: []
99
+
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ requirements: []
121
+
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.11
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Ruby script to talk to chefkitchen web services.
127
+ test_files: []
128
+
129
+ has_rdoc: