fig-lock 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fig/lock.rb +146 -146
  3. data/lib/fig/lock/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf65230525cc94224a28ccc8387a29c5de3557d4
4
- data.tar.gz: 7eaa81a30dc3a9611b8fb5a4ce22797a00d1d4b0
3
+ metadata.gz: 1019d9bc52a6930d08d2081f060a7e1f6b279bd0
4
+ data.tar.gz: d6f8c98733fc2ecd62f3a9e3a605df0e51e8bfe9
5
5
  SHA512:
6
- metadata.gz: 3e5808866906d23d274d4c250a5c91a4e95ac7f147bde50431799b68e1e6ba755f6d51e7936f5d83999c1d77a30211fb81a20cc179e01d9b6f9bb506342937a6
7
- data.tar.gz: 951e80d34a26d8a612e82027540a04694683a5881b04d8894a994122e1b911cae6a510e90a7e16baa96d23d4f1c589204fc3837c405fd22683a3fde9592ae1df
6
+ metadata.gz: 007170d706e208f31cb555abb0d5469aab2a7cf4d49f074b4354cffdd9f584d4dc2c27bb2943ea52e37dbaac5029a94f30498638e5e2b68844a582bc27456f42
7
+ data.tar.gz: 54b9256feeafded12eada0bdaa82018f4ae17809c1f7c643f582c36b0313b420fb451a614a5c188106e04bf47cadbfc70fef499443474092eb96e16e32103b4f
@@ -4,158 +4,158 @@ require 'fig/lock/log'
4
4
  require 'fig/lock/version'
5
5
 
6
6
  module Fig
7
+ class Lock
8
+ class << self
9
+ def install(opts={})
10
+ Lock.new(opts).install
11
+ end
12
+
13
+ def update(opts={})
14
+ Lock.new(opts).update
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ module Fig
7
21
  class Lock
8
- class << self
9
- def install(opts={})
10
- Lock.new(opts).install
11
- end
22
+ include Fig::Lock::Log
23
+
24
+ attr_reader :lock_file, :file
25
+
26
+ def initialize(opts)
27
+ opts = merge_opts(opts)
28
+ @file = opts[:file]
29
+ @lock_file = lock_file_name(file)
12
30
 
13
- def update(opts={})
14
- Lock.new(opts).update
15
- end
16
- end
31
+ fail "File #{file} does not exist" unless File.exists?(file)
32
+ fail "File #{file} is a directory" if File.directory?(file)
33
+ end
34
+
35
+ # Install images from a lock file on the local system
36
+ def install
37
+ if File.exists?(lock_file)
38
+ log.info "Lock file #{lock_file} found."
39
+ fetch_images(YAML.load(File.read(lock_file)))
40
+ else
41
+ log.info "No lock file found."
42
+ update
43
+ end
17
44
  end
45
+
46
+ # Update an existing lock file, or create if none exists
47
+ def update
48
+ log.info "Generating lock file for #{file} ..."
49
+ hash = YAML.load(File.read(file))
50
+ select_latest(hash)
51
+
52
+ log.info "Writing lock file #{lock_file} ..."
53
+ File.write(lock_file, hash.to_yaml)
54
+
55
+ log.info "Done."
56
+ end
57
+
58
+ private
59
+
60
+ # Construct the name of the lock file from the name of the fig file
61
+ def lock_file_name(file)
62
+ index = file.rindex('.')
63
+ raise "Input file #{file} has no file extension" unless index
64
+ file[0, index] << '.lock'
65
+ end
66
+
67
+ # Select the latest version for images in the fig hash. Updates the input hash.
68
+ def select_latest(hash)
69
+ log.info "Selecting latest tags:"
70
+
71
+ resolved_tags = {}
72
+
73
+ old_lock_yaml = lock_yaml
74
+
75
+ hash.each do |k,v|
76
+ image = v['image']
77
+ next unless image
78
+ image = "#{image}:latest" unless image.index(':')
79
+ log.info "Selecting latest tag for #{image} ..."
80
+
81
+ unless image.nil?
82
+ result = resolved_tags[image]
83
+ if result
84
+ log.debug "Using previously resolved image: #{result}"
85
+ else
86
+ # Fetch tags
87
+ tags = docker_client.tags(image)
88
+ latest = tags['latest']
89
+ fail "Image #{image} has no latest tag" if latest.nil?
90
+
91
+ # Figure out which one corresponds to "latest"
92
+ version = tags.detect{|k,v|
93
+ v == latest && k != 'latest'
94
+ }
95
+ version = version[0] if version
96
+ fail "No matching version found for hash #{latest}" unless version
97
+
98
+ result = "#{image[0..image.rindex(':')-1]}:#{version}"
99
+ log.debug "Resolved image: #{result}"
100
+ resolved_tags[image] = result
18
101
  end
19
102
 
