build-labels 0.0.1 → 0.0.2

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: 0dcae3ead743d1387c98b31eb227be0c14ba162c092b417ed45815b129bc3b81
4
- data.tar.gz: f9ead067fd0089650a063737fc51b0d2686f40a0699576d40ae256ce70a579bb
3
+ metadata.gz: 66c6c1a4fa1d711c38dd4bf846d1445093dce81aee45621a10337e6d6b92f2ab
4
+ data.tar.gz: 72b0e9c44d03f2aa1f30947fa8603bbad1b73c5f5604bfe30bccff5bd9a579d8
5
5
  SHA512:
6
- metadata.gz: 75acdb27cb268daab9dafe3484de6b53abab2e6abcb8e7e2454cede885af4f9df038d8d3162f692eadfcfb2446a8093fadd2268b32af2d74a65d4da931bad0c8
7
- data.tar.gz: c6d62b2c80d37039ad07a15a37a9d29b2754851508ebce6d1fabb308375c167197f937ef80eb24ded6f2073f88143268541aeec09f137bc86b5a4955b41d98c2
6
+ metadata.gz: 961c117b55eae94369aff3dec1f3ecf73664d1953bb662f1b98a88ae05600a5f1d48e38d8b8aaf15e75cc70d0a1f618407242fb1aae10423697f309b9bcb4e71
7
+ data.tar.gz: 39ada551b9180fabba42b2375cb0e21824bad51a1fd7f633a952170e35db5251c373c1b044be411e134b9ec4913b669ffbf131da6aff120f0ee980f7a4a08bb9
data/bin/build-labels CHANGED
@@ -5,6 +5,8 @@ $LOAD_PATH << lib
5
5
 
6
6
  require 'build-labels'
7
7
  require 'build-labels/command_line'
8
+ require 'build-labels/command_gitlab'
9
+
8
10
  include BuildLabels
9
11
 
10
12
  BuildLabels::CommandLine.run ARGV
@@ -0,0 +1,9 @@
1
+ version: '3.8'
2
+
3
+ services:
4
+ service-a:
5
+ image: service-a
6
+ build:
7
+ context: .
8
+ dockerfile: ./Dockerfile
9
+
@@ -1,12 +1,62 @@
1
1
  # frozen_string_literal: true
2
2
  require 'yaml'
3
- require 'optparse'
4
-
3
+ require 'ostruct'
4
+ require 'pp'
5
5
  module BuildLabels
6
6
 
7
7
  class Builder
8
8
 
9
- def initialize()
9
+ def add_namespace(name, path, labels=[])
10
+ @namespaces ||= {}
11
+ @namespaces[path] = OpenStruct.new
12
+ self.class.send :define_method, name, -> { @namespaces[path] }
13
+ end
14
+
15
+ def initialize
16
+ # https://github.com/opencontainers/image-spec/blob/v1.0.1/annotations.md
17
+ add_namespace :oc, 'org.opencontainers.image', [
18
+ :vendor, :authors, :revision, :source, :documentation, :licenses, :url,
19
+ :version, :'ref.name', :title, :description, :created
20
+ ]
21
+ end
22
+
23
+ def generate_label_schema
24
+ # Label Schema compatibility
25
+ add_namespace :ls, 'org.label-schema'
26
+ self.ls['build-date'] = self.oc.created
27
+ self.ls.url = self.oc.url
28
+ self.ls['vcs-url'] = self.oc.source
29
+ self.ls.version = self.oc.version
30
+ self.ls['vcs-ref'] = self.oc.revision
31
+ self.ls.vendor = self.oc.vendor
32
+ self.ls.name = self.oc.title
33
+ self.ls.description = self.oc.description
34
+ self.ls.usage = self.oc.documentation
35
+ self.ls['schema-version'] = '1.0'
36
+ end
37
+
38
+ def apply_environment
39
+ @namespaces.each do |ns, struct|
40
+ struct.each_pair do |name, value|
41
+ value.sub! /^.*$/, `printf #{value}` if value.to_s =~ /\$/
42
+ end
43
+ end
44
+ end
45
+
46
+ def extend_compose(compose_text)
47
+ generate_label_schema
48
+ apply_environment
49
+ compose = YAML.load compose_text
50
+ compose['services'].each do |name, service|
51
+ next unless service['build']
52
+ service['build']['labels'] ||= []
53
+ @namespaces.each do |ns, values|
54
+ values.each_pair do |k,v|
55
+ service['build']['labels'] << "#{ns}.#{k}=#{v}" unless v.to_s.empty?
56
+ end
57
+ end
58
+ end
59
+ puts compose.to_yaml
10
60
  end
