retag 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0cbe24b954c5ae0cc55386882c55d0d056239c20df8780ed732346e6fc577656
4
- data.tar.gz: e71492bf16f2deb5ca71ca252a8324511c9469c20a5502cd00a703968c0782ae
3
+ metadata.gz: e2df15980e560df6495c0565d3b2eef9ea085c34ff1756dc53861e058c31833f
4
+ data.tar.gz: 0db02684208f5b8e4b97a6230495025a9033d7b8dfc2ecb3df4294c03b79eec6
5
5
  SHA512:
6
- metadata.gz: bfb8cc1fa4fed581f33e96c53a0258ed9b9532d163a532a554d492e988202411eb70f23a8542f5b68acd7b79f1440d2f9429b0b454ed2f1d1a6873a2427caf1c
7
- data.tar.gz: 7e764cecb0bfd5015a7c7b0f91b2cefbc72008421ede60e40c21d72ca8bb3806812120f7e35faa57c3c41c7da673ed2d7b9a483737e7bb46288dcdbbc1255bcc
6
+ metadata.gz: 1d71f1915637a2ca9b256094d49f615cc4f54413338467f82589853f172a4fb41992eb58db19b7bea0ae45743438c0ca80d3409994b2d1450e18ea30473ad762
7
+ data.tar.gz: 87ad3d50a71c22574565437d3192333bfe47a006ecdc1d8f26d86f3eba1a5e2a884bf7699176b82f528bd91c07761f629169bd87067619f6e3b373514a061520
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- retag (0.1.2)
4
+ retag (0.1.3)
5
5
  activesupport (~> 6.0)
6
6
  colorize
7
+ gitlab
7
8
  json
8
9
  thor
9
10
  uri
@@ -28,11 +29,21 @@ GEM
28
29
  rexml
29
30
  diff-lcs (1.5.0)
30
31
  docile (1.4.0)
32
+ gitlab (4.18.0)
33
+ httparty (~> 0.18)
34
+ terminal-table (>= 1.5.1)
31
35
  hashdiff (1.0.1)
36
+ httparty (0.20.0)
37
+ mime-types (~> 3.0)
38
+ multi_xml (>= 0.5.2)
32
39
  i18n (1.8.11)
33
40
  concurrent-ruby (~> 1.0)
34
41
  json (2.6.1)
42
+ mime-types (3.4.1)
43
+ mime-types-data (~> 3.2015)
44
+ mime-types-data (3.2022.0105)
35
45
  minitest (5.15.0)
46
+ multi_xml (0.6.0)
36
47
  parallel (1.21.0)
37
48
  parser (3.1.0.0)
38
49
  ast (~> 2.4.1)
data/bin/retag CHANGED
@@ -5,46 +5,23 @@ require 'thor'
5
5
  require 'colorize'
6
6
  require 'retag'
7
7
 
8
- UTIL = File.basename(__FILE__)
9
8
  ENV_PREFIX = 'RETAG'
10
9
 
11
10
  $logger = ActiveSupport::TaggedLogging.new(Retag::Logger.new(STDOUT))
12
- $logger.level = :debug
13
-
14
- # config = YAML.load_file('release.yml')
15
- # release = config['release']
16
- # services = config['services'].map{|(name, cfg)| Service.new(name, cfg, release)}
17
-
18
- # services.each do |srv|
19
- # $logger.tagged(srv.name, srv.revision) do
20
- # $logger.info "==> Releasing service #{srv.name} from #{srv.repo} at #{srv.branch}:"
21
- # $logger.shift do
22
- # #$logger.info "* Pulling service..."
23
- # #srv.pull
24
- # $logger.info "* Tagging service..."
25
- # srv.release!
26
- # #$logger.info "* Pushing service..."
27
- # #srv.push
28
- # #$logger.info "* Cleaning service..."
29
- # #srv.rm
30
- # end
31
- # end
32
- # end
33
-
34
- # $logger.info "==> #{services.count} relesed as #{release}"
11
+ $logger.level = :info
35
12
 
36
13
  class Cli < Thor
37
14
 
38
15
  def self.exit_on_failure?
39
16
  true
40
17
  end
18
+
19
+ package_name "Retag is cli to manipulate docker images in handy way.
41
20
 
42
- # class_option :plugin_gemname, default: ENV.fetch('PLUGIN_GEMNAME', 'unknown'),
43
- # desc: 'Name of the current gem'
44
- # class_option :service_image, default: ENV.fetch('SERVICE_IMAGE', 'tmpimage'),
45
- # desc: 'Docker image name to build. Taken from SERVICE_IMAGE enviroment variable'
46
- # class_option :service_tag, default: ENV.fetch('SERVICE_TAG', 'latest'),
47
- # desc: 'Docker image tag to build. Taken from SERVICE_TAG enviroment variable'
21
+ # Gitlab authorization
22
+ Using GITLAB_API_ENDPOINT and GITLAB_API_PRIVATE_TOKEN environment variable used for authorization
23
+
24
+ #{File.basename(__FILE__)}"
48
25
 
