dockdev 0.4.0 → 0.4.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 190fb93712b6c1bae63281b42de5937866791af1a1d00f1e42b7ebee453436cb
4
- data.tar.gz: 1b6bd909c27c7ab06f419c3f91846f036dd86548668ba31f0b201acda311fb3b
3
+ metadata.gz: 46c0990e754ea7e99a901a36fc68b2ecec7382563063546f31a65e6e0e0d3c98
4
+ data.tar.gz: 716529dec6307e70706aaa02ff11b2cdf1287fd766411695d6b7b3e07bab2a17
5
5
  SHA512:
6
- metadata.gz: 2d63a111dc55feab9e5b562a4e8fc75667b0fbc5bc8163a4fd67ef15ad991fa0dbb7919f8614e363a29b0f3f7b9e6a9afe4d6f14bd9567d2a2d829f63a9d4fc2
7
- data.tar.gz: 36de0b82ec04b79fbf94dff6b375f91eaaee5b676aa9e252a7064f77f96b2c5c81568825dd565936538e900c1d717e85bb4debccb360c2fdfc9eec999f1f4693
6
+ metadata.gz: 4d38a7214147deb4541da538d2b23564cb443ae3f1e4aa8e3721ea636e3712aec4072637fc59f046189619a3f1a814e3772e43195822b97d09b0dc25c653ab75
7
+ data.tar.gz: a73e6b416d9268ec0c38239fea64e3655c4d0a8a1eb1a6527e40c43f1329f38f11a252fba592467643041deb17b51433a47aa99854b264ac0174d6359e103246
data/exe/dockdev CHANGED
@@ -7,11 +7,16 @@
7
7
 
8
8
  require_relative '../lib/dockdev'
9
9
 
10
- contName = ARGV.first || File.basename(Dir.getwd)
11
- cmd = ARGV[1]
10
+ #contName = ARGV.first || File.basename(Dir.getwd)
11
+ #cmd = ARGV[1]
12
+ #input = ARGV[1]
13
+
14
+ uconf = Dockdev::UserConfig.new(ARGV.first)
15
+ contName = uconf.container_name || File.basename(Dir.getwd)
12
16
 
13
17
  begin
14
- Dockdev.with_running_container(contName, command: cmd, root: Dir.getwd)
18
+ #Dockdev.with_running_container(contName, command: cmd, root: Dir.getwd)
19
+ Dockdev.with_running_container(contName, user_config: uconf, root: Dir.getwd)
15
20
  rescue StandardError => ex
16
21
  STDERR.puts ex.message
17
22
  STDERR.puts ex.backtrace.join("\n")
@@ -69,5 +69,6 @@ module Dockdev
69
69
  end
70
70
  end
71
71
 
72
+
72
73
  end
73
74
  end
@@ -43,7 +43,7 @@ module Dockdev
43
43
  if not d.source.nil?
44
44
  src = d.source
45
45
  if src.path.to_s != "."
46
- pathInsideDocker = File.join(dir_inside_docker, d.name)
46
+ pathInsideDocker = File.join(ddConf.workdir, d.name)
47
47
  ddConf.add_mount(src.path.expand_path.to_s,pathInsideDocker)
48
48
  # following line assumed 'bundle' program already installed inside the image
49
49
  cmd << "bundle config --global local.#{d.name} #{pathInsideDocker}"
@@ -58,6 +58,7 @@ module Dockdev
58
58
 
59
59
  # for container
60
60
  attr_reader :mounts, :ports
61
+ attr_accessor :network
61
62
 
62
63
  # for image
63
64
  attr_reader :dockerfile_entries
@@ -69,6 +70,7 @@ module Dockdev
69
70
  def initialize(val = {})
70
71
  @mounts = val[:mounts] || {}
71
72
  @ports = val[:ports] || {}
73
+ @network = val[:network] || nil
72
74
  @dockerfile_entries = val[:dockerfile_entries] || []
73
75
  @workdir = val[:workdir] || "/opt"
74
76
  @skip_context = val[:skip_context] || []
data/lib/dockdev/image.rb CHANGED
@@ -1,6 +1,10 @@
1
1
 
2
2
  require 'docker/cli'
3
3
 
4
+ require 'securerandom'
5
+
6
+ require_relative 'user_info'
7
+
4
8
  module Dockdev
5
9
  class Image
6
10
  include TR::CondUtils
