Class: Kontena::Plugin::Local::UpCommand

Inherits:
Command
  • Object
show all
Includes:
Cli::Common
Defined in:
lib/kontena/plugin/local/up_command.rb

Instance Method Summary collapse

Instance Method Details

#create_gridObject



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
# File 'lib/kontena/plugin/local/up_command.rb', line 103

def create_grid
  token = SecureRandom.hex(16)
  Kontena.run!([
    'grid', 'create', '--token', token,
    '--silent', 'local-kontena'
  ])

  image = "kontena/agent:#{version}"
  ensure_image(image)
  agent = nil
  spinner "Creating #{pastel.cyan('agent')}" do
    agent = Docker::Container.create(
      'name' => 'kontena-agent',
      'Image' => image,
      'Env' => [
        "KONTENA_URI=http://localhost:8181",
        "KONTENA_TOKEN=#{token}",
        "CADVISOR_IMAGE=google/cadvisor",
        "CADVISOR_VERSION=v0.26.1"
      ],
      'Volumes' => {
        '/var/run/docker.sock' => {}
      },
      'HostConfig' => {
        'Binds' => ['/var/run/docker.sock:/var/run/docker.sock'],
        'NetworkMode' => 'host',
        'RestartPolicy' => { 'Name' => 'unless-stopped' }
      }
    )
    agent.start!
  end
end

#docker_uriObject



168
169
170
171
172
173
174
# File 'lib/kontena/plugin/local/up_command.rb', line 168

def docker_uri
  if @docker_uri.nil?
    @docker_uri = URI.parse(ENV['DOCKER_HOST'] || 'tcp://localhost:2376')
  end

  @docker_uri
end

#ensure_image(image) ⇒ Object



162
163
164
165
166
# File 'lib/kontena/plugin/local/up_command.rb', line 162

def ensure_image(image)
  spinner "Pulling container image #{pastel.cyan(image)}" do
    Docker::Image.create('fromImage' => image)
  end
end

#ensure_masterObject



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
# File 'lib/kontena/plugin/local/up_command.rb', line 45

def ensure_master
  master = Docker::Container.get('kontena-master-api') rescue nil
  abort('master already exists') if master
  image = "kontena/server:#{version}"
  ensure_image(image)
  spinner "Creating #{pastel.cyan('master')}" do
    master = Docker::Container.create(
      'name' => 'kontena-master-api',
      'Image' => image,
      'Env' => [
        'MONGODB_URI=mongodb://mongodb:27017/kontena_server',
        "VAULT_KEY=#{SecureRandom.hex(24)}",
        "VAULT_IV=#{SecureRandom.hex(24)}",
        "INITIAL_ADMIN_CODE=initialadmincode",
        "CONTAINER_LOGS_CAPPED_SIZE=128",
        "CONTAINER_STATS_CAPPED_SIZE=64",
        "EVENT_LOGS_CAPPED_SIZE=32",
        "WEB_CONCURRENCY=1"
      ],
      'ExposedPorts' => {
        '9292/tcp' => {}
      },
      'HostConfig' => {
        'Links' => ['kontena-master-mongo:mongodb'],
        'PortBindings' => {
          '9292/tcp' => [
            {'HostPort' => '8181'}
          ]
        },
        'RestartPolicy' => {'Name' => 'unless-stopped'}
      }
    )
    master.start
  end
end

#ensure_mongodbObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kontena/plugin/local/up_command.rb', line 24

def ensure_mongodb
  mongodb = Docker::Container.get('kontena-master-mongo') rescue nil
  return if mongodb
  ensure_image('mongo:3.2')

  spinner "Creating #{pastel.cyan('database')} container" do
    mongodb = Docker::Container.create(
      'name' => 'kontena-master-mongo',
      'Image' => 'mongo:3.2',
      'Volumes' => {
        '/data/db' => {}
      },
      'HostConfig' => {
        'Binds' => ['kontena-master-db:/data/db'],
        'RestartPolicy' => {'Name' => 'unless-stopped'}
      }
    )
    mongodb.start
  end
end

#ensure_registryObject



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
# File 'lib/kontena/plugin/local/up_command.rb', line 136

def ensure_registry
  registry = Docker::Container.get('kontena-registry') rescue nil
  return if registry

  image = "kontena/registry:2.6.2"
  ensure_image(image)
  spinner "Creating #{pastel.cyan('registry')}" do
    registry = Docker::Container.create(
      'name' => 'kontena-registry',
      'Image' => image,
      'ExposedPorts' => {
        '5000/tcp' => {}
      },
      'HostConfig' => {
        'PortBindings' => {
          '5000/tcp' => [
            {'HostPort' => '5000'}
          ]
        },
        'RestartPolicy' => {'Name' => 'unless-stopped'}
      }
    )
    registry.start
  end
end

#executeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kontena/plugin/local/up_command.rb', line 8

def execute
  ensure_mongodb
  ensure_master
  wait_master_response
  
  create_grid
  ensure_registry

  puts ""
  puts ""
  puts "  Kontena Platform Master: #{Kontena.pastel.green.on_black("http://#{docker_uri.host}:8181")}"
  puts "  Kontena Image Registry: #{Kontena.pastel.green.on_black("http://#{docker_uri.host}:5000")}"
  puts ""
  puts "  Kontena CLI is configured to use local installation, have fun!!!"
end

#login_to_masterObject



94
95
96
97
98
99
100
101
# File 'lib/kontena/plugin/local/up_command.rb', line 94

def 
  spinner "Logging in to #{pastel.cyan('master')}"
  Kontena.run!([
    'master', 'login', '--name', 'local-kontena',
    '--code', 'initialadmincode', '--silent',
    "http://#{docker_uri.host}:8181"
  ])
end

#wait_master_responseObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kontena/plugin/local/up_command.rb', line 81

def wait_master_response
  spinner "Waiting for #{pastel.cyan('master')} to start" do
    begin
      while Excon.get("http://#{docker_uri.host}:8181").status != 200
        sleep 2
      end
    rescue
      sleep 1
      retry
    end
  end
end