49
26
  desc 'version', 'Display version'
50
27
  map %w[-v --version] => :version
@@ -53,7 +30,7 @@ class Cli < Thor
53
30
  say Retag::VERSION
54
31
  end
55
32
 
56
- desc 'tag', 'Tag Docker image without downloading'
33
+ desc 'tag', "Tag Docker image without downloading"
57
34
  map %w[--tag] => :tag
58
35
 
59
36
  option :image, default: ENV.fetch("#{ENV_PREFIX}_TAG_IMAGE", nil),
@@ -82,6 +59,7 @@ class Cli < Thor
82
59
  image.retag!(opts['tag'])
83
60
  end
84
61
 
62
+
85
63
  desc 'release', 'Release projects bundler from release.yml manifest'
86
64
  map %w[--release] => :release
87
65
 
@@ -98,70 +76,48 @@ class Cli < Thor
98
76
  config = YAML.load_file(File.expand_path(opts['config']))
99
77
  config['release'] = opts['name'] || config['release']
100
78
 
101
- release = config['release']
79
+ release = config['release'].to_s
102
80
  suffix = config['suffix']
103
81
 
104
82
  services = config['services'].map do|(name, cfg)|
105
83
  Retag::Service.new(name, cfg, release, suffix: cfg.fetch('suffix', suffix)) unless name.start_with?('.')
106
84
  end.compact
107
85
 
86
+ tag_message = "Release #{release} at #{DateTime.now.iso8601}"
87
+
88
+ $logger.info "*** Starting '#{tag_message.colorize(mode: :bold)}'...".colorize(:cyan)
89
+
108
90
  services.each do |srv|
109
91
  $logger.tagged(srv.name.colorize(:yellow), "#{srv.revision.colorize(mode: :bold)}@#{srv.branch}") do
110
- $logger.info "#{'==>'.colorize(mode: :bold, color: :white)} Releasing service #{srv.name.colorize(:yellow)} from #{srv.repo} at #{srv.branch}:"
92
+ $logger.info "#{'==>'.colorize(mode: :bold, color: :white)} Releasing service #{srv.name.colorize(mode: :bold)} from #{srv.repo.repo} at #{srv.branch.colorize(mode: :bold)}:".colorize(:green)
111
93
  $logger.shift do
112
94
  srv.images.each do |img|
113
- $logger.info "Source image: #{img.full}"
114
- newimg = img.retag!(release)
115
- $logger.info "Release image: #{newimg.full}"
95
+ $logger.info ">>> Image #{img.image}"
96
+
97
+ $logger.shift do
98
+ $logger.info "From ".ljust(5) + "[#{img.tag.colorize(:yellow)}]: ".ljust(35) + img.full
99
+ newimg = img.retag!(release)
100
+ $logger.info "To ".ljust(5) + "[#{newimg.tag.colorize(:yellow)}]: ".ljust(35) + newimg.full
101
+ end
102
+
103
+ $logger.info ">>> Repo #{srv.repo.repo}"
104
+
105
+ $logger.shift do
106
+ $logger.info "From ".ljust(5) + "[#{srv.revision.colorize(:yellow)}]: ".ljust(35) + "#{srv.repo.repo}~#{srv.revision}@#{srv.branch}"
107
+ newimg = img.retag!(release)
108
+ $logger.info "To ".ljust(5) + "[#{release.colorize(:yellow)}]: ".ljust(35) + "#{srv.repo.repo}~#{srv.revision}@#{release}"
109
+ end
110
+
111
+ srv.repo.tag!(release, message: tag_message)
116
112
  end
117
-
118
- #$logger.info "* Pulling service..."
119
- #srv.pull
120
- #$logger.info "* Tagging service..."
121
- #srv.release!
122
- #$logger.info "* Pushing service..."
123
- #srv.push
124
- #$logger.info "* Cleaning service..."
125
- #srv.rm
126
113
  end
127
114
  end
128
115
  end
129
116
 
130
- end
117
+ $logger.info "*** Completed '#{tag_message.colorize(mode: :bold)}'".colorize(:cyan)
131
118
 
132
- private
133
-
134
- def cache_image(opts)
135
- "#{opts['service_image']}_cache:latest"
136
- end
119
+ end
137
120
 
138
121
  end
139
122
 
140
123
  ::Cli.start
