skyed 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/skyed/aws.rb +138 -0
- data/lib/skyed/destroy.rb +28 -5
- data/lib/skyed/run.rb +23 -60
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3dff20f796f444c5110db4e24ab5fbe30640f2a
|
4
|
+
data.tar.gz: 4c90c8a4b5d6f667ee8f4d21e3734c1dd9a09146
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7c950fbf09f8d159da451fcb1cb89c5775de866b224d18396373724f480db5028054cff8a9bc6cf49079fbffbaa91b2e168c2f78efcd63d905da893551ff739
|
7
|
+
data.tar.gz: dd78e8e85aacf0f4ea3aea195d8450759a245503b14268caffa25a53dbe5204f7b723ea1bc60393ccde03abd0274e4ec235724d6443aef47f28433c2adabe8fc
|
data/lib/skyed/aws.rb
CHANGED
@@ -66,6 +66,98 @@ module Skyed
|
|
66
66
|
}
|
67
67
|
|
68
68
|
class << self
|
69
|
+
def stack(stack_criteria, opsworks)
|
70
|
+
stack_by_name(
|
71
|
+
stack_criteria,
|
72
|
+
opsworks
|
73
|
+
) || stack_by_id(stack_criteria, opsworks)
|
74
|
+
end
|
75
|
+
|
76
|
+
def layer(layer_criteria, opsworks)
|
77
|
+
layer_by_name(
|
78
|
+
layer_criteria,
|
79
|
+
opsworks
|
80
|
+
) || layer_by_id(layer_criteria, opsworks)
|
81
|
+
end
|
82
|
+
|
83
|
+
def deploy(opts)
|
84
|
+
xtra = { instance_ids: opts[:instance_ids] }
|
85
|
+
xtra[:custom_json] = opts[:custom_json] if opts.key? :custom_json
|
86
|
+
Skyed::AWS::OpsWorks.wait_for_deploy(
|
87
|
+
opts[:client].create_deployment(
|
88
|
+
Skyed::AWS::OpsWorks.generate_deploy_params(
|
89
|
+
opts[:stack_id],
|
90
|
+
opts[:command],
|
91
|
+
xtra)),
|
92
|
+
opts[:client],
|
93
|
+
opts[:wait_interval])
|
94
|
+
end
|
95
|
+
|
96
|
+
def layer_by_id(layer_id, opsworks)
|
97
|
+
layers(opsworks).select { |x| x[:layer_id] == layer_id }[0] || nil
|
98
|
+
end
|
99
|
+
|
100
|
+
def layer_by_name(layer_name, opsworks)
|
101
|
+
layers(opsworks).select { |x| x[:name] == layer_name }[0] || nil
|
102
|
+
end
|
103
|
+
|
104
|
+
def layers(opsworks)
|
105
|
+
opsworks.describe_layers(stack_id: Skyed::Settings.stack_id)[:layers]
|
106
|
+
end
|
107
|
+
|
108
|
+
def stack_by_id(stack_id, opsworks)
|
109
|
+
stacks(opsworks).select { |x| x[:stack_id] == stack_id }[0] || nil
|
110
|
+
end
|
111
|
+
|
112
|
+
def running_instances(options = {}, opsworks)
|
113
|
+
instances = opsworks.describe_instances(options)
|
114
|
+
instances[:instances].map do |instance|
|
115
|
+
instance[:instance_id] if instance[:status] != 'stopped'
|
116
|
+
end.compact
|
117
|
+
end
|
118
|
+
|
119
|
+
def instance_by_name(hostname, stack_id, opsworks)
|
120
|
+
opsworks.describe_instances(
|
121
|
+
stack_id: stack_id)[:instances].select do |i|
|
122
|
+
i.hostname == hostname
|
123
|
+
end[0]
|
124
|
+
end
|
125
|
+
|
126
|
+
def wait_for_deploy(deploy, opsworks, wait = 0)
|
127
|
+
status = Skyed::AWS::OpsWorks.deploy_status(deploy, opsworks)
|
128
|
+
while status[0] == 'running'
|
129
|
+
sleep(wait)
|
130
|
+
status = Skyed::AWS::OpsWorks.deploy_status(deploy, opsworks)
|
131
|
+
end
|
132
|
+
status
|
133
|
+
end
|
134
|
+
|
135
|
+
def deploy_status(deploy, opsworks)
|
136
|
+
deploy = opsworks.describe_deployments(
|
137
|
+
deployment_ids: [deploy[:deployment_id]])
|
138
|
+
deploy[:deployments].map do |s|
|
139
|
+
s[:status]
|
140
|
+
end.compact
|
141
|
+
end
|
142
|
+
|
143
|
+
def generate_deploy_params(stack_id, command, options = {})
|
144
|
+
options = {} if options.nil?
|
145
|
+
params = {
|
146
|
+
stack_id: stack_id,
|
147
|
+
command: generate_command_params(command)
|
148
|
+
}
|
149
|
+
params.merge(options)
|
150
|
+
end
|
151
|
+
|
152
|
+
def generate_command_params(options = {})
|
153
|
+
response = options
|
154
|
+
response = {
|
155
|
+
name: options[:name],
|
156
|
+
args: options.reject { |k, _v| k == :name }
|
157
|
+
} unless options[:name] != 'execute_recipes'
|
158
|
+
response
|
159
|
+
end
|
160
|
+
|
69
161
|
def create_layer(layer_params, opsworks)
|
70
162
|
layer = opsworks.create_layer(layer_params)
|
71
163
|
Skyed::Settings.layer_id = layer.data[:layer_id]
|
@@ -179,6 +271,52 @@ module Skyed
|
|
179
271
|
# This module encapsulates all the IAM related functions.
|
180
272
|
module IAM
|
181
273
|
class << self
|
274
|
+
def remove_user_from_group(user, group)
|
275
|
+
iam = login
|
276
|
+
puts "Removes #{user} from #{group}"
|
277
|
+
iam.remove_user_from_group(
|
278
|
+
group_name: group,
|
279
|
+
user_name: user)
|
280
|
+
rescue Aws::IAM::Errors::NoSuchEntity
|
281
|
+
puts "User #{user} already removed from group #{group}"
|
282
|
+
end
|
283
|
+
|
284
|
+
def clear_user_access_keys(user)
|
285
|
+
iam = login
|
286
|
+
iam.list_access_keys(
|
287
|
+
user_name: user)[:access_key_metadata].each do |access_key|
|
288
|
+
id = access_key.to_h[:access_key_id]
|
289
|
+
puts "Delete access key #{id}"
|
290
|
+
iam.delete_access_key(user_name: user, access_key_id: id)
|
291
|
+
end
|
292
|
+
rescue Aws::IAM::Errors::NoSuchEntity
|
293
|
+
puts "User #{user} access keys already removed"
|
294
|
+
end
|
295
|
+
|
296
|
+
def clear_user_policies(user)
|
297
|
+
iam = login
|
298
|
+
iam.list_user_policies(
|
299
|
+
user_name: user)[:policy_names].each do |policy|
|
300
|
+
puts "Delete user policy #{policy}"
|
301
|
+
iam.delete_user_policy(user_name: user, policy_name: policy)
|
302
|
+
end
|
303
|
+
rescue Aws::IAM::Errors::NoSuchEntity
|
304
|
+
puts "User #{user} policies already removed"
|
305
|
+
end
|
306
|
+
|
307
|
+
def delete_user(user)
|
308
|
+
iam = login
|
309
|
+
clear_user_policies user
|
310
|
+
clear_user_access_keys user
|
311
|
+
remove_user_from_group user, "OpsWorks-#{Skyed::Settings.stack_id}"
|
312
|
+
puts "Delete group OpsWorks-#{Skyed::Settings.stack_id}"
|
313
|
+
iam.delete_group(group_name: "OpsWorks-#{Skyed::Settings.stack_id}")
|
314
|
+
puts "Delete User #{user}"
|
315
|
+
iam.delete_user(user_name: user)
|
316
|
+
rescue Aws::IAM::Errors::NoSuchEntity
|
317
|
+
puts "User #{user} already removed"
|
318
|
+
end
|
319
|
+
|
182
320
|
def login(
|
183
321
|
access = Skyed::Settings.access_key,
|
184
322
|
secret = Skyed::Settings.secret_key,
|
data/lib/skyed/destroy.rb
CHANGED
@@ -7,11 +7,34 @@ module Skyed
|
|
7
7
|
hostname = `cd #{repo_path} && vagrant ssh -c hostname`.strip
|
8
8
|
`cd #{repo_path} && vagrant destroy -f`
|
9
9
|
ow = Skyed::AWS::OpsWorks.login
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
deregister_instance hostname, ow
|
11
|
+
delete_user ow
|
12
|
+
end
|
13
|
+
|
14
|
+
def deregister_instance(hostname, ow)
|
15
|
+
instance = Skyed::AWS::OpsWorks.instance_by_name(
|
16
|
+
hostname, Skyed::Settings.stack_id, ow)
|
17
|
+
ow.deregister_instance(
|
18
|
+
instance_id: instance.instance_id) unless instance.nil?
|
19
|
+
wait_for_instance(
|
20
|
+
hostname, Skyed::Settings.stack_id, ow)
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete_user(ow)
|
24
|
+
stack = ow.describe_stacks(
|
25
|
+
stack_ids: [Skyed::Settings.stack_id])[:stacks][0][:name]
|
26
|
+
layer = ow.describe_layers(
|
27
|
+
layer_ids: [Skyed::Settings.layer_id])[:layers][0][:name]
|
28
|
+
Skyed::AWS::IAM.delete_user "OpsWorks-#{stack}-#{layer}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def wait_for_instance(hostname, stack_id, opsworks)
|
32
|
+
instance = Skyed::AWS::OpsWorks.instance_by_name(
|
33
|
+
hostname, stack_id, opsworks)
|
34
|
+
until instance.nil? || instance.status == 'terminated'
|
35
|
+
sleep(0)
|
36
|
+
instance = Skyed::AWS::OpsWorks.instance_by_name(
|
37
|
+
hostname, stack_id, opsworks)
|
15
38
|
end
|
16
39
|
end
|
17
40
|
end
|
data/lib/skyed/run.rb
CHANGED
@@ -17,66 +17,36 @@ module Skyed
|
|
17
17
|
ow = login
|
18
18
|
Skyed::Settings.stack_id = stack(ow, options)
|
19
19
|
Skyed::Settings.layer_id = layer(ow, options)
|
20
|
-
instances =
|
20
|
+
instances = Skyed::AWS::OpsWorks.running_instances(
|
21
|
+
{ layer_id: Skyed::Settings.layer_id },
|
22
|
+
ow)
|
21
23
|
update_custom_cookbooks(ow, Skyed::Settings.stack_id, instances,
|
22
24
|
options[:wait_interval])
|
23
25
|
execute_recipes(ow, args, instances, options)
|
24
26
|
end
|
25
27
|
|
26
|
-
def deploy_status(ow, id)
|
27
|
-
deploy = ow.describe_deployments(deployment_ids: [id[:deployment_id]])
|
28
|
-
deploy[:deployments].map do |s|
|
29
|
-
s[:status]
|
30
|
-
end.compact
|
31
|
-
end
|
32
|
-
|
33
|
-
def wait_for_deploy(ow, deploy_id, wait = 0)
|
34
|
-
status = deploy_status(ow, deploy_id)
|
35
|
-
while status[0] == 'running'
|
36
|
-
sleep(wait)
|
37
|
-
status = deploy_status(ow, deploy_id)
|
38
|
-
end
|
39
|
-
fail 'Deployment failed' unless status[0] == 'successful'
|
40
|
-
status
|
41
|
-
end
|
42
|
-
|
43
28
|
def update_custom_cookbooks(ow, stack_id, instances, wait = 0)
|
44
|
-
|
45
|
-
wait_for_deploy(ow, ow.create_deployment(
|
29
|
+
status = Skyed::AWS::OpsWorks.deploy(
|
46
30
|
stack_id: stack_id,
|
31
|
+
command: { name: 'update_custom_cookbooks' },
|
47
32
|
instance_ids: instances,
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
def running_instances(ow, layer_id)
|
52
|
-
instances = ow.describe_instances(layer_id: layer_id)
|
53
|
-
instances[:instances].map do |instance|
|
54
|
-
instance[:instance_id] if instance[:status] != 'stopped'
|
55
|
-
end.compact
|
33
|
+
client: ow,
|
34
|
+
wait_interval: wait)
|
35
|
+
fail 'Deployment failed' unless status[0] == 'successful'
|
56
36
|
end
|
57
37
|
|
58
38
|
def layer(ow, options)
|
59
|
-
|
60
|
-
layer = nil
|
61
|
-
layers.each do |layer_reply|
|
62
|
-
id = layer_reply[:layer_id]
|
63
|
-
layer = id if id == options[:layer]
|
64
|
-
end
|
39
|
+
layer = Skyed::AWS::OpsWorks.layer(options[:layer], ow)
|
65
40
|
msg = "There's no such layer with id #{options[:layer]}"
|
66
41
|
fail msg unless layer
|
67
|
-
layer
|
42
|
+
layer[:layer_id]
|
68
43
|
end
|
69
44
|
|
70
45
|
def stack(ow, options)
|
71
|
-
|
72
|
-
stack = nil
|
73
|
-
stacks.each do |stack_reply|
|
74
|
-
id = stack_reply[:stack_id]
|
75
|
-
stack = id if id == options[:stack]
|
76
|
-
end
|
46
|
+
stack = Skyed::AWS::OpsWorks.stack(options[:stack], ow)
|
77
47
|
msg = "There's no such stack with id #{options[:stack]}"
|
78
48
|
fail msg unless stack
|
79
|
-
stack
|
49
|
+
stack[:stack_id]
|
80
50
|
end
|
81
51
|
|
82
52
|
def login
|
@@ -104,24 +74,17 @@ module Skyed
|
|
104
74
|
recipes
|
105
75
|
end
|
106
76
|
|
107
|
-
def execute_recipes(
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
def execute_params(recipes, instances, custom_json)
|
120
|
-
command = { name: 'execute_recipes', args: { recipes: recipes } }
|
121
|
-
deploy_params = { stack_id: Skyed::Settings.stack_id, command: command }
|
122
|
-
deploy_params[:custom_json] = custom_json unless custom_json.empty?
|
123
|
-
deploy_params[:instance_ids] = instances unless instances.nil?
|
124
|
-
deploy_params
|
77
|
+
def execute_recipes(ow, recipes, instances = nil, opts = {})
|
78
|
+
args = {
|
79
|
+
stack_id: Skyed::Settings.stack_id,
|
80
|
+
command: { name: 'execute_recipes', recipes: recipes },
|
81
|
+
instance_ids: instances,
|
82
|
+
client: ow,
|
83
|
+
wait_interval: opts[:wait_interval] || 0
|
84
|
+
}
|
85
|
+
args[:custom_json] = opts[:custom_json] if opts.key? :custom_json
|
86
|
+
status = Skyed::AWS::OpsWorks.deploy(args)
|
87
|
+
fail 'Deployment failed' unless status[0] == 'successful'
|
125
88
|
end
|
126
89
|
|
127
90
|
def recipe_in_cookbook(recipe)
|
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.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ignasi Fosch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: git
|