socore 0.0.1

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/socore +6 -0
  3. data/lib/socore.rb +209 -0
  4. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6da26b805e9c89db93d990b76a43b2684db5c610
4
+ data.tar.gz: dcd529b4632fd5584b40a715bd803655b92eead8
5
+ SHA512:
6
+ metadata.gz: 822b556a76cb2b6c31713dbded645a48ecdaa9640b847a4b84ed8c36b1ef709f4904497b37b06f1779103491e34faa18aa6b899360532cc107385809be9de47a
7
+ data.tar.gz: cce91ec7a6f033e7899299e80144a488b1c56fe1e1ef0da960ea12ea45a9f7d6555b3d3dde14c971fdbb230daa193d4f50df0a06b59a1a709fe06950e385efdc
data/bin/socore ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'socore'
3
+
4
+ core = Socore.new
5
+
6
+ core.socore
data/lib/socore.rb ADDED
@@ -0,0 +1,209 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class Socore
4
+ $debug = false
5
+ $pwd = Dir.pwd
6
+ $lock_file = "socore.lock"
7
+ $config_file = "socore.conf"
8
+
9
+ def read_config(config_file)
10
+ $cookbooks = {}
11
+ puts "Config: #{File.join($pwd, config_file)}"
12
+ File.readlines(File.join($pwd, config_file)).each do |line|
13
+ #puts "DEBUG - read_config - Line: #{line}" if $debug
14
+ line.gsub!("\n", "")
15
+ regex_result = line.match(/(\w+)?cookbook '([-\w]+)', :git => '([^']*)'/)
16
+ #puts "DEBUG - read_config - regex[2]: #{regex_result[2].to_s}, regex[3]: #{regex_result[3].to_s}" if $debug
17
+ $cookbooks[regex_result[2].to_s] = regex_result[3].to_s
18
+ end
19
+ $cookbooks.each do |cookbook, uri|
20
+ #puts "DEBUG - Found cookbook: #{cookbook} URI: #{uri}"
21
+ end unless $cookbooks.nil? if $debug
22
+ $cookbooks
23
+ end
24
+
25
+ def read_lockfile(lock_file)
26
+ $locks = {}
27
+ begin
28
+ File.readlines(File.join($pwd, lock_file)).each do |line|
29
+ line.gsub!("\n", "")
30
+ regex_result = line.match(/(\w+)?cookbook '([-\w]+)', :git_sha => '([^']*)', :dir_hash => '([^']*)'/)
31
+ $locks[regex_result[2].to_s] = { :git_sha => regex_result[3].to_s, :dir_hash => regex_result[4].to_s }
32
+ end
33
+ rescue
34
+ puts "No lockfile found"
35
+ end
36
+ $locks
37
+ end
38
+
39
+ def get_dir_hash(cookbook)
40
+ require 'find'
41
+ require 'digest/md5'
42
+
43
+ #puts "DEBUG - get_dir_hash - cookbooks: #{cookbooks.inspect}" if $debug
44
+ #puts " - get_dir_hash -" if $debug
45
+ # Find all directories in cookbook folder and add to an array
46
+ #puts "DEBUG - Getting hash for '#{cookbook}' cookbook" if $debug
47
+
48
+ $cookbook_dir = File.join($pwd, cookbook)
49
+ $directories = []
50
+ puts "DEBUG - get_dir_hash - Looking for all files in #{$cookbook_dir}" if $debug
51
+ begin
52
+ Find.find($cookbook_dir) do |path|
53
+ if FileTest.directory?(path)
54
+ if File.basename(path) == '../' || File.basename(path) == './' || File.basename(path) == '.git'
55
+ Find.prune
56
+ else
57
+ $directories << path
58
+ puts path if $debug
59
+ next
60
+ end
61
+ end
62
+ end
63
+ # sort the array
64
+ $directories.sort!
65
+ ls_glob = ""
66
+ # run an ls on each of those directories and append the output to a string
67
+ $directories.each do |dir|
68
+ ls_glob += `ls -Altr #{dir}`
69
+ end
70
+ puts "DEBUG - get_dir_hash - [#{cookbook}] ls_glob.length: #{ls_glob.length}" if $debug
71
+ # get the hash of that string
72
+ $cookbook_hash = Digest::MD5.hexdigest ls_glob
73
+ # return the hash
74
+ rescue Exception => e
75
+ puts "Cookbook '#{cookbook}' does not yet exist."
76
+ puts "EXCEPTION: #{e.backtrace}" if $debug
77
+ end
78
+ puts "DEBUG - get_dir_hash - returning $hashes: #{$cookbook_hash.inspect}" if $debug
79
+ return $cookbook_hash
80
+ end
81
+
82
+ def install_cookbook(name, uri)
83
+ output = `rm -rf ./#{name} && git clone #{uri} #{name} >/dev/null 2>&1`
84
+ git_sha = `cd ./#{name} && git rev-parse HEAD`.chomp
85
+ output = `rm -rf ./#{name}/.git`
86
+ git_sha
87
+ end
88
+
89
+ def get_git_remote_sha(remote, branch_or_tag)
90
+ git_sha = nil
91
+ #if FileTest.directory?("./#{name}")
92
+ # git_sha = `cd ./#{name} && git rev-parse HEAD`.chomp
93
+ #end
94
+ git_sha = `git ls-remote #{remote} #{branch_or_tag}`.split(%r{\t})[0]
95
+ git_sha
96
+ end
97
+
98
+ def commit_changes(updated_cookbooks)
99
+ changed_string = ""
100
+ updated_cookbooks.each { |entry| changed_string << "#{entry.to_s}, " }
101
+ changed_string.chomp!
102
+ results = `git add . --all && git commit -m "SOCORE - Commiting changed cookbooks" && git push >/dev/null 2>&1`
103
+ results
104
+ end
105
+
106
+ def update_lockfile(lock_file, name, cur_git_sha, cur_dir_hash)
107
+ locks = read_lockfile(lock_file)
108
+ puts " - READ LOCKFILE - " if $debug
109
+ locks.each do |lock|
110
+ puts " - LOCK: #{lock.inspect}"
111
+ end if $debug
112
+ locks[name.to_s] = { :git_sha => cur_git_sha.to_s, :dir_hash => cur_dir_hash.to_s }
113
+ # write locks to file converting to locks format
114
+ puts " - WRITE LOCKFILE - LockFile: #{lock_file}" if $debug
115
+
116
+ file = File.open(File.join($pwd, lock_file), 'w')
117
+ file.truncate(file.size)
118
+ locks.each do |cookbook, data|
119
+ puts " - LOCK: Cookbook: #{cookbook}, Data: #{data}" if $debug
120
+ file.write("cookbook '#{cookbook}', :git_sha => '#{data[:git_sha]}', :dir_hash => '#{data[:dir_hash]}'\n")
121
+
122
+ end
123
+ file.close()
124
+ end
125
+
126
+ def socore
127
+ cookbooks = read_config($config_file)
128
+ locks = read_lockfile($lock_file)
129
+ $updated_cookbooks = {}
130
+
131
+ if $debug
132
+ locks.each do |cookbook, data|
133
+ puts "DEBUG - Lock: #{cookbook}, Git SHA: #{data[:git_sha]}, Dir Hash: #{data[:dir_hash]}"
134
+ end
135
+ #hashes.each do |hash|
136
+ # puts "DEBUG - Hash: #{hash}"
137
+ #end unless hashes.empty?
138
+ end
139
+
140
+ cookbooks.each do |name, uri|
141
+ puts " - Processing cookbook '#{name}', URI: #{uri}"
142
+
143
+ # Check lockfile for currently pulled hash
144
+ puts "Getting lock data..." if $debug
145
+ lock_data = locks[name.to_s] || nil
146
+ tag_or_branch = tag_or_branch || "HEAD"
147
+
148
+ puts "Getting lock_git_sha..." if $debug
149
+ lock_git_sha = lock_data[:git_sha] if !lock_data.nil?
150
+
151
+ # TODO - Compare remote SHA to lock file SHA.
152
+ # if same - compare dir hashes
153
+ # if different - update local copy
154
+ puts "Getting cur_git_remote_sha..." if $debug
155
+ cur_git_remote_sha = get_git_remote_sha(uri, tag_or_branch)
156
+
157
+ puts "Getting lock_dir_hash..." if $debug
158
+ lock_dir_hash = lock_data[:dir_hash] if !lock_data.nil?
159
+ puts "Getting cur_dir_hash..." if $debug
160
+ cur_dir_hash = get_dir_hash(name) || nil
161
+ puts "DEBUG - inspect cur_dir_hash: [#{name}] #{cur_dir_hash.inspect}" if $debug
162
+
163
+ # get the SHA without checking out the cookbook here
164
+ # cur_git_sha = install_cookbook(name, uri)
165
+
166
+ puts "-DEBUG - lock_git_sha: #{lock_git_sha}" unless lock_git_sha.nil? if $debug
167
+ puts "-DEBUG - cur_git_remote_sha: #{cur_git_remote_sha} " unless cur_git_remote_sha.nil? if $debug
168
+ puts "-DEBUG - lock_dir_hash: #{lock_dir_hash}" unless lock_dir_hash.nil? if $debug
169
+ puts "-DEBUG - cur_dir_hash: #{cur_dir_hash}" unless cur_dir_hash.nil? if $debug
170
+ if lock_git_sha.nil? || lock_dir_hash.nil?
171
+ # No lock data is present so we should check out the repository, get the git sha and dir hash and write those to the lock file
172
+ puts "#{name} has no metadata, installing a fresh copy..."
173
+ # Clone the cookbook repository and get the current sha
174
+ # FIX
175
+ # TODO !!!!!
176
+ # implement a check to see if the remote is the same but local has changed and ask user if they want to blow away local changes!!!!
177
+ # !!!!!
178
+ cur_git_sha = install_cookbook(name, uri)
179
+ cur_dir_hash = get_dir_hash(name)
180
+ # Need to update cur_git_sha and cur_dir_hash here
181
+ puts "DEBUG - Updating LOCK file with - Cookbook: #{name}, cur_git_sha: #{cur_git_sha}, cur_dir_hash: #{cur_dir_hash}" if $debug
182
+ update_lockfile($lock_file, name, cur_git_sha, cur_dir_hash)
183
+ $updated_cookbooks[name] = {"git_sha" => cur_git_sha.to_s, "dir_hash" => cur_dir_hash.to_s}
184
+ # add new sha and git repo name to changed hash
185
+ elsif lock_dir_hash == cur_dir_hash && lock_data[:git_sha] == cur_git_remote_sha
186
+ #elsif lock_git_sha == cur_git_sha && lock_dir_hash == cur_dir_hash
187
+ # if hash - compare to git.has file in dir & compare directories hash to git hash to make sure it has not changed locally either
188
+ # if hashes are the same - do nothing
189
+ puts "#{name} is up to date" if $debug
190
+ else
191
+ # else - have git download the repo, create the git.hash file, write the git hash and directory sha to the pull.lock
192
+ puts "#{name} needs an update..."
193
+ cur_git_sha = install_cookbook(name, uri)
194
+ cur_dir_hash = get_dir_hash(name)
195
+ puts "DEBUG - Current GIT SHA: #{cur_git_sha}" if $debug
196
+ update_lockfile($lock_file, name, cur_git_sha, cur_dir_hash)
197
+ $updated_cookbooks[name] = {"git_hash" => cur_git_sha.to_s, "dir_hash" => cur_dir_hash.to_s}
198
+ # add new sha and git repo name to changed hash
199
+ end
200
+ end
201
+ if !$updated_cookbooks.empty?
202
+ # if changed - commit all changes and push using changed hash as comment
203
+ puts "Cookbooks updated..."
204
+ commit_changes($updated_cookbooks)
205
+ end
206
+ puts "Done..."
207
+ end
208
+ end
209
+
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: socore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Philip Hutchins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Builds a repository from other repositories intended for Chef Solo
14
+ email: flipture@gmail.com
15
+ executables:
16
+ - socore
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/socore
21
+ - lib/socore.rb
22
+ homepage: http://phutchins.com/
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: SoCoRe
46
+ test_files: []
47
+ has_rdoc: