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.
Files changed (166) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -1
  3. data/VERSION +1 -1
  4. data/bin/kontena +3 -50
  5. data/examples/kontena-plugin-hello/.gitignore +9 -0
  6. data/examples/kontena-plugin-hello/Gemfile +4 -0
  7. data/examples/kontena-plugin-hello/README.md +3 -0
  8. data/examples/kontena-plugin-hello/bin/console +14 -0
  9. data/examples/kontena-plugin-hello/bin/setup +8 -0
  10. data/examples/kontena-plugin-hello/kontena-plugin-hello.gemspec +22 -0
  11. data/examples/kontena-plugin-hello/lib/kontena/plugin/hello.rb +7 -0
  12. data/examples/kontena-plugin-hello/lib/kontena/plugin/hello/world_command.rb +6 -0
  13. data/examples/kontena-plugin-hello/lib/kontena/plugin/hello_command.rb +9 -0
  14. data/examples/kontena-plugin-hello/lib/kontena_cli_plugin.rb +5 -0
  15. data/kontena-cli.gemspec +1 -3
  16. data/lib/kontena/cli/apps/build_command.rb +3 -0
  17. data/lib/kontena/cli/apps/common.rb +6 -1
  18. data/lib/kontena/cli/apps/deploy_command.rb +1 -1
  19. data/lib/kontena/cli/apps/docker_helper.rb +13 -14
  20. data/lib/kontena/cli/apps/service_generator.rb +19 -11
  21. data/lib/kontena/cli/apps/service_generator_v2.rb +1 -0
  22. data/lib/kontena/cli/apps/yaml/reader.rb +15 -0
  23. data/lib/kontena/cli/apps/yaml/service_extender.rb +28 -0
  24. data/lib/kontena/cli/apps/yaml/validations.rb +38 -24
  25. data/lib/kontena/cli/apps/yaml/validator.rb +7 -7
  26. data/lib/kontena/cli/apps/yaml/validator_v2.rb +7 -6
  27. data/lib/kontena/cli/certificate/authorize_command.rb +23 -0
  28. data/lib/kontena/cli/certificate/get_command.rb +25 -0
  29. data/lib/kontena/cli/certificate/register_command.rb +19 -0
  30. data/lib/kontena/cli/certificate_command.rb +14 -0
  31. data/lib/kontena/cli/containers/containers_helper.rb +22 -0
  32. data/lib/kontena/cli/containers/exec_command.rb +9 -2
  33. data/lib/kontena/cli/grids/logs_command.rb +14 -3
  34. data/lib/kontena/cli/master_command.rb +1 -12
  35. data/lib/kontena/cli/node_command.rb +0 -16
  36. data/lib/kontena/cli/plugin_command.rb +15 -0
  37. data/lib/kontena/cli/plugins/install_command.rb +28 -0
  38. data/lib/kontena/cli/plugins/list_command.rb +13 -0
  39. data/lib/kontena/cli/plugins/search_command.rb +29 -0
  40. data/lib/kontena/cli/plugins/uninstall_command.rb +30 -0
  41. data/lib/kontena/cli/registry/create_command.rb +1 -1
  42. data/lib/kontena/cli/services/create_command.rb +16 -0
  43. data/lib/kontena/cli/services/list_command.rb +17 -3
  44. data/lib/kontena/cli/services/logs_command.rb +63 -18
  45. data/lib/kontena/cli/services/services_helper.rb +47 -4
  46. data/lib/kontena/cli/services/update_command.rb +16 -0
  47. data/lib/kontena/cli/stack_command.rb +20 -0
  48. data/lib/kontena/cli/stacks/common.rb +39 -0
  49. data/lib/kontena/cli/stacks/create_command.rb +27 -0
  50. data/lib/kontena/cli/stacks/deploy_command.rb +26 -0
  51. data/lib/kontena/cli/stacks/list_command.rb +38 -0
  52. data/lib/kontena/cli/stacks/remove_command.rb +26 -0
  53. data/lib/kontena/cli/stacks/show_command.rb +38 -0
  54. data/lib/kontena/cli/stacks/update_command.rb +27 -0
  55. data/lib/kontena/client.rb +11 -4
  56. data/lib/kontena/command.rb +4 -0
  57. data/lib/kontena/main_command.rb +61 -0
  58. data/lib/kontena/plugin_manager.rb +38 -0
  59. data/lib/kontena/util.rb +17 -0
  60. data/lib/kontena_cli.rb +6 -0
  61. data/spec/fixtures/health.yml +26 -0
  62. data/spec/fixtures/kontena-build.yml +16 -0
  63. data/spec/fixtures/kontena_build_v2.yaml +26 -0
  64. data/spec/kontena/cli/app/build_command_spec.rb +13 -1
  65. data/spec/kontena/cli/app/common_spec.rb +11 -0
  66. data/spec/kontena/cli/app/deploy_command_spec.rb +1 -1
  67. data/spec/kontena/cli/app/docker_helper_spec.rb +45 -0
  68. data/spec/kontena/cli/app/service_generator_spec.rb +52 -1
  69. data/spec/kontena/cli/app/yaml/service_extender_spec.rb +24 -0
  70. data/spec/kontena/cli/app/yaml/validator_spec.rb +97 -14
  71. data/spec/kontena/cli/common_spec.rb +0 -5
  72. data/spec/kontena/cli/containers/containers_helper_spec.rb +16 -0
  73. data/spec/kontena/cli/services/services_helper_spec.rb +50 -1
  74. data/spec/kontena/cli/version_command_spec.rb +1 -1
  75. data/spec/kontena/client_spec.rb +156 -0
  76. data/spec/kontena/plugin_manager_spec.rb +22 -0
  77. data/spec/spec_helper.rb +1 -0
  78. metadata +54 -125
  79. data/lib/kontena/cli/master/aws/create_command.rb +0 -48
  80. data/lib/kontena/cli/master/aws_command.rb +0 -8
  81. data/lib/kontena/cli/master/azure/create_command.rb +0 -37
  82. data/lib/kontena/cli/master/azure_command.rb +0 -13
  83. data/lib/kontena/cli/master/digital_ocean/create_command.rb +0 -38
  84. data/lib/kontena/cli/master/digital_ocean_command.rb +0 -13
  85. data/lib/kontena/cli/master/packet/create_command.rb +0 -42
  86. data/lib/kontena/cli/master/packet_command.rb +0 -14
  87. data/lib/kontena/cli/master/upcloud/create_command.rb +0 -39
  88. data/lib/kontena/cli/master/upcloud_command.rb +0 -13
  89. data/lib/kontena/cli/master/vagrant/create_command.rb +0 -25
  90. data/lib/kontena/cli/master/vagrant/restart_command.rb +0 -20
  91. data/lib/kontena/cli/master/vagrant/ssh_command.rb +0 -15
  92. data/lib/kontena/cli/master/vagrant/start_command.rb +0 -20
  93. data/lib/kontena/cli/master/vagrant/stop_command.rb +0 -20
  94. data/lib/kontena/cli/master/vagrant/terminate_command.rb +0 -13
  95. data/lib/kontena/cli/master/vagrant_command.rb +0 -23
  96. data/lib/kontena/cli/nodes/aws/create_command.rb +0 -44
  97. data/lib/kontena/cli/nodes/aws/restart_command.rb +0 -29
  98. data/lib/kontena/cli/nodes/aws/terminate_command.rb +0 -21
  99. data/lib/kontena/cli/nodes/aws_command.rb +0 -15
  100. data/lib/kontena/cli/nodes/azure/create_command.rb +0 -38
  101. data/lib/kontena/cli/nodes/azure/restart_command.rb +0 -32
  102. data/lib/kontena/cli/nodes/azure/terminate_command.rb +0 -21
  103. data/lib/kontena/cli/nodes/azure_command.rb +0 -15
  104. data/lib/kontena/cli/nodes/digital_ocean/create_command.rb +0 -32
  105. data/lib/kontena/cli/nodes/digital_ocean/restart_command.rb +0 -27
  106. data/lib/kontena/cli/nodes/digital_ocean/terminate_command.rb +0 -19
  107. data/lib/kontena/cli/nodes/digital_ocean_command.rb +0 -15
  108. data/lib/kontena/cli/nodes/packet/create_command.rb +0 -35
  109. data/lib/kontena/cli/nodes/packet/restart_command.rb +0 -17
  110. data/lib/kontena/cli/nodes/packet/terminate_command.rb +0 -20
  111. data/lib/kontena/cli/nodes/packet_command.rb +0 -15
  112. data/lib/kontena/cli/nodes/upcloud/create_command.rb +0 -33
  113. data/lib/kontena/cli/nodes/upcloud/restart_command.rb +0 -20
  114. data/lib/kontena/cli/nodes/upcloud/terminate_command.rb +0 -20
  115. data/lib/kontena/cli/nodes/upcloud_command.rb +0 -15
  116. data/lib/kontena/cli/nodes/vagrant/create_command.rb +0 -27
  117. data/lib/kontena/cli/nodes/vagrant/restart_command.rb +0 -26
  118. data/lib/kontena/cli/nodes/vagrant/ssh_command.rb +0 -21
  119. data/lib/kontena/cli/nodes/vagrant/start_command.rb +0 -26
  120. data/lib/kontena/cli/nodes/vagrant/stop_command.rb +0 -26
  121. data/lib/kontena/cli/nodes/vagrant/terminate_command.rb +0 -17
  122. data/lib/kontena/cli/nodes/vagrant_command.rb +0 -21
  123. data/lib/kontena/machine/aws.rb +0 -13
  124. data/lib/kontena/machine/aws/cloudinit.yml +0 -71
  125. data/lib/kontena/machine/aws/cloudinit_master.yml +0 -118
  126. data/lib/kontena/machine/aws/common.rb +0 -58
  127. data/lib/kontena/machine/aws/master_provisioner.rb +0 -187
  128. data/lib/kontena/machine/aws/node_destroyer.rb +0 -51
  129. data/lib/kontena/machine/aws/node_provisioner.rb +0 -204
  130. data/lib/kontena/machine/azure.rb +0 -13
  131. data/lib/kontena/machine/azure/cloudinit.yml +0 -64
  132. data/lib/kontena/machine/azure/cloudinit_master.yml +0 -106
  133. data/lib/kontena/machine/azure/logger.rb +0 -27
  134. data/lib/kontena/machine/azure/master_provisioner.rb +0 -128
  135. data/lib/kontena/machine/azure/node_destroyer.rb +0 -53
  136. data/lib/kontena/machine/azure/node_provisioner.rb +0 -132
  137. data/lib/kontena/machine/digital_ocean.rb +0 -13
  138. data/lib/kontena/machine/digital_ocean/cloudinit.yml +0 -64
  139. data/lib/kontena/machine/digital_ocean/cloudinit_master.yml +0 -118
  140. data/lib/kontena/machine/digital_ocean/master_provisioner.rb +0 -99
  141. data/lib/kontena/machine/digital_ocean/node_destroyer.rb +0 -40
  142. data/lib/kontena/machine/digital_ocean/node_provisioner.rb +0 -88
  143. data/lib/kontena/machine/packet.rb +0 -17
  144. data/lib/kontena/machine/packet/cloudinit.yml +0 -66
  145. data/lib/kontena/machine/packet/cloudinit_master.yml +0 -118
  146. data/lib/kontena/machine/packet/master_provisioner.rb +0 -93
  147. data/lib/kontena/machine/packet/node_destroyer.rb +0 -42
  148. data/lib/kontena/machine/packet/node_provisioner.rb +0 -77
  149. data/lib/kontena/machine/packet/node_restarter.rb +0 -41
  150. data/lib/kontena/machine/packet/packet_common.rb +0 -89
  151. data/lib/kontena/machine/upcloud.rb +0 -9
  152. data/lib/kontena/machine/upcloud/cloudinit.yml +0 -64
  153. data/lib/kontena/machine/upcloud/cloudinit_master.yml +0 -118
  154. data/lib/kontena/machine/upcloud/master_provisioner.rb +0 -138
  155. data/lib/kontena/machine/upcloud/node_destroyer.rb +0 -85
  156. data/lib/kontena/machine/upcloud/node_provisioner.rb +0 -121
  157. data/lib/kontena/machine/upcloud/node_restarter.rb +0 -49
  158. data/lib/kontena/machine/upcloud/upcloud_common.rb +0 -74
  159. data/lib/kontena/machine/vagrant.rb +0 -12
  160. data/lib/kontena/machine/vagrant/Vagrantfile.master.rb.erb +0 -104
  161. data/lib/kontena/machine/vagrant/Vagrantfile.node.rb.erb +0 -32
  162. data/lib/kontena/machine/vagrant/cloudinit.yml +0 -73
  163. data/lib/kontena/machine/vagrant/master_destroyer.rb +0 -37
  164. data/lib/kontena/machine/vagrant/master_provisioner.rb +0 -79
  165. data/lib/kontena/machine/vagrant/node_destroyer.rb +0 -40
  166. data/lib/kontena/machine/vagrant/node_provisioner.rb +0 -68
