miniexec 0.0.5 → 0.1.0

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 +11 -9
  3. data/lib/miniexec.rb +112 -103
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec080972e866dfa304be63c976aafbee1adf057d87e6b3db67d41f0da96fa8aa
4
- data.tar.gz: 7062906cc91e078022d69fd47f3306ae05164e237db624328dd240d098f32ed5
3
+ metadata.gz: 8678ef2aa21bd66fbe29563f4621c2a1656b2c6fbcf0a1a3e231b750d65f9707
4
+ data.tar.gz: a946564ca4922eb7b0d55432e83e5827d310753ca4ebae0d150852db8eb269ec
5
5
  SHA512:
6
- metadata.gz: d669bfb19e7e2773e03d68fc1a5323e98042a1da88b55cd08d169beb9c6e572ad9fd30c3f0b67e5f6a59ee5ce3217f835774b0c91cf7d0ff3fb5c2ee08f530b4
7
- data.tar.gz: 88556134ee2c8dd33e0bc9f754a92d7721867d425270cdc8981275e154fa56721f76f4a67f835f165839f235f141550854433b96c330bf5345de94d0a1709279
6
+ metadata.gz: 534199cfb3d7ff71b05d2a6bc55591e9a259090375bc0e120c55a93de6ffcaf14aed7f147672543442a250ba165abaa0a41925530008491fae4e831c524953df
7
+ data.tar.gz: 7ce0d9cb62a9c7f0241376c79cbaa393846afd7a8995f22790fe7bb1ed932170eaa555ac8547c8babf3d71306f1e07f9218e89e6e30466870dfc5022f389628f
data/bin/miniexec CHANGED
@@ -1,12 +1,16 @@
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'
6
8
 
7
9
  options = {
8
10
  binds: [],
9
- env: {}
11
+ env: {},
12
+ path: '.',
13
+ docker: ENV['DOCKER_HOST'] || '/run/docker.sock'
10
14
  }
11
15
 
12
16
  OptionParser.new do |opts|
@@ -34,17 +38,15 @@ OptionParser.new do |opts|
34
38
  end
35
39
  opts.on('-d', '--docker-url URL',
36
40
  'Location of the docker socket') do |sock|
37
- options[:docker] = sock || ENV['DOCKER_HOST'] || '/run/docker.sock'
41
+ options[:docker] = sock
38
42
  end
39
43
  end.parse!
40
44
 
41
45
  raise OptionParser::MissingArgument, 'Specify a job with -j' if options[:job].nil?
42
- raise OptionParser::MissingArgument, 'Specify a job with -p' if options[:path].nil?
43
-
44
- MiniExec.config(project_path: options[:path])
45
- exec = MiniExec.new options[:job],
46
- docker_url: options[:docker],
47
- binds: options[:binds],
48
- env: options[:env]
49
46
 
47
+ MiniExec::MiniExec.config(project_path: options[:path])
48
+ exec = MiniExec::MiniExec.new options[:job],
49
+ docker_url: options[:docker],
50
+ binds: options[:binds],
51
+ env: options[:env]
50
52
  exec.run_job
data/lib/miniexec.rb CHANGED
@@ -1,123 +1,132 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Main class
4
- class MiniExec
5
- require 'logger'
6
- require 'docker-api'
7
- require 'json'
8
- require 'tempfile'
9
- require 'yaml'
10
- require 'git'
11
- require 'pry'
12
- # Class instance variables
13
- @project_path = '.'
14
- @workflow_file = '.gitlab-ci.yml'
4
+ module MiniExec
5
+ class MiniExec
6
+ require 'logger'
7
+ require 'docker-api'
8
+ require 'json'
9
+ require 'tempfile'
10
+ require 'yaml'
11
+ require 'git'
12
+ require_relative './util'
13
+ # Class instance variables
14
+ @project_path = '.'
15
+ @workflow_file = '.gitlab-ci.yml'
15
16
 
16
- class << self
17
- attr_accessor :project_path, :workflow_file
18
- end
17
+ class << self
18
+ attr_accessor :project_path, :workflow_file
19
+ end
19
20
 
