cloud_job_aws 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 +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +10 -0
- data/README.md +35 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/cloud_job_aws.gemspec +25 -0
- data/lib/cloud_job_aws.rb +159 -0
- data/lib/cloud_job_aws/version.rb +3 -0
- data/lib/cloud_storage_aws.rb +36 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: be855a13a81cf9699b98274474a522a5c4ef6cab
|
4
|
+
data.tar.gz: 0f2aa68c64a7ae3b5b317637e6a897c052a1aed4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6806fb6e8977253fb7d06ef2a412020260fa5265fedc238706b4df6a0ab13f9d5b17f908c252da6c980b8da08af70566f683b1c099560c2369a80359c39346c3
|
7
|
+
data.tar.gz: d18ed3778adbf92e1030c96978c4cbb3ec7ca46ce8060dc0fa752a9a8d42b5b345718a40d06c0469137fa3efc6abdb0d81fe21c7b24088e628bcb73f74abc6ae
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# CloudJobAws
|
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/cloud_job_aws`. 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 'cloud_job_aws'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install cloud_job_aws
|
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 test` 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]/cloud_job_aws.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cloud_job_aws"
|
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,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "cloud_job_aws/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cloud_job_aws"
|
8
|
+
spec.version = CloudJobAws::VERSION
|
9
|
+
spec.authors = ["Elena Evdokimenko"]
|
10
|
+
spec.email = ["x17102782@student.ncirl.ie"]
|
11
|
+
|
12
|
+
spec.summary = "Implementation of cloud_job_base gem for aws"
|
13
|
+
spec.description = "Implementation of cloud_job_base gem for aws"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require "cloud_job_aws/version"
|
2
|
+
require "cloud_job_base"
|
3
|
+
require "aws-sdk"
|
4
|
+
require "net/ssh"
|
5
|
+
require "logger"
|
6
|
+
|
7
|
+
module CloudJobAws
|
8
|
+
|
9
|
+
class AwsConnector < CloudJobBase::CloudConnector
|
10
|
+
attr_reader :ec2client, :ec2resource, :security_group, :key_pair
|
11
|
+
|
12
|
+
def set_log_level(log_level)
|
13
|
+
Aws.config.update({log_level: log_level})
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_region(region)
|
17
|
+
@region = region
|
18
|
+
end
|
19
|
+
|
20
|
+
def check_credentials
|
21
|
+
raise "Key not set." if ENV['AWS_ACCESS_KEY_ID'].nil?
|
22
|
+
@key = ENV['AWS_ACCESS_KEY_ID']
|
23
|
+
raise "Secret not set." if ENV['AWS_SECRET_ACCESS_KEY'].nil?
|
24
|
+
@secret = ENV['AWS_SECRET_ACCESS_KEY']
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_client
|
28
|
+
# credentials aren't mandatory if they are set in ENV, though having it here for extendability
|
29
|
+
@ec2client = Aws::EC2::Client.new(region: @region, credentials: Aws::Credentials.new(@key, @secret))
|
30
|
+
@ec2resource = Aws::EC2::Resource.new(client: @ec2client)
|
31
|
+
create_security_group
|
32
|
+
create_key_pair
|
33
|
+
end
|
34
|
+
|
35
|
+
def create_security_group
|
36
|
+
begin
|
37
|
+
@security_group = @ec2resource.create_security_group({ group_name: "SSH #{SecureRandom.uuid}", description: "ssh" })
|
38
|
+
puts @security_group.inspect
|
39
|
+
# allow port for ssh
|
40
|
+
@security_group.authorize_ingress({ip_permissions: [{ ip_protocol: 'tcp', from_port: 22, to_port: 22, ip_ranges: [{ cidr_ip: '0.0.0.0/0'}] }] })
|
41
|
+
rescue => ex
|
42
|
+
puts ex.message
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_key_pair
|
47
|
+
@key_pair = @ec2resource.create_key_pair({ key_name: "key-pair #{SecureRandom.uuid}"})
|
48
|
+
puts @key_pair.inspect
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class AwsVmHandler < CloudJobBase::VmHandler
|
53
|
+
def provision
|
54
|
+
begin
|
55
|
+
allowed_types = ["t2.micro", "t1.micro"]
|
56
|
+
raise ArgumentError, "AWS VM type not supported." if !(allowed_types.include?(@vm.series))
|
57
|
+
|
58
|
+
# free tier AMI for Ireland Microsoft Windows Server 2008 R2 Base - ami-a7ed50de
|
59
|
+
# free tier AMI for Amazon Linux AMI 2017.09.1 (HVM), SSD Volume Type - ami-1a962263
|
60
|
+
@vm.image == "" ? image = "ami-1a962263" : image = @vm.image
|
61
|
+
|
62
|
+
instance = @cloudConnector.ec2resource.create_instances({ instance_type: @vm.series, image_id: image, min_count: 1, max_count: 1, key_name: @cloudConnector.key_pair.key_name, security_group_ids: [@cloudConnector.security_group.id]})
|
63
|
+
|
64
|
+
@cloudConnector.ec2client.wait_until(:instance_status_ok, {instance_ids: [instance[0].id]})
|
65
|
+
@vm.instance_id = instance[0].id
|
66
|
+
|
67
|
+
# set the observable's field
|
68
|
+
@status = :ok
|
69
|
+
rescue => ex # aws exceptions are subclasses of StandardError, thus must be caught by this rescue
|
70
|
+
puts "provision request failed"
|
71
|
+
# observer's fields:
|
72
|
+
@exception = ex
|
73
|
+
@status = :failed
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def deploy_exe(exe_bucket_key)
|
78
|
+
puts "In aws deploy_exe"
|
79
|
+
begin
|
80
|
+
instance = @cloudConnector.ec2resource.instance(@vm.instance_id)
|
81
|
+
if (instance.exists?)
|
82
|
+
# start VM if it's not in pending/started state
|
83
|
+
puts instance.state.code
|
84
|
+
case instance.state.code
|
85
|
+
when 0 # pending
|
86
|
+
instance.wait_until_started
|
87
|
+
when 16 # started - do nothing
|
88
|
+
puts "VM already started."
|
89
|
+
else
|
90
|
+
puts "start request sent"
|
91
|
+
instance.start
|
92
|
+
instance.wait_until_started
|
93
|
+
end
|
94
|
+
|
95
|
+
# deploy exe
|
96
|
+
@copied_exe_name = :copied_exe_name
|
97
|
+
Net::SSH.start(instance.public_dns_name, "ec2-user", key_data: [ @cloudConnector.key_pair.key_material ], verbose: :info) do |ssh|
|
98
|
+
result = ssh.exec!("wget #{exe_bucket_key}")
|
99
|
+
puts result
|
100
|
+
result = ssh.exec!("wget https:#{exe_bucket_key}")
|
101
|
+
puts result
|
102
|
+
result = ssh.exec!("wget -O #{@copied_exe_name} http:#{exe_bucket_key}")
|
103
|
+
puts result
|
104
|
+
result = ssh.exec!("ls -l")
|
105
|
+
puts result
|
106
|
+
end
|
107
|
+
|
108
|
+
@status = :ok
|
109
|
+
else
|
110
|
+
raise "VM doesn't exist."
|
111
|
+
end
|
112
|
+
rescue => ex
|
113
|
+
puts "run exe request failed"
|
114
|
+
@exception = ex
|
115
|
+
@status = :failed
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def run_exe(exe_bucket_key)
|
120
|
+
puts "In aws run_exe"
|
121
|
+
begin
|
122
|
+
instance = @cloudConnector.ec2resource.instance(@vm.instance_id)
|
123
|
+
if (instance.exists?)
|
124
|
+
# run exe
|
125
|
+
Net::SSH.start(instance.public_dns_name, "ec2-user", key_data: [ @cloudConnector.key_pair.key_material ], verbose: :info) do |ssh|
|
126
|
+
result = ssh.exec!("#{@copied_exe_name}")
|
127
|
+
puts result
|
128
|
+
end
|
129
|
+
|
130
|
+
@status = :ok
|
131
|
+
else
|
132
|
+
raise "VM doesn't exist."
|
133
|
+
end
|
134
|
+
rescue => ex
|
135
|
+
puts "run exe request failed"
|
136
|
+
@exception = ex
|
137
|
+
@status = :failed
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def delete
|
142
|
+
begin
|
143
|
+
instance = @cloudConnector.ec2resource.instance(@vm.instance_id)
|
144
|
+
if (instance.exists? and instance.state.code != 48)
|
145
|
+
puts "terminate request sent"
|
146
|
+
instance.terminate
|
147
|
+
instance.wait_until_terminated
|
148
|
+
@status = :ok
|
149
|
+
end
|
150
|
+
rescue => ex
|
151
|
+
puts "terminate request failed"
|
152
|
+
@exception = ex
|
153
|
+
@status = :failed
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "cloud_job_base"
|
2
|
+
require "cloud_storage_handler"
|
3
|
+
require "aws-sdk"
|
4
|
+
|
5
|
+
module CloudJobAws
|
6
|
+
|
7
|
+
class AwsInternalStorageHandler < CloudJobBase::InternalStorageHandler
|
8
|
+
|
9
|
+
def init_connection
|
10
|
+
raise "Key not set." if ENV['AWS_ACCESS_KEY_ID'].nil?
|
11
|
+
@key = ENV['AWS_ACCESS_KEY_ID']
|
12
|
+
raise "Secret not set." if ENV['AWS_SECRET_ACCESS_KEY'].nil?
|
13
|
+
@secret = ENV['AWS_SECRET_ACCESS_KEY']
|
14
|
+
|
15
|
+
@s3_default = Aws::S3::Resource.new(region: "eu-west-1", credentials: Aws::Credentials.new(@key, @secret))
|
16
|
+
@default_bucket = @s3_default.bucket("user-exe-files")
|
17
|
+
end
|
18
|
+
|
19
|
+
def put_file(file_path)
|
20
|
+
name = File.basename(file_path)
|
21
|
+
object = @default_bucket.object(name)
|
22
|
+
object.upload_file(file_path)
|
23
|
+
return object.key
|
24
|
+
end
|
25
|
+
|
26
|
+
def read_file(file_url)
|
27
|
+
@default_bucket.get(file_url)
|
28
|
+
end
|
29
|
+
|
30
|
+
def presigned_post(bucket_key)
|
31
|
+
@default_bucket.presigned_post(key: bucket_key, success_action_status: '201', acl: 'public-read')
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloud_job_aws
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elena Evdokimenko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Implementation of cloud_job_base gem for aws
|
56
|
+
email:
|
57
|
+
- x17102782@student.ncirl.ie
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- cloud_job_aws.gemspec
|
70
|
+
- lib/cloud_job_aws.rb
|
71
|
+
- lib/cloud_job_aws/version.rb
|
72
|
+
- lib/cloud_storage_aws.rb
|
73
|
+
homepage:
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.6.8
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Implementation of cloud_job_base gem for aws
|
96
|
+
test_files: []
|