miniexec 0.0.2 → 0.0.7

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 +4 -4
  2. data/bin/miniexec +9 -5
  3. data/lib/miniexec.rb +50 -19
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e03f7a3801afeabed8952f4813e0dc107053f6697052e0613be969a4853f0c0
4
- data.tar.gz: 2d82ddc61ea8be64219e733d3704e56e9c30f703fe74b121ef24558a3f0f324a
3
+ metadata.gz: e698920b225de9f23f4eb3822d1eec118aaed8a64c7a4b3b517d90866af47f46
4
+ data.tar.gz: 7b4b0b8a671528ac4872286aff9451988ab820fd11179a7750dde0f1648755af
5
5
  SHA512:
6
- metadata.gz: 3b0b78c30b6cf9cb186f2a9531fd86ff8229d2a28793ded8149753c8bc64162fee95e28a9305bb1a8995a7d60e27dc06faeb002f4ee0f65213765c52b2b805b5
7
- data.tar.gz: a0657ec2d0cbada87e8fa9d6bedc8c580b529c362ed1fd84a1fcec04e7d5248f72d615bf5523649f313bc304a1e6c34272754348900fb395a9a14391ea01773a
6
+ metadata.gz: f261c258a1c28e15ef19bae2a620e7301274c58c1f0672e3f45623e43a7f506572502ef6a4f4582aebe6047818e01220465710372d0e1d8bf51f94ba00dd90dd
7
+ data.tar.gz: ef7e02e019e1405be2baed577db4812e6ae828d20f2685f3903fdf8d6355e06722e8875ee92539bd664ea0763dcb94241fd35c5027af7e4b2a59316924707349
data/bin/miniexec CHANGED
@@ -1,12 +1,17 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ lib = File.expand_path('../lib', __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require 'miniexec'
5
7
  require 'optparse'
8
+ require 'pry'
6
9
 
7
10
  options = {
8
11
  binds: [],
9
- env: []
12
+ env: {},
13
+ path: '.',
14
+ docker: ENV['DOCKER_HOST'] || '/run/docker.sock'
10
15
  }
11
16
 
12
17
  OptionParser.new do |opts|
@@ -29,21 +34,20 @@ OptionParser.new do |opts|
29
34
  opts.on('-e', '--environment VARIABLE',
30
35
  'Specify an environment variable to be passed to the container',
31
36
  'Example: SOMEVAR=thing') do |env|
32
- options[:env].push env
37
+ k, v = env.split('=')
38
+ options[:env][k] = v
33
39
  end
34
40
  opts.on('-d', '--docker-url URL',
35
41
  'Location of the docker socket') do |sock|
36
- options[:docker] = sock || ENV['DOCKER_HOST'] || '/run/docker.sock'
42
+ options[:docker] = sock
37
43
  end
38
44
  end.parse!
39
45
 
40
46
  raise OptionParser::MissingArgument, 'Specify a job with -j' if options[:job].nil?
41
- raise OptionParser::MissingArgument, 'Specify a job with -p' if options[:path].nil?
42
47
 
43
48
  MiniExec.config(project_path: options[:path])
44
49
  exec = MiniExec.new options[:job],
45
50
  docker_url: options[:docker],
46
51
  binds: options[:binds],
47
52
  env: options[:env]
48
-
49
53
  exec.run_job
data/lib/miniexec.rb CHANGED
@@ -7,6 +7,7 @@ class MiniExec
7
7
  require 'json'
8
8
  require 'tempfile'
9
9
  require 'yaml'
10
+ require 'git'
10
11
  # Class instance variables
11
12
  @project_path = '.'
12
13
  @workflow_file = '.gitlab-ci.yml'
@@ -17,7 +18,7 @@ class MiniExec
17
18
 
18
19
  def self.config(project_path: @project_path, workflow_file: @workflow_file)
19
20
  @project_path = project_path
20
- @class = workflow_file
21
+ @workflow_file = workflow_file
21
22
  self
22
23
  end
23
24
 
@@ -25,43 +26,40 @@ class MiniExec
25
26
 
26
27
  def initialize(job,
27
28
  project_path: self.class.project_path,
28
- workflow_file: self.class.workflow_file,
29
29
  docker_url: nil,
30
30
  binds: [],
31
- env: [])
31
+ env: {})
32
32
  @job_name = job
33
33
  @project_path = project_path
