Class: Puppetfactory::Plugins::Docker
- Inherits:
-
Puppetfactory::Plugins
- Object
- Puppetfactory::Plugins
- Puppetfactory::Plugins::Docker
- Defined in:
- lib/puppetfactory/plugins/docker.rb
Instance Attribute Summary
Attributes inherited from Puppetfactory::Plugins
Instance Method Summary collapse
- #create(username, password) ⇒ Object
- #delete(username) ⇒ Object
-
#initialize(options) ⇒ Docker
constructor
A new instance of Docker.
- #login ⇒ Object
- #repair(username) ⇒ Object
- #userinfo(username, extended = false) ⇒ Object
Constructor Details
#initialize(options) ⇒ Docker
Returns a new instance of Docker.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/puppetfactory/plugins/docker.rb', line 10 def initialize() super() @weight = 5 @confdir = [:confdir] @stagedir = [:stagedir] @environments = "#{@stagedir}/environments" @puppetcode = [:puppetcode] @master = [:master] @usersuffix = [:usersuffix] @modulepath = [:modulepath] @templatedir = [:templatedir] @container = [:container_name] || 'centosagent' @group = [:docker_group] || 'docker' @docker_ip = [:docker_ip] || `facter ipaddress_docker0`.strip @privileged = [:privileged] || false end |
Instance Method Details
#create(username, password) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/puppetfactory/plugins/docker.rb', line 29 def create(username, password) begin environment = "#{@environments}/#{Puppetfactory::Helpers.environment_name(username)}" binds = [ "/var/yum:/var/yum", "/var/cache:/var/cache", "/etc/pki/rpm-gpg:/etc/pki/rpm-gpg", # "/etc/yum.repos.d:/etc/yum.repos.d", # we can't share this because of pe_repo.repo "/opt/puppetlabs/server:/opt/puppetlabs/server", "/home/#{username}/puppet:#{@confdir}", "/sys/fs/cgroup:/sys/fs/cgroup:ro" ] case @modulepath when :readonly binds.push("#{environment}:#{@puppetcode}:ro") when :readwrite binds.push("#{environment}:#{@puppetcode}") when :none #pass else raise "Uknown modulepath setting (#{@modulepath})" end container = ::Docker::Container.create( "Cmd" => [ "/usr/lib/systemd/systemd" ], "Tty" => true, "Domainname" => @usersuffix, "Env" => [ "RUNLEVEL=3", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "HOME=/root/", "TERM=xterm" ], "ExposedPorts" => { "80/tcp" => { } }, "Hostname" => "#{username}", "Image" => "#{@container}", "HostConfig" => { "Privileged" => @privileged, "SecurityOpt" => [ "seccomp=unconfined" ], "Tmpfs" => { "/run" => "", "/tmp" => "" }, "Binds" => binds, "ExtraHosts" => [ "#{@master} puppet:#{@docker_ip}" ], "PortBindings" => { "80/tcp" => [ { "HostPort" => "#{user_port(username)}" } ] }, }, "Name" => "#{username}" ) container.rename(username) # Set container name so we can identify it init_scripts(username) # Create init scripts so container restarts on boot container.start rescue => e # fatal error, so we stop execution here raise "Error creating container #{username}: #{e.}" end $logger.info "Container #{username} created" true end |
#delete(username) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/puppetfactory/plugins/docker.rb', line 108 def delete(username) begin remove_init_scripts(username) container = ::Docker::Container.get(username) output = container.delete(:force => true) rescue => e $logger.warn "Error removing container #{username}: #{e.}" return false end $logger.info "Container #{username} removed" true end |
#login ⇒ Object
152 153 154 155 156 |
# File 'lib/puppetfactory/plugins/docker.rb', line 152 def login require 'etc' username = Etc.getpwuid(Process.euid).name exec("docker exec -it #{username} su -") end |
#repair(username) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/puppetfactory/plugins/docker.rb', line 123 def repair(username) begin container = ::Docker::Container.get(username) container.delete(:force => true) create(username, nil) rescue => e raise "Error reparing container #{username}: #{e.}" end $logger.info "Container #{username} repaired" true end |
#userinfo(username, extended = false) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/puppetfactory/plugins/docker.rb', line 137 def userinfo(username, extended = false) user = { :username => username, :port => user_port(username), :url => sandbox_url(username), } if extended user_container = ::Docker::Container.get(username).json rescue {} user[:container_status] = massage_container_state(user_container['State']) end user end |