@@ -20,22 +24,41 @@ module Dockdev
20
24
  end
21
25
 
22
26
  def new_container(cont_name, opts = {})
27
+
23
28
  optss = {
24
29
  interactive: true,
25
30
  tty: true,
26
- container_name: cont_name
31
+ container_name: cont_name,
32
+ match_user: TR::RTUtils.on_linux?
27
33
  }
28
34
  optss.merge!(opts)
35
+
29
36
  @cmd_fact.create_container_from_image(@image_name, optss).run
37
+
30
38
  end
31
39
 
32
40
  def build(dockerfile, opts = {})
41
+
42
+ dockerfilePath = dockerfile
43
+ if is_transfer_user?(opts)
44
+ cont = append_transfer_user_in_dockerfile(dockerfile)
45
+ dockerfilePath = generated_dockerfile
46
+ File.open(dockerfilePath, "w") do |f|
47
+ f.write cont
48
+ end
49
+ end
50
+
33
51
  optss = {
34
52
  context_root: opts[:root],
35
- dockerfile: dockerfile
53
+ dockerfile: dockerfilePath
36
54
  }
37
55
  optss.merge!(opts)
38
- @cmd_fact.build_image(@image_name, optss).run
56
+ res = @cmd_fact.build_image(@image_name, optss).run
57
+
58
+ FileUtils.rm(generated_dockerfile) if File.exist?(generated_dockerfile) and not is_keep_generated_dockerfile?
59
+
60
+ res
61
+
39
62
  end
40
63
 
41
64
  def destroy
@@ -47,5 +70,67 @@ module Dockdev
47
70
  end
48
71
  end
49
72
 
73
+
74
+ private
75
+ def logger
76
+ Dockdev.logger(:dockdev_image)
77
+ end
78
+
79
+ def is_keep_generated_dockerfile?
80
+ v = ENV["DOCKDEV_KEEP_GENERATED_DOCKERFILE"]
81
+ is_empty?(v) ? false : (v.downcase == "true") ? true : false
82
+ end
83
+
84
+ def generated_dockerfile
85
+ "Dockerfile-dockdev"
86
+ end
87
+
88
+ def is_transfer_user?(opts = {})
89
+ if TR::RTUtils.on_linux?
90
+ true
91
+ else
92
+ (opts[:match_user].nil? || not_bool?(opts[:match_user])) ? false : opts[:match_user]
93
+ end
94
+ end
95
+
96
+ def append_transfer_user_in_dockerfile(file)
97
+ if File.exist?(file)
98
+ logger.debug "Append transfer user in dockerfile '#{file}'"
99
+ res = []
100
+ cont = File.read(file)
101
+ indx = cont =~ /CMD/
102
+ if indx != nil
103
+
104
+ res << cont[0...indx]
105
+ res << transfer_user_command
106
+ res << cont[indx..-1]
107
+
108
+ else
109
+
110
+ res << cont
111
+ res << transfer_user_command
112
+
113
+ end
114
+
115
+ res.join
116
+
117
+ else
118
+ ""
119
+ end
120
+ end
121
+
122
+ def transfer_user_command
123
+
124
+ ui = UserInfo.user_info
125
+ gi = UserInfo.group_info
126
+
127
+ res = []
128
+ res << "RUN apt-get update && apt-get install -y sudo"
129
+ res << "RUN groupadd -f -g #{gi[:gid]} #{gi[:group_name]} && useradd -u #{ui[:uid]} -g #{gi[:gid]} -m #{ui[:login]} && usermod -aG sudo #{ui[:login]} && echo '#{ui[:login]} ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers"
130
+ res << "USER #{ui[:login]}"
131
+ res.join("\n")
132
+
133
+ end
134
+
50
135
  end
51
136
  end
