Class: Kontena::Cli::Registry::CreateCommand

Inherits:
Kontena::Command show all
Includes:
Common, GridOptions, Stacks::StacksHelper
Defined in:
lib/kontena/cli/registry/create_command.rb

Constant Summary collapse

REGISTRY_VERSION =
'2.6.0'

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from Stacks::StacksHelper

#wait_for_deploy_to_finish, #wait_for_deployment_to_start, #wait_for_service_deploy

Methods included from GridOptions

included

Methods included from Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#configure_registry_auth(password) ⇒ Object

Parameters:



141
142
143
144
145
146
147
148
149
# File 'lib/kontena/cli/registry/create_command.rb', line 141

def configure_registry_auth(password)
  data = {
    username: 'admin',
    password: password,
    email: '[email protected]',
    url: "http://registry.#{current_grid}.kontena.local/"
  }
  client(require_token).post("grids/#{current_grid}/external_registries", data) rescue nil
end

#executeObject



21
22
23
24
25
26
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kontena/cli/registry/create_command.rb', line 21

def execute
  require_api_url
  token = require_token
  preferred_node = node
  secrets = []
  affinity = []
  stateful = true
  instances = 1

  registry = client(token).get("services/#{current_grid}/registry") rescue nil
  exit_with_error('Registry already exists') if registry

  nodes = client(token).get("grids/#{current_grid}/nodes")

  if s3_bucket
    ['REGISTRY_STORAGE_S3_ACCESSKEY', 'REGISTRY_STORAGE_S3_SECRETKEY'].each do |secret|
      exit_with_error("Secret #{secret} does not exist in the vault") unless vault_secret_exists?(secret)
    end
    env = [
        "REGISTRY_STORAGE=s3",
        "REGISTRY_STORAGE_S3_REGION=#{s3_region}",
        "REGISTRY_STORAGE_S3_BUCKET=#{s3_bucket}",
        "REGISTRY_STORAGE_S3_ENCRYPT=#{s3_encrypt?}",
        "REGISTRY_STORAGE_S3_SECURE=#{s3_secure?}",
        "REGISTRY_STORAGE_S3_V4AUTH=#{s3_v4auth?}"
    ]
    secrets = [
      {secret: 'REGISTRY_STORAGE_S3_ACCESSKEY', name: 'REGISTRY_STORAGE_S3_ACCESSKEY', type: 'env'},
      {secret: 'REGISTRY_STORAGE_S3_SECRETKEY', name: 'REGISTRY_STORAGE_S3_SECRETKEY', type: 'env'}
    ]
    stateful = false
    instances = 2 if nodes['nodes'].size > 1
  elsif  || azure_container_name
    exit_with_error('Option --azure-account-name is missing') if .nil?
    exit_with_error('Option --azure-container-name is missing') if azure_container_name.nil?
    exit_with_error('Secret REGISTRY_STORAGE_AZURE_ACCOUNTKEY does not exist in the vault') unless vault_secret_exists?('REGISTRY_STORAGE_AZURE_ACCOUNTKEY')
    env = [
        "REGISTRY_STORAGE=azure",
        "REGISTRY_STORAGE_AZURE_ACCOUNTNAME=#{}",
        "REGISTRY_STORAGE_AZURE_CONTAINER=#{azure_container_name}"
    ]
    secrets = [
      {secret: 'REGISTRY_STORAGE_AZURE_ACCOUNTKEY', name: 'REGISTRY_STORAGE_AZURE_ACCOUNTKEY', type: 'env'}
    ]
    stateful = false
    instances = 2 if nodes['nodes'].size > 1
  else
    env = [
        "REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/registry"
    ]
    if preferred_node
      node = nodes['nodes'].find{|n| n['connected'] && n['name'] == preferred_node }
      exit_with_error('Node not found') if node.nil?
      affinity << "node==#{node['name']}"
    end
  end

  if vault_secret_exists?('REGISTRY_AUTH_PASSWORD')
    secrets << {secret: 'REGISTRY_AUTH_PASSWORD', name: 'AUTH_PASSWORD', type: 'env'}
    configure_registry_auth(vault_secret('REGISTRY_AUTH_PASSWORD'))
  end
  if vault_secret_exists?('REGISTRY_HTTP_TLS_CERTIFICATE')
    secrets << {secret: 'REGISTRY_HTTP_TLS_CERTIFICATE', name: 'REGISTRY_HTTP_TLS_CERTIFICATE', type: 'env'}
    secrets << {secret: 'REGISTRY_HTTP_TLS_KEY', name: 'REGISTRY_HTTP_TLS_KEY', type: 'env'}
    env << "REGISTRY_HTTP_ADDR=0.0.0.0:443"
  else
    env << "REGISTRY_HTTP_ADDR=0.0.0.0:80"
  end
  env << "REGISTRY_HTTP_SECRET=#{SecureRandom.hex(24)}"

  data = {
    name: 'registry',
    stack: 'kontena/registry',
    version: Kontena::Cli::VERSION,
    source: '---',
    registry: 'file://',
    expose: 'api',
    services: [
      {
        name: 'api',
        stateful: stateful,
        container_count: instances,
        image: "kontena/registry:#{REGISTRY_VERSION}",
        volumes: ['/registry'],
        env: env,
        secrets: secrets,
        affinity: affinity
      }
    ]
  }

  client(token).post("grids/#{current_grid}/stacks", data)
  deployment = client(token).post("stacks/#{current_grid}/registry/deploy", {})
  spinner "Deploying #{pastel.cyan(data[:name])} stack " do
    wait_for_deploy_to_finish(deployment)
  end
  puts "\n"
  puts "Docker Registry #{REGISTRY_VERSION} is now running at registry.#{current_grid}.kontena.local."
  puts "Note: "
  puts "  - OpenVPN connection is needed to establish connection to this registry. See http://www.kontena.io/docs/using-kontena/vpn-access for details"
  puts "  - you must set '--insecure-registry registry.#{current_grid}.kontena.local' to your client docker daemon before you are able to push to this registry"
end

#vault_secret(name) ⇒ String

Parameters:

Returns:



135
136
137
138
# File 'lib/kontena/cli/registry/create_command.rb', line 135

def vault_secret(name)
  secret = client(require_token).get("secrets/#{current_grid}/#{name}")
  secret['value']
end

#vault_secret_exists?(name) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


126
127
128
129
130
131
# File 'lib/kontena/cli/registry/create_command.rb', line 126

def vault_secret_exists?(name)
  client(require_token).get("secrets/#{current_grid}/#{name}")
  true
rescue
  false
end