11
61
  end
12
62
 
@@ -0,0 +1,37 @@
1
+ require_relative 'command_line'
2
+
3
+ BuildLabels::CommandLine::COMMANDS[:gitlab] = Class.new do
4
+ def run(builder, params)
5
+ builder.oc.vendor = '$CI_SERVER_URL/$GITLAB_USER_LOGIN'
6
+ builder.oc.authors = '$CI_SERVER_URL/$GITLAB_USER_LOGIN'
7
+ builder.oc.revision = '$CI_COMMIT_SHA'
8
+ builder.oc['ref.name'] = '$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"'
9
+ builder.oc.source = '$CI_PROJECT_URL'
10
+ builder.oc.documentation = '$CI_PROJECT_URL'
11
+ builder.oc.licenses = '$CI_PROJECT_URL'
12
+ builder.oc.url = '$CI_PROJECT_URL'
13
+ builder.oc.title = '$CI_PROJECT_TITLE'
14
+ builder.oc.created = '$CI_JOB_STARTED_AT'
15
+ builder.oc.version = '$CI_COMMIT_REF_NAME' # $CI_COMMIT_TAG
16
+
17
+ builder.add_namespace :gitlab, 'com.gitlab.ci'
18
+ builder.gitlab.user = '$CI_SERVER_URL/$GITLAB_USER_LOGIN'
19
+ builder.gitlab.email = '$GITLAB_USER_EMAIL'
20
+ builder.gitlab.tagorbranch = '$CI_COMMIT_REF_NAME'
21
+ builder.gitlab.pipelineurl = '$CI_PIPELINE_URL'
22
+ builder.gitlab.commiturl = '$CI_PROJECT_URL/commit/$CI_COMMIT_SHA'
23
+ builder.gitlab.cijoburl = '$CI_JOB_URL'
24
+ builder.gitlab.mrurl = '$CI_PROJECT_URL/-/merge_requests/$CI_MERGE_REQUEST_ID'
25
+ builder.gitlab.tag = '$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA'
26
+
27
+ # org.opencontainers.image.title BUILDTITLE=$(echo $CI_PROJECT_TITLE | tr " " "_")
28
+ # org.opencontainers.image.description
29
+ # date '+%FT%T%z' | sed -E -n 's/(\+[0-9]{2})([0-9]{2})$/\1:\2/p' #rfc 3339 date
30
+ # org.opencontainers.image.created
31
+
32
+ end
33
+
34
+ def help = 'Use GitLab CI variables'
35
+ end.new
36
+
37
+
@@ -1,18 +1,25 @@
1
1
  require_relative '../version'
2
+ require 'optparse'
2
3
 
3
4
  module BuildLabels
4
5
  module CommandLine
5
6
  COMMANDS = {}
6
7
 
7
8
  class << self
8
- def load_env
9
- File.read('.env').lines.map(&:strip).grep_v(/^\s*#/).reject(&:empty?)
10
- .map {_1.split('=').map(&:strip).tap { |k,v|
11
- ENV[k] = v
12
- }}.to_h
9
+ def load_env(filename)
10
+ File.read(filename).lines.map(&:strip).grep_v(/^\s*#/).reject(&:empty?)
11
+ .map {
12
+ if _1 =~ /([^=]+)=(.*)/
13
+ a, k,v = Regexp.last_match.to_a
14
+ ENV[k] = v
15
+ [k,v]
16
+ else
17
+ nil
18
+ end
19
+ }.compact.to_h
13
20
  rescue =>e
14
21
  puts "Load env error: #{e.message}"
15
- raise 'Invalid .env file'
22
+ raise "Invalid #{filename} file"
16
23
  end
17
24
 
18
25
  def run(args)
@@ -23,9 +30,9 @@ module BuildLabels
23
30
  o.version = "#{BuildLabels::Builder::VERSION}"
24
31
 
25
32
  usage = [
26
- 'dry-stack -s stackfile [options] COMMAND',
27
- 'cat stackfile | dry-stack COMMAND',
28
- 'dry-stack COMMAND < stack.drs'
33
+ 'build-labels docker-compose.yml',
34
+ 'cat docker-compose.yml | dry-stack build-labels',
35
+ 'build-labels < docker-compose.yml'
29
36
  ]
30
37
  o.banner = "Version: #{o.version}\nUsage:\n\t#{usage.join "\n\t"}"
31
38
  o.separator ''
@@ -36,25 +43,25 @@ module BuildLabels
36
43
  o.separator 'Options:'
37
44
 
38
45
  # in all caps are required
39
- o.on('-s', '--stack STACK_NAME', 'Stack file')
40
- o.on('-e', '--env', 'Load .env file') { load_env }
41
- o.on('', '--name STACK_NAME', 'Define stack name')
42
- o.on('', '--ingress', 'Generate ingress labels') { true }
43
- o.on('', '--traefik', 'Generate traefik labels') { true }
44
- o.on('', '--traefik_tls', 'Generate traefik tls labels') { true }
45
- o.on('', '--host_sed /from/to/', 'Sed ingress host /\\*/dev.*/')
46
+ o.on('-c', '--compose COMPOSE_FILE', 'Compose file')
47
+ o.on('-e', '--env FILE', 'Load .build_info FILE') { load_env _1 }
46
48
  o.on('-n', '--no-env', 'Do not process env variables') { true }
47
49
  o.on('-h', '--help') { puts o; exit }
48
50
  o.parse! args, into: params
49
51
 
50
- raise 'Stack file not defined' if $stdin.tty? && !params[:stack]
52
+ raise 'Compose file not defined' if $stdin.tty? && !params[:compose]
51
53
 
52
54
  command = args.shift || ''
53
55
  raise "Unknown command: #{command}" unless COMMANDS.key?(command.to_sym)
54
56
 
55
- stack_text = File.read(params[:stack]) if params[:stack]
56
- stack_text ||= STDIN.read unless $stdin.tty?
57
+ compose_text = File.read(params[:compose]) if params[:compose]
58
+ compose_text ||= STDIN.read unless $stdin.tty?
57
59
 
60
+ builder = Builder.new
61
+ COMMANDS[command.to_sym].run builder, params
62
+ # eval $(grep -v -e '^#' .build_info | xargs -I {} echo export \'{}\') && echo $CI_COMMIT_AUTHOR
63
+
64
+ builder.extend_compose compose_text
58
65
  rescue => e
59
66
  puts e.message
60
67
  exit 1
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module BuildLabels
2
2
  class Builder
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: build-labels
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artyom B
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-21 00:00:00.000000000 Z
11
+ date: 2022-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -102,8 +102,10 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - bin/build-labels
105
+ - examples/simple-compose.yml
105
106
  - lib/build-labels.rb
106
107
  - lib/build-labels/builder.rb
108
+ - lib/build-labels/command_gitlab.rb
107
109
  - lib/build-labels/command_line.rb
108
110
  - lib/version.rb
109
111
  homepage: https://rubygems.org/gems/build-labels