20
- def self.config(project_path: @project_path, workflow_file: @workflow_file)
21
- @project_path = project_path
22
- @workflow_file = workflow_file
23
- self
24
- end
21
+ def self.config(project_path: @project_path, workflow_file: @workflow_file)
22
+ @project_path = project_path
23
+ @workflow_file = workflow_file
24
+ self
25
+ end
25
26
 
26
- attr_accessor :script
27
+ attr_accessor :script
27
28
 
28
- def initialize(job,
29
- project_path: self.class.project_path,
30
- docker_url: nil,
31
- binds: [],
32
- env: {})
33
- @job_name = job
34
- @project_path = project_path
35
- @workflow = YAML.load(File.read("#{@project_path}/#{MiniExec.workflow_file}"))
36
- @job = @workflow[job]
37
- @job['name'] = job
38
- @default_image = @workflow['image'] || 'debian:buster-slim'
39
- @image = set_job_image
40
- @script = compile_script
41
- @binds = binds
42
- @env = env.merge gitlab_env, variables
43
- configure_logger
44
- Docker.options[:read_timeout] = 6000
45
- Docker.url = docker_url if docker_url
46
- binding.pry
47
- end
29
+ def initialize(job,
30
+ project_path: self.class.project_path,
31
+ docker_url: nil,
32
+ binds: [],
33
+ env: {})
34
+ @job_name = job
35
+ @project_path = project_path
36
+ @workflow = YAML.load(File.read("#{@project_path}/#{MiniExec.workflow_file}"))
37
+ @job = @workflow[job]
38
+ @job['name'] = job
39
+ @default_image = @workflow['image'] || 'debian:buster-slim'
40
+ @image = set_job_image
41
+ @binds = binds
42
+ @env = {}
43
+ [
44
+ env,
45
+ gitlab_env,
46
+ @workflow['variables'],
47
+ @job['variables']
48
+ ].each do |var_set|
49
+ @env.merge!(var_set.transform_values { |v| Util.expand_var(v.to_s, @env) }) if var_set
50
+ end
51
+ @script = compile_script
52
+ configure_logger
53
+ Docker.options[:read_timeout] = 6000
54
+ Docker.url = docker_url if docker_url
55
+ end
48
56
 
49
- def run_job
50
- script_path = "/tmp/#{@job['name']}.sh"
51
- @logger.debug "Fetching image #{@image}"
52
- Docker::Image.create(fromImage: @image)
53
- @logger.debug 'Image fetched'
54
- Dir.chdir(@project_path) do
55
- @logger.debug 'Creating container'
56
- container = Docker::Container.create(
57
- Cmd: ['/bin/bash', script_path],
58
- Image: @image,
59
- Volumes: @binds.map { |b| { b => { path_parent: 'rw' } } }.inject(:merge),
60
- Env: @env.map { |k, v| "#{k}=#{v}" }
61
- )
62
- container.store_file(script_path, @script)
63
- container.start({ Binds: [@binds] })
64
- container.tap(&:start).attach { |_, chunk| @logger.info chunk }
57
+ def run_job
58
+ script_path = "/tmp/#{@job['name']}.sh"
59
+ @logger.info "Fetching image #{@image}"
60
+ Docker::Image.create(fromImage: @image)
61
+ @logger.info 'Image fetched'
62
+ Dir.chdir(@project_path) do
63
+ @logger.info 'Creating container'
64
+ container = Docker::Container.create(
65
+ Cmd: ['/bin/bash', script_path],
66
+ Image: @image,
67
+ Volumes: @binds.map { |b| { b => { path_parent: 'rw' } } }.inject(:merge),
68
+ Env: @env.map { |k, v| "#{k}=#{v}" }
69
+ )
70
+ container.store_file(script_path, @script)
71
+ container.start({ Binds: [@binds] })
72
+ container.tap(&:start).attach { |_, chunk| puts chunk }
73
+ end
65
74
  end
66
- end
67
75
 
68
- private
76
+ private
69
77
 
70
- def set_job_image
71
- return @job['image'] if @job['image']
78
+ def set_job_image
79
+ return @job['image'] if @job['image']
72
80
 
73
- @default_image
74
- end
81
+ @default_image
82
+ end
75
83
 
76
- # Set gitlab's predefined env vars as per
77
- # https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
78
- def gitlab_env
79
- g = Git.open(@project_path)
80
- commit = g.gcommit 'HEAD'
81
- tag = g.tags.find { |t| t.objectish == commit.sha }
82
- commit_branch = g.branch.name
83
- if tag.nil?
84
- ref_name = g.branch.name
85
- commit_tag = nil
86
- else
87
- ref_name = tag.name
88
- commit_tag = ref_name
84
+ # Set gitlab's predefined env vars as per
85
+ # https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
86
+ def gitlab_env
87
+ g = Git.open(@project_path)
88
+ commit = g.gcommit 'HEAD'
89
+ tag = g.tags.find { |t| t.objectish == commit.sha }
90
+ commit_branch = g.branch.name
91
+ if tag.nil?
92
+ ref_name = g.branch.name
93
+ commit_tag = nil
94
+ else
95
+ ref_name = tag.name
96
+ commit_tag = ref_name
97
+ end
98
+ {
99
+ 'CI': true,
100
+ 'CI_COMMIT_REF_SHA': commit.sha,
101
+ 'CI_COMMIT_SHORT_SHA': commit.sha[0, 8],
102
+ 'CI_COMMIT_REF_NAME': ref_name,
103
+ 'CI_COMMIT_BRANCH': commit_branch,
104
+ 'CI_COMMIT_TAG': commit_tag,
105
+ 'CI_COMMIT_MESSAGE': commit.message,
106
+ 'CI_COMMIT_REF_PROTECTED': false,
107
+ 'CI_COMMIT_TIMESTAMP': commit.date.strftime('%FT%T')
108
+ }.transform_keys(&:to_s)
89
109
  end
90
- {
91
- 'CI': true,
92
- 'CI_COMMIT_REF_SHA': commit.sha,
93
- 'CI_COMMIT_SHORT_SHA': commit.sha[0, 8],
94
- 'CI_COMMIT_REF_NAME': ref_name,
95
- 'CI_COMMIT_BRANCH': commit_branch,
96
- 'CI_COMMIT_TAG': commit_tag,
97
- 'CI_COMMIT_MESSAGE': commit.message,
98
- 'CI_COMMIT_REF_PROTECTED': false,
99
- 'CI_COMMIT_TIMESTAMP': commit.date.strftime('%FT%T')
100
- }.transform_keys(&:to_s)
101
- end
102
110
 
103
- def variables
104
- globals = @workflow['variables']
105
- job_locals = @job['variables']
106
- globals.merge job_locals
107
- end
111
+ def variables
112
+ globals = @workflow['variables'] || {}
113
+ job_locals = @job['variables'] || {}
114
+ globals.merge job_locals
115
+ end
108
116
 
109
- def configure_logger
110
- @logger = Logger.new($stdout)
111
- @logger.formatter = proc do |severity, _, _, msg|
112
- "[#{severity}]: #{msg}\n"
117
+ def configure_logger
118
+ @logger = Logger.new($stdout)
119
+ @logger.formatter = proc do |severity, _, _, msg|
120
+ "[#{severity}]: #{msg}\n"
121
+ end
122
+ @logger.level = ENV['LOGLEVEL'] || Logger::INFO
113
123
  end
114
- @logger.level = ENV['LOGLEVEL'] || Logger::WARN
115
- end
116
124
 
117
- def compile_script
118
- before_script = @job['before_script'] || []
119
- script = @job['script'] || []
120
- after_script = @job['after_script'] || []
121
- (before_script + script + after_script).flatten.join("\n")
125
+ def compile_script
126
+ before_script = @job['before_script'] || ''
127
+ script = @job['script'] || ''
128
+ after_script = @job['after_script'] || ''
129
+ [before_script, script, after_script].flatten.join("\n").strip
130
+ end
122
131
  end
123
132
  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.5
4
+ version: 0.1.0
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-30 00:00:00.000000000 Z
11
+ date: 2021-05-04 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