34
- workflow = YAML.load(File.read("#{@project_path}/#{workflow_file}"))
35
- @job = workflow[job]
34
+ @workflow = YAML.load(File.read("#{@project_path}/#{MiniExec.workflow_file}"))
35
+ @job = @workflow[job]
36
36
  @job['name'] = job
37
- @default_image = workflow['image'] || 'debian:buster-slim'
37
+ @default_image = @workflow['image'] || 'debian:buster-slim'
38
38
  @image = set_job_image
39
39
  @script = compile_script
40
40
  @binds = binds
41
- @env = env
42
-
41
+ @env = env.merge gitlab_env, variables
43
42
  configure_logger
44
-
45
43
  Docker.options[:read_timeout] = 6000
46
44
  Docker.url = docker_url if docker_url
47
45
  end
48
46
 
49
47
  def run_job
50
48
  script_path = "/tmp/#{@job['name']}.sh"
51
- @logger.debug "Fetching image #{@image}"
49
+ @logger.info "Fetching image #{@image}"
52
50
  Docker::Image.create(fromImage: @image)
53
- @logger.debug 'Image fetched'
51
+ @logger.info 'Image fetched'
54
52
  Dir.chdir(@project_path) do
55
- @logger.debug 'Creating container'
53
+ @logger.info 'Creating container'
56
54
  container = Docker::Container.create(
57
55
  Cmd: ['/bin/bash', script_path],
58
56
  Image: @image,
59
57
  Volumes: @binds.map { |b| { b => { path_parent: 'rw' } } }.inject(:merge),
60
- Env: @env
58
+ Env: @env.map { |k, v| "#{k}=#{v}" }
61
59
  )
62
60
  container.store_file(script_path, @script)
63
61
  container.start({ Binds: [@binds] })
64
- container.tap(&:start).attach { |_, chunk| @logger.info chunk }
62
+ container.tap(&:start).attach { |_, chunk| puts chunk }
65
63
  end
66
64
  end
67
65
 
@@ -73,18 +71,51 @@ class MiniExec
73
71
  @default_image
74
72
  end
75
73
 
74
+ # Set gitlab's predefined env vars as per
75
+ # https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
76
+ def gitlab_env
77
+ g = Git.open(@project_path)
78
+ commit = g.gcommit 'HEAD'
79
+ tag = g.tags.find { |t| t.objectish == commit.sha }
80
+ commit_branch = g.branch.name
81
+ if tag.nil?
82
+ ref_name = g.branch.name
83
+ commit_tag = nil
84
+ else
85
+ ref_name = tag.name
86
+ commit_tag = ref_name
87
+ end
88
+ {
89
+ 'CI': true,
90
+ 'CI_COMMIT_REF_SHA': commit.sha,
91
+ 'CI_COMMIT_SHORT_SHA': commit.sha[0, 8],
92
+ 'CI_COMMIT_REF_NAME': ref_name,
93
+ 'CI_COMMIT_BRANCH': commit_branch,
94
+ 'CI_COMMIT_TAG': commit_tag,
95
+ 'CI_COMMIT_MESSAGE': commit.message,
96
+ 'CI_COMMIT_REF_PROTECTED': false,
97
+ 'CI_COMMIT_TIMESTAMP': commit.date.strftime('%FT%T')
98
+ }.transform_keys(&:to_s)
99
+ end
100
+
101
+ def variables
102
+ globals = @workflow['variables'] || {}
103
+ job_locals = @job['variables'] || {}
104
+ globals.merge job_locals
105
+ end
106
+
76
107
  def configure_logger
77
108
  @logger = Logger.new($stdout)
78
109
  @logger.formatter = proc do |severity, _, _, msg|
79
110
  "[#{severity}]: #{msg}\n"
80
111
  end
81
- @logger.level = ENV['LOGLEVEL'] || Logger::WARN
112
+ @logger.level = ENV['LOGLEVEL'] || Logger::INFO
82
113
  end
83
114
 
84
115
  def compile_script
85
- before_script = @job['before_script'] || []
86
- script = @job['script'] || []
87
- after_script = @job['after_script'] || []
88
- (before_script + script + after_script).flatten.join("\n")
116
+ before_script = @job['before_script'] || ''
117
+ script = @job['script'] || ''
118
+ after_script = @job['after_script'] || ''
119
+ [before_script, script, after_script].flatten.join("\n").strip
89
120
  end
90
121
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miniexec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Pugh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-03 00:00:00.000000000 Z
11
+ date: 2021-04-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A minimal interpretor/executor for .gitlab-ci.yml
14
14
  email: pugh@s3kr.it