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.
@@ -1,69 +0,0 @@
1
- module Skyed
2
- # This module encapsulates all the create command steps.
3
- module Check
4
- class << self
5
- def execute(_global_options, options, args)
6
- settings(options)
7
- elb = login('elb')
8
- original_health_check = Skyed::AWS::ELB.get_health_check(args[0], elb)
9
- reduce_health_check(args[0], original_health_check, elb)
10
- wait_for_backend_restart(args[0], args[1], options)
11
- Skyed::AWS::ELB.set_health_check(args[0], original_health_check, elb)
12
- end
13
-
14
- def wait_for_backend_restart(elb_name, instance_name, opts)
15
- ow = settings(opts)
16
- instance_id = instance_by_name(instance_name, ow)
17
- .ec2_instance_id
18
- elb = login('elb')
19
- wait = opts[:wait_interval] || 0
20
- timeout = opts[:timeout] || 0
21
- [true, false].each do |op|
22
- wait_for_backend(elb_name, instance_id, elb, op, wait, timeout)
23
- end
24
- end
25
-
26
- def instance_by_name(name, ow)
27
- Skyed::AWS::OpsWorks
28
- .instance_by_name(name, Skyed::Settings.stack_id, ow)
29
- end
30
-
31
- def wait_for_backend(
32
- elb_name, instance_id, elb, *other_args)
33
- ok, wait, timeout = other_args
34
- time = 0
35
- until ok == Skyed::AWS::ELB.instance_ok?(elb_name, instance_id, elb) ||
36
- time >= timeout
37
- Kernel.sleep(wait)
38
- time += wait
39
- end
40
- end
41
-
42
- def reduce_health_check(elb_name, original_health_check, elb = nil)
43
- elb = login('elb') if elb.nil?
44
- new_health_check = original_health_check.clone
45
- new_health_check.timeout = 2
46
- new_health_check.interval = 5
47
- new_health_check.unhealthy_threshold = 2
48
- new_health_check.healthy_threshold = 2
49
- Skyed::AWS::ELB.set_health_check(elb_name, new_health_check, elb)
50
- end
51
-
52
- def settings(options)
53
- ow = login('ow')
54
- Skyed::Settings.stack_id = Skyed::AWS::OpsWorks.stack(
55
- options[:stack], ow)[:stack_id]
56
- Skyed::Settings.layer_id = Skyed::AWS::OpsWorks.layer(
57
- options[:layer], ow)[:layer_id]
58
- ow
59
- end
60
-
61
- def login(kind = 'elb')
62
- Skyed::Init.credentials if Skyed::Settings.empty?
63
- result = Skyed::AWS::ELB.login if kind == 'elb'
64
- result = Skyed::AWS::OpsWorks.login if kind == 'ow'
65
- result
66
- end
67
- end
68
- end
69
- end
@@ -1,174 +0,0 @@
1
- version Skyed::VERSION
2
-
3
- desc 'Initialize skyed'
4
- long_desc 'Sets up skyed configuration for a repository'
5
-
6
- command :init do |cmd|
7
- cmd.flag [:remote], default_value: nil,
8
- type: String,
9
- desc: 'Remote to use in OpsWorks'
10
- cmd.flag [:repo], default_value: '.',
11
- type: String,
12
- desc: 'OpsWorks repository location'
13
- cmd.flag [:repo_key, 'repo-key'], default_value: nil,
14
- type: String,
15
- desc: 'Key to use with repo'
16
- cmd.flag [:j, :custom_json, 'custom-json'], default_value: '',
17
- type: String,
18
- desc: 'Custom JSON to pass to OW'
19
- desc = 'Chef version to use in OpsWorks'
20
- cmd.flag [:chef_version, 'chef-version'], default_value: '11.10',
21
- type: String,
22
- desc: desc
23
- cmd.action do |global_options, options|
24
- Skyed::Init.execute(global_options, options)
25
- end
26
- end
27
-
28
- desc 'Deploy current setup'
29
- long_desc 'Deploys from current repository'
30
-
31
- command :deploy do |cmd|
32
- cmd.action do |global_options|
33
- Skyed::Deploy.execute(global_options)
34
- end
35
- end
36
-
37
- desc 'Run specific recipes on instance'
38
- long_desc 'Runs specified recipes on all running instances'
39
-
40
- stack_desc = 'Stack to which the command affects.'
41
- layer_desc = 'Layer to which the command affects.'
42
- command :run do |cmd|
43
- cmd.flag [:s, :stack], default_value: nil,
44
- type: String,
45
- desc: stack_desc
46
- cmd.flag [:l, :layer], default_value: nil,
47
- type: String,
48
- desc: layer_desc
49
- cmd.flag [:i, :instance], default_value: nil,
50
- type: String,
51
- desc: layer_desc
52
- desc = 'Time to wait for AWS responses'
53
- cmd.flag [:w, :wait_interval, 'wait-interval'], default_value: 30,
54
- type: Integer,
55
- desc: desc
56
- cmd.flag [:j, :custom_json, 'custom-json'], default_value: '',
57
- type: String,
58
- desc: 'Custom JSON to pass to OW'
59
- cmd.action do |global_options, options, args|
60
- Skyed::Run.execute(global_options, options, args)
61
- end
62
- end
63
-
64
- desc 'Destroy instance'
65
- long_desc 'Destroy instance'
66
-
67
- command :destroy do |cmd|
68
- cmd.switch [:rds], default_value: false,
69
- desc: 'Destroys RDS instance'
70
- desc = 'Final snapshot name. Ommit to skip'
71
- cmd.flag [:final_snapshot_name, 'final-snapshot-name'], default_value: '',
72
- type: String,
73
- desc: desc
74
- cmd.action do |global_options, options, args|
75
- Skyed::Destroy.execute(global_options, options, args)
76
- end
77
- end
78
-
79
- desc 'Stop instance'
80
- long_desc 'Stop instance'
81
-
82
- command :stop do |cmd|
83
- cmd.flag [:s, :stack], default_value: nil,
84
- type: String,
85
- desc: stack_desc
86
- desc = 'Time to wait for AWS responses'
87
- cmd.flag [:w, :wait_interval, 'wait-interval'], default_value: 30,
88
- type: Integer,
89
- desc: desc
90
- cmd.action do |global_options, options, args|
91
- Skyed::Stop.execute(global_options, options, args)
92
- end
93
- end
94
-
95
- desc 'Create instance'
96
- long_desc 'Create instance'
97
-
98
- start_desc = 'Avoids creation if stopped OW instance already exists'
99
- command :create do |cmd|
100
- cmd.switch [:rds], default_value: false,
101
- desc: 'Creates RDS instance'
102
- cmd.flag [:size], default_value: 100,
103
- type: Integer,
104
- desc: 'Size of the RDS instance'
105
- cmd.flag [:type], default_value: 'm1.large',
106
- type: String,
107
- desc: 'Type of the RDS instance'
108
- cmd.flag [:user], default_value: 'root',
109
- type: String,
110
- desc: 'Master user of the RDS instance'
111
- cmd.flag [:password], default_value: 'password',
112
- type: String,
113
- desc: 'Master password of the RDS instance'
114
- cmd.flag [:s, :stack], default_value: nil,
115
- type: String,
116
- desc: stack_desc
117
- cmd.flag [:l, :layer], default_value: nil,
118
- type: String,
119
- desc: layer_desc
120
- cmd.switch [:start], default_value: false,
121
- desc: start_desc
122
- desc = 'Time to wait for AWS responses'
123
- cmd.flag [:w, :wait_interval, 'wait-interval'], default_value: 30,
124
- type: Integer,
125
- desc: desc
126
- defval = 'default'
127
- desc = 'Name of the DB Security Group'
128
- cmd.flag [:db_security_group, 'db-security-group'], default_value: defval,
129
- type: String,
130
- desc: desc
131
- defval = 'default.postgres9.4'
132
- desc = 'Name of the DB Parameter Group'
133
- cmd.flag [:db_parameters_group, 'db-parameters-group'], default_value: defval,
134
- type: String,
135
- desc: desc
136
- cmd.action do |global_options, options, args|
137
- Skyed::Create.execute(global_options, options, args)
138
- end
139
- end
140
-
141
- desc 'List objects'
142
- long_desc 'List objects'
143
-
144
- command :list do |cmd|
145
- cmd.switch [:rds], default_value: false,
146
- desc: 'Lists RDS objects'
147
-
148
- cmd.action do |global_options, options, args|
149
- Skyed::List.execute(global_options, options, args)
150
- end
151
- end
152
-
153
- desc 'Check ELB'
154
- long_desc 'Check ELB'
155
-
156
- start_desc = 'Allows check ELB health state'
157
- command :check do |cmd|
158
- cmd.flag [:s, :stack], default_value: nil,
159
- type: String,
160
- desc: stack_desc
161
- cmd.flag [:l, :layer], default_value: nil,
162
- type: String,
163
- desc: layer_desc
164
- desc = 'Time to wait for AWS responses'
165
- cmd.flag [:t, :timeout, 'timeout'], default_value: 30,
166
- type: Integer,
167
- desc: 'Defines timout for the check'
168
- cmd.flag [:w, :wait_interval, 'wait-interval'], default_value: 30,
169
- type: Integer,
170
- desc: desc
171
- cmd.action do |global_options, options, args|
172
- Skyed::Check.execute(global_options, options, args)
173
- end
174
- end
@@ -1,84 +0,0 @@
1
- module Skyed
2
- # This module encapsulates all the create command steps.
3
- module Create
4
- @non_rds_options = [:rds, :start, :stack, :layer]
5
- class << self
6
- def execute(_global_options, options, args)
7
- Skyed::Init.credentials if Skyed::Settings.empty?
8
- execute_rds(options, args) if options[:rds]
9
- create_opsworks(options, args) unless options[:rds]
10
- end
11
-
12
- def start_opsworks(options, ow)
13
- stopped_instances = Skyed::AWS::OpsWorks.instances_by_status(
14
- options[:stack], options[:layer], 'stopped', ow)
15
- return false if stopped_instances.empty?
16
- Skyed::AWS::OpsWorks.start_instance(
17
- stopped_instances.first.instance_id,
18
- ow)
19
- true
20
- end
21
-
22
- def check_create_options(options)
23
- msg = 'Specify stack and layer or initialize for local management'
24
- fail msg unless options[:stack] && options[:layer]
25
- end
26
-
27
- def create_opsworks(options, _args)
28
- check_create_options(options)
29
- ow = settings(options)
30
- options[:start] = start_opsworks(options, ow) if options[:start]
31
- Skyed::AWS::OpsWorks.create_instance(
32
- Skyed::Settings.stack_id,
33
- Skyed::Settings.layer_id,
34
- options[:type],
35
- ow) unless options[:start]
36
- end
37
-
38
- def stack(ow, options)
39
- stack = Skyed::AWS::OpsWorks.stack(options[:stack], ow)
40
- msg = "There's no such stack with id #{options[:stack]}"
41
- fail msg unless stack
42
- stack[:stack_id]
43
- end
44
-
45
- def layer(ow, options)
46
- layer = Skyed::AWS::OpsWorks.layer(options[:layer], ow)
47
- msg = "There's no such layer with id #{options[:layer]}"
48
- fail msg unless layer
49
- layer[:layer_id]
50
- end
51
-
52
- def execute_rds(options, args)
53
- endpoint = create_new(options, args) if args.length == 1
54
- endpoint = restore_new(options, args) if args.length == 2
55
- puts endpoint
56
- end
57
-
58
- def restore_new(options, args)
59
- Skyed::AWS::RDS.create_instance_from_snapshot(
60
- args[0],
61
- args[1],
62
- options.select { |k| ! @non_rds_options.include? k })
63
- end
64
-
65
- def create_new(options, args)
66
- Skyed::AWS::RDS.create_instance(
67
- args[0],
68
- options.select { |k| ! @non_rds_options.include? k })
69
- end
70
-
71
- def settings(options)
72
- ow = login
73
- Skyed::Settings.stack_id = stack(ow, options)
74
- Skyed::Settings.layer_id = layer(ow, options)
75
- ow
76
- end
77
-
78
- def login
79
- Skyed::Init.credentials if Skyed::Settings.empty?
80
- Skyed::AWS::OpsWorks.login
81
- end
82
- end
83
- end
84
- end
@@ -1,20 +0,0 @@
1
- module Skyed
2
- # This module encapsulates all the deploy command steps.
3
- module Deploy
4
- class << self
5
- def execute(global_options)
6
- fail 'Not initialized, please run skyed init' if Skyed::Settings.empty?
7
- Skyed::Utils.export_credentials
8
- push_devel_branch(global_options)
9
- output = `cd #{Skyed::Settings.repo} && vagrant up`
10
- fail output unless $CHILD_STATUS.success?
11
- $CHILD_STATUS.success?
12
- end
13
-
14
- def push_devel_branch(_global_options)
15
- repo = ::Git.open(Skyed::Settings.repo)
16
- repo.push(Skyed::Settings.remote_name, Skyed::Settings.branch)
17
- end
18
- end
19
- end
20
- end
@@ -1,21 +0,0 @@
1
- module Skyed
2
- # This module encapsulates all the destroy command steps.
3
- module Destroy
4
- class << self
5
- def execute(global_options, options, args)
6
- Skyed::Init.credentials if Skyed::Settings.empty?
7
- destroy_vagrant(global_options, options, args) unless options[:rds]
8
- Skyed::AWS::RDS.destroy_instance(args[0], options) if options[:rds]
9
- end
10
-
11
- def destroy_vagrant(_global_options, _options, _args)
12
- repo_path = Skyed::Settings.repo
13
- hostname = `cd #{repo_path} && vagrant ssh -c hostname`.strip
14
- `cd #{repo_path} && vagrant destroy -f`
15
- ow = Skyed::AWS::OpsWorks.login
16
- Skyed::AWS::OpsWorks.deregister_instance hostname, ow
17
- Skyed::AWS::OpsWorks.delete_user ow
18
- end
19
- end
20
- end
21
- end
@@ -1,29 +0,0 @@
1
- require 'securerandom'
2
- require 'erb'
3
-
4
- module Skyed
5
- # This module encapsulates all the Git features.
6
- module Git
7
- class << self
8
- def clone_cmd(stack, path)
9
- cmd = 'export GIT_SSH=/tmp/ssh-git; '
10
- cmd += 'git clone --branch '
11
- cmd += stack[:custom_cookbooks_source][:revision]
12
- cmd += " #{stack[:custom_cookbooks_source][:url]} "
13
- cmd += path
14
- cmd
15
- end
16
-
17
- def clone_stack_remote(stack, options)
18
- unless Skyed::Settings.current_stack?(stack[:stack_id])
19
- Skyed::Init.opsworks_git_key options
20
- end
21
- ENV['PKEY'] ||= Skyed::Settings.opsworks_git_key
22
- Skyed::Utils.create_template('/tmp', 'ssh-git', 'ssh-git.erb', 0755)
23
- path = "/tmp/skyed.#{SecureRandom.hex}"
24
- `#{clone_cmd(stack, path)}`
25
- path
26
- end
27
- end
28
- end
29
- end
@@ -1,157 +0,0 @@
1
- require 'English'
2
- require 'fileutils'
3
- require 'erb'
4
- require 'git'
5
- require 'aws-sdk'
6
- require 'highline/import'
7
- require 'digest/sha1'
8
-
9
- module Skyed
10
- # This module encapsulates all the init command steps.
11
- module Init
12
- class << self
13
- def execute(global_options, options)
14
- fail 'Already initialized' unless Skyed::Settings.empty?
15
- Skyed::Settings.repo = repo_path(get_repo(options[:repo])).to_s
16
- branch global_options, options
17
- credentials
18
- opsworks_git_key options
19
- opsworks options
20
- vagrant
21
- Skyed::Settings.save
22
- end
23
-
24
- def opsworks(options = {})
25
- opsworks = Skyed::AWS::OpsWorks.login
26
- params = Skyed::AWS::OpsWorks.generate_params nil, options
27
- check_stack(opsworks, params[:name])
28
- Skyed::AWS::OpsWorks.create_stack(params, opsworks)
29
- params = Skyed::AWS::OpsWorks.generate_params(Skyed::Settings.stack_id)
30
- Skyed::AWS::OpsWorks.create_layer(params, opsworks)
31
- end
32
-
33
- def check_stack(ow, name)
34
- stack = Skyed::AWS::OpsWorks.stack_summary_by_name(name, ow)
35
- Skyed::AWS::OpsWorks.delete_stack(stack[:name], ow) unless stack.nil?
36
- File.delete(vagrantfile) if File.exist?(vagrantfile)
37
- end
38
-
39
- def vagrantfile
40
- File.join(Skyed::Settings.repo, 'Vagrantfile')
41
- end
42
-
43
- def create_vagrant_files
44
- provisioning_path = File.join(Skyed::Settings.repo, '.provisioning')
45
- tasks_path = File.join(provisioning_path, 'tasks')
46
- aws_path = File.join(provisioning_path, 'templates', 'aws')
47
- Skyed::Utils.create_template(Skyed::Settings.repo, 'Vagrantfile',
48
- 'Vagrantfile.erb')
49
- Skyed::Utils.create_template(tasks_path, 'ow-on-premise.yml',
50
- 'ow-on-premise.yml.erb')
51
- Skyed::Utils.create_template(aws_path, 'config.j2', 'config.j2.erb')
52
- Skyed::Utils.create_template(aws_path, 'credentials.j2',
53
- 'credentials.j2.erb')
54
- end
55
-
56
- def vagrant
57
- return if File.exist?(vagrantfile)
58
- pip_install 'ansible'
59
- create_directory(Skyed::Settings.repo, '.provisioning/templates/aws')
60
- create_directory(Skyed::Settings.repo, '.provisioning/tasks')
61
- create_vagrant_files
62
- end
63
-
64
- def create_directory(base, subpath)
65
- folders = subpath.split('/')
66
- new_dir = File.join(base, folders)
67
- FileUtils.mkdir_p(new_dir)
68
- end
69
-
70
- def pip_install(package)
71
- `pip list | grep #{package}`
72
- return if $CHILD_STATUS.success?
73
- `which pip`
74
- easy_install 'pip' unless $CHILD_STATUS.success?
75
- `sudo pip install #{package}`
76
- fail "Can't install #{package}" unless $CHILD_STATUS.success?
77
- end
78
-
79
- def easy_install(package)
80
- `easy_install #{package}`
81
- fail "Can't install #{package}" unless $CHILD_STATUS.success?
82
- end
83
-
84
- def branch(global_options, options)
85
- branch = "devel-#{Digest::SHA1.hexdigest Skyed::Settings.repo}"
86
- repo = repo?(Skyed::Settings.repo)
87
- repo.branch(branch).checkout
88
- Skyed::Settings.branch = branch
89
- remote_data = git_remote_data(repo, global_options, options)
90
- Skyed::Settings.remote_name = remote_data[:name]
91
- Skyed::Settings.remote_url = remote_data[:url]
92
- end
93
-
94
- def git_remote_data(repo, _global_options, options = {})
95
- name ||= options[:remote]
96
- name = ask_remote_name(
97
- repo.remotes.map(&:name)) if repo.remotes.length > 1 && name.nil?
98
- name = repo.remotes[0].name if name.nil?
99
- select_remote(name, repo.remotes)
100
- end
101
-
102
- def select_remote(name, remotes)
103
- url = nil
104
- remotes.each do |remote|
105
- url = remote.url if remote.name == name
106
- end
107
- if url.nil?
108
- { name: remotes[0].name, url: remotes[0].url }
109
- else
110
- { name: name, url: url }
111
- end
112
- end
113
-
114
- def ask_remote_name(remotes_names)
115
- question = 'Which remote should be used for the git repository? '
116
- ask(question + remotes_names.to_s)
117
- end
118
-
119
- def opsworks_git_key(options = nil)
120
- question = 'Which ssh key should be used for the git repository? '
121
- Skyed::Settings.opsworks_git_key = options[:repo_key] || ask(question)
122
- end
123
-
124
- def credentials(
125
- access = ENV['AWS_ACCESS_KEY'],
126
- secret = ENV['AWS_SECRET_KEY'],
127
- role_arn = ENV['OW_SERVICE_ROLE'],
128
- profile_arn = ENV['OW_INSTANCE_PROFILE'],
129
- aws_key_name = ENV['AWS_SSH_KEY_NAME'])
130
- Skyed::AWS.set_credentials(access, secret)
131
- Skyed::AWS::OpsWorks.set_arns(profile_arn, role_arn)
132
- Skyed::Settings.aws_key_name = aws_key_name
133
- end
134
-
135
- def repo_path(repo)
136
- Pathname.new(repo.repo.path).dirname
137
- end
138
-
139
- def get_repo(path = '.')
140
- question = 'Which is your CM repository? '
141
- force_ask = path == '.'
142
- repo = repo?(path)
143
- say("ERROR: #{path} is not a repository") unless repo
144
- repo = get_repo(
145
- ask(question) { |q| q.default = repo_path(repo).to_s }
146
- ) if !repo || force_ask
147
- repo
148
- end
149
-
150
- def repo?(path)
151
- ::Git.open(path)
152
- rescue ArgumentError
153
- return false
154
- end
155
- end
156
- end
157
- end