cenvup 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d3f4737ce3d91a3a8fac0134fa3e6f3f16194a2
4
+ data.tar.gz: 20043e08e98ec0644ae70fed777bc2d478c047fd
5
+ SHA512:
6
+ metadata.gz: d1b6405812b6bab382b0114976bbfc08eb932423f3dce7f27696afae5552a7194ec27d259b5a254386e1fb5eb1136faa57d9d567a0f8694edb5c54750ea137a0
7
+ data.tar.gz: 211dd9519d6838bb35e3052c2feaa537fb1f401fca092c791346cdf8168a73f422d2cc827cdc9112418875c3173a37ff778dabf67e8305fbe826da7b61fd6c70
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .DS_STORE
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Yann David
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Cenvup
2
+
3
+ Upload environments to chef server without being afraid of overwriting something.
4
+
5
+ This gem was created by the Devtools Team @ Lightspeed POS as an internal tool to upload environments to our chef server without being worry of overwriting it with an old local version.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cenvup'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cenvup
22
+
23
+ ## Usage
24
+
25
+ Upon first use, you will be prompt to enter the path to your "Kitchen". This tool will look for environments file under <KITCHEN_PATH>/environments.
26
+
27
+ Then you can use it from anywhere on your system and will issue the knife upload from <environments_path>.
28
+
29
+ ```bash
30
+ cenvup list
31
+ ```
32
+
33
+ Should give you which environment you can use, then upload it with this command (without the file extension)
34
+
35
+ ```bash
36
+ cenvup from_file <environment>
37
+ ```
38
+
39
+ ## Configuration
40
+
41
+ For now only one configuration is available int `$HOME/.cenvup/config.json`
42
+
43
+ ```json
44
+ {
45
+ "environments_path": "/Users/yanndavid/development/devtools/devtools-kitchen"
46
+ }
47
+ ```
48
+
49
+ You can manually change it.
50
+
51
+ ## To-Do
52
+
53
+ - Only support .json for environment files for now, will auto-detect in the futur.
54
+ - Fix auto-discovery for kitchen path.
55
+ - Remove requirement for environments folder under <environments_path>
56
+ - Moar fixes.
57
+
58
+ ## Help Me
59
+
60
+ ```ruby
61
+ def help_me
62
+ puts 'Usage: cenvup (option)'
63
+ puts ''
64
+ puts 'Available options:'
65
+ puts ' from_file <ENV> Upload <ENV> to chef server'
66
+ puts ' locate_kitchen Do a lookup for Kitchen from $HOME'
67
+ puts ' help This friendly help message'
68
+ puts ' list List available environments'
69
+ exit 1
70
+ end
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( https://github.com/[my-github-username]/cenvup/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/cenvup ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/cenvup/cli'
4
+ require 'json'
5
+ require 'find'
6
+ require 'highline/import'
7
+ require 'colorize'
8
+ require 'timeout'
9
+
10
+ Cenvup::CLI.new
data/cenvup.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cenvup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cenvup"
8
+ spec.version = Cenvup::VERSION
9
+ spec.authors = ["Yann David (Typositoire)"]
10
+ spec.email = ["davidyann88@gmail.com"]
11
+ spec.summary = %q{ Cenvup is used to upload Chef Environment to Chef Server. }
12
+ spec.description = %q{ Cenvup is used to upload Chef Environment to Chef Server.
13
+ This tool actually wrap around "Knife environment from file"
14
+ and adds some validation to make sure we don't push outdated environments
15
+ }
16
+ spec.homepage = "http://github.com"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\u0000")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake"
26
+
27
+ spec.add_dependency 'highline'
28
+ spec.add_dependency "json"
29
+ spec.add_dependency "colorize"
30
+ end
data/lib/cenvup/cli.rb ADDED
@@ -0,0 +1,234 @@
1
+ module Cenvup
2
+ class CLI
3
+ def initialize
4
+ @conf = ::File.join(ENV['HOME'], ".cenvup/config.json")
5
+
6
+ help_me if ARGV.count == 0
7
+ help_me if ARGV[0] =~ /^(-h|--help|help)$/
8
+ help_me unless ARGV[0] =~ /^(from_file|list)$/
9
+ run
10
+ end
11
+
12
+ def validate_conf(config_file)
13
+ config = File.read(config_file)
14
+ begin
15
+ JSON.parse(config)
16
+ rescue JSON::ParserError => e
17
+ puts "cenvup.json is not a valid json : #{e}"
18
+ nb_lines = `wc -l #{config_file}`.split(" ")[0]
19
+ unless nb_lines.to_i > 2
20
+ config_init(config_file)
21
+ end
22
+ run
23
+ end
24
+ end
25
+
26
+ def config_init(cenvup_conf)
27
+ puts "Creating empty config at #{cenvup_conf}".green
28
+ mkdir_conf = "mkdir #{::File.join(ENV['HOME'], ".cenvup")}"
29
+ touch_conf = "touch #{cenvup_conf}"
30
+ unless ::File.exist?(::File.join(ENV['HOME'], ".cenvup"))
31
+ `#{mkdir_conf}`
32
+ end
33
+ `#{touch_conf}`
34
+
35
+ prompt_config
36
+ end
37
+
38
+ def config_check(cenvup_conf)
39
+ if ::File.exist?(cenvup_conf)
40
+ validate_conf(cenvup_conf)
41
+ else
42
+ puts "Could not locate cenvup.json at #{cenvup_conf}, launching config_init".yellow
43
+ config_init(cenvup_conf)
44
+ end
45
+ end
46
+
47
+
48
+ # WIP
49
+ def find_kitchen
50
+ Find.find(ENV['HOME']) do |p|
51
+ if p.match(/kitchen/)
52
+ return p
53
+ Find.prune
54
+ end
55
+ end
56
+ end
57
+
58
+ def locate_kitchen
59
+ puts "Trying to locate kitchen".green
60
+ search = find_kitchen
61
+
62
+ if search.nil? || search.empty?
63
+ puts "Could not locate kitchen and environments_path is not setup in #{@conf}".red
64
+ exit 1
65
+ else
66
+ config = {
67
+ "environments_path" => search
68
+ }
69
+ File.open(@conf, 'w') do |f|
70
+ f.puts JSON.pretty_generate(config)
71
+ end
72
+
73
+ puts "Kitchen found and added to #{@conf}".green
74
+ end
75
+ end
76
+
77
+ def prompt_config
78
+ # puts "No config to your environments location was found.\nIf you are not using devtools-kitchen, please enter path manually.\nOtherwise you can try auto-discovery.\n".yellow
79
+ puts "No config to your environments location was found.".yellow
80
+ puts "1. Enter path manually".green
81
+ # puts "2. Try discovery".green
82
+ puts "0. Exit".green
83
+ menu_choice = ask("Choice: ".green, Integer)
84
+
85
+ case menu_choice
86
+ when 1
87
+ path = ask("Kitchen full path: ".green)
88
+ if ::File.exist?(path.to_s)
89
+ puts "Using #{path}".green
90
+ config = {
91
+ "environments_path" => path
92
+ }
93
+ File.open(@conf, 'w') do |f|
94
+ f.puts JSON.pretty_generate(config)
95
+ end
96
+ else
97
+ puts "Path does not exist. Exiting".red
98
+ exit 1
99
+ end
100
+ when 2
101
+ locate_kitchen
102
+ when 0
103
+ puts "Exiting.".red
104
+ exit 1
105
+ else
106
+ puts "Wrong input detected".red
107
+ end
108
+ end
109
+
110
+ def list_environments
111
+ config = JSON.parse(File.read(@conf))
112
+
113
+ unless config['environments_path']
114
+ prompt_config
115
+ config = JSON.parse(File.read(@conf))
116
+ end
117
+
118
+ listing = ::Dir.entries(::File.join(config['environments_path'], "environments"))
119
+ puts "Listing environments: ".green
120
+ listing.each do |d|
121
+ file = d.gsub(".json", "")
122
+ puts " #{file}".green unless file =~ /^\./
123
+ end
124
+ end
125
+
126
+ def verify_kitchen_branch_status?(kitchen)
127
+ status = "git -C #{kitchen} status"
128
+ fetch = "git -C #{kitchen} fetch"
129
+ status_result = ""
130
+ begin
131
+ Timeout.timeout(10) do
132
+ status_result = `#{fetch};#{status}`.yellow
133
+ end
134
+ rescue Timeout::Error
135
+ puts "Command timeout.".red
136
+ puts "Aborting.".red
137
+ exit 1
138
+ end
139
+
140
+ if status_result.match(/On\sbranch\smaster\n/)
141
+ if status_result.match(%r[Your\sbranch\sis\sup-to-date\swith\s'origin/master'])
142
+ if status_result.match(/nothing\sto\scommit/)
143
+ return true
144
+ else
145
+ puts "You have uncommited work.".red
146
+ puts "Aborting.".red
147
+ return false
148
+ end
149
+ else
150
+ puts "On master branch but origin master is ahead. You should update your master branch!".red
151
+ puts "Aborting.".red
152
+ return false
153
+ end
154
+ else
155
+ puts "Not on master branch.".red
156
+ puts "Aborting.".red
157
+ return false
158
+ end
159
+ end
160
+
161
+ def from_file(env)
162
+ config = JSON.parse(File.read(@conf))
163
+
164
+ file_name = "#{env}.json"
165
+
166
+ if verify_kitchen_branch_status?(config['environments_path'])
167
+ chef_server_url = ""
168
+ org_name = ""
169
+ if File.exist?(::File.join(config['environments_path'], ".chef/knife.rb"))
170
+ File.open("#{::File.join(config['environments_path'], ".chef/knife.rb")}").each do |line|
171
+ if line.match(/chef_server_url/)
172
+ chef_server_url = line.split(" ")[1]
173
+ end
174
+
175
+ if line.match(/^org_name/)
176
+ org_name = line.split(" ")[2]
177
+ end
178
+ end
179
+ else
180
+ exit 1
181
+ end
182
+
183
+ # puts chef_server_url
184
+ if ::File.exist?(::File.join(config['environments_path'], "environments", file_name))
185
+ cmd = "knife environment from file #{::File.join(config['environments_path'], "environments", file_name)}"
186
+ puts "Kitchen on master and up to date.".green
187
+ puts "Ready to upload #{::File.join(config['environments_path'], "environments", file_name)}".green
188
+ puts "*** Command: #{cmd} ***".green
189
+ prompt = ask('Do you want to proceed? (y/n)'.green)
190
+ case prompt
191
+ when "y"
192
+ puts "Uploading #{file_name} to #{chef_server_url}".gsub("\#{org_name}", org_name[1..-2]).green
193
+ puts `(cd #{config['environments_path']} && #{cmd})`.green
194
+ when "n"
195
+ puts "Cancelling.".red
196
+ exit 1
197
+ else
198
+ puts "Invalid choice".red
199
+ exit 1
200
+ end
201
+ else
202
+ raise "Environment file #{::File.join(config['environments_path'], "environments", file_name)} could not be found".red
203
+ end
204
+ end
205
+ end
206
+
207
+ def run
208
+ config_check(@conf)
209
+
210
+ case ARGV[0]
211
+ when 'from_file'
212
+ from_file(ARGV[1])
213
+ when 'locate_kitchen'
214
+ puts "WIP"
215
+ # locate_kitchen
216
+ when 'list'
217
+ list_environments
218
+ else
219
+ exit 1
220
+ end
221
+ end
222
+
223
+ def help_me
224
+ puts 'Usage: cenvup (option)'
225
+ puts ''
226
+ puts 'Available options:'
227
+ puts ' from_file <ENV> Upload <ENV> to chef server'
228
+ puts ' locate_kitchen Do a lookup for Kitchen from $HOME'
229
+ puts ' help This friendly help message'
230
+ puts ' list List available environments'
231
+ exit 1
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,3 @@
1
+ module Cenvup
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cenvup.rb ADDED
@@ -0,0 +1,2 @@
1
+ module Cenvup
2
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cenvup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yann David (Typositoire)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: highline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: colorize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: " Cenvup is used to upload Chef Environment to Chef Server.\n This
84
+ tool actually wrap around \"Knife environment from file\"\n and
85
+ adds some validation to make sure we don't push outdated environments\n "
86
+ email:
87
+ - davidyann88@gmail.com
88
+ executables:
89
+ - cenvup
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".gitignore"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/cenvup
99
+ - cenvup.gemspec
100
+ - lib/cenvup.rb
101
+ - lib/cenvup/cli.rb
102
+ - lib/cenvup/version.rb
103
+ homepage: http://github.com
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.4
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Cenvup is used to upload Chef Environment to Chef Server.
127
+ test_files: []
128
+ has_rdoc: