linode_cluster 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop_todo.yml +85 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/linode_cluster/client_wrapper.rb +86 -0
- data/lib/linode_cluster/cluster.rb +66 -0
- data/lib/linode_cluster/node.rb +40 -0
- data/lib/linode_cluster/node_factory.rb +85 -0
- data/lib/linode_cluster/node_group.rb +78 -0
- data/lib/linode_cluster/version.rb +3 -0
- data/lib/linode_cluster.rb +9 -0
- data/linode_cluster.gemspec +28 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8d16e05b82b5374305de75916421044f78bcb194
|
4
|
+
data.tar.gz: 051bb20d4eec6914127f7fda25db51a750bbd5cc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8abf69bf934bb425ff48cdb44d85feeb60d64b35c66996484a22beefabbbb471b8f96ade93c4f2f9a606016fbeb03c56076abf2e7bead94512d10f2bdb34c539
|
7
|
+
data.tar.gz: 1ffccd1f6f81c472685abd1ca0a182b8bde55850e5311104432e936e69f6a399c1762275af7721034605458ea1c798da687b9effa9e8894134589297e5d99ea1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-06-12 22:31:54 +0100 using RuboCop version 0.49.1.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
# Cop supports --auto-correct.
|
11
|
+
Layout/EmptyLineAfterMagicComment:
|
12
|
+
Exclude:
|
13
|
+
- 'linode_cluster.gemspec'
|
14
|
+
|
15
|
+
# Offense count: 1
|
16
|
+
# Cop supports --auto-correct.
|
17
|
+
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
|
18
|
+
Layout/ExtraSpacing:
|
19
|
+
Exclude:
|
20
|
+
- 'linode_cluster.gemspec'
|
21
|
+
|
22
|
+
# Offense count: 1
|
23
|
+
# Cop supports --auto-correct.
|
24
|
+
# Configuration parameters: AllowForAlignment.
|
25
|
+
Layout/SpaceAroundOperators:
|
26
|
+
Exclude:
|
27
|
+
- 'linode_cluster.gemspec'
|
28
|
+
|
29
|
+
# Offense count: 3
|
30
|
+
Metrics/AbcSize:
|
31
|
+
Max: 51
|
32
|
+
|
33
|
+
# Offense count: 1
|
34
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
35
|
+
Metrics/BlockLength:
|
36
|
+
Max: 59
|
37
|
+
|
38
|
+
# Offense count: 11
|
39
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
40
|
+
# URISchemes: http, https
|
41
|
+
Metrics/LineLength:
|
42
|
+
Max: 112
|
43
|
+
|
44
|
+
# Offense count: 2
|
45
|
+
# Configuration parameters: CountComments.
|
46
|
+
Metrics/MethodLength:
|
47
|
+
Max: 35
|
48
|
+
|
49
|
+
# Offense count: 1
|
50
|
+
Style/Documentation:
|
51
|
+
Exclude:
|
52
|
+
- 'spec/**/*'
|
53
|
+
- 'test/**/*'
|
54
|
+
- 'lib/linode_cluster.rb'
|
55
|
+
|
56
|
+
# Offense count: 1
|
57
|
+
# Cop supports --auto-correct.
|
58
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
|
59
|
+
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
|
60
|
+
Style/HashSyntax:
|
61
|
+
Exclude:
|
62
|
+
- 'Rakefile'
|
63
|
+
|
64
|
+
# Offense count: 1
|
65
|
+
# Cop supports --auto-correct.
|
66
|
+
# Configuration parameters: PreferredDelimiters.
|
67
|
+
Style/PercentLiteralDelimiters:
|
68
|
+
Exclude:
|
69
|
+
- 'linode_cluster.gemspec'
|
70
|
+
|
71
|
+
# Offense count: 19
|
72
|
+
# Cop supports --auto-correct.
|
73
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
|
74
|
+
# SupportedStyles: single_quotes, double_quotes
|
75
|
+
Style/StringLiterals:
|
76
|
+
Exclude:
|
77
|
+
- 'Rakefile'
|
78
|
+
- 'bin/console'
|
79
|
+
- 'linode_cluster.gemspec'
|
80
|
+
|
81
|
+
# Offense count: 1
|
82
|
+
# Cop supports --auto-correct.
|
83
|
+
Style/UnneededPercentQ:
|
84
|
+
Exclude:
|
85
|
+
- 'linode_cluster.gemspec'
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at r.stranders@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Ruben Stranders
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# LinodeCluster
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/linode_cluster`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'linode_cluster'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install linode_cluster
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/linode_cluster. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "linode_cluster"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
module LinodeCluster
|
2
|
+
# Client Wrapper class
|
3
|
+
class ClientWrapper
|
4
|
+
attr_accessor :client
|
5
|
+
|
6
|
+
def initialize(client)
|
7
|
+
@client = client
|
8
|
+
end
|
9
|
+
|
10
|
+
def linode
|
11
|
+
client.linode
|
12
|
+
end
|
13
|
+
|
14
|
+
def nodes
|
15
|
+
@nodes ||= begin
|
16
|
+
ip_addresses = client.linode.ip.list
|
17
|
+
linodes = client.linode.list
|
18
|
+
|
19
|
+
linodes.map do |l|
|
20
|
+
node = Node.new(l, self)
|
21
|
+
node.ip_address = ip_addresses.find { |i| i.linodeid == node.linodeid }.ipaddress
|
22
|
+
node
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_node_by_name(name)
|
28
|
+
node_by_name = nodes.select { |d| d.label == name }
|
29
|
+
raise "Found multiple nodes with name #{name}" if node_by_name.count > 1
|
30
|
+
node_by_name.first
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_node_by_id(id)
|
34
|
+
node_by_id = nodes.select { |d| d.linodeid == id }
|
35
|
+
raise "Found multiple nodes with id #{id}" if node_by_id.count > 1
|
36
|
+
node_by_id.first
|
37
|
+
end
|
38
|
+
|
39
|
+
def refresh_nodes
|
40
|
+
@nodes = nil
|
41
|
+
nodes
|
42
|
+
end
|
43
|
+
|
44
|
+
def find_datacenter_id(region)
|
45
|
+
datacenters.find { |d| d.abbr == region }.datacenterid
|
46
|
+
end
|
47
|
+
|
48
|
+
def find_datacenter_by_id(id)
|
49
|
+
datacenters.find { |d| d.datacenterid == id }
|
50
|
+
end
|
51
|
+
|
52
|
+
def find_plan_id(size)
|
53
|
+
plans.find { |p| p.ram.to_s == size.to_s }.planid
|
54
|
+
end
|
55
|
+
|
56
|
+
def find_plan_by_id(id)
|
57
|
+
plans.find { |p| p.planid == id }
|
58
|
+
end
|
59
|
+
|
60
|
+
def find_distribution_id_by_name(name)
|
61
|
+
distributions.find { |p| p.label == name }.distributionid
|
62
|
+
end
|
63
|
+
|
64
|
+
def find_default_kernel
|
65
|
+
kernels.find { |k| k.label.include?('Latest 64') }
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def datacenters
|
71
|
+
@datacenters ||= client.avail.datacenters
|
72
|
+
end
|
73
|
+
|
74
|
+
def plans
|
75
|
+
@plans ||= client.avail.linodeplans
|
76
|
+
end
|
77
|
+
|
78
|
+
def distributions
|
79
|
+
@distributions ||= client.avail.distributions
|
80
|
+
end
|
81
|
+
|
82
|
+
def kernels
|
83
|
+
@kernels ||= client.avail.kernels
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'linode_cluster/node'
|
2
|
+
require 'linode_cluster/node_group'
|
3
|
+
require 'linode_cluster/client_wrapper'
|
4
|
+
require 'linode'
|
5
|
+
|
6
|
+
module LinodeCluster
|
7
|
+
# Cluster class
|
8
|
+
class Cluster
|
9
|
+
attr_accessor :client, :ansible_ssh_user, :app_name, :stage
|
10
|
+
|
11
|
+
def initialize(api_key, app_name, stage, options)
|
12
|
+
@node_groups = {}
|
13
|
+
@app_name = app_name || ''
|
14
|
+
@stage = stage || ''
|
15
|
+
@client = ClientWrapper.new(Linode.new(api_key: api_key))
|
16
|
+
@ansible_ssh_user = options[:ansible_ssh_user] || 'root'
|
17
|
+
|
18
|
+
raise 'app name cannot be blank' if app_name.empty?
|
19
|
+
raise 'stage cannot be blank' if stage.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_node_group(name, region, size, count, options = {})
|
23
|
+
raise "Group with name '#{name}' already exists" if @node_groups[name]
|
24
|
+
@node_groups[name] = NodeGroup.new(name, name_prefix, region, size, count, self, default_options(options))
|
25
|
+
end
|
26
|
+
|
27
|
+
def create!
|
28
|
+
@node_groups.each_value(&:create!)
|
29
|
+
end
|
30
|
+
|
31
|
+
def name_prefix
|
32
|
+
"#{app_name}-#{stage}-"
|
33
|
+
end
|
34
|
+
|
35
|
+
def as_ansible_inventory
|
36
|
+
@node_groups.map { |_, group| group.as_ansible_inventory }.join
|
37
|
+
end
|
38
|
+
|
39
|
+
def nodes
|
40
|
+
@node_groups.values.flat_map(&:nodes)
|
41
|
+
end
|
42
|
+
|
43
|
+
def cost_per_month
|
44
|
+
nodes.map(&:cost_per_month).sum
|
45
|
+
end
|
46
|
+
|
47
|
+
def create_node(attributes)
|
48
|
+
result = node_factory.create(attributes)
|
49
|
+
@nodes = nil
|
50
|
+
result
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def node_factory
|
56
|
+
NodeFactory.new(client)
|
57
|
+
end
|
58
|
+
|
59
|
+
def default_options(options)
|
60
|
+
actual_options = options.dup
|
61
|
+
actual_options[:ansible_ssh_user] ||= ansible_ssh_user
|
62
|
+
actual_options[:ansible_ssh_host] ||= '{{ ip_address }}'
|
63
|
+
actual_options
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module LinodeCluster
|
2
|
+
# Node class
|
3
|
+
class Node < Delegator
|
4
|
+
attr_accessor :ip_address, :client
|
5
|
+
|
6
|
+
def initialize(obj, client)
|
7
|
+
super obj # pass obj to Delegator constructor, required
|
8
|
+
@delegate_sd_obj = obj # store obj for future use
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def __getobj__
|
13
|
+
@delegate_sd_obj # return object we are delegating to, required
|
14
|
+
end
|
15
|
+
|
16
|
+
def __setobj__(obj)
|
17
|
+
@delegate_sd_obj = obj
|
18
|
+
end
|
19
|
+
|
20
|
+
def region
|
21
|
+
client.find_datacenter_by_id(datacenterid).abbr
|
22
|
+
end
|
23
|
+
|
24
|
+
def size
|
25
|
+
plan.ram
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
label
|
30
|
+
end
|
31
|
+
|
32
|
+
def plan
|
33
|
+
client.find_plan_by_id(planid)
|
34
|
+
end
|
35
|
+
|
36
|
+
def cost_per_month
|
37
|
+
plan.price
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LinodeCluster
|
4
|
+
# node factory class
|
5
|
+
class NodeFactory
|
6
|
+
# DEFAULT_IMAGE_NAME = 'Ubuntu 16.04 LTS'.freeze
|
7
|
+
DEFAULT_IMAGE_NAME = 'Ubuntu 14.04 LTS'.freeze
|
8
|
+
|
9
|
+
attr_accessor :client
|
10
|
+
|
11
|
+
def initialize(client)
|
12
|
+
@client = client
|
13
|
+
end
|
14
|
+
|
15
|
+
def find_datacenter_id
|
16
|
+
client.find_datacenter_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_plan_id
|
20
|
+
client.find_plan_id
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_node_by_id
|
24
|
+
client.find_node_by_id
|
25
|
+
end
|
26
|
+
|
27
|
+
def refresh_nodes
|
28
|
+
client.refresh_nodes
|
29
|
+
end
|
30
|
+
|
31
|
+
def find_distribution_id_by_name
|
32
|
+
client.find_distribution_id_by_name
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_default_kernel
|
36
|
+
client.find_default_kernel
|
37
|
+
end
|
38
|
+
|
39
|
+
def create(attributes)
|
40
|
+
linode_attributes = { datacenterid: find_datacenter_id(attributes[:region]),
|
41
|
+
planid: find_plan_id(attributes[:size]) }
|
42
|
+
|
43
|
+
result = client.linode.create(linode_attributes)
|
44
|
+
|
45
|
+
begin
|
46
|
+
client.linode.update(linodeid: result.linodeid,
|
47
|
+
label: attributes[:name],
|
48
|
+
lpm_displaygroup: attributes[:group_name])
|
49
|
+
|
50
|
+
swap_disk = client.linode.disk.create(
|
51
|
+
linodeid: result.linodeid,
|
52
|
+
type: 'swap',
|
53
|
+
label: 'Swap',
|
54
|
+
size: 512
|
55
|
+
)
|
56
|
+
|
57
|
+
# need to fetch data about the new node to calculate the size of the new disk
|
58
|
+
refresh_nodes
|
59
|
+
new_node = find_node_by_id(result.linodeid)
|
60
|
+
|
61
|
+
os_disk = client.linode.disk.createfromdistribution(
|
62
|
+
linodeid: result.linodeid,
|
63
|
+
distributionid: find_distribution_id_by_name(DEFAULT_IMAGE_NAME),
|
64
|
+
label: DEFAULT_IMAGE_NAME,
|
65
|
+
rootpass: SecureRandom.hex,
|
66
|
+
size: new_node.totalhd - 512,
|
67
|
+
rootsshkey: File.read('/home/rs06r/.ssh/id_rsa.pub')
|
68
|
+
)
|
69
|
+
|
70
|
+
client.linode.config.create(
|
71
|
+
linodeid: result.linodeid,
|
72
|
+
label: 'default',
|
73
|
+
kernelid: find_default_kernel.kernelid,
|
74
|
+
disklist: [os_disk.diskid, swap_disk.diskid].join(',')
|
75
|
+
)
|
76
|
+
|
77
|
+
client.linode.boot(linodeid: result.linodeid)
|
78
|
+
new_node
|
79
|
+
rescue StandardError => e
|
80
|
+
client.linode.delete(linodeid: result.linodeid, skipchecks: true)
|
81
|
+
raise e
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LinodeCluster
|
4
|
+
NodeGroup = Struct.new(:name, :group_name_prefix, :region, :size, :count, :cluster, :options) do
|
5
|
+
def names
|
6
|
+
Array.new(count) { |i| "#{name_prefix}#{i}" }
|
7
|
+
end
|
8
|
+
|
9
|
+
def name_prefix
|
10
|
+
"#{group_name}-"
|
11
|
+
end
|
12
|
+
|
13
|
+
def group_name
|
14
|
+
"#{group_name_prefix}#{name}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def create!
|
18
|
+
names.each do |name|
|
19
|
+
node = cluster.client.find_node_by_name(name)
|
20
|
+
if node
|
21
|
+
check_node_specs(node)
|
22
|
+
else
|
23
|
+
create_node(name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def nodes
|
29
|
+
names.map { |n| cluster.client.find_node_by_name(n) }.compact
|
30
|
+
end
|
31
|
+
|
32
|
+
def template_options(node)
|
33
|
+
Hash[options.map do |k, v|
|
34
|
+
v = node.ip_address if v == '{{ ip_address }}'
|
35
|
+
[k, v]
|
36
|
+
end]
|
37
|
+
end
|
38
|
+
|
39
|
+
def as_ansible_inventory
|
40
|
+
result = +"[#{name}]\n"
|
41
|
+
nodes.each do |node|
|
42
|
+
string_options = template_options(node).map { |k, v| "#{k}=#{v}" }.join(' ')
|
43
|
+
|
44
|
+
result << "#{node.name} #{string_options}".strip
|
45
|
+
result << "\n"
|
46
|
+
end
|
47
|
+
result << "\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def create_node(name)
|
53
|
+
puts "Creating #{name}"
|
54
|
+
cluster.create_node(name: name, region: region, size: size, group_name: group_name)
|
55
|
+
end
|
56
|
+
|
57
|
+
def check_node_specs(node)
|
58
|
+
warnings = []
|
59
|
+
|
60
|
+
if node.region.to_s != region.to_s
|
61
|
+
warnings << "REGION: Expected #{region}, but got #{node.region}"
|
62
|
+
end
|
63
|
+
|
64
|
+
if node.size.to_s != size.to_s
|
65
|
+
warnings << "SIZE: Expected #{size}, but got #{node.size}"
|
66
|
+
end
|
67
|
+
|
68
|
+
if warnings.empty?
|
69
|
+
puts "Node #{node.name}: EXISTS"
|
70
|
+
else
|
71
|
+
puts "Node #{node.name}: WARNING"
|
72
|
+
warnings.each do |warning|
|
73
|
+
puts ' ' + warning
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'linode_cluster/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "linode_cluster"
|
8
|
+
spec.version = LinodeCluster::VERSION
|
9
|
+
spec.authors = ["Ruben Stranders"]
|
10
|
+
spec.email = ["r.stranders@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple wrapper library for creating a cluster of Linodes.}
|
13
|
+
spec.homepage = "https://github.com/rhomeister/linode-cluster"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency 'linode', '~> 0.9'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linode_cluster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ruben Stranders
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: linode
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- r.stranders@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop_todo.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/linode_cluster.rb
|
88
|
+
- lib/linode_cluster/client_wrapper.rb
|
89
|
+
- lib/linode_cluster/cluster.rb
|
90
|
+
- lib/linode_cluster/node.rb
|
91
|
+
- lib/linode_cluster/node_factory.rb
|
92
|
+
- lib/linode_cluster/node_group.rb
|
93
|
+
- lib/linode_cluster/version.rb
|
94
|
+
- linode_cluster.gemspec
|
95
|
+
homepage: https://github.com/rhomeister/linode-cluster
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.6.10
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: A simple wrapper library for creating a cluster of Linodes.
|
119
|
+
test_files: []
|
120
|
+
has_rdoc:
|