miniexec 0.1.0 → 0.2.3

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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/miniexec +14 -2
  3. data/lib/miniexec.rb +39 -10
  4. data/lib/util.rb +19 -0
  5. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8678ef2aa21bd66fbe29563f4621c2a1656b2c6fbcf0a1a3e231b750d65f9707
4
- data.tar.gz: a946564ca4922eb7b0d55432e83e5827d310753ca4ebae0d150852db8eb269ec
3
+ metadata.gz: d1384a25b8dc2ad08cbc25bb427b2e6011a729a725bcbffd475491c06be91bcc
4
+ data.tar.gz: d34da5dcfdc51327a4462d0e3db39e2c001ea50570570ee1c125d19462face63
5
5
  SHA512:
6
- metadata.gz: 534199cfb3d7ff71b05d2a6bc55591e9a259090375bc0e120c55a93de6ffcaf14aed7f147672543442a250ba165abaa0a41925530008491fae4e831c524953df
7
- data.tar.gz: 7ce0d9cb62a9c7f0241376c79cbaa393846afd7a8995f22790fe7bb1ed932170eaa555ac8547c8babf3d71306f1e07f9218e89e6e30466870dfc5022f389628f
6
+ metadata.gz: 9066cbd4927f2e27e389eac2746398b0c280d6257c80915918c8ec90b596fa40cb477dc33dc94671deae3e03aebd6b3459327705ee59bd6377cd0ce6fdf535d7
7
+ data.tar.gz: 6c999684401609efd4bac19c66072e5859e31765df630c23b6adb5c950cd567f53a3f50315e65a17d5bf123509b2b49f330b4982de2e14bf22c35078a4d0ff34
data/bin/miniexec CHANGED
@@ -10,7 +10,9 @@ options = {
10
10
  binds: [],
11
11
  env: {},
12
12
  path: '.',
13
- docker: ENV['DOCKER_HOST'] || '/run/docker.sock'
13
+ docker: ENV['DOCKER_HOST'] || '/run/docker.sock',
14
+ cwd: true,
15
+ file: nil
14
16
  }
15
17
 
16
18
  OptionParser.new do |opts|
@@ -40,6 +42,14 @@ OptionParser.new do |opts|
40
42
  'Location of the docker socket') do |sock|
41
43
  options[:docker] = sock
42
44
  end
45
+ opts.on('-f', '--file FILE',
46
+ 'Manually specify a custom .gitlab-ci.yml file.') do |file|
47
+ options[:file] = file
48
+ end
49
+ opts.on('-n', '--no-mount-cwd',
50
+ 'Don\'t mount the CWD in the container\'s WORKDIR by default.') do
51
+ options[:cwd] = false
52
+ end
43
53
  end.parse!
44
54
 
45
55
  raise OptionParser::MissingArgument, 'Specify a job with -j' if options[:job].nil?
@@ -48,5 +58,7 @@ MiniExec::MiniExec.config(project_path: options[:path])
48
58
  exec = MiniExec::MiniExec.new options[:job],
49
59
  docker_url: options[:docker],
50
60
  binds: options[:binds],
51
- env: options[:env]
61
+ env: options[:env],
62
+ mount_cwd: options[:cwd] || false,
63
+ file: options[:file]
52
64
  exec.run_job
data/lib/miniexec.rb CHANGED
@@ -25,30 +25,37 @@ module MiniExec
25
25
  end
26
26
 
27
27
  attr_accessor :script
28
+ attr_reader :runlog
28
29
 
29
30
  def initialize(job,
30
31
  project_path: self.class.project_path,
31
32
  docker_url: nil,
32
33
  binds: [],
33
- env: {})
34
+ env: {},
35
+ mount_cwd: true,
36
+ file: nil)
34
37
  @job_name = job
35
38
  @project_path = project_path
36
- @workflow = YAML.load(File.read("#{@project_path}/#{MiniExec.workflow_file}"))
39
+ file ||= "#{@project_path}/#{MiniExec.workflow_file}"
40
+ @workflow = YAML.load(File.read(file))
37
41
  @job = @workflow[job]
38
42
  @job['name'] = job
39
43
  @default_image = @workflow['image'] || 'debian:buster-slim'
40
44
  @image = set_job_image
