Class: CloudJobAws::AwsVmHandler

Inherits:
CloudJobBase::VmHandler
  • Object
show all
Defined in:
lib/cloud_job_aws.rb

Instance Method Summary collapse

Instance Method Details

#deleteObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cloud_job_aws.rb', line 141

def delete
    begin
        instance = @cloudConnector.ec2resource.instance(@vm.instance_id)
        if (instance.exists? and instance.state.code != 48)
            puts "terminate request sent"
            instance.terminate
            instance.wait_until_terminated
            @status = :ok
        end
    rescue => ex
        puts "terminate request failed"
        @exception = ex
        @status =  :failed
    end
end

#deploy_exe(exe_bucket_key) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cloud_job_aws.rb', line 77

def deploy_exe(exe_bucket_key)
    puts "In aws deploy_exe"
    begin
        instance = @cloudConnector.ec2resource.instance(@vm.instance_id)
        if (instance.exists?)
            # start VM if it's not in pending/started state
            puts instance.state.code
            case instance.state.code
            when 0 # pending
                instance.wait_until_started
            when 16 # started - do nothing
                puts "VM already started."
            else    
                puts "start request sent"
                instance.start
                instance.wait_until_started
            end
            
            # deploy exe
            @copied_exe_name = :copied_exe_name
            Net::SSH.start(instance.public_dns_name, "ec2-user", key_data: [ @cloudConnector.key_pair.key_material ], verbose: :info) do |ssh|
                result = ssh.exec!("wget #{exe_bucket_key}")
                puts result
                result = ssh.exec!("wget https:#{exe_bucket_key}")
                puts result
                result = ssh.exec!("wget -O #{@copied_exe_name} http:#{exe_bucket_key}")
                puts result
                result = ssh.exec!("ls -l")
                puts result
            end
            
            @status = :ok
        else
            raise "VM doesn't exist."
        end
    rescue => ex
        puts "run exe request failed"
        @exception = ex
        @status = :failed
    end
end

#provisionObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cloud_job_aws.rb', line 53

def provision
    begin
        allowed_types = ["t2.micro", "t1.micro"]
        raise ArgumentError, "AWS VM type not supported." if !(allowed_types.include?(@vm.series))
        
        # free tier AMI for Ireland Microsoft Windows Server 2008 R2 Base - ami-a7ed50de
        # free tier AMI for Amazon Linux AMI 2017.09.1 (HVM), SSD Volume Type - ami-1a962263
        @vm.image == "" ? image = "ami-1a962263" : image = @vm.image
        
        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]})
        
        @cloudConnector.ec2client.wait_until(:instance_status_ok, {instance_ids: [instance[0].id]})
        @vm.instance_id = instance[0].id
        
        # set the observable's field
        @status = :ok
    rescue => ex # aws exceptions are subclasses of StandardError, thus must be caught by this rescue
        puts "provision request failed"
        # observer's fields:
        @exception = ex
        @status = :failed
    end
end

#run_exe(exe_bucket_key) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cloud_job_aws.rb', line 119

def run_exe(exe_bucket_key)
    puts "In aws run_exe"
    begin
        instance = @cloudConnector.ec2resource.instance(@vm.instance_id)
        if (instance.exists?)
            # run exe
            Net::SSH.start(instance.public_dns_name, "ec2-user", key_data: [ @cloudConnector.key_pair.key_material ], verbose: :info) do |ssh|
                result = ssh.exec!("#{@copied_exe_name}")
                puts result
            end

            @status = :ok
        else
            raise "VM doesn't exist."
        end
    rescue => ex
        puts "run exe request failed"
        @exception = ex
        @status = :failed
    end
end