Class: Puppetfactory::Plugins::Docker

Inherits:
Puppetfactory::Plugins show all
Defined in:
lib/puppetfactory/plugins/docker.rb

Instance Attribute Summary

Attributes inherited from Puppetfactory::Plugins

#weight

Instance Method Summary collapse

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(options)
  super(options)

  @weight = 5

  @confdir      = options[:confdir]
  @stagedir     = options[:stagedir]
  @environments = "#{@stagedir}/environments"
  @puppetcode   = options[:puppetcode]
  @master       = options[:master]
  @usersuffix   = options[:usersuffix]
  @modulepath   = options[:modulepath]
  @templatedir  = options[:templatedir]
  @container    = options[:container_name] || 'centosagent'
  @group        = options[:docker_group]   || 'docker'
  @docker_ip    = options[:docker_ip]      || `facter ipaddress_docker0`.strip
  @privileged   = options[: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
107
# 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/rubygems:/var/cache/rubygems",
      "/var/cache/yum:/var/cache/yum",
      "/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.message}"
  end

  $logger.info "Container #{username} created"
  true
end

#delete(username) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/puppetfactory/plugins/docker.rb', line 109

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.message}"
    return false
  end

  $logger.info "Container #{username} removed"
  true
end

#loginObject



153
154
155
156
157
# File 'lib/puppetfactory/plugins/docker.rb', line 153

def 
  require 'etc'
  username = Etc.getpwuid(Process.euid).name
  exec("docker exec -it #{username} su -")
end

#repair(username) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/puppetfactory/plugins/docker.rb', line 124

def repair(username)
  begin
    container = ::Docker::Container.get(username)
    container.delete(:force => true)

    create(username, nil)
  rescue => e
    raise "Error reparing container #{username}: #{e.message}"
  end

  $logger.info "Container #{username} repaired"
  true
end

#userinfo(username, extended = false) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puppetfactory/plugins/docker.rb', line 138

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