kontena-plugin-digitalocean 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzdkYWU0MjcyODUzNjAzM2NmODY5ZTVjMjRkMDI5NzQ3NzliZWEyZg==
5
+ data.tar.gz: !binary |-
6
+ ZGM5ODU3ZjQyNjgzNzMyM2Y0ZTEwM2M5YWMwNDc5MGMwZGY2YjA1Zg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZDNmNmJhMmFkOWEwYTdkZjY5OGI4NTlhODJiZGI2MzI4Yzc4NTM0MDhkM2Uw
10
+ MmRmMTlhODQ5YTAyYzI2YzJiMjMzOGY3YjNhMDdhNGQyZGZlMzVlMDliYWEy
11
+ MjhiMWM1YTNhMGZkZjlmNWM0ZDE1M2NmOTdjMjg4ZDVmNWUyZmM=
12
+ data.tar.gz: !binary |-
13
+ NDA1N2JlYzU4YzY4ZmM0NjlhYWZlYzlhMGMxYTI0ZDI1Yjg0NjEyMjBjN2M0
14
+ YmUzY2NmZmRiNTE0OGMyZTg0YWMwMmEyOGZiYWI1ZDNmNjE0ZmEyMDUwMTRh
15
+ MmM2ZTE2YzM4Y2QzNzIwYTFkN2NiYjgwNDViM2IzZmRmMjY1ZjI=
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.gitmodules ADDED
File without changes
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0-p648
4
+ - 2.1.8
5
+ - 2.2.3
6
+ - 2.3.1
7
+ env:
8
+ - secure: "HAJRG+iaNDuyOMlbYSsXNjA2jwTMQonYJ3fHhm4zDPmIMupNnM+Krx650rVqlMP2KpNIYg4BEWCaDobTU9HoOy0AApmPGc8gGHMCP6v3cSnoGHxmiFy8yB7szgltcBz27PLeJN+exxRCjWPk677jW7JaooyYVSD64dEgVUrFju7olWUrvR0a0hAfBfawXU6kiGgdLnIEtc4AK+mokN9J6fR2PJthuDirZNgDPtVPuBJNFErI9Rh200stcZDOE3F1QE80HyAbrsyfvN/9cosSYDhJS9G+awAw6S+BqeBFZTQlDkWqJgdGsaacasxAmB5bgpkFY5tuv8DdqksK4nXlvsGVurQOYyufuc0y7HllPFVBPkzETCyWaafzek9acfTriLKJIJ26sFYQpuGQlgmAT/KyKYqqzX1ZAk/qe+haT5dt19bXzCHQHWapnWIll11AU0F67TiastmfyaBhnO8d983rwyxIww+8n3AfGPJXZ2eEjw2Rr17yqPYIVtkRoy7ctvCVJTeC6nDGhCqgU5lfl59656fzmJaBI9nWkadK0hQY8WB/5sLJ72pnRZDBbv3xIddS+JnDqxQWheMaJ7Oz5MkbHzdmRo3EhuHGPYpsdPQoeMudqGXX6OTxkQcRbKqJOHDE58rQzz7cZPvY2fwYOkhCspjLWcUAytbaMDkrEVs="
9
+ cache: bundler
10
+ script: bundle install && bundle exec rspec spec/
11
+ deploy:
12
+ provider: rubygems
13
+ api_key: $GEM_TOKEN
14
+ gem: kontena-plugin-digitalocean
15
+ on:
16
+ tags: true
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kontena-plugin-aws.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rspec'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2015 Kontena, Inc.
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ http://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Kontena DigitalOcean Plugin
2
+
3
+ This plugin provides support for DigitalOcean provisioning to Kontena CLI.
4
+
5
+
6
+ ## Contributing
7
+
8
+ 1. Fork it ( https://github.com/kontena/kontena-plugin-digitalocean )
9
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
10
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
11
+ 4. Push to the branch (`git push origin my-new-feature`)
12
+ 5. Create a new Pull Request
13
+
14
+ ## License
15
+
16
+ Kontena is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE.txt) for full license text.
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kontena/plugin/digital_ocean'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kontena-plugin-digitalocean"
8
+ spec.version = Kontena::Plugin::DigitalOcean::VERSION
9
+ spec.authors = ["Kontena, Inc."]
10
+ spec.email = ["info@kontena.io"]
11
+
12
+ spec.summary = "Kontena DigitalOcean plugin"
13
+ spec.description = "Kontena DigitalOcean plugin"
14
+ spec.homepage = "https://github.com/kontena/kontena-plugin-digitalocean"
15
+ spec.license = "Apache-2.0"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency 'kontena-cli', '>= 0.15.0.rc1'
21
+ spec.add_runtime_dependency 'droplet_kit', '~> 1.4', '>= 1.4.1'
22
+ spec.add_runtime_dependency 'activesupport', '~> 4.0'
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,6 @@
1
+ require 'droplet_kit'
2
+ require 'kontena/machine/random_name'
3
+ require 'kontena/machine/cert_helper'
4
+ require_relative 'digital_ocean/node_provisioner'
5
+ require_relative 'digital_ocean/node_destroyer'
6
+ require_relative 'digital_ocean/master_provisioner'
@@ -0,0 +1,65 @@
1
+ #cloud-config
2
+ write_files:
3
+ - path: /etc/kontena-agent.env
4
+ permissions: 0600
5
+ owner: root
6
+ content: |
7
+ KONTENA_URI="<%= master_uri %>"
8
+ KONTENA_TOKEN="<%= grid_token %>"
9
+ KONTENA_PEER_INTERFACE=eth1
10
+ KONTENA_VERSION=<%= version %>
11
+ - path: /etc/systemd/system/docker.service.d/50-kontena.conf
12
+ content: |
13
+ [Service]
14
+ Environment='DOCKER_OPTS=--insecure-registry="10.81.0.0/19" --bip="172.17.43.1/16"'
15
+ Environment='DOCKER_CGROUPS=--exec-opt native.cgroupdriver=cgroupfs'
16
+ - path: /etc/sysctl.d/99-inotify.conf
17
+ owner: root
18
+ permissions: 0644
19
+ content: |
20
+ fs.inotify.max_user_instances = 8192
21
+ - path: /etc/resolv.conf
22
+ permissions: 0644
23
+ owner: root
24
+ content: |
25
+ nameserver 172.17.43.1
26
+ nameserver 8.8.8.8
27
+ nameserver 8.8.4.4
28
+ coreos:
29
+ units:
30
+ - name: 10-weave.network
31
+ runtime: false
32
+ content: |
33
+ [Match]
34
+ Type=bridge
35
+ Name=weave*
36
+
37
+ [Network]
38
+ - name: kontena-agent.service
39
+ command: start
40
+ enable: true
41
+ content: |
42
+ [Unit]
43
+ Description=kontena-agent
44
+ After=network-online.target
45
+ After=docker.service
46
+ Description=Kontena Agent
47
+ Documentation=http://www.kontena.io/
48
+ Requires=network-online.target
49
+ Requires=docker.service
50
+
51
+ [Service]
52
+ Restart=always
53
+ RestartSec=5
54
+ EnvironmentFile=/etc/kontena-agent.env
55
+ ExecStartPre=-/usr/bin/docker stop kontena-agent
56
+ ExecStartPre=-/usr/bin/docker rm kontena-agent
57
+ ExecStartPre=/usr/bin/docker pull kontena/agent:${KONTENA_VERSION}
58
+ ExecStart=/usr/bin/docker run --name kontena-agent \
59
+ -e KONTENA_URI=${KONTENA_URI} \
60
+ -e KONTENA_TOKEN=${KONTENA_TOKEN} \
61
+ -e KONTENA_PEER_INTERFACE=${KONTENA_PEER_INTERFACE} \
62
+ -v=/var/run/docker.sock:/var/run/docker.sock \
63
+ -v=/etc/kontena-agent.env:/etc/kontena.env \
64
+ --net=host \
65
+ kontena/agent:${KONTENA_VERSION}
@@ -0,0 +1,120 @@
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
+ EnvironmentFile=-/etc/kontena-server.custom.env
82
+ ExecStartPre=-/usr/bin/docker stop kontena-server-api
83
+ ExecStartPre=-/usr/bin/docker rm kontena-server-api
84
+ ExecStartPre=-/usr/bin/docker pull kontena/server:${KONTENA_VERSION}
85
+ ExecStart=/usr/bin/docker run --name kontena-server-api \
86
+ <% if mongodb_uri -%>
87
+ -e MONGODB_URI=<%= mongodb_uri %> \
88
+ <% else -%>
89
+ --link kontena-server-mongo:mongodb \
90
+ -e MONGODB_URI=mongodb://mongodb:27017/kontena_server \
91
+ <% end -%>
92
+ <% if auth_server %>
93
+ -e AUTH_API_URL=<%= auth_server %> \
94
+ <% end -%>
95
+ -e VAULT_KEY=${KONTENA_VAULT_KEY} -e VAULT_IV=${KONTENA_VAULT_IV} \
96
+ kontena/server:${KONTENA_VERSION}
97
+
98
+ - name: kontena-server-haproxy.service
99
+ command: start
100
+ enable: true
101
+ content: |
102
+ [Unit]
103
+ Description=kontena-server-haproxy
104
+ After=network-online.target
105
+ After=docker.service
106
+ Description=Kontena Server HAProxy
107
+ Documentation=http://www.kontena.io/
108
+ Requires=network-online.target
109
+ Requires=docker.service
110
+ Requires=kontena-server-api.service
111
+
112
+ [Service]
113
+ Restart=always
114
+ RestartSec=5
115
+ EnvironmentFile=/etc/kontena-server.env
116
+ EnvironmentFile=-/etc/kontena-server.custom.env
117
+ ExecStartPre=-/usr/bin/docker stop kontena-server-haproxy
118
+ ExecStartPre=-/usr/bin/docker rm kontena-server-haproxy
119
+ ExecStartPre=-/usr/bin/docker pull kontena/haproxy:latest
120
+ ExecStart=/opt/bin/kontena-haproxy.sh
@@ -0,0 +1,99 @@
1
+ require 'fileutils'
2
+ require 'erb'
3
+ require 'open3'
4
+ require 'shell-spinner'
5
+
6
+ module Kontena
7
+ module Machine
8
+ module DigitalOcean
9
+ class MasterProvisioner
10
+ include RandomName
11
+ include Machine::CertHelper
12
+
13
+ attr_reader :client, :http_client
14
+
15
+ # @param [String] token Digital Ocean token
16
+ def initialize(token)
17
+ @client = DropletKit::Client.new(access_token: token)
18
+ end
19
+
20
+ def run!(opts)
21
+ abort('Invalid ssh key') unless File.exists?(File.expand_path(opts[:ssh_key]))
22
+
23
+ ssh_key = ssh_key(File.read(File.expand_path(opts[:ssh_key])).strip)
24
+ abort('Ssh key does not exist in Digital Ocean') unless ssh_key
25
+
26
+ if opts[:ssl_cert]
27
+ abort('Invalid ssl cert') unless File.exists?(File.expand_path(opts[:ssl_cert]))
28
+ ssl_cert = File.read(File.expand_path(opts[:ssl_cert]))
29
+ else
30
+ ShellSpinner "Generating self-signed SSL certificate" do
31
+ ssl_cert = generate_self_signed_cert
32
+ end
33
+ end
34
+
35
+ userdata_vars = {
36
+ ssl_cert: ssl_cert,
37
+ auth_server: opts[:auth_server],
38
+ version: opts[:version],
39
+ vault_secret: opts[:vault_secret],
40
+ vault_iv: opts[:vault_iv],
41
+ mongodb_uri: opts[:mongodb_uri]
42
+ }
43
+
44
+ droplet = DropletKit::Droplet.new(
45
+ name: generate_name,
46
+ region: opts[:region],
47
+ image: 'coreos-stable',
48
+ size: opts[:size],
49
+ private_networking: true,
50
+ user_data: user_data(userdata_vars),
51
+ ssh_keys: [ssh_key.id]
52
+ )
53
+
54
+ ShellSpinner "Creating DigitalOcean droplet #{droplet.name.colorize(:cyan)} " do
55
+ droplet = client.droplets.create(droplet)
56
+ until droplet.status == 'active'
57
+ droplet = client.droplets.find(id: droplet.id)
58
+ sleep 5
59
+ end
60
+ end
61
+
62
+ master_url = "https://#{droplet.public_ip}"
63
+ Excon.defaults[:ssl_verify_peer] = false
64
+ @http_client = Excon.new("#{master_url}", :connect_timeout => 10)
65
+
66
+ ShellSpinner "Waiting for #{droplet.name.colorize(:cyan)} to start" do
67
+ sleep 5 until master_running?
68
+ end
69
+
70
+ puts "Kontena Master is now running at #{master_url}"
71
+ puts "Use #{"kontena login --name=#{droplet.name.sub('kontena-master-', '')} #{master_url}".colorize(:light_black)} to complete Kontena Master setup"
72
+ end
73
+
74
+ def user_data(vars)
75
+ cloudinit_template = File.join(__dir__ , '/cloudinit_master.yml')
76
+ erb(File.read(cloudinit_template), vars)
77
+ end
78
+
79
+ def generate_name
80
+ "kontena-master-#{super}-#{rand(1..9)}"
81
+ end
82
+
83
+ def ssh_key(public_key)
84
+ client.ssh_keys.all.find{|key| key.public_key == public_key}
85
+ end
86
+
87
+ def master_running?
88
+ http_client.get(path: '/').status == 200
89
+ rescue
90
+ false
91
+ end
92
+
93
+ def erb(template, vars)
94
+ ERB.new(template, nil, '%<>-').result(OpenStruct.new(vars).instance_eval { binding })
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,40 @@
1
+ require 'shell-spinner'
2
+
3
+ module Kontena
4
+ module Machine
5
+ module DigitalOcean
6
+ class NodeDestroyer
7
+ include RandomName
8
+
9
+ attr_reader :client, :api_client
10
+
11
+ # @param [Kontena::Client] api_client Kontena api client
12
+ # @param [String] token Digital Ocean token
13
+ def initialize(api_client, token)
14
+ @api_client = api_client
15
+ @client = DropletKit::Client.new(access_token: token)
16
+ end
17
+
18
+ def run!(grid, name)
19
+ droplet = client.droplets.all.find{|d| d.name == name}
20
+ if droplet
21
+ ShellSpinner "Terminating DigitalOcean droplet #{name.colorize(:cyan)} " do
22
+ result = client.droplets.delete(id: droplet.id)
23
+ if result.is_a?(String)
24
+ abort "Cannot delete droplet #{name.colorize(:cyan)} in DigitalOcean"
25
+ end
26
+ end
27
+ else
28
+ abort "Cannot find droplet #{name.colorize(:cyan)} in DigitalOcean"
29
+ end
30
+ node = api_client.get("grids/#{grid['id']}/nodes")['nodes'].find{|n| n['name'] == name}
31
+ if node
32
+ ShellSpinner "Removing node #{name.colorize(:cyan)} from grid #{grid['name'].colorize(:cyan)} " do
33
+ api_client.delete("grids/#{grid['id']}/nodes/#{name}")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,88 @@
1
+ require 'fileutils'
2
+ require 'erb'
3
+ require 'open3'
4
+ require 'shell-spinner'
5
+
6
+ module Kontena
7
+ module Machine
8
+ module DigitalOcean
9
+ class NodeProvisioner
10
+ include RandomName
11
+
12
+ attr_reader :client, :api_client
13
+
14
+ # @param [Kontena::Client] api_client Kontena api client
15
+ # @param [String] token Digital Ocean token
16
+ def initialize(api_client, token)
17
+ @api_client = api_client
18
+ @client = DropletKit::Client.new(access_token: token)
19
+ end
20
+
21
+ def run!(opts)
22
+ abort('Invalid ssh key') unless File.exists?(File.expand_path(opts[:ssh_key]))
23
+
24
+ ssh_key = ssh_key(File.read(File.expand_path(opts[:ssh_key])).strip)
25
+ abort('Ssh key does not exist in Digital Ocean') unless ssh_key
26
+
27
+ userdata_vars = {
28
+ version: opts[:version],
29
+ master_uri: opts[:master_uri],
30
+ grid_token: opts[:grid_token],
31
+ }
32
+
33
+ droplet = DropletKit::Droplet.new(
34
+ name: opts[:name] || generate_name,
35
+ region: opts[:region],
36
+ image: 'coreos-stable',
37
+ size: opts[:size],
38
+ private_networking: true,
39
+ user_data: user_data(userdata_vars),
40
+ ssh_keys: [ssh_key.id]
41
+ )
42
+ created = client.droplets.create(droplet)
43
+ ShellSpinner "Creating DigitalOcean droplet #{droplet.name.colorize(:cyan)} " do
44
+ sleep 5 until client.droplets.find(id: created.id).status == 'active'
45
+ end
46
+ node = nil
47
+ ShellSpinner "Waiting for node #{droplet.name.colorize(:cyan)} join to grid #{opts[:grid].colorize(:cyan)} " do
48
+ sleep 2 until node = droplet_exists_in_grid?(opts[:grid], droplet)
49
+ end
50
+ set_labels(
51
+ node,
52
+ [
53
+ "region=#{opts[:region]}",
54
+ "az=#{opts[:region]}",
55
+ "provider=digitalocean"
56
+ ]
57
+ )
58
+ end
59
+
60
+ def user_data(vars)
61
+ cloudinit_template = File.join(__dir__ , '/cloudinit.yml')
62
+ erb(File.read(cloudinit_template), vars)
63
+ end
64
+
65
+ def generate_name
66
+ "#{super}-#{rand(1..99)}"
67
+ end
68
+
69
+ def ssh_key(public_key)
70
+ ssh_key = client.ssh_keys.all.find{|key| key.public_key == public_key}
71
+ end
72
+
73
+ def droplet_exists_in_grid?(grid, droplet)
74
+ api_client.get("grids/#{grid}/nodes")['nodes'].find{|n| n['name'] == droplet.name}
75
+ end
76
+
77
+ def erb(template, vars)
78
+ ERB.new(template).result(OpenStruct.new(vars).instance_eval { binding })
79
+ end
80
+
81
+ def set_labels(node, labels)
82
+ data = {labels: labels}
83
+ api_client.put("nodes/#{node['id']}", data, {}, {'Kontena-Grid-Token' => node['grid']['token']})
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,7 @@
1
+ module Kontena
2
+ module Plugin
3
+ module DigitalOcean
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ require 'securerandom'
2
+
3
+ module Kontena::Plugin::DigitalOcean::Master
4
+ class CreateCommand < Kontena::Command
5
+ include Kontena::Cli::Common
6
+
7
+ option "--token", "TOKEN", "DigitalOcean API token", required: true
8
+ option "--ssh-key", "SSH_KEY", "Path to ssh public key", default: '~/.ssh/id_rsa.pub'
9
+ option "--ssl-cert", "SSL CERT", "SSL certificate file"
10
+ option "--size", "SIZE", "Droplet size", default: '1gb'
11
+ option "--region", "REGION", "Region", default: 'ams2'
12
+ option "--vault-secret", "VAULT_SECRET", "Secret key for Vault (optional)"
13
+ option "--vault-iv", "VAULT_IV", "Initialization vector for Vault (optional)"
14
+ option "--mongodb-uri", "URI", "External MongoDB uri (optional)"
15
+ option "--version", "VERSION", "Define installed Kontena version", default: 'latest'
16
+ option "--auth-provider-url", "AUTH_PROVIDER_URL", "Define authentication provider url"
17
+
18
+
19
+ def execute
20
+ require 'kontena/machine/digital_ocean'
21
+
22
+ provisioner = provisioner(token)
23
+ provisioner.run!(
24
+ ssh_key: ssh_key,
25
+ ssl_cert: ssl_cert,
26
+ size: size,
27
+ region: region,
28
+ version: version,
29
+ auth_server: auth_provider_url,
30
+ vault_secret: vault_secret || SecureRandom.hex(24),
31
+ vault_iv: vault_iv || SecureRandom.hex(24),
32
+ mongodb_uri: mongodb_uri
33
+ )
34
+ end
35
+
36
+ # @param [String] token
37
+ def provisioner(token)
38
+ Kontena::Machine::DigitalOcean::MasterProvisioner.new(token)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'master/create_command'
2
+
3
+ class Kontena::Plugin::DigitalOcean::MasterCommand < Kontena::Command
4
+
5
+ subcommand "create", "Create a new master to DigitalOcean", Kontena::Plugin::DigitalOcean::Master::CreateCommand
6
+
7
+ def execute
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'nodes/create_command'
2
+ require_relative 'nodes/restart_command'
3
+ require_relative 'nodes/terminate_command'
4
+
5
+ class Kontena::Plugin::DigitalOcean::NodeCommand < Kontena::Command
6
+
7
+ subcommand "create", "Create a new node to DigitalOcean", Kontena::Plugin::DigitalOcean::Nodes::CreateCommand
8
+ subcommand "restart", "Restart DigitalOcean node", Kontena::Plugin::DigitalOcean::Nodes::RestartCommand
9
+ subcommand "terminate", "Terminate DigitalOcean node", Kontena::Plugin::DigitalOcean::Nodes::TerminateCommand
10
+
11
+ def execute
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ module Kontena::Plugin::DigitalOcean::Nodes
2
+ class CreateCommand < Kontena::Command
3
+ include Kontena::Cli::Common
4
+ include Kontena::Cli::GridOptions
5
+
6
+ parameter "[NAME]", "Node name"
7
+ option "--token", "TOKEN", "DigitalOcean API token", required: true
8
+ option "--ssh-key", "SSH_KEY", "Path to ssh public key", default: '~/.ssh/id_rsa.pub'
9
+ option "--size", "SIZE", "Droplet size", default: '1gb'
10
+ option "--region", "REGION", "Region", default: 'ams2'
11
+ option "--version", "VERSION", "Define installed Kontena version", default: 'latest'
12
+
13
+ def execute
14
+ require_api_url
15
+ require_current_grid
16
+
17
+ require 'kontena/machine/digital_ocean'
18
+ grid = fetch_grid
19
+ provisioner = provisioner(client(require_token), token)
20
+ provisioner.run!(
21
+ master_uri: api_url,
22
+ grid_token: grid['token'],
23
+ grid: current_grid,
24
+ ssh_key: ssh_key,
25
+ name: name,
26
+ size: size,
27
+ region: region,
28
+ version: version
29
+ )
30
+ end
31
+
32
+ # @param [Kontena::Client] client
33
+ # @param [String] token
34
+ def provisioner(client, token)
35
+ Kontena::Machine::DigitalOcean::NodeProvisioner.new(client, token)
36
+ end
37
+
38
+ # @return [Hash]
39
+ def fetch_grid
40
+ client(require_token).get("grids/#{current_grid}")
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ module Kontena::Plugin::DigitalOcean::Nodes
2
+ class RestartCommand < Kontena::Command
3
+ include Kontena::Cli::Common
4
+ include Kontena::Cli::GridOptions
5
+
6
+ parameter "NAME", "Node name"
7
+ option "--token", "TOKEN", "DigitalOcean API token", required: true
8
+
9
+ def execute
10
+ require_api_url
11
+ require_current_grid
12
+
13
+ require 'kontena/machine/digital_ocean'
14
+
15
+ client = DropletKit::Client.new(access_token: token)
16
+ droplet = client.droplets.all.find{|d| d.name == name}
17
+ if droplet
18
+ ShellSpinner "Restarting DigitalOcean droplet #{name.colorize(:cyan)} " do
19
+ client.droplet_actions.reboot(droplet_id: droplet.id)
20
+ sleep 5 until client.droplets.find(id: droplet.id).status == 'active'
21
+ end
22
+ else
23
+ abort "Cannot find droplet #{name.colorize(:cyan)} in DigitalOcean"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module Kontena::Plugin::DigitalOcean::Nodes
2
+ class TerminateCommand < Kontena::Command
3
+ include Kontena::Cli::Common
4
+ include Kontena::Cli::GridOptions
5
+
6
+ parameter "NAME", "Node name"
7
+ option "--token", "TOKEN", "DigitalOcean API token", required: true
8
+
9
+ def execute
10
+ require_api_url
11
+ require_current_grid
12
+
13
+ require 'kontena/machine/digital_ocean'
14
+ grid = client(require_token).get("grids/#{current_grid}")
15
+ destroyer = destroyer(client(require_token), token)
16
+ destroyer.run!(grid, name)
17
+ end
18
+
19
+ # @param [Kontena::Client] client
20
+ # @param [String] token
21
+ def destroyer(client, token)
22
+ Kontena::Machine::DigitalOcean::NodeDestroyer.new(client, token)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'digital_ocean/master_command'
2
+ require_relative 'digital_ocean/node_command'
3
+
4
+ class Kontena::Plugin::DigitalOceanCommand < Kontena::Command
5
+
6
+ subcommand 'master', 'DigitalOcean master related commands', Kontena::Plugin::DigitalOcean::MasterCommand
7
+ subcommand 'node', 'DigitalOcean node related commands', Kontena::Plugin::DigitalOcean::NodeCommand
8
+
9
+ def execute
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require 'kontena_cli'
2
+ require_relative 'kontena/plugin/digital_ocean'
3
+ require_relative 'kontena/plugin/digital_ocean_command'
4
+
5
+ Kontena::MainCommand.register("digitalocean", "DigitalOcean specific commands", Kontena::Plugin::DigitalOceanCommand)
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kontena-plugin-digitalocean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kontena, Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kontena-cli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.15.0.rc1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.15.0.rc1
27
+ - !ruby/object:Gem::Dependency
28
+ name: droplet_kit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 1.4.1
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.4'
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.4.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '4.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '4.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: '1.11'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: '1.11'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ description: Kontena DigitalOcean plugin
90
+ email:
91
+ - info@kontena.io
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - .gitignore
97
+ - .gitmodules
98
+ - .rspec
99
+ - .travis.yml
100
+ - Gemfile
101
+ - LICENSE.txt
102
+ - README.md
103
+ - kontena-plugin-digitalocean.gemspec
104
+ - lib/kontena/machine/digital_ocean.rb
105
+ - lib/kontena/machine/digital_ocean/cloudinit.yml
106
+ - lib/kontena/machine/digital_ocean/cloudinit_master.yml
107
+ - lib/kontena/machine/digital_ocean/master_provisioner.rb
108
+ - lib/kontena/machine/digital_ocean/node_destroyer.rb
109
+ - lib/kontena/machine/digital_ocean/node_provisioner.rb
110
+ - lib/kontena/plugin/digital_ocean.rb
111
+ - lib/kontena/plugin/digital_ocean/master/create_command.rb
112
+ - lib/kontena/plugin/digital_ocean/master_command.rb
113
+ - lib/kontena/plugin/digital_ocean/node_command.rb
114
+ - lib/kontena/plugin/digital_ocean/nodes/create_command.rb
115
+ - lib/kontena/plugin/digital_ocean/nodes/restart_command.rb
116
+ - lib/kontena/plugin/digital_ocean/nodes/terminate_command.rb
117
+ - lib/kontena/plugin/digital_ocean_command.rb
118
+ - lib/kontena_cli_plugin.rb
119
+ homepage: https://github.com/kontena/kontena-plugin-digitalocean
120
+ licenses:
121
+ - Apache-2.0
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.4.5
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Kontena DigitalOcean plugin
143
+ test_files: []