45
+ @entrypoint = set_job_entrypoint
41
46
  @binds = binds
47
+ @mount_cwd = mount_cwd
42
48
  @env = {}
43
49
  [
44
- env,
45
- gitlab_env,
46
50
  @workflow['variables'],
47
- @job['variables']
51
+ @job['variables'],
52
+ gitlab_env,
53
+ env
48
54
  ].each do |var_set|
49
55
  @env.merge!(var_set.transform_values { |v| Util.expand_var(v.to_s, @env) }) if var_set
50
56
  end
51
57
  @script = compile_script
58
+ @runlog = []
52
59
  configure_logger
53
60
  Docker.options[:read_timeout] = 6000
54
61
  Docker.url = docker_url if docker_url
@@ -59,26 +66,48 @@ module MiniExec
59
66
  @logger.info "Fetching image #{@image}"
60
67
  Docker::Image.create(fromImage: @image)
61
68
  @logger.info 'Image fetched'
69
+
70
+ config = Docker::Image.get(@image).info['Config']
71
+ working_dir = if config['WorkingDir'].empty?
72
+ '/gitlab'
73
+ else
74
+ config['WorkingDir']
75
+ end
76
+ binds = @binds
77
+ binds.push "#{ENV['PWD']}:#{working_dir}" if @mount_cwd
62
78
  Dir.chdir(@project_path) do
63
79
  @logger.info 'Creating container'
64
80
  container = Docker::Container.create(
65
- Cmd: ['/bin/bash', script_path],
66
81
  Image: @image,
67
- Volumes: @binds.map { |b| { b => { path_parent: 'rw' } } }.inject(:merge),
82
+ Cmd: ['/usr/bin/env', 'bash', script_path],
83
+ WorkingDir: working_dir,
84
+ Entrypoint: @entrypoint,
85
+ Volumes: binds.map { |b| { b => { path_parent: 'rw' } } }.inject(:merge),
68
86
  Env: @env.map { |k, v| "#{k}=#{v}" }
69
87
  )
70
88
  container.store_file(script_path, @script)
71
89
  container.start({ Binds: [@binds] })
72
- container.tap(&:start).attach { |_, chunk| puts chunk }
90
+ container.tap(&:start).attach { |_, chunk| puts chunk; @runlog.push chunk}
91
+ @logger.info 'Job finished. Removing container.'
92
+ # After running, we want to remove the container.
93
+ container.remove
94
+ @logger.info 'Container removed.'
73
95
  end
74
96
  end
75
97
 
76
98
  private
77
99
 
78
100
  def set_job_image
79
- return @job['image'] if @job['image']
101
+ if @job['image']
102
+ image = @job['image'] if @job['image'].instance_of?(String)
103
+ image = @job['image']['name'] if @job['image'].instance_of?(Hash)
104
+ end
105
+
106
+ image || @default_image
107
+ end
80
108
 
81
- @default_image
109
+ def set_job_entrypoint
110
+ @job['image']['entrypoint'] if @job['image'].instance_of?(Hash)
82
111
  end
83
112
 
84
113
  # Set gitlab's predefined env vars as per
data/lib/util.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniExec
4
+ module Util
5
+ # Given a string and an env, replace any instance of shell-style variables
6
+ # with their value in the env. NOT POSIX COMPLIANT, just mostly hacky so
7
+ # I can get gitlab-ci.yml parsing to work properly. Required in MiniExec
8
+ # because of https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html#gitlab-internal-variable-expansion-mechanism
9
+ def self.expand_var(string, env)
10
+ # Match group 1 = the text to replace
11
+ # Match group 2 = the key from env we want to replace it with
12
+ regex = /(\${?(\w+)}?)/
13
+ string.scan(regex).uniq.each do |match|
14
+ string.gsub! match[0], env[match[1]].to_s
15
+ end
16
+ string
17
+ end
18
+ end
19
+ 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.1.0
4
+ version: 0.2.3
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-05-04 00:00:00.000000000 Z
11
+ date: 2021-06-01 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
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - bin/miniexec
21
21
  - lib/miniexec.rb
22
+ - lib/util.rb
22
23
  homepage: https://github.com/s3krit/miniexec
23
24
  licenses:
24
25
  - AGPL-3.0-or-later