Class: Dockerun::Dfile
- Inherits:
-
Object
- Object
- Dockerun::Dfile
- Includes:
- TR::CondUtils
- Defined in:
- lib/dockerun/dfile.rb
Overview
Generate Dockerfile
Instance Method Summary collapse
- #base_image ⇒ Object
- #base_image=(img) ⇒ Object
- #command ⇒ Object
- #default_command ⇒ Object
- #generate(opts = {}) ⇒ Object
- #generated_image_name ⇒ Object
- #image_name ⇒ Object
-
#initialize(config) ⇒ Dfile
constructor
A new instance of Dfile.
- #install(*app) ⇒ Object
- #is_command_given? ⇒ Boolean
- #is_image_name_given? ⇒ Boolean
- #is_set_workdir? ⇒ Boolean
- #keep_dockerfile(bool = true) ⇒ Object
- #keep_dockerfile? ⇒ Boolean
-
#listener(&block) ⇒ Object
for context to install listener.
-
#output=(val) ⇒ Object
DSL.
- #output_filename ⇒ Object
- #required_app ⇒ Object
- #set_command(*val) ⇒ Object
-
#set_image_name(val) ⇒ Object
DSL.
- #set_workdir(bool = true) ⇒ Object
- #skip_update(bool = false) ⇒ Object
- #skip_update? ⇒ Boolean
- #skip_upgrade(bool = false) ⇒ Object
- #skip_upgrade? ⇒ Boolean
Constructor Details
#initialize(config) ⇒ Dfile
Returns a new instance of Dfile.
10 11 12 |
# File 'lib/dockerun/dfile.rb', line 10 def initialize(config) @_config = config end |
Instance Method Details
#base_image ⇒ Object
62 63 64 |
# File 'lib/dockerun/dfile.rb', line 62 def base_image @base_image end |
#base_image=(img) ⇒ Object
59 60 61 |
# File 'lib/dockerun/dfile.rb', line 59 def base_image=(img) @base_image = img end |
#command ⇒ Object
49 50 51 |
# File 'lib/dockerun/dfile.rb', line 49 def command @cmd end |
#default_command ⇒ Object
52 53 54 |
# File 'lib/dockerun/dfile.rb', line 52 def default_command ["/bin/bash"] end |
#generate(opts = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/dockerun/dfile.rb', line 100 def generate(opts = {}) opts = {} if opts.nil? config = @_config raise Error, "Config is required" if config.nil? cont = [] cont << "FROM #{base_image}" cont << "" pkgMgr = pkg_mgr(config) if not skip_update? or not skip_upgrade? st = [] st << "#{pkgMgr} update" if not skip_update? st << "#{pkgMgr} install -y apt-transport-https" st << "#{pkgMgr} -y upgrade" if not skip_upgrade? cont << "RUN #{st.join(" && ")}" if not_empty?(st) cont << "" end cb = trigger_listener(:dockerfile_before_install_app) cont << cb if not_empty?(cb) cont << "RUN #{pkgMgr} -y install #{required_app.join(" ")}" cont << "" cb = trigger_listener(:dockerfile_after_install_app) cont << cb if not_empty?(cb) if config.set_current_user? #RUN groupadd -f -g <%= @user_group_id %> <%= @user_group_name %> && useradd -u <%= @user_id %> -g <%= @user_group_id %> -m <%= @user_login %> && usermod -aG sudo <%= @user_login %> && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers #USER <%= @user_login %> uInfo = Dockerun::Cli::UserInfo.user_info gInfo = Dockerun::Cli::UserInfo.group_info uCmd = [] uCmd << "groupadd -f -g" uCmd << gInfo[:gid] uCmd << gInfo[:group_name] uCmd << "&& useradd" uCmd << "-u" uCmd << uInfo[:uid] uCmd << "-g" uCmd << gInfo[:gid] uCmd << "-m" uCmd << uInfo[:login] uCmd << "&& usermod -aG sudo" uCmd << uInfo[:login] uCmd << "&& echo '#{uInfo[:login]} ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers" cb = trigger_listener(:dockerfile_before_add_user) cont << cb if not_empty?(cb) cont << "RUN #{pkgMgr} -y install sudo && #{uCmd.join(" ")}" cont << "USER #{uInfo[:login]}" cont << "" cb = trigger_listener(:dockerfile_after_add_user) cont << cb if not_empty?(cb) end cb = trigger_listener(:dockerfile_before_workdir) cont << cb if not_empty?(cb) if config.is_workdir_given? and is_set_workdir? cont << "WORKDIR #{config.workdir}" cont << "" end cb = trigger_listener(:dockerfile_after_workdir) cont << cb if not_empty?(cb) cb = trigger_listener(:dockerfile_before_command) cont << cb if not_empty?(cb) cont << "CMD [\"#{command.join(",")}\"]" if is_command_given? cont.join("\n") end |
#generated_image_name ⇒ Object
24 25 26 27 28 29 |
# File 'lib/dockerun/dfile.rb', line 24 def generated_image_name if @_genImgName.nil? @_genImgName = "#{File.basename(Dir.getwd.gsub(" ","-"))}_docker_image" end @_genImgName end |
#image_name ⇒ Object
18 19 20 |
# File 'lib/dockerun/dfile.rb', line 18 def image_name @image_name end |
#install(*app) ⇒ Object
39 40 41 |
# File 'lib/dockerun/dfile.rb', line 39 def install(*app) @installApp = app end |
#is_command_given? ⇒ Boolean
55 56 57 |
# File 'lib/dockerun/dfile.rb', line 55 def is_command_given? not_empty?(command) end |
#is_image_name_given? ⇒ Boolean
21 22 23 |
# File 'lib/dockerun/dfile.rb', line 21 def is_image_name_given? not_empty?(@image_name) end |
#is_set_workdir? ⇒ Boolean
69 70 71 |
# File 'lib/dockerun/dfile.rb', line 69 def is_set_workdir? @set_workdir.nil? ? true : @set_workdir end |
#keep_dockerfile(bool = true) ⇒ Object
87 88 89 |
# File 'lib/dockerun/dfile.rb', line 87 def keep_dockerfile(bool = true) @keep_dockerfile = bool end |
#keep_dockerfile? ⇒ Boolean
90 91 92 |
# File 'lib/dockerun/dfile.rb', line 90 def keep_dockerfile? @keep_dockerfile.nil? ? true : @keep_dockerfile end |
#listener(&block) ⇒ Object
for context to install listener
96 97 98 |
# File 'lib/dockerun/dfile.rb', line 96 def listener(&block) @listener = block end |
#output=(val) ⇒ Object
DSL
32 33 34 |
# File 'lib/dockerun/dfile.rb', line 32 def output=(val) @output = val end |
#output_filename ⇒ Object
35 36 37 |
# File 'lib/dockerun/dfile.rb', line 35 def output_filename @output end |
#required_app ⇒ Object
42 43 44 |
# File 'lib/dockerun/dfile.rb', line 42 def required_app @installApp.nil? ? [] : @installApp end |
#set_command(*val) ⇒ Object
46 47 48 |
# File 'lib/dockerun/dfile.rb', line 46 def set_command(*val) @cmd = val end |
#set_image_name(val) ⇒ Object
DSL
15 16 17 |
# File 'lib/dockerun/dfile.rb', line 15 def set_image_name(val) @image_name = val end |
#set_workdir(bool = true) ⇒ Object
66 67 68 |
# File 'lib/dockerun/dfile.rb', line 66 def set_workdir(bool = true) @set_workdir = bool end |
#skip_update(bool = false) ⇒ Object
73 74 75 |
# File 'lib/dockerun/dfile.rb', line 73 def skip_update(bool = false) @skip_update = bool end |
#skip_update? ⇒ Boolean
76 77 78 |
# File 'lib/dockerun/dfile.rb', line 76 def skip_update? @skip_update.nil? ? false : @skip_update end |
#skip_upgrade(bool = false) ⇒ Object
80 81 82 |
# File 'lib/dockerun/dfile.rb', line 80 def skip_upgrade(bool = false) @skip_upgrade = bool end |
#skip_upgrade? ⇒ Boolean
83 84 85 |
# File 'lib/dockerun/dfile.rb', line 83 def skip_upgrade? @skip_upgrade.nil? ? false : @skip_upgrade end |