kontena-cli 0.14.7 → 0.15.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/VERSION +1 -1
- data/bin/kontena +3 -50
- data/examples/kontena-plugin-hello/.gitignore +9 -0
- data/examples/kontena-plugin-hello/Gemfile +4 -0
- data/examples/kontena-plugin-hello/README.md +3 -0
- data/examples/kontena-plugin-hello/bin/console +14 -0
- data/examples/kontena-plugin-hello/bin/setup +8 -0
- data/examples/kontena-plugin-hello/kontena-plugin-hello.gemspec +22 -0
- data/examples/kontena-plugin-hello/lib/kontena/plugin/hello.rb +7 -0
- data/examples/kontena-plugin-hello/lib/kontena/plugin/hello/world_command.rb +6 -0
- data/examples/kontena-plugin-hello/lib/kontena/plugin/hello_command.rb +9 -0
- data/examples/kontena-plugin-hello/lib/kontena_cli_plugin.rb +5 -0
- data/kontena-cli.gemspec +1 -3
- data/lib/kontena/cli/apps/build_command.rb +3 -0
- data/lib/kontena/cli/apps/common.rb +6 -1
- data/lib/kontena/cli/apps/deploy_command.rb +1 -1
- data/lib/kontena/cli/apps/docker_helper.rb +13 -14
- data/lib/kontena/cli/apps/service_generator.rb +19 -11
- data/lib/kontena/cli/apps/service_generator_v2.rb +1 -0
- data/lib/kontena/cli/apps/yaml/reader.rb +15 -0
- data/lib/kontena/cli/apps/yaml/service_extender.rb +28 -0
- data/lib/kontena/cli/apps/yaml/validations.rb +38 -24
- data/lib/kontena/cli/apps/yaml/validator.rb +7 -7
- data/lib/kontena/cli/apps/yaml/validator_v2.rb +7 -6
- data/lib/kontena/cli/certificate/authorize_command.rb +23 -0
- data/lib/kontena/cli/certificate/get_command.rb +25 -0
- data/lib/kontena/cli/certificate/register_command.rb +19 -0
- data/lib/kontena/cli/certificate_command.rb +14 -0
- data/lib/kontena/cli/containers/containers_helper.rb +22 -0
- data/lib/kontena/cli/containers/exec_command.rb +9 -2
- data/lib/kontena/cli/grids/logs_command.rb +14 -3
- data/lib/kontena/cli/master_command.rb +1 -12
- data/lib/kontena/cli/node_command.rb +0 -16
- data/lib/kontena/cli/plugin_command.rb +15 -0
- data/lib/kontena/cli/plugins/install_command.rb +28 -0
- data/lib/kontena/cli/plugins/list_command.rb +13 -0
- data/lib/kontena/cli/plugins/search_command.rb +29 -0
- data/lib/kontena/cli/plugins/uninstall_command.rb +30 -0
- data/lib/kontena/cli/registry/create_command.rb +1 -1
- data/lib/kontena/cli/services/create_command.rb +16 -0
- data/lib/kontena/cli/services/list_command.rb +17 -3
- data/lib/kontena/cli/services/logs_command.rb +63 -18
- data/lib/kontena/cli/services/services_helper.rb +47 -4
- data/lib/kontena/cli/services/update_command.rb +16 -0
- data/lib/kontena/cli/stack_command.rb +20 -0
- data/lib/kontena/cli/stacks/common.rb +39 -0
- data/lib/kontena/cli/stacks/create_command.rb +27 -0
- data/lib/kontena/cli/stacks/deploy_command.rb +26 -0
- data/lib/kontena/cli/stacks/list_command.rb +38 -0
- data/lib/kontena/cli/stacks/remove_command.rb +26 -0
- data/lib/kontena/cli/stacks/show_command.rb +38 -0
- data/lib/kontena/cli/stacks/update_command.rb +27 -0
- data/lib/kontena/client.rb +11 -4
- data/lib/kontena/command.rb +4 -0
- data/lib/kontena/main_command.rb +61 -0
- data/lib/kontena/plugin_manager.rb +38 -0
- data/lib/kontena/util.rb +17 -0
- data/lib/kontena_cli.rb +6 -0
- data/spec/fixtures/health.yml +26 -0
- data/spec/fixtures/kontena-build.yml +16 -0
- data/spec/fixtures/kontena_build_v2.yaml +26 -0
- data/spec/kontena/cli/app/build_command_spec.rb +13 -1
- data/spec/kontena/cli/app/common_spec.rb +11 -0
- data/spec/kontena/cli/app/deploy_command_spec.rb +1 -1
- data/spec/kontena/cli/app/docker_helper_spec.rb +45 -0
- data/spec/kontena/cli/app/service_generator_spec.rb +52 -1
- data/spec/kontena/cli/app/yaml/service_extender_spec.rb +24 -0
- data/spec/kontena/cli/app/yaml/validator_spec.rb +97 -14
- data/spec/kontena/cli/common_spec.rb +0 -5
- data/spec/kontena/cli/containers/containers_helper_spec.rb +16 -0
- data/spec/kontena/cli/services/services_helper_spec.rb +50 -1
- data/spec/kontena/cli/version_command_spec.rb +1 -1
- data/spec/kontena/client_spec.rb +156 -0
- data/spec/kontena/plugin_manager_spec.rb +22 -0
- data/spec/spec_helper.rb +1 -0
- metadata +54 -125
- data/lib/kontena/cli/master/aws/create_command.rb +0 -48
- data/lib/kontena/cli/master/aws_command.rb +0 -8
- data/lib/kontena/cli/master/azure/create_command.rb +0 -37
- data/lib/kontena/cli/master/azure_command.rb +0 -13
- data/lib/kontena/cli/master/digital_ocean/create_command.rb +0 -38
- data/lib/kontena/cli/master/digital_ocean_command.rb +0 -13
- data/lib/kontena/cli/master/packet/create_command.rb +0 -42
- data/lib/kontena/cli/master/packet_command.rb +0 -14
- data/lib/kontena/cli/master/upcloud/create_command.rb +0 -39
- data/lib/kontena/cli/master/upcloud_command.rb +0 -13
- data/lib/kontena/cli/master/vagrant/create_command.rb +0 -25
- data/lib/kontena/cli/master/vagrant/restart_command.rb +0 -20
- data/lib/kontena/cli/master/vagrant/ssh_command.rb +0 -15
- data/lib/kontena/cli/master/vagrant/start_command.rb +0 -20
- data/lib/kontena/cli/master/vagrant/stop_command.rb +0 -20
- data/lib/kontena/cli/master/vagrant/terminate_command.rb +0 -13
- data/lib/kontena/cli/master/vagrant_command.rb +0 -23
- data/lib/kontena/cli/nodes/aws/create_command.rb +0 -44
- data/lib/kontena/cli/nodes/aws/restart_command.rb +0 -29
- data/lib/kontena/cli/nodes/aws/terminate_command.rb +0 -21
- data/lib/kontena/cli/nodes/aws_command.rb +0 -15
- data/lib/kontena/cli/nodes/azure/create_command.rb +0 -38
- data/lib/kontena/cli/nodes/azure/restart_command.rb +0 -32
- data/lib/kontena/cli/nodes/azure/terminate_command.rb +0 -21
- data/lib/kontena/cli/nodes/azure_command.rb +0 -15
- data/lib/kontena/cli/nodes/digital_ocean/create_command.rb +0 -32
- data/lib/kontena/cli/nodes/digital_ocean/restart_command.rb +0 -27
- data/lib/kontena/cli/nodes/digital_ocean/terminate_command.rb +0 -19
- data/lib/kontena/cli/nodes/digital_ocean_command.rb +0 -15
- data/lib/kontena/cli/nodes/packet/create_command.rb +0 -35
- data/lib/kontena/cli/nodes/packet/restart_command.rb +0 -17
- data/lib/kontena/cli/nodes/packet/terminate_command.rb +0 -20
- data/lib/kontena/cli/nodes/packet_command.rb +0 -15
- data/lib/kontena/cli/nodes/upcloud/create_command.rb +0 -33
- data/lib/kontena/cli/nodes/upcloud/restart_command.rb +0 -20
- data/lib/kontena/cli/nodes/upcloud/terminate_command.rb +0 -20
- data/lib/kontena/cli/nodes/upcloud_command.rb +0 -15
- data/lib/kontena/cli/nodes/vagrant/create_command.rb +0 -27
- data/lib/kontena/cli/nodes/vagrant/restart_command.rb +0 -26
- data/lib/kontena/cli/nodes/vagrant/ssh_command.rb +0 -21
- data/lib/kontena/cli/nodes/vagrant/start_command.rb +0 -26
- data/lib/kontena/cli/nodes/vagrant/stop_command.rb +0 -26
- data/lib/kontena/cli/nodes/vagrant/terminate_command.rb +0 -17
- data/lib/kontena/cli/nodes/vagrant_command.rb +0 -21
- data/lib/kontena/machine/aws.rb +0 -13
- data/lib/kontena/machine/aws/cloudinit.yml +0 -71
- data/lib/kontena/machine/aws/cloudinit_master.yml +0 -118
- data/lib/kontena/machine/aws/common.rb +0 -58
- data/lib/kontena/machine/aws/master_provisioner.rb +0 -187
- data/lib/kontena/machine/aws/node_destroyer.rb +0 -51
- data/lib/kontena/machine/aws/node_provisioner.rb +0 -204
- data/lib/kontena/machine/azure.rb +0 -13
- data/lib/kontena/machine/azure/cloudinit.yml +0 -64
- data/lib/kontena/machine/azure/cloudinit_master.yml +0 -106
- data/lib/kontena/machine/azure/logger.rb +0 -27
- data/lib/kontena/machine/azure/master_provisioner.rb +0 -128
- data/lib/kontena/machine/azure/node_destroyer.rb +0 -53
- data/lib/kontena/machine/azure/node_provisioner.rb +0 -132
- data/lib/kontena/machine/digital_ocean.rb +0 -13
- data/lib/kontena/machine/digital_ocean/cloudinit.yml +0 -64
- data/lib/kontena/machine/digital_ocean/cloudinit_master.yml +0 -118
- data/lib/kontena/machine/digital_ocean/master_provisioner.rb +0 -99
- data/lib/kontena/machine/digital_ocean/node_destroyer.rb +0 -40
- data/lib/kontena/machine/digital_ocean/node_provisioner.rb +0 -88
- data/lib/kontena/machine/packet.rb +0 -17
- data/lib/kontena/machine/packet/cloudinit.yml +0 -66
- data/lib/kontena/machine/packet/cloudinit_master.yml +0 -118
- data/lib/kontena/machine/packet/master_provisioner.rb +0 -93
- data/lib/kontena/machine/packet/node_destroyer.rb +0 -42
- data/lib/kontena/machine/packet/node_provisioner.rb +0 -77
- data/lib/kontena/machine/packet/node_restarter.rb +0 -41
- data/lib/kontena/machine/packet/packet_common.rb +0 -89
- data/lib/kontena/machine/upcloud.rb +0 -9
- data/lib/kontena/machine/upcloud/cloudinit.yml +0 -64
- data/lib/kontena/machine/upcloud/cloudinit_master.yml +0 -118
- data/lib/kontena/machine/upcloud/master_provisioner.rb +0 -138
- data/lib/kontena/machine/upcloud/node_destroyer.rb +0 -85
- data/lib/kontena/machine/upcloud/node_provisioner.rb +0 -121
- data/lib/kontena/machine/upcloud/node_restarter.rb +0 -49
- data/lib/kontena/machine/upcloud/upcloud_common.rb +0 -74
- data/lib/kontena/machine/vagrant.rb +0 -12
- data/lib/kontena/machine/vagrant/Vagrantfile.master.rb.erb +0 -104
- data/lib/kontena/machine/vagrant/Vagrantfile.node.rb.erb +0 -32
- data/lib/kontena/machine/vagrant/cloudinit.yml +0 -73
- data/lib/kontena/machine/vagrant/master_destroyer.rb +0 -37
- data/lib/kontena/machine/vagrant/master_provisioner.rb +0 -79
- data/lib/kontena/machine/vagrant/node_destroyer.rb +0 -40
- data/lib/kontena/machine/vagrant/node_provisioner.rb +0 -68
@@ -1,138 +0,0 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
require 'erb'
|
3
|
-
require 'open3'
|
4
|
-
require 'shell-spinner'
|
5
|
-
|
6
|
-
module Kontena
|
7
|
-
module Machine
|
8
|
-
module Upcloud
|
9
|
-
class MasterProvisioner
|
10
|
-
include RandomName
|
11
|
-
include Machine::CertHelper
|
12
|
-
include UpcloudCommon
|
13
|
-
|
14
|
-
attr_reader :http_client, :username, :password
|
15
|
-
|
16
|
-
# @param [String] token Upcloud token
|
17
|
-
def initialize(upcloud_username, upcloud_password)
|
18
|
-
@username = upcloud_username
|
19
|
-
@password = upcloud_password
|
20
|
-
end
|
21
|
-
|
22
|
-
def run!(opts)
|
23
|
-
if File.readable?(File.expand_path(opts[:ssh_key]))
|
24
|
-
ssh_key = File.read(File.expand_path(opts[:ssh_key])).strip
|
25
|
-
end
|
26
|
-
|
27
|
-
abort('Invalid ssh key') unless ssh_key && ssh_key.start_with?('ssh-')
|
28
|
-
|
29
|
-
if opts[:ssl_cert]
|
30
|
-
abort('Invalid ssl cert') unless File.exists?(File.expand_path(opts[:ssl_cert]))
|
31
|
-
ssl_cert = File.read(File.expand_path(opts[:ssl_cert]))
|
32
|
-
else
|
33
|
-
ShellSpinner "Generating self-signed SSL certificate" do
|
34
|
-
ssl_cert = generate_self_signed_cert
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
abort_unless_api_access
|
39
|
-
|
40
|
-
abort('CoreOS template not found on Upcloud') unless coreos_template = find_template('CoreOS Stable')
|
41
|
-
abort('Server plan not found on Upcloud') unless plan = find_plan(opts[:plan])
|
42
|
-
abort('Zone not found on Upcloud') unless zone_exist?(opts[:zone])
|
43
|
-
|
44
|
-
hostname = generate_name
|
45
|
-
|
46
|
-
userdata_vars = {
|
47
|
-
ssl_cert: ssl_cert,
|
48
|
-
auth_server: opts[:auth_server],
|
49
|
-
version: opts[:version],
|
50
|
-
vault_secret: opts[:vault_secret],
|
51
|
-
vault_iv: opts[:vault_iv],
|
52
|
-
mongodb_uri: opts[:mongodb_uri]
|
53
|
-
}
|
54
|
-
|
55
|
-
device_data = {
|
56
|
-
server: {
|
57
|
-
zone: opts[:zone],
|
58
|
-
title: "Kontena Master #{hostname}",
|
59
|
-
hostname: hostname,
|
60
|
-
plan: plan[:name],
|
61
|
-
vnc: 'off',
|
62
|
-
timezone: 'UTC',
|
63
|
-
user_data: user_data(userdata_vars),
|
64
|
-
firewall: 'off',
|
65
|
-
storage_devices: {
|
66
|
-
storage_device: [
|
67
|
-
{
|
68
|
-
action: 'clone',
|
69
|
-
storage: coreos_template[:uuid],
|
70
|
-
title: "From template #{coreos_template[:title]}",
|
71
|
-
size: plan[:storage_size],
|
72
|
-
tier: 'maxiops'
|
73
|
-
}
|
74
|
-
]
|
75
|
-
},
|
76
|
-
login_user: {
|
77
|
-
create_password: 'no',
|
78
|
-
username: 'root',
|
79
|
-
ssh_keys: {
|
80
|
-
ssh_key: [ssh_key]
|
81
|
-
}
|
82
|
-
}
|
83
|
-
}
|
84
|
-
}.to_json
|
85
|
-
|
86
|
-
ShellSpinner "Creating Upcloud master #{hostname.colorize(:cyan)} " do
|
87
|
-
response = post('server', body: device_data)
|
88
|
-
if response.has_key?(:error)
|
89
|
-
abort("\nUpcloud server creation failed (#{response[:error].fetch(:error_message, '')})")
|
90
|
-
end
|
91
|
-
device_data = response[:server]
|
92
|
-
|
93
|
-
until device_data && device_data.fetch(:state, nil).to_s == 'maintenance'
|
94
|
-
device_data = get("server/#{device[:uuid]}").fetch(:server, {}) rescue nil
|
95
|
-
sleep 5
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
device_public_ip = device_data[:ip_addresses][:ip_address].find do |ip|
|
100
|
-
ip[:access].eql?('public') && ip[:family].eql?('IPv4')
|
101
|
-
end
|
102
|
-
|
103
|
-
abort('Server public ip not found, destroy manually.') unless device_public_ip
|
104
|
-
|
105
|
-
master_url = "https://#{device_public_ip[:address]}"
|
106
|
-
Excon.defaults[:ssl_verify_peer] = false
|
107
|
-
@http_client = Excon.new("#{master_url}", :connect_timeout => 10)
|
108
|
-
|
109
|
-
ShellSpinner "Waiting for #{hostname.colorize(:cyan)} to start" do
|
110
|
-
sleep 5 until master_running?
|
111
|
-
end
|
112
|
-
|
113
|
-
puts "Kontena Master is now running at #{master_url}"
|
114
|
-
puts "Use #{"kontena login --name=#{hostname.sub('kontena-master-', '')} #{master_url}".colorize(:light_black)} to complete Kontena Master setup"
|
115
|
-
end
|
116
|
-
|
117
|
-
def user_data(vars)
|
118
|
-
cloudinit_template = File.join(__dir__ , '/cloudinit_master.yml')
|
119
|
-
erb(File.read(cloudinit_template), vars)
|
120
|
-
end
|
121
|
-
|
122
|
-
def generate_name
|
123
|
-
"kontena-master-#{super}-#{rand(1..9)}"
|
124
|
-
end
|
125
|
-
|
126
|
-
def master_running?
|
127
|
-
http_client.get(path: '/').status == 200
|
128
|
-
rescue
|
129
|
-
false
|
130
|
-
end
|
131
|
-
|
132
|
-
def erb(template, vars)
|
133
|
-
ERB.new(template, nil, '%<>-').result(OpenStruct.new(vars).instance_eval { binding })
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require 'shell-spinner'
|
2
|
-
|
3
|
-
module Kontena
|
4
|
-
module Machine
|
5
|
-
module Upcloud
|
6
|
-
class NodeDestroyer
|
7
|
-
include RandomName
|
8
|
-
include UpcloudCommon
|
9
|
-
|
10
|
-
attr_reader :api_client, :username, :password
|
11
|
-
|
12
|
-
# @param [Kontena::Client] api_client Kontena api client
|
13
|
-
# @param [String] token Upcloud token
|
14
|
-
def initialize(api_client, upcloud_username, upcloud_password)
|
15
|
-
@api_client = api_client
|
16
|
-
@username = upcloud_username
|
17
|
-
@password = upcloud_password
|
18
|
-
end
|
19
|
-
|
20
|
-
def run!(grid, name)
|
21
|
-
|
22
|
-
abort_unless_api_access
|
23
|
-
|
24
|
-
servers = get('server')
|
25
|
-
unless servers && servers.has_key?(:servers)
|
26
|
-
abort('Upcloud API error')
|
27
|
-
end
|
28
|
-
|
29
|
-
server = servers[:servers][:server].find{|s| s[:hostname] == name}
|
30
|
-
|
31
|
-
abort "Cannot find node #{name.colorize(:cyan)} in Upcloud" unless server
|
32
|
-
|
33
|
-
server_data = get("server/#{server[:uuid]}")
|
34
|
-
|
35
|
-
storage_devices = server_data.fetch(:server, {}).fetch(:storage_devices, {}).fetch(:storage_device, [])
|
36
|
-
storage_uuids = storage_devices.map{|s| s[:storage]}
|
37
|
-
|
38
|
-
abort('No storage devices found for Upcloud node') if storage_uuids.empty?
|
39
|
-
|
40
|
-
if server
|
41
|
-
unless server[:state].eql?('stopped')
|
42
|
-
ShellSpinner "Shutting down Upcloud node #{name.colorize(:cyan)} " do
|
43
|
-
device_data = post(
|
44
|
-
"server/#{server[:uuid]}/stop", body: {
|
45
|
-
stop_server: {
|
46
|
-
stop_type: 'soft',
|
47
|
-
timeout: 120
|
48
|
-
}
|
49
|
-
}.to_json
|
50
|
-
)
|
51
|
-
|
52
|
-
until device_data && device_data.fetch(:state, nil).to_s.eql?('stopped')
|
53
|
-
device_data = get("server/#{server[:uuid]}").fetch(:server, {}) rescue nil
|
54
|
-
sleep 5
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
ShellSpinner "Terminating Upcloud node #{name.colorize(:cyan)} " do
|
60
|
-
response = delete("server/#{server[:uuid]}")
|
61
|
-
abort "Cannot delete node #{name.colorize(:cyan)} in Upcloud" unless response[:success]
|
62
|
-
end
|
63
|
-
|
64
|
-
storage_uuids.each do |uuid|
|
65
|
-
ShellSpinner "Deleting Upcloud storage device '#{uuid.colorize(:cyan)}' " do
|
66
|
-
response = delete("storage/#{uuid}")
|
67
|
-
unless response[:success]
|
68
|
-
puts "#{"WARNING".colorize(:red)}: Couldn't delete Upcloud storage '#{uuid.colorize(:cyan)}', check manually."
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
else
|
73
|
-
abort "Cannot find node #{name.colorize(:cyan)} in Upcloud"
|
74
|
-
end
|
75
|
-
node = api_client.get("grids/#{grid['id']}/nodes")['nodes'].find{|n| n['name'] == name}
|
76
|
-
if node
|
77
|
-
ShellSpinner "Removing node #{name.colorize(:cyan)} from grid #{grid['name'].colorize(:cyan)} " do
|
78
|
-
api_client.delete("grids/#{grid['id']}/nodes/#{name}")
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
@@ -1,121 +0,0 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
require 'erb'
|
3
|
-
require 'open3'
|
4
|
-
require 'shell-spinner'
|
5
|
-
|
6
|
-
module Kontena
|
7
|
-
module Machine
|
8
|
-
module Upcloud
|
9
|
-
class NodeProvisioner
|
10
|
-
include RandomName
|
11
|
-
include UpcloudCommon
|
12
|
-
|
13
|
-
attr_reader :api_client, :username, :password
|
14
|
-
|
15
|
-
# @param [Kontena::Client] api_client Kontena api client
|
16
|
-
# @param [String] upcloud_username Upcloud username
|
17
|
-
# @param [String] upcloud_password Upcloud password
|
18
|
-
def initialize(api_client, upcloud_username, upcloud_password)
|
19
|
-
@api_client = api_client
|
20
|
-
@username = upcloud_username
|
21
|
-
@password = upcloud_password
|
22
|
-
end
|
23
|
-
|
24
|
-
def run!(opts)
|
25
|
-
if File.readable?(File.expand_path(opts[:ssh_key]))
|
26
|
-
ssh_key = File.read(File.expand_path(opts[:ssh_key])).strip
|
27
|
-
end
|
28
|
-
|
29
|
-
abort('Invalid ssh key') unless ssh_key && ssh_key.start_with?('ssh-')
|
30
|
-
|
31
|
-
userdata_vars = {
|
32
|
-
version: opts[:version],
|
33
|
-
master_uri: opts[:master_uri],
|
34
|
-
grid_token: opts[:grid_token],
|
35
|
-
}
|
36
|
-
|
37
|
-
abort_unless_api_access
|
38
|
-
|
39
|
-
abort('CoreOS template not found on Upcloud') unless coreos_template = find_template('CoreOS Stable')
|
40
|
-
abort('Server plan not found on Upcloud') unless plan = find_plan(opts[:plan])
|
41
|
-
abort('Zone not found on Upcloud') unless zone_exist?(opts[:zone])
|
42
|
-
|
43
|
-
hostname = generate_name
|
44
|
-
|
45
|
-
device_data = {
|
46
|
-
server: {
|
47
|
-
zone: opts[:zone],
|
48
|
-
title: "Kontena Grid #{opts[:grid]} Node #{hostname}",
|
49
|
-
hostname: hostname,
|
50
|
-
plan: plan[:name],
|
51
|
-
vnc: 'off',
|
52
|
-
timezone: 'UTC',
|
53
|
-
user_data: user_data(userdata_vars),
|
54
|
-
firewall: 'off',
|
55
|
-
storage_devices: {
|
56
|
-
storage_device: [
|
57
|
-
{
|
58
|
-
action: 'clone',
|
59
|
-
storage: coreos_template[:uuid],
|
60
|
-
title: "From template #{coreos_template[:title]}",
|
61
|
-
size: plan[:storage_size],
|
62
|
-
tier: 'maxiops'
|
63
|
-
}
|
64
|
-
]
|
65
|
-
},
|
66
|
-
login_user: {
|
67
|
-
create_password: 'no',
|
68
|
-
username: 'root',
|
69
|
-
ssh_keys: {
|
70
|
-
ssh_key: [ssh_key]
|
71
|
-
}
|
72
|
-
}
|
73
|
-
}
|
74
|
-
}.to_json
|
75
|
-
|
76
|
-
ShellSpinner "Creating Upcloud node #{hostname.colorize(:cyan)} " do
|
77
|
-
response = post('server', body: device_data)
|
78
|
-
|
79
|
-
if response.has_key?(:error)
|
80
|
-
abort("\nUpcloud server creation failed (#{response[:error].fetch(:error_message, '')})")
|
81
|
-
end
|
82
|
-
device_data = response[:server]
|
83
|
-
|
84
|
-
until device_data && device_data.fetch(:state, nil).to_s == 'maintenance'
|
85
|
-
device_data = get("server/#{device[:uuid]}").fetch(:server, {}) rescue nil
|
86
|
-
sleep 5
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
node = nil
|
91
|
-
ShellSpinner "Waiting for node #{hostname.colorize(:cyan)} join to grid #{opts[:grid].colorize(:cyan)} " do
|
92
|
-
sleep 2 until node = node_exists_in_grid?(opts[:grid], hostname)
|
93
|
-
end
|
94
|
-
set_labels(node, ["region=#{opts[:zone]}", "provider=upcloud"])
|
95
|
-
end
|
96
|
-
|
97
|
-
def user_data(vars)
|
98
|
-
cloudinit_template = File.join(__dir__ , '/cloudinit.yml')
|
99
|
-
erb(File.read(cloudinit_template), vars)
|
100
|
-
end
|
101
|
-
|
102
|
-
def generate_name
|
103
|
-
"#{super}-#{rand(1..99)}"
|
104
|
-
end
|
105
|
-
|
106
|
-
def node_exists_in_grid?(grid, hostname)
|
107
|
-
api_client.get("grids/#{grid}/nodes")['nodes'].find{|n| n['name'] == hostname}
|
108
|
-
end
|
109
|
-
|
110
|
-
def erb(template, vars)
|
111
|
-
ERB.new(template).result(OpenStruct.new(vars).instance_eval { binding })
|
112
|
-
end
|
113
|
-
|
114
|
-
def set_labels(node, labels)
|
115
|
-
data = {labels: labels}
|
116
|
-
api_client.put("nodes/#{node['id']}", data, {}, {'Kontena-Grid-Token' => node['grid']['token']})
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'shell-spinner'
|
2
|
-
|
3
|
-
module Kontena
|
4
|
-
module Machine
|
5
|
-
module Upcloud
|
6
|
-
class NodeRestarter
|
7
|
-
include RandomName
|
8
|
-
include UpcloudCommon
|
9
|
-
|
10
|
-
attr_reader :username, :password
|
11
|
-
|
12
|
-
# @param [String] upcloud_username Upcloud username
|
13
|
-
# @param [String] upcloud_password Upcloud password
|
14
|
-
def initialize(upcloud_username, upcloud_password)
|
15
|
-
@username = upcloud_username
|
16
|
-
@password = upcloud_password
|
17
|
-
end
|
18
|
-
|
19
|
-
def run!(name)
|
20
|
-
abort_unless_api_access
|
21
|
-
|
22
|
-
servers = get('server')
|
23
|
-
unless servers && servers.has_key?(:servers)
|
24
|
-
abort('Upcloud API error')
|
25
|
-
end
|
26
|
-
|
27
|
-
server = servers[:servers][:server].find{|s| s[:hostname] == name}
|
28
|
-
|
29
|
-
if server
|
30
|
-
ShellSpinner "Restarting Upcloud node #{name.colorize(:cyan)} " do
|
31
|
-
result = post(
|
32
|
-
"server/#{server[:uuid]}/restart", body: {
|
33
|
-
restart_server: {
|
34
|
-
stop_type: 'soft',
|
35
|
-
timeout: 600,
|
36
|
-
timeout_action: 'ignore'
|
37
|
-
}
|
38
|
-
}.to_json
|
39
|
-
)
|
40
|
-
end
|
41
|
-
else
|
42
|
-
abort "Cannot find node #{name.colorize(:cyan)} in Upcloud"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
@@ -1,74 +0,0 @@
|
|
1
|
-
require 'excon'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
module Kontena
|
5
|
-
module Machine
|
6
|
-
module Upcloud
|
7
|
-
module UpcloudCommon
|
8
|
-
|
9
|
-
attr_reader :username
|
10
|
-
attr_reader :password
|
11
|
-
|
12
|
-
def client
|
13
|
-
@client ||= Excon.new(
|
14
|
-
'https://api.upcloud.com',
|
15
|
-
omit_default_port: true,
|
16
|
-
user: username,
|
17
|
-
password: password,
|
18
|
-
headers: { "Accept-Encoding" => 'application/json' }
|
19
|
-
)
|
20
|
-
end
|
21
|
-
|
22
|
-
def find_template(name)
|
23
|
-
get('storage/template')[:storages][:storage].find{|s| s[:title].downcase.start_with?(name.downcase)}
|
24
|
-
end
|
25
|
-
|
26
|
-
def find_plan(name)
|
27
|
-
get('plan')[:plans][:plan].find{|s| s[:name].downcase.eql?(name.downcase)}
|
28
|
-
end
|
29
|
-
|
30
|
-
def zone_exist?(name)
|
31
|
-
get('zone')[:zones][:zone].map{|p| p[:id]}.include?(name)
|
32
|
-
end
|
33
|
-
|
34
|
-
def get_server(id)
|
35
|
-
get("server/#{id}").fetch(:server, nil)
|
36
|
-
end
|
37
|
-
|
38
|
-
def api_access?
|
39
|
-
response = get('account')
|
40
|
-
response.kind_of?(Hash) && response.has_key?(:account)
|
41
|
-
rescue
|
42
|
-
false
|
43
|
-
end
|
44
|
-
|
45
|
-
def abort_unless_api_access
|
46
|
-
unless api_access?
|
47
|
-
abort('Upcloud API authentication failed. Check that API access is enabled for the user.')
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
[:get, :post, :delete].each do |http_method|
|
52
|
-
define_method http_method do |path, opts={}|
|
53
|
-
response = client.send(
|
54
|
-
http_method,
|
55
|
-
opts.merge(
|
56
|
-
path: File.join('/1.2', path),
|
57
|
-
headers: {
|
58
|
-
'Content-Type': 'application/json'
|
59
|
-
}
|
60
|
-
)
|
61
|
-
)
|
62
|
-
if response.body && response.body.start_with?('{')
|
63
|
-
JSON.parse(response.body, symbolize_names: true)
|
64
|
-
elsif response.status.to_s.start_with?('2')
|
65
|
-
{success: true}
|
66
|
-
else
|
67
|
-
{error: response.status}
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|