141
-
142
- # puts ARGV.inspect
143
-
144
- # parser.parse!
145
-
146
- # puts ARGV.inspect
147
-
148
- # warn parser.help
149
- # exit 1
150
-
151
- # config = {
152
- # services: [
153
- # {core: {images: []}}
154
- # ]
155
- # }
156
-
157
- # release=$(cat .release | head -n 1)
158
-
159
- # for line in "${services[@]}"; do
160
- # module=$(echo $line | cut -f1 -d '|')
161
- # repo=$(echo $line | cut -f2 -d '|')
162
- # branch=$(echo $line | cut -f3 -d '|')
163
-
164
- # echo "Tagging ${module} from ${branch} of ${repo}"
165
- # git subtree push --prefix="src/${module}" "${repo}" "feature/otr"
166
- # done
167
-
data/lib/retag/image.rb CHANGED
@@ -35,18 +35,6 @@ module Retag
35
35
  Image.new(image, newtag)
36
36
  end
37
37
 
38
- def pull(img = full)
39
- cmd!("docker pull -q #{img}")
40
- end
41
-
42
- def push(img = full)
43
- cmd!("docker push -q #{img}")
44
- end
45
-
46
- def rm(img = full)
47
- cmd!("docker rmi #{img}")
48
- end
49
-
50
38
  end
51
39
  end
52
40
 
data/lib/retag/repo.rb ADDED
@@ -0,0 +1,67 @@
1
+
2
+ module Retag
3
+ class Repo
4
+
5
+ include Utils
6
+ attr_reader :repo, :branch, :revision, :name
7
+
8
+ def initialize(repo, branch)
9
+ @repo = repo
10
+ @branch = branch
11
+ @gitlab = Gitlab.client
12
+ end
13
+
14
+ def tag!(tag, message: nil)
15
+ begin
16
+ result = @gitlab.tag(name, tag)
17
+ if result.to_h.dig('commit', 'short_id').to_s == revision
18
+ # Success: tag with same revision already exists
19
+ return true
20
+ end
21
+ rescue Gitlab::Error::NotFound
22
+ # Success: there is no such tag
23
+ end
24
+
25
+ @gitlab.create_tag(name, tag, revision, message)
26
+ end
27
+
28
+ def name
29
+ @name ||= @repo.split(':').last.gsub('.git', '')
30
+ end
31
+
32
+ def revision
33
+ @revision ||= begin
34
+ rev, _name = cmd!("git ls-remote #{repo} #{branch}", capture: true).split("\n").select{|line| line["heads/#{branch}"] }.first.split
35
+ rev[0..7]
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+
42
+
43
+
44
+
45
+ # ==> /var/log/gitlab/gitlab-rails/api_json.log <==
46
+ # {"time":"2022-01-21T10:48:07.308Z","severity":"INFO",
47
+ # "duration_s":0.20214,"db_duration_s":0.00607,"view_duration_s":0.19607,"status":201,
48
+ # "method":"POST","path":"/api/v4/projects/aggredator%2Fservices%2Fuprid3/repository/tags",
49
+ # "params":[{"key":"tag_name","value":"testtag3"},{"key":"ref","value":"master"}],
50
+ # "host":"br.rnds.pro","remote_ip":"213.138.65.74, 172.22.20.31, 213.138.65.74","ua":"curl/7.79.1",
51
+ # "route":"/api/:version/projects/:id/repository/tags","user_id":38,"username":"bot",
52
+ # "queue_duration_s":0.008366,"gitaly_calls":1,"gitaly_duration_s":0.16823,"redis_calls":6,
53
+ # "redis_duration_s":0.004274,"redis_read_bytes":113,"redis_write_bytes":521,"redis_cache_calls":6,
54
+ # "redis_cache_duration_s":0.004274,"redis_cache_read_bytes":113,"redis_cache_write_bytes":521,"db_count":10,
55
+ # "db_write_count":0,"db_cached_count":0,"cpu_s":0.034309,"mem_objects":15111,"mem_bytes":920832,"mem_mallocs":3159,
56
+ # "mem_total_bytes":1525272,"correlation_id":"01FSY40ANS3TD0CJJTDCQXGQ1N","meta.user":"bot","meta.project":"aggredator/services/uprid3",
57
+ # "meta.root_namespace":"aggredator","meta.caller_id":"POST /api/:version/projects/:id/repository/tags",
58
+ # "meta.remote_ip":"213.138.65.74","meta.feature_category":"source_code_management","meta.client_id":
59
+ # "user/38","content_length":"0"}
60
+
61
+ # ==> /var/log/gitlab/gitlab-rails/api_json.log <==
62
+ # {"time":"2022-01-21T10:49:36.954Z","severity":"INFO",
63
+ # "duration_s":0.01892,"db_duration_s":0.0036,"view_duration_s":0.01532,"status":400,
64
+ # "method":"POST","path":"/api/v4/projects/aggredator%2Fservices%2Fuprid3/repository/tags",
65
+ # "params":[{"key":"tag_name","value":"44444444"},{"key":"ref","value":"509a3719"},{"key":"message","value":{"message":"hello"}},{"key":"release_description","value":""}],"host":"br.rnds.pro","remote_ip":"213.138.65.74, 172.22.20.31, 213.138.65.74","ua":"Gitlab Ruby Gem 4.18.0","route":"/api/:version/projects/:id/repository/tags","user_id":38,"username":"bot","queue_duration_s":0.007279,"db_count":6,"db_write_count":0,"db_cached_count":0,"cpu_s":0.022143,"mem_objects":11483,"mem_bytes":783064,"mem_mallocs":2634,"mem_total_bytes":1242384,"correlation_id":"01FSY432CZMEFNDJQ88HMNW0E8","meta.user":"bot","meta.project":"aggredator/services/uprid3","meta.root_namespace":"aggredator","meta.caller_id":"POST /api/:version/projects/:id/repository/tags","meta.remote_ip":"213.138.65.74","meta.feature_category":"source_code_management","meta.client_id":"user/38","content_length":"78"}
66
+
67
+
data/lib/retag/service.rb CHANGED
@@ -1,15 +1,18 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+
1
3
  module Retag
