skyed 0.1.16 → 0.2.0.dev
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/skyed +4 -12
- data/lib/skyed.rb +4 -13
- data/lib/skyed/cli.rb +10 -0
- data/lib/skyed/settings.rb +7 -43
- metadata +12 -37
- data/lib/skyed/aws.rb +0 -549
- data/lib/skyed/check.rb +0 -69
- data/lib/skyed/commands.rb +0 -174
- data/lib/skyed/create.rb +0 -84
- data/lib/skyed/deploy.rb +0 -20
- data/lib/skyed/destroy.rb +0 -21
- data/lib/skyed/git.rb +0 -29
- data/lib/skyed/init.rb +0 -157
- data/lib/skyed/list.rb +0 -22
- data/lib/skyed/run.rb +0 -133
- data/lib/skyed/stop.rb +0 -14
- data/lib/skyed/utils.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3124fdc3cf9df40d2a03eeffa5917fe023bb89e2
|
4
|
+
data.tar.gz: 8247811351a59cb45f821dbedc10e11c52905db2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1484ec18626a64f4c96eb8eec6dad307f1c2eea966b468aef58b7566759ec0ac47237c6986ffd0c3bb0521d4ecc527b69f1e6f8333d001349c8b24d2a846f180
|
7
|
+
data.tar.gz: 22c9d5d85ea8bbc0ef6863ce21ad08d3a9f3b81bab872a002221b2788013ea846b9ab0b3b6e94abbc42022f27f69c22093f9557af320646de578fba02569b5c4
|
data/bin/skyed
CHANGED
@@ -1,17 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
|
2
3
|
project_path = File.dirname(File.dirname(__FILE__))
|
3
4
|
$LOAD_PATH.unshift project_path + '/lib'
|
5
|
+
$LOAD_PATH.unshift project_path + '/plugins'
|
4
6
|
|
5
|
-
require '
|
6
|
-
require 'skyed'
|
7
|
-
|
8
|
-
include GLI::App
|
9
|
-
|
10
|
-
program_desc 'Are you surrounded by sky?'
|
11
|
-
Skyed::Settings.load!
|
12
|
-
|
13
|
-
switch [:q, :quiet]
|
14
|
-
|
15
|
-
require 'skyed/commands'
|
7
|
+
require 'skyed/cli'
|
16
8
|
|
17
|
-
|
9
|
+
Skyed::CLI.start(ARGV)
|
data/lib/skyed.rb
CHANGED
@@ -1,17 +1,8 @@
|
|
1
|
-
require '
|
2
|
-
require 'skyed/create'
|
3
|
-
require 'skyed/check'
|
4
|
-
require 'skyed/utils'
|
5
|
-
require 'skyed/git'
|
6
|
-
require 'skyed/aws'
|
7
|
-
require 'skyed/destroy'
|
8
|
-
require 'skyed/stop'
|
9
|
-
require 'skyed/run'
|
10
|
-
require 'skyed/deploy'
|
11
|
-
require 'skyed/init'
|
1
|
+
require 'require_all'
|
12
2
|
require 'skyed/settings'
|
3
|
+
require_all 'plugins/*/lib/**/*.rb'
|
13
4
|
|
14
|
-
# Skyed
|
5
|
+
# Main Skyed module
|
15
6
|
module Skyed
|
16
|
-
VERSION = '0.
|
7
|
+
VERSION = '0.2.0.dev'
|
17
8
|
end
|
data/lib/skyed/cli.rb
ADDED
data/lib/skyed/settings.rb
CHANGED
@@ -1,47 +1,11 @@
|
|
1
|
-
|
2
|
-
# This module encapsulates settings for Skyed
|
3
|
-
module Settings
|
4
|
-
CONFIG_FILE = "#{ENV['HOME']}/.skyed"
|
5
|
-
@_settings = {}
|
6
|
-
attr_accessor :_settings
|
7
|
-
|
8
|
-
class << self
|
9
|
-
def current_stack?(stack_id)
|
10
|
-
!Skyed::Settings.empty? && Skyed::Settings.stack_id == stack_id
|
11
|
-
end
|
12
|
-
|
13
|
-
def load!(filename = CONFIG_FILE)
|
14
|
-
newsets = {}
|
15
|
-
newsets = YAML.load_file(filename) if File.file? filename
|
16
|
-
deep_merge!(@_settings, newsets)
|
17
|
-
end
|
18
|
-
|
19
|
-
def empty?
|
20
|
-
@_settings.empty?
|
21
|
-
end
|
1
|
+
require 'thor'
|
22
2
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
def method_missing(name, *args)
|
31
|
-
msg = "unknown configuration root #{name}."
|
32
|
-
msg += ' Initialize skyed or export PKEY'
|
33
|
-
if name.match(/.*=/)
|
34
|
-
@_settings[name.to_s.split('=')[0]] = args[0]
|
35
|
-
else
|
36
|
-
@_settings[name.to_s] ||
|
37
|
-
fail(
|
38
|
-
NoMethodError, msg, caller)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def save(filename = CONFIG_FILE)
|
43
|
-
File.open(filename, 'w') { |f| YAML.dump @_settings, f }
|
44
|
-
end
|
3
|
+
module Skyed
|
4
|
+
# Settings CLI for Skyed
|
5
|
+
class Settings < Thor
|
6
|
+
desc 'list', 'List Skyed settings'
|
7
|
+
def list
|
8
|
+
fail NotImplementedError, 'settings list is not yet implemented'
|
45
9
|
end
|
46
10
|
end
|
47
11
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: skyed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0.dev
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ignasi Fosch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: git
|
@@ -28,44 +28,30 @@ dependencies:
|
|
28
28
|
name: aws-sdk
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 2.0.
|
33
|
+
version: 2.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 2.0.
|
40
|
+
version: 2.0.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: thor
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - '='
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 0.19.1
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - '='
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: highline
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - '='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 1.6.21
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - '='
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: 1.6.21
|
54
|
+
version: 0.19.1
|
69
55
|
description: A cloudy gem
|
70
56
|
email: natx@y10k.ws
|
71
57
|
executables:
|
@@ -75,19 +61,8 @@ extra_rdoc_files: []
|
|
75
61
|
files:
|
76
62
|
- bin/skyed
|
77
63
|
- lib/skyed.rb
|
78
|
-
- lib/skyed/
|
79
|
-
- lib/skyed/check.rb
|
80
|
-
- lib/skyed/commands.rb
|
81
|
-
- lib/skyed/create.rb
|
82
|
-
- lib/skyed/deploy.rb
|
83
|
-
- lib/skyed/destroy.rb
|
84
|
-
- lib/skyed/git.rb
|
85
|
-
- lib/skyed/init.rb
|
86
|
-
- lib/skyed/list.rb
|
87
|
-
- lib/skyed/run.rb
|
64
|
+
- lib/skyed/cli.rb
|
88
65
|
- lib/skyed/settings.rb
|
89
|
-
- lib/skyed/stop.rb
|
90
|
-
- lib/skyed/utils.rb
|
91
66
|
- templates/Vagrantfile.erb
|
92
67
|
- templates/config.j2.erb
|
93
68
|
- templates/credentials.j2.erb
|
@@ -108,9 +83,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
83
|
version: '0'
|
109
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
85
|
requirements:
|
111
|
-
- - "
|
86
|
+
- - ">"
|
112
87
|
- !ruby/object:Gem::Version
|
113
|
-
version:
|
88
|
+
version: 1.3.1
|
114
89
|
requirements: []
|
115
90
|
rubyforge_project:
|
116
91
|
rubygems_version: 2.4.8
|
data/lib/skyed/aws.rb
DELETED
@@ -1,549 +0,0 @@
|
|
1
|
-
require 'aws-sdk'
|
2
|
-
require 'highline/import'
|
3
|
-
|
4
|
-
ACCESS_QUESTION = 'What is your AWS Access Key? '
|
5
|
-
SECRET_QUESTION = 'What is your AWS Secret Key? '
|
6
|
-
|
7
|
-
module Skyed
|
8
|
-
# This module encapsulates all the AWS related functions.
|
9
|
-
module AWS
|
10
|
-
class << self
|
11
|
-
def region
|
12
|
-
ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
|
13
|
-
end
|
14
|
-
|
15
|
-
def set_credentials(access, secret, skip_question = true)
|
16
|
-
access = ask(ACCESS_QUESTION) unless Skyed::AWS.valid_credential?(
|
17
|
-
'AWS_ACCESS_KEY') && skip_question
|
18
|
-
secret = ask(SECRET_QUESTION) unless Skyed::AWS.valid_credential?(
|
19
|
-
'AWS_SECRET_KEY') && skip_question
|
20
|
-
if Skyed::AWS.confirm_credentials?(access, secret)
|
21
|
-
Skyed::Settings.access_key = access
|
22
|
-
Skyed::Settings.secret_key = secret
|
23
|
-
else
|
24
|
-
set_credentials(access, secret, false)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def valid_credential?(env_var_name)
|
29
|
-
ENV[env_var_name] != '' && !ENV[env_var_name].nil?
|
30
|
-
end
|
31
|
-
|
32
|
-
def confirm_credentials?(access, secret)
|
33
|
-
client = Skyed::AWS::IAM.login(access, secret)
|
34
|
-
client.get_account_summary
|
35
|
-
true
|
36
|
-
rescue Aws::IAM::Errors::InvalidClientTokenId
|
37
|
-
false
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# This module encapsulates all the RDS related functions.
|
42
|
-
module ELB
|
43
|
-
class << self
|
44
|
-
def instance_ok?(elb_name, ec2_instance_id, elb = nil)
|
45
|
-
elb.describe_instance_health(
|
46
|
-
load_balancer_name: elb_name,
|
47
|
-
instances: [{ instance_id: ec2_instance_id }]
|
48
|
-
).instance_states[0].state == 'InService'
|
49
|
-
end
|
50
|
-
|
51
|
-
def set_health_check(elb_name, health_check, elb = nil)
|
52
|
-
elb.configure_health_check(
|
53
|
-
load_balancer_name: elb_name,
|
54
|
-
health_check: {
|
55
|
-
target: health_check.target,
|
56
|
-
interval: health_check.interval,
|
57
|
-
timeout: health_check.timeout,
|
58
|
-
unhealthy_threshold: health_check.unhealthy_threshold,
|
59
|
-
healthy_threshold: health_check.healthy_threshold
|
60
|
-
}
|
61
|
-
)
|
62
|
-
end
|
63
|
-
|
64
|
-
def get_health_check(elb_name, elb = nil)
|
65
|
-
elbs = elb.describe_load_balancers(load_balancer_names: [elb_name])
|
66
|
-
elbs.load_balancer_descriptions[0].health_check
|
67
|
-
end
|
68
|
-
|
69
|
-
def login(
|
70
|
-
access = Skyed::Settings.access_key,
|
71
|
-
secret = Skyed::Settings.secret_key,
|
72
|
-
region = Skyed::AWS.region)
|
73
|
-
Aws::ElasticLoadBalancing::Client.new(
|
74
|
-
access_key_id: access,
|
75
|
-
secret_access_key: secret,
|
76
|
-
region: region)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
# This module encapsulates all the RDS related functions.
|
82
|
-
module RDS
|
83
|
-
class << self
|
84
|
-
def create_instance_from_snapshot(
|
85
|
-
instance_name, snapshot, options, rds = nil)
|
86
|
-
rds = login if rds.nil?
|
87
|
-
rds.restore_db_instance_from_db_snapshot(
|
88
|
-
db_instance_identifier: instance_name,
|
89
|
-
db_snapshot_identifier: snapshot)[:db_instance]
|
90
|
-
db_instance = wait_for_instance(
|
91
|
-
instance_name, 'available', options[:wait_interval], rds)
|
92
|
-
"#{db_instance[:endpoint][:address]}:#{db_instance[:endpoint][:port]}"
|
93
|
-
end
|
94
|
-
|
95
|
-
def list_snapshots(_options, args, rds = nil)
|
96
|
-
rds = login if rds.nil?
|
97
|
-
request = {}
|
98
|
-
request[:db_instance_identifier] = args.first unless args.nil?
|
99
|
-
response = rds.describe_db_snapshots(request)
|
100
|
-
response.db_snapshots
|
101
|
-
end
|
102
|
-
|
103
|
-
def destroy_instance(instance_name, options, rds = nil)
|
104
|
-
rds = login if rds.nil?
|
105
|
-
rds.delete_db_instance(
|
106
|
-
generate_params(instance_name, options))[:db_instance]
|
107
|
-
end
|
108
|
-
|
109
|
-
def generate_delete_params(instance_name, options)
|
110
|
-
snapshot = !options[:final_snapshot_name].empty?
|
111
|
-
params = {
|
112
|
-
db_instance_identifier: instance_name,
|
113
|
-
skip_final_snapshot: !snapshot
|
114
|
-
}
|
115
|
-
snapshot_name = options[:final_snapshot_name]
|
116
|
-
params[:final_db_snapshot_identifier] = snapshot_name if snapshot
|
117
|
-
params
|
118
|
-
end
|
119
|
-
|
120
|
-
def generate_create_params(instance_name, options)
|
121
|
-
{
|
122
|
-
db_instance_identifier: instance_name,
|
123
|
-
allocated_storage: options[:size],
|
124
|
-
db_instance_class: "db.#{options[:type]}",
|
125
|
-
engine: 'postgres',
|
126
|
-
master_username: options[:user],
|
127
|
-
master_user_password: options[:password],
|
128
|
-
db_security_groups: [options[:db_security_group]],
|
129
|
-
db_parameter_group_name: options[:db_parameters_group]
|
130
|
-
}
|
131
|
-
end
|
132
|
-
|
133
|
-
def wait_for_instance(instance_name, status, wait = 0, rds = nil)
|
134
|
-
rds = login if rds.nil?
|
135
|
-
instance = rds.describe_db_instances(
|
136
|
-
db_instance_identifier: instance_name)[:db_instances][0]
|
137
|
-
while instance[:db_instance_status] != status
|
138
|
-
sleep(wait)
|
139
|
-
instance = rds.describe_db_instances(
|
140
|
-
db_instance_identifier: instance_name)[:db_instances][0]
|
141
|
-
end
|
142
|
-
instance
|
143
|
-
end
|
144
|
-
|
145
|
-
def generate_params(instance_name, options)
|
146
|
-
params = generate_delete_params(
|
147
|
-
instance_name, options) if options.key? :final_snapshot_name
|
148
|
-
params = generate_create_params(
|
149
|
-
instance_name, options) if options.key? :user
|
150
|
-
params
|
151
|
-
end
|
152
|
-
|
153
|
-
def create_instance(instance_name, options, rds = nil)
|
154
|
-
rds = login if rds.nil?
|
155
|
-
rds.create_db_instance(
|
156
|
-
generate_params(instance_name, options))[:db_instance]
|
157
|
-
db_instance = wait_for_instance(instance_name, 'available', 0, rds)
|
158
|
-
"#{db_instance[:endpoint][:address]}:#{db_instance[:endpoint][:port]}"
|
159
|
-
end
|
160
|
-
|
161
|
-
def login(
|
162
|
-
access = Skyed::Settings.access_key,
|
163
|
-
secret = Skyed::Settings.secret_key,
|
164
|
-
region = Skyed::AWS.region)
|
165
|
-
Aws::RDS::Client.new(
|
166
|
-
access_key_id: access,
|
167
|
-
secret_access_key: secret,
|
168
|
-
region: region)
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
# This module encapsulates all the OpsWorks related functions.
|
174
|
-
module OpsWorks
|
175
|
-
STACK = {
|
176
|
-
name: '',
|
177
|
-
region: '',
|
178
|
-
service_role_arn: '',
|
179
|
-
default_instance_profile_arn: '',
|
180
|
-
default_os: 'Ubuntu 12.04 LTS',
|
181
|
-
default_ssh_key_name: '',
|
182
|
-
custom_cookbooks_source: {
|
183
|
-
type: 'git'
|
184
|
-
},
|
185
|
-
configuration_manager: {
|
186
|
-
name: 'Chef',
|
187
|
-
version: '11.10'
|
188
|
-
},
|
189
|
-
use_custom_cookbooks: true,
|
190
|
-
use_opsworks_security_groups: false
|
191
|
-
}
|
192
|
-
LAYER = {
|
193
|
-
stack_id: nil,
|
194
|
-
type: 'custom',
|
195
|
-
name: '',
|
196
|
-
shortname: '',
|
197
|
-
custom_security_group_ids: []
|
198
|
-
}
|
199
|
-
|
200
|
-
class << self
|
201
|
-
def instance_by_id(instance_id, opsworks)
|
202
|
-
instances_data = opsworks.describe_instances(
|
203
|
-
instance_ids: [instance_id])
|
204
|
-
instances = instances_data[:instances] unless instances_data.nil?
|
205
|
-
instances[0] unless instances.nil?
|
206
|
-
end
|
207
|
-
|
208
|
-
def start_instance(instance_id, opsworks)
|
209
|
-
opsworks.start_instance(instance_id: instance_id)
|
210
|
-
wait_for_instance_id(instance_id, 'online', opsworks)
|
211
|
-
puts instance_by_id(instance_id, opsworks).public_dns
|
212
|
-
end
|
213
|
-
|
214
|
-
def instances_by_status(stack_id, layer_id, status, ow)
|
215
|
-
Skyed::Settings.stack_id = stack(stack_id, ow).stack_id
|
216
|
-
ow.describe_instances(
|
217
|
-
layer_id: layer(layer_id, ow).layer_id)[:instances].select do |i|
|
218
|
-
i.status == status
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
def stop_instance(stack_id, hostname, opsworks = nil)
|
223
|
-
opsworks = login if opsworks.nil?
|
224
|
-
instance = instance_by_name(hostname, stack_id, opsworks)
|
225
|
-
opsworks.stop_instance(instance_id: instance.instance_id)
|
226
|
-
wait_for_instance_id(
|
227
|
-
instance.instance_id,
|
228
|
-
'stopped',
|
229
|
-
opsworks)
|
230
|
-
end
|
231
|
-
|
232
|
-
def create_instance(stack_id, layer_id, instance_type, opsworks)
|
233
|
-
instance = opsworks.create_instance(
|
234
|
-
stack_id: stack_id,
|
235
|
-
layer_ids: [layer_id],
|
236
|
-
instance_type: instance_type)
|
237
|
-
opsworks.start_instance(instance_id: instance.instance_id)
|
238
|
-
wait_for_instance_id(
|
239
|
-
instance.instance_id,
|
240
|
-
'online',
|
241
|
-
opsworks)
|
242
|
-
end
|
243
|
-
|
244
|
-
def wait_for_instance_id(instance_id, status, opsworks)
|
245
|
-
instance = opsworks.describe_instances(
|
246
|
-
instance_ids: [instance_id]).instances.first
|
247
|
-
until instance.nil? || instance.status == status
|
248
|
-
sleep(0)
|
249
|
-
instance = opsworks.describe_instances(
|
250
|
-
instance_ids: [instance_id]).instances.first
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
def deregister_instance(hostname, opsworks)
|
255
|
-
instance = instance_by_name(
|
256
|
-
hostname, Skyed::Settings.stack_id, opsworks)
|
257
|
-
opsworks.deregister_instance(
|
258
|
-
instance_id: instance.instance_id) unless instance.nil?
|
259
|
-
wait_for_instance(
|
260
|
-
hostname, Skyed::Settings.stack_id, 'terminated', opsworks)
|
261
|
-
end
|
262
|
-
|
263
|
-
def delete_user(opsworks)
|
264
|
-
stack = opsworks.describe_stacks(
|
265
|
-
stack_ids: [Skyed::Settings.stack_id])[:stacks][0][:name]
|
266
|
-
layer = opsworks.describe_layers(
|
267
|
-
layer_ids: [Skyed::Settings.layer_id])[:layers][0][:name]
|
268
|
-
Skyed::AWS::IAM.delete_user "OpsWorks-#{stack}-#{layer}"
|
269
|
-
end
|
270
|
-
|
271
|
-
def wait_for_instance(instance_name, stack_id, status, opsworks)
|
272
|
-
instance = Skyed::AWS::OpsWorks.instance_by_name(
|
273
|
-
instance_name, stack_id, opsworks)
|
274
|
-
until instance.nil? || instance.status == status
|
275
|
-
sleep(0)
|
276
|
-
instance = Skyed::AWS::OpsWorks.instance_by_name(
|
277
|
-
instance_name, stack_id, opsworks)
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
def stack(stack_criteria, opsworks)
|
282
|
-
stack_by_name(
|
283
|
-
stack_criteria,
|
284
|
-
opsworks
|
285
|
-
) || stack_by_id(stack_criteria, opsworks)
|
286
|
-
end
|
287
|
-
|
288
|
-
def layer(layer_criteria, opsworks, stack_id = nil)
|
289
|
-
layer = layer_by_name(layer_criteria, opsworks) ||
|
290
|
-
layer_by_id(layer_criteria, opsworks)
|
291
|
-
instance = nil
|
292
|
-
instance = instance_by_name(
|
293
|
-
layer_criteria, stack_id, opsworks) unless stack_id.nil?
|
294
|
-
layer ||= layer_by_id(
|
295
|
-
instance.layer_ids[0], opsworks) unless instance.nil?
|
296
|
-
layer
|
297
|
-
end
|
298
|
-
|
299
|
-
def deploy(opts)
|
300
|
-
xtra = { instance_ids: opts[:instance_ids] }
|
301
|
-
xtra[:custom_json] = opts[:custom_json] if opts.key? :custom_json
|
302
|
-
Skyed::AWS::OpsWorks.wait_for_deploy(
|
303
|
-
opts[:client].create_deployment(
|
304
|
-
Skyed::AWS::OpsWorks.generate_deploy_params(
|
305
|
-
opts[:stack_id],
|
306
|
-
opts[:command],
|
307
|
-
xtra)),
|
308
|
-
opts[:client],
|
309
|
-
opts[:wait_interval])
|
310
|
-
end
|
311
|
-
|
312
|
-
def layer_by_id(layer_id, opsworks)
|
313
|
-
layers(opsworks).select { |x| x[:layer_id] == layer_id }[0] || nil
|
314
|
-
end
|
315
|
-
|
316
|
-
def layer_by_name(layer_name, opsworks)
|
317
|
-
layers(opsworks).select { |x| x[:name] == layer_name }[0] || nil
|
318
|
-
end
|
319
|
-
|
320
|
-
def layers(opsworks)
|
321
|
-
opsworks.describe_layers(stack_id: Skyed::Settings.stack_id)[:layers]
|
322
|
-
end
|
323
|
-
|
324
|
-
def stack_by_id(stack_id, opsworks)
|
325
|
-
stacks(opsworks).select { |x| x[:stack_id] == stack_id }[0] || nil
|
326
|
-
end
|
327
|
-
|
328
|
-
def running_instances(options = {}, opsworks)
|
329
|
-
instances = opsworks.describe_instances(options)
|
330
|
-
instances[:instances].map do |instance|
|
331
|
-
instance[:instance_id] if instance[:status] != 'stopped'
|
332
|
-
end.compact
|
333
|
-
end
|
334
|
-
|
335
|
-
def instance_by_name(hostname, stack_id, opsworks)
|
336
|
-
opsworks.describe_instances(
|
337
|
-
stack_id: stack_id)[:instances].select do |i|
|
338
|
-
i.hostname == hostname
|
339
|
-
end[0]
|
340
|
-
end
|
341
|
-
|
342
|
-
def wait_for_deploy(deploy, opsworks, wait = 0)
|
343
|
-
status = Skyed::AWS::OpsWorks.deploy_status(deploy, opsworks)
|
344
|
-
while status[0] == 'running'
|
345
|
-
sleep(wait)
|
346
|
-
status = Skyed::AWS::OpsWorks.deploy_status(deploy, opsworks)
|
347
|
-
end
|
348
|
-
status
|
349
|
-
end
|
350
|
-
|
351
|
-
def deploy_status(deploy, opsworks)
|
352
|
-
deploy = opsworks.describe_deployments(
|
353
|
-
deployment_ids: [deploy[:deployment_id]])
|
354
|
-
deploy[:deployments].map do |s|
|
355
|
-
s[:status]
|
356
|
-
end.compact
|
357
|
-
end
|
358
|
-
|
359
|
-
def generate_deploy_params(stack_id, command, options = {})
|
360
|
-
options = {} if options.nil?
|
361
|
-
params = {
|
362
|
-
stack_id: stack_id,
|
363
|
-
command: generate_command_params(command)
|
364
|
-
}
|
365
|
-
params.merge(options)
|
366
|
-
end
|
367
|
-
|
368
|
-
def generate_command_params(options = {})
|
369
|
-
response = options
|
370
|
-
response = {
|
371
|
-
name: options[:name],
|
372
|
-
args: options.reject { |k, _v| k == :name }
|
373
|
-
} unless options[:name] != 'execute_recipes'
|
374
|
-
response
|
375
|
-
end
|
376
|
-
|
377
|
-
def create_layer(layer_params, opsworks)
|
378
|
-
layer = opsworks.create_layer(layer_params)
|
379
|
-
Skyed::Settings.layer_id = layer.data[:layer_id]
|
380
|
-
end
|
381
|
-
|
382
|
-
def create_stack(stack_params, opsworks)
|
383
|
-
stack = opsworks.create_stack(stack_params)
|
384
|
-
Skyed::Settings.stack_id = stack.data[:stack_id]
|
385
|
-
end
|
386
|
-
|
387
|
-
def delete_stack(stack_name, opsworks)
|
388
|
-
total = count_instances(stack_name, opsworks)
|
389
|
-
error_msg = "Stack with name #{stack_name}"
|
390
|
-
error_msg += ' exists and contains instances'
|
391
|
-
fail error_msg unless total == 0
|
392
|
-
stack = stack_by_name(stack_name, opsworks)
|
393
|
-
opsworks.delete_stack(stack_id: stack[:stack_id])
|
394
|
-
end
|
395
|
-
|
396
|
-
def count_instances(stack_name, opsworks)
|
397
|
-
stack_summary = stack_summary_by_name(stack_name, opsworks)
|
398
|
-
return nil if stack_summary.nil?
|
399
|
-
total = stack_summary[:instances_count].values.compact.inject(:+) || 0
|
400
|
-
total
|
401
|
-
end
|
402
|
-
|
403
|
-
def stack_summary_by_name(stack_name, opsworks)
|
404
|
-
stack = stack_by_name(stack_name, opsworks)
|
405
|
-
opsworks.describe_stack_summary(
|
406
|
-
stack_id: stack[:stack_id])[:stack_summary] unless stack.nil?
|
407
|
-
end
|
408
|
-
|
409
|
-
def stack_by_name(stack_name, opsworks)
|
410
|
-
stacks(opsworks).select { |x| x[:name] == stack_name }[0] || nil
|
411
|
-
end
|
412
|
-
|
413
|
-
def stacks(opsworks)
|
414
|
-
opsworks.describe_stacks[:stacks]
|
415
|
-
end
|
416
|
-
|
417
|
-
def basic_stack_params
|
418
|
-
params = STACK
|
419
|
-
params[:name] = ENV['USER']
|
420
|
-
params[:region] = Skyed::AWS.region
|
421
|
-
params[:service_role_arn] = Skyed::Settings.role_arn
|
422
|
-
params[:default_instance_profile_arn] = Skyed::Settings.profile_arn
|
423
|
-
params[:default_ssh_key_name] = Skyed::Settings.aws_key_name
|
424
|
-
params
|
425
|
-
end
|
426
|
-
|
427
|
-
def custom_cookbooks_source(base_source)
|
428
|
-
base_source[:url] = Skyed::Settings.remote_url
|
429
|
-
base_source[:revision] = Skyed::Settings.branch
|
430
|
-
base_source[:ssh_key] = Skyed::Utils.read_key_file(
|
431
|
-
Skyed::Settings.opsworks_git_key)
|
432
|
-
base_source
|
433
|
-
end
|
434
|
-
|
435
|
-
def configuration_manager(base_config, options)
|
436
|
-
base_config[:name] = 'Chef'
|
437
|
-
base_config[:version] = options[:chef_version] || '11.10'
|
438
|
-
base_config
|
439
|
-
end
|
440
|
-
|
441
|
-
def generate_params(stack_id = nil, options = {})
|
442
|
-
params = generate_layer_params(stack_id) unless stack_id.nil?
|
443
|
-
params = generate_stack_params(options) if stack_id.nil?
|
444
|
-
params
|
445
|
-
end
|
446
|
-
|
447
|
-
def generate_layer_params(stack_id)
|
448
|
-
params = LAYER
|
449
|
-
params[:stack_id] = stack_id
|
450
|
-
params[:name] = "test-#{ENV['USER']}"
|
451
|
-
params[:shortname] = "test-#{ENV['USER']}"
|
452
|
-
params[:custom_security_group_ids] = ['sg-f1cc2498']
|
453
|
-
params
|
454
|
-
end
|
455
|
-
|
456
|
-
def generate_stack_params(options)
|
457
|
-
params = basic_stack_params
|
458
|
-
params[:custom_json] = options[:custom_json] || ''
|
459
|
-
params[:custom_cookbooks_source] = custom_cookbooks_source(
|
460
|
-
STACK[:custom_cookbooks_source])
|
461
|
-
params[:configuration_manager] = configuration_manager(
|
462
|
-
STACK[:configuration_manager], options)
|
463
|
-
params
|
464
|
-
end
|
465
|
-
|
466
|
-
def login(
|
467
|
-
access = Skyed::Settings.access_key,
|
468
|
-
secret = Skyed::Settings.secret_key,
|
469
|
-
region = Skyed::AWS.region)
|
470
|
-
Aws::OpsWorks::Client.new(
|
471
|
-
access_key_id: access,
|
472
|
-
secret_access_key: secret,
|
473
|
-
region: region)
|
474
|
-
end
|
475
|
-
|
476
|
-
def set_arns(service_role = nil, instance_profile = nil)
|
477
|
-
iam = Skyed::AWS::IAM.login
|
478
|
-
Skyed::Settings.role_arn = service_role || iam.get_role(
|
479
|
-
role_name: 'aws-opsworks-service-role')[:role][:arn]
|
480
|
-
Skyed::Settings.profile_arn = instance_profile || iam
|
481
|
-
.get_instance_profile(
|
482
|
-
instance_profile_name: 'aws-opsworks-ec2-role'
|
483
|
-
)[:instance_profile][:arn]
|
484
|
-
end
|
485
|
-
end
|
486
|
-
end
|
487
|
-
|
488
|
-
# This module encapsulates all the IAM related functions.
|
489
|
-
module IAM
|
490
|
-
class << self
|
491
|
-
def remove_user_from_group(user, group)
|
492
|
-
iam = login
|
493
|
-
puts "Removes #{user} from #{group}"
|
494
|
-
iam.remove_user_from_group(
|
495
|
-
group_name: group,
|
496
|
-
user_name: user)
|
497
|
-
rescue Aws::IAM::Errors::NoSuchEntity
|
498
|
-
puts "User #{user} already removed from group #{group}"
|
499
|
-
end
|
500
|
-
|
501
|
-
def clear_user_access_keys(user)
|
502
|
-
iam = login
|
503
|
-
iam.list_access_keys(
|
504
|
-
user_name: user)[:access_key_metadata].each do |access_key|
|
505
|
-
id = access_key.to_h[:access_key_id]
|
506
|
-
puts "Delete access key #{id}"
|
507
|
-
iam.delete_access_key(user_name: user, access_key_id: id)
|
508
|
-
end
|
509
|
-
rescue Aws::IAM::Errors::NoSuchEntity
|
510
|
-
puts "User #{user} access keys already removed"
|
511
|
-
end
|
512
|
-
|
513
|
-
def clear_user_policies(user)
|
514
|
-
iam = login
|
515
|
-
iam.list_user_policies(
|
516
|
-
user_name: user)[:policy_names].each do |policy|
|
517
|
-
puts "Delete user policy #{policy}"
|
518
|
-
iam.delete_user_policy(user_name: user, policy_name: policy)
|
519
|
-
end
|
520
|
-
rescue Aws::IAM::Errors::NoSuchEntity
|
521
|
-
puts "User #{user} policies already removed"
|
522
|
-
end
|
523
|
-
|
524
|
-
def delete_user(user)
|
525
|
-
iam = login
|
526
|
-
clear_user_policies user
|
527
|
-
clear_user_access_keys user
|
528
|
-
remove_user_from_group user, "OpsWorks-#{Skyed::Settings.stack_id}"
|
529
|
-
puts "Delete group OpsWorks-#{Skyed::Settings.stack_id}"
|
530
|
-
iam.delete_group(group_name: "OpsWorks-#{Skyed::Settings.stack_id}")
|
531
|
-
puts "Delete User #{user}"
|
532
|
-
iam.delete_user(user_name: user)
|
533
|
-
rescue Aws::IAM::Errors::NoSuchEntity
|
534
|
-
puts "User #{user} already removed"
|
535
|
-
end
|
536
|
-
|
537
|
-
def login(
|
538
|
-
access = Skyed::Settings.access_key,
|
539
|
-
secret = Skyed::Settings.secret_key,
|
540
|
-
region = Skyed::AWS.region)
|
541
|
-
Aws::IAM::Client.new(
|
542
|
-
access_key_id: access,
|
543
|
-
secret_access_key: secret,
|
544
|
-
region: region)
|
545
|
-
end
|
546
|
-
end
|
547
|
-
end
|
548
|
-
end
|
549
|
-
end
|