@@ -0,0 +1,44 @@
1
+
2
+
3
+ module Dockdev
4
+ class UserConfig
5
+ include TR::CondUtils
6
+
7
+ def initialize(conf)
8
+ @res = parse(conf)
9
+ end
10
+
11
+ def is_valid?
12
+ @valid
13
+ end
14
+
15
+ def method_missing(mtd, *args, &block)
16
+ if has_key?(mtd)
17
+ @res[mtd.to_sym]
18
+ else
19
+ nil
20
+ #super
21
+ end
22
+ end
23
+
24
+ def has_key?(key)
25
+ @res.keys.include?(key.to_sym)
26
+ end
27
+
28
+ private
29
+ def parse(conf)
30
+ res = {}
31
+ if conf.is_a?(String)
32
+ @valid = true
33
+ conf.split(";").each do |v|
34
+ vv = v.split("=")
35
+ res[vv[0].to_sym] = vv[1] if not_empty?(vv[0]) and not_empty?(vv[1])
36
+ end
37
+ else
38
+ @valid = false
39
+ end
40
+
41
+ res
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+
2
+ require 'etc'
3
+
4
+ module Dockdev
5
+ module UserInfo
6
+ include TR::CondUtils
7
+
8
+ def self.user_info(login = nil)
9
+ login = Etc.getlogin if is_empty?(login)
10
+ res = { login: login }
11
+ begin
12
+ res[:uid] = Etc.getpwnam(login).uid
13
+ rescue Exception => ex
14
+ res[:uid] = nil
15
+ end
16
+ res
17
+ end
18
+
19
+ def self.group_info(login = nil)
20
+ login = Etc.getlogin if is_empty?(login)
21
+ res = { }
22
+ begin
23
+ gnm = Etc.getgrnam(login)
24
+ res[:group_name] = gnm.name
25
+ res[:gid] = gnm.gid
26
+ rescue Exception => ex
27
+ p ex
28
+ res[:group_name] = ""
29
+ res[:gid] = nil
30
+ end
31
+ res
32
+ end
33
+
34
+ end
35
+ end
36
+
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dockdev
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.2"
5
5
  end
data/lib/dockdev.rb CHANGED
@@ -15,6 +15,8 @@ require_relative 'dockdev/container'
15
15
 
16
16
  require_relative 'dockdev/dockdev_config'
17
17
 
18
+ require_relative 'dockdev/user_config'
19
+
18
20
  module Dockdev
19
21
  include TR::CondUtils
20
22
 
@@ -26,10 +28,12 @@ module Dockdev
26
28
 
27
29
  pmt = TTY::Prompt.new
28
30
  root = opts[:root]
29
- cmd = opts[:command]
31
+ cmd = opts[:command] || ""
30
32
 
31
33
  ddConf = load_config(root)
32
34
 
35
+ user_config = opts[:user_config]
36
+
33
37
  cont = Container.new(contName)
34
38
  if cont.has_container?
35
39
 
@@ -51,6 +55,10 @@ module Dockdev
51
55
  # root directory is mounted by default
52
56
  ddConf.add_mount(root, File.join(ddConf.workdir,File.basename(root)))
53
57
 
58
+ if user_config.has_key?(:network)
59
+ ddConf.network = user_config.network
60
+ end
61
+
54
62
  ctx = Dockdev::Context::ContextManager.instance.get_context(root)
55
63
  logger.debug("Found context : #{ctx}")
56
64
 
@@ -109,6 +117,20 @@ module Dockdev
109
117
  # Proceed to create container
110
118
 
111
119
  param = { command: cmd, mounts: ddConf.mounts, ports: ddConf.ports }
120
+ if not_empty?(ddConf.network)
121
+ cmd_fact = Docker::Cli::CommandFactory.new
122
+ res = cmd_fact.create_network(ddConf.network).run
123
+ if res.success? and not res.is_out_stream_empty?
124
+ param[:network] = ddConf.network
125
+ else
126
+ err = res.err_stream
127
+ if err =~ /already exist/
128
+ param[:network] = ddConf.network
129
+ else
130
+ raise Error, "\n Failed to create network. Error was : #{res.err_stream}\n"
131
+ end
132
+ end
133
+ end
112
134
  img.new_container(cont.name, param)
113
135
 
114
136
  #if img.has_image?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dockdev
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-29 00:00:00.000000000 Z
11
+ date: 2024-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: teLogger
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.5.1
47
+ version: 0.5.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.5.1
54
+ version: 0.5.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: tty-prompt
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -130,6 +130,8 @@ files:
130
130
  - lib/dockdev/context/rubygems.rb
131
131
  - lib/dockdev/dockdev_config.rb
132
132
  - lib/dockdev/image.rb
133
+ - lib/dockdev/user_config.rb
134
+ - lib/dockdev/user_info.rb
133
135
  - lib/dockdev/version.rb
134
136
  - lib/dockdev/workspace.rb
135
137
  - sig/dockdev.rbs