20
- module Fig
21
- class Lock
22
- include Fig::Lock::Log
23
-
24
- attr_reader :lock_file, :file
25
-
26
- def initialize(opts)
27
- opts = merge_opts(opts)
28
- @file = opts[:file]
29
- @lock_file = lock_file_name(file)
30
-
31
- fail "File #{file} does not exist" unless File.exists?(file)
32
- fail "File #{file} is a directory" if File.directory?(file)
33
- end
34
-
35
- # Install images from a lock file on the local system
36
- def install
37
- if File.exists?(lock_file)
38
- log.info "Lock file #{lock_file} found."
39
- fetch_images(YAML.load(File.read(lock_file)))
40
- else
41
- log.info "No lock file found."
42
- update
43
- end
44
- end
45
-
46
- # Update an existing lock file, or create if none exists
47
- def update
48
- log.info "Generating lock file for #{file} ..."
49
- hash = YAML.load(File.read(file))
50
- select_latest(hash)
51
-
52
- log.info "Writing lock file #{lock_file} ..."
53
- File.write(lock_file, hash.to_yaml)
54
-
55
- log.info "Done."
56
- end
57
-
58
- private
59
-
60
- # Construct the name of the lock file from the name of the fig file
61
- def lock_file_name(file)
62
- index = file.rindex('.')
63
- raise "Input file #{file} has no file extension" unless index
64
- file[0, index] << '.lock'
65
- end
66
-
67
- # Select the latest version for images in the fig hash. Updates the input hash.
68
- def select_latest(hash)
69
- log.info "Selecting latest tags:"
70
-
71
- resolved_tags = {}
72
-
73
- old_lock_yaml = lock_yaml
74
-
75
- hash.each do |k,v|
76
- image = v['image']
77
- continue unless image
78
- image = "#{image}:latest" unless image.index(':')
79
- log.info "Selecting latest tag for #{image} ..."
80
-
81
- unless image.nil?
82
- result = resolved_tags[image]
83
- if result
84
- log.debug "Using previously resolved image: #{result}"
85
- else
86
- # Fetch tags
87
- tags = docker_client.tags(image)
88
- latest = tags['latest']
89
- fail "Image #{image} has no latest tag" if latest.nil?
90
-
91
- # Figure out which one corresponds to "latest"
92
- version = tags.detect{|k,v|
93
- v == latest && k != 'latest'
94
- }
95
- version = version[0] if version
96
- fail "No matching version found for hash #{latest}" unless version
97
-
98
- result = "#{image[0..image.rindex(':')-1]}:#{version}"
99
- log.debug "Resolved image: #{result}"
100
- resolved_tags[image] = result
101
- end
102
-
103
- # Update hash
104
- v['image'] = result
105
- if old_lock_yaml && old_lock_yaml[k] && old_lock_yaml[k]['image'] == result
106
- log.debug "Component #{k} already using tag: #{result}"
107
- else
108
- log.info "Component #{k} updated to tag: #{result}"
109
- end
110
- end
111
- end
112
- end
113
-
114
- def lock_yaml
115
- if File.exists?(lock_file)
116
- YAML.load(File.read(lock_file))
117
- end
118
- end
119
-
120
- # Fetch all images from the fig hash.
121
- def fetch_images(hash)
122
- log.info "Fetching images:"
123
- hash.each do |k,v|
124
- image = v['image']
125
- if image
126
- log.info "Fetching #{image} ..."
127
- system "sudo docker pull #{image}"
128
- fail "Unable to fetch image: #{image}.\n\tExit code:#{$?.exitstatus}" unless $?.exitstatus.zero?
129
- end
130
- end
131
- log.info "Done fetching images."
132
- end
133
-
134
- def merge_opts(opts)
135
- Defaults.opts.merge(opts).tap do |hash|
136
- fail("file must be specified") unless hash[:file]
137
- end
138
- end
139
-
140
- def docker_client
141
- @docker_client ||= DockerClient.new
142
- end
143
- end
144
-
145
- # Default values for parameters
146
- class Defaults
147
- class << self
148
- def opts
149
- {
150
- file: file
151
- }
152
- end
153
-
154
- def file
155
- ENV.fetch 'FIG_FILE', File.join(__dir__, 'fig.yml')
156
- end
157
- end
103
+ # Update hash
104
+ v['image'] = result
105
+ if old_lock_yaml && old_lock_yaml[k] && old_lock_yaml[k]['image'] == result
106
+ log.debug "Component #{k} already using tag: #{result}"
107
+ else
108
+ log.info "Component #{k} updated to tag: #{result}"
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ def lock_yaml
115
+ if File.exists?(lock_file)
116
+ YAML.load(File.read(lock_file))
117
+ end
118
+ end
119
+
120
+ # Fetch all images from the fig hash.
121
+ def fetch_images(hash)
122
+ log.info "Fetching images:"
123
+ hash.each do |k,v|
124
+ image = v['image']
125
+ if image
126
+ log.info "Fetching #{image} ..."
127
+ system "sudo docker pull #{image}"
128
+ fail "Unable to fetch image: #{image}.\n\tExit code:#{$?.exitstatus}" unless $?.exitstatus.zero?
129
+ end
130
+ end
131
+ log.info "Done fetching images."
132
+ end
133
+
134
+ def merge_opts(opts)
135
+ Defaults.opts.merge(opts).tap do |hash|
136
+ fail("file must be specified") unless hash[:file]
158
137
  end
138
+ end
139
+
140
+ def docker_client
141
+ @docker_client ||= DockerClient.new
142
+ end
143
+ end
144
+
145
+ # Default values for parameters
146
+ class Defaults
147
+ class << self
148
+ def opts
149
+ {
150
+ file: file
151
+ }
152
+ end
153
+
154
+ def file
155
+ ENV.fetch 'FIG_FILE', File.join(__dir__, 'fig.yml')
156
+ end
157
+ end
158
+ end
159
159
 
160
160
 
161
161
  end
@@ -1,5 +1,5 @@
1
1
  module Fig
2
2
  class Lock
3
- VERSION = "0.0.10"
3
+ VERSION = "0.0.11"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fig-lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Shea
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-23 00:00:00.000000000 Z
11
+ date: 2014-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client