@@ -1,17 +0,0 @@
1
- module Kontena::Cli::Nodes::Vagrant
2
- class TerminateCommand < Clamp::Command
3
- include Kontena::Cli::Common
4
- include Kontena::Cli::GridOptions
5
-
6
- parameter "NAME", "Node name"
7
-
8
- def execute
9
- require_api_url
10
- require_current_grid
11
-
12
- require 'kontena/machine/vagrant'
13
- destroyer = Kontena::Machine::Vagrant::NodeDestroyer.new(client(require_token))
14
- destroyer.run!(current_grid, name)
15
- end
16
- end
17
- end
@@ -1,21 +0,0 @@
1
- require_relative 'vagrant/create_command'
2
- require_relative 'vagrant/start_command'
3
- require_relative 'vagrant/stop_command'
4
- require_relative 'vagrant/restart_command'
5
- require_relative 'vagrant/terminate_command'
6
- require_relative 'vagrant/ssh_command'
7
-
8
- module Kontena::Cli::Nodes
9
- class VagrantCommand < Clamp::Command
10
-
11
- subcommand "create", "Create a new Vagrant node", Vagrant::CreateCommand
12
- subcommand "ssh", "SSH into Vagrant node", Vagrant::SshCommand
13
- subcommand "start", "Start Vagrant node", Vagrant::StartCommand
14
- subcommand "stop", "Stop Vagrant node", Vagrant::StopCommand
15
- subcommand "restart", "Restart Vagrant node", Vagrant::RestartCommand
16
- subcommand "terminate", "Terminate Vagrant node", Vagrant::TerminateCommand
17
-
18
- def execute
19
- end
20
- end
21
- end
@@ -1,13 +0,0 @@
1
- begin
2
- require "aws-sdk"
3
- rescue LoadError
4
- puts "It seems that you don't have gem for AWS API installed."
5
- puts "Install it using: gem install aws-sdk"
6
- exit 1
7
- end
8
-
9
- require_relative 'random_name'
10
- require_relative 'cert_helper'
11
- require_relative 'aws/master_provisioner'
12
- require_relative 'aws/node_provisioner'
13
- require_relative 'aws/node_destroyer'
@@ -1,71 +0,0 @@
1
- #cloud-config
2
- hostname: <%= name %>
3
- write_files:
4
- - path: /etc/kontena-agent.env
5
- permissions: 0600
6
- owner: root
7
- content: |
8
- KONTENA_URI="<%= master_uri %>"
9
- KONTENA_TOKEN="<%= grid_token %>"
10
- KONTENA_PEER_INTERFACE=eth1
11
- KONTENA_VERSION=<%= version %>
12
- - path: /etc/systemd/system/docker.service.d/50-kontena.conf
13
- content: |
14
- [Service]
15
- Environment='DOCKER_OPTS=--insecure-registry="10.81.0.0/19" --bip="172.17.43.1/16"'
16
- - path: /etc/sysctl.d/99-inotify.conf
17
- owner: root
18
- permissions: 0644
19
- content: |
20
- fs.inotify.max_user_instances = 8192
21
- coreos:
22
- units:
23
- - name: 00-eth.network
24
- runtime: true
25
- content: |
26
- [Match]
27
- Name=eth*
28
- [Network]
29
- DHCP=yes
30
- DNS=172.17.43.1
31
- DNS=<%= dns_server %>
32
- DOMAINS=kontena.local
33
- [DHCP]
34
- UseDNS=false
35
-
36
- - name: 10-weave.network
37
- runtime: false
38
- content: |
39
- [Match]
40
- Type=bridge
41
- Name=weave*
42
-
43
- [Network]
44
- - name: kontena-agent.service
45
- command: start
46
- enable: true
47
- content: |
48
- [Unit]
49
- Description=kontena-agent
50
- After=network-online.target
51
- After=docker.service
52
- Description=Kontena Agent
53
- Documentation=http://www.kontena.io/
54
- Requires=network-online.target
55
- Requires=docker.service
56
-
57
- [Service]
58
- Restart=always
59
- RestartSec=5
60
- EnvironmentFile=/etc/kontena-agent.env
61
- ExecStartPre=-/usr/bin/docker stop kontena-agent
62
- ExecStartPre=-/usr/bin/docker rm kontena-agent
63
- ExecStartPre=/usr/bin/docker pull kontena/agent:${KONTENA_VERSION}
64
- ExecStart=/usr/bin/docker run --name kontena-agent \
65
- -e KONTENA_URI=${KONTENA_URI} \
66
- -e KONTENA_TOKEN=${KONTENA_TOKEN} \
67
- -e KONTENA_PEER_INTERFACE=${KONTENA_PEER_INTERFACE} \
68
- -v=/var/run/docker.sock:/var/run/docker.sock \
69
- -v=/etc/kontena-agent.env:/etc/kontena.env \
70
- --net=host \
71
- kontena/agent:${KONTENA_VERSION}
@@ -1,118 +0,0 @@
1
- #cloud-config
2
- write_files:
3
- - path: /etc/kontena-server.env
4
- permissions: 0600
5
- owner: root
6
- content: |
7
- KONTENA_VERSION=<%= version %>
8
- KONTENA_VAULT_KEY=<%= vault_secret %>
9
- KONTENA_VAULT_IV=<%= vault_iv %>
10
- <% if ssl_cert %>SSL_CERT="/etc/kontena-server.pem"
11
-
12
- - path: /etc/kontena-server.pem
13
- permissions: 0600
14
- owner: root
15
- content: | <% ssl_cert.split(/\n/).each do |row| %>
16
- <%= row %><% end %><% end %>
17
- - path: /opt/bin/kontena-haproxy.sh
18
- permissions: 0755
19
- owner: root
20
- content: |
21
- #!/bin/sh
22
- if [ -n "$SSL_CERT" ]; then
23
- SSL_CERT=$(awk 1 ORS='\\n' $SSL_CERT)
24
- else
25
- SSL_CERT="**None**"
26
- fi
27
- /usr/bin/docker run --name=kontena-server-haproxy \
28
- --link kontena-server-api:kontena-server-api \
29
- -e SSL_CERT="$SSL_CERT" \
30
- -p 80:80 -p 443:443 kontena/haproxy:latest
31
- coreos:
32
- units:
33
- <% unless mongodb_uri -%>
34
- - name: kontena-server-mongo.service
35
- command: start
36
- enable: true
37
- content: |
38
- [Unit]
39
- Description=kontena-server-mongo
40
- After=network-online.target
41
- After=docker.service
42
- Description=Kontena Server MongoDB
43
- Documentation=http://www.mongodb.org/
44
- Requires=network-online.target
45
- Requires=docker.service
46
-
47
- [Service]
48
- Restart=always
49
- RestartSec=5
50
- ExecStartPre=/usr/bin/docker pull mongo:3.0
51
- ExecStartPre=-/usr/bin/docker create --name=kontena-server-mongo-data mongo:3.0
52
- ExecStartPre=-/usr/bin/docker stop kontena-server-mongo
53
- ExecStartPre=-/usr/bin/docker rm kontena-server-mongo
54
- ExecStart=/usr/bin/docker run --name=kontena-server-mongo \
55
- --volumes-from=kontena-server-mongo-data \
56
- mongo:3.0 mongod --smallfiles
57
- <% end -%>
58
- - name: kontena-server-api.service
59
- command: start
60
- enable: true
61
- content: |
62
- [Unit]
63
- Description=kontena-server-api
64
- After=network-online.target
65
- After=docker.service
66
- After=kontena-server-mongo.service
67
- Description=Kontena Master
68
- Documentation=http://www.kontena.io/
69
- Before=kontena-server-haproxy.service
70
- Wants=kontena-server-haproxy.service
71
- Requires=network-online.target
72
- Requires=docker.service
73
- <% unless mongodb_uri -%>
74
- Requires=kontena-server-mongo.service
75
- <% end %>
76
-
77
- [Service]
78
- Restart=always
79
- RestartSec=5
80
- EnvironmentFile=/etc/kontena-server.env
81
- ExecStartPre=-/usr/bin/docker stop kontena-server-api
82
- ExecStartPre=-/usr/bin/docker rm kontena-server-api
83
- ExecStartPre=/usr/bin/docker pull kontena/server:${KONTENA_VERSION}
84
- ExecStart=/usr/bin/docker run --name kontena-server-api \
85
- <% if mongodb_uri -%>
86
- -e MONGODB_URI=<%= mongodb_uri %> \
87
- <% else -%>
88
- --link kontena-server-mongo:mongodb \
89
- -e MONGODB_URI=mongodb://mongodb:27017/kontena_server \
90
- <% end -%>
91
- <% if auth_server %>
92
- -e AUTH_API_URL=<%= auth_server %> \
93
- <% end -%>
94
- -e VAULT_KEY=${KONTENA_VAULT_KEY} -e VAULT_IV=${KONTENA_VAULT_IV} \
95
- kontena/server:${KONTENA_VERSION}
96
-
97
- - name: kontena-server-haproxy.service
98
- command: start
99
- enable: true
100
- content: |
101
- [Unit]
102
- Description=kontena-server-haproxy
103
- After=network-online.target
104
- After=docker.service
105
- Description=Kontena Server HAProxy
106
- Documentation=http://www.kontena.io/
107
- Requires=network-online.target
108
- Requires=docker.service
109
- Requires=kontena-server-api.service
110
-
111
- [Service]
112
- Restart=always
113
- RestartSec=5
114
- EnvironmentFile=/etc/kontena-server.env
115
- ExecStartPre=-/usr/bin/docker stop kontena-server-haproxy
116
- ExecStartPre=-/usr/bin/docker rm kontena-server-haproxy
117
- ExecStartPre=/usr/bin/docker pull kontena/haproxy:latest
118
- ExecStart=/opt/bin/kontena-haproxy.sh
@@ -1,58 +0,0 @@
1
- module Kontena
2
- module Machine
3
- module Aws
4
- module Common
5
-
6
- # @param [String] region
7
- # @return String
8
- def resolve_ami(region)
9
- response = Excon.get("https://coreos.com/dist/aws/aws-stable.json")
10
- images = JSON.parse(response.body)
11
- info = images[region]
12
- if info
13
- info['hvm']
14
- else
15
- nil
16
- end
17
- end
18
-
19
- # @param [String] vpc_id
20
- # @param [String] zone
21
- # @return [Aws::EC2::Types::Subnet, NilClass]
22
- def default_subnet(vpc_id, zone)
23
- ec2.subnets({
24
- filters: [
25
- {name: "vpc-id", values: [vpc_id]},
26
- {name: "availability-zone", values: [zone]}
27
- ]
28
- }).first
29
- end
30
-
31
- # @return [Aws::EC2::Types::Vpc, NilClass]
32
- def default_vpc
33
- ec2.vpcs({filters: [{name: "is-default", values: ["true"]}]}).first
34
- end
35
-
36
-
37
-
38
- ##
39
- # Resolves givne list of group names into group ids
40
- # @param [String] comma separated list of group names
41
- # @return [Array]
42
- def resolve_security_groups_to_ids(group_list, vpc_id)
43
- ids = group_list.split(',').map { |group|
44
- sg = ec2.security_groups({
45
- filters: [
46
- {name: 'group-name', values: [group]},
47
- {name: 'vpc-id', values: [vpc_id]}
48
- ]
49
- }).first
50
-
51
- sg ? sg.group_id : nil
52
- }
53
- ids.compact
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,187 +0,0 @@
1
- require 'fileutils'
2
- require 'erb'
3
- require 'open3'
4
- require 'shell-spinner'
5
- require_relative 'common'
6
-
7
- module Kontena
8
- module Machine
9
- module Aws
10
- class MasterProvisioner
11
- include RandomName
12
- include Common
13
- include Machine::CertHelper
14
- attr_reader :ec2, :http_client, :region
15
-
16
- # @param [String] access_key_id aws_access_key_id
17
- # @param [String] secret_key aws_secret_access_key
18
- # @param [String] region
19
- def initialize(access_key_id, secret_key, region)
20
- @ec2 = ::Aws::EC2::Resource.new(
21
- region: region, credentials: ::Aws::Credentials.new(access_key_id, secret_key)
22
- )
23
- end
24
-
25
- # @param [Hash] opts
26
- def run!(opts)
27
- ssl_cert = nil
28
- if opts[:ssl_cert]
29
- abort('Invalid ssl cert') unless File.exists?(File.expand_path(opts[:ssl_cert]))
30
- ssl_cert = File.read(File.expand_path(opts[:ssl_cert]))
31
- else
32
- ShellSpinner "Generating self-signed SSL certificate" do
33
- ssl_cert = generate_self_signed_cert
34
- end
35
- end
36
-
37
- ami = resolve_ami(region)
38
- abort('No valid AMI found for region') unless ami
39
- opts[:vpc] = default_vpc.vpc_id unless opts[:vpc]
40
- if opts[:subnet].nil?
41
- subnet = default_subnet(opts[:vpc], region+opts[:zone])
42
- else
43
- subnet = ec2.subnet(opts[:subnet])
44
- end
45
- abort('Failed to find subnet!') unless subnet
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
- security_groups = opts[:security_groups] ?
56
- resolve_security_groups_to_ids(opts[:security_groups], opts[:vpc]) :
57
- ensure_security_group(opts[:vpc])
58
-
59
- name = generate_name
60
- ec2_instance = ec2.create_instances({
61
- image_id: ami,
62
- min_count: 1,
63
- max_count: 1,
64
- instance_type: opts[:type],
65
- key_name: opts[:key_pair],
66
- user_data: Base64.encode64(user_data(userdata_vars)),
67
- block_device_mappings: [
68
- {
69
- device_name: '/dev/xvda',
70
- virtual_name: 'Root',
71
- ebs: {
72
- volume_size: opts[:storage],
73
- volume_type: 'gp2'
74
- }
75
- }
76
- ],
77
- network_interfaces: [
78
- {
79
- device_index: 0,
80
- subnet_id: subnet.subnet_id,
81
- groups: security_groups,
82
- associate_public_ip_address: opts[:associate_public_ip],
83
- delete_on_termination: true
84
- }
85
- ]
86
- }).first
87
- ec2_instance.create_tags({
88
- tags: [
89
- {key: 'Name', value: name}
90
- ]
91
- })
92
-
93
- ShellSpinner "Creating AWS instance #{name.colorize(:cyan)} " do
94
- sleep 5 until ec2_instance.reload.state.name == 'running'
95
- end
96
- public_ip = ec2_instance.reload.public_ip_address
97
- if public_ip.nil?
98
- master_url = "https://#{ec2_instance.private_ip_address}"
99
- puts "Could not get public IP for the created master, private connect url is: #{master_url}"
100
- else
101
- master_url = "https://#{ec2_instance.public_ip_address}"
102
- Excon.defaults[:ssl_verify_peer] = false
103
- http_client = Excon.new(master_url, :connect_timeout => 10)
104
- ShellSpinner "Waiting for #{name.colorize(:cyan)} to start " do
105
- sleep 5 until master_running?(http_client)
106
- end
107
- end
108
-
109
- puts "Kontena Master is now running at #{master_url}"
110
- puts "Use #{"kontena login --name=#{name.sub('kontena-master-', '')} #{master_url}".colorize(:light_black)} to complete Kontena Master setup"
111
- end
112
-
113
- ##
114
- # @param [String] vpc_id
115
- # @return [Array] Security group id in array
116
- def ensure_security_group(vpc_id)
117
- group_name = "kontena_master"
118
- group_id = resolve_security_groups_to_ids(group_name, vpc_id)
119
-
120
- if group_id.empty?
121
- ShellSpinner "Creating AWS security group" do
122
- sg = create_security_group(group_name, vpc_id)
123
- group_id = [sg.group_id]
124
- end
125
- end
126
- group_id
127
- end
128
-
129
- ##
130
- # creates security_group and authorizes default port ranges
131
- #
132
- # @param [String] name
133
- # @param [String, NilClass] vpc_id
134
- # @return Aws::EC2::SecurityGroup
135
- def create_security_group(name, vpc_id = nil)
136
- sg = ec2.create_security_group({
137
- group_name: name,
138
- description: "Kontena Master",
139
- vpc_id: vpc_id
140
- })
141
-
142
- sg.authorize_ingress({
143
- ip_protocol: 'tcp',
144
- from_port: 443,
145
- to_port: 443,
146
- cidr_ip: '0.0.0.0/0'
147
- })
148
-
149
- sg.authorize_ingress({
150
- ip_protocol: 'tcp',
151
- from_port: 22,
152
- to_port: 22,
153
- cidr_ip: '0.0.0.0/0'
154
- })
155
-
156
- sg
157
- end
158
-
159
- # @return [String]
160
- def region
161
- ec2.client.config.region
162
- end
163
-
164
- def user_data(vars)
165
- cloudinit_template = File.join(__dir__ , '/cloudinit_master.yml')
166
- erb(File.read(cloudinit_template), vars)
167
- end
168
-
169
- def generate_name
170
- "kontena-master-#{super}-#{rand(1..9)}"
171
- end
172
-
173
- def master_running?(http_client)
174
- http_client.get(path: '/').status == 200
175
- rescue
176
- false
177
- end
178
-
179
- def erb(template, vars)
180
- ERB.new(template, nil, '%<>-').result(
181
- OpenStruct.new(vars).instance_eval { binding }
182
- )
183
- end
184
- end
185
- end
186
- end
187
- end