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
# File 'lib/puppetfactory/plugins/docker.rb', line 10

def initialize(options)
  super(options)

  @weight = 5

  @confdir      = options[:confdir]
  @environments = options[:environments]
  @puppetcode   = options[:puppetcode]
  @master       = options[:master]
  @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]     || true
end

Instance Method Details

#create(username, password) ⇒ Object



27
28
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
# File 'lib/puppetfactory/plugins/docker.rb', line 27

def create(username, password)
  begin
    environment = "#{@environments}/#{Puppetfactory::Helpers.environment_name(username)}"

    binds = [
      "/var/yum:/var/yum",
      "/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" => "puppetlabs.vm",
      "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,
        "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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppetfactory/plugins/docker.rb', line 96

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

#login ⇒ Object



140
141
142
143
144
# File 'lib/puppetfactory/plugins/docker.rb', line 140

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

#repair(username) ⇒ Object



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

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



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

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