miniexec 0.0.1 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/miniexec +9 -5
- data/lib/miniexec.rb +50 -59
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e5abfbd093e75a0bb48a481b1ce13cddaf849680faf63f02839d151847daf25
|
4
|
+
data.tar.gz: c759f6ec46d9bbe5812988cdd76706afb2aca6d9d7d7064294f848814ae26bb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8d3604262b29677b356b0ddaffb581b3527183ce084bed754f286bff5c4cf3e9b980af47048796975cb040b974adb77c58f21ffa1fb54b9cf13bedde15f50f7
|
7
|
+
data.tar.gz: cd413fee962e6795592637a8c2278c6e9d24b9066a448c2b9c20a6b5b1542dce2007fd9960ad85b038edbb7ddb5d472ab4654104d3314fed66764d9dcaa15740
|
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
|
-
|
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
|
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
|
-
@
|
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.
|
49
|
+
@logger.info "Fetching image #{@image}"
|
52
50
|
Docker::Image.create(fromImage: @image)
|
53
|
-
@logger.
|
51
|
+
@logger.info 'Image fetched'
|
54
52
|
Dir.chdir(@project_path) do
|
55
|
-
@logger.
|
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|
|
62
|
+
container.tap(&:start).attach { |_, chunk| puts chunk }
|
65
63
|
end
|
66
64
|
end
|
67
65
|
|
@@ -73,58 +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::
|
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
|
-
|
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
|
91
|
-
|
92
|
-
options = {
|
93
|
-
binds: [],
|
94
|
-
env: []
|
95
|
-
}
|
96
|
-
|
97
|
-
OptionParser.new do |opts|
|
98
|
-
opts.banner = 'Usage: miniexec.rb [options]'
|
99
|
-
opts.separator ''
|
100
|
-
opts.separator 'specific options:'
|
101
|
-
|
102
|
-
opts.on('-p', '--path PATH', 'Path to the repository containing a valid .gitlab-ci.yml') do |path|
|
103
|
-
options[:path] = path
|
104
|
-
end
|
105
|
-
|
106
|
-
opts.on('-j', '--job JOBNAME', 'Specify the gitlab job to run') do |job|
|
107
|
-
options[:job] = job
|
108
|
-
end
|
109
|
-
|
110
|
-
opts.on('-b', '--bind BIND', 'Specify a bind mapping',
|
111
|
-
'Example: /some/local/dir:/mapping/in/container') do |bind|
|
112
|
-
options[:binds].push bind
|
113
|
-
end
|
114
|
-
opts.on('-e', '--environment VARIABLE',
|
115
|
-
'Specify an environment variable to be passed to the container',
|
116
|
-
'Example: SOMEVAR=thing') do |env|
|
117
|
-
options[:env].push env
|
118
|
-
end
|
119
|
-
end.parse!
|
120
|
-
|
121
|
-
raise OptionParser::MissingArgument, 'Specify a job with -j' if options[:job].nil?
|
122
|
-
raise OptionParser::MissingArgument, 'Specify a job with -p' if options[:path].nil?
|
123
|
-
|
124
|
-
MiniExec.config(project_path: options[:path])
|
125
|
-
exec = MiniExec.new options[:job],
|
126
|
-
docker_url: 'unix:///var/run/user/1000/podman/podman.sock',
|
127
|
-
binds: options[:binds],
|
128
|
-
env: options[:env]
|
129
|
-
|
130
|
-
exec.run_job
|
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.
|
4
|
+
version: 0.0.6
|
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-
|
11
|
+
date: 2021-04-07 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
|
@@ -31,7 +31,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
31
|
requirements:
|
32
32
|
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '0'
|
34
|
+
version: '2.0'
|
35
35
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ">="
|