2
4
  class Service
3
5
 
4
6
  include Retag::Utils
5
7
 
6
- attr_reader :name, :config, :repo, :branch, :release, :suffix
8
+ attr_reader :name, :config, :repo, :release, :suffix
9
+
10
+ delegate :revision, :branch, to: :repo
7
11
 
8
12
  def initialize(name, config, release, suffix: )
9
13
  @name = name
10
14
  @config = config
11
- @repo = config['repo']
12
- @branch = config['branch']
15
+ @repo = Retag::Repo.new(config['repo'], config.fetch('branch', 'master'))
13
16
  @release = release
14
17
  @suffix = suffix
15
18
  end
@@ -17,33 +20,6 @@ module Retag
17
20
  def images
18
21
  @images ||= config['images'].map{|img| Retag::Image.new(img, revision, suffix: @suffix) }
19
22
  end
20
-
21
- def pull
22
- images.each(&:pull)
23
- end
24
-
25
- def push
26
- images.each(&:push)
27
- end
28
-
29
- def rm
30
- images.each(&:rm)
31
- end
32
-
33
- def release!(tag = release)
34
- images.each do |image|
35
- image.retag!(tag)
36
- end
37
- end
38
-
39
- def revision
40
- @revision ||= begin
41
- rev, _name = cmd!("git ls-remote #{repo} #{branch}", capture: true).split("\n").select{|line| line["heads/#{branch}"] }.first.split
42
- rev[0..7]
43
- end
44
- end
45
-
46
-
47
23
  end
48
24
  end
49
25
 
data/lib/retag/version.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Retag
4
4
 
5
- VERSION = '0.1.2'
5
+ VERSION = '0.1.3'
6
6
 
7
7
  end
8
8
 
data/lib/retag.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
+ require 'gitlab'
4
5
  require_relative 'retag/version'
5
6
  require_relative 'retag/logger'
6
7
  require_relative 'retag/utils'
7
8
  require_relative 'retag/image'
9
+ require_relative 'retag/repo'
8
10
  require_relative 'retag/service'
9
11
 
10
12
  module Retag
data/retag.gemspec CHANGED
@@ -35,5 +35,7 @@ Gem::Specification.new 'retag' do |spec|
35
35
  spec.add_runtime_dependency 'thor'
36
36
  spec.add_runtime_dependency 'uri'
37
37
  spec.add_runtime_dependency 'yaml'
38
+ spec.add_runtime_dependency 'gitlab'
39
+
38
40
  end
39
41
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: retag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samoilenko Yuri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-20 00:00:00.000000000 Z
11
+ date: 2022-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -212,6 +212,20 @@ dependencies:
212
212
  - - ">="
213
213
  - !ruby/object:Gem::Version
214
214
  version: '0'
215
+ - !ruby/object:Gem::Dependency
216
+ name: gitlab
217
+ requirement: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - ">="
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ type: :runtime
223
+ prerelease: false
224
+ version_requirements: !ruby/object:Gem::Requirement
225
+ requirements:
226
+ - - ">="
227
+ - !ruby/object:Gem::Version
228
+ version: '0'
215
229
  description: Retag is cli to manipulate docker images in handy way
216
230
  email:
217
231
  - kinnalru@gmail.com
@@ -230,6 +244,7 @@ files:
230
244
  - lib/retag.rb
231
245
  - lib/retag/image.rb
232
246
  - lib/retag/logger.rb
247
+ - lib/retag/repo.rb
233
248
  - lib/retag/service.rb
234
249
  - lib/retag/utils.rb
235
250
  